undici 8.4.1 → 8.6.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 +3 -2
- package/docs/docs/api/Agent.md +248 -44
- package/docs/docs/api/BalancedPool.md +246 -48
- package/docs/docs/api/CacheStorage.md +171 -13
- package/docs/docs/api/CacheStore.md +294 -98
- package/docs/docs/api/Client.md +365 -178
- package/docs/docs/api/ClientStats.md +80 -14
- package/docs/docs/api/Connector.md +118 -36
- package/docs/docs/api/ContentType.md +65 -25
- package/docs/docs/api/Cookies.md +125 -47
- package/docs/docs/api/Debug.md +34 -18
- package/docs/docs/api/DiagnosticsChannel.md +364 -164
- package/docs/docs/api/Dispatcher.md +479 -1093
- package/docs/docs/api/EnvHttpProxyAgent.md +101 -87
- package/docs/docs/api/Errors.md +503 -40
- package/docs/docs/api/EventSource.md +294 -32
- package/docs/docs/api/Fetch.md +680 -31
- package/docs/docs/api/GlobalInstallation.md +78 -98
- package/docs/docs/api/H2CClient.md +298 -176
- package/docs/docs/api/Interceptors.md +355 -0
- package/docs/docs/api/MockAgent.md +314 -322
- package/docs/docs/api/MockCallHistory.md +272 -98
- package/docs/docs/api/MockCallHistoryLog.md +189 -26
- package/docs/docs/api/MockClient.md +151 -33
- package/docs/docs/api/MockErrors.md +56 -5
- package/docs/docs/api/MockPool.md +299 -263
- package/docs/docs/api/Pool.md +235 -43
- package/docs/docs/api/PoolStats.md +119 -13
- package/docs/docs/api/ProxyAgent.md +172 -160
- package/docs/docs/api/RedirectHandler.md +238 -63
- package/docs/docs/api/RetryAgent.md +113 -26
- package/docs/docs/api/RetryHandler.md +161 -77
- package/docs/docs/api/RoundRobinPool.md +166 -72
- package/docs/docs/api/SnapshotAgent.md +264 -542
- package/docs/docs/api/Socks5ProxyAgent.md +162 -186
- package/docs/docs/api/Util.md +68 -11
- package/docs/docs/api/WebSocket.md +475 -80
- package/docs/docs/api/api-lifecycle.md +100 -32
- package/docs/docs/best-practices/client-certificate.md +2 -0
- package/docs/docs/best-practices/crawling.md +3 -1
- package/docs/docs/best-practices/migrating-from-v7-to-v8.md +6 -4
- package/docs/docs/best-practices/mocking-request.md +8 -6
- package/docs/docs/best-practices/proxy.md +3 -1
- package/docs/docs/best-practices/undici-vs-builtin-fetch.md +10 -8
- package/docs/docs/best-practices/writing-tests.md +2 -0
- package/docs/docs/{GettingStarted.md → getting-started.md} +22 -19
- package/docs/docs/index.md +779 -0
- package/docs/docs/site.json +125 -0
- package/docs/docs/type-map.json +79 -0
- package/lib/api/api-request.js +7 -1
- package/lib/api/readable.js +43 -2
- package/lib/core/errors.js +20 -0
- package/lib/core/request.js +1 -1
- package/lib/core/util.js +3 -1
- package/lib/dispatcher/client-h1.js +92 -1
- package/lib/dispatcher/client-h2.js +124 -18
- package/lib/dispatcher/client.js +6 -2
- package/lib/dispatcher/dispatcher-base.js +1 -0
- package/lib/dispatcher/proxy-agent.js +11 -2
- package/lib/dispatcher/socks5-proxy-agent.js +4 -2
- package/lib/handler/redirect-handler.js +1 -0
- package/lib/handler/retry-handler.js +57 -19
- package/lib/util/cache.js +8 -2
- package/lib/web/cookies/parse.js +17 -25
- package/lib/web/eventsource/eventsource.js +7 -18
- package/lib/web/eventsource/util.js +32 -1
- package/lib/web/fetch/body.js +43 -0
- package/lib/web/fetch/constants.js +1 -1
- package/lib/web/fetch/index.js +11 -2
- package/lib/web/fetch/request.js +1 -0
- package/lib/web/websocket/receiver.js +20 -3
- package/lib/web/websocket/stream/websocketstream.js +8 -1
- package/lib/web/websocket/websocket.js +3 -1
- package/package.json +1 -1
- package/types/client.d.ts +6 -1
- package/types/cookies.d.ts +1 -1
- package/types/errors.d.ts +10 -0
- package/types/fetch.d.ts +1 -0
package/README.md
CHANGED
|
@@ -717,8 +717,9 @@ Refs: https://tools.ietf.org/html/rfc7231#section-5.1.1
|
|
|
717
717
|
#### Pipelining
|
|
718
718
|
|
|
719
719
|
Undici will only use pipelining if configured with a `pipelining` factor
|
|
720
|
-
greater than `1`.
|
|
721
|
-
|
|
720
|
+
greater than `1`. Only enable pipelining when the remote server is trusted.
|
|
721
|
+
Also it is important to pass `blocking: false` to the request options to
|
|
722
|
+
properly pipeline requests.
|
|
722
723
|
|
|
723
724
|
Undici always assumes that connections are persistent and will immediately
|
|
724
725
|
pipeline requests, without checking whether the connection is persistent.
|
package/docs/docs/api/Agent.md
CHANGED
|
@@ -1,87 +1,291 @@
|
|
|
1
1
|
# Agent
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<!--introduced_in=v3.2.0-->
|
|
4
|
+
<!--type=module-->
|
|
5
|
+
<!-- source_link=lib/dispatcher/agent.js -->
|
|
6
|
+
|
|
7
|
+
> Stability: 2 - Stable
|
|
8
|
+
|
|
9
|
+
An `Agent` dispatches requests against multiple different origins, creating and
|
|
10
|
+
reusing a per-origin [`Pool`][] (or [`Client`][]) on demand. It is the default
|
|
11
|
+
dispatcher used by [`request`][], [`stream`][], and [`fetch`][] when no explicit
|
|
12
|
+
dispatcher is supplied.
|
|
13
|
+
|
|
14
|
+
Requests are not guaranteed to be dispatched in their order of invocation.
|
|
15
|
+
|
|
16
|
+
```mjs
|
|
17
|
+
import { Agent, setGlobalDispatcher } from 'undici'
|
|
18
|
+
|
|
19
|
+
const agent = new Agent({ connections: 10 })
|
|
20
|
+
setGlobalDispatcher(agent)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Class: `Agent`
|
|
24
|
+
|
|
25
|
+
<!-- YAML
|
|
26
|
+
added: v3.2.0
|
|
27
|
+
-->
|
|
28
|
+
|
|
29
|
+
* Extends: {Dispatcher}
|
|
30
|
+
|
|
31
|
+
`Agent` keeps an internal map of origins to dispatchers. The first request to a
|
|
32
|
+
given origin lazily creates a dispatcher through the configured `factory`, and
|
|
33
|
+
subsequent requests to the same origin reuse it. Idle dispatchers are closed
|
|
34
|
+
automatically once they have no open connections and are no longer busy.
|
|
35
|
+
|
|
36
|
+
### `new Agent([options])`
|
|
37
|
+
|
|
38
|
+
<!-- YAML
|
|
39
|
+
added: v3.2.0
|
|
40
|
+
changes:
|
|
41
|
+
- version: v7.16.0
|
|
42
|
+
pr-url: https://github.com/nodejs/undici/pull/4365
|
|
43
|
+
description: Added the `maxOrigins` option to cap the number of origins.
|
|
44
|
+
-->
|
|
45
|
+
|
|
46
|
+
* `options` {AgentOptions} (optional) Extends {PoolOptions}.
|
|
47
|
+
* `factory` {Function} Builds the dispatcher used for a given origin. When the
|
|
48
|
+
resolved options contain `connections: 1`, the default factory creates a
|
|
49
|
+
{Client}; otherwise it creates a {Pool}. **Default:** `(origin, opts) => new Pool(origin, opts)`.
|
|
50
|
+
* `origin` {URL|string} The origin to create a dispatcher for.
|
|
51
|
+
* `opts` {Object} The resolved options for the new dispatcher.
|
|
52
|
+
* Returns: {Dispatcher}
|
|
53
|
+
* `maxOrigins` {number} Limits the total number of distinct origins that may
|
|
54
|
+
receive requests at the same time. A {MaxOriginsReachedError} is thrown when
|
|
55
|
+
a request targets a new origin once the limit is reached. When set to
|
|
56
|
+
`Infinity`, no limit is enforced. Must be a number greater than `0`.
|
|
57
|
+
**Default:** `Infinity`.
|
|
58
|
+
|
|
59
|
+
`Agent` inherits all {PoolOptions} (and therefore all {ClientOptions}). The
|
|
60
|
+
per-origin {Pool} it creates uses the default unlimited `connections`, so
|
|
61
|
+
concurrent requests to the same origin are spread across separate {Client}
|
|
62
|
+
instances on separate sockets.
|
|
4
63
|
|
|
5
|
-
|
|
64
|
+
> [!NOTE]
|
|
65
|
+
> Because each concurrent request to an origin may use a different {Client},
|
|
66
|
+
> HTTP/2 multiplexing on a shared session does not apply unless `connections` is
|
|
67
|
+
> set to a small value (for example `connections: 1`). See {PoolOptions} and
|
|
68
|
+
> {ClientOptions} for the full set of inherited options such as `allowH2`
|
|
69
|
+
> (default `true`) and `maxConcurrentStreams` (default `100`).
|
|
6
70
|
|
|
7
|
-
|
|
71
|
+
### `agent.closed`
|
|
8
72
|
|
|
9
|
-
|
|
73
|
+
<!-- YAML
|
|
74
|
+
added: v3.2.0
|
|
75
|
+
-->
|
|
10
76
|
|
|
11
|
-
|
|
77
|
+
* Type: {boolean}
|
|
12
78
|
|
|
13
|
-
|
|
79
|
+
`true` after [`agent.close()`][] has been called.
|
|
14
80
|
|
|
15
|
-
|
|
81
|
+
### `agent.destroyed`
|
|
16
82
|
|
|
17
|
-
|
|
83
|
+
<!-- YAML
|
|
84
|
+
added: v3.2.0
|
|
85
|
+
-->
|
|
18
86
|
|
|
19
|
-
|
|
87
|
+
* Type: {boolean}
|
|
20
88
|
|
|
21
|
-
|
|
22
|
-
|
|
89
|
+
`true` after [`agent.destroy()`][] has been called, or after [`agent.close()`][]
|
|
90
|
+
has been called and the shutdown has completed.
|
|
23
91
|
|
|
24
|
-
|
|
25
|
-
|
|
92
|
+
### `agent.stats`
|
|
93
|
+
|
|
94
|
+
<!-- YAML
|
|
95
|
+
added: v7.9.0
|
|
96
|
+
-->
|
|
97
|
+
|
|
98
|
+
* Type: {Record<string, ClientStats|PoolStats>}
|
|
99
|
+
|
|
100
|
+
Aggregate statistics for the agent, keyed by origin. Each value is the
|
|
101
|
+
[`ClientStats`][] or [`PoolStats`][] object reported by the dispatcher currently
|
|
102
|
+
serving that origin. Origins whose dispatcher does not expose `stats` are
|
|
103
|
+
omitted.
|
|
104
|
+
|
|
105
|
+
```mjs
|
|
106
|
+
import { Agent } from 'undici'
|
|
107
|
+
|
|
108
|
+
const agent = new Agent()
|
|
109
|
+
await agent.request({ origin: 'http://localhost:3000', path: '/', method: 'GET' })
|
|
110
|
+
console.log(agent.stats)
|
|
111
|
+
// { 'http://localhost:3000': PoolStats { ... } }
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### `agent.dispatch(options, handler)`
|
|
115
|
+
|
|
116
|
+
<!-- YAML
|
|
117
|
+
added: v3.2.0
|
|
118
|
+
-->
|
|
119
|
+
|
|
120
|
+
* `options` {AgentDispatchOptions} Extends {DispatchOptions}.
|
|
121
|
+
* `origin` {string|URL} The origin to dispatch the request against. Required.
|
|
122
|
+
* `handler` {DispatchHandler}
|
|
123
|
+
* Returns: {boolean} `false` if the dispatcher is busy and the caller should
|
|
124
|
+
wait for the [`'drain'`][] event before dispatching again.
|
|
125
|
+
|
|
126
|
+
Routes the request to the dispatcher for `options.origin`, creating one through
|
|
127
|
+
the `factory` if needed. Throws an {InvalidArgumentError} when `options.origin`
|
|
128
|
+
is not a non-empty string or {URL}, and a {MaxOriginsReachedError} when a new
|
|
129
|
+
origin would exceed `maxOrigins`. Implements [`Dispatcher.dispatch()`][].
|
|
130
|
+
|
|
131
|
+
### `agent.close([callback])`
|
|
132
|
+
|
|
133
|
+
<!-- YAML
|
|
134
|
+
added: v3.2.0
|
|
135
|
+
-->
|
|
136
|
+
|
|
137
|
+
* `callback` {Function} (optional) Invoked once every per-origin dispatcher has
|
|
138
|
+
closed. When omitted, a {Promise} is returned.
|
|
139
|
+
* Returns: {Promise|void}
|
|
140
|
+
|
|
141
|
+
Gracefully closes the agent and every dispatcher it owns, waiting for in-flight
|
|
142
|
+
requests to complete. Implements [`Dispatcher.close()`][].
|
|
143
|
+
|
|
144
|
+
### `agent.destroy([error[, callback]])`
|
|
145
|
+
|
|
146
|
+
<!-- YAML
|
|
147
|
+
added: v3.2.0
|
|
148
|
+
-->
|
|
149
|
+
|
|
150
|
+
* `error` {Error} (optional) The error to abort pending requests with.
|
|
151
|
+
* `callback` {Function} (optional) Invoked once every per-origin dispatcher has
|
|
152
|
+
been destroyed. When omitted, a {Promise} is returned.
|
|
153
|
+
* Returns: {Promise|void}
|
|
154
|
+
|
|
155
|
+
Forcefully destroys the agent and every dispatcher it owns, aborting all pending
|
|
156
|
+
requests. Implements [`Dispatcher.destroy()`][].
|
|
157
|
+
|
|
158
|
+
### `agent.connect(options[, callback])`
|
|
159
|
+
|
|
160
|
+
<!-- YAML
|
|
161
|
+
added: v3.2.0
|
|
162
|
+
-->
|
|
163
|
+
|
|
164
|
+
See [`Dispatcher.connect()`][]. The request is routed to the dispatcher for
|
|
165
|
+
`options.origin`.
|
|
166
|
+
|
|
167
|
+
### `agent.pipeline(options, handler)`
|
|
26
168
|
|
|
27
|
-
|
|
169
|
+
<!-- YAML
|
|
170
|
+
added: v3.2.0
|
|
171
|
+
-->
|
|
28
172
|
|
|
29
|
-
|
|
173
|
+
See [`Dispatcher.pipeline()`][]. The request is routed to the dispatcher for
|
|
174
|
+
`options.origin`.
|
|
30
175
|
|
|
31
|
-
|
|
176
|
+
### `agent.request(options[, callback])`
|
|
32
177
|
|
|
33
|
-
|
|
178
|
+
<!-- YAML
|
|
179
|
+
added: v3.2.0
|
|
180
|
+
-->
|
|
34
181
|
|
|
35
|
-
|
|
182
|
+
See [`Dispatcher.request()`][]. The request is routed to the dispatcher for
|
|
183
|
+
`options.origin`.
|
|
36
184
|
|
|
37
|
-
|
|
185
|
+
```mjs
|
|
186
|
+
import { Agent } from 'undici'
|
|
38
187
|
|
|
39
|
-
|
|
188
|
+
const agent = new Agent()
|
|
189
|
+
const { statusCode, body } = await agent.request({
|
|
190
|
+
origin: 'http://localhost:3000',
|
|
191
|
+
path: '/',
|
|
192
|
+
method: 'GET',
|
|
193
|
+
})
|
|
40
194
|
|
|
41
|
-
|
|
195
|
+
console.log('response received', statusCode)
|
|
196
|
+
console.log('data', await body.text())
|
|
197
|
+
```
|
|
42
198
|
|
|
43
|
-
### `
|
|
199
|
+
### `agent.stream(options, factory[, callback])`
|
|
44
200
|
|
|
45
|
-
|
|
201
|
+
<!-- YAML
|
|
202
|
+
added: v3.2.0
|
|
203
|
+
-->
|
|
46
204
|
|
|
47
|
-
|
|
205
|
+
See [`Dispatcher.stream()`][]. The request is routed to the dispatcher for
|
|
206
|
+
`options.origin`.
|
|
48
207
|
|
|
49
|
-
|
|
208
|
+
### `agent.upgrade(options[, callback])`
|
|
50
209
|
|
|
51
|
-
|
|
210
|
+
<!-- YAML
|
|
211
|
+
added: v3.2.0
|
|
212
|
+
-->
|
|
52
213
|
|
|
53
|
-
|
|
214
|
+
See [`Dispatcher.upgrade()`][]. The request is routed to the dispatcher for
|
|
215
|
+
`options.origin`.
|
|
54
216
|
|
|
55
|
-
|
|
217
|
+
### Event: `'connect'`
|
|
56
218
|
|
|
57
|
-
|
|
219
|
+
<!-- YAML
|
|
220
|
+
added: v6.6.0
|
|
221
|
+
-->
|
|
58
222
|
|
|
59
|
-
|
|
223
|
+
* `origin` {URL}
|
|
224
|
+
* `targets` {Array<Dispatcher>} The dispatchers involved, starting with this
|
|
225
|
+
`Agent` followed by the per-origin dispatcher chain.
|
|
60
226
|
|
|
61
|
-
|
|
227
|
+
Emitted when a socket has been created for an origin and is ready to receive
|
|
228
|
+
requests. Forwarded from the underlying dispatcher's [`'connect'`][] event.
|
|
62
229
|
|
|
63
|
-
###
|
|
230
|
+
### Event: `'disconnect'`
|
|
64
231
|
|
|
65
|
-
|
|
232
|
+
<!-- YAML
|
|
233
|
+
added: v6.6.0
|
|
234
|
+
-->
|
|
66
235
|
|
|
67
|
-
|
|
236
|
+
* `origin` {URL}
|
|
237
|
+
* `targets` {Array<Dispatcher>} The dispatchers involved, starting with this
|
|
238
|
+
`Agent`.
|
|
239
|
+
* `error` {Error}
|
|
68
240
|
|
|
69
|
-
|
|
241
|
+
Emitted when a socket for an origin has disconnected. Forwarded from the
|
|
242
|
+
underlying dispatcher's [`'disconnect'`][] event.
|
|
70
243
|
|
|
71
|
-
###
|
|
244
|
+
### Event: `'connectionError'`
|
|
72
245
|
|
|
73
|
-
|
|
246
|
+
<!-- YAML
|
|
247
|
+
added: v6.6.0
|
|
248
|
+
-->
|
|
74
249
|
|
|
75
|
-
|
|
250
|
+
* `origin` {URL}
|
|
251
|
+
* `targets` {Array<Dispatcher>} The dispatchers involved, starting with this
|
|
252
|
+
`Agent`.
|
|
253
|
+
* `error` {Error}
|
|
76
254
|
|
|
77
|
-
|
|
255
|
+
Emitted when a connection attempt for an origin fails. Forwarded from the
|
|
256
|
+
underlying dispatcher's [`'connectionError'`][] event.
|
|
78
257
|
|
|
79
|
-
###
|
|
258
|
+
### Event: `'drain'`
|
|
80
259
|
|
|
81
|
-
|
|
260
|
+
<!-- YAML
|
|
261
|
+
added: v6.6.0
|
|
262
|
+
-->
|
|
82
263
|
|
|
83
|
-
|
|
264
|
+
* `origin` {URL}
|
|
265
|
+
* `targets` {Array<Dispatcher>} The dispatchers involved, starting with this
|
|
266
|
+
`Agent`.
|
|
84
267
|
|
|
85
|
-
|
|
268
|
+
Emitted when an origin's dispatcher is no longer busy and can accept more
|
|
269
|
+
requests. Forwarded from the underlying dispatcher's [`'drain'`][] event.
|
|
86
270
|
|
|
87
|
-
|
|
271
|
+
[`'connect'`]: Dispatcher.md#event-connect
|
|
272
|
+
[`'connectionError'`]: Dispatcher.md#event-connectionerror
|
|
273
|
+
[`'disconnect'`]: Dispatcher.md#event-disconnect
|
|
274
|
+
[`'drain'`]: Dispatcher.md#event-drain
|
|
275
|
+
[`Client`]: Client.md#class-client
|
|
276
|
+
[`ClientStats`]: ClientStats.md
|
|
277
|
+
[`Dispatcher.close()`]: Dispatcher.md#dispatcherclosecallback-promise
|
|
278
|
+
[`Dispatcher.connect()`]: Dispatcher.md#dispatcherconnectoptions-callback
|
|
279
|
+
[`Dispatcher.destroy()`]: Dispatcher.md#dispatcherdestroyerror-callback-promise
|
|
280
|
+
[`Dispatcher.dispatch()`]: Dispatcher.md#dispatcherdispatchoptions-handler
|
|
281
|
+
[`Dispatcher.pipeline()`]: Dispatcher.md#dispatcherpipelineoptions-handler
|
|
282
|
+
[`Dispatcher.request()`]: Dispatcher.md#dispatcherrequestoptions-callback
|
|
283
|
+
[`Dispatcher.stream()`]: Dispatcher.md#dispatcherstreamoptions-factory-callback
|
|
284
|
+
[`Dispatcher.upgrade()`]: Dispatcher.md#dispatcherupgradeoptions-callback
|
|
285
|
+
[`Pool`]: Pool.md#class-pool
|
|
286
|
+
[`PoolStats`]: PoolStats.md
|
|
287
|
+
[`agent.close()`]: #agentclosecallback
|
|
288
|
+
[`agent.destroy()`]: #agentdestroyerror-callback
|
|
289
|
+
[`fetch`]: Fetch.md
|
|
290
|
+
[`request`]: Dispatcher.md#dispatcherrequestoptions-callback
|
|
291
|
+
[`stream`]: Dispatcher.md#dispatcherstreamoptions-factory-callback
|