@nxtedition/nxt-undici 7.4.1 → 7.4.3

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.
@@ -10,6 +10,7 @@ class Handler extends DecoratorHandler {
10
10
  #body = ''
11
11
  #bodySize = 0
12
12
  #opts
13
+ #reason = null
13
14
 
14
15
  constructor(opts, { handler }) {
15
16
  super(handler)
@@ -23,8 +24,14 @@ class Handler extends DecoratorHandler {
23
24
  this.#headers = null
24
25
  this.#body = ''
25
26
  this.#bodySize = 0
26
-
27
- super.onConnect(abort)
27
+ this.#reason = null
28
+
29
+ super.onConnect((reason) => {
30
+ // Remember the abort reason so onError can recognize it. The reason is
31
+ // owned by the caller (e.g. signal.reason) and must not be decorated.
32
+ this.#reason = reason ?? null
33
+ abort(reason)
34
+ })
28
35
  }
29
36
 
30
37
  onHeaders(statusCode, headers, resume) {
@@ -81,6 +88,16 @@ class Handler extends DecoratorHandler {
81
88
  }
82
89
 
83
90
  onError(err) {
91
+ // An abort reason is owned by the caller and may be shared by every
92
+ // request in flight on the same signal — decorating it in place would
93
+ // permanently mutate the caller's object and leak one request's req/res
94
+ // onto another request's rejection (last writer wins). Callers also rely
95
+ // on receiving the exact reason object, so pass it through untouched.
96
+ if (err != null && (err === this.#reason || err === this.#opts.signal?.reason)) {
97
+ super.onError(err)
98
+ return
99
+ }
100
+
84
101
  super.onError(
85
102
  decorateError(err, this.#opts, {
86
103
  statusCode: this.#statusCode || undefined,
@@ -1,9 +1,96 @@
1
1
  import assert from 'node:assert'
2
2
  import tp from 'node:timers/promises'
3
- import { DecoratorHandler, isDisturbed, decorateError, parseContentRange } from '../utils.js'
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
- if (this.#abort) {
64
- this.#abort(reason)
65
- } else {
66
- this.#reason = reason
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
- this.#maybeError(this.#retryError)
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 > 256 * 1024) {
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 (this.#aborted || isDisturbed(this.#opts.body) || (this.#pos && !this.#etag)) {
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
- this.#opts = { ...this.#opts, headers: { ...this.#opts.headers } }
313
- this.#opts.headers['if-match'] = this.#etag
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
- this.#maybeError(err)
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 tp.setTimeout(delay, true, { signal: opts?.signal ?? undefined })
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 tp.setTimeout(Math.min(10e3, retryCount * 1e3), true, {
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 tp.setTimeout(Math.min(10e3, retryCount * 1e3), true, {
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
- path = url.search ? `${url.pathname}${url.search}` : url.pathname
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 = {