undici 8.3.0 → 8.4.1
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/GettingStarted.md +278 -0
- package/docs/docs/api/Agent.md +3 -0
- package/docs/docs/api/BalancedPool.md +1 -1
- package/docs/docs/api/Client.md +43 -5
- package/docs/docs/api/Connector.md +1 -0
- package/docs/docs/api/Cookies.md +1 -1
- package/docs/docs/api/Dispatcher.md +12 -4
- package/docs/docs/api/EnvHttpProxyAgent.md +6 -9
- package/docs/docs/api/Errors.md +12 -0
- package/docs/docs/api/EventSource.md +50 -3
- package/docs/docs/api/Fetch.md +5 -3
- package/docs/docs/api/H2CClient.md +3 -3
- package/docs/docs/api/MockAgent.md +1 -1
- package/docs/docs/api/MockCallHistory.md +1 -1
- package/docs/docs/api/Pool.md +4 -1
- package/docs/docs/api/RedirectHandler.md +4 -1
- package/docs/docs/api/RetryAgent.md +3 -3
- package/docs/docs/api/RetryHandler.md +6 -6
- package/docs/docs/api/RoundRobinPool.md +1 -1
- package/docs/docs/api/SnapshotAgent.md +3 -3
- package/docs/docs/api/api-lifecycle.md +4 -4
- package/lib/core/connect.js +29 -4
- package/lib/core/util.js +8 -6
- package/lib/dispatcher/client-h1.js +0 -1
- package/lib/dispatcher/client-h2.js +76 -25
- package/lib/dispatcher/client.js +30 -5
- package/lib/handler/redirect-handler.js +36 -11
- package/lib/interceptor/dns.js +4 -0
- 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
|
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
# Getting Started
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install undici
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Fetch
|
|
10
|
+
|
|
11
|
+
The quickest way to get started is with `fetch`, which follows the
|
|
12
|
+
[Fetch Standard](https://fetch.spec.whatwg.org/) and works the same way as
|
|
13
|
+
the browser API:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import { fetch } from 'undici'
|
|
17
|
+
|
|
18
|
+
const res = await fetch('https://example.com')
|
|
19
|
+
const data = await res.json()
|
|
20
|
+
console.log(data)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Using the Request object
|
|
24
|
+
|
|
25
|
+
undici also exports a `Request` class that follows the Fetch Standard:
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import { fetch, Request } from 'undici'
|
|
29
|
+
|
|
30
|
+
const req = new Request('https://example.com', {
|
|
31
|
+
method: 'POST',
|
|
32
|
+
headers: { 'content-type': 'application/json' },
|
|
33
|
+
body: JSON.stringify({ hello: 'world' })
|
|
34
|
+
})
|
|
35
|
+
const res = await fetch(req)
|
|
36
|
+
console.log(res.status)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Streaming the response
|
|
40
|
+
|
|
41
|
+
`res.body` is a web `ReadableStream`. Use `pipeline` from
|
|
42
|
+
`node:stream/promises` to stream it to a file:
|
|
43
|
+
|
|
44
|
+
```js
|
|
45
|
+
import { fetch } from 'undici'
|
|
46
|
+
import { pipeline } from 'node:stream/promises'
|
|
47
|
+
import { createWriteStream } from 'node:fs'
|
|
48
|
+
|
|
49
|
+
const res = await fetch('https://example.com/large-file.zip')
|
|
50
|
+
await pipeline(res.body, createWriteStream('./file.zip'))
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
> Always consume or cancel the response body. In Node.js, garbage collection
|
|
54
|
+
> is not aggressive enough to release connections promptly, so leaving a body
|
|
55
|
+
> unread can cause connection leaks and stalled requests. See
|
|
56
|
+
> [Specification Compliance - Garbage Collection](/docs/#garbage-collection)
|
|
57
|
+
> for details.
|
|
58
|
+
|
|
59
|
+
For more on `fetch`, see [API Reference: Fetch](/docs/docs/api/Fetch.md).
|
|
60
|
+
|
|
61
|
+
## Dispatchers: Connection reuse and pooling
|
|
62
|
+
|
|
63
|
+
By default, `fetch`, `request`, `stream`, and `pipeline` create a new connection
|
|
64
|
+
for each call. For applications that make many requests to the same origin,
|
|
65
|
+
this is wasteful. undici provides **dispatchers** that manage connections
|
|
66
|
+
internally.
|
|
67
|
+
|
|
68
|
+
### `Agent` — for requests to multiple origins
|
|
69
|
+
|
|
70
|
+
`Agent` is the most general-purpose dispatcher. It pools connections per-origin
|
|
71
|
+
and is the recommended default for most applications. Use it with
|
|
72
|
+
`setGlobalDispatcher` to affect all undici calls globally:
|
|
73
|
+
|
|
74
|
+
```js
|
|
75
|
+
import { Agent, setGlobalDispatcher, fetch } from 'undici'
|
|
76
|
+
|
|
77
|
+
const agent = new Agent({
|
|
78
|
+
keepAliveTimeout: 30_000,
|
|
79
|
+
keepAliveMaxTimeout: 600_000
|
|
80
|
+
})
|
|
81
|
+
setGlobalDispatcher(agent)
|
|
82
|
+
|
|
83
|
+
// All subsequent fetch/request/stream/pipeline calls reuse connections
|
|
84
|
+
const res = await fetch('https://api.example.com/data')
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
You can also pass a dispatcher per-request:
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
await fetch('https://api.example.com/data', { dispatcher: agent })
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### `Pool` — for requests to a single origin
|
|
94
|
+
|
|
95
|
+
`Pool` manages a fixed set of connections to one origin. It gives you explicit
|
|
96
|
+
control over concurrency:
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
import { Pool, request } from 'undici'
|
|
100
|
+
|
|
101
|
+
const pool = new Pool('https://api.example.com', { connections: 10 })
|
|
102
|
+
|
|
103
|
+
const { body } = await request('https://api.example.com/data', {
|
|
104
|
+
dispatcher: pool
|
|
105
|
+
})
|
|
106
|
+
const data = await body.json()
|
|
107
|
+
|
|
108
|
+
pool.close()
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### `Client` — for a single connection
|
|
112
|
+
|
|
113
|
+
`Client` maps to a single TCP connection. It supports pipelining (sending
|
|
114
|
+
multiple requests before responses arrive):
|
|
115
|
+
|
|
116
|
+
```js
|
|
117
|
+
import { Client } from 'undici'
|
|
118
|
+
|
|
119
|
+
const client = new Client('https://api.example.com', {
|
|
120
|
+
pipelining: 5
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
const { body } = await client.request({ path: '/', method: 'GET' })
|
|
124
|
+
await body.dump()
|
|
125
|
+
|
|
126
|
+
client.close()
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
For more on dispatcher options and lifecycle, see:
|
|
130
|
+
- [API Reference: Agent](/docs/docs/api/Agent.md)
|
|
131
|
+
- [API Reference: Pool](/docs/docs/api/Pool.md)
|
|
132
|
+
- [API Reference: Client](/docs/docs/api/Client.md)
|
|
133
|
+
|
|
134
|
+
## Timeouts
|
|
135
|
+
|
|
136
|
+
undici applies timeouts at two levels:
|
|
137
|
+
|
|
138
|
+
- **`headersTimeout`** — time to wait for response headers (default: 300s).
|
|
139
|
+
- **`bodyTimeout`** — time between consecutive body chunks (default: 300s).
|
|
140
|
+
|
|
141
|
+
Set these on the dispatcher or per-request:
|
|
142
|
+
|
|
143
|
+
```js
|
|
144
|
+
import { Agent, setGlobalDispatcher } from 'undici'
|
|
145
|
+
|
|
146
|
+
const agent = new Agent({
|
|
147
|
+
headersTimeout: 5_000,
|
|
148
|
+
bodyTimeout: 30_000
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
setGlobalDispatcher(agent)
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Timeout errors are thrown as `HeadersTimeoutError` and `BodyTimeoutError`.
|
|
155
|
+
See [API Reference: Errors](/docs/docs/api/Errors.md) for the full list.
|
|
156
|
+
|
|
157
|
+
## Error handling
|
|
158
|
+
|
|
159
|
+
undici exposes structured errors via `error.code`:
|
|
160
|
+
|
|
161
|
+
```js
|
|
162
|
+
import { request, errors } from 'undici'
|
|
163
|
+
|
|
164
|
+
try {
|
|
165
|
+
const { body } = await request('https://example.com')
|
|
166
|
+
await body.json()
|
|
167
|
+
} catch (err) {
|
|
168
|
+
switch (err.code) {
|
|
169
|
+
case 'UND_ERR_CONNECT_TIMEOUT':
|
|
170
|
+
console.error('Connection timed out')
|
|
171
|
+
break
|
|
172
|
+
case 'UND_ERR_HEADERS_TIMEOUT':
|
|
173
|
+
console.error('Headers timed out')
|
|
174
|
+
break
|
|
175
|
+
case 'UND_ERR_BODY_TIMEOUT':
|
|
176
|
+
console.error('Body timed out')
|
|
177
|
+
break
|
|
178
|
+
case 'UND_ERR_ABORTED':
|
|
179
|
+
console.error('Request was aborted')
|
|
180
|
+
break
|
|
181
|
+
default:
|
|
182
|
+
console.error(err)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Aborting requests
|
|
188
|
+
|
|
189
|
+
```js
|
|
190
|
+
import { request } from 'undici'
|
|
191
|
+
|
|
192
|
+
const ac = new AbortController()
|
|
193
|
+
|
|
194
|
+
setTimeout(() => ac.abort(), 1000)
|
|
195
|
+
|
|
196
|
+
try {
|
|
197
|
+
const { body } = await request('https://example.com', {
|
|
198
|
+
signal: ac.signal
|
|
199
|
+
})
|
|
200
|
+
await body.dump()
|
|
201
|
+
} catch (err) {
|
|
202
|
+
console.error(err.code) // UND_ERR_ABORTED
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
## Common patterns
|
|
207
|
+
|
|
208
|
+
### Proxies
|
|
209
|
+
|
|
210
|
+
Use `ProxyAgent` for HTTP(S) proxies, or `EnvHttpProxyAgent` to pick up
|
|
211
|
+
proxy settings from environment variables:
|
|
212
|
+
|
|
213
|
+
```js
|
|
214
|
+
import { ProxyAgent, setGlobalDispatcher } from 'undici'
|
|
215
|
+
|
|
216
|
+
const proxy = new ProxyAgent('http://proxy.internal:8080')
|
|
217
|
+
setGlobalDispatcher(proxy)
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
See [Best Practices: Proxy](/docs/docs/best-practices/proxy.md) and
|
|
221
|
+
[API Reference: ProxyAgent](/docs/docs/api/ProxyAgent.md).
|
|
222
|
+
|
|
223
|
+
### Mocking in tests
|
|
224
|
+
|
|
225
|
+
```js
|
|
226
|
+
import { MockAgent, setGlobalDispatcher, request } from 'undici'
|
|
227
|
+
|
|
228
|
+
const mockAgent = new MockAgent()
|
|
229
|
+
setGlobalDispatcher(mockAgent)
|
|
230
|
+
|
|
231
|
+
const mockPool = mockAgent.get('https://api.example.com')
|
|
232
|
+
mockPool.intercept({ path: '/users' }).reply(200, [{ id: 1 }])
|
|
233
|
+
|
|
234
|
+
const { body } = await request('https://api.example.com/users')
|
|
235
|
+
console.log(await body.json())
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
See [Best Practices: Mocking Request](/docs/docs/best-practices/mocking-request.md)
|
|
239
|
+
and [API Reference: MockAgent](/docs/docs/api/MockAgent.md).
|
|
240
|
+
|
|
241
|
+
### Testing with undici
|
|
242
|
+
|
|
243
|
+
For test suites, set short keep-alive timeouts to avoid slow teardowns:
|
|
244
|
+
|
|
245
|
+
```js
|
|
246
|
+
import { Agent, setGlobalDispatcher } from 'undici'
|
|
247
|
+
|
|
248
|
+
const agent = new Agent({
|
|
249
|
+
keepAliveTimeout: 10,
|
|
250
|
+
keepAliveMaxTimeout: 10
|
|
251
|
+
})
|
|
252
|
+
setGlobalDispatcher(agent)
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
See [Best Practices: Writing Tests](/docs/docs/best-practices/writing-tests.md).
|
|
256
|
+
|
|
257
|
+
### Customizing the global fetch
|
|
258
|
+
|
|
259
|
+
You can override Node.js's built-in globals with `install()`:
|
|
260
|
+
|
|
261
|
+
```js
|
|
262
|
+
import { install } from 'undici'
|
|
263
|
+
|
|
264
|
+
install()
|
|
265
|
+
|
|
266
|
+
// Global fetch, Headers, Response, Request, and FormData
|
|
267
|
+
// now come from undici, not the Node.js bundle
|
|
268
|
+
const res = await fetch('https://example.com')
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
See [API Reference: Global Installation](/docs/docs/api/GlobalInstallation.md).
|
|
272
|
+
|
|
273
|
+
## Further reading
|
|
274
|
+
|
|
275
|
+
- [Undici vs. Built-in Fetch](/docs/docs/best-practices/undici-vs-builtin-fetch.md) —
|
|
276
|
+
when to install undici vs using Node.js built-in fetch
|
|
277
|
+
- [API Reference](/docs/docs/api/Dispatcher.md) — full dispatcher API documentation
|
|
278
|
+
- [Examples](/docs/examples/) — runnable code examples
|
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`
|
|
@@ -34,7 +34,7 @@ Implements [Client.closed](/docs/docs/api/Client.md#clientclosed)
|
|
|
34
34
|
|
|
35
35
|
Implements [Client.destroyed](/docs/docs/api/Client.md#clientdestroyed)
|
|
36
36
|
|
|
37
|
-
### `
|
|
37
|
+
### `BalancedPool.stats`
|
|
38
38
|
|
|
39
39
|
Returns [`PoolStats`](/docs/docs/api/PoolStats.md) instance for this pool.
|
|
40
40
|
|
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)
|
package/docs/docs/api/Cookies.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* **path** `string` (optional)
|
|
11
11
|
* **secure** `boolean` (optional)
|
|
12
12
|
* **httpOnly** `boolean` (optional)
|
|
13
|
-
* **sameSite** `'
|
|
13
|
+
* **sameSite** `'Strict'|'Lax'|'None'` (optional)
|
|
14
14
|
* **unparsed** `string[]` (optional) Left over attributes that weren't parsed.
|
|
15
15
|
|
|
16
16
|
## `deleteCookie(headers, name[, attributes])`
|
|
@@ -211,6 +211,7 @@ Returns: `Boolean` - `false` if dispatcher is busy and further dispatch calls wo
|
|
|
211
211
|
* **onResponseData** `(controller: DispatchController, chunk: Buffer) => void` - Invoked when response payload data is received. Not required for `upgrade` requests.
|
|
212
212
|
* **onResponseEnd** `(controller: DispatchController, trailers: Record<string, string | string[]>) => void` - Invoked when response payload and trailers have been received and the request has completed. Not required for `upgrade` requests.
|
|
213
213
|
* **onResponseError** `(controller: DispatchController, error: Error) => void` - Invoked when an error has occurred. May not throw.
|
|
214
|
+
* **onBodySent** `(chunk: Buffer) => void` (optional) - Invoked when a chunk of the request body is sent.
|
|
214
215
|
|
|
215
216
|
#### Migration from legacy handler API
|
|
216
217
|
|
|
@@ -688,7 +689,7 @@ return null
|
|
|
688
689
|
|
|
689
690
|
A faster version of `Dispatcher.request`. This method expects the second argument `factory` to return a [`stream.Writable`](https://nodejs.org/api/stream.html#stream_class_stream_writable) stream which the response will be written to. This improves performance by avoiding creating an intermediate [`stream.Readable`](https://nodejs.org/api/stream.html#stream_readable_streams) stream when the user expects to directly pipe the response body to a [`stream.Writable`](https://nodejs.org/api/stream.html#stream_class_stream_writable) stream.
|
|
690
691
|
|
|
691
|
-
As demonstrated in [Example 1 - Basic GET stream request](/docs/docs/api/Dispatcher.md#example-1-basic-get-stream-request), it is recommended to use the `option.opaque` property to avoid creating a closure for the `factory` method. This pattern works well with Node.js Web Frameworks such as [Fastify](https://fastify.io). See [Example 2 - Stream to Fastify Response](/docs/docs/api/
|
|
692
|
+
As demonstrated in [Example 1 - Basic GET stream request](/docs/docs/api/Dispatcher.md#example-1-basic-get-stream-request), it is recommended to use the `option.opaque` property to avoid creating a closure for the `factory` method. This pattern works well with Node.js Web Frameworks such as [Fastify](https://fastify.io). See [Example 2 - Stream to Fastify Response](/docs/docs/api/Dispatcher.md#example-2-stream-to-fastify-response) for more details.
|
|
692
693
|
|
|
693
694
|
Arguments:
|
|
694
695
|
|
|
@@ -991,6 +992,13 @@ The `redirect` interceptor allows you to customize the way your dispatcher handl
|
|
|
991
992
|
|
|
992
993
|
It accepts the same arguments as the [`RedirectHandler` constructor](/docs/docs/api/RedirectHandler.md).
|
|
993
994
|
|
|
995
|
+
Options:
|
|
996
|
+
|
|
997
|
+
- **maxRedirections** `number` - Maximum number of redirections allowed.
|
|
998
|
+
- **throwOnMaxRedirect** `boolean` - Throw when the maximum number of redirections is reached.
|
|
999
|
+
- **stripHeadersOnRedirect** `string[]` - Header names to remove from all redirected requests.
|
|
1000
|
+
- **stripHeadersOnCrossOriginRedirect** `string[]` - Header names to remove from cross-origin redirected requests.
|
|
1001
|
+
|
|
994
1002
|
**Example - Basic Redirect Interceptor**
|
|
995
1003
|
|
|
996
1004
|
```js
|
|
@@ -1009,7 +1017,7 @@ The `retry` interceptor allows you to customize the way your dispatcher handles
|
|
|
1009
1017
|
|
|
1010
1018
|
It accepts the same arguments as the [`RetryHandler` constructor](/docs/docs/api/RetryHandler.md).
|
|
1011
1019
|
|
|
1012
|
-
**Example - Basic
|
|
1020
|
+
**Example - Basic Retry Interceptor**
|
|
1013
1021
|
|
|
1014
1022
|
```js
|
|
1015
1023
|
const { Client, interceptors } = require("undici");
|
|
@@ -1105,7 +1113,7 @@ It represents a storage object for resolved DNS records.
|
|
|
1105
1113
|
**Example - Basic DNS Interceptor**
|
|
1106
1114
|
|
|
1107
1115
|
```js
|
|
1108
|
-
const {
|
|
1116
|
+
const { Agent, interceptors } = require("undici");
|
|
1109
1117
|
const { dns } = interceptors;
|
|
1110
1118
|
|
|
1111
1119
|
const client = new Agent().compose([
|
|
@@ -1121,7 +1129,7 @@ const response = await client.request({
|
|
|
1121
1129
|
**Example - DNS Interceptor and LRU cache as a storage**
|
|
1122
1130
|
|
|
1123
1131
|
```js
|
|
1124
|
-
const {
|
|
1132
|
+
const { Agent, interceptors } = require("undici");
|
|
1125
1133
|
const QuickLRU = require("quick-lru");
|
|
1126
1134
|
const { dns } = interceptors;
|
|
1127
1135
|
|
|
@@ -52,11 +52,11 @@ import { setGlobalDispatcher, fetch, EnvHttpProxyAgent } from 'undici'
|
|
|
52
52
|
const envHttpProxyAgent = new EnvHttpProxyAgent()
|
|
53
53
|
setGlobalDispatcher(envHttpProxyAgent)
|
|
54
54
|
|
|
55
|
-
const
|
|
55
|
+
const response = await fetch('http://localhost:3000/foo')
|
|
56
56
|
|
|
57
|
-
console.log('response received', status) // response received 200
|
|
57
|
+
console.log('response received', response.status) // response received 200
|
|
58
58
|
|
|
59
|
-
const data = await json() // data { foo: "bar" }
|
|
59
|
+
const data = await response.json() // data { foo: "bar" }
|
|
60
60
|
```
|
|
61
61
|
|
|
62
62
|
#### Example - Basic Proxy Request with global agent dispatcher
|
|
@@ -102,14 +102,11 @@ import { EnvHttpProxyAgent, fetch } from 'undici'
|
|
|
102
102
|
|
|
103
103
|
const envHttpProxyAgent = new EnvHttpProxyAgent()
|
|
104
104
|
|
|
105
|
-
const {
|
|
106
|
-
status,
|
|
107
|
-
json
|
|
108
|
-
} = await fetch('http://localhost:3000/foo', { dispatcher: envHttpProxyAgent })
|
|
105
|
+
const response = await fetch('http://localhost:3000/foo', { dispatcher: envHttpProxyAgent })
|
|
109
106
|
|
|
110
|
-
console.log('response received', status) // response received 200
|
|
107
|
+
console.log('response received', response.status) // response received 200
|
|
111
108
|
|
|
112
|
-
const data = await json() // data { foo: "bar" }
|
|
109
|
+
const data = await response.json() // data { foo: "bar" }
|
|
113
110
|
```
|
|
114
111
|
|
|
115
112
|
## Instance Methods
|
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:
|