Skip to content

Commit 7b64f07

Browse files
author
Alexander
authored
feat!: status indicator operation for all url types (#87)
1 parent fcbaecf commit 7b64f07

File tree

3 files changed

+17
-19
lines changed

3 files changed

+17
-19
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"docs:preview": "vitepress preview docs"
1919
},
2020
"dependencies": {
21+
"@network-utils/tcp-ping": "^1.2.3",
2122
"@vueuse/nuxt": "^10.9.0",
2223
"consola": "^3.2.3",
2324
"defu": "^6.1.4",
@@ -53,4 +54,4 @@
5354
"lint-staged": {
5455
"*.ts": "yarn run lint"
5556
}
56-
}
57+
}

src/server/utils/services.ts

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1+
import { ping } from '@network-utils/tcp-ping'
12
import type { H3Event } from 'h3'
23
import type { PingServiceData, ReturnServiceWithData, Service, ServiceWithDefaultData } from '~/types'
3-
import { isUrl } from '~/utils/validation'
44

5-
export async function pingService(url: string): Promise<PingServiceData> {
5+
export async function pingService(endpoint: string): Promise<PingServiceData> {
66
try {
7-
if (!isUrl(url)) {
8-
throw createError({
9-
statusCode: 400,
10-
statusMessage: 'URL is not valid',
11-
})
12-
}
7+
const url = new URL(endpoint)
138

14-
const startTime = new Date().getTime()
15-
await $fetch(url, { timeout: 15000 })
16-
const endTime = new Date().getTime()
9+
const probe = await ping({
10+
address: url.hostname,
11+
port: Number.parseInt(url.port || '80'),
12+
attempts: 1,
13+
})
1714

1815
return {
19-
status: true,
20-
time: endTime - startTime,
16+
status: probe.errors.length === 0,
17+
time: Math.floor(probe.averageLatency),
2118
}
2219
} catch (e) {
2320
logger.error(e)

yarn.lock

+5-5
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,11 @@
18721872
"@netlify/node-cookies" "^0.1.0"
18731873
urlpattern-polyfill "8.0.2"
18741874

1875+
"@network-utils/tcp-ping@^1.2.3":
1876+
version "1.2.3"
1877+
resolved "https://registry.yarnpkg.com/@network-utils/tcp-ping/-/tcp-ping-1.2.3.tgz#a67cf2afd40f0272daf83f48ed4796016dc3974e"
1878+
integrity sha512-YKSnfvKGabggw+r6xVk9mxF5AdvDSL0pXX2EydkaBpIspcOokK7HsG7N+i94eaakDnlVFxN2EcXEh0UYOSUKvw==
1879+
18751880
"@nodelib/[email protected]":
18761881
version "2.1.5"
18771882
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:
62626267
dependencies:
62636268
which-typed-array "^1.1.14"
62646269

6265-
is-unicode-supported@^1.3.0:
6266-
version "1.3.0"
6267-
resolved "https://registry.yarnpkg.com/is-unicode-supported/-/is-unicode-supported-1.3.0.tgz#d824984b616c292a2e198207d4a609983842f714"
6268-
integrity sha512-43r2mRvz+8JRIKnWJ+3j8JtjRKZ6GmjzfaE/qiBJnikNnYv/6bagRJ1kUhNk8R5EX/GkobD+r+sfxCPJsiKBLQ==
6269-
62706270
is-weakref@^1.0.2:
62716271
version "1.0.2"
62726272
resolved "https://registry.yarnpkg.com/is-weakref/-/is-weakref-1.0.2.tgz#9529f383a9338205e89765e0392efc2f100f06f2"

0 commit comments

Comments
 (0)