fixing issue with pings
This commit is contained in:
1 parent
41eb568610
commit
c42d256454
2 files changed
+30
-38
No files matched your search
@@ -0,0 +1,30 @@
|
||||
async function ping(url: string, multiplier = 1, signal?: AbortSignal): Promise<number> {
|
||||
const address = url.trim();
|
||||
const target = new URL(/^[a-z][a-z\d+.-]*:\/\//i.test(address) ? address : `http://${address}`);
|
||||
if (target.protocol !== 'http:' && target.protocol !== 'https:') {
|
||||
throw new Error('Server addresses must use HTTP or HTTPS.');
|
||||
}
|
||||
|
||||
const controller = new AbortController();
|
||||
const abort = () => controller.abort();
|
||||
if (signal?.aborted) abort();
|
||||
signal?.addEventListener('abort', abort, { once: true });
|
||||
const timeout = setTimeout(abort, 5000);
|
||||
const start = performance.now();
|
||||
|
||||
try {
|
||||
await fetch(target.href, {
|
||||
method: 'HEAD',
|
||||
mode: 'no-cors',
|
||||
cache: 'no-store',
|
||||
credentials: 'omit',
|
||||
signal: controller.signal,
|
||||
});
|
||||
return (performance.now() - start) * multiplier;
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
signal?.removeEventListener('abort', abort);
|
||||
}
|
||||
}
|
||||
|
||||
export default ping;
|
||||
Reference in new issue
Block a user