undici 8.12.3 → 8.12.4

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/README.md CHANGED
@@ -171,7 +171,7 @@ const response = await fetch('https://api.example.com/data');
171
171
  - Building isomorphic code that runs in browsers and Node.js
172
172
  - Publishing to npm and want to maximize compatibility with JS runtimes
173
173
  - Simple HTTP requests without advanced configuration
174
- - You're publishing to npm and you want to maximize compatiblity
174
+ - You're publishing to npm and you want to maximize compatibility
175
175
  - You don't depend on features from a specific version of undici
176
176
 
177
177
  #### Use Undici Module When:
@@ -205,7 +205,10 @@ added: v4.0.0
205
205
  -->
206
206
 
207
207
  * `options` {Object}
208
- * `origin` {string|URL} (optional) The origin to dispatch the request to.
208
+ * `origin` {string|URL} The origin to dispatch the request to. Optional for a
209
+ dispatcher that is already bound to a single origin, such as `Client` or
210
+ `Pool`, which then use their own origin. Required on `Agent`, which is not
211
+ bound to one origin and throws an `InvalidArgumentError` when it is missing.
209
212
  * `path` {string} The request path.
210
213
  * `method` {string} The HTTP method, e.g. `'GET'` or `'POST'`.
211
214
  * `body` {string|Buffer|Uint8Array|Readable|FormData|null} The request body.
@@ -60,7 +60,7 @@ added: v6.7.0
60
60
  * `retryAfter` {boolean} When `true`, the delay between retries is inferred from
61
61
  the `Retry-After` response header when present. **Default:** `true`.
62
62
  * `methods` {string[]} HTTP methods that are eligible for retry.
63
- **Default:** `['GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE']`.
63
+ **Default:** `['GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE', 'QUERY']`.
64
64
  * `statusCodes` {number[]} HTTP status codes that trigger a retry.
65
65
  **Default:** `[500, 502, 503, 504, 429]`.
66
66
  * `errorCodes` {string[]} Transport error codes that trigger a retry.
@@ -85,7 +85,7 @@ changes:
85
85
  * `retryAfter` {boolean} When `true`, the delay before the next retry is inferred
86
86
  from the `Retry-After` response header when present. **Default:** `true`.
87
87
  * `methods` {string[]} HTTP methods that are eligible for retrying. **Default:**
88
- `['GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE']`.
88
+ `['GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE', 'QUERY']`.
89
89
  * `statusCodes` {number[]} HTTP status codes that trigger a retry. **Default:**
90
90
  `[500, 502, 503, 504, 429]`.
91
91
  * `errorCodes` {string[]} Network error codes that trigger a retry. **Default:**
@@ -155,7 +155,7 @@ const response = await fetch('https://api.example.com/data');
155
155
  - Building isomorphic code that runs in browsers and Node.js
156
156
  - Publishing to npm and want to maximize compatibility with JS runtimes
157
157
  - Simple HTTP requests without advanced configuration
158
- - You're publishing to npm and you want to maximize compatiblity
158
+ - You're publishing to npm and you want to maximize compatibility
159
159
  - You don't depend on features from a specific version of undici
160
160
 
161
161
  #### Use Undici Module When:
@@ -6,7 +6,9 @@ const { zstdCompressSync, zstdDecompressSync } = require('node:zlib')
6
6
 
7
7
  let DatabaseSync
8
8
 
9
- const VERSION = 4
9
+ // Bump when entries written by an older cache policy are unsafe to reuse.
10
+ // Version 5 invalidates entries that may have been poisoned through redirects.
11
+ const VERSION = 5
10
12
 
11
13
  // 2gb
12
14
  const MAX_ENTRY_SIZE = 2 * 1000 * 1000 * 1000
@@ -18,6 +20,20 @@ const COMPRESS_THRESHOLD = 1024 * 1024
18
20
  // node:sqlite is available.
19
21
  const hasZstd = typeof zstdCompressSync === 'function' && typeof zstdDecompressSync === 'function'
20
22
 
