undici 7.3.0 → 7.4.0

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
@@ -337,7 +337,8 @@ See [Dispatcher.upgrade](./docs/docs/api/Dispatcher.md#dispatcherupgradeoptions-
337
337
 
338
338
  * dispatcher `Dispatcher`
339
339
 
340
- Sets the global dispatcher used by Common API Methods.
340
+ Sets the global dispatcher used by Common API Methods. Global dispatcher is shared among compatible undici modules,
341
+ including undici that is bundled internally with node.js.
341
342
 
342
343
  ### `undici.getGlobalDispatcher()`
343
344
 
@@ -210,7 +210,7 @@ Returns: `Boolean` - `false` if dispatcher is busy and further dispatch calls wo
210
210
  * **onResponseStart** `(controller: DispatchController, statusCode: number, headers: Record<string, string | string []>, statusMessage?: string) => void` - Invoked when statusCode and headers have been received. May be invoked multiple times due to 1xx informational headers. Not required for `upgrade` requests.
211
211
  * **onResponseData** `(controller: DispatchController, chunk: Buffer) => void` - Invoked when response payload data is received. Not required for `upgrade` requests.
212
212
  * **onResponseEnd** `(controller: DispatchController, trailers: Record<string, string | string[]>) => void` - Invoked when response payload and trailers have been received and the request has completed. Not required for `upgrade` requests.
213
- * **onResponseError** `(error: Error) => void` - Invoked when an error has occurred. May not throw.
213
+ * **onResponseError** `(controller: DispatchController, error: Error) => void` - Invoked when an error has occurred. May not throw.
214
214
 
215
215
  #### Example 1 - Dispatch GET request
216
216
 
@@ -1,7 +1,5 @@
1
1
  # Class: EnvHttpProxyAgent
2
2
 
3
- Stability: Experimental.
4
-
5
3
  Extends: `undici.Dispatcher`
6
4
 
7
5
  EnvHttpProxyAgent automatically reads the proxy configuration from the environment variables `http_proxy`, `https_proxy`, and `no_proxy` and sets up the proxy agents accordingly. When `http_proxy` and `https_proxy` are set, `http_proxy` is used for HTTP requests and `https_proxy` is used for HTTPS requests. If only `http_proxy` is set, `http_proxy` is used for both HTTP and HTTPS requests. If only `https_proxy` is set, it is only used for HTTPS requests.
@@ -28,6 +28,7 @@ import { errors } from 'undici'
28
28
  | `ResponseExceededMaxSizeError` | `UND_ERR_RES_EXCEEDED_MAX_SIZE` | response body exceed the max size allowed |
29
29
  | `SecureProxyConnectionError` | `UND_ERR_PRX_TLS` | tls connection to a proxy failed |
30
30
 
31
+ Be aware of the possible difference between the global dispatcher version and the actual undici version you might be using. We recommend to avoid the check `instanceof errors.UndiciError` and seek for the `error.code === '<error_code>'` instead to avoid inconsistencies.
31
32
  ### `SocketError`
32
33
 
33
34
  The `SocketError` has a `.socket` property which holds socket metadata:
package/index-fetch.js CHANGED
@@ -26,6 +26,9 @@ module.exports.createFastMessageEvent = createFastMessageEvent
26
26
 
27
27
  module.exports.EventSource = require('./lib/web/eventsource/eventsource').EventSource
28
28
 
29
+ const api = require('./lib/api')
30
+ const Dispatcher = require('./lib/dispatcher/dispatcher')
31
+ Object.assign(Dispatcher.prototype, api)
29
32
  // Expose the fetch implementation to be enabled in Node.js core via a flag
30
33
  module.exports.EnvHttpProxyAgent = EnvHttpProxyAgent
31
34
  module.exports.getGlobalDispatcher = getGlobalDispatcher
@@ -79,7 +79,13 @@ class MemoryCacheStore {
79
79
  const entry = this.#entries.get(topLevelKey)?.find((entry) => (
80
80
  entry.deleteAt > now &&
81
81
  entry.method === key.method &&
82
- (entry.vary == null || Object.keys(entry.vary).every(headerName => entry.vary[headerName] === key.headers?.[headerName]))
82
+ (entry.vary == null || Object.keys(entry.vary).every(headerName => {
83
+ if (entry.vary[headerName] === null) {
84
+ return key.headers[headerName] === undefined
85
+ }
86
+
87
+ return entry.vary[headerName] === key.headers[headerName]
88
+ }))
83
89
  ))
84
90
 
85
91
  return entry == null
@@ -232,7 +232,7 @@ module.exports = class SqliteCacheStore {
232
232
  const value = this.#findValue(key)
233
233
  return value
234
234
  ? {
235
- body: value.body ? Buffer.from(value.body.buffer) : undefined,
235
+ body: value.body ? Buffer.from(value.body.buffer, value.body.byteOffset, value.body.byteLength) : undefined,
236
236
  statusCode: value.statusCode,
237
237
  statusMessage: value.statusMessage,
238
238
  headers: value.headers ? JSON.parse(value.headers) : undefined,
@@ -411,10 +411,6 @@ module.exports = class SqliteCacheStore {
411
411
  let matches = true
412
412
 
413
413
  if (value.vary) {
414
- if (!headers) {
415
- return undefined
416
- }
417
-
418
414
  const vary = JSON.parse(value.vary)
419
415
 
420
416
  for (const header in vary) {
@@ -440,18 +436,21 @@ module.exports = class SqliteCacheStore {
440
436
  * @returns {boolean}
441
437
  */
442
438
  function headerValueEquals (lhs, rhs) {
439
+ if (lhs == null && rhs == null) {
440
+ return true
441
+ }
442
+
443
+ if ((lhs == null && rhs != null) ||
444
+ (lhs != null && rhs == null)) {
445
+ return false
446
+ }
447
+
443
448
  if (Array.isArray(lhs) && Array.isArray(rhs)) {
444
449
  if (lhs.length !== rhs.length) {
445
450
  return false
446
451
  }
447
452
 
448
- for (let i = 0; i < lhs.length; i++) {
449
- if (rhs.includes(lhs[i])) {
450
- return false
451
- }
452
- }
453
-
454
- return true
453
+ return lhs.every((x, i) => x === rhs[i])
455
454
  }
456
455
 
457
456
  return lhs === rhs
@@ -10,8 +10,6 @@ const DEFAULT_PORTS = {
10
10
  'https:': 443
11
11
  }
12
12
 
13
- let experimentalWarned = false
14
-
15
13
  class EnvHttpProxyAgent extends DispatcherBase {
16
14
  #noProxyValue = null
17
15
  #noProxyEntries = null
@@ -21,13 +19,6 @@ class EnvHttpProxyAgent extends DispatcherBase {
21
19
  super()
22
20
  this.#opts = opts
23
21
 
24
- if (!experimentalWarned) {
25
- experimentalWarned = true
26
- process.emitWarning('EnvHttpProxyAgent is experimental, expect them to change at any time.', {
27
- code: 'UNDICI-EHPA'
28
- })
29
- }
30
-
31
22
  const { httpProxy, httpsProxy, noProxy, ...agentOpts } = opts
32
23
 
33
24
  this[kNoProxyAgent] = new Agent(agentOpts)
@@ -124,8 +124,10 @@ function getResponseData (data) {
124
124
  return data
125
125
  } else if (typeof data === 'object') {
126
126
  return JSON.stringify(data)
127
- } else {
127
+ } else if (data) {
128
128
  return data.toString()
129
+ } else {
130
+ return ''
129
131
  }
130
132
  }
131
133
 
package/lib/util/cache.js CHANGED
@@ -26,10 +26,14 @@ function makeCacheKey (opts) {
26
26
  if (typeof key !== 'string' || typeof val !== 'string') {
27
27
  throw new Error('opts.headers is not a valid header map')
28
28
  }
29
- headers[key] = val
29
+ headers[key.toLowerCase()] = val
30
30
  }
31
31
  } else if (typeof opts.headers === 'object') {
32
- headers = opts.headers
32
+ headers = {}
33
+
34
+ for (const key of Object.keys(opts.headers)) {
35
+ headers[key.toLowerCase()] = opts.headers[key]
36
+ }
33
37
  } else {
34
38
  throw new Error('opts.headers is not an object')
35
39
  }
@@ -260,19 +264,16 @@ function parseVaryHeader (varyHeader, headers) {
260
264
  return headers
261
265
  }
262
266
 
263
- const output = /** @type {Record<string, string | string[]>} */ ({})
267
+ const output = /** @type {Record<string, string | string[] | null>} */ ({})
264
268
 
265
269
  const varyingHeaders = typeof varyHeader === 'string'
266
270
  ? varyHeader.split(',')
267
271
  : varyHeader
272
+
268
273
  for (const header of varyingHeaders) {
269
274
  const trimmedHeader = header.trim().toLowerCase()
270
275
 
271
- if (headers[trimmedHeader]) {
272
- output[trimmedHeader] = headers[trimmedHeader]
273
- } else {
274
- return undefined
275
- }
276
+ output[trimmedHeader] = headers[trimmedHeader] ?? null
276
277
  }
277
278
 
278
279
  return output
@@ -23,7 +23,7 @@ try {
23
23
  const crypto = require('node:crypto')
24
24
  random = (max) => crypto.randomInt(0, max)
25
25
  } catch {
26
- random = (max) => Math.floor(Math.random(max))
26
+ random = (max) => Math.floor(Math.random() * max)
27
27
  }
28
28
 
29
29
  const textEncoder = new TextEncoder()
@@ -37,6 +37,14 @@ const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => {
37
37
 
38
38
  const dependentControllerMap = new WeakMap()
39
39
 
40
+ let abortSignalHasEventHandlerLeakWarning
41
+
42
+ try {
43
+ abortSignalHasEventHandlerLeakWarning = getMaxListeners(new AbortController().signal) > 0
44
+ } catch {
45
+ abortSignalHasEventHandlerLeakWarning = false
46
+ }
47
+
40
48
  function buildAbort (acRef) {
41
49
  return abort
42
50
 
@@ -424,15 +432,10 @@ class Request {
424
432
  const acRef = new WeakRef(ac)
425
433
  const abort = buildAbort(acRef)
426
434
 
427
- // Third-party AbortControllers may not work with these.
428
- // See, https://github.com/nodejs/undici/pull/1910#issuecomment-1464495619.
429
- try {
430
- // If the max amount of listeners is equal to the default, increase it
431
- // This is only available in node >= v19.9.0
432
- if (typeof getMaxListeners === 'function' && getMaxListeners(signal) === defaultMaxListeners) {
433
- setMaxListeners(1500, signal)
434
- }
435
- } catch {}
435
+ // If the max amount of listeners is equal to the default, increase it
436
+ if (abortSignalHasEventHandlerLeakWarning && getMaxListeners(signal) === defaultMaxListeners) {
437
+ setMaxListeners(1500, signal)
438
+ }
436
439
 
437
440
  util.addAbortListener(signal, abort)
438
441
  // The third argument must be a registry key to be unregistered.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "undici",
3
- "version": "7.3.0",
3
+ "version": "7.4.0",
4
4
  "description": "An HTTP/1.1 client, written from scratch for Node.js",
5
5
  "homepage": "https://undici.nodejs.org",
6
6
  "bugs": {
@@ -70,7 +70,7 @@ declare namespace CacheHandler {
70
70
  statusCode: number
71
71
  statusMessage: string
72
72
  headers: Record<string, string | string[]>
73
- vary?: Record<string, string | string[]>
73
+ vary?: Record<string, string | string[] | null>
74
74
  etag?: string
75
75
  cacheControlDirectives?: CacheControlDirectives
76
76
  cachedAt: number
@@ -88,7 +88,7 @@ declare namespace CacheHandler {
88
88
  statusCode: number
89
89
  statusMessage: string
90
90
  headers: Record<string, string | string[]>
91
- vary?: Record<string, string | string[]>
91
+ vary?: Record<string, string | string[] | null>
92
92
  etag?: string
93
93
  body?: Readable | Iterable<Buffer> | AsyncIterable<Buffer> | Buffer | Iterable<string> | AsyncIterable<string> | string
94
94
  cacheControlDirectives: CacheControlDirectives,