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
|
@@ -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
|
@@ -15,7 +15,7 @@ same implementation. Use the built-in global `FormData` with the built-in
|
|
|
15
15
|
global `fetch()`, and use `undici`'s `FormData` with `undici.fetch()`.
|
|
16
16
|
|
|
17
17
|
If you want the installed `undici` package to provide the globals, call
|
|
18
|
-
[`install()`](/docs/api/GlobalInstallation.md) so `fetch`, `Headers`,
|
|
18
|
+
[`install()`](/docs/docs/api/GlobalInstallation.md) so `fetch`, `Headers`,
|
|
19
19
|
`Response`, `Request`, and `FormData` are installed together as a matching set.
|
|
20
20
|
|
|
21
21
|
## Response
|
|
@@ -26,7 +26,7 @@ This API is implemented as per the standard, you can find documentation on [MDN]
|
|
|
26
26
|
|
|
27
27
|
This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Request)
|
|
28
28
|
|
|
29
|
-
##
|
|
29
|
+
## Headers
|
|
30
30
|
|
|
31
31
|
This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Headers)
|
|
32
32
|
|
|
@@ -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
|
|
|
@@ -17,7 +17,7 @@ const server = createServer((req, res) => {
|
|
|
17
17
|
})
|
|
18
18
|
|
|
19
19
|
server.listen()
|
|
20
|
-
once(server, 'listening').then(() => {
|
|
20
|
+
once(server, 'listening').then(async () => {
|
|
21
21
|
const client = new H2CClient(`http://localhost:${server.address().port}/`)
|
|
22
22
|
|
|
23
23
|
const response = await client.request({ path: '/', method: 'GET' })
|
|
@@ -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.
|
|
@@ -165,7 +165,7 @@ Parameters :
|
|
|
165
165
|
|
|
166
166
|
- criteria : the first parameter. a function, regexp or object.
|
|
167
167
|
- function : filter MockCallHistoryLog when the function returns false
|
|
168
|
-
- regexp : filter MockCallHistoryLog when the regexp does not match on MockCallHistoryLog.toString() ([see](
|
|
168
|
+
- regexp : filter MockCallHistoryLog when the regexp does not match on MockCallHistoryLog.toString() ([see](/docs/docs/api/MockCallHistoryLog.md#to-string))
|
|
169
169
|
- object : an object with MockCallHistoryLog properties as keys to apply multiple filters. each values are a [filter parameter](/docs/docs/api/MockCallHistory.md#filter-parameter)
|
|
170
170
|
- options : the second parameter. an object.
|
|
171
171
|
- options.operator : `'AND'` or `'OR'` (default `'OR'`). Used only if criteria is an object. see below
|
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`
|
|
@@ -33,7 +36,7 @@ Implements [Client.destroyed](/docs/docs/api/Client.md#clientdestroyed)
|
|
|
33
36
|
|
|
34
37
|
### `Pool.stats`
|
|
35
38
|
|
|
36
|
-
Returns [`PoolStats`](PoolStats.md) instance for this pool.
|
|
39
|
+
Returns [`PoolStats`](/docs/docs/api/PoolStats.md) instance for this pool.
|
|
37
40
|
|
|
38
41
|
## Instance Methods
|
|
39
42
|
|
|
@@ -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
|
|
@@ -12,7 +12,7 @@ Arguments:
|
|
|
12
12
|
* **dispatcher** `undici.Dispatcher` (required) - the dispatcher to wrap
|
|
13
13
|
* **options** `RetryHandlerOptions` (optional) - the options
|
|
14
14
|
|
|
15
|
-
Returns: `
|
|
15
|
+
Returns: `RetryAgent`
|
|
16
16
|
|
|
17
17
|
### Parameter: `RetryHandlerOptions`
|
|
18
18
|
|
|
@@ -23,9 +23,9 @@ Returns: `ProxyAgent`
|
|
|
23
23
|
- **minTimeout** `number` (optional) - Minimum number of milliseconds to wait before retrying. Default: `500` (half a second)
|
|
24
24
|
- **timeoutFactor** `number` (optional) - Factor to multiply the timeout by for each retry attempt. Default: `2`
|
|
25
25
|
- **retryAfter** `boolean` (optional) - It enables automatic retry after the `Retry-After` header is received. Default: `true`
|
|
26
|
-
- **methods** `string[]` (optional) - Array of HTTP methods to retry. Default: `['GET', '
|
|
26
|
+
- **methods** `string[]` (optional) - Array of HTTP methods to retry. Default: `['GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE']`
|
|
27
27
|
- **statusCodes** `number[]` (optional) - Array of HTTP status codes to retry. Default: `[429, 500, 502, 503, 504]`
|
|
28
|
-
- **errorCodes** `string[]` (optional) - Array of Error codes to retry. Default: `['ECONNRESET', 'ECONNREFUSED', 'ENOTFOUND', 'ENETDOWN','ENETUNREACH', 'EHOSTDOWN', 'UND_ERR_SOCKET']`
|
|
28
|
+
- **errorCodes** `string[]` (optional) - Array of Error codes to retry. Default: `['ECONNRESET', 'ECONNREFUSED', 'ENOTFOUND', 'ENETDOWN', 'ENETUNREACH', 'EHOSTDOWN', 'EHOSTUNREACH', 'EPIPE', 'UND_ERR_SOCKET']`
|
|
29
29
|
|
|
30
30
|
**`RetryContext`**
|
|
31
31
|
|
|
@@ -4,12 +4,12 @@ Extends: `undici.DispatcherHandlers`
|
|
|
4
4
|
|
|
5
5
|
A handler class that implements the retry logic for a request.
|
|
6
6
|
|
|
7
|
-
## `new RetryHandler(
|
|
7
|
+
## `new RetryHandler(opts, { dispatch, handler })`
|
|
8
8
|
|
|
9
9
|
Arguments:
|
|
10
10
|
|
|
11
|
-
- **
|
|
12
|
-
- **
|
|
11
|
+
- **opts** `Dispatch.DispatchOptions & { retryOptions?: RetryOptions }` (required) - An intersection of `Dispatcher.DispatchOptions` and an optional `RetryOptions` object.
|
|
12
|
+
- **{ dispatch, handler }** `RetryHandlers` (required) - Object containing the `dispatch` to be used on every retry, and `handler` for handling the `dispatch` lifecycle.
|
|
13
13
|
|
|
14
14
|
Returns: `retryHandler`
|
|
15
15
|
|
|
@@ -20,15 +20,15 @@ Extends: [`Dispatch.DispatchOptions`](/docs/docs/api/Dispatcher.md#parameter-dis
|
|
|
20
20
|
#### `RetryOptions`
|
|
21
21
|
|
|
22
22
|
- **throwOnError** `boolean` (optional) - Disable to prevent throwing error on last retry attept, useful if you need the body on errors from server or if you have custom error handler.
|
|
23
|
-
- **retry** `(err: Error, context: RetryContext, callback: (err?: Error | null) => void) =>
|
|
23
|
+
- **retry** `(err: Error, context: RetryContext, callback: (err?: Error | null) => void) => void` (optional) - Function to be called after every retry. It should pass error if no more retries should be performed.
|
|
24
24
|
- **maxRetries** `number` (optional) - Maximum number of retries. Default: `5`
|
|
25
25
|
- **maxTimeout** `number` (optional) - Maximum number of milliseconds to wait before retrying. Default: `30000` (30 seconds)
|
|
26
26
|
- **minTimeout** `number` (optional) - Minimum number of milliseconds to wait before retrying. Default: `500` (half a second)
|
|
27
27
|
- **timeoutFactor** `number` (optional) - Factor to multiply the timeout by for each retry attempt. Default: `2`
|
|
28
28
|
- **retryAfter** `boolean` (optional) - It enables automatic retry after the `Retry-After` header is received. Default: `true`
|
|
29
|
-
- **methods** `string[]` (optional) - Array of HTTP methods to retry. Default: `['GET', '
|
|
29
|
+
- **methods** `string[]` (optional) - Array of HTTP methods to retry. Default: `['GET', 'HEAD', 'OPTIONS', 'PUT', 'DELETE', 'TRACE']`
|
|
30
30
|
- **statusCodes** `number[]` (optional) - Array of HTTP status codes to retry. Default: `[429, 500, 502, 503, 504]`
|
|
31
|
-
- **errorCodes** `string[]` (optional) - Array of Error codes to retry. Default: `['ECONNRESET', 'ECONNREFUSED', 'ENOTFOUND', 'ENETDOWN','ENETUNREACH', 'EHOSTDOWN', 'UND_ERR_SOCKET']`
|
|
31
|
+
- **errorCodes** `string[]` (optional) - Array of Error codes to retry. Default: `['ECONNRESET', 'ECONNREFUSED', 'ENOTFOUND', 'ENETDOWN', 'ENETUNREACH', 'EHOSTDOWN', 'EHOSTUNREACH', 'EPIPE', 'UND_ERR_SOCKET']`
|
|
32
32
|
|
|
33
33
|
**`RetryContext`**
|
|
34
34
|
|
|
@@ -66,7 +66,7 @@ Implements [Client.destroyed](/docs/docs/api/Client.md#clientdestroyed)
|
|
|
66
66
|
|
|
67
67
|
### `RoundRobinPool.stats`
|
|
68
68
|
|
|
69
|
-
Returns [`PoolStats`](PoolStats.md) instance for this pool.
|
|
69
|
+
Returns [`PoolStats`](/docs/docs/api/PoolStats.md) instance for this pool.
|
|
70
70
|
|
|
71
71
|
## Instance Methods
|
|
72
72
|
|
|
@@ -634,6 +634,6 @@ SnapshotAgent provides similar functionality to nock but is specifically designe
|
|
|
634
634
|
|
|
635
635
|
## See Also
|
|
636
636
|
|
|
637
|
-
- [MockAgent](
|
|
638
|
-
- [MockCallHistory](
|
|
639
|
-
- [Testing Best Practices](
|
|
637
|
+
- [MockAgent](/docs/docs/api/MockAgent.md) - Manual mocking for more control
|
|
638
|
+
- [MockCallHistory](/docs/docs/api/MockCallHistory.md) - Inspecting request history
|
|
639
|
+
- [Testing Best Practices](/docs/docs/best-practices/writing-tests.md) - General testing guidance
|
|
@@ -58,9 +58,9 @@ stateDiagram-v2
|
|
|
58
58
|
|
|
59
59
|
### idle
|
|
60
60
|
|
|
61
|
-
The **idle** state is the initial state of a `Client` instance. While an `origin` is required for instantiating a `Client` instance, the underlying socket connection will not be established until a request is queued using [`Client.dispatch()`](/docs/docs/api/Client.md#clientdispatchoptions-handlers). By calling `Client.dispatch()` directly or using one of the multiple implementations ([`Client.connect()`](Client.md#clientconnectoptions-callback), [`Client.pipeline()`](Client.md#clientpipelineoptions-handler), [`Client.request()`](Client.md#clientrequestoptions-callback), [`Client.stream()`](Client.md#clientstreamoptions-factory-callback), and [`Client.upgrade()`](/docs/docs/api/Client.md#clientupgradeoptions-callback)), the `Client` instance will transition from **idle** to [**pending**](/docs/docs/api/Client.md#pending) and then most likely directly to [**processing**](/docs/docs/api/Client.md#processing).
|
|
61
|
+
The **idle** state is the initial state of a `Client` instance. While an `origin` is required for instantiating a `Client` instance, the underlying socket connection will not be established until a request is queued using [`Client.dispatch()`](/docs/docs/api/Client.md#clientdispatchoptions-handlers). By calling `Client.dispatch()` directly or using one of the multiple implementations ([`Client.connect()`](/docs/docs/api/Client.md#clientconnectoptions-callback), [`Client.pipeline()`](/docs/docs/api/Client.md#clientpipelineoptions-handler), [`Client.request()`](/docs/docs/api/Client.md#clientrequestoptions-callback), [`Client.stream()`](/docs/docs/api/Client.md#clientstreamoptions-factory-callback), and [`Client.upgrade()`](/docs/docs/api/Client.md#clientupgradeoptions-callback)), the `Client` instance will transition from **idle** to [**pending**](/docs/docs/api/Client.md#pending) and then most likely directly to [**processing**](/docs/docs/api/Client.md#processing).
|
|
62
62
|
|
|
63
|
-
Calling [`Client.close()`](/docs/docs/api/Client.md#clientclosecallback) or [`Client.destroy()`](Client.md#clientdestroyerror-callback) transitions directly to the [**destroyed**](/docs/docs/api/Client.md#destroyed) state since the `Client` instance will have no queued requests in this state.
|
|
63
|
+
Calling [`Client.close()`](/docs/docs/api/Client.md#clientclosecallback) or [`Client.destroy()`](/docs/docs/api/Client.md#clientdestroyerror-callback) transitions directly to the [**destroyed**](/docs/docs/api/Client.md#destroyed) state since the `Client` instance will have no queued requests in this state.
|
|
64
64
|
|
|
65
65
|
### pending
|
|
66
66
|
|
|
@@ -72,11 +72,11 @@ Calling [`Client.destroy()`](/docs/docs/api/Client.md#clientdestroyerror-callbac
|
|
|
72
72
|
|
|
73
73
|
### processing
|
|
74
74
|
|
|
75
|
-
The **processing** state is a state machine within itself. It initializes to the [**processing.running**](/docs/docs/api/Client.md#running) state. The [`Client.dispatch()`](/docs/docs/api/Client.md#clientdispatchoptions-handlers), [`Client.close()`](Client.md#clientclosecallback), and [`Client.destroy()`](Client.md#clientdestroyerror-callback) can be called at any time while the `Client` is in this state. `Client.dispatch()` will add more requests to the queue while existing requests continue to be processed. `Client.close()` will transition to the [**processing.closing**](/docs/docs/api/Client.md#closing) state. And `Client.destroy()` will transition to [**destroyed**](/docs/docs/api/Client.md#destroyed).
|
|
75
|
+
The **processing** state is a state machine within itself. It initializes to the [**processing.running**](/docs/docs/api/Client.md#running) state. The [`Client.dispatch()`](/docs/docs/api/Client.md#clientdispatchoptions-handlers), [`Client.close()`](/docs/docs/api/Client.md#clientclosecallback), and [`Client.destroy()`](/docs/docs/api/Client.md#clientdestroyerror-callback) can be called at any time while the `Client` is in this state. `Client.dispatch()` will add more requests to the queue while existing requests continue to be processed. `Client.close()` will transition to the [**processing.closing**](/docs/docs/api/Client.md#closing) state. And `Client.destroy()` will transition to [**destroyed**](/docs/docs/api/Client.md#destroyed).
|
|
76
76
|
|
|
77
77
|
#### running
|
|
78
78
|
|
|
79
|
-
In the **processing.running** sub-state, queued requests are being processed in a FIFO order. If a request body requires draining, the *needDrain* event transitions to the [**processing.busy**](/docs/docs/api/Client.md#busy) sub-state. The *close* event transitions the Client to the [**process.closing**](/docs/docs/api/Client.md#closing) sub-state. If all queued requests are processed and neither [`Client.close()`](/docs/docs/api/Client.md#clientclosecallback) nor [`Client.destroy()`](Client.md#clientdestroyerror-callback) are called, then the [**processing**](/docs/docs/api/Client.md#processing) machine will trigger a *keepalive* event transitioning the `Client` back to the [**pending**](/docs/docs/api/Client.md#pending) state. During this time, the `Client` is waiting for the socket connection to timeout, and once it does, it triggers the *timeout* event and transitions to the [**idle**](/docs/docs/api/Client.md#idle) state.
|
|
79
|
+
In the **processing.running** sub-state, queued requests are being processed in a FIFO order. If a request body requires draining, the *needDrain* event transitions to the [**processing.busy**](/docs/docs/api/Client.md#busy) sub-state. The *close* event transitions the Client to the [**process.closing**](/docs/docs/api/Client.md#closing) sub-state. If all queued requests are processed and neither [`Client.close()`](/docs/docs/api/Client.md#clientclosecallback) nor [`Client.destroy()`](/docs/docs/api/Client.md#clientdestroyerror-callback) are called, then the [**processing**](/docs/docs/api/Client.md#processing) machine will trigger a *keepalive* event transitioning the `Client` back to the [**pending**](/docs/docs/api/Client.md#pending) state. During this time, the `Client` is waiting for the socket connection to timeout, and once it does, it triggers the *timeout* event and transitions to the [**idle**](/docs/docs/api/Client.md#idle) state.
|
|
80
80
|
|
|
81
81
|
#### busy
|
|
82
82
|
|
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
|
|
|
@@ -526,6 +556,19 @@ function onUpgradeStreamClose () {
|
|
|
526
556
|
}
|
|
527
557
|
|
|
528
558
|
function onRequestStreamClose () {
|
|
559
|
+
const state = this[kRequestStreamState]
|
|
560
|
+
|
|
561
|
+
if (state) {
|
|
562
|
+
// Release the stream first so request references are cleared,
|
|
563
|
+
// then complete the response with trailers if available.
|
|
564
|
+
releaseRequestStream(this)
|
|
565
|
+
|
|
566
|
+
if (state.pendingEnd && !state.request.aborted && !state.request.completed) {
|
|
567
|
+
state.request.onResponseEnd(state.trailers || {})
|
|
568
|
+
state.finalizeRequest()
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
|
|
529
572
|
this.off('data', onData)
|
|
530
573
|
this.off('error', noop)
|
|
531
574
|
closeStreamSession(this)
|
|
@@ -629,7 +672,7 @@ function onUpgradeStreamEnd () {
|
|
|
629
672
|
|
|
630
673
|
function onUpgradeStreamTimeout () {
|
|
631
674
|
const state = this[kRequestStreamState]
|
|
632
|
-
failUpgradeStream(state, new InformationalError(`HTTP/2: "stream timeout after ${state.
|
|
675
|
+
failUpgradeStream(state, new InformationalError(`HTTP/2: "stream timeout after ${state.headersTimeout}"`))
|
|
633
676
|
}
|
|
634
677
|
|
|
635
678
|
function onUpgradeResponse (headers, _flags) {
|
|
@@ -654,7 +697,7 @@ function onUpgradeResponse (headers, _flags) {
|
|
|
654
697
|
}
|
|
655
698
|
|
|
656
699
|
function setupUpgradeStream (stream, state) {
|
|
657
|
-
const { request,
|
|
700
|
+
const { request, headersTimeout, session } = state
|
|
658
701
|
|
|
659
702
|
stream[kHTTP2Stream] = true
|
|
660
703
|
stream[kHTTP2Session] = session
|
|
@@ -669,11 +712,12 @@ function setupUpgradeStream (stream, state) {
|
|
|
669
712
|
stream.once('close', onUpgradeStreamClose)
|
|
670
713
|
|
|
671
714
|
++session[kOpenStreams]
|
|
672
|
-
stream.setTimeout(
|
|
715
|
+
stream.setTimeout(headersTimeout)
|
|
673
716
|
}
|
|
674
717
|
|
|
675
718
|
function writeH2 (client, request) {
|
|
676
|
-
const
|
|
719
|
+
const headersTimeout = request.headersTimeout ?? client[kHeadersTimeout]
|
|
720
|
+
const bodyTimeout = request.bodyTimeout ?? client[kBodyTimeout]
|
|
677
721
|
const session = client[kHTTP2Session]
|
|
678
722
|
const { method, path, host, upgrade, expectContinue, signal, protocol, headers: reqHeaders } = request
|
|
679
723
|
let { body } = request
|
|
@@ -736,8 +780,14 @@ function writeH2 (client, request) {
|
|
|
736
780
|
try {
|
|
737
781
|
return session.request(headers, options)
|
|
738
782
|
} catch (err) {
|
|
739
|
-
if (err?.code
|
|
740
|
-
|
|
783
|
+
if (err?.code === 'ERR_HTTP2_INVALID_SESSION') {
|
|
784
|
+
const wrappedErr = new SocketError(err.message, util.getSocketInfo(session[kSocket]))
|
|
785
|
+
wrappedErr.cause = err
|
|
786
|
+
session[kError] = wrappedErr
|
|
787
|
+
resetHttp2Session(session, wrappedErr)
|
|
788
|
+
requeueUnsentRequest(client, request)
|
|
789
|
+
|
|
790
|
+
return null
|
|
741
791
|
}
|
|
742
792
|
|
|
743
793
|
const wrappedErr = new InformationalError(err.message, { cause: err })
|
|
@@ -771,7 +821,8 @@ function writeH2 (client, request) {
|
|
|
771
821
|
abort,
|
|
772
822
|
finalizeRequest,
|
|
773
823
|
request,
|
|
774
|
-
|
|
824
|
+
headersTimeout,
|
|
825
|
+
bodyTimeout,
|
|
775
826
|
responseReceived: false,
|
|
776
827
|
session,
|
|
777
828
|
stream: null
|
|
@@ -912,7 +963,8 @@ function writeH2 (client, request) {
|
|
|
912
963
|
expectsPayload,
|
|
913
964
|
finalizeRequest,
|
|
914
965
|
request,
|
|
915
|
-
|
|
966
|
+
headersTimeout,
|
|
967
|
+
bodyTimeout,
|
|
916
968
|
responseReceived: false,
|
|
917
969
|
session,
|
|
918
970
|
stream: null
|
|
@@ -929,11 +981,10 @@ function writeH2 (client, request) {
|
|
|
929
981
|
stream[kHTTP2Stream] = true
|
|
930
982
|
stream[kRequestStreamState] = state
|
|
931
983
|
state.stream = stream
|
|
932
|
-
bindRequestToStream(request, stream, null)
|
|
933
984
|
|
|
934
985
|
// Increment counter as we have new streams open
|
|
935
986
|
++session[kOpenStreams]
|
|
936
|
-
stream.setTimeout(
|
|
987
|
+
stream.setTimeout(headersTimeout)
|
|
937
988
|
|
|
938
989
|
stream[kHTTP2Session] = session
|
|
939
990
|
stream.once('close', onRequestStreamClose)
|
|
@@ -1017,6 +1068,7 @@ function onResponse (headers) {
|
|
|
1017
1068
|
delete headers[HTTP2_HEADER_STATUS]
|
|
1018
1069
|
request.onResponseStarted()
|
|
1019
1070
|
state.responseReceived = true
|
|
1071
|
+
stream.setTimeout(state.bodyTimeout)
|
|
1020
1072
|
|
|
1021
1073
|
// Due to the stream nature, it is possible we face a race condition
|
|
1022
1074
|
// where the stream has been assigned, but the request has been aborted
|
|
@@ -1042,14 +1094,14 @@ function onEnd () {
|
|
|
1042
1094
|
|
|
1043
1095
|
stream.off('end', onEnd)
|
|
1044
1096
|
|
|
1045
|
-
|
|
1046
|
-
//
|
|
1097
|
+
// If we received a response, this is a normal completion.
|
|
1098
|
+
// Defer actual completion to onRequestStreamClose so that
|
|
1099
|
+
// onTrailers (which may fire after 'end' on Windows) can
|
|
1100
|
+
// store trailers first.
|
|
1047
1101
|
if (state.responseReceived) {
|
|
1048
1102
|
if (!request.aborted && !request.completed) {
|
|
1049
|
-
|
|
1103
|
+
state.pendingEnd = true
|
|
1050
1104
|
}
|
|
1051
|
-
|
|
1052
|
-
state.finalizeRequest()
|
|
1053
1105
|
} else {
|
|
1054
1106
|
// Stream ended without receiving a response - this is an error
|
|
1055
1107
|
// (e.g., server destroyed the stream before sending headers)
|
|
@@ -1062,8 +1114,6 @@ function onError (err) {
|
|
|
1062
1114
|
const state = stream[kRequestStreamState]
|
|
1063
1115
|
|
|
1064
1116
|
stream.off('error', onError)
|
|
1065
|
-
|
|
1066
|
-
releaseRequestStream(stream)
|
|
1067
1117
|
state.abort(err)
|
|
1068
1118
|
}
|
|
1069
1119
|
|
|
@@ -1072,8 +1122,6 @@ function onFrameError (type, code) {
|
|
|
1072
1122
|
const state = stream[kRequestStreamState]
|
|
1073
1123
|
|
|
1074
1124
|
stream.off('frameError', onFrameError)
|
|
1075
|
-
|
|
1076
|
-
releaseRequestStream(stream)
|
|
1077
1125
|
state.abort(new InformationalError(`HTTP/2: "frameError" received - type ${type}, code ${code}`))
|
|
1078
1126
|
}
|
|
1079
1127
|
|
|
@@ -1085,9 +1133,12 @@ function onTimeout () {
|
|
|
1085
1133
|
const stream = this
|
|
1086
1134
|
const state = stream[kRequestStreamState]
|
|
1087
1135
|
|
|
1088
|
-
|
|
1136
|
+
// Remove self so timeout doesn't fire again after we handle it
|
|
1137
|
+
stream.off('timeout', onTimeout)
|
|
1089
1138
|
|
|
1090
|
-
const err =
|
|
1139
|
+
const err = state.responseReceived
|
|
1140
|
+
? new BodyTimeoutError(`HTTP/2: "stream timeout after ${state.bodyTimeout}"`)
|
|
1141
|
+
: new HeadersTimeoutError(`HTTP/2: "headers timeout after ${state.headersTimeout}"`)
|
|
1091
1142
|
state.abort(err)
|
|
1092
1143
|
}
|
|
1093
1144
|
|
|
@@ -1097,14 +1148,14 @@ function onTrailers (trailers) {
|
|
|
1097
1148
|
const { request } = state
|
|
1098
1149
|
|
|
1099
1150
|
stream.off('trailers', onTrailers)
|
|
1151
|
+
stream.off('data', onData)
|
|
1100
1152
|
|
|
1101
1153
|
if (request.aborted || request.completed) {
|
|
1102
1154
|
return
|
|
1103
1155
|
}
|
|
1104
1156
|
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
state.finalizeRequest()
|
|
1157
|
+
// Store trailers for onRequestStreamClose to use when completing
|
|
1158
|
+
state.trailers = trailers
|
|
1108
1159
|
}
|
|
1109
1160
|
|
|
1110
1161
|
function writeBodyH2 () {
|