undici 8.2.0 → 8.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.
Files changed (45) hide show
  1. package/README.md +67 -23
  2. package/docs/docs/api/Agent.md +3 -0
  3. package/docs/docs/api/Client.md +43 -5
  4. package/docs/docs/api/Connector.md +1 -0
  5. package/docs/docs/api/Dispatcher.md +7 -0
  6. package/docs/docs/api/Errors.md +12 -0
  7. package/docs/docs/api/EventSource.md +50 -3
  8. package/docs/docs/api/Fetch.md +3 -1
  9. package/docs/docs/api/GlobalInstallation.md +7 -5
  10. package/docs/docs/api/H2CClient.md +2 -2
  11. package/docs/docs/api/Pool.md +3 -0
  12. package/docs/docs/api/RedirectHandler.md +4 -1
  13. package/docs/docs/api/SnapshotAgent.md +23 -0
  14. package/lib/api/api-pipeline.js +4 -0
  15. package/lib/api/api-stream.js +51 -5
  16. package/lib/core/connect.js +29 -4
  17. package/lib/core/symbols.js +1 -0
  18. package/lib/core/util.js +10 -8
  19. package/lib/dispatcher/client-h1.js +59 -18
  20. package/lib/dispatcher/client-h2.js +418 -298
  21. package/lib/dispatcher/client.js +25 -4
  22. package/lib/dispatcher/pool-base.js +21 -3
  23. package/lib/dispatcher/pool.js +23 -0
  24. package/lib/dispatcher/proxy-agent.js +21 -4
  25. package/lib/dispatcher/round-robin-pool.js +26 -0
  26. package/lib/dispatcher/socks5-proxy-agent.js +19 -19
  27. package/lib/handler/redirect-handler.js +36 -11
  28. package/lib/handler/retry-handler.js +14 -0
  29. package/lib/interceptor/redirect.js +3 -3
  30. package/lib/mock/mock-call-history.js +1 -1
  31. package/lib/mock/mock-utils.js +3 -1
  32. package/lib/mock/snapshot-agent.js +11 -1
  33. package/lib/mock/snapshot-recorder.js +38 -3
  34. package/lib/web/fetch/body.js +2 -7
  35. package/lib/web/fetch/formdata.js +21 -2
  36. package/lib/web/fetch/index.js +19 -3
  37. package/lib/web/fetch/request.js +32 -3
  38. package/package.json +4 -4
  39. package/types/client.d.ts +7 -7
  40. package/types/connector.d.ts +1 -0
  41. package/types/dispatcher.d.ts +0 -2
  42. package/types/fetch.d.ts +4 -1
  43. package/types/formdata.d.ts +0 -6
  44. package/types/interceptors.d.ts +1 -1
  45. package/types/snapshot-agent.d.ts +4 -0
package/README.md CHANGED
@@ -21,28 +21,65 @@ npm i undici
21
21
 
22
22
  ## Benchmarks
23
23
 
24
- The benchmark is a simple getting data [example](https://github.com/nodejs/undici/blob/main/benchmarks/benchmark.js) using a
25
- 50 TCP connections with a pipelining depth of 10 running on Node 22.11.0.
24
+ The benchmark is a simple getting data [example](https://github.com/nodejs/undici/blob/main/benchmarks/benchmark.js) using
25
+ 50 TCP connections with a pipelining depth of 10 running on Node 24.14.1.
26
+
27
+ ### HTTP/1.1
26
28
 
27
29
  ```
28
30
  ┌────────────────────────┬─────────┬────────────────────┬────────────┬─────────────────────────┐
29
31
  │ Tests │ Samples │ Result │ Tolerance │ Difference with slowest │
30
32
  ├────────────────────────┼─────────┼────────────────────┼────────────┼─────────────────────────┤
31
- │ 'axios' 15 │ '5708.26 req/sec' │ '± 2.91 %' │ '-' │
32
- │ 'http - no keepalive' 10 │ '5809.80 req/sec' │ '± 2.30 %' │ '+ 1.78 %'
33
- │ 'request' 30 │ '5828.80 req/sec' │ '± 2.91 %' │ '+ 2.11 %'
34
- │ 'undici - fetch' 40 │ '5903.78 req/sec' │ '± 2.87 %' │ '+ 3.43 %'
35
- │ 'node-fetch' 10 │ '5945.40 req/sec' │ '± 2.13 %' │ '+ 4.15 %'
36
- │ 'got' │ 35 │ '6511.45 req/sec' │ '± 2.84 %' │ '+ 14.07 %' │
37
- │ 'http - keepalive' 65 │ '9193.24 req/sec' │ '± 2.92 %' │ '+ 61.05 %' │
38
- │ 'superagent' 35 │ '9339.43 req/sec' │ '± 2.95 %' │ '+ 63.61 %' │
39
- │ 'undici - pipeline' │ 50 │ '13364.62 req/sec' │ '± 2.93 %' │ '+ 134.13 %' │
40
- │ 'undici - stream' 95 │ '18245.36 req/sec' │ '± 2.99 %' │ '+ 219.63 %' │
41
- │ 'undici - request' 50 │ '18340.17 req/sec' │ '± 2.84 %' │ '+ 221.29 %' │
42
- │ 'undici - dispatch' │ 40 │ '22234.42 req/sec' │ '± 2.94 %' │ '+ 289.51 %' │
33
+ │ 'node-fetch' 50 │ '4711.86 req/sec' │ '± 2.92 %' │ '-' │
34
+ │ 'undici - fetch' 75 │ '5438.50 req/sec' │ '± 2.97 %' │ '+ 15.42 %'
35
+ │ 'axios' 45 │ '5448.08 req/sec' │ '± 2.98 %' │ '+ 15.62 %'
36
+ │ 'request' 65 │ '5809.63 req/sec' │ '± 2.90 %' │ '+ 23.30 %'
37
+ │ 'http - no keepalive' 35 │ '5910.77 req/sec' │ '± 2.87 %' │ '+ 25.44 %'
38
+ │ 'got' │ 50 │ '6047.80 req/sec' │ '± 2.91 %' │ '+ 28.35 %' │
39
+ │ 'superagent' 60 │ '7534.53 req/sec' │ '± 2.97 %' │ '+ 59.91 %' │
40
+ │ 'http - keepalive' 75 │ '9343.41 req/sec' │ '± 2.90 %' │ '+ 98.30 %' │
41
+ │ 'undici - pipeline' │ 65 │ '13470.70 req/sec' │ '± 2.93 %' │ '+ 185.89 %' │
42
+ │ 'undici - request' 80 │ '16850.87 req/sec' │ '± 2.93 %' │ '+ 257.63 %' │
43
+ │ 'undici - stream' 101 │ '18488.56 req/sec' │ '± 3.81 %' │ '+ 292.38 %' │
44
+ │ 'undici - dispatch' │ 101 │ '20786.44 req/sec' │ '± 3.08 %' │ '+ 341.15 %' │
43
45
  └────────────────────────┴─────────┴────────────────────┴────────────┴─────────────────────────┘
44
46
  ```
45
47
 
48
+ ### HTTP/1.1 over HTTPS
49
+
50
+ Using [benchmark-https.js](https://github.com/nodejs/undici/blob/main/benchmarks/benchmark-https.js) against an h1-over-TLS server (50 connections, pipelining depth 10, Node 24.14.1).
51
+
52
+ ```
53
+ ┌────────────────────────┬─────────┬───────────────────┬────────────┬─────────────────────────┐
54
+ │ Tests │ Samples │ Result │ Tolerance │ Difference with slowest │
55
+ ├────────────────────────┼─────────┼───────────────────┼────────────┼─────────────────────────┤
56
+ │ 'https - no keepalive'│ 10 │ '1358.40 req/sec' │ '± 1.99 %' │ '-' │
57
+ │ 'undici - fetch' │ 30 │ '3721.76 req/sec' │ '± 2.97 %' │ '+ 173.98 %' │
58
+ │ 'https - keepalive' │ 35 │ '5633.91 req/sec' │ '± 2.84 %' │ '+ 314.75 %' │
59
+ │ 'undici - pipeline' │ 15 │ '6254.05 req/sec' │ '± 2.80 %' │ '+ 360.40 %' │
60
+ │ 'undici - request' │ 25 │ '6669.80 req/sec' │ '± 2.73 %' │ '+ 391.01 %' │
61
+ │ 'undici - stream' │ 25 │ '7019.04 req/sec' │ '± 2.77 %' │ '+ 416.71 %' │
62
+ │ 'undici - dispatch' │ 20 │ '7361.85 req/sec' │ '± 2.90 %' │ '+ 441.95 %' │
63
+ └────────────────────────┴─────────┴───────────────────┴────────────┴─────────────────────────┘
64
+ ```
65
+
66
+ ### HTTP/2
67
+
68
+ Using [benchmark-http2.js](https://github.com/nodejs/undici/blob/main/benchmarks/benchmark-http2.js) against an h2 server (50 connections, pipelining depth 10, Node 24.14.1).
69
+
70
+ ```
71
+ ┌────────────────────────┬─────────┬───────────────────┬────────────┬─────────────────────────┐
72
+ │ Tests │ Samples │ Result │ Tolerance │ Difference with slowest │
73
+ ├────────────────────────┼─────────┼───────────────────┼────────────┼─────────────────────────┤
74
+ │ 'undici - fetch' │ 45 │ '3499.03 req/sec' │ '± 2.93 %' │ '-' │
75
+ │ 'native - http2' │ 25 │ '4904.58 req/sec' │ '± 2.81 %' │ '+ 40.17 %' │
76
+ │ 'undici - pipeline' │ 60 │ '5836.82 req/sec' │ '± 2.99 %' │ '+ 66.81 %' │
77
+ │ 'undici - request' │ 65 │ '6831.25 req/sec' │ '± 2.83 %' │ '+ 95.23 %' │
78
+ │ 'undici - stream' │ 55 │ '6874.30 req/sec' │ '± 2.91 %' │ '+ 96.46 %' │
79
+ │ 'undici - dispatch' │ 55 │ '7791.23 req/sec' │ '± 2.96 %' │ '+ 122.67 %' │
80
+ └────────────────────────┴─────────┴───────────────────┴────────────┴─────────────────────────┘
81
+ ```
82
+
46
83
  ## Undici vs. Fetch
47
84
 
48
85
  ### Overview
@@ -200,7 +237,9 @@ await fetch('https://example.com', {
200
237
  ```
201
238
 
202
239
  `install()` replaces the global `fetch`, `Headers`, `Response`, `Request`, and
203
- `FormData` implementations with undici's versions, so they all match.
240
+ `FormData` implementations with undici's versions, so they all match. It also
241
+ installs undici's `WebSocket`, `CloseEvent`, `ErrorEvent`, `MessageEvent`, and
242
+ `EventSource` globals.
204
243
 
205
244
  Avoid mixing a global `FormData` with `undici.fetch()`, or `undici.FormData`
206
245
  with the built-in global `fetch()`.
@@ -283,12 +322,12 @@ const data2 = await getData();
283
322
 
284
323
  ## Global Installation
285
324
 
286
- Undici provides an `install()` function to add all WHATWG fetch classes to `globalThis`, making them available globally:
325
+ Undici provides an `install()` function to add fetch-related and other web API classes to `globalThis`, making them available globally:
287
326
 
288
327
  ```js
289
328
  import { install } from 'undici'
290
329
 
291
- // Install all WHATWG fetch classes globally
330
+ // Install undici's global web APIs
292
331
  install()
293
332
 
294
333
  // Now you can use fetch classes globally without importing
@@ -316,8 +355,9 @@ The `install()` function adds the following classes to `globalThis`:
316
355
 
317
356
  When you call `install()`, these globals come from the same undici
318
357
  implementation. For example, global `fetch` and global `FormData` will both be
319
- undici's versions, which is the recommended setup if you want to use undici
320
- through globals.
358
+ undici's versions, and `WebSocket` and `EventSource` will also come from
359
+ undici, which is the recommended setup if you want to use undici through
360
+ globals.
321
361
 
322
362
  This is useful for:
323
363
  - Polyfilling environments that don't have fetch
@@ -337,6 +377,9 @@ The `body` mixins are the most common way to format the request/response body. M
337
377
  > [!NOTE]
338
378
  > The body returned from `undici.request` does not implement `.formData()`.
339
379
 
380
+ > [!WARNING]
381
+ > Calling `body.formData()` on a fetch response causes undici to buffer and parse the entire body. Since this is dictated by the spec, `body.formData()` must only be called on responses from trusted servers.
382
+
340
383
  Example usage:
341
384
 
342
385
  ```js
@@ -737,10 +780,11 @@ and `undici.Agent`) which will enable the family autoselection algorithm when es
737
780
  Undici aligns with the Node.js LTS schedule. The following table shows the supported versions:
738
781
 
739
782
  | Undici Version | Bundled in Node.js | Node.js Versions Supported | End of Life |
740
- |----------------|-------------------|----------------------------|-------------|
741
- | 5.x | 18.x | ≥14.0 (tested: 14, 16, 18) | 2024-04-30 |
742
- | 6.x | 20.x, 22.x | ≥18.17 (tested: 18, 20, 21, 22) | 2026-04-30 |
743
- | 7.x | 24.x | ≥20.18.1 (tested: 20, 22, 24) | 2027-04-30 |
783
+ |----------------|--------------------|----------------------------|-------------|
784
+ | 5.x | 18.x | ≥14.0 (tested: 14, 16, 18) | 2024-04-30 |
785
+ | 6.x | 20.x, 22.x | ≥18.17 (tested: 18, 20, 21, 22) | 2027-04-30 |
786
+ | 7.x | 24.x | ≥20.18.1 (tested: 20, 22, 24) | 2028-04-30 |
787
+ | 8.x | 26.x | ≥22.19.0 (tested: 22, 24, 26) | 2029-04-30 |
744
788
 
745
789
  ## License
746
790
 
@@ -21,6 +21,9 @@ Extends: [`PoolOptions`](/docs/docs/api/Pool.md#parameter-pooloptions)
21
21
  * **factory** `(origin: URL, opts: Object) => Dispatcher` - Default: `(origin, opts) => new Pool(origin, opts)`
22
22
  * **maxOrigins** `number` (optional) - Default: `Infinity` - Limits the total number of origins that can receive requests at a time, throwing an `MaxOriginsReachedError` error when attempting to dispatch when the max is reached. If `Infinity`, no limit is enforced.
23
23
 
24
+ > [!NOTE]
25
+ > Like `Pool`, `Agent` inherits all [`ClientOptions`](/docs/docs/api/Client.md#parameter-clientoptions). `allowH2` defaults to `true` and `maxConcurrentStreams` to `100`. The per-origin `Pool` it creates uses the default unlimited `connections`, so concurrent requests to the same origin land on separate `Client` instances and separate TCP/TLS sockets — HTTP/2 multiplexing on a shared session does not apply unless `connections` is set to a small value. See [`PoolOptions`](/docs/docs/api/Pool.md#parameter-pooloptions).
26
+
24
27
  ## Instance Properties
25
28
 
26
29
  ### `Agent.closed`
@@ -26,14 +26,16 @@ Returns: `Client`
26
26
  * **maxResponseSize** `number | null` (optional) - Default: `-1` - The maximum length of response body in bytes. Set to `-1` to disable.
27
27
  * **webSocket** `WebSocketOptions` (optional) - WebSocket-specific configuration options.
28
28
  * **maxPayloadSize** `number` (optional) - Default: `134217728` (128 MB) - Maximum allowed payload size in bytes for WebSocket messages. Applied to uncompressed messages, compressed frame payloads, and decompressed (permessage-deflate) messages. Set to 0 to disable the limit.
29
- * **pipelining** `number | null` (optional) - Default: `1` - The amount of concurrent requests to be sent over the single TCP/TLS connection according to [RFC7230](https://tools.ietf.org/html/rfc7230#section-6.3.2). Carefully consider your workload and environment before enabling concurrent requests as pipelining may reduce performance if used incorrectly. Pipelining is sensitive to network stack settings as well as head of line blocking caused by e.g. long running requests. Set to `0` to disable keep-alive connections.
30
- * **connect** `ConnectOptions | Function | null` (optional) - Default: `null`.
29
+ * **pipelining** `number | null` (optional) - Default: `1` - The amount of concurrent requests to be sent over the single TCP/TLS connection according to [RFC7230](https://tools.ietf.org/html/rfc7230#section-6.3.2). Carefully consider your workload and environment before enabling concurrent requests as pipelining may reduce performance if used incorrectly. Pipelining is sensitive to network stack settings as well as head of line blocking caused by e.g. long running requests. Set to `0` to disable keep-alive connections. This option has no effect once HTTP/2 is negotiated — see `maxConcurrentStreams` for the h2 dispatch ceiling.
30
+ * **connect** `ConnectOptions | Function | null` (optional) - Default: `null` - Configures how undici establishes TCP/TLS connections. Accepts two forms:
31
+ * **Object (`ConnectOptions`)**: Options passed directly to the internal [`buildConnector()`](/docs/docs/api/Connector.md). This is the simplest way to customize TLS or socket behavior (e.g., setting `rejectUnauthorized`, `ca`, `socketPath`). See [`ConnectOptions`](#parameter-connectoptions) for available fields.
32
+ * **Function**: A custom connector with the signature `(options, callback)`, where `options` contains `{ hostname, host, protocol, port, servername, localAddress, httpSocket }` and `callback` follows `(error, socket)`. Useful when you need full control over socket creation, such as adding custom validation or proxy logic. When a function is provided, undici wraps it to automatically inject `socketPath` and `allowH2` into the `options` argument if those values are set on the client.
31
33
  * **strictContentLength** `Boolean` (optional) - Default: `true` - Whether to treat request content length mismatches as errors. If true, an error is thrown when the request content-length header doesn't match the length of the request body. **Security Warning:** Disabling this option can expose your application to HTTP Request Smuggling attacks, where mismatched content-length headers cause servers and proxies to interpret request boundaries differently. This can lead to cache poisoning, credential hijacking, and bypassing security controls. Only disable this in controlled environments where you fully trust the request source.
32
34
  * **autoSelectFamily**: `boolean` (optional) - Default: depends on local Node version, on Node 18.13.0 and above is `false`. Enables a family autodetection algorithm that loosely implements section 5 of [RFC 8305](https://tools.ietf.org/html/rfc8305#section-5). See [here](https://nodejs.org/api/net.html#socketconnectoptions-connectlistener) for more details. This option is ignored if not supported by the current Node version.
33
35
  * **autoSelectFamilyAttemptTimeout**: `number` - Default: depends on local Node version, on Node 18.13.0 and above is `250`. The amount of time in milliseconds to wait for a connection attempt to finish before trying the next address when using the `autoSelectFamily` option. See [here](https://nodejs.org/api/net.html#socketconnectoptions-connectlistener) for more details.
34
36
  * **allowH2**: `boolean` - Default: `true`. Enables support for H2 if the server has assigned bigger priority to it through ALPN negotiation.
35
37
  * **useH2c**: `boolean` - Default: `false`. Enforces h2c for non-https connections.
36
- * **maxConcurrentStreams**: `number` - Default: `100`. Dictates the maximum number of concurrent streams for a single H2 session. It can be overridden by a SETTINGS remote frame.
38
+ * **maxConcurrentStreams**: `number` - Default: `100`. The maximum number of concurrent HTTP/2 streams per session. When `allowH2` negotiates h2, this — not `pipelining` (which is HTTP/1.1 only, per [RFC7230](https://tools.ietf.org/html/rfc7230#section-6.3.2)) — is the ceiling the Client uses to dispatch in-flight requests on a shared session. The same value is advertised to the server as `peerMaxConcurrentStreams`, capping how many streams the server may push back. The initial value is replaced by the server's `SETTINGS_MAX_CONCURRENT_STREAMS` whenever the server sends one, so a user-supplied value acts as a pre-`SETTINGS` default rather than a hard cap.
37
39
  * **initialWindowSize**: `number` (optional) - Default: `262144` (256KB). Sets the HTTP/2 stream-level flow-control window size (SETTINGS_INITIAL_WINDOW_SIZE). Must be a positive integer greater than 0. This default is higher than Node.js core's default (65535 bytes) to improve throughput, Node's choice is very conservative for current high-bandwith networks. See [RFC 7540 Section 6.9.2](https://datatracker.ietf.org/doc/html/rfc7540#section-6.9.2) for more details.
38
40
  * **connectionWindowSize**: `number` (optional) - Default `524288` (512KB). Sets the HTTP/2 connection-level flow-control window size using `ClientHttp2Session.setLocalWindowSize()`. Must be a positive integer greater than 0. This provides better flow control for the entire connection across multiple streams. See [Node.js HTTP/2 documentation](https://nodejs.org/api/http2.html#clienthttp2sessionsetlocalwindowsize) for more details.
39
41
  * **pingInterval**: `number` - Default: `60e3`. The time interval in milliseconds between PING frames sent to the server. Set to `0` to disable PING frames. This is only applicable for HTTP/2 connections. This will emit a `ping` event on the client with the duration of the ping in milliseconds.
@@ -72,9 +74,43 @@ import { Client } from 'undici'
72
74
  const client = new Client('http://localhost:3000')
73
75
  ```
74
76
 
75
- ### Example - Custom connector
77
+ ### Example - Connect with TLS options (object form)
76
78
 
77
- This will allow you to perform some additional check on the socket that will be used for the next request.
79
+ Pass a `ConnectOptions` object to customize the TLS connection. The options are forwarded to the internal `buildConnector()`.
80
+
81
+ ```js
82
+ 'use strict'
83
+ import { Client } from 'undici'
84
+ import fs from 'node:fs'
85
+
86
+ const client = new Client('https://localhost:3000', {
87
+ connect: {
88
+ rejectUnauthorized: false,
89
+ ca: fs.readFileSync('./ca-cert.pem')
90
+ }
91
+ })
92
+ ```
93
+
94
+ ### Example - Connect via Unix domain socket
95
+
96
+ Use the `socketPath` option to connect through an IPC endpoint instead of a TCP connection.
97
+
98
+ ```js
99
+ 'use strict'
100
+ import { Client } from 'undici'
101
+
102
+ const client = new Client('http://localhost:3000', {
103
+ connect: {
104
+ socketPath: '/var/run/docker.sock'
105
+ }
106
+ })
107
+ ```
108
+
109
+ ### Example - Custom connector (function form)
110
+
111
+ Pass a function for full control over socket creation. This allows you to perform additional checks on the socket, use a proxy, or implement custom connection logic.
112
+
113
+ > **Note:** When a function is provided, undici wraps it to automatically inject `socketPath` and `allowH2` into the first argument (`options`) when those values are set on the client.
78
114
 
79
115
  ```js
80
116
  'use strict'
@@ -97,6 +133,8 @@ const client = new Client('https://localhost:3000', {
97
133
  })
98
134
  ```
99
135
 
136
+ For more details on building custom connectors, see [Connector](/docs/docs/api/Connector.md).
137
+
100
138
  ## Instance Methods
101
139
 
102
140
  ### `Client.close([callback])`
@@ -13,6 +13,7 @@ Every Tls option, see [here](https://nodejs.org/api/tls.html#tls_tls_connect_opt
13
13
  Furthermore, the following options can be passed:
14
14
 
15
15
  * **socketPath** `string | null` (optional) - Default: `null` - An IPC endpoint, either Unix domain socket or Windows named pipe.
16
+ * **preferH2** `boolean` (optional) - Default: `false` - Only effective together with `allowH2`. When `true`, ALPN is offered as `['h2', 'http/1.1']` (HTTP/2 first) instead of the default `['http/1.1', 'h2']`. Use this when the server selects the ALPN protocol by *client* preference (e.g. some load balancers) so that HTTP/2 is negotiated whenever the server supports it. If the server does not support HTTP/2, ALPN transparently falls back to `http/1.1`.
16
17
  * **maxCachedSessions** `number | null` (optional) - Default: `100` - Maximum number of TLS cached sessions. Use 0 to disable TLS session caching. Default: `100`.
17
18
  * **timeout** `number | null` (optional) - In milliseconds. Default `10e3`.
18
19
  * **servername** `string | null` (optional)
@@ -991,6 +991,13 @@ The `redirect` interceptor allows you to customize the way your dispatcher handl
991
991
 
992
992
  It accepts the same arguments as the [`RedirectHandler` constructor](/docs/docs/api/RedirectHandler.md).
993
993
 
994
+ Options:
995
+
996
+ - **maxRedirections** `number` - Maximum number of redirections allowed.
997
+ - **throwOnMaxRedirect** `boolean` - Throw when the maximum number of redirections is reached.
998
+ - **stripHeadersOnRedirect** `string[]` - Header names to remove from all redirected requests.
999
+ - **stripHeadersOnCrossOriginRedirect** `string[]` - Header names to remove from cross-origin redirected requests.
1000
+
994
1001
  **Example - Basic Redirect Interceptor**
995
1002
 
996
1003
  ```js
@@ -27,8 +27,20 @@ import { errors } from 'undici'
27
27
  | `ResponseExceededMaxSizeError` | `UND_ERR_RES_EXCEEDED_MAX_SIZE` | response body exceed the max size allowed |
28
28
  | `SecureProxyConnectionError` | `UND_ERR_PRX_TLS` | tls connection to a proxy failed |
29
29
  | `MessageSizeExceededError` | `UND_ERR_WS_MESSAGE_SIZE_EXCEEDED` | WebSocket decompressed message exceeded the maximum allowed size |
30
+ | `AbortError` | `UND_ERR_ABORT` | the operation was aborted (base class of `RequestAbortedError`). |
31
+ | `RequestRetryError` | `UND_ERR_REQ_RETRY` | request failed and could not be retried; carries `statusCode`, `headers` and `data`. |
32
+ | `ResponseError` | `UND_ERR_RESPONSE` | response returned an error status code; carries `statusCode`, `headers` and `body`. |
33
+ | `MaxOriginsReachedError` | `UND_ERR_MAX_ORIGINS_REACHED` | the maximum number of allowed origins has been reached. |
34
+ | `BalancedPoolMissingUpstreamError` | `UND_ERR_BPL_MISSING_UPSTREAM` | no upstream has been added to the `BalancedPool`. |
35
+ | `Socks5ProxyError` | `UND_ERR_SOCKS5*` | an error occurred during SOCKS5 proxy negotiation. |
36
+ | `HTTPParserError` | `HPE_*` | an error occurred while parsing the HTTP response (extends `Error`, not `UndiciError`). |
30
37
 
31
38
  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.
39
+
40
+ ### `ConnectTimeoutError`
41
+
42
+ When `autoSelectFamily` is enabled and every attempted address fails with a timeout, Node raises an `AggregateError`. Undici surfaces these multi-address timeouts as `ConnectTimeoutError` (so the error shape is the same regardless of whether Node's family-attempt timer or undici's `connectTimeout` wins the race); the original `AggregateError` is preserved on `error.cause`.
43
+
32
44
  ### `SocketError`
33
45
 
34
46
  The `SocketError` has a `.socket` property which holds socket metadata:
@@ -7,7 +7,7 @@ for [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server
7
7
 
8
8
  ## Instantiating EventSource
9
9
 
10
- Undici exports a EventSource class. You can instantiate the EventSource as
10
+ Undici exports an EventSource class. You can instantiate the EventSource as
11
11
  follows:
12
12
 
13
13
  ```mjs
@@ -19,9 +19,57 @@ eventSource.onmessage = (event) => {
19
19
  }
20
20
  ```
21
21
 
22
+ ## Receiving events from a server
23
+
24
+ EventSource connects to an HTTP endpoint that responds with a `text/event-stream`
25
+ content type. The connection stays open and receives events as the server writes
26
+ them.
27
+
28
+ ```mjs
29
+ import { createServer } from 'node:http'
30
+ import { EventSource } from 'undici'
31
+
32
+ const server = createServer((request, response) => {
33
+ response.writeHead(200, {
34
+ 'content-type': 'text/event-stream',
35
+ 'cache-control': 'no-cache',
36
+ connection: 'keep-alive'
37
+ })
38
+
39
+ response.write('event: ping\n')
40
+ response.write('data: connected\n\n')
41
+
42
+ const interval = setInterval(() => {
43
+ response.write(`data: ${Date.now()}\n\n`)
44
+ }, 1000)
45
+
46
+ request.on('close', () => clearInterval(interval))
47
+ })
48
+
49
+ server.listen(3000, () => {
50
+ const eventSource = new EventSource('http://localhost:3000')
51
+
52
+ eventSource.addEventListener('ping', (event) => {
53
+ console.log('ping:', event.data)
54
+ })
55
+
56
+ eventSource.onmessage = (event) => {
57
+ console.log('message:', event.data)
58
+ }
59
+
60
+ eventSource.onerror = () => {
61
+ eventSource.close()
62
+ server.close()
63
+ }
64
+ })
65
+ ```
66
+
67
+ The `message` event receives events without an explicit `event:` field. Use
68
+ `addEventListener()` to subscribe to named events.
69
+
22
70
  ## Using a custom Dispatcher
23
71
 
24
- undici allows you to set your own Dispatcher in the EventSource constructor.
72
+ Undici allows you to set your own Dispatcher in the EventSource constructor.
25
73
 
26
74
  An example which allows you to modify the request headers is:
27
75
 
@@ -38,7 +86,6 @@ class CustomHeaderAgent extends Agent {
38
86
  const eventSource = new EventSource('http://localhost:3000', {
39
87
  dispatcher: new CustomHeaderAgent()
40
88
  })
41
-
42
89
  ```
43
90
 
44
91
  More information about the EventSource API can be found on
@@ -41,7 +41,9 @@ This API is implemented as per the standard, you can find documentation on [MDN]
41
41
  - [`.json()`](https://fetch.spec.whatwg.org/#dom-body-json)
42
42
  - [`.text()`](https://fetch.spec.whatwg.org/#dom-body-text)
43
43
 
44
- There is an ongoing discussion regarding `.formData()` and its usefulness and performance in server environments. It is recommended to use a dedicated library for parsing `multipart/form-data` bodies, such as [Busboy](https://www.npmjs.com/package/busboy) or [@fastify/busboy](https://www.npmjs.com/package/@fastify/busboy).
44
+ There is an ongoing discussion regarding `body.formData()` and its usefulness, performance, and security in server environments. Calling `body.formData()` causes undici to buffer and parse the entire body. Because multipart parsing has inherent security risks, `body.formData()` must only be called on responses from trusted servers.
45
+
46
+ For responses from untrusted or user-controlled servers, use a dedicated streaming library for parsing `multipart/form-data` bodies, such as [Busboy](https://www.npmjs.com/package/busboy) or [@fastify/busboy](https://www.npmjs.com/package/@fastify/busboy), and apply application-specific limits.
45
47
 
46
48
  These libraries can be interfaced with fetch with the following example code:
47
49
 
@@ -1,17 +1,17 @@
1
1
  # Global Installation
2
2
 
3
- Undici provides an `install()` function to add all WHATWG fetch classes to `globalThis`, making them available globally without requiring imports.
3
+ Undici provides an `install()` function to add fetch-related and other web API classes to `globalThis`, making them available globally without requiring imports.
4
4
 
5
5
  ## `install()`
6
6
 
7
- Install all WHATWG fetch classes globally on `globalThis`.
7
+ Install undici's global web APIs on `globalThis`.
8
8
 
9
9
  **Example:**
10
10
 
11
11
  ```js
12
12
  import { install } from 'undici'
13
13
 
14
- // Install all WHATWG fetch classes globally
14
+ // Install undici's global web APIs
15
15
  install()
16
16
 
17
17
  // Now you can use fetch classes globally without importing
@@ -74,6 +74,8 @@ await fetch('https://example.com', {
74
74
 
75
75
  After `install()`, `fetch`, `Headers`, `Response`, `Request`, and `FormData`
76
76
  all come from the installed `undici` package, so they work as a matching set.
77
+ `WebSocket`, `CloseEvent`, `ErrorEvent`, `MessageEvent`, and `EventSource`
78
+ also come from the installed `undici` package.
77
79
 
78
80
  If you do not want to install globals, import both from `undici` instead:
79
81
 
@@ -135,5 +137,5 @@ test('fetch API test', async () => {
135
137
 
136
138
  - The `install()` function overwrites any existing global implementations
137
139
  - Classes installed are undici's implementations, not Node.js built-ins
138
- - This provides access to undici's latest features and performance improvements
139
- - The global installation persists for the lifetime of the process
140
+ - This provides access to undici's latest fetch, WebSocket, and EventSource features and performance improvements
141
+ - The global installation persists for the lifetime of the process
@@ -46,8 +46,8 @@ Returns: `H2CClient`
46
46
  - **keepAliveTimeoutThreshold** `number | null` (optional) - Default: `2e3` - A number of milliseconds subtracted from server _keep-alive_ hints when overriding `keepAliveTimeout` to account for timing inaccuracies caused by e.g. transport latency. Defaults to 2 seconds.
47
47
  - **maxHeaderSize** `number | null` (optional) - Default: `--max-http-header-size` or `16384` - The maximum length of request headers in bytes. Defaults to Node.js' --max-http-header-size or 16KiB.
48
48
  - **maxResponseSize** `number | null` (optional) - Default: `-1` - The maximum length of response body in bytes. Set to `-1` to disable.
49
- - **maxConcurrentStreams**: `number` - Default: `100`. Dictates the maximum number of concurrent streams for a single H2 session. It can be overridden by a SETTINGS remote frame.
50
- - **pipelining** `number | null` (optional) - Default to `maxConcurrentStreams` - The amount of concurrent requests sent over a single HTTP/2 session in accordance with [RFC-7540](https://httpwg.org/specs/rfc7540.html#StreamsLayer) Stream specification. Streams can be closed up by remote server at any time.
49
+ - **maxConcurrentStreams**: `number` - Default: `100`. The maximum number of concurrent HTTP/2 streams per session also advertised to the server as `peerMaxConcurrentStreams` (the cap on streams the server may push back). The initial value is replaced by the server's `SETTINGS_MAX_CONCURRENT_STREAMS` whenever the server sends one, so a user-supplied value acts as a pre-`SETTINGS` default rather than a hard cap.
50
+ - **pipelining** `number | null` (optional) - Default to `maxConcurrentStreams` - The amount of concurrent requests sent over a single HTTP/2 session in accordance with [RFC-7540](https://httpwg.org/specs/rfc7540.html#StreamsLayer) Stream specification. Streams can be closed up by remote server at any time. Unlike on a regular [`Client`](/docs/docs/api/Client.md), `H2CClient` aliases `pipelining` to `maxConcurrentStreams` at construction time, so the two move together.
51
51
  - **pingInterval**: `number` - Default: `60e3`. The time interval in milliseconds between PING frames sent to the server. Set to `0` to disable PING frames. This is only applicable for HTTP/2 connections.
52
52
  - **connect** `ConnectOptions | null` (optional) - Default: `null`.
53
53
  - **strictContentLength** `Boolean` (optional) - Default: `true` - Whether to treat request content length mismatches as errors. If true, an error is thrown when the request content-length header doesn't match the length of the request body. **Security Warning:** Disabling this option can expose your application to HTTP Request Smuggling attacks, where mismatched content-length headers cause servers and proxies to interpret request boundaries differently. This can lead to cache poisoning, credential hijacking, and bypassing security controls. Only disable this in controlled environments where you fully trust the request source.
@@ -21,6 +21,9 @@ Extends: [`ClientOptions`](/docs/docs/api/Client.md#parameter-clientoptions)
21
21
  * **connections** `number | null` (optional) - Default: `null` - The number of `Client` instances to create. When set to `null`, the `Pool` instance will create an unlimited amount of `Client` instances.
22
22
  * **clientTtl** `number | null` (optional) - Default: `null` - The amount of time before a `Client` instance is removed from the `Pool` and closed. When set to `null`, `Client` instances will not be removed or closed based on age.
23
23
 
24
+ > [!NOTE]
25
+ > `Pool` inherits all [`ClientOptions`](/docs/docs/api/Client.md#parameter-clientoptions), including `allowH2` (default `true`) and `maxConcurrentStreams` (default `100`). With the unlimited default of `connections`, `Pool` will open a new `Client` — and therefore a new TCP/TLS socket — per concurrent dispatch, which defeats HTTP/2 multiplexing on a shared session. To benefit from h2 multiplexing on a single session, cap `connections` (e.g. `connections: 1`) so that concurrent requests share a session up to `maxConcurrentStreams`.
26
+
24
27
  ## Instance Properties
25
28
 
26
29
  ### `Pool.closed`
@@ -8,7 +8,7 @@ Arguments:
8
8
 
9
9
  - **dispatch** `function` - The dispatch function to be called after every retry.
10
10
  - **maxRedirections** `number` - Maximum number of redirections allowed.
11
- - **opts** `object` - Options for handling redirection.
11
+ - **opts** `object` - Options for handling redirection. Supports `throwOnMaxRedirect`, `stripHeadersOnRedirect`, and `stripHeadersOnCrossOriginRedirect`.
12
12
  - **handler** `object` - An object containing handlers for different stages of the request lifecycle.
13
13
 
14
14
  Returns: `RedirectHandler`
@@ -18,6 +18,9 @@ Returns: `RedirectHandler`
18
18
  - **dispatch** `(options: Dispatch.DispatchOptions, handlers: Dispatch.DispatchHandler) => Promise<Dispatch.DispatchResponse>` (required) - Dispatch function to be called after every redirection.
19
19
  - **maxRedirections** `number` (required) - Maximum number of redirections allowed.
20
20
  - **opts** `object` (required) - Options for handling redirection.
21
+ - **throwOnMaxRedirect** `boolean` - Throw when the maximum number of redirections is reached.
22
+ - **stripHeadersOnRedirect** `string[]` - Header names to remove from all redirected requests.
23
+ - **stripHeadersOnCrossOriginRedirect** `string[]` - Header names to remove from cross-origin redirected requests.
21
24
  - **handler** `object` (required) - Handlers for different stages of the request lifecycle.
22
25
 
23
26
  ### Properties
@@ -27,7 +27,9 @@ new SnapshotAgent([options])
27
27
  - **ignoreHeaders** `Array<String>` - Headers to ignore during request matching
28
28
  - **excludeHeaders** `Array<String>` - Headers to exclude from snapshots (for security)
29
29
  - **matchBody** `Boolean` - Whether to include request body in matching. Default: `true`
30
+ - **normalizeBody** `Function` - Optional function `(body) => string` to normalize the request body before matching (e.g. strip volatile fields like timestamps). Only used when `matchBody` is `true`.
30
31
  - **matchQuery** `Boolean` - Whether to include query parameters in matching. Default: `true`
32
+ - **normalizeQuery** `Function` - Optional function `(query: URLSearchParams) => string` to normalize query parameters before matching (e.g. strip volatile params like cache-busters). Only used when `matchQuery` is `true`.
31
33
  - **caseSensitive** `Boolean` - Whether header matching is case-sensitive. Default: `false`
32
34
  - **shouldRecord** `Function` - Callback to determine if a request should be recorded
33
35
  - **shouldPlayback** `Function` - Callback to determine if a request should be played back
@@ -108,6 +110,27 @@ await agent.saveSnapshots('./custom-snapshots.json')
108
110
 
109
111
  ## Advanced Configuration
110
112
 
113
+ ### Body Matching
114
+
115
+ By default (`matchBody: true`) the full request body string is included in the snapshot key. Set it to `false` to ignore the body entirely, or use `normalizeBody` to strip volatile fields (like timestamps) before matching:
116
+
117
+ ```javascript
118
+ const agent = new SnapshotAgent({
119
+ mode: 'playback',
120
+ snapshotPath: './snapshots.json',
121
+
122
+ // Match on everything except the timestamp field
123
+ normalizeBody: (body) => {
124
+ if (!body) return ''
125
+ const parsed = JSON.parse(String(body))
126
+ delete parsed.timestamp
127
+ return JSON.stringify(parsed)
128
+ }
129
+ })
130
+ ```
131
+
132
+ `normalizeBody` receives the raw body (`string | Buffer | null | undefined`) and must return a `string`. It runs at both record and playback time so the hash is consistent. Two requests match the same snapshot whenever their normalized strings are identical.
133
+
111
134
  ### Header Filtering
112
135
 
113
136
  Control which headers are used for request matching and what gets stored in snapshots:
@@ -13,6 +13,7 @@ const {
13
13
  RequestAbortedError
14
14
  } = require('../core/errors')
15
15
  const util = require('../core/util')
16
+ const { kBodyUsed } = require('../core/symbols')
16
17
  const { addSignal, removeSignal } = require('./abort-signal')
17
18
 
18
19
  function noop () {}
@@ -24,6 +25,9 @@ class PipelineRequest extends Readable {
24
25
  super({ autoDestroy: true })
25
26
 
26
27
  this[kResume] = null
28
+ // Pipeline request bodies come from a live writable side and cannot be
29
+ // replayed across redirects or retries, even before any bytes are read.
30
+ this[kBodyUsed] = true
27
31
  }
28
32
 
29
33
  _read () {
@@ -1,7 +1,6 @@
1
1
  'use strict'
2
2
 
3
3
  const assert = require('node:assert')
4
- const { finished } = require('node:stream')
5
4
  const { AsyncResource } = require('node:async_hooks')
6
5
  const { InvalidArgumentError, InvalidReturnValueError } = require('../core/errors')
7
6
  const util = require('../core/util')
@@ -9,6 +8,54 @@ const { addSignal, removeSignal } = require('./abort-signal')
9
8
 
10
9
  function noop () {}
11
10
 
11
+ function getWritableError (stream) {
12
+ return stream.errored ?? stream.writableErrored ?? stream._writableState?.errored
13
+ }
14
+
15
+ function createPrematureCloseError () {
16
+ const err = new Error('Premature close')
17
+ err.code = 'ERR_STREAM_PREMATURE_CLOSE'
18
+ return err
19
+ }
20
+
21
+ function trackWritableLifecycle (stream, callback) {
22
+ let done = false
23
+
24
+ const cleanup = () => {
25
+ stream.removeListener('close', onClose)
26
+ stream.removeListener('error', onError)
27
+ stream.removeListener('finish', onFinish)
28
+ }
29
+
30
+ const finish = (err, fromErrorEvent = false) => {
31
+ if (done) {
32
+ return
33
+ }
34
+
35
+ done = true
36
+ cleanup()
37
+ callback(err, fromErrorEvent)
38
+ }
39
+
40
+ const onClose = () => {
41
+ const err = getWritableError(stream)
42
+ finish(err ?? (!stream.writableFinished ? createPrematureCloseError() : undefined))
43
+ }
44
+
45
+ const onError = (err) => finish(err, true)
46
+ const onFinish = () => finish()
47
+
48
+ stream.on('close', onClose)
49
+ stream.on('error', onError)
50
+ stream.on('finish', onFinish)
51
+
52
+ if (stream.closed) {
53
+ process.nextTick(onClose)
54
+ } else if (stream.writableFinished) {
55
+ process.nextTick(onFinish)
56
+ }
57
+ }
58
+
12
59
  class StreamHandler extends AsyncResource {
13
60
  constructor (opts, factory, callback) {
14
61
  if (!opts || typeof opts !== 'object') {
@@ -117,20 +164,19 @@ class StreamHandler extends AsyncResource {
117
164
  throw new InvalidReturnValueError('expected Writable')
118
165
  }
119
166
 
120
- // TODO: Avoid finished. It registers an unnecessary amount of listeners.
121
- finished(res, { readable: false }, (err) => {
167
+ trackWritableLifecycle(res, (err, fromErrorEvent) => {
122
168
  const { callback, res, opaque, trailers, abort } = this
123
169
 
124
170
  this.res = null
125
171
  if (err || !res?.readable) {
126
- util.destroy(res, err)
172
+ util.destroy(res, fromErrorEvent ? undefined : err)
127
173
  }
128
174
 
129
175
  this.callback = null
130
176
  this.runInAsyncScope(callback, null, err || null, { opaque, trailers })
131
177
 
132
178
  if (err) {
133
- abort()
179
+ abort(err)
134
180
  }
135
181
  })
136
182