Skip to content

Commit

Permalink
feat!: status indicator operation for all url types (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
hywax authored Apr 14, 2024
1 parent fcbaecf commit 7b64f07
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -53,4 +54,4 @@
"lint-staged": {
"*.ts": "yarn run lint"
}
}
}
23 changes: 10 additions & 13 deletions src/server/utils/services.ts
Original file line number Diff line number Diff line change
@@ -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<PingServiceData> {
export async function pingService(endpoint: string): Promise<PingServiceData> {
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)
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]":
version "2.1.5"
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 7b64f07

Please sign in to comment.