Abort Controller for whois

This commit is contained in:
Raven Scott
2026-04-27 21:04:34 -04:00
parent 60e2efa64a
commit 93742db1cc
9 changed files with 81 additions and 24 deletions
+25 -6
View File
@@ -165,13 +165,32 @@ function isLikelyDomain(s) {
}
async function fetchJson(fetchFn, url, timeoutMs) {
const ac = new AbortController()
const tid = setTimeout(() => ac.abort(), timeoutMs)
const AC =
typeof globalThis.AbortController === 'function'
? globalThis.AbortController
: null
let res
try {
res = await fetchFn(url, { method: 'GET', signal: ac.signal })
} finally {
clearTimeout(tid)
if (AC) {
const ac = new AC()
const tid = setTimeout(() => ac.abort(), timeoutMs)
try {
res = await fetchFn(url, { method: 'GET', signal: ac.signal })
} finally {
clearTimeout(tid)
}
} else {
/** Minimal hosts may lack AbortController; race fetch against a timer (does not cancel TCP). */
let tid
const abortErr = Object.assign(new Error('Aborted'), { name: 'AbortError' })
const deadline = new Promise((_, reject) => {
tid = setTimeout(() => reject(abortErr), timeoutMs)
})
try {
res = await Promise.race([fetchFn(url, { method: 'GET' }), deadline])
} finally {
if (tid) clearTimeout(tid)
}
}
if (!res || !res.ok) {
const status = res ? `${res.status} ${res.statusText || ''}`.trim() : 'unknown'