undici 8.5.0 → 8.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/README.md +3 -2
  2. package/docs/docs/api/Agent.md +248 -44
  3. package/docs/docs/api/BalancedPool.md +246 -48
  4. package/docs/docs/api/CacheStorage.md +171 -13
  5. package/docs/docs/api/CacheStore.md +294 -98
  6. package/docs/docs/api/Client.md +365 -179
  7. package/docs/docs/api/ClientStats.md +80 -14
  8. package/docs/docs/api/Connector.md +118 -36
  9. package/docs/docs/api/ContentType.md +65 -25
  10. package/docs/docs/api/Cookies.md +122 -71
  11. package/docs/docs/api/Debug.md +34 -18
  12. package/docs/docs/api/DiagnosticsChannel.md +364 -164
  13. package/docs/docs/api/Dispatcher.md +479 -1102
  14. package/docs/docs/api/EnvHttpProxyAgent.md +101 -87
  15. package/docs/docs/api/Errors.md +503 -40
  16. package/docs/docs/api/EventSource.md +294 -32
  17. package/docs/docs/api/Fetch.md +680 -31
  18. package/docs/docs/api/GlobalInstallation.md +78 -98
  19. package/docs/docs/api/H2CClient.md +298 -176
  20. package/docs/docs/api/Interceptors.md +355 -0
  21. package/docs/docs/api/MockAgent.md +314 -322
  22. package/docs/docs/api/MockCallHistory.md +272 -98
  23. package/docs/docs/api/MockCallHistoryLog.md +189 -26
  24. package/docs/docs/api/MockClient.md +151 -33
  25. package/docs/docs/api/MockErrors.md +56 -5
  26. package/docs/docs/api/MockPool.md +299 -263
  27. package/docs/docs/api/Pool.md +235 -43
  28. package/docs/docs/api/PoolStats.md +119 -13
  29. package/docs/docs/api/ProxyAgent.md +179 -160
  30. package/docs/docs/api/RedirectHandler.md +238 -63
  31. package/docs/docs/api/RetryAgent.md +113 -26
  32. package/docs/docs/api/RetryHandler.md +161 -77
  33. package/docs/docs/api/RoundRobinPool.md +166 -72
  34. package/docs/docs/api/SnapshotAgent.md +264 -542
  35. package/docs/docs/api/Socks5ProxyAgent.md +162 -187
  36. package/docs/docs/api/Util.md +68 -11
  37. package/docs/docs/api/WebSocket.md +475 -80
  38. package/docs/docs/api/api-lifecycle.md +100 -32
  39. package/docs/docs/best-practices/client-certificate.md +2 -0
  40. package/docs/docs/best-practices/crawling.md +3 -1
  41. package/docs/docs/best-practices/migrating-from-v7-to-v8.md +6 -4
  42. package/docs/docs/best-practices/mocking-request.md +9 -7
  43. package/docs/docs/best-practices/proxy.md +3 -1
  44. package/docs/docs/best-practices/undici-vs-builtin-fetch.md +10 -8
  45. package/docs/docs/best-practices/writing-tests.md +2 -0
  46. package/docs/docs/{GettingStarted.md → getting-started.md} +22 -19
  47. package/docs/docs/index.md +779 -0
  48. package/docs/docs/site.json +125 -0
  49. package/docs/docs/type-map.json +79 -0
  50. package/lib/api/api-request.js +7 -1
  51. package/lib/api/readable.js +47 -2
  52. package/lib/core/errors.js +20 -0
  53. package/lib/core/request.js +1 -1
  54. package/lib/core/util.js +24 -1
  55. package/lib/dispatcher/client-h1.js +23 -0
  56. package/lib/dispatcher/client-h2.js +243 -111
  57. package/lib/dispatcher/proxy-agent.js +40 -6
  58. package/lib/handler/redirect-handler.js +1 -0
  59. package/lib/handler/retry-handler.js +57 -19
  60. package/lib/web/cookies/parse.js +3 -2
  61. package/lib/web/cookies/util.js +1 -1
  62. package/lib/web/eventsource/util.js +1 -1
  63. package/lib/web/fetch/constants.js +1 -1
  64. package/lib/web/fetch/index.js +11 -2
  65. package/lib/web/fetch/util.js +4 -1
  66. package/package.json +1 -1
  67. package/types/client.d.ts +1 -1
  68. package/types/cookies.d.ts +1 -1
  69. package/types/errors.d.ts +10 -0
  70. package/types/handlers.d.ts +2 -0
  71. package/types/proxy-agent.d.ts +7 -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`. Also it is important to pass `blocking: false` to the
721
- request options to properly pipeline requests.
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.
@@ -1,87 +1,291 @@
1
1
  # Agent
2
2
 
3
- Extends: `undici.Dispatcher`
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
- Agent allows dispatching requests against multiple different origins.
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
- Requests are not guaranteed to be dispatched in order of invocation.
71
+ ### `agent.closed`
8
72
 
9
- ## `new undici.Agent([options])`
73
+ <!-- YAML
74
+ added: v3.2.0
75
+ -->
10
76
 
11
- Arguments:
77
+ * Type: {boolean}
12
78
 
13
- * **options** `AgentOptions` (optional)
79
+ `true` after [`agent.close()`][] has been called.
14
80
 
15
- Returns: `Agent`
81
+ ### `agent.destroyed`
16
82
 
17
- ### Parameter: `AgentOptions`
83
+ <!-- YAML
84
+ added: v3.2.0
85
+ -->
18
86
 
19
- Extends: [`PoolOptions`](/docs/docs/api/Pool.md#parameter-pooloptions)
87
+ * Type: {boolean}
20
88
 
21
- * **factory** `(origin: URL, opts: Object) => Dispatcher` - Default: `(origin, opts) => new Pool(origin, opts)`
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.
89
+ `true` after [`agent.destroy()`][] has been called, or after [`agent.close()`][]
90
+ has been called and the shutdown has completed.
23
91
 
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).
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
- ## Instance Properties
169
+ <!-- YAML
170
+ added: v3.2.0
171
+ -->
28
172
 
29
- ### `Agent.closed`
173
+ See [`Dispatcher.pipeline()`][]. The request is routed to the dispatcher for
174
+ `options.origin`.
30
175
 
31
- Implements [Client.closed](/docs/docs/api/Client.md#clientclosed)
176
+ ### `agent.request(options[, callback])`
32
177
 
33
- ### `Agent.destroyed`
178
+ <!-- YAML
179
+ added: v3.2.0
180
+ -->
34
181
 
35
- Implements [Client.destroyed](/docs/docs/api/Client.md#clientdestroyed)
182
+ See [`Dispatcher.request()`][]. The request is routed to the dispatcher for
183
+ `options.origin`.
36
184
 
37
- ## Instance Methods
185
+ ```mjs
186
+ import { Agent } from 'undici'
38
187
 
39
- ### `Agent.close([callback])`
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
- Implements [`Dispatcher.close([callback])`](/docs/docs/api/Dispatcher.md#dispatcherclosecallback-promise).
195
+ console.log('response received', statusCode)
196
+ console.log('data', await body.text())
197
+ ```
42
198
 
43
- ### `Agent.destroy([error, callback])`
199
+ ### `agent.stream(options, factory[, callback])`
44
200
 
45
- Implements [`Dispatcher.destroy([error, callback])`](/docs/docs/api/Dispatcher.md#dispatcherdestroyerror-callback-promise).
201
+ <!-- YAML
202
+ added: v3.2.0
203
+ -->
46
204
 
47
- ### `Agent.dispatch(options, handler: AgentDispatchOptions)`
205
+ See [`Dispatcher.stream()`][]. The request is routed to the dispatcher for
206
+ `options.origin`.
48
207
 
49
- Implements [`Dispatcher.dispatch(options, handler)`](/docs/docs/api/Dispatcher.md#dispatcherdispatchoptions-handler).
208
+ ### `agent.upgrade(options[, callback])`
50
209
 
51
- #### Parameter: `AgentDispatchOptions`
210
+ <!-- YAML
211
+ added: v3.2.0
212
+ -->
52
213
 
53
- Extends: [`DispatchOptions`](/docs/docs/api/Dispatcher.md#parameter-dispatchoptions)
214
+ See [`Dispatcher.upgrade()`][]. The request is routed to the dispatcher for
215
+ `options.origin`.
54
216
 
55
- * **origin** `string | URL`
217
+ ### Event: `'connect'`
56
218
 
57
- Implements [`Dispatcher.destroy([error, callback])`](/docs/docs/api/Dispatcher.md#dispatcherdestroyerror-callback-promise).
219
+ <!-- YAML
220
+ added: v6.6.0
221
+ -->
58
222
 
59
- ### `Agent.connect(options[, callback])`
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
- See [`Dispatcher.connect(options[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherconnectoptions-callback).
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
- ### `Agent.dispatch(options, handler)`
230
+ ### Event: `'disconnect'`
64
231
 
65
- Implements [`Dispatcher.dispatch(options, handler)`](/docs/docs/api/Dispatcher.md#dispatcherdispatchoptions-handler).
232
+ <!-- YAML
233
+ added: v6.6.0
234
+ -->
66
235
 
67
- ### `Agent.pipeline(options, handler)`
236
+ * `origin` {URL}
237
+ * `targets` {Array<Dispatcher>} The dispatchers involved, starting with this
238
+ `Agent`.
239
+ * `error` {Error}
68
240
 
69
- See [`Dispatcher.pipeline(options, handler)`](/docs/docs/api/Dispatcher.md#dispatcherpipelineoptions-handler).
241
+ Emitted when a socket for an origin has disconnected. Forwarded from the
242
+ underlying dispatcher's [`'disconnect'`][] event.
70
243
 
71
- ### `Agent.request(options[, callback])`
244
+ ### Event: `'connectionError'`
72
245
 
73
- See [`Dispatcher.request(options [, callback])`](/docs/docs/api/Dispatcher.md#dispatcherrequestoptions-callback).
246
+ <!-- YAML
247
+ added: v6.6.0
248
+ -->
74
249
 
75
- ### `Agent.stream(options, factory[, callback])`
250
+ * `origin` {URL}
251
+ * `targets` {Array<Dispatcher>} The dispatchers involved, starting with this
252
+ `Agent`.
253
+ * `error` {Error}
76
254
 
77
- See [`Dispatcher.stream(options, factory[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherstreamoptions-factory-callback).
255
+ Emitted when a connection attempt for an origin fails. Forwarded from the
256
+ underlying dispatcher's [`'connectionError'`][] event.
78
257
 
79
- ### `Agent.upgrade(options[, callback])`
258
+ ### Event: `'drain'`
80
259
 
81
- See [`Dispatcher.upgrade(options[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherupgradeoptions-callback).
260
+ <!-- YAML
261
+ added: v6.6.0
262
+ -->
82
263
 
83
- ### `Agent.stats()`
264
+ * `origin` {URL}
265
+ * `targets` {Array<Dispatcher>} The dispatchers involved, starting with this
266
+ `Agent`.
84
267
 
85
- Returns an object of stats by origin in the format of `Record<string, TClientStats | TPoolStats>`
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
- See [`PoolStats`](/docs/docs/api/PoolStats.md) and [`ClientStats`](/docs/docs/api/ClientStats.md).
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