whoispk 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +29 -17
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -43,12 +43,20 @@ export function normalizeDomain(input) {
|
|
|
43
43
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
44
44
|
|
|
45
45
|
async function request(domain, { endpoint, timeout, signal, fetch: fetchImpl }) {
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
// A regular (ref'd) timer rather than AbortSignal.timeout(): the latter doesn't keep the
|
|
47
|
+
// process alive, so a stalled request could let Node exit before the timeout ever fires.
|
|
48
|
+
const controller = new AbortController();
|
|
49
|
+
let timedOut = false;
|
|
50
|
+
const timer = setTimeout(() => {
|
|
51
|
+
timedOut = true;
|
|
52
|
+
controller.abort(new DOMException(`Timed out after ${timeout} ms`, 'TimeoutError'));
|
|
53
|
+
}, timeout);
|
|
54
|
+
const forwardAbort = () => controller.abort(signal.reason);
|
|
55
|
+
if (signal?.aborted) forwardAbort();
|
|
56
|
+
else signal?.addEventListener('abort', forwardAbort, { once: true });
|
|
48
57
|
|
|
49
|
-
let response;
|
|
50
58
|
try {
|
|
51
|
-
response = await fetchImpl(endpoint, {
|
|
59
|
+
const response = await fetchImpl(endpoint, {
|
|
52
60
|
method: 'POST',
|
|
53
61
|
headers: {
|
|
54
62
|
'content-type': 'application/x-www-form-urlencoded',
|
|
@@ -56,29 +64,33 @@ async function request(domain, { endpoint, timeout, signal, fetch: fetchImpl })
|
|
|
56
64
|
'user-agent': USER_AGENT,
|
|
57
65
|
},
|
|
58
66
|
body: new URLSearchParams({ name: domain }).toString(),
|
|
59
|
-
signal:
|
|
67
|
+
signal: controller.signal,
|
|
60
68
|
});
|
|
69
|
+
|
|
70
|
+
if (!response.ok) {
|
|
71
|
+
throw new WhoisPkError(`PKNIC responded with HTTP ${response.status}`, {
|
|
72
|
+
code: 'HTTP_ERROR',
|
|
73
|
+
status: response.status,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return await response.text();
|
|
61
78
|
} catch (err) {
|
|
79
|
+
if (err instanceof WhoisPkError) throw err;
|
|
80
|
+
if (timedOut) {
|
|
81
|
+
throw new WhoisPkError(`PKNIC did not respond within ${timeout} ms`, { code: 'TIMEOUT', cause: err });
|
|
82
|
+
}
|
|
62
83
|
if (signal?.aborted) {
|
|
63
84
|
throw new WhoisPkError('Lookup aborted', { code: 'ABORTED', cause: err });
|
|
64
85
|
}
|
|
65
|
-
if (err?.name === 'TimeoutError') {
|
|
66
|
-
throw new WhoisPkError(`PKNIC did not respond within ${timeout} ms`, { code: 'TIMEOUT', cause: err });
|
|
67
|
-
}
|
|
68
86
|
throw new WhoisPkError(`Could not reach PKNIC: ${err?.cause?.message ?? err?.message ?? err}`, {
|
|
69
87
|
code: 'NETWORK_ERROR',
|
|
70
88
|
cause: err,
|
|
71
89
|
});
|
|
90
|
+
} finally {
|
|
91
|
+
clearTimeout(timer);
|
|
92
|
+
signal?.removeEventListener('abort', forwardAbort);
|
|
72
93
|
}
|
|
73
|
-
|
|
74
|
-
if (!response.ok) {
|
|
75
|
-
throw new WhoisPkError(`PKNIC responded with HTTP ${response.status}`, {
|
|
76
|
-
code: 'HTTP_ERROR',
|
|
77
|
-
status: response.status,
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return response.text();
|
|
82
94
|
}
|
|
83
95
|
|
|
84
96
|
/**
|