23
+ // SQLite reports a database that another connection is writing as SQLITE_BUSY
24
+ // (5) or SQLITE_LOCKED (6).
25
+ // https://www.sqlite.org/rescode.html#busy
26
+ const SQLITE_BUSY = 5
27
+ const SQLITE_LOCKED = 6
28
+
29
+ /**
30
+ * @param {Error & { errcode?: number }} err
31
+ * @returns {boolean}
32
+ */
33
+ function isLockedError (err) {
34
+ return err?.errcode === SQLITE_BUSY || err?.errcode === SQLITE_LOCKED
35
+ }
36
+
21
37
  /**
22
38
  * @typedef {import('../../types/cache-interceptor.d.ts').default.CacheStore} CacheStore
23
39
  * @implements {CacheStore}
@@ -259,7 +275,7 @@ module.exports = class SqliteCacheStore {
259
275
  get (key) {
260
276
  assertCacheKey(key)
261
277
 
262
- const value = this.#findValue(key)
278
+ const value = this.#lockSafe(() => this.#findValue(key))
263
279
  return value
264
280
  ? {
265
281
  body: value.body
@@ -307,10 +323,10 @@ module.exports = class SqliteCacheStore {
307
323
  }
308
324
  }
309
325
 
310
- const existingValue = this.#findValue(key, true)
326
+ const existingValue = this.#lockSafe(() => this.#findValue(key, true))
311
327
  if (existingValue) {
312
328
  // Updating an existing response, let's overwrite it
313
- this.#updateValueQuery.run(
329
+ this.#lockSafe(() => this.#updateValueQuery.run(
314
330
  storedBody,
315
331
  compressed,
316
332
  value.deleteAt,
@@ -323,10 +339,10 @@ module.exports = class SqliteCacheStore {
323
339
  value.cachedAt,
324
340
  value.staleAt,
325
341
  existingValue.id
326
- )
342
+ ))
327
343
  } else {
328
344
  // New response, let's insert it
329
- this.#insertValueQuery.run(
345
+ this.#lockSafe(() => this.#insertValueQuery.run(
330
346
  url,
331
347
  key.method,
332
348
  storedBody,
@@ -340,7 +356,7 @@ module.exports = class SqliteCacheStore {
340
356
  value.vary ? JSON.stringify(value.vary) : null,
341
357
  value.cachedAt,
342
358
  value.staleAt
343
- )
359
+ ))
344
360
  this.#prune()
345
361
  }
346
362
  }
@@ -389,23 +405,53 @@ module.exports = class SqliteCacheStore {
389
405
  throw new TypeError(`expected key to be object, got ${typeof key}`)
390
406
  }
391
407
 
392
- this.#deleteByUrlQuery.run(this.#makeValueUrl(key))
408
+ this.#lockSafe(() => this.#deleteByUrlQuery.run(this.#makeValueUrl(key)))
409
+ }
410
+
411
+ /**
412
+ * Runs a database operation, skipping it when another connection holds the
413
+ * database lock.
414
+ *
415
+ * The cache store is meant to be shared between processes, and SQLite reports
416
+ * a concurrent writer as SQLITE_BUSY/SQLITE_LOCKED. Those errors are
417
+ * transient, and caching is best effort: the entry is skipped and the next
418
+ * request retries, rather than failing the request that triggered the write.
419
+ *
420
+ * @template T
421
+ * @param {() => T} fn
422
+ * @returns {T | undefined}
423
+ */
424
+ #lockSafe (fn) {
425
+ try {
426
+ return fn()
427
+ } catch (err) {
428
+ if (!isLockedError(err)) {
429
+ throw err
430
+ }
431
+
432
+ return undefined
433
+ }
393
434
  }
394
435
 
395
436
  #prune () {
396
- if (Number.isFinite(this.#maxCount) && this.size <= this.#maxCount) {
397
- return 0
437
+ if (Number.isFinite(this.#maxCount)) {
438
+ // A locked database cannot be measured, so skip pruning instead of
439
+ // failing the write that triggered it.
440
+ const size = this.#lockSafe(() => this.size)
441
+ if (size === undefined || size <= this.#maxCount) {
442
+ return 0
443
+ }
398
444
  }
399
445
 
400
446
  {
401
- const removed = this.#deleteExpiredValuesQuery.run(Date.now()).changes
447
+ const removed = this.#lockSafe(() => this.#deleteExpiredValuesQuery.run(Date.now()).changes)
402
448
  if (removed) {
403
449
  return removed
404
450
  }
405
451
  }
406
452
 
407
453
  {
408
- const removed = this.#deleteOldValuesQuery?.run(Math.max(Math.floor(this.#maxCount * 0.1), 1)).changes
454
+ const removed = this.#lockSafe(() => this.#deleteOldValuesQuery?.run(Math.max(Math.floor(this.#maxCount * 0.1), 1)).changes)
409
455
  if (removed) {
410
456
  return removed
411
457
  }
@@ -83,7 +83,9 @@ function lazyllhttp () {
83
83
 
84
84
  // We disable wasm SIMD on older versions of Node.js on ppc64 that are broken on Power >=9 architectures.
85
85
  let useWasmSIMD = true
86
- if (process.arch === 'ppc64') {
86
+ if (process.arch === 'riscv64') {
87
+ useWasmSIMD = false
88
+ } else if (process.arch === 'ppc64') {
87
89
  const [major, minor] = process.versions.node.split('.').map(n => parseInt(n, 10))
88
90
  if (major < 24 || (major === 24 && minor < 12)) {
89
91
  useWasmSIMD = false
@@ -47,7 +47,6 @@ const {
47
47
  const { channels } = require('../core/diagnostics.js')
48
48
 
49
49
  const kOpenStreams = Symbol('open streams')
50
- const kRequestStreamId = Symbol('request stream id')
51
50
  const kRequestStream = Symbol('request stream')
52
51
  const kRequestStreamCleanup = Symbol('request stream cleanup')
53
52
  const kRequestStreamState = Symbol('request stream state')
@@ -61,6 +60,9 @@ const kRefusedStreamRetry = Symbol('refused stream retry')
61
60
  // event loop.
62
61
  const MAX_GOAWAY_REPLAY_ATTEMPTS = 1
63
62
 
63
+ // RFC 9113 section 6.2. node:http2 reports frame types as raw numbers.
64
+ const HTTP2_FRAME_TYPE_HEADERS = 1
65
+
64
66
  // Lower bound on how long a liveness PING may take to be acked. Stream
65
67
  // timeouts can be tiny (tens of ms), while an ack on a live connection can take
66
68
  // longer than that on a loaded event loop; without a floor a slow but healthy
@@ -133,7 +135,9 @@ function getGoAwayPendingIdx (client, lastStreamID) {
133
135
  continue
134
136
  }
135
137
 
136
- if (typeof request[kRequestStreamId] !== 'number' || request[kRequestStreamId] > maxAcceptedStreamID) {
138
+ // A pending stream may not have had an ID when it was bound to the request.
139
+ const streamId = request[kRequestStream]?.id
140
+ if (typeof streamId !== 'number' || streamId > maxAcceptedStreamID) {
137
141
  return i
138
142
  }
139
143
  }
@@ -142,7 +146,6 @@ function getGoAwayPendingIdx (client, lastStreamID) {
142
146
  }
143
147
 
144
148
  function detachRequestFromStream (request) {
145
- request[kRequestStreamId] = null
146
149
  request[kRequestStream] = null
147
150
  request[kRequestStreamCleanup] = null
148
151
  }
@@ -152,7 +155,6 @@ function bindRequestToStream (request, stream, cleanup) {
152
155
  const previousStream = request[kRequestStream]
153
156
  detachRequestFromStream(request)
154
157
  previousCleanup?.(previousStream)
155
- request[kRequestStreamId] = stream.id
156
158
  request[kRequestStream] = stream
157
159
  request[kRequestStreamCleanup] = cleanup
158
160
  }
@@ -253,8 +255,14 @@ function severRequestStream (stream) {
253
255
  closeStreamSession(stream)
254
256
  }
255
257
 
258
+ // #5936: Node can still deliver a session-level error to a native stream
259
+ // after undici has stopped tracking it (e.g. on idle session teardown). Keep
260
+ // a persistent noop error sink so the 'error' event is never emitted with no
261
+ // listener, which would crash the process. A single sink is enough even if
262
+ // one was already attached by the onError path, so clear it first.
256
263
  if (!stream.destroyed && !stream.closed) {
257
- stream.once('error', noop)
264
+ stream.off('error', noop)
265
+ stream.on('error', noop)
258
266
  }
259
267
  }
260
268
 
@@ -732,16 +740,18 @@ function onHttp2SessionGoAway (errorCode, lastStreamID) {
732
740
  }
733
741
  }
734
742
 
735
- for (let i = 0; i < streamsToClose.length; i++) {
736
- closeStream(streamsToClose[i])
737
- }
738
-
739
743
  if (pendingIdx !== previousPendingIdx) {
740
744
  const remainingPendingRequests = client[kQueue].slice(previousPendingIdx)
741
745
  client[kQueue].length = pendingIdx
742
746
  client[kQueue].push(...retriableRequests, ...remainingPendingRequests)
743
747
  }
744
748
 
749
+ // Publish the rebuilt queue and detach the session before closing anything.
750
+ // A synchronous sibling HEADERS frameError can requeue an accepted-but-unsent
751
+ // request and resume the client. It must see the final indices and must not
752
+ // dispatch its retry on the session that is being retired.
753
+ client[kPendingIdx] = pendingIdx
754
+
745
755
  if (client[kHTTP2Session] === this) {
746
756
  client[kSocket] = null
747
757
  client[kHTTPContext] = null
@@ -756,12 +766,14 @@ function onHttp2SessionGoAway (errorCode, lastStreamID) {
756
766
  clearNoStreamsTimeout(this)
757
767
  clearHttp2ProbeTimeout(this)
758
768
 
769
+ for (let i = 0; i < streamsToClose.length; i++) {
770
+ closeStream(streamsToClose[i])
771
+ }
772
+
759
773
  if (!this.closed && !this.destroyed) {
760
774
  this.close()
761
775
  }
762
776
 
763
- client[kPendingIdx] = pendingIdx
764
-
765
777
  client.emit('disconnect', client[kUrl], [client], err)
766
778
 
767
779
  client[kResume]()
@@ -868,8 +880,8 @@ function closeStreamSession (stream) {
868
880
  }
869
881
 
870
882
  function onUpgradeStreamClose () {
871
- this.off('error', noop)
872
-
883
+ // #5936: Retain the noop error sink so a session-level error delivered to
884
+ // this stream after undici detaches it cannot crash via an unhandled 'error'.
873
885
  const state = this[kRequestStreamState]
874
886
  this[kRequestStreamState] = null
875
887
 
@@ -973,8 +985,11 @@ function releaseUpgradeStream (stream) {
973
985
 
974
986
  removeUpgradeStreamListeners(stream)
975
987
 
988
+ // #5936: removeUpgradeStreamListeners() dropped the noop sink; re-attach a
989
+ // persistent one so a late session-level error on this (possibly still native
990
+ // tracked) stream cannot crash via an unhandled 'error'.
976
991
  if (!stream.destroyed && !stream.closed) {
977
- stream.once('error', noop)
992
+ stream.on('error', noop)
978
993
  }
979
994
  }
980
995
 
@@ -1407,12 +1422,14 @@ function releaseRequestStream (stream) {
1407
1422
  detachRequestFromStream(request)
1408
1423
  }
1409
1424
 
1410
- // A closed or destroyed stream cannot emit further events; leaving the
1411
- // listeners in place saves the removal scans (they are collected with
1412
- // the stream). All handlers bail out when the stream state is gone.
1425
+ // #5936: Node can deliver a session-level error to a released stream before
1426
+ // its native handle is dropped (e.g. on idle session teardown). Attach a
1427
+ // persistent noop error sink so a late 'error' always has a listener and
1428
+ // cannot crash the process. A closed/destroyed stream cannot emit further
1429
+ // events and is no longer tracked natively, so skip the listener teardown.
1413
1430
  if (!stream.destroyed && !stream.closed) {
1414
1431
  removeRequestStreamListeners(stream)
1415
- stream.once('error', noop)
1432
+ stream.on('error', noop)
1416
1433
  }
1417
1434
  }
1418
1435
 
@@ -1607,10 +1624,43 @@ function retryRefusedStream (stream, state) {
1607
1624
  // SHOULD NOT automatically retry the same request more than once.
1608
1625
  request[kRefusedStreamRetry] = true
1609
1626
 
1627
+ // The peer only reset this stream, so the HTTP/2 session remains usable for
1628
+ // the retry.
1629
+ requeueStreamRequest(client, state)
1630
+
1631
+ return true
1632
+ }
1633
+
1634
+ // nghttp2 refuses to send the HEADERS of a stream opened before a GOAWAY was
1635
+ // received but flushed after it. The peer never saw the request, so it can be
1636
+ // replayed on a new connection whatever its method. A GOAWAY whose
1637
+ // lastStreamID covers the stream (e.g. 2^31-1, a graceful shutdown notice)
1638
+ // leaves the stream attached to the session, so this is the only signal.
1639
+ function retryUnsentStream (state) {
1640
+ const { client, request, session } = state
1641
+
1642
+ if (
1643
+ !session[kReceivedGoAway] ||
1644
+ state.responseReceived ||
1645
+ request.aborted ||
1646
+ request.completed ||
1647
+ !canReplayRequest(request) ||
1648
+ !registerGoAwayRefusal(request)
1649
+ ) {
1650
+ return false
1651
+ }
1652
+
1653
+ requeueStreamRequest(client, state)
1654
+
1655
+ return true
1656
+ }
1657
+
1658
+ function requeueStreamRequest (client, state) {
1659
+ const { request } = state
1660
+
1610
1661
  // Detach the failed attempt before moving the request back to the pending
1611
- // queue. The peer only reset this stream, so the HTTP/2 session remains
1612
- // usable for the retry. Severing also drops the 'close' listener, so the
1613
- // abandoned stream cannot later complete the retried request.
1662
+ // queue. Severing also drops the 'close' listener, so the abandoned stream
1663
+ // cannot later complete the retried request.
1614
1664
  detachRequestStreamForClose(request)
1615
1665
  state.stream = null
1616
1666
  state.requestFinalized = true
@@ -1618,8 +1668,6 @@ function retryRefusedStream (stream, state) {
1618
1668
  completeRequest(client, request)
1619
1669
  client[kQueue].splice(client[kPendingIdx], 0, request)
1620
1670
  client[kResume]()
1621
-
1622
- return true
1623
1671
  }
1624
1672
 
1625
1673
  function onError (err) {
@@ -1631,6 +1679,10 @@ function onError (err) {
1631
1679
  }
1632
1680
 
1633
1681
  stream.off('error', onError)
1682
+ // #5936: The stream has already errored, but Node may destroy it again with a
1683
+ // session-level error before the native handle is released. Keep a persistent
1684
+ // noop error sink so that re-delivery cannot go unhandled.
1685
+ stream.on('error', noop)
1634
1686
 
1635
1687
  // Node's HTTP/2 implementation can turn an incomplete Content-Length body
1636
1688
  // into a protocol stream error instead of emitting 'end'. Prefer the
@@ -1662,6 +1714,11 @@ function onFrameError (type, code) {
1662
1714
  }
1663
1715
 
1664
1716
  stream.off('frameError', onFrameError)
1717
+
1718
+ if (type === HTTP2_FRAME_TYPE_HEADERS && retryUnsentStream(state)) {
1719
+ return
1720
+ }
1721
+
1665
1722
  state.abort(new InformationalError(`HTTP/2: "frameError" received - type ${type}, code ${code}`))
1666
1723
  }
1667
1724
 
@@ -52,7 +52,8 @@ const EVENT_HEADERS_SIZE = 19
52
52
  let miloPackage
53
53
 
54
54
  // We disable wasm SIMD on ppc64 as it seems to be broken on Power 9 architectures.
55
- let useWasmSIMD = process.arch !== 'ppc64'
55
+ // We disable wasm SIMD on riscv64 as it seems to be broken on there as well.
56
+ let useWasmSIMD = (process.arch !== 'riscv64' && process.arch !== 'ppc64')
56
57
  // The Env Variable UNDICI_NO_WASM_SIMD allows explicitly overriding the default behavior.
57
58
  if (process.env.UNDICI_NO_WASM_SIMD === '1') {
58
59
  useWasmSIMD = false