undici 8.3.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.
- package/README.md +59 -18
- package/docs/docs/api/Agent.md +3 -0
- package/docs/docs/api/Client.md +43 -5
- package/docs/docs/api/Connector.md +1 -0
- package/docs/docs/api/Dispatcher.md +7 -0
- package/docs/docs/api/Errors.md +12 -0
- package/docs/docs/api/EventSource.md +50 -3
- package/docs/docs/api/Fetch.md +3 -1
- package/docs/docs/api/H2CClient.md +2 -2
- package/docs/docs/api/Pool.md +3 -0
- package/docs/docs/api/RedirectHandler.md +4 -1
- package/lib/core/connect.js +29 -4
- package/lib/core/util.js +8 -6
- package/lib/dispatcher/client-h2.js +53 -12
- package/lib/dispatcher/client.js +22 -3
- package/lib/handler/redirect-handler.js +36 -11
- package/lib/interceptor/redirect.js +3 -3
- package/lib/mock/mock-call-history.js +1 -1
- package/lib/mock/snapshot-agent.js +9 -1
- package/lib/web/fetch/index.js +17 -3
- package/lib/web/fetch/request.js +32 -3
- package/package.json +1 -1
- package/types/connector.d.ts +1 -0
- package/types/fetch.d.ts +4 -1
- package/types/interceptors.d.ts +1 -1
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
|
|
25
|
-
50 TCP connections with a pipelining depth of 10 running on Node
|
|
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
|
-
│ '
|
|
32
|
-
│ '
|
|
33
|
-
│ '
|
|
34
|
-
│ '
|
|
35
|
-
│ '
|
|
36
|
-
│ 'got' │
|
|
37
|
-
│ '
|
|
38
|
-
│ '
|
|
39
|
-
│ 'undici - pipeline' │
|
|
40
|
-
│ 'undici -
|
|
41
|
-
│ 'undici -
|
|
42
|
-
│ 'undici - dispatch' │
|
|
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
|
|
@@ -340,6 +377,9 @@ The `body` mixins are the most common way to format the request/response body. M
|
|
|
340
377
|
> [!NOTE]
|
|
341
378
|
> The body returned from `undici.request` does not implement `.formData()`.
|
|
342
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
|
+
|
|
343
383
|
Example usage:
|
|
344
384
|
|
|
345
385
|
```js
|
|
@@ -740,10 +780,11 @@ and `undici.Agent`) which will enable the family autoselection algorithm when es
|
|
|
740
780
|
Undici aligns with the Node.js LTS schedule. The following table shows the supported versions:
|
|
741
781
|
|
|
742
782
|
| Undici Version | Bundled in Node.js | Node.js Versions Supported | End of Life |
|
|
743
|
-
|
|
744
|
-
| 5.x
|
|
745
|
-
| 6.x
|
|
746
|
-
| 7.x
|
|
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 |
|
|
747
788
|
|
|
748
789
|
## License
|
|
749
790
|
|
package/docs/docs/api/Agent.md
CHANGED
|
@@ -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`
|
package/docs/docs/api/Client.md
CHANGED
|
@@ -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`.
|
|
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 -
|
|
77
|
+
### Example - Connect with TLS options (object form)
|
|
76
78
|
|
|
77
|
-
|
|
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
|
package/docs/docs/api/Errors.md
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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
|
package/docs/docs/api/Fetch.md
CHANGED
|
@@ -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
|
|
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
|
|
|
@@ -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`.
|
|
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.
|
package/docs/docs/api/Pool.md
CHANGED
|
@@ -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
|
package/lib/core/connect.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
const net = require('node:net')
|
|
4
4
|
const assert = require('node:assert')
|
|
5
5
|
const util = require('./util')
|
|
6
|
-
const { InvalidArgumentError } = require('./errors')
|
|
6
|
+
const { InvalidArgumentError, ConnectTimeoutError } = require('./errors')
|
|
7
7
|
|
|
8
8
|
let tls // include tls conditionally since it is not always available
|
|
9
9
|
|
|
@@ -59,7 +59,7 @@ const SessionCache = class WeakSessionCache {
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
function buildConnector ({ allowH2, useH2c, maxCachedSessions, socketPath, timeout, session: customSession, ...opts }) {
|
|
62
|
+
function buildConnector ({ allowH2, preferH2, useH2c, maxCachedSessions, socketPath, timeout, session: customSession, ...opts }) {
|
|
63
63
|
if (maxCachedSessions != null && (!Number.isInteger(maxCachedSessions) || maxCachedSessions < 0)) {
|
|
64
64
|
throw new InvalidArgumentError('maxCachedSessions must be a positive integer or zero')
|
|
65
65
|
}
|
|
@@ -89,7 +89,7 @@ function buildConnector ({ allowH2, useH2c, maxCachedSessions, socketPath, timeo
|
|
|
89
89
|
servername,
|
|
90
90
|
session,
|
|
91
91
|
localAddress,
|
|
92
|
-
ALPNProtocols: allowH2 ? ['http/1.1', 'h2'] : ['http/1.1'],
|
|
92
|
+
ALPNProtocols: allowH2 ? (preferH2 ? ['h2', 'http/1.1'] : ['http/1.1', 'h2']) : ['http/1.1'],
|
|
93
93
|
socket: httpSocket, // upgrade socket connection
|
|
94
94
|
port,
|
|
95
95
|
host: hostname
|
|
@@ -142,7 +142,7 @@ function buildConnector ({ allowH2, useH2c, maxCachedSessions, socketPath, timeo
|
|
|
142
142
|
if (callback) {
|
|
143
143
|
const cb = callback
|
|
144
144
|
callback = null
|
|
145
|
-
cb(err)
|
|
145
|
+
cb(maybeNormalizeConnectError(err, this, { timeout, hostname, port }))
|
|
146
146
|
}
|
|
147
147
|
})
|
|
148
148
|
|
|
@@ -150,4 +150,29 @@ function buildConnector ({ allowH2, useH2c, maxCachedSessions, socketPath, timeo
|
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
152
|
|
|
153
|
+
// `net.connect` with `autoSelectFamily` raises an `AggregateError` when every
|
|
154
|
+
// attempted address fails. If any of those failures is a timeout, surface the
|
|
155
|
+
// error as a `ConnectTimeoutError` so callers see the same error regardless of
|
|
156
|
+
// which timer (Node's internal one or undici's `connectTimeout`) wins the race.
|
|
157
|
+
// The original `AggregateError` is preserved on `.cause`.
|
|
158
|
+
function maybeNormalizeConnectError (err, socket, opts) {
|
|
159
|
+
if (
|
|
160
|
+
err instanceof AggregateError &&
|
|
161
|
+
(err.code === 'ETIMEDOUT' || err.errors.some((e) => e != null && e.code === 'ETIMEDOUT'))
|
|
162
|
+
) {
|
|
163
|
+
let message = 'Connect Timeout Error'
|
|
164
|
+
if (Array.isArray(socket.autoSelectFamilyAttemptedAddresses)) {
|
|
165
|
+
message += ` (attempted addresses: ${socket.autoSelectFamilyAttemptedAddresses.join(', ')},`
|
|
166
|
+
} else {
|
|
167
|
+
message += ` (attempted address: ${opts.hostname}:${opts.port},`
|
|
168
|
+
}
|
|
169
|
+
message += ` timeout: ${opts.timeout}ms)`
|
|
170
|
+
|
|
171
|
+
const wrapped = new ConnectTimeoutError(message)
|
|
172
|
+
wrapped.cause = err
|
|
173
|
+
return wrapped
|
|
174
|
+
}
|
|
175
|
+
return err
|
|
176
|
+
}
|
|
177
|
+
|
|
153
178
|
module.exports = buildConnector
|
package/lib/core/util.js
CHANGED
|
@@ -698,9 +698,8 @@ function isFormDataLike (object) {
|
|
|
698
698
|
}
|
|
699
699
|
|
|
700
700
|
function addAbortListener (signal, listener) {
|
|
701
|
-
if (signal
|
|
702
|
-
|
|
703
|
-
return () => disposable[Symbol.dispose]()
|
|
701
|
+
if (!signal || 'aborted' in signal) {
|
|
702
|
+
return addAbortListenerNative(signal, listener)[Symbol.dispose]
|
|
704
703
|
}
|
|
705
704
|
|
|
706
705
|
if (typeof signal.addEventListener === 'function') {
|
|
@@ -793,8 +792,9 @@ const rangeHeaderRegex = /^bytes (\d+)-(\d+)\/(\d+|\*)?$/
|
|
|
793
792
|
*/
|
|
794
793
|
function parseRangeHeader (range) {
|
|
795
794
|
if (range == null || range === '') return { start: 0, end: null, size: null }
|
|
795
|
+
if (!range) return null
|
|
796
796
|
|
|
797
|
-
const m =
|
|
797
|
+
const m = rangeHeaderRegex.exec(range)
|
|
798
798
|
return m
|
|
799
799
|
? {
|
|
800
800
|
start: parseInt(m[1]),
|
|
@@ -943,8 +943,10 @@ function getProtocolFromUrlString (urlString) {
|
|
|
943
943
|
return urlString.slice(0, urlString.indexOf(':') + 1)
|
|
944
944
|
}
|
|
945
945
|
|
|
946
|
-
const kEnumerableProperty =
|
|
947
|
-
|
|
946
|
+
const kEnumerableProperty = {
|
|
947
|
+
__proto__: null,
|
|
948
|
+
enumerable: true
|
|
949
|
+
}
|
|
948
950
|
|
|
949
951
|
const normalizedMethodRecordsBase = {
|
|
950
952
|
delete: 'DELETE',
|
|
@@ -8,7 +8,9 @@ const {
|
|
|
8
8
|
RequestAbortedError,
|
|
9
9
|
SocketError,
|
|
10
10
|
InformationalError,
|
|
11
|
-
InvalidArgumentError
|
|
11
|
+
InvalidArgumentError,
|
|
12
|
+
HeadersTimeoutError,
|
|
13
|
+
BodyTimeoutError
|
|
12
14
|
} = require('../core/errors.js')
|
|
13
15
|
const {
|
|
14
16
|
kUrl,
|
|
@@ -33,6 +35,7 @@ const {
|
|
|
33
35
|
kSize,
|
|
34
36
|
kHTTPContext,
|
|
35
37
|
kClosed,
|
|
38
|
+
kHeadersTimeout,
|
|
36
39
|
kBodyTimeout,
|
|
37
40
|
kEnableConnectProtocol,
|
|
38
41
|
kRemoteSettings,
|
|
@@ -81,6 +84,29 @@ function getGoAwayError (session, errorCode) {
|
|
|
81
84
|
: new SocketError(`HTTP/2: "GOAWAY" frame received with code ${errorCode}`, util.getSocketInfo(session[kSocket])))
|
|
82
85
|
}
|
|
83
86
|
|
|
87
|
+
function resetHttp2Session (session, err) {
|
|
88
|
+
const client = session[kClient]
|
|
89
|
+
const socket = session[kSocket]
|
|
90
|
+
|
|
91
|
+
if (client[kHTTP2Session] === session) {
|
|
92
|
+
client[kSocket] = null
|
|
93
|
+
client[kHTTPContext] = null
|
|
94
|
+
client[kHTTP2Session] = null
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (socket != null && socket[kError] == null) {
|
|
98
|
+
socket[kError] = err
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (!session.closed && !session.destroyed) {
|
|
102
|
+
try {
|
|
103
|
+
session.destroy(err)
|
|
104
|
+
} catch {}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
util.destroy(socket, err)
|
|
108
|
+
}
|
|
109
|
+
|
|
84
110
|
function getGoAwayPendingIdx (client, lastStreamID) {
|
|
85
111
|
const maxAcceptedStreamID = Number.isInteger(lastStreamID) ? lastStreamID : Number.MAX_SAFE_INTEGER
|
|
86
112
|
|
|
@@ -122,6 +148,10 @@ function clearRequestStream (request) {
|
|
|
122
148
|
cleanup?.(stream)
|
|
123
149
|
}
|
|
124
150
|
|
|
151
|
+
function requeueUnsentRequest (client, request) {
|
|
152
|
+
client[kQueue].splice(client[kPendingIdx] + 1, 0, request)
|
|
153
|
+
}
|
|
154
|
+
|
|
125
155
|
function canRetryRequestAfterGoAway (request) {
|
|
126
156
|
const { body } = request
|
|
127
157
|
|
|
@@ -629,7 +659,7 @@ function onUpgradeStreamEnd () {
|
|
|
629
659
|
|
|
630
660
|
function onUpgradeStreamTimeout () {
|
|
631
661
|
const state = this[kRequestStreamState]
|
|
632
|
-
failUpgradeStream(state, new InformationalError(`HTTP/2: "stream timeout after ${state.
|
|
662
|
+
failUpgradeStream(state, new InformationalError(`HTTP/2: "stream timeout after ${state.headersTimeout}"`))
|
|
633
663
|
}
|
|
634
664
|
|
|
635
665
|
function onUpgradeResponse (headers, _flags) {
|
|
@@ -654,7 +684,7 @@ function onUpgradeResponse (headers, _flags) {
|
|
|
654
684
|
}
|
|
655
685
|
|
|
656
686
|
function setupUpgradeStream (stream, state) {
|
|
657
|
-
const { request,
|
|
687
|
+
const { request, headersTimeout, session } = state
|
|
658
688
|
|
|
659
689
|
stream[kHTTP2Stream] = true
|
|
660
690
|
stream[kHTTP2Session] = session
|
|
@@ -669,11 +699,12 @@ function setupUpgradeStream (stream, state) {
|
|
|
669
699
|
stream.once('close', onUpgradeStreamClose)
|
|
670
700
|
|
|
671
701
|
++session[kOpenStreams]
|
|
672
|
-
stream.setTimeout(
|
|
702
|
+
stream.setTimeout(headersTimeout)
|
|
673
703
|
}
|
|
674
704
|
|
|
675
705
|
function writeH2 (client, request) {
|
|
676
|
-
const
|
|
706
|
+
const headersTimeout = request.headersTimeout ?? client[kHeadersTimeout]
|
|
707
|
+
const bodyTimeout = request.bodyTimeout ?? client[kBodyTimeout]
|
|
677
708
|
const session = client[kHTTP2Session]
|
|
678
709
|
const { method, path, host, upgrade, expectContinue, signal, protocol, headers: reqHeaders } = request
|
|
679
710
|
let { body } = request
|
|
@@ -736,8 +767,14 @@ function writeH2 (client, request) {
|
|
|
736
767
|
try {
|
|
737
768
|
return session.request(headers, options)
|
|
738
769
|
} catch (err) {
|
|
739
|
-
if (err?.code
|
|
740
|
-
|
|
770
|
+
if (err?.code === 'ERR_HTTP2_INVALID_SESSION') {
|
|
771
|
+
const wrappedErr = new SocketError(err.message, util.getSocketInfo(session[kSocket]))
|
|
772
|
+
wrappedErr.cause = err
|
|
773
|
+
session[kError] = wrappedErr
|
|
774
|
+
resetHttp2Session(session, wrappedErr)
|
|
775
|
+
requeueUnsentRequest(client, request)
|
|
776
|
+
|
|
777
|
+
return null
|
|
741
778
|
}
|
|
742
779
|
|
|
743
780
|
const wrappedErr = new InformationalError(err.message, { cause: err })
|
|
@@ -771,7 +808,8 @@ function writeH2 (client, request) {
|
|
|
771
808
|
abort,
|
|
772
809
|
finalizeRequest,
|
|
773
810
|
request,
|
|
774
|
-
|
|
811
|
+
headersTimeout,
|
|
812
|
+
bodyTimeout,
|
|
775
813
|
responseReceived: false,
|
|
776
814
|
session,
|
|
777
815
|
stream: null
|
|
@@ -912,7 +950,8 @@ function writeH2 (client, request) {
|
|
|
912
950
|
expectsPayload,
|
|
913
951
|
finalizeRequest,
|
|
914
952
|
request,
|
|
915
|
-
|
|
953
|
+
headersTimeout,
|
|
954
|
+
bodyTimeout,
|
|
916
955
|
responseReceived: false,
|
|
917
956
|
session,
|
|
918
957
|
stream: null
|
|
@@ -929,11 +968,10 @@ function writeH2 (client, request) {
|
|
|
929
968
|
stream[kHTTP2Stream] = true
|
|
930
969
|
stream[kRequestStreamState] = state
|
|
931
970
|
state.stream = stream
|
|
932
|
-
bindRequestToStream(request, stream, null)
|
|
933
971
|
|
|
934
972
|
// Increment counter as we have new streams open
|
|
935
973
|
++session[kOpenStreams]
|
|
936
|
-
stream.setTimeout(
|
|
974
|
+
stream.setTimeout(headersTimeout)
|
|
937
975
|
|
|
938
976
|
stream[kHTTP2Session] = session
|
|
939
977
|
stream.once('close', onRequestStreamClose)
|
|
@@ -1017,6 +1055,7 @@ function onResponse (headers) {
|
|
|
1017
1055
|
delete headers[HTTP2_HEADER_STATUS]
|
|
1018
1056
|
request.onResponseStarted()
|
|
1019
1057
|
state.responseReceived = true
|
|
1058
|
+
stream.setTimeout(state.bodyTimeout)
|
|
1020
1059
|
|
|
1021
1060
|
// Due to the stream nature, it is possible we face a race condition
|
|
1022
1061
|
// where the stream has been assigned, but the request has been aborted
|
|
@@ -1087,7 +1126,9 @@ function onTimeout () {
|
|
|
1087
1126
|
|
|
1088
1127
|
releaseRequestStream(stream)
|
|
1089
1128
|
|
|
1090
|
-
const err =
|
|
1129
|
+
const err = state.responseReceived
|
|
1130
|
+
? new BodyTimeoutError(`HTTP/2: "stream timeout after ${state.bodyTimeout}"`)
|
|
1131
|
+
: new HeadersTimeoutError(`HTTP/2: "headers timeout after ${state.headersTimeout}"`)
|
|
1091
1132
|
state.abort(err)
|
|
1092
1133
|
}
|
|
1093
1134
|
|
package/lib/dispatcher/client.js
CHANGED
|
@@ -76,6 +76,18 @@ function getPipelining (client) {
|
|
|
76
76
|
return client[kPipelining] ?? client[kHTTPContext]?.defaultPipelining ?? 1
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
+
// Protocol-aware dispatch ceiling. h1 RFC7230 pipelining is unrelated to h2
|
|
80
|
+
// stream multiplexing — over h2 the ceiling is the (server-confirmed)
|
|
81
|
+
// maxConcurrentStreams. Before a context is attached we use the h1
|
|
82
|
+
// pipelining factor; once h2 attaches the queued requests can drain in
|
|
83
|
+
// one batch up to maxConcurrentStreams.
|
|
84
|
+
function getMaxConcurrent (client) {
|
|
85
|
+
if (client[kHTTPContext]?.version === 'h2') {
|
|
86
|
+
return client[kMaxConcurrentStreams]
|
|
87
|
+
}
|
|
88
|
+
return getPipelining(client)
|
|
89
|
+
}
|
|
90
|
+
|
|
79
91
|
/**
|
|
80
92
|
* @type {import('../../types/client.js').default}
|
|
81
93
|
*/
|
|
@@ -326,10 +338,17 @@ class Client extends DispatcherBase {
|
|
|
326
338
|
}
|
|
327
339
|
|
|
328
340
|
get [kBusy] () {
|
|
341
|
+
// The `kPending > 0` check below is the gate Pool uses to decide whether
|
|
342
|
+
// to spin up an additional Client. For h1 that fan-out is correct —
|
|
343
|
+
// each socket only handles one pipelined request at a time. Once an h2
|
|
344
|
+
// context is attached we want concurrent dispatches to multiplex onto
|
|
345
|
+
// the shared session, so suppress that signal in the h2 case.
|
|
346
|
+
const allowsMux = this[kHTTPContext]?.version === 'h2'
|
|
347
|
+
|
|
329
348
|
return Boolean(
|
|
330
349
|
this[kHTTPContext]?.busy(null) ||
|
|
331
|
-
(this[kSize] >= (
|
|
332
|
-
this[kPending] > 0
|
|
350
|
+
(this[kSize] >= (getMaxConcurrent(this) || 1)) ||
|
|
351
|
+
(this[kPending] > 0 && !allowsMux)
|
|
333
352
|
)
|
|
334
353
|
}
|
|
335
354
|
|
|
@@ -616,7 +635,7 @@ function _resume (client, sync) {
|
|
|
616
635
|
return
|
|
617
636
|
}
|
|
618
637
|
|
|
619
|
-
if (client[kRunning] >= (
|
|
638
|
+
if (client[kRunning] >= (getMaxConcurrent(client) || 1)) {
|
|
620
639
|
return
|
|
621
640
|
}
|
|
622
641
|
|
|
@@ -29,9 +29,11 @@ class RedirectHandler {
|
|
|
29
29
|
|
|
30
30
|
this.dispatch = dispatch
|
|
31
31
|
this.location = null
|
|
32
|
-
const { maxRedirections: _, ...cleanOpts } = opts
|
|
32
|
+
const { maxRedirections: _, stripHeadersOnRedirect, stripHeadersOnCrossOriginRedirect, ...cleanOpts } = opts
|
|
33
33
|
this.opts = cleanOpts // opts must be a copy, exclude maxRedirections
|
|
34
34
|
this.opts.body = util.wrapRequestBody(this.opts.body)
|
|
35
|
+
this.stripHeadersOnRedirect = normalizeStripHeaders(stripHeadersOnRedirect, 'stripHeadersOnRedirect')
|
|
36
|
+
this.stripHeadersOnCrossOriginRedirect = normalizeStripHeaders(stripHeadersOnCrossOriginRedirect, 'stripHeadersOnCrossOriginRedirect')
|
|
35
37
|
this.maxRedirections = maxRedirections
|
|
36
38
|
this.handler = handler
|
|
37
39
|
this.history = []
|
|
@@ -100,7 +102,7 @@ class RedirectHandler {
|
|
|
100
102
|
// Remove headers referring to the original URL.
|
|
101
103
|
// By default it is Host only, unless it's a 303 (see below), which removes also all Content-* headers.
|
|
102
104
|
// https://tools.ietf.org/html/rfc7231#section-6.4
|
|
103
|
-
this.opts.headers = cleanRequestHeaders(this.opts.headers, statusCode === 303, this.opts.origin !== origin)
|
|
105
|
+
this.opts.headers = cleanRequestHeaders(this.opts.headers, statusCode === 303, this.opts.origin !== origin, this.stripHeadersOnRedirect, this.stripHeadersOnCrossOriginRedirect)
|
|
104
106
|
this.opts.path = path
|
|
105
107
|
this.opts.origin = origin
|
|
106
108
|
this.opts.query = null
|
|
@@ -152,26 +154,49 @@ class RedirectHandler {
|
|
|
152
154
|
}
|
|
153
155
|
|
|
154
156
|
// https://tools.ietf.org/html/rfc7231#section-6.4.4
|
|
155
|
-
function shouldRemoveHeader (header, removeContent, unknownOrigin) {
|
|
156
|
-
|
|
157
|
-
|
|
157
|
+
function shouldRemoveHeader (header, removeContent, unknownOrigin, stripHeaders, stripHeadersOnCrossOrigin) {
|
|
158
|
+
const name = util.headerNameToString(header)
|
|
159
|
+
if (name === 'host') {
|
|
160
|
+
return true
|
|
161
|
+
}
|
|
162
|
+
if (stripHeaders?.has(name) || (unknownOrigin && stripHeadersOnCrossOrigin?.has(name))) {
|
|
163
|
+
return true
|
|
158
164
|
}
|
|
159
|
-
if (removeContent &&
|
|
165
|
+
if (removeContent && name.startsWith('content-')) {
|
|
160
166
|
return true
|
|
161
167
|
}
|
|
162
|
-
if (unknownOrigin
|
|
163
|
-
const name = util.headerNameToString(header)
|
|
168
|
+
if (unknownOrigin) {
|
|
164
169
|
return name === 'authorization' || name === 'cookie' || name === 'proxy-authorization'
|
|
165
170
|
}
|
|
166
171
|
return false
|
|
167
172
|
}
|
|
168
173
|
|
|
169
174
|
// https://tools.ietf.org/html/rfc7231#section-6.4
|
|
170
|
-
function
|
|
175
|
+
function normalizeStripHeaders (headers, optionName) {
|
|
176
|
+
if (headers == null) {
|
|
177
|
+
return null
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (!Array.isArray(headers)) {
|
|
181
|
+
throw new InvalidArgumentError(`${optionName} must be an array`)
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const normalized = new Set()
|
|
185
|
+
for (const header of headers) {
|
|
186
|
+
if (typeof header !== 'string') {
|
|
187
|
+
throw new InvalidArgumentError(`${optionName} must contain header names`)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
normalized.add(util.headerNameToString(header))
|
|
191
|
+
}
|
|
192
|
+
return normalized
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function cleanRequestHeaders (headers, removeContent, unknownOrigin, stripHeaders, stripHeadersOnCrossOrigin) {
|
|
171
196
|
const ret = []
|
|
172
197
|
if (Array.isArray(headers)) {
|
|
173
198
|
for (let i = 0; i < headers.length; i += 2) {
|
|
174
|
-
if (!shouldRemoveHeader(headers[i], removeContent, unknownOrigin)) {
|
|
199
|
+
if (!shouldRemoveHeader(headers[i], removeContent, unknownOrigin, stripHeaders, stripHeadersOnCrossOrigin)) {
|
|
175
200
|
ret.push(headers[i], headers[i + 1])
|
|
176
201
|
}
|
|
177
202
|
}
|
|
@@ -179,7 +204,7 @@ function cleanRequestHeaders (headers, removeContent, unknownOrigin) {
|
|
|
179
204
|
const entries = util.hasSafeIterator(headers) ? headers : Object.entries(headers)
|
|
180
205
|
|
|
181
206
|
for (const [key, value] of entries) {
|
|
182
|
-
if (!shouldRemoveHeader(key, removeContent, unknownOrigin)) {
|
|
207
|
+
if (!shouldRemoveHeader(key, removeContent, unknownOrigin, stripHeaders, stripHeadersOnCrossOrigin)) {
|
|
183
208
|
ret.push(key, value)
|
|
184
209
|
}
|
|
185
210
|
}
|
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
const RedirectHandler = require('../handler/redirect-handler')
|
|
4
4
|
|
|
5
|
-
function createRedirectInterceptor ({ maxRedirections: defaultMaxRedirections, throwOnMaxRedirect: defaultThrowOnMaxRedirect } = {}) {
|
|
5
|
+
function createRedirectInterceptor ({ maxRedirections: defaultMaxRedirections, throwOnMaxRedirect: defaultThrowOnMaxRedirect, stripHeadersOnRedirect: defaultStripHeadersOnRedirect, stripHeadersOnCrossOriginRedirect: defaultStripHeadersOnCrossOriginRedirect } = {}) {
|
|
6
6
|
return (dispatch) => {
|
|
7
7
|
return function Intercept (opts, handler) {
|
|
8
|
-
const { maxRedirections = defaultMaxRedirections, throwOnMaxRedirect = defaultThrowOnMaxRedirect, ...rest } = opts
|
|
8
|
+
const { maxRedirections = defaultMaxRedirections, throwOnMaxRedirect = defaultThrowOnMaxRedirect, stripHeadersOnRedirect = defaultStripHeadersOnRedirect, stripHeadersOnCrossOriginRedirect = defaultStripHeadersOnCrossOriginRedirect, ...rest } = opts
|
|
9
9
|
|
|
10
10
|
if (maxRedirections == null || maxRedirections === 0) {
|
|
11
11
|
return dispatch(opts, handler)
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
const dispatchOpts = { ...rest, throwOnMaxRedirect } // Stop sub dispatcher from also redirecting.
|
|
14
|
+
const dispatchOpts = { ...rest, throwOnMaxRedirect, stripHeadersOnRedirect, stripHeadersOnCrossOriginRedirect } // Stop sub dispatcher from also redirecting.
|
|
15
15
|
const redirectHandler = new RedirectHandler(dispatch, maxRedirections, dispatchOpts, handler)
|
|
16
16
|
return dispatch(dispatchOpts, redirectHandler)
|
|
17
17
|
}
|
|
@@ -35,7 +35,7 @@ function buildAndValidateFilterCallsOptions (options = {}) {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
function makeFilterCalls (parameterName) {
|
|
38
|
-
return (parameterValue, logs) => {
|
|
38
|
+
return (parameterValue, logs = this.logs) => {
|
|
39
39
|
if (typeof parameterValue === 'string' || parameterValue == null) {
|
|
40
40
|
return logs.filter((log) => {
|
|
41
41
|
return log[parameterName] === parameterValue
|
|
@@ -354,7 +354,15 @@ class SnapshotAgent extends MockAgent {
|
|
|
354
354
|
* @returns {Promise<void>}
|
|
355
355
|
*/
|
|
356
356
|
async close () {
|
|
357
|
-
|
|
357
|
+
// In playback mode the recorder must not persist to disk. findSnapshot()
|
|
358
|
+
// mutates each matched snapshot's callCount, so saving on close would
|
|
359
|
+
// rewrite the snapshot file even though nothing new was recorded. Only
|
|
360
|
+
// record/update modes should write snapshots; playback just cleans up.
|
|
361
|
+
if (this[kSnapshotMode] === 'playback') {
|
|
362
|
+
this[kSnapshotRecorder].destroy()
|
|
363
|
+
} else {
|
|
364
|
+
await this[kSnapshotRecorder].close()
|
|
365
|
+
}
|
|
358
366
|
await this[kRealAgent]?.close()
|
|
359
367
|
await super.close()
|
|
360
368
|
}
|
package/lib/web/fetch/index.js
CHANGED
|
@@ -11,7 +11,7 @@ const {
|
|
|
11
11
|
getResponseState
|
|
12
12
|
} = require('./response')
|
|
13
13
|
const { HeadersList } = require('./headers')
|
|
14
|
-
const { Request, cloneRequest, getRequestDispatcher, getRequestState } = require('./request')
|
|
14
|
+
const { Request, cloneRequest, getRequestDispatcher, getRequestState, removeRequestAbortListener } = require('./request')
|
|
15
15
|
const zlib = require('node:zlib')
|
|
16
16
|
const {
|
|
17
17
|
makePolicyContainer,
|
|
@@ -208,7 +208,7 @@ function fetch (input, init = undefined) {
|
|
|
208
208
|
let controller = null
|
|
209
209
|
|
|
210
210
|
// 11. Add the following abort steps to requestObject’s signal:
|
|
211
|
-
addAbortListener(
|
|
211
|
+
const removeAbortListener = addAbortListener(
|
|
212
212
|
requestObject.signal,
|
|
213
213
|
() => {
|
|
214
214
|
// 1. Set locallyAborted to true.
|
|
@@ -228,6 +228,15 @@ function fetch (input, init = undefined) {
|
|
|
228
228
|
}
|
|
229
229
|
)
|
|
230
230
|
|
|
231
|
+
// Remove the `abort` listeners registered above and in the Request
|
|
232
|
+
// constructor once the fetch has settled. Without this, reusing a single
|
|
233
|
+
// signal across many requests leaks listeners and Node.js emits a
|
|
234
|
+
// MaxListenersExceededWarning. See https://github.com/nodejs/undici/issues/5285
|
|
235
|
+
const cleanupAbortListeners = () => {
|
|
236
|
+
removeAbortListener()
|
|
237
|
+
removeRequestAbortListener(requestObject)
|
|
238
|
+
}
|
|
239
|
+
|
|
231
240
|
// 12. Let handleFetchDone given response response be to finalize and
|
|
232
241
|
// report timing with response, globalObject, and "fetch".
|
|
233
242
|
// see function handleFetchDone
|
|
@@ -252,6 +261,7 @@ function fetch (input, init = undefined) {
|
|
|
252
261
|
// deserializedError.
|
|
253
262
|
|
|
254
263
|
abortFetch(p, request, responseObject, controller.serializedAbortReason, controller.controller)
|
|
264
|
+
cleanupAbortListeners()
|
|
255
265
|
return
|
|
256
266
|
}
|
|
257
267
|
|
|
@@ -259,6 +269,7 @@ function fetch (input, init = undefined) {
|
|
|
259
269
|
// and terminate these substeps.
|
|
260
270
|
if (response.type === 'error') {
|
|
261
271
|
p.reject(new TypeError('fetch failed', { cause: response.error }))
|
|
272
|
+
cleanupAbortListeners()
|
|
262
273
|
return
|
|
263
274
|
}
|
|
264
275
|
|
|
@@ -273,7 +284,10 @@ function fetch (input, init = undefined) {
|
|
|
273
284
|
|
|
274
285
|
controller = fetching({
|
|
275
286
|
request,
|
|
276
|
-
processResponseEndOfBody:
|
|
287
|
+
processResponseEndOfBody: (response) => {
|
|
288
|
+
handleFetchDone(response)
|
|
289
|
+
cleanupAbortListeners()
|
|
290
|
+
},
|
|
277
291
|
processResponse,
|
|
278
292
|
dispatcher: getRequestDispatcher(requestObject), // undici
|
|
279
293
|
// Keep requestObject alive to prevent its AbortController from being GC'd
|
package/lib/web/fetch/request.js
CHANGED
|
@@ -97,6 +97,13 @@ class Request {
|
|
|
97
97
|
|
|
98
98
|
#state
|
|
99
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Removes the `abort` listener that makes this request's signal follow the
|
|
102
|
+
* passed signal. `null` when no such listener was registered.
|
|
103
|
+
* @type {(() => void) | null}
|
|
104
|
+
*/
|
|
105
|
+
#abortCleanup = null
|
|
106
|
+
|
|
100
107
|
// https://fetch.spec.whatwg.org/#dom-request
|
|
101
108
|
constructor (input, init = undefined) {
|
|
102
109
|
webidl.util.markAsUncloneable(this)
|
|
@@ -436,12 +443,23 @@ class Request {
|
|
|
436
443
|
setMaxListeners(1500, signal)
|
|
437
444
|
}
|
|
438
445
|
|
|
439
|
-
util.addAbortListener(signal, abort)
|
|
446
|
+
const removeAbortListener = util.addAbortListener(signal, abort)
|
|
440
447
|
// The third argument must be a registry key to be unregistered.
|
|
441
448
|
// Without it, you cannot unregister.
|
|
442
449
|
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/FinalizationRegistry
|
|
443
450
|
// abort is used as the unregister key. (because it is unique)
|
|
444
451
|
requestFinalizer.register(ac, { signal, abort }, abort)
|
|
452
|
+
|
|
453
|
+
// Allow the listener to be removed deterministically once the fetch
|
|
454
|
+
// that owns this request has settled, instead of relying solely on the
|
|
455
|
+
// FinalizationRegistry (i.e. garbage collection). Reusing a single
|
|
456
|
+
// signal across many requests would otherwise leak listeners.
|
|
457
|
+
// See https://github.com/nodejs/undici/issues/5285
|
|
458
|
+
this.#abortCleanup = () => {
|
|
459
|
+
requestFinalizer.unregister(abort)
|
|
460
|
+
removeAbortListener()
|
|
461
|
+
this.#abortCleanup = null
|
|
462
|
+
}
|
|
445
463
|
}
|
|
446
464
|
}
|
|
447
465
|
|
|
@@ -868,15 +886,25 @@ class Request {
|
|
|
868
886
|
static setRequestState (request, newState) {
|
|
869
887
|
request.#state = newState
|
|
870
888
|
}
|
|
889
|
+
|
|
890
|
+
/**
|
|
891
|
+
* Removes the `abort` listener that makes this request's signal follow the
|
|
892
|
+
* signal passed to its constructor, if any. Idempotent.
|
|
893
|
+
* @param {Request} request
|
|
894
|
+
*/
|
|
895
|
+
static removeRequestAbortListener (request) {
|
|
896
|
+
request.#abortCleanup?.()
|
|
897
|
+
}
|
|
871
898
|
}
|
|
872
899
|
|
|
873
|
-
const { setRequestSignal, getRequestDispatcher, setRequestDispatcher, setRequestHeaders, getRequestState, setRequestState } = Request
|
|
900
|
+
const { setRequestSignal, getRequestDispatcher, setRequestDispatcher, setRequestHeaders, getRequestState, setRequestState, removeRequestAbortListener } = Request
|
|
874
901
|
Reflect.deleteProperty(Request, 'setRequestSignal')
|
|
875
902
|
Reflect.deleteProperty(Request, 'getRequestDispatcher')
|
|
876
903
|
Reflect.deleteProperty(Request, 'setRequestDispatcher')
|
|
877
904
|
Reflect.deleteProperty(Request, 'setRequestHeaders')
|
|
878
905
|
Reflect.deleteProperty(Request, 'getRequestState')
|
|
879
906
|
Reflect.deleteProperty(Request, 'setRequestState')
|
|
907
|
+
Reflect.deleteProperty(Request, 'removeRequestAbortListener')
|
|
880
908
|
|
|
881
909
|
mixinBody(Request, getRequestState)
|
|
882
910
|
|
|
@@ -1111,5 +1139,6 @@ module.exports = {
|
|
|
1111
1139
|
fromInnerRequest,
|
|
1112
1140
|
cloneRequest,
|
|
1113
1141
|
getRequestDispatcher,
|
|
1114
|
-
getRequestState
|
|
1142
|
+
getRequestState,
|
|
1143
|
+
removeRequestAbortListener
|
|
1115
1144
|
}
|
package/package.json
CHANGED
package/types/connector.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ declare function buildConnector (options?: buildConnector.BuildOptions): buildCo
|
|
|
7
7
|
declare namespace buildConnector {
|
|
8
8
|
export type BuildOptions = (ConnectionOptions | TcpNetConnectOpts | IpcNetConnectOpts) & {
|
|
9
9
|
allowH2?: boolean;
|
|
10
|
+
preferH2?: boolean;
|
|
10
11
|
maxCachedSessions?: number | null;
|
|
11
12
|
socketPath?: string | null;
|
|
12
13
|
timeout?: number | null;
|
package/types/fetch.d.ts
CHANGED
|
@@ -36,7 +36,10 @@ export class BodyMixin {
|
|
|
36
36
|
readonly bytes: () => Promise<Uint8Array>
|
|
37
37
|
/**
|
|
38
38
|
* @deprecated This method is not recommended for parsing multipart/form-data bodies in server environments.
|
|
39
|
-
*
|
|
39
|
+
* Calling body.formData() buffers and parses the entire body. Since this is dictated by the spec,
|
|
40
|
+
* this method must only be called on responses from trusted servers.
|
|
41
|
+
* For responses from untrusted or user-controlled servers, use a dedicated streaming parser such as
|
|
42
|
+
* [@fastify/busboy](https://www.npmjs.com/package/@fastify/busboy) and apply application-specific limits as follows:
|
|
40
43
|
*
|
|
41
44
|
* @example
|
|
42
45
|
* ```js
|
package/types/interceptors.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ export default Interceptors
|
|
|
8
8
|
declare namespace Interceptors {
|
|
9
9
|
export type DumpInterceptorOpts = { maxSize?: number }
|
|
10
10
|
export type RetryInterceptorOpts = RetryHandler.RetryOptions
|
|
11
|
-
export type RedirectInterceptorOpts = { maxRedirections?: number, throwOnMaxRedirect?: boolean }
|
|
11
|
+
export type RedirectInterceptorOpts = { maxRedirections?: number, throwOnMaxRedirect?: boolean, stripHeadersOnRedirect?: string[], stripHeadersOnCrossOriginRedirect?: string[] }
|
|
12
12
|
export type DecompressInterceptorOpts = {
|
|
13
13
|
skipErrorResponses?: boolean
|
|
14
14
|
skipStatusCodes?: number[]
|