diff --git a/package.json b/package.json index 61c6799..27d133c 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "docs:preview": "vitepress preview docs" }, "dependencies": { + "@network-utils/tcp-ping": "^1.2.3", "@vueuse/nuxt": "^10.9.0", "consola": "^3.2.3", "defu": "^6.1.4", @@ -53,4 +54,4 @@ "lint-staged": { "*.ts": "yarn run lint" } -} \ No newline at end of file +} diff --git a/src/server/utils/services.ts b/src/server/utils/services.ts index b18c5b8..0ff44d3 100644 --- a/src/server/utils/services.ts +++ b/src/server/utils/services.ts @@ -1,23 +1,20 @@ +import { ping } from '@network-utils/tcp-ping' import type { H3Event } from 'h3' import type { PingServiceData, ReturnServiceWithData, Service, ServiceWithDefaultData } from '~/types' -import { isUrl } from '~/utils/validation' -export async function pingService(url: string): Promise { +export async function pingService(endpoint: string): Promise { try { - if (!isUrl(url)) { - throw createError({ - statusCode: 400, - statusMessage: 'URL is not valid', - }) - } + const url = new URL(endpoint) - const startTime = new Date().getTime() - await $fetch(url, { timeout: 15000 }) - const endTime = new Date().getTime() + const probe = await ping({ + address: url.hostname, + port: Number.parseInt(url.port || '80'), + attempts: 1, + }) return { - status: true, - time: endTime - startTime, + status: probe.errors.length === 0, + time: Math.floor(probe.averageLatency), } } catch (e) { logger.error(e) diff --git a/yarn.lock b/yarn.lock index 9ded104..e9c5385 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1872,6 +1872,11 @@ "@netlify/node-cookies" "^0.1.0" urlpattern-polyfill "8.0.2" +"@network-utils/tcp-ping@^1.2.3": + version "1.2.3" + resolved "https://registry.yarnpkg.com/@network-utils/tcp-ping/-/tcp-ping-1.2.3.tgz#a67cf2afd40f0272daf83f48ed4796016dc3974e" + integrity sha512-YKSnfvKGabggw+r6xVk9mxF5AdvDSL0pXX2EydkaBpIspcOokK7HsG7N+i94eaakDnlVFxN2EcXEh0UYOSUKvw== + "@nodelib/fs.scandir@2.1.5": version "2.1.5" resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5" @@ -6262,11 +6267,6 @@ is-typed-array@^1.1.13: dependencies: which-typed-array "^1.1.14" -is-unicode-supported@^1.3.0: - version "1.3.0" - resolved "https://registry.yarnpkg.com/is-unicode-supported/-/is-unicode-supported-1.3.0.tgz#d824984b616c292a2e198207d4a609983842f714" - integrity sha512-43r2mRvz+8JRIKnWJ+3j8JtjRKZ6GmjzfaE/qiBJnikNnYv/6bagRJ1kUhNk8R5EX/GkobD+r+sfxCPJsiKBLQ== - is-weakref@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/is-weakref/-/is-weakref-1.0.2.tgz#9529f383a9338205e89765e0392efc2f100f06f2"