@nxtedition/nxt-undici 7.4.1 → 7.4.2
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/lib/index.d.ts +36 -8
- package/lib/index.js +1 -0
- package/lib/interceptor/cache.js +79 -40
- package/lib/interceptor/dns.js +73 -6
- package/lib/interceptor/log.js +184 -9
- package/lib/interceptor/pressure.js +11 -0
- package/lib/interceptor/proxy.js +42 -14
- package/lib/interceptor/redirect.js +35 -5
- package/lib/interceptor/request-body-factory.js +59 -8
- package/lib/interceptor/response-error.js +19 -2
- package/lib/interceptor/response-retry.js +273 -21
- package/lib/request.js +5 -1
- package/lib/sqlite-cache-store.js +86 -9
- package/lib/utils.js +38 -15
- package/package.json +1 -1
|
@@ -1,9 +1,96 @@
|
|
|
1
1
|
import assert from 'node:assert'
|
|
2
2
|
import tp from 'node:timers/promises'
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
DecoratorHandler,
|
|
5
|
+
isDisturbed,
|
|
6
|
+
decorateError,
|
|
7
|
+
parseContentRange,
|
|
8
|
+
parseHeaders,
|
|
9
|
+
} from '../utils.js'
|
|
10
|
+
import { RequestAbortedError } from '../errors.js'
|
|
11
|
+
|
|
12
|
+
// Maximum number of >= 400 response body bytes buffered for replay in case
|
|
13
|
+
// the response is not retried. Larger bodies are passed straight through and
|
|
14
|
+
// the response is not status-code retried.
|
|
15
|
+
const MAX_ERROR_BODY_SIZE = 256 * 1024
|
|
4
16
|
|
|
5
17
|
function noop() {}
|
|
6
18
|
|
|
19
|
+
// Subscribe `onAbort` to an EventEmitter-style OR EventTarget-style signal and
|
|
20
|
+
// return an unsubscribe function; return null when it cannot be done safely.
|
|
21
|
+
//
|
|
22
|
+
// The caller (both sleep() and #backoff) must be able to unsubscribe once the
|
|
23
|
+
// wait settles, so we require a MATCHING subscribe/unsubscribe PAIR up front:
|
|
24
|
+
// subscribing with `on` but no `removeListener`/`off`, or `addEventListener`
|
|
25
|
+
// but no `removeEventListener`, would crash later when we try to remove the
|
|
26
|
+
// listener. request() validates signals up front (lib/request.js throws
|
|
27
|
+
// InvalidArgumentError for anything without .on/.addEventListener), so garbage
|
|
28
|
+
// only reaches here through a raw compose()/dispatch() caller — for that case
|
|
29
|
+
// return null so the caller degrades to a plain timer instead of crashing.
|
|
30
|
+
// A throwing subscribe is treated the same way.
|
|
31
|
+
function subscribeAbort(signal, onAbort) {
|
|
32
|
+
const isEventTarget =
|
|
33
|
+
typeof signal.addEventListener === 'function' &&
|
|
34
|
+
typeof signal.removeEventListener === 'function'
|
|
35
|
+
const removeEmitterListener =
|
|
36
|
+
typeof signal.removeListener === 'function' ? signal.removeListener : signal.off
|
|
37
|
+
const isEmitter = typeof signal.on === 'function' && typeof removeEmitterListener === 'function'
|
|
38
|
+
|
|
39
|
+
if (!isEventTarget && !isEmitter) {
|
|
40
|
+
return null
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
if (isEventTarget) {
|
|
45
|
+
signal.addEventListener('abort', onAbort)
|
|
46
|
+
return () => signal.removeEventListener('abort', onAbort)
|
|
47
|
+
}
|
|
48
|
+
signal.on('abort', onAbort)
|
|
49
|
+
return () => removeEmitterListener.call(signal, 'abort', onAbort)
|
|
50
|
+
} catch {
|
|
51
|
+
// A throwing subscribe leaves nothing to unsubscribe.
|
|
52
|
+
return null
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// timers/promises.setTimeout only accepts a real AbortSignal and throws
|
|
57
|
+
// ERR_INVALID_ARG_TYPE for anything else — but the library also accepts
|
|
58
|
+
// EventEmitter-style abort signals (see RequestHandler in lib/request.js).
|
|
59
|
+
// For those, race the backoff timer against the 'abort' event instead of
|
|
60
|
+
// passing the signal through. When no complete subscribe/unsubscribe pair is
|
|
61
|
+
// available (a raw dispatch() caller passing a bare object), fall back to a
|
|
62
|
+
// plain timer rather than crash.
|
|
63
|
+
function sleep(delay, signal) {
|
|
64
|
+
if (signal == null || signal instanceof AbortSignal) {
|
|
65
|
+
return tp.setTimeout(delay, true, { signal: signal ?? undefined })
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (signal.aborted) {
|
|
69
|
+
return Promise.reject(signal.reason ?? new RequestAbortedError())
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return new Promise((resolve, reject) => {
|
|
73
|
+
let removeAbortListener = noop
|
|
74
|
+
|
|
75
|
+
const timer = setTimeout(() => {
|
|
76
|
+
removeAbortListener()
|
|
77
|
+
resolve(true)
|
|
78
|
+
}, delay)
|
|
79
|
+
|
|
80
|
+
const onAbort = () => {
|
|
81
|
+
clearTimeout(timer)
|
|
82
|
+
removeAbortListener()
|
|
83
|
+
reject(signal.reason ?? new RequestAbortedError())
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Decide the branch up front by verifying a matching add/remove pair;
|
|
87
|
+
// without one there is nothing to safely race against, so the already
|
|
88
|
+
// running plain timer completes the sleep. A throwing subscribe leaves
|
|
89
|
+
// removeAbortListener as noop, and the timer still resolves.
|
|
90
|
+
removeAbortListener = subscribeAbort(signal, onAbort) ?? noop
|
|
91
|
+
})
|
|
92
|
+
}
|
|
93
|
+
|
|
7
94
|
class Handler extends DecoratorHandler {
|
|
8
95
|
#dispatch
|
|
9
96
|
#opts
|
|
@@ -23,6 +110,7 @@ class Handler extends DecoratorHandler {
|
|
|
23
110
|
#aborted = false
|
|
24
111
|
#reason
|
|
25
112
|
#resume
|
|
113
|
+
#retryAbortController = null
|
|
26
114
|
|
|
27
115
|
#pos
|
|
28
116
|
#end
|
|
@@ -60,11 +148,17 @@ class Handler extends DecoratorHandler {
|
|
|
60
148
|
super.onConnect((reason) => {
|
|
61
149
|
if (!this.#aborted) {
|
|
62
150
|
this.#aborted = true
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
151
|
+
// Always remember the reason: a downstream abort can land during the
|
|
152
|
+
// backoff wait between attempts, when #abort still points at the
|
|
153
|
+
// finished attempt's abort which undici has made a no-op. Without
|
|
154
|
+
// the recorded reason there is nothing to deliver once the retry
|
|
155
|
+
// machinery observes #aborted. Mirrors redirect.js.
|
|
156
|
+
this.#reason = reason
|
|
157
|
+
this.#abort?.(reason)
|
|
158
|
+
// Wake a pending backoff wait so the terminal onError is delivered
|
|
159
|
+
// promptly and the ref'd retry timer (up to 60s for retry-after)
|
|
160
|
+
// does not hold the event loop.
|
|
161
|
+
this.#retryAbortController?.abort(reason)
|
|
68
162
|
}
|
|
69
163
|
})
|
|
70
164
|
}
|
|
@@ -121,18 +215,33 @@ class Handler extends DecoratorHandler {
|
|
|
121
215
|
this.#end = contentLength
|
|
122
216
|
this.#etag = headers.etag
|
|
123
217
|
} else if (statusCode >= 400) {
|
|
218
|
+
if (
|
|
219
|
+
contentLength != null &&
|
|
220
|
+
contentLength > MAX_ERROR_BODY_SIZE &&
|
|
221
|
+
this.#opts.method !== 'HEAD'
|
|
222
|
+
) {
|
|
223
|
+
// The error body is too large to buffer for a replay if the retry
|
|
224
|
+
// is declined. Pass it straight through instead of buffering —
|
|
225
|
+
// such a response is simply not status-code retried.
|
|
226
|
+
this.#headersSent = true
|
|
227
|
+
return super.onHeaders(statusCode, headers, resume)
|
|
228
|
+
}
|
|
229
|
+
|
|
124
230
|
this.#body = []
|
|
125
231
|
this.#bodySize = 0
|
|
232
|
+
this.#resume = resume
|
|
126
233
|
return true
|
|
127
234
|
} else {
|
|
128
235
|
this.#headersSent = true
|
|
129
236
|
return super.onHeaders(statusCode, headers, resume)
|
|
130
237
|
}
|
|
131
238
|
|
|
239
|
+
// A duplicated etag response header arrives as an array, which has no
|
|
240
|
+
// startsWith and cannot be used for resumption — treat it as absent.
|
|
132
241
|
// Weak etags are not useful for comparison nor cache
|
|
133
242
|
// for instance not safe to assume if the response is byte-per-byte
|
|
134
243
|
// equal
|
|
135
|
-
if (this.#etag != null && this.#etag.startsWith('W/')) {
|
|
244
|
+
if (this.#etag != null && (typeof this.#etag !== 'string' || this.#etag.startsWith('W/'))) {
|
|
136
245
|
this.#etag = null
|
|
137
246
|
}
|
|
138
247
|
|
|
@@ -158,6 +267,17 @@ class Handler extends DecoratorHandler {
|
|
|
158
267
|
// permits ignoring Range; if-match still guards against changed
|
|
159
268
|
// content via a 412.) Without this, a legal full 200 retry was
|
|
160
269
|
// rejected with "Response retry failed".
|
|
270
|
+
//
|
|
271
|
+
// The server restarted the response from scratch, so the previous
|
|
272
|
+
// attempt's resume metadata no longer describes what we're receiving:
|
|
273
|
+
// refresh #end/#etag from THIS response's headers, otherwise a second
|
|
274
|
+
// failure would resume against the stale content-length/etag.
|
|
275
|
+
const contentLength = headers['content-length'] ? Number(headers['content-length']) : null
|
|
276
|
+
this.#end = Number.isFinite(contentLength) ? contentLength : null
|
|
277
|
+
// Same guard as the first-response path: only strong (non-weak),
|
|
278
|
+
// scalar etags are safe to use for resume validation.
|
|
279
|
+
this.#etag =
|
|
280
|
+
typeof headers.etag === 'string' && !headers.etag.startsWith('W/') ? headers.etag : null
|
|
161
281
|
this.#resume = resume
|
|
162
282
|
return true
|
|
163
283
|
}
|
|
@@ -183,7 +303,17 @@ class Handler extends DecoratorHandler {
|
|
|
183
303
|
// TODO (fix): What if we were paused before the error?
|
|
184
304
|
return true
|
|
185
305
|
} else {
|
|
186
|
-
|
|
306
|
+
// A resume attempt landed on an unexpected status (e.g. a 503 while
|
|
307
|
+
// resuming). #retryError describes the PREVIOUS failure — surfacing it
|
|
308
|
+
// as-is would report a stale error that says nothing about what just
|
|
309
|
+
// happened. Report the current status and keep the prior failure as
|
|
310
|
+
// the cause.
|
|
311
|
+
const err = new Error(
|
|
312
|
+
`Response retry failed with status code ${statusCode}`,
|
|
313
|
+
this.#retryError != null ? { cause: this.#retryError } : undefined,
|
|
314
|
+
)
|
|
315
|
+
err.statusCode = statusCode
|
|
316
|
+
this.#maybeError(err)
|
|
187
317
|
return false
|
|
188
318
|
}
|
|
189
319
|
}
|
|
@@ -193,16 +323,32 @@ class Handler extends DecoratorHandler {
|
|
|
193
323
|
this.#pos += chunk.byteLength
|
|
194
324
|
}
|
|
195
325
|
|
|
196
|
-
if (this.#statusCode < 400) {
|
|
326
|
+
if (this.#statusCode < 400 || (this.#headersSent && !this.#errorSent)) {
|
|
197
327
|
return super.onData(chunk)
|
|
198
328
|
}
|
|
199
329
|
|
|
200
330
|
if (this.#body) {
|
|
201
331
|
this.#body.push(chunk)
|
|
202
332
|
this.#bodySize += chunk.byteLength
|
|
203
|
-
if (this.#bodySize >
|
|
333
|
+
if (this.#bodySize > MAX_ERROR_BODY_SIZE) {
|
|
334
|
+
// The error body has grown too large to buffer for a replay if the
|
|
335
|
+
// retry is declined. Flush the buffered chunks downstream and fall
|
|
336
|
+
// back to passing the response through — it is no longer
|
|
337
|
+
// status-code retried. Previously the buffer was discarded here,
|
|
338
|
+
// which made #maybeError replay headers followed by zero body bytes.
|
|
339
|
+
const body = this.#body
|
|
204
340
|
this.#body = null
|
|
205
341
|
this.#bodySize = 0
|
|
342
|
+
|
|
343
|
+
this.#headersSent = true
|
|
344
|
+
let ret = super.onHeaders(this.#statusCode, this.#headers, () => this.#resume?.())
|
|
345
|
+
for (const buffered of body) {
|
|
346
|
+
if (this.#aborted) {
|
|
347
|
+
return false
|
|
348
|
+
}
|
|
349
|
+
ret = super.onData(buffered)
|
|
350
|
+
}
|
|
351
|
+
return ret
|
|
206
352
|
}
|
|
207
353
|
}
|
|
208
354
|
}
|
|
@@ -214,6 +360,12 @@ class Handler extends DecoratorHandler {
|
|
|
214
360
|
return super.onComplete(trailers)
|
|
215
361
|
}
|
|
216
362
|
|
|
363
|
+
if (this.#headersSent && !this.#errorSent) {
|
|
364
|
+
// The >= 400 response was passed through (too large to buffer for
|
|
365
|
+
// replay) — headers and data have already been forwarded downstream.
|
|
366
|
+
return super.onComplete(trailers)
|
|
367
|
+
}
|
|
368
|
+
|
|
217
369
|
this.#maybeRetry(null)
|
|
218
370
|
}
|
|
219
371
|
|
|
@@ -229,6 +381,18 @@ class Handler extends DecoratorHandler {
|
|
|
229
381
|
}
|
|
230
382
|
|
|
231
383
|
#maybeError(err) {
|
|
384
|
+
if (!err && this.#aborted) {
|
|
385
|
+
// Downstream aborted (e.g. during the backoff wait between attempts).
|
|
386
|
+
// The replay branches below are suppressed by the aborted
|
|
387
|
+
// DecoratorHandler, so without this no terminal event would ever reach
|
|
388
|
+
// downstream (raw dispatch would hang forever), or a generic
|
|
389
|
+
// 'Response retry failed' would replace the abort reason.
|
|
390
|
+
// DecoratorHandler.onError still forwards after an abort, so deliver
|
|
391
|
+
// the abort reason as the terminal onError (exactly once, guarded by
|
|
392
|
+
// #errorSent below).
|
|
393
|
+
err = this.#reason ?? new RequestAbortedError()
|
|
394
|
+
}
|
|
395
|
+
|
|
232
396
|
if (err) {
|
|
233
397
|
if (!this.#errorSent) {
|
|
234
398
|
this.#errorSent = true
|
|
@@ -264,7 +428,22 @@ class Handler extends DecoratorHandler {
|
|
|
264
428
|
}
|
|
265
429
|
|
|
266
430
|
#maybeRetry(err) {
|
|
267
|
-
if (
|
|
431
|
+
if (
|
|
432
|
+
this.#aborted ||
|
|
433
|
+
isDisturbed(this.#opts.body) ||
|
|
434
|
+
// Once headers have been forwarded, a range resume is the only option —
|
|
435
|
+
// but it is impossible when the response wasn't tracked for resumption
|
|
436
|
+
// (#pos == null, e.g. a trailer response or a passed-through >= 400
|
|
437
|
+
// body) or #end is not positive: zero means nothing left to request
|
|
438
|
+
// (`bytes=0--1` is invalid), negative means the server sent a bogus
|
|
439
|
+
// content-length (e.g. `-5`). A non-positive #end can enter both from
|
|
440
|
+
// the initial response and from a full-200 restart, so guard here at
|
|
441
|
+
// the single resume decision point, mirroring the precondition asserted
|
|
442
|
+
// below. Without this, the resume asserts would throw and be delivered
|
|
443
|
+
// to the user IN PLACE of the original error.
|
|
444
|
+
(this.#headersSent && (this.#pos == null || (this.#end != null && this.#end <= 0))) ||
|
|
445
|
+
(this.#pos && !this.#etag)
|
|
446
|
+
) {
|
|
268
447
|
this.#maybeError(err)
|
|
269
448
|
return
|
|
270
449
|
}
|
|
@@ -309,8 +488,29 @@ class Handler extends DecoratorHandler {
|
|
|
309
488
|
assert(Number.isFinite(this.#pos))
|
|
310
489
|
assert(this.#end == null || (Number.isFinite(this.#end) && this.#end > 0))
|
|
311
490
|
|
|
312
|
-
|
|
313
|
-
|
|
491
|
+
// Direct dispatch()/compose() callers may pass undici's legal flat
|
|
492
|
+
// [name, value, ...] array headers; spreading that form would send
|
|
493
|
+
// garbage numeric header names ('0', '1', ...) on the wire instead
|
|
494
|
+
// of the real ones. Normalize to an object first (same as
|
|
495
|
+
// redirect.js does).
|
|
496
|
+
this.#opts = {
|
|
497
|
+
...this.#opts,
|
|
498
|
+
headers: Array.isArray(this.#opts.headers)
|
|
499
|
+
? parseHeaders(this.#opts.headers)
|
|
500
|
+
: { ...this.#opts.headers },
|
|
501
|
+
}
|
|
502
|
+
// A pos 0 resume is allowed without an etag (nothing was forwarded
|
|
503
|
+
// yet, so nothing can tear) — but then there is no etag to validate
|
|
504
|
+
// against. Only send if-match when we actually hold one; a null
|
|
505
|
+
// value would go on the wire as an invalid empty `if-match:` header.
|
|
506
|
+
// Delete any if-match a PREVIOUS resume attempt wrote first: the
|
|
507
|
+
// spread above copies it from the reassigned opts, and #etag may
|
|
508
|
+
// have been cleared since (e.g. a full-200 restart with a weak or
|
|
509
|
+
// missing etag) — the stale validator must not go on the wire.
|
|
510
|
+
delete this.#opts.headers['if-match']
|
|
511
|
+
if (typeof this.#etag === 'string') {
|
|
512
|
+
this.#opts.headers['if-match'] = this.#etag
|
|
513
|
+
}
|
|
314
514
|
this.#opts.headers.range = `bytes=${this.#pos}-${this.#end ? this.#end - 1 : ''}`
|
|
315
515
|
this.#opts.logger?.debug({ err, retryCount: this.#retryCount }, 'retry response body')
|
|
316
516
|
|
|
@@ -321,11 +521,67 @@ class Handler extends DecoratorHandler {
|
|
|
321
521
|
}
|
|
322
522
|
})
|
|
323
523
|
.catch((err) => {
|
|
324
|
-
|
|
524
|
+
// When the downstream abort cancelled the backoff timer, the timer's
|
|
525
|
+
// own AbortError rejection is just plumbing — deliver the recorded
|
|
526
|
+
// abort reason instead (falsy reasons are normalized in #maybeError).
|
|
527
|
+
this.#maybeError(this.#aborted ? this.#reason : err)
|
|
325
528
|
})
|
|
326
529
|
}
|
|
327
530
|
|
|
531
|
+
// Backoff wait that is abortable by the handler-chain abort (via the
|
|
532
|
+
// internal AbortController the onConnect wrapper aborts) AND by opts.signal —
|
|
533
|
+
// otherwise a downstream abort during the wait leaves a ref'd timer holding
|
|
534
|
+
// the event loop for up to 60s (retry-after).
|
|
535
|
+
//
|
|
536
|
+
// The internal controller's signal is always a real AbortSignal, so it is
|
|
537
|
+
// the single thing the wait races against:
|
|
538
|
+
// - a real AbortSignal opts.signal is composed with AbortSignal.any (the
|
|
539
|
+
// idiomatic path; a real AbortSignal already carries the add/remove pair
|
|
540
|
+
// subscribeAbort would look for, so .any subsumes it), and
|
|
541
|
+
// - an EventEmitter-style opts.signal (the library also accepts those, see
|
|
542
|
+
// RequestHandler in lib/request.js) — which AbortSignal.any cannot take —
|
|
543
|
+
// is bridged into the internal controller via the same crash-safe
|
|
544
|
+
// subscribeAbort used by sleep(): its 'abort' aborts the controller with
|
|
545
|
+
// the signal's reason. A signal with no complete subscribe/unsubscribe
|
|
546
|
+
// pair (raw dispatch() garbage) yields no bridge and the wait is still
|
|
547
|
+
// cancellable by the internal controller, so the retry proceeds.
|
|
548
|
+
// sleep() then takes its tp.setTimeout fast path on the resulting real
|
|
549
|
+
// AbortSignal.
|
|
550
|
+
#backoff(delay, opts) {
|
|
551
|
+
this.#retryAbortController ??= new AbortController()
|
|
552
|
+
const controller = this.#retryAbortController
|
|
553
|
+
const signal = opts?.signal
|
|
554
|
+
|
|
555
|
+
if (signal == null) {
|
|
556
|
+
return sleep(delay, controller.signal)
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
if (signal instanceof AbortSignal) {
|
|
560
|
+
return sleep(delay, AbortSignal.any([controller.signal, signal]))
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
if (signal.aborted) {
|
|
564
|
+
controller.abort(signal.reason ?? new RequestAbortedError())
|
|
565
|
+
return sleep(delay, controller.signal)
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
const removeAbortListener =
|
|
569
|
+
subscribeAbort(signal, () => controller.abort(signal.reason ?? new RequestAbortedError())) ??
|
|
570
|
+
noop
|
|
571
|
+
const wait = sleep(delay, controller.signal)
|
|
572
|
+
// Detach the bridge once the wait settles so an EE signal that outlives the
|
|
573
|
+
// backoff (e.g. aborted after a successful retry) is not still referenced.
|
|
574
|
+
return wait.finally(removeAbortListener)
|
|
575
|
+
}
|
|
576
|
+
|
|
328
577
|
async #retryFn(err, retryCount, opts) {
|
|
578
|
+
if (this.#aborted) {
|
|
579
|
+
// A user retry callback may invoke this after downstream has already
|
|
580
|
+
// aborted — don't start a backoff timer that nothing will cancel.
|
|
581
|
+
// #maybeRetry's promise chain observes #aborted and delivers #reason.
|
|
582
|
+
return false
|
|
583
|
+
}
|
|
584
|
+
|
|
329
585
|
let retryOpts = opts?.retry
|
|
330
586
|
|
|
331
587
|
if (!retryOpts) {
|
|
@@ -362,7 +618,7 @@ class Handler extends DecoratorHandler {
|
|
|
362
618
|
Math.min(retryAfter, 60e3)
|
|
363
619
|
: Math.min(10e3, retryCount * 1e3)
|
|
364
620
|
this.#opts.logger?.debug({ statusCode, retryAfter, delay, retryCount }, 'retry delay')
|
|
365
|
-
return
|
|
621
|
+
return this.#backoff(delay, opts)
|
|
366
622
|
}
|
|
367
623
|
|
|
368
624
|
if (
|
|
@@ -383,16 +639,12 @@ class Handler extends DecoratorHandler {
|
|
|
383
639
|
].includes(err.code)
|
|
384
640
|
) {
|
|
385
641
|
this.#opts.logger?.debug({ err, retryCount }, 'retry delay')
|
|
386
|
-
return
|
|
387
|
-
signal: opts?.signal ?? undefined,
|
|
388
|
-
})
|
|
642
|
+
return this.#backoff(Math.min(10e3, retryCount * 1e3), opts)
|
|
389
643
|
}
|
|
390
644
|
|
|
391
645
|
if (err?.message && ['other side closed'].includes(err.message)) {
|
|
392
646
|
this.#opts.logger?.debug({ err, retryCount }, 'retry delay')
|
|
393
|
-
return
|
|
394
|
-
signal: opts?.signal ?? undefined,
|
|
395
|
-
})
|
|
647
|
+
return this.#backoff(Math.min(10e3, retryCount * 1e3), opts)
|
|
396
648
|
}
|
|
397
649
|
|
|
398
650
|
return false
|
package/lib/request.js
CHANGED
|
@@ -201,7 +201,11 @@ export function request(dispatch, urlOrOpts, optsOrNully) {
|
|
|
201
201
|
|
|
202
202
|
let path = url.path
|
|
203
203
|
if (!path) {
|
|
204
|
-
|
|
204
|
+
// URLObject marks every field optional; default the path so e.g.
|
|
205
|
+
// request({ origin }) works instead of undici rejecting with
|
|
206
|
+
// "path must be a string".
|
|
207
|
+
const pathname = url.pathname || '/'
|
|
208
|
+
path = url.search ? `${pathname}${url.search}` : pathname
|
|
205
209
|
}
|
|
206
210
|
|
|
207
211
|
opts = {
|
|
@@ -4,16 +4,41 @@ import { parseRangeHeader, getFastNow } from './utils.js'
|
|
|
4
4
|
// Bump version when the URL key format or schema changes to invalidate old caches.
|
|
5
5
|
const VERSION = 10
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
// Registry of live stores so process-level broadcasts (nxt:offPeak,
|
|
8
|
+
// nxt:clearCache) can reach them. Stores are held via WeakRef so that a store
|
|
9
|
+
// dropped without close() is not pinned forever (together with its open
|
|
10
|
+
// DatabaseSync handle and page cache) — GC can still collect it. close()
|
|
11
|
+
// remains the recommended, deterministic cleanup path.
|
|
12
|
+
/** @type {Set<WeakRef<SqliteCacheStore>>} */
|
|
8
13
|
const stores = new Set()
|
|
9
14
|
|
|
15
|
+
// Removes a collected store's WeakRef entry once the store has been GC'd.
|
|
16
|
+
// The callback runs after the store is already gone, so it must not (and
|
|
17
|
+
// cannot) touch the store or its DatabaseSync — the native handle has its own
|
|
18
|
+
// lifecycle and is released by GC/process exit. This only drops bookkeeping.
|
|
19
|
+
const registry = new FinalizationRegistry((ref) => {
|
|
20
|
+
stores.delete(ref)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @param {(store: SqliteCacheStore) => void} fn
|
|
25
|
+
*/
|
|
26
|
+
function forEachStore(fn) {
|
|
27
|
+
for (const ref of stores) {
|
|
28
|
+
const store = ref.deref()
|
|
29
|
+
if (store === undefined) {
|
|
30
|
+
stores.delete(ref)
|
|
31
|
+
} else {
|
|
32
|
+
fn(store)
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
10
37
|
{
|
|
11
38
|
const offPeakBC = new BroadcastChannel('nxt:offPeak')
|
|
12
39
|
offPeakBC.unref()
|
|
13
40
|
offPeakBC.onmessage = () => {
|
|
14
|
-
|
|
15
|
-
store.gc()
|
|
16
|
-
}
|
|
41
|
+
forEachStore((store) => store.gc())
|
|
17
42
|
}
|
|
18
43
|
}
|
|
19
44
|
|
|
@@ -21,9 +46,7 @@ const stores = new Set()
|
|
|
21
46
|
const clearCacheBC = new BroadcastChannel('nxt:clearCache')
|
|
22
47
|
clearCacheBC.unref()
|
|
23
48
|
clearCacheBC.onmessage = () => {
|
|
24
|
-
|
|
25
|
-
store.clear()
|
|
26
|
-
}
|
|
49
|
+
forEachStore((store) => store.clear())
|
|
27
50
|
}
|
|
28
51
|
}
|
|
29
52
|
|
|
@@ -81,6 +104,11 @@ export class SqliteCacheStore {
|
|
|
81
104
|
#insertSeq = 0
|
|
82
105
|
#closed = false
|
|
83
106
|
|
|
107
|
+
/**
|
|
108
|
+
* @type {WeakRef<SqliteCacheStore>}
|
|
109
|
+
*/
|
|
110
|
+
#ref
|
|
111
|
+
|
|
84
112
|
/**
|
|
85
113
|
* @param {import('undici-types/cache-interceptor.d.ts').default.SqliteCacheStoreOpts & { maxSize?: number } | undefined} opts
|
|
86
114
|
*/
|
|
@@ -122,6 +150,50 @@ export class SqliteCacheStore {
|
|
|
122
150
|
CREATE INDEX IF NOT EXISTS idx_cacheInterceptorV${VERSION}_deleteExpiredValuesQuery ON cacheInterceptorV${VERSION}(deleteAt);
|
|
123
151
|
`)
|
|
124
152
|
|
|
153
|
+
// Drop tables left behind by previous schema versions. gc(), clear() and
|
|
154
|
+
// the SQLITE_FULL eviction only ever touch the current version's table, so
|
|
155
|
+
// after a VERSION bump the old table's pages would otherwise stay allocated
|
|
156
|
+
// to its b-tree forever while max_page_count caps the whole file — new
|
|
157
|
+
// inserts hit SQLITE_FULL almost immediately and eviction frees nothing.
|
|
158
|
+
// Dropping returns the pages to SQLite's freelist, which subsequent inserts
|
|
159
|
+
// reuse, so no VACUUM is needed. SQLite drops the table's indexes and its
|
|
160
|
+
// sqlite_sequence row along with it.
|
|
161
|
+
try {
|
|
162
|
+
// LIKE is only a coarse pre-filter; the regexp restricts matches to
|
|
163
|
+
// digit-only version suffixes so user tables sharing the prefix (e.g.
|
|
164
|
+
// "cacheInterceptorVBackup") in a shared database file are never dropped.
|
|
165
|
+
const staleTables = this.#db
|
|
166
|
+
.prepare(
|
|
167
|
+
`SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE 'cacheInterceptorV%'`,
|
|
168
|
+
)
|
|
169
|
+
.all()
|
|
170
|
+
.filter(
|
|
171
|
+
({ name }) =>
|
|
172
|
+
/^cacheInterceptorV\d+$/.test(name) && name !== `cacheInterceptorV${VERSION}`,
|
|
173
|
+
)
|
|
174
|
+
if (staleTables.length > 0) {
|
|
175
|
+
this.#db.exec('BEGIN')
|
|
176
|
+
try {
|
|
177
|
+
for (const { name } of staleTables) {
|
|
178
|
+
// name comes from sqlite_master; quote it defensively anyway.
|
|
179
|
+
this.#db.exec(`DROP TABLE IF EXISTS "${String(name).replaceAll('"', '""')}"`)
|
|
180
|
+
}
|
|
181
|
+
this.#db.exec('COMMIT')
|
|
182
|
+
} catch (err) {
|
|
183
|
+
try {
|
|
184
|
+
this.#db.exec('ROLLBACK')
|
|
185
|
+
} catch {
|
|
186
|
+
// already rolled back automatically
|
|
187
|
+
}
|
|
188
|
+
throw err
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
} catch (err) {
|
|
192
|
+
// A failed cleanup must not brick construction — the current version's
|
|
193
|
+
// table still works, the stale one just keeps occupying pages.
|
|
194
|
+
process.emitWarning(err)
|
|
195
|
+
}
|
|
196
|
+
|
|
125
197
|
this.#getValuesQuery = this.#db.prepare(`
|
|
126
198
|
SELECT
|
|
127
199
|
id,
|
|
@@ -174,7 +246,11 @@ export class SqliteCacheStore {
|
|
|
174
246
|
`DELETE FROM cacheInterceptorV${VERSION} WHERE id IN (SELECT id FROM cacheInterceptorV${VERSION} ORDER BY deleteAt ASC LIMIT ?)`,
|
|
175
247
|
)
|
|
176
248
|
|
|
177
|
-
|
|
249
|
+
this.#ref = new WeakRef(this)
|
|
250
|
+
stores.add(this.#ref)
|
|
251
|
+
// The store itself doubles as the unregister token so close() can drop
|
|
252
|
+
// the registry entry deterministically.
|
|
253
|
+
registry.register(this, this.#ref, this)
|
|
178
254
|
}
|
|
179
255
|
|
|
180
256
|
gc() {
|
|
@@ -222,7 +298,8 @@ export class SqliteCacheStore {
|
|
|
222
298
|
}
|
|
223
299
|
|
|
224
300
|
close() {
|
|
225
|
-
stores.delete(this)
|
|
301
|
+
stores.delete(this.#ref)
|
|
302
|
+
registry.unregister(this)
|
|
226
303
|
// Drain the entire batch synchronously before closing. A plain #flush()
|
|
227
304
|
// only commits one time-budget slice and reschedules the rest via
|
|
228
305
|
// setImmediate; that deferred flush would see #closed and discard the
|
package/lib/utils.js
CHANGED
|
@@ -15,7 +15,13 @@ export function getFastNow() {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export function parseCacheControl(str) {
|
|
18
|
-
|
|
18
|
+
if (Array.isArray(str)) {
|
|
19
|
+
// Cache-Control is a list-typed field, so duplicated field lines are legal
|
|
20
|
+
// (e.g. CDN + origin each adding one) and combine into a single
|
|
21
|
+
// comma-separated value per RFC 9110 §5.2.
|
|
22
|
+
str = str.join(', ')
|
|
23
|
+
}
|
|
24
|
+
return str && typeof str === 'string' ? cacheControlParser.parse(str) : null
|
|
19
25
|
}
|
|
20
26
|
|
|
21
27
|
export function isDisturbed(body) {
|
|
@@ -133,26 +139,43 @@ export function parseURL(url) {
|
|
|
133
139
|
|
|
134
140
|
if (!(url instanceof URL)) {
|
|
135
141
|
const port = url.port != null ? url.port : url.protocol === 'https:' ? 443 : 80
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
if (origin.endsWith('/')) {
|
|
140
|
-
origin = origin.substring(0, origin.length - 1)
|
|
141
|
-
}
|
|
142
|
+
const origin = url.origin != null ? url.origin : `${url.protocol}//${url.hostname}:${port}`
|
|
143
|
+
const path = url.path != null ? url.path : `${url.pathname || ''}${url.search || ''}`
|
|
142
144
|
|
|
143
|
-
|
|
144
|
-
path = `/${path}`
|
|
145
|
-
}
|
|
146
|
-
// new URL(path, origin) is unsafe when `path` contains an absolute URL
|
|
147
|
-
// From https://developer.mozilla.org/en-US/docs/Web/API/URL/URL:
|
|
148
|
-
// If first parameter is a relative URL, second param is required, and will be used as the base URL.
|
|
149
|
-
// If first parameter is an absolute URL, a given second param will be ignored.
|
|
150
|
-
url = new URL(origin + path)
|
|
145
|
+
url = buildURL(origin, path)
|
|
151
146
|
}
|
|
152
147
|
|
|
153
148
|
return url
|
|
154
149
|
}
|
|
155
150
|
|
|
151
|
+
// Build an absolute URL from an origin and a path by concatenation.
|
|
152
|
+
//
|
|
153
|
+
// `new URL(path, origin)` is unsafe when `path` is itself absolute or
|
|
154
|
+
// protocol-relative (e.g. starts with `//host`): per the WHATWG URL spec, a
|
|
155
|
+
// protocol-relative first argument inherits only the scheme from the base and
|
|
156
|
+
// resolves its authority from `path`, so the origin's host is silently
|
|
157
|
+
// discarded. Concatenating `origin + path` and parsing the whole thing as a
|
|
158
|
+
// single absolute URL keeps the origin authoritative — which matters for
|
|
159
|
+
// redirect resolution, where a leaked host is an SSRF/misrouting vector.
|
|
160
|
+
//
|
|
161
|
+
// From https://developer.mozilla.org/en-US/docs/Web/API/URL/URL:
|
|
162
|
+
// If first parameter is a relative URL, second param is required, and will be used as the base URL.
|
|
163
|
+
// If first parameter is an absolute URL, a given second param will be ignored.
|
|
164
|
+
export function buildURL(origin, path) {
|
|
165
|
+
origin = String(origin)
|
|
166
|
+
path = path == null ? '' : String(path)
|
|
167
|
+
|
|
168
|
+
if (origin.endsWith('/')) {
|
|
169
|
+
origin = origin.substring(0, origin.length - 1)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (path && !path.startsWith('/')) {
|
|
173
|
+
path = `/${path}`
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return new URL(origin + path)
|
|
177
|
+
}
|
|
178
|
+
|
|
156
179
|
export function parseOrigin(url) {
|
|
157
180
|
url = parseURL(url)
|
|
158
181
|
|