@noodleseed/one 0.21.0 → 0.23.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/dist/commands/author-loop.d.ts.map +1 -1
- package/dist/commands/author-loop.js +2 -2
- package/dist/commands/author-loop.js.map +1 -1
- package/dist/commands/catalog-dispatch.d.ts.map +1 -1
- package/dist/commands/catalog-dispatch.js +21 -2
- package/dist/commands/catalog-dispatch.js.map +1 -1
- package/dist/commands/deploy-ops.d.ts.map +1 -1
- package/dist/commands/deploy-ops.js +28 -11
- package/dist/commands/deploy-ops.js.map +1 -1
- package/dist/first-run.d.ts +11 -0
- package/dist/first-run.d.ts.map +1 -1
- package/dist/first-run.js +19 -4
- package/dist/first-run.js.map +1 -1
- package/dist/project-scaffold-templates.d.ts +11 -0
- package/dist/project-scaffold-templates.d.ts.map +1 -0
- package/dist/project-scaffold-templates.js +359 -0
- package/dist/project-scaffold-templates.js.map +1 -0
- package/dist/project.d.ts.map +1 -1
- package/dist/project.js +1 -323
- package/dist/project.js.map +1 -1
- package/dist/react.d.ts +2 -2
- package/dist/react.d.ts.map +1 -1
- package/dist/react.js +1 -1
- package/dist/react.js.map +1 -1
- package/dist/validate.d.ts +12 -0
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +11 -0
- package/dist/validate.js.map +1 -1
- package/node_modules/@noodle-borg/agent-kit/dist/curated/hook-notes.d.ts.map +1 -1
- package/node_modules/@noodle-borg/agent-kit/dist/curated/hook-notes.js +2 -1
- package/node_modules/@noodle-borg/agent-kit/dist/curated/hook-notes.js.map +1 -1
- package/node_modules/@noodle-borg/agent-kit/dist/generated/surface.d.ts.map +1 -1
- package/node_modules/@noodle-borg/agent-kit/dist/generated/surface.js +1 -0
- package/node_modules/@noodle-borg/agent-kit/dist/generated/surface.js.map +1 -1
- package/node_modules/@noodle-borg/agent-kit/dist/skill-content.d.ts.map +1 -1
- package/node_modules/@noodle-borg/agent-kit/dist/skill-content.js +6 -9
- package/node_modules/@noodle-borg/agent-kit/dist/skill-content.js.map +1 -1
- package/node_modules/@noodle-borg/agent-kit/package.json +1 -1
- package/node_modules/@noodle-borg/authoring/dist/index.d.ts +3 -3
- package/node_modules/@noodle-borg/authoring/dist/index.d.ts.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/index.js +1 -1
- package/node_modules/@noodle-borg/authoring/dist/index.js.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react/bridge.d.ts +9 -1
- package/node_modules/@noodle-borg/authoring/dist/react/bridge.d.ts.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react/bridge.js +11 -1
- package/node_modules/@noodle-borg/authoring/dist/react/bridge.js.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react/components.d.ts +306 -9
- package/node_modules/@noodle-borg/authoring/dist/react/components.d.ts.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react/components.js +63 -11
- package/node_modules/@noodle-borg/authoring/dist/react/components.js.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react/hooks.d.ts +2 -0
- package/node_modules/@noodle-borg/authoring/dist/react/hooks.d.ts.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react/hooks.js +11 -0
- package/node_modules/@noodle-borg/authoring/dist/react/hooks.js.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react/index.d.ts +7 -3
- package/node_modules/@noodle-borg/authoring/dist/react/index.d.ts.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react/index.js +4 -2
- package/node_modules/@noodle-borg/authoring/dist/react/index.js.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react/native-components.d.ts +1159 -0
- package/node_modules/@noodle-borg/authoring/dist/react/native-components.d.ts.map +1 -0
- package/node_modules/@noodle-borg/authoring/dist/react/native-components.js +66 -0
- package/node_modules/@noodle-borg/authoring/dist/react/native-components.js.map +1 -0
- package/node_modules/@noodle-borg/authoring/dist/react/semantic-components.d.ts +2618 -0
- package/node_modules/@noodle-borg/authoring/dist/react/semantic-components.d.ts.map +1 -0
- package/node_modules/@noodle-borg/authoring/dist/react/semantic-components.js +110 -0
- package/node_modules/@noodle-borg/authoring/dist/react/semantic-components.js.map +1 -0
- package/node_modules/@noodle-borg/authoring/dist/react.d.ts +2 -2
- package/node_modules/@noodle-borg/authoring/dist/react.d.ts.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react.js +1 -1
- package/node_modules/@noodle-borg/authoring/dist/react.js.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/recording.d.ts +1 -1
- package/node_modules/@noodle-borg/authoring/dist/recording.d.ts.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/recording.js +14 -1
- package/node_modules/@noodle-borg/authoring/dist/recording.js.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/server.d.ts +25 -2
- package/node_modules/@noodle-borg/authoring/dist/server.d.ts.map +1 -1
- package/node_modules/@noodle-borg/authoring/dist/server.js +7 -3
- package/node_modules/@noodle-borg/authoring/dist/server.js.map +1 -1
- package/node_modules/@noodle-borg/compiler/dist/compile.d.ts.map +1 -1
- package/node_modules/@noodle-borg/compiler/dist/compile.js +20 -8
- package/node_modules/@noodle-borg/compiler/dist/compile.js.map +1 -1
- package/node_modules/@noodle-borg/compiler/dist/manifest/schema.d.ts +2 -0
- package/node_modules/@noodle-borg/compiler/dist/manifest/schema.d.ts.map +1 -1
- package/node_modules/@noodle-borg/compiler/dist/manifest/schema.js +6 -0
- package/node_modules/@noodle-borg/compiler/dist/manifest/schema.js.map +1 -1
- package/node_modules/@noodle-borg/connector-http/package.json +1 -1
- package/node_modules/@noodle-borg/protocol/dist/mapping.d.ts.map +1 -1
- package/node_modules/@noodle-borg/protocol/dist/mapping.js +8 -0
- package/node_modules/@noodle-borg/protocol/dist/mapping.js.map +1 -1
- package/node_modules/hono/dist/adapter/aws-lambda/handler.js +15 -21
- package/node_modules/hono/dist/adapter/bun/websocket.js +3 -1
- package/node_modules/hono/dist/adapter/lambda-edge/handler.js +8 -2
- package/node_modules/hono/dist/cjs/adapter/aws-lambda/handler.js +15 -21
- package/node_modules/hono/dist/cjs/adapter/bun/websocket.js +3 -1
- package/node_modules/hono/dist/cjs/adapter/lambda-edge/handler.js +8 -2
- package/node_modules/hono/dist/cjs/helper/css/common.js +3 -1
- package/node_modules/hono/dist/cjs/helper/css/index.js +9 -1
- package/node_modules/hono/dist/cjs/jsx/base.js +8 -14
- package/node_modules/hono/dist/cjs/jsx/components.js +41 -21
- package/node_modules/hono/dist/cjs/jsx/context.js +131 -5
- package/node_modules/hono/dist/cjs/jsx/streaming.js +9 -7
- package/node_modules/hono/dist/cjs/middleware/cors/index.js +2 -5
- package/node_modules/hono/dist/cjs/middleware/serve-static/index.js +2 -2
- package/node_modules/hono/dist/cjs/utils/body.js +12 -4
- package/node_modules/hono/dist/cjs/utils/buffer.js +2 -1
- package/node_modules/hono/dist/cjs/validator/validator.js +3 -3
- package/node_modules/hono/dist/helper/css/common.js +3 -1
- package/node_modules/hono/dist/helper/css/index.js +9 -1
- package/node_modules/hono/dist/jsx/base.js +15 -15
- package/node_modules/hono/dist/jsx/components.js +42 -22
- package/node_modules/hono/dist/jsx/context.js +129 -5
- package/node_modules/hono/dist/jsx/streaming.js +10 -8
- package/node_modules/hono/dist/middleware/cors/index.js +2 -5
- package/node_modules/hono/dist/middleware/serve-static/index.js +2 -2
- package/node_modules/hono/dist/types/adapter/aws-lambda/handler.d.ts +1 -1
- package/node_modules/hono/dist/types/jsx/base.d.ts +1 -3
- package/node_modules/hono/dist/types/jsx/context.d.ts +39 -0
- package/node_modules/hono/dist/types/middleware/context-storage/index.d.ts +2 -2
- package/node_modules/hono/dist/types/utils/body.d.ts +1 -1
- package/node_modules/hono/dist/utils/body.js +12 -4
- package/node_modules/hono/dist/utils/buffer.js +2 -1
- package/node_modules/hono/dist/validator/validator.js +3 -3
- package/node_modules/hono/package.json +9 -10
- package/node_modules/tailwindcss/dist/chunk-HMCCH6MG.mjs +1 -0
- package/node_modules/tailwindcss/dist/default-theme.mjs +1 -1
- package/node_modules/tailwindcss/dist/lib.js +13 -13
- package/node_modules/tailwindcss/dist/lib.mjs +18 -18
- package/node_modules/tailwindcss/package.json +2 -2
- package/node_modules/undici/README.md +3 -2
- package/node_modules/undici/docs/docs/api/Agent.md +248 -44
- package/node_modules/undici/docs/docs/api/BalancedPool.md +246 -48
- package/node_modules/undici/docs/docs/api/CacheStorage.md +171 -13
- package/node_modules/undici/docs/docs/api/CacheStore.md +294 -98
- package/node_modules/undici/docs/docs/api/Client.md +365 -178
- package/node_modules/undici/docs/docs/api/ClientStats.md +80 -14
- package/node_modules/undici/docs/docs/api/Connector.md +118 -36
- package/node_modules/undici/docs/docs/api/ContentType.md +65 -25
- package/node_modules/undici/docs/docs/api/Cookies.md +125 -47
- package/node_modules/undici/docs/docs/api/Debug.md +34 -18
- package/node_modules/undici/docs/docs/api/DiagnosticsChannel.md +364 -164
- package/node_modules/undici/docs/docs/api/Dispatcher.md +479 -1093
- package/node_modules/undici/docs/docs/api/EnvHttpProxyAgent.md +101 -87
- package/node_modules/undici/docs/docs/api/Errors.md +503 -40
- package/node_modules/undici/docs/docs/api/EventSource.md +294 -32
- package/node_modules/undici/docs/docs/api/Fetch.md +680 -31
- package/node_modules/undici/docs/docs/api/GlobalInstallation.md +78 -98
- package/node_modules/undici/docs/docs/api/H2CClient.md +298 -176
- package/node_modules/undici/docs/docs/api/Interceptors.md +355 -0
- package/node_modules/undici/docs/docs/api/MockAgent.md +314 -322
- package/node_modules/undici/docs/docs/api/MockCallHistory.md +272 -98
- package/node_modules/undici/docs/docs/api/MockCallHistoryLog.md +189 -26
- package/node_modules/undici/docs/docs/api/MockClient.md +151 -33
- package/node_modules/undici/docs/docs/api/MockErrors.md +56 -5
- package/node_modules/undici/docs/docs/api/MockPool.md +299 -263
- package/node_modules/undici/docs/docs/api/Pool.md +235 -43
- package/node_modules/undici/docs/docs/api/PoolStats.md +119 -13
- package/node_modules/undici/docs/docs/api/ProxyAgent.md +179 -160
- package/node_modules/undici/docs/docs/api/RedirectHandler.md +238 -63
- package/node_modules/undici/docs/docs/api/RetryAgent.md +113 -26
- package/node_modules/undici/docs/docs/api/RetryHandler.md +161 -77
- package/node_modules/undici/docs/docs/api/RoundRobinPool.md +166 -72
- package/node_modules/undici/docs/docs/api/SnapshotAgent.md +264 -542
- package/node_modules/undici/docs/docs/api/Socks5ProxyAgent.md +162 -186
- package/node_modules/undici/docs/docs/api/Util.md +68 -11
- package/node_modules/undici/docs/docs/api/WebSocket.md +475 -80
- package/node_modules/undici/docs/docs/api/api-lifecycle.md +100 -32
- package/node_modules/undici/docs/docs/best-practices/client-certificate.md +2 -0
- package/node_modules/undici/docs/docs/best-practices/crawling.md +3 -1
- package/node_modules/undici/docs/docs/best-practices/migrating-from-v7-to-v8.md +6 -4
- package/node_modules/undici/docs/docs/best-practices/mocking-request.md +9 -7
- package/node_modules/undici/docs/docs/best-practices/proxy.md +3 -1
- package/node_modules/undici/docs/docs/best-practices/undici-vs-builtin-fetch.md +10 -8
- package/node_modules/undici/docs/docs/best-practices/writing-tests.md +2 -0
- package/node_modules/undici/docs/docs/{GettingStarted.md → getting-started.md} +22 -19
- package/node_modules/undici/docs/docs/index.md +779 -0
- package/node_modules/undici/docs/docs/site.json +125 -0
- package/node_modules/undici/docs/docs/type-map.json +79 -0
- package/node_modules/undici/lib/api/api-request.js +7 -1
- package/node_modules/undici/lib/api/readable.js +47 -2
- package/node_modules/undici/lib/core/errors.js +20 -0
- package/node_modules/undici/lib/core/request.js +1 -1
- package/node_modules/undici/lib/core/util.js +24 -1
- package/node_modules/undici/lib/dispatcher/client-h1.js +92 -1
- package/node_modules/undici/lib/dispatcher/client-h2.js +322 -118
- package/node_modules/undici/lib/dispatcher/client.js +6 -2
- package/node_modules/undici/lib/dispatcher/dispatcher-base.js +1 -0
- package/node_modules/undici/lib/dispatcher/proxy-agent.js +42 -7
- package/node_modules/undici/lib/dispatcher/socks5-proxy-agent.js +4 -2
- package/node_modules/undici/lib/handler/redirect-handler.js +1 -0
- package/node_modules/undici/lib/handler/retry-handler.js +57 -19
- package/node_modules/undici/lib/util/cache.js +8 -2
- package/node_modules/undici/lib/web/cookies/parse.js +20 -27
- package/node_modules/undici/lib/web/cookies/util.js +1 -1
- package/node_modules/undici/lib/web/eventsource/eventsource.js +7 -18
- package/node_modules/undici/lib/web/eventsource/util.js +32 -1
- package/node_modules/undici/lib/web/fetch/body.js +43 -0
- package/node_modules/undici/lib/web/fetch/constants.js +1 -1
- package/node_modules/undici/lib/web/fetch/index.js +11 -2
- package/node_modules/undici/lib/web/fetch/request.js +1 -0
- package/node_modules/undici/lib/web/fetch/util.js +4 -1
- package/node_modules/undici/lib/web/websocket/receiver.js +20 -3
- package/node_modules/undici/lib/web/websocket/stream/websocketstream.js +8 -1
- package/node_modules/undici/lib/web/websocket/websocket.js +3 -1
- package/node_modules/undici/package.json +1 -1
- package/node_modules/undici/types/client.d.ts +6 -1
- package/node_modules/undici/types/cookies.d.ts +1 -1
- package/node_modules/undici/types/errors.d.ts +10 -0
- package/node_modules/undici/types/fetch.d.ts +1 -0
- package/node_modules/undici/types/handlers.d.ts +2 -0
- package/node_modules/undici/types/proxy-agent.d.ts +7 -0
- package/package.json +8 -6
- package/react/styles.css +851 -0
- package/react/styles.source.css +850 -0
- package/node_modules/hono/dist/tsconfig.build.tsbuildinfo +0 -1
- package/node_modules/tailwindcss/dist/chunk-XVOTX5KS.mjs +0 -1
|
@@ -1,34 +1,61 @@
|
|
|
1
1
|
# Dispatcher
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<!--introduced_in=v4.0.0-->
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
<!--type=module-->
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
<!-- source_link=lib/dispatcher/dispatcher.js -->
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
> Stability: 2 - Stable
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
`Dispatcher` is the core abstraction used to dispatch HTTP requests in undici.
|
|
12
|
+
It extends [`EventEmitter`][] and defines the low-level [`dispatch()`][] contract
|
|
13
|
+
along with the higher-level [`request()`][], [`stream()`][], [`pipeline()`][],
|
|
14
|
+
[`connect()`][], and [`upgrade()`][] methods that every concrete dispatcher
|
|
15
|
+
provides.
|
|
12
16
|
|
|
13
|
-
|
|
17
|
+
`Dispatcher` itself is abstract: its [`dispatch()`][], [`close()`][], and
|
|
18
|
+
[`destroy()`][] methods throw `Error: not implemented`. Concrete dispatchers such
|
|
19
|
+
as [`Client`][], [`Pool`][], [`BalancedPool`][], and [`Agent`][] implement the
|
|
20
|
+
contract and are what application code instantiates. Requests are not guaranteed
|
|
21
|
+
to be dispatched in the order in which they are invoked.
|
|
14
22
|
|
|
15
|
-
|
|
23
|
+
```mjs
|
|
24
|
+
import { Dispatcher, Agent } from 'undici'
|
|
16
25
|
|
|
17
|
-
|
|
26
|
+
const dispatcher = new Agent()
|
|
27
|
+
console.log(dispatcher instanceof Dispatcher) // true
|
|
28
|
+
```
|
|
18
29
|
|
|
19
|
-
|
|
30
|
+
## Class: `Dispatcher`
|
|
20
31
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
32
|
+
<!-- YAML
|
|
33
|
+
added: v4.0.0
|
|
34
|
+
-->
|
|
35
|
+
|
|
36
|
+
* Extends: {EventEmitter}
|
|
37
|
+
|
|
38
|
+
Base dispatcher class. Concrete dispatchers extend it and implement the
|
|
39
|
+
[`dispatch()`][] method on which the other request helpers are built.
|
|
40
|
+
|
|
41
|
+
### `dispatcher.close([callback])`
|
|
42
|
+
|
|
43
|
+
<!-- YAML
|
|
44
|
+
added: v4.0.0
|
|
45
|
+
-->
|
|
46
|
+
|
|
47
|
+
* `callback` {Function} (optional) Invoked once the dispatcher is closed.
|
|
48
|
+
* `error` {Error|null}
|
|
49
|
+
* Returns: {Promise} A `Promise` is returned only when `callback` is omitted. It
|
|
50
|
+
resolves with `void` once the dispatcher is closed.
|
|
25
51
|
|
|
26
|
-
|
|
52
|
+
Closes the dispatcher and gracefully waits for enqueued requests to complete
|
|
53
|
+
before resolving or invoking `callback`.
|
|
27
54
|
|
|
28
|
-
```
|
|
29
|
-
import { createServer } from 'http'
|
|
55
|
+
```mjs
|
|
56
|
+
import { createServer } from 'node:http'
|
|
30
57
|
import { Client } from 'undici'
|
|
31
|
-
import { once } from 'events'
|
|
58
|
+
import { once } from 'node:events'
|
|
32
59
|
|
|
33
60
|
const server = createServer((request, response) => {
|
|
34
61
|
response.end('undici')
|
|
@@ -39,10 +66,7 @@ await once(server, 'listening')
|
|
|
39
66
|
const client = new Client(`http://localhost:${server.address().port}`)
|
|
40
67
|
|
|
41
68
|
try {
|
|
42
|
-
const { body } = await client.request({
|
|
43
|
-
path: '/',
|
|
44
|
-
method: 'GET'
|
|
45
|
-
})
|
|
69
|
+
const { body } = await client.request({ path: '/', method: 'GET' })
|
|
46
70
|
body.setEncoding('utf8')
|
|
47
71
|
body.on('data', console.log)
|
|
48
72
|
} catch (error) {}
|
|
@@ -53,40 +77,41 @@ console.log('Client closed')
|
|
|
53
77
|
server.close()
|
|
54
78
|
```
|
|
55
79
|
|
|
56
|
-
### `
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
*
|
|
70
|
-
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
80
|
+
### `dispatcher.connect(options[, callback])`
|
|
81
|
+
|
|
82
|
+
<!-- YAML
|
|
83
|
+
added: v4.0.0
|
|
84
|
+
-->
|
|
85
|
+
|
|
86
|
+
* `options` {Object}
|
|
87
|
+
* `origin` {string|URL} The origin to connect to.
|
|
88
|
+
* `path` {string} The request path.
|
|
89
|
+
* `headers` {UndiciHeaders} Request headers. **Default:** `null`.
|
|
90
|
+
* `signal` {AbortSignal|EventEmitter|null} A signal used to abort the request.
|
|
91
|
+
**Default:** `null`.
|
|
92
|
+
* `opaque` {any} An opaque value passed through to the returned `ConnectData`.
|
|
93
|
+
* `responseHeaders` {string|null} Set to `'raw'` to return the response headers
|
|
94
|
+
as a raw array instead of an object. **Default:** `null`.
|
|
95
|
+
* `callback` {Function} (optional) Invoked when the connection is established.
|
|
96
|
+
* `error` {Error|null}
|
|
97
|
+
* `data` {Object}
|
|
98
|
+
* `statusCode` {number}
|
|
99
|
+
* `headers` {Object} The response headers.
|
|
100
|
+
* `socket` {Duplex} The established socket.
|
|
101
|
+
* `opaque` {any} The `opaque` value passed in `options`.
|
|
102
|
+
* Returns: {Promise} A `Promise` is returned only when `callback` is omitted. It
|
|
103
|
+
resolves with the `data` object described above.
|
|
104
|
+
|
|
105
|
+
Starts two-way communications with the requested resource using the
|
|
106
|
+
[HTTP `CONNECT`][] method.
|
|
107
|
+
|
|
108
|
+
```mjs
|
|
109
|
+
import { createServer } from 'node:http'
|
|
85
110
|
import { Client } from 'undici'
|
|
86
|
-
import { once } from 'events'
|
|
111
|
+
import { once } from 'node:events'
|
|
87
112
|
|
|
88
113
|
const server = createServer((request, response) => {
|
|
89
|
-
throw Error('should never get here')
|
|
114
|
+
throw new Error('should never get here')
|
|
90
115
|
}).listen()
|
|
91
116
|
|
|
92
117
|
server.on('connect', (req, socket, head) => {
|
|
@@ -107,9 +132,7 @@ await once(server, 'listening')
|
|
|
107
132
|
const client = new Client(`http://localhost:${server.address().port}`)
|
|
108
133
|
|
|
109
134
|
try {
|
|
110
|
-
const { socket } = await client.connect({
|
|
111
|
-
path: '/'
|
|
112
|
-
})
|
|
135
|
+
const { socket } = await client.connect({ path: '/' })
|
|
113
136
|
const wanted = 'Body'
|
|
114
137
|
let data = ''
|
|
115
138
|
socket.on('data', d => { data += d })
|
|
@@ -120,35 +143,39 @@ try {
|
|
|
120
143
|
})
|
|
121
144
|
socket.write(wanted)
|
|
122
145
|
socket.end()
|
|
123
|
-
} catch (error) {
|
|
146
|
+
} catch (error) {}
|
|
124
147
|
```
|
|
125
148
|
|
|
126
|
-
### `
|
|
127
|
-
|
|
128
|
-
Destroy the dispatcher abruptly with the given error. All the pending and running requests will be asynchronously aborted and error. Since this operation is asynchronously dispatched there might still be some progress on dispatched requests.
|
|
149
|
+
### `dispatcher.destroy([error[, callback]])`
|
|
129
150
|
|
|
130
|
-
|
|
151
|
+
<!-- YAML
|
|
152
|
+
added: v4.0.0
|
|
153
|
+
-->
|
|
131
154
|
|
|
132
|
-
|
|
155
|
+
* `error` {Error|null} (optional) The error with which pending requests are
|
|
156
|
+
rejected. **Default:** `null`.
|
|
157
|
+
* `callback` {Function} (optional) Invoked once the socket is closed.
|
|
158
|
+
* Returns: {Promise} A `Promise` is returned only when `callback` is omitted. It
|
|
159
|
+
resolves with `void` once the dispatcher is destroyed.
|
|
133
160
|
|
|
134
|
-
|
|
135
|
-
|
|
161
|
+
Destroys the dispatcher abruptly with the given `error`. All pending and running
|
|
162
|
+
requests are asynchronously aborted and rejected. Because the operation is
|
|
163
|
+
dispatched asynchronously, dispatched requests may still make some progress
|
|
164
|
+
before being aborted.
|
|
136
165
|
|
|
137
|
-
|
|
166
|
+
Both arguments are optional, so the method can be called in four ways:
|
|
138
167
|
|
|
139
|
-
```
|
|
168
|
+
```mjs
|
|
140
169
|
dispatcher.destroy() // -> Promise
|
|
141
170
|
dispatcher.destroy(new Error()) // -> Promise
|
|
142
171
|
dispatcher.destroy(() => {}) // -> void
|
|
143
172
|
dispatcher.destroy(new Error(), () => {}) // -> void
|
|
144
173
|
```
|
|
145
174
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
```js
|
|
149
|
-
import { createServer } from 'http'
|
|
175
|
+
```mjs
|
|
176
|
+
import { createServer } from 'node:http'
|
|
150
177
|
import { Client } from 'undici'
|
|
151
|
-
import { once } from 'events'
|
|
178
|
+
import { once } from 'node:events'
|
|
152
179
|
|
|
153
180
|
const server = createServer((request, response) => {
|
|
154
181
|
response.end()
|
|
@@ -159,10 +186,7 @@ await once(server, 'listening')
|
|
|
159
186
|
const client = new Client(`http://localhost:${server.address().port}`)
|
|
160
187
|
|
|
161
188
|
try {
|
|
162
|
-
const request = client.request({
|
|
163
|
-
path: '/',
|
|
164
|
-
method: 'GET'
|
|
165
|
-
})
|
|
189
|
+
const request = client.request({ path: '/', method: 'GET' })
|
|
166
190
|
client.destroy()
|
|
167
191
|
.then(() => {
|
|
168
192
|
console.log('Client destroyed')
|
|
@@ -174,86 +198,107 @@ try {
|
|
|
174
198
|
}
|
|
175
199
|
```
|
|
176
200
|
|
|
177
|
-
### `
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
*
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
*
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
*
|
|
199
|
-
|
|
200
|
-
*
|
|
201
|
-
|
|
202
|
-
*
|
|
203
|
-
|
|
204
|
-
*
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
*
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
*
|
|
213
|
-
*
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
201
|
+
### `dispatcher.dispatch(options, handler)`
|
|
202
|
+
|
|
203
|
+
<!-- YAML
|
|
204
|
+
added: v4.0.0
|
|
205
|
+
-->
|
|
206
|
+
|
|
207
|
+
* `options` {Object}
|
|
208
|
+
* `origin` {string|URL} (optional) The origin to dispatch the request to.
|
|
209
|
+
* `path` {string} The request path.
|
|
210
|
+
* `method` {string} The HTTP method, e.g. `'GET'` or `'POST'`.
|
|
211
|
+
* `body` {string|Buffer|Uint8Array|Readable|FormData|null} The request body.
|
|
212
|
+
**Default:** `null`.
|
|
213
|
+
* `headers` {UndiciHeaders} Request headers. **Default:** `null`.
|
|
214
|
+
* `query` {Object} Query string parameters embedded into the request URL. Both
|
|
215
|
+
keys and values are encoded with `encodeURIComponent`. **Default:** `null`.
|
|
216
|
+
* `idempotent` {boolean} Whether the request can be safely retried. If `false`,
|
|
217
|
+
the request is not sent until all preceding requests in the pipeline have
|
|
218
|
+
completed. **Default:** `true` when `method` is `'HEAD'` or `'GET'`.
|
|
219
|
+
* `blocking` {boolean} Whether the response is expected to block the pipeline.
|
|
220
|
+
When `true`, further pipelining on the same connection is avoided until
|
|
221
|
+
headers have been received. **Default:** `method !== 'HEAD'`.
|
|
222
|
+
* `typeOfService` {number|null} The IP Type of Service (ToS) value for the
|
|
223
|
+
request socket. Must be an integer between `0` and `255`. **Default:** `0`.
|
|
224
|
+
* `upgrade` {boolean|string|null} Upgrades the request, e.g. to `'Websocket'`.
|
|
225
|
+
**Default:** `null`, or the request method when it is `'CONNECT'`.
|
|
226
|
+
* `headersTimeout` {number|null} The time, in milliseconds, the parser waits to
|
|
227
|
+
receive the complete HTTP headers. Defaults to 300 seconds.
|
|
228
|
+
* `bodyTimeout` {number|null} The time, in milliseconds, after which the request
|
|
229
|
+
times out while receiving body data. Monitors the time between body chunks.
|
|
230
|
+
Use `0` to disable it entirely. Defaults to 300 seconds.
|
|
231
|
+
* `reset` {boolean} Whether the request should establish a keep-alive
|
|
232
|
+
connection. **Default:** `false`.
|
|
233
|
+
* `expectContinue` {boolean} For HTTP/2, appends the `expect: 100-continue`
|
|
234
|
+
header and halts the request body until a `100 Continue` is received from the
|
|
235
|
+
remote server. **Default:** `false`.
|
|
236
|
+
* `handler` {Object} A set of callbacks invoked over the request lifecycle.
|
|
237
|
+
* `onRequestStart` {Function} Invoked before the request is dispatched on the
|
|
238
|
+
socket. May be invoked multiple times when the request is retried.
|
|
239
|
+
* `controller` {DispatchController}
|
|
240
|
+
* `context` {any}
|
|
241
|
+
* `onRequestUpgrade` {Function} (optional) Invoked when the request is upgraded.
|
|
242
|
+
Required when `options.upgrade` is set or `options.method` is `'CONNECT'`.
|
|
243
|
+
* `controller` {DispatchController}
|
|
244
|
+
* `statusCode` {number}
|
|
245
|
+
* `headers` {Object}
|
|
246
|
+
* `socket` {Duplex}
|
|
247
|
+
* `onResponseStart` {Function} (optional) Invoked when the status code and
|
|
248
|
+
headers have been received. May be invoked multiple times for 1xx
|
|
249
|
+
informational responses. Not used for `upgrade` requests.
|
|
250
|
+
* `controller` {DispatchController}
|
|
251
|
+
* `statusCode` {number}
|
|
252
|
+
* `headers` {Object}
|
|
253
|
+
* `statusMessage` {string} (optional)
|
|
254
|
+
* `onResponseData` {Function} (optional) Invoked when response payload data is
|
|
255
|
+
received. Not used for `upgrade` requests.
|
|
256
|
+
* `controller` {DispatchController}
|
|
257
|
+
* `chunk` {Buffer}
|
|
258
|
+
* `onResponseEnd` {Function} (optional) Invoked when the response payload and
|
|
259
|
+
trailers have been received and the request has completed. Not used for
|
|
260
|
+
`upgrade` requests.
|
|
261
|
+
* `controller` {DispatchController}
|
|
262
|
+
* `trailers` {Object}
|
|
263
|
+
* `onResponseError` {Function} (optional) Invoked when an error occurs. Must not
|
|
264
|
+
throw.
|
|
265
|
+
* `controller` {DispatchController}
|
|
266
|
+
* `error` {Error}
|
|
267
|
+
* `onResponseStarted` {Function} (optional) Invoked when the response is
|
|
268
|
+
received, before headers have been read.
|
|
269
|
+
* `onBodySent` {Function} (optional) Invoked when a chunk of the request body is
|
|
270
|
+
sent. May be invoked multiple times for chunked requests.
|
|
271
|
+
* `chunk` {Buffer}
|
|
272
|
+
* `onRequestSent` {Function} (optional) Invoked after the request body is fully
|
|
273
|
+
sent.
|
|
274
|
+
* Returns: {boolean} `false` when the dispatcher is busy and further `dispatch()`
|
|
275
|
+
calls will make no progress until the [`'drain'`][] event is emitted.
|
|
276
|
+
|
|
277
|
+
This is the low-level API on which all the higher-level methods are built. This
|
|
278
|
+
API is expected to evolve through semver-major versions and is less stable than
|
|
279
|
+
the higher-level methods. It is primarily intended for library developers who
|
|
280
|
+
implement higher-level APIs on top of it.
|
|
281
|
+
|
|
282
|
+
The `controller` passed to each handler is a `DispatchController` with the
|
|
283
|
+
following shape:
|
|
284
|
+
|
|
285
|
+
* `aborted` {boolean} Whether the request has been aborted.
|
|
286
|
+
* `paused` {boolean} Whether the request is paused.
|
|
287
|
+
* `reason` {Error|null} The reason the request was aborted, if any.
|
|
288
|
+
* `rawHeaders` {Buffer[]|string[]|Object|null} The raw response headers,
|
|
289
|
+
preserving duplicates and casing.
|
|
290
|
+
* `rawTrailers` {Buffer[]|string[]|Object|null} The raw response trailers.
|
|
291
|
+
* `abort(reason)` Aborts the request with the given `reason`.
|
|
292
|
+
* `pause()` Pauses the response stream.
|
|
293
|
+
* `resume()` Resumes the response stream.
|
|
294
|
+
|
|
295
|
+
Call `controller.pause()` and `controller.resume()` to apply backpressure rather
|
|
296
|
+
than returning `false` from a handler.
|
|
297
|
+
|
|
298
|
+
```mjs
|
|
299
|
+
import { createServer } from 'node:http'
|
|
255
300
|
import { Client } from 'undici'
|
|
256
|
-
import { once } from 'events'
|
|
301
|
+
import { once } from 'node:events'
|
|
257
302
|
|
|
258
303
|
const server = createServer((request, response) => {
|
|
259
304
|
response.end('Hello, World!')
|
|
@@ -268,112 +313,7 @@ const data = []
|
|
|
268
313
|
client.dispatch({
|
|
269
314
|
path: '/',
|
|
270
315
|
method: 'GET',
|
|
271
|
-
headers: {
|
|
272
|
-
'x-foo': 'bar'
|
|
273
|
-
}
|
|
274
|
-
}, {
|
|
275
|
-
onRequestStart: () => {
|
|
276
|
-
console.log('Connected!')
|
|
277
|
-
},
|
|
278
|
-
onResponseError: (_controller, error) => {
|
|
279
|
-
console.error(error)
|
|
280
|
-
},
|
|
281
|
-
onResponseStart: (_controller, statusCode, headers) => {
|
|
282
|
-
console.log(`onResponseStart | statusCode: ${statusCode} | headers: ${JSON.stringify(headers)}`)
|
|
283
|
-
},
|
|
284
|
-
onResponseData: (_controller, chunk) => {
|
|
285
|
-
console.log('onResponseData: chunk received')
|
|
286
|
-
data.push(chunk)
|
|
287
|
-
},
|
|
288
|
-
onResponseEnd: (_controller, trailers) => {
|
|
289
|
-
console.log(`onResponseEnd | trailers: ${JSON.stringify(trailers)}`)
|
|
290
|
-
const res = Buffer.concat(data).toString('utf8')
|
|
291
|
-
console.log(`Data: ${res}`)
|
|
292
|
-
client.close()
|
|
293
|
-
server.close()
|
|
294
|
-
}
|
|
295
|
-
})
|
|
296
|
-
```
|
|
297
|
-
|
|
298
|
-
#### Example 2 - Dispatch Upgrade Request
|
|
299
|
-
|
|
300
|
-
```js
|
|
301
|
-
import { createServer } from 'http'
|
|
302
|
-
import { Client } from 'undici'
|
|
303
|
-
import { once } from 'events'
|
|
304
|
-
|
|
305
|
-
const server = createServer((request, response) => {
|
|
306
|
-
response.end()
|
|
307
|
-
}).listen()
|
|
308
|
-
|
|
309
|
-
await once(server, 'listening')
|
|
310
|
-
|
|
311
|
-
server.on('upgrade', (request, socket, head) => {
|
|
312
|
-
console.log('Node.js Server - upgrade event')
|
|
313
|
-
socket.write('HTTP/1.1 101 Web Socket Protocol Handshake\r\n')
|
|
314
|
-
socket.write('Upgrade: WebSocket\r\n')
|
|
315
|
-
socket.write('Connection: Upgrade\r\n')
|
|
316
|
-
socket.write('\r\n')
|
|
317
|
-
socket.end()
|
|
318
|
-
})
|
|
319
|
-
|
|
320
|
-
const client = new Client(`http://localhost:${server.address().port}`)
|
|
321
|
-
|
|
322
|
-
client.dispatch({
|
|
323
|
-
path: '/',
|
|
324
|
-
method: 'GET',
|
|
325
|
-
upgrade: 'websocket'
|
|
326
|
-
}, {
|
|
327
|
-
onRequestStart: () => {
|
|
328
|
-
console.log('Undici Client - onRequestStart')
|
|
329
|
-
},
|
|
330
|
-
onResponseError: () => {
|
|
331
|
-
console.log('onResponseError') // shouldn't print
|
|
332
|
-
},
|
|
333
|
-
onRequestUpgrade: (_controller, statusCode, headers, socket) => {
|
|
334
|
-
console.log('Undici Client - onRequestUpgrade')
|
|
335
|
-
console.log(`onRequestUpgrade Headers: ${JSON.stringify(headers)}`)
|
|
336
|
-
socket.on('data', buffer => {
|
|
337
|
-
console.log(buffer.toString('utf8'))
|
|
338
|
-
})
|
|
339
|
-
socket.on('end', () => {
|
|
340
|
-
client.close()
|
|
341
|
-
server.close()
|
|
342
|
-
})
|
|
343
|
-
socket.end()
|
|
344
|
-
}
|
|
345
|
-
})
|
|
346
|
-
```
|
|
347
|
-
|
|
348
|
-
#### Example 3 - Dispatch POST request
|
|
349
|
-
|
|
350
|
-
```js
|
|
351
|
-
import { createServer } from 'http'
|
|
352
|
-
import { Client } from 'undici'
|
|
353
|
-
import { once } from 'events'
|
|
354
|
-
|
|
355
|
-
const server = createServer((request, response) => {
|
|
356
|
-
request.on('data', (data) => {
|
|
357
|
-
console.log(`Request Data: ${data.toString('utf8')}`)
|
|
358
|
-
const body = JSON.parse(data)
|
|
359
|
-
body.message = 'World'
|
|
360
|
-
response.end(JSON.stringify(body))
|
|
361
|
-
})
|
|
362
|
-
}).listen()
|
|
363
|
-
|
|
364
|
-
await once(server, 'listening')
|
|
365
|
-
|
|
366
|
-
const client = new Client(`http://localhost:${server.address().port}`)
|
|
367
|
-
|
|
368
|
-
const data = []
|
|
369
|
-
|
|
370
|
-
client.dispatch({
|
|
371
|
-
path: '/',
|
|
372
|
-
method: 'POST',
|
|
373
|
-
headers: {
|
|
374
|
-
'content-type': 'application/json'
|
|
375
|
-
},
|
|
376
|
-
body: JSON.stringify({ message: 'Hello' })
|
|
316
|
+
headers: { 'x-foo': 'bar' }
|
|
377
317
|
}, {
|
|
378
318
|
onRequestStart: () => {
|
|
379
319
|
console.log('Connected!')
|
|
@@ -382,55 +322,52 @@ client.dispatch({
|
|
|
382
322
|
console.error(error)
|
|
383
323
|
},
|
|
384
324
|
onResponseStart: (_controller, statusCode, headers) => {
|
|
385
|
-
console.log(`
|
|
325
|
+
console.log(`statusCode: ${statusCode} | headers: ${JSON.stringify(headers)}`)
|
|
386
326
|
},
|
|
387
327
|
onResponseData: (_controller, chunk) => {
|
|
388
|
-
console.log('onResponseData: chunk received')
|
|
389
328
|
data.push(chunk)
|
|
390
329
|
},
|
|
391
330
|
onResponseEnd: (_controller, trailers) => {
|
|
392
|
-
console.log(`
|
|
393
|
-
|
|
394
|
-
console.log(`Response Data: ${res}`)
|
|
331
|
+
console.log(`trailers: ${JSON.stringify(trailers)}`)
|
|
332
|
+
console.log(`Data: ${Buffer.concat(data).toString('utf8')}`)
|
|
395
333
|
client.close()
|
|
396
334
|
server.close()
|
|
397
335
|
}
|
|
398
336
|
})
|
|
399
337
|
```
|
|
400
338
|
|
|
401
|
-
### `
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
*
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
*
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
import {
|
|
431
|
-
import { createServer } from 'http'
|
|
339
|
+
### `dispatcher.pipeline(options, handler)`
|
|
340
|
+
|
|
341
|
+
<!-- YAML
|
|
342
|
+
added: v4.0.0
|
|
343
|
+
-->
|
|
344
|
+
|
|
345
|
+
* `options` {Object} Accepts every field of [`dispatcher.request()`][]'s
|
|
346
|
+
`options`, plus:
|
|
347
|
+
* `objectMode` {boolean} Set to `true` when `handler` returns an object stream.
|
|
348
|
+
**Default:** `false`.
|
|
349
|
+
* `handler` {Function} Returns a `Readable` from which the result is read.
|
|
350
|
+
* `data` {Object}
|
|
351
|
+
* `statusCode` {number}
|
|
352
|
+
* `headers` {Object} The response headers.
|
|
353
|
+
* `opaque` {any} The `opaque` value passed in `options`.
|
|
354
|
+
* `body` {Readable} The response body.
|
|
355
|
+
* `context` {Object}
|
|
356
|
+
* Returns: {Readable}
|
|
357
|
+
* Returns: {Duplex} A duplex stream that writes to the request and reads from the
|
|
358
|
+
response.
|
|
359
|
+
|
|
360
|
+
For easy use with [`stream.pipeline()`][]. The `handler` should return a
|
|
361
|
+
`Readable` from which the result is read. Usually it returns the `body` directly,
|
|
362
|
+
unless a transformation is needed based on, for example, `headers` or
|
|
363
|
+
`statusCode`. The `handler` should validate the response and save any required
|
|
364
|
+
state; if there is an error, it should be thrown.
|
|
365
|
+
|
|
366
|
+
```mjs
|
|
367
|
+
import { Readable, Writable, PassThrough, pipeline } from 'node:stream'
|
|
368
|
+
import { createServer } from 'node:http'
|
|
432
369
|
import { Client } from 'undici'
|
|
433
|
-
import { once } from 'events'
|
|
370
|
+
import { once } from 'node:events'
|
|
434
371
|
|
|
435
372
|
const server = createServer((request, response) => {
|
|
436
373
|
request.pipe(response)
|
|
@@ -449,10 +386,7 @@ pipeline(
|
|
|
449
386
|
this.push(null)
|
|
450
387
|
}
|
|
451
388
|
}),
|
|
452
|
-
client.pipeline({
|
|
453
|
-
path: '/',
|
|
454
|
-
method: 'GET'
|
|
455
|
-
}, ({ statusCode, headers, body }) => {
|
|
389
|
+
client.pipeline({ path: '/', method: 'GET' }, ({ statusCode, headers, body }) => {
|
|
456
390
|
console.log(`response received ${statusCode}`)
|
|
457
391
|
console.log('headers', headers)
|
|
458
392
|
return pipeline(body, new PassThrough(), () => {})
|
|
@@ -471,79 +405,72 @@ pipeline(
|
|
|
471
405
|
if (error) {
|
|
472
406
|
console.error(error)
|
|
473
407
|
}
|
|
474
|
-
|
|
475
408
|
client.close()
|
|
476
409
|
server.close()
|
|
477
410
|
}
|
|
478
411
|
)
|
|
479
412
|
```
|
|
480
413
|
|
|
481
|
-
### `
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
*
|
|
498
|
-
*
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
*
|
|
507
|
-
*
|
|
508
|
-
*
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
*
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
`
|
|
534
|
-
|
|
535
|
-
`
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
Note that body will still be a `Readable` even if it is empty, but attempting to deserialize it with `json()` will result in an exception. Recommended way to ensure there is a body to deserialize is to check if status code is not 204, and `content-type` header starts with `application/json`.
|
|
540
|
-
|
|
541
|
-
#### Example 1 - Basic GET Request
|
|
542
|
-
|
|
543
|
-
```js
|
|
544
|
-
import { createServer } from 'http'
|
|
414
|
+
### `dispatcher.request(options[, callback])`
|
|
415
|
+
|
|
416
|
+
<!-- YAML
|
|
417
|
+
added: v4.0.0
|
|
418
|
+
-->
|
|
419
|
+
|
|
420
|
+
* `options` {Object} Accepts every field of [`dispatcher.dispatch()`][]'s
|
|
421
|
+
`options`, plus:
|
|
422
|
+
* `opaque` {any} A value passed through to the returned response data.
|
|
423
|
+
**Default:** `null`.
|
|
424
|
+
* `signal` {AbortSignal|EventEmitter|null} A signal used to abort the request.
|
|
425
|
+
**Default:** `null`.
|
|
426
|
+
* `onInfo` {Function|null} Invoked for each informational (1xx) response
|
|
427
|
+
received. **Default:** `null`.
|
|
428
|
+
* `info` {Object}
|
|
429
|
+
* `statusCode` {number}
|
|
430
|
+
* `headers` {Object}
|
|
431
|
+
* `responseHeaders` {string|null} Set to `'raw'` to return the response headers
|
|
432
|
+
as a raw array. **Default:** `null`.
|
|
433
|
+
* `highWaterMark` {number} The high water mark of the response body stream.
|
|
434
|
+
**Default:** `65536` (64 KiB).
|
|
435
|
+
* `callback` {Function} (optional) Invoked when the response is received.
|
|
436
|
+
* `error` {Error|null}
|
|
437
|
+
* `data` {Object}
|
|
438
|
+
* `statusCode` {number}
|
|
439
|
+
* `statusText` {string} The status message, e.g. `'OK'` or `'Not Found'`.
|
|
440
|
+
* `headers` {Object} The response headers. All keys are lower-cased.
|
|
441
|
+
* `body` {Readable} The response body, which also implements the
|
|
442
|
+
[body mixin][] from the Fetch Standard.
|
|
443
|
+
* `trailers` {Object} Starts out empty and is mutated to contain the trailers
|
|
444
|
+
after `body` emits `'end'`.
|
|
445
|
+
* `opaque` {any} The `opaque` value passed in `options`.
|
|
446
|
+
* `context` {Object}
|
|
447
|
+
* Returns: {Promise} A `Promise` is returned only when `callback` is omitted. It
|
|
448
|
+
resolves with the `data` object described above.
|
|
449
|
+
|
|
450
|
+
Performs an HTTP request. `options.method` must not be `'CONNECT'`.
|
|
451
|
+
|
|
452
|
+
Non-idempotent requests are not pipelined, to avoid indirect failures. Idempotent
|
|
453
|
+
requests are automatically retried if they fail due to an indirect failure from
|
|
454
|
+
the request at the head of the pipeline; this does not apply to idempotent
|
|
455
|
+
requests with a stream request body. All response bodies must always be fully
|
|
456
|
+
consumed or destroyed.
|
|
457
|
+
|
|
458
|
+
The `body` exposes the following [body mixin][] methods and properties:
|
|
459
|
+
[`.arrayBuffer()`][], [`.blob()`][], [`.bytes()`][], [`.json()`][],
|
|
460
|
+
[`.text()`][], `body`, and `bodyUsed`. A body cannot be consumed twice; for
|
|
461
|
+
example, calling `text()` after `json()` throws a `TypeError`. The body also
|
|
462
|
+
provides `dump({ limit })`, which discards up to `limit` bytes (default
|
|
463
|
+
`131072`) without destroying the socket.
|
|
464
|
+
|
|
465
|
+
The body is always a `Readable`, even when empty. Deserializing an empty body
|
|
466
|
+
with `json()` throws. To guard against this, verify the status code is not `204`
|
|
467
|
+
and the `content-type` header starts with `application/json` before calling
|
|
468
|
+
`json()`.
|
|
469
|
+
|
|
470
|
+
```mjs
|
|
471
|
+
import { createServer } from 'node:http'
|
|
545
472
|
import { Client } from 'undici'
|
|
546
|
-
import { once } from 'events'
|
|
473
|
+
import { once } from 'node:events'
|
|
547
474
|
|
|
548
475
|
const server = createServer((request, response) => {
|
|
549
476
|
response.end('Hello, World!')
|
|
@@ -574,107 +501,28 @@ try {
|
|
|
574
501
|
}
|
|
575
502
|
```
|
|
576
503
|
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
> Node.js v15+ is required to run this example
|
|
504
|
+
A request can be aborted with an `AbortSignal`, with any `EventEmitter` that
|
|
505
|
+
emits an `'abort'` event, or by destroying the response body:
|
|
580
506
|
|
|
581
|
-
```
|
|
582
|
-
import { createServer } from 'http'
|
|
507
|
+
```mjs
|
|
583
508
|
import { Client } from 'undici'
|
|
584
|
-
import { once } from 'events'
|
|
585
|
-
|
|
586
|
-
const server = createServer((request, response) => {
|
|
587
|
-
response.end('Hello, World!')
|
|
588
|
-
}).listen()
|
|
589
509
|
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
const client = new Client(`http://localhost:${server.address().port}`)
|
|
510
|
+
const client = new Client('http://localhost:3000')
|
|
593
511
|
const abortController = new AbortController()
|
|
594
512
|
|
|
595
513
|
try {
|
|
596
|
-
client.request({
|
|
597
|
-
path: '/',
|
|
598
|
-
method: 'GET',
|
|
599
|
-
signal: abortController.signal
|
|
600
|
-
})
|
|
514
|
+
client.request({ path: '/', method: 'GET', signal: abortController.signal })
|
|
601
515
|
} catch (error) {
|
|
602
|
-
console.error(error) //
|
|
603
|
-
client.close()
|
|
604
|
-
server.close()
|
|
516
|
+
console.error(error) // RequestAbortedError
|
|
605
517
|
}
|
|
606
518
|
|
|
607
519
|
abortController.abort()
|
|
608
520
|
```
|
|
609
521
|
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
```js
|
|
613
|
-
import { createServer } from 'http'
|
|
614
|
-
import { Client } from 'undici'
|
|
615
|
-
import EventEmitter, { once } from 'events'
|
|
616
|
-
|
|
617
|
-
const server = createServer((request, response) => {
|
|
618
|
-
response.end('Hello, World!')
|
|
619
|
-
}).listen()
|
|
620
|
-
|
|
621
|
-
await once(server, 'listening')
|
|
622
|
-
|
|
623
|
-
const client = new Client(`http://localhost:${server.address().port}`)
|
|
624
|
-
const ee = new EventEmitter()
|
|
625
|
-
|
|
626
|
-
try {
|
|
627
|
-
client.request({
|
|
628
|
-
path: '/',
|
|
629
|
-
method: 'GET',
|
|
630
|
-
signal: ee
|
|
631
|
-
})
|
|
632
|
-
} catch (error) {
|
|
633
|
-
console.error(error) // should print an RequestAbortedError
|
|
634
|
-
client.close()
|
|
635
|
-
server.close()
|
|
636
|
-
}
|
|
637
|
-
|
|
638
|
-
ee.emit('abort')
|
|
639
|
-
```
|
|
640
|
-
|
|
641
|
-
Destroying the request or response body will have the same effect.
|
|
522
|
+
When the response body is conditionally read, always fully consume it otherwise:
|
|
642
523
|
|
|
643
|
-
```
|
|
644
|
-
|
|
645
|
-
import { Client } from 'undici'
|
|
646
|
-
import { once } from 'events'
|
|
647
|
-
|
|
648
|
-
const server = createServer((request, response) => {
|
|
649
|
-
response.end('Hello, World!')
|
|
650
|
-
}).listen()
|
|
651
|
-
|
|
652
|
-
await once(server, 'listening')
|
|
653
|
-
|
|
654
|
-
const client = new Client(`http://localhost:${server.address().port}`)
|
|
655
|
-
|
|
656
|
-
try {
|
|
657
|
-
const { body } = await client.request({
|
|
658
|
-
path: '/',
|
|
659
|
-
method: 'GET'
|
|
660
|
-
})
|
|
661
|
-
body.destroy()
|
|
662
|
-
} catch (error) {
|
|
663
|
-
console.error(error) // should print an RequestAbortedError
|
|
664
|
-
client.close()
|
|
665
|
-
server.close()
|
|
666
|
-
}
|
|
667
|
-
```
|
|
668
|
-
|
|
669
|
-
#### Example 3 - Conditionally reading the body
|
|
670
|
-
|
|
671
|
-
Remember to fully consume the body even in the case when it is not read.
|
|
672
|
-
|
|
673
|
-
```js
|
|
674
|
-
const { body, statusCode } = await client.request({
|
|
675
|
-
path: '/',
|
|
676
|
-
method: 'GET'
|
|
677
|
-
})
|
|
524
|
+
```mjs
|
|
525
|
+
const { body, statusCode } = await client.request({ path: '/', method: 'GET' })
|
|
678
526
|
|
|
679
527
|
if (statusCode === 200) {
|
|
680
528
|
return await body.arrayBuffer()
|
|
@@ -685,40 +533,42 @@ await body.dump()
|
|
|
685
533
|
return null
|
|
686
534
|
```
|
|
687
535
|
|
|
688
|
-
### `
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
*
|
|
697
|
-
*
|
|
698
|
-
*
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
*
|
|
705
|
-
*
|
|
706
|
-
*
|
|
707
|
-
*
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
536
|
+
### `dispatcher.stream(options, factory[, callback])`
|
|
537
|
+
|
|
538
|
+
<!-- YAML
|
|
539
|
+
added: v4.0.0
|
|
540
|
+
-->
|
|
541
|
+
|
|
542
|
+
* `options` {Object} Accepts the same fields as [`dispatcher.request()`][]'s
|
|
543
|
+
`options`.
|
|
544
|
+
* `factory` {Function} Returns the `Writable` to which the response is written.
|
|
545
|
+
* `data` {Object}
|
|
546
|
+
* `statusCode` {number}
|
|
547
|
+
* `headers` {Object} The response headers.
|
|
548
|
+
* `opaque` {any} The `opaque` value passed in `options`.
|
|
549
|
+
* `context` {Object}
|
|
550
|
+
* Returns: {Writable}
|
|
551
|
+
* `callback` {Function} (optional) Invoked when the request has completed.
|
|
552
|
+
* `error` {Error|null}
|
|
553
|
+
* `data` {Object}
|
|
554
|
+
* `opaque` {any} The `opaque` value passed in `options`.
|
|
555
|
+
* `trailers` {Object}
|
|
556
|
+
* Returns: {Promise} A `Promise` is returned only when `callback` is omitted. It
|
|
557
|
+
resolves with the `data` object described above.
|
|
558
|
+
|
|
559
|
+
A faster version of [`dispatcher.request()`][]. The `factory` returns a
|
|
560
|
+
[`Writable`][] to which the response is written, which avoids creating an
|
|
561
|
+
intermediate [`Readable`][] when the caller intends to pipe the response body
|
|
562
|
+
directly to a `Writable`.
|
|
563
|
+
|
|
564
|
+
Use `options.opaque` to avoid creating a closure for the `factory` method. This
|
|
565
|
+
pattern works well with Node.js web frameworks such as [Fastify][].
|
|
566
|
+
|
|
567
|
+
```mjs
|
|
568
|
+
import { createServer } from 'node:http'
|
|
719
569
|
import { Client } from 'undici'
|
|
720
|
-
import { once } from 'events'
|
|
721
|
-
import { Writable } from 'stream'
|
|
570
|
+
import { once } from 'node:events'
|
|
571
|
+
import { Writable } from 'node:stream'
|
|
722
572
|
|
|
723
573
|
const server = createServer((request, response) => {
|
|
724
574
|
response.end('Hello, World!')
|
|
@@ -755,95 +605,38 @@ try {
|
|
|
755
605
|
}
|
|
756
606
|
```
|
|
757
607
|
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
}
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
}, ({ opaque }) => opaque.raw)
|
|
789
|
-
}
|
|
790
|
-
})
|
|
791
|
-
|
|
792
|
-
await fastifyServer.listen()
|
|
793
|
-
|
|
794
|
-
console.log('Fastify Server listening')
|
|
795
|
-
|
|
796
|
-
const fastifyServerUndiciClient = new Client(`http://localhost:${fastifyServer.server.address().port}`)
|
|
797
|
-
|
|
798
|
-
try {
|
|
799
|
-
const { statusCode, body } = await fastifyServerUndiciClient.request({
|
|
800
|
-
path: '/',
|
|
801
|
-
method: 'GET'
|
|
802
|
-
})
|
|
803
|
-
|
|
804
|
-
console.log(`response received ${statusCode}`)
|
|
805
|
-
body.setEncoding('utf8')
|
|
806
|
-
body.on('data', console.log)
|
|
807
|
-
|
|
808
|
-
nodeServerUndiciClient.close()
|
|
809
|
-
fastifyServerUndiciClient.close()
|
|
810
|
-
fastifyServer.close()
|
|
811
|
-
nodeServer.close()
|
|
812
|
-
} catch (error) { }
|
|
813
|
-
```
|
|
814
|
-
|
|
815
|
-
### `Dispatcher.upgrade(options[, callback])`
|
|
816
|
-
|
|
817
|
-
Upgrade to a different protocol. Visit [MDN - HTTP - Protocol upgrade mechanism](https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism) for more details.
|
|
818
|
-
|
|
819
|
-
Arguments:
|
|
820
|
-
|
|
821
|
-
* **options** `UpgradeOptions`
|
|
822
|
-
|
|
823
|
-
* **callback** `(error: Error | null, data: UpgradeData) => void` (optional)
|
|
824
|
-
|
|
825
|
-
Returns: `void | Promise<UpgradeData>` - Only returns a `Promise` if no `callback` argument was passed
|
|
826
|
-
|
|
827
|
-
#### Parameter: `UpgradeOptions`
|
|
828
|
-
|
|
829
|
-
* **path** `string`
|
|
830
|
-
* **method** `string` (optional) - Default: `'GET'`
|
|
831
|
-
* **headers** `UndiciHeaders` (optional) - Default: `null`
|
|
832
|
-
* **protocol** `string` (optional) - Default: `'Websocket'` - A string of comma separated protocols, in descending preference order.
|
|
833
|
-
* **signal** `AbortSignal | EventEmitter | null` (optional) - Default: `null`
|
|
834
|
-
|
|
835
|
-
#### Parameter: `UpgradeData`
|
|
836
|
-
|
|
837
|
-
* **headers** `http.IncomingHeaders`
|
|
838
|
-
* **socket** `stream.Duplex`
|
|
839
|
-
* **opaque** `unknown`
|
|
840
|
-
|
|
841
|
-
#### Example 1 - Basic Upgrade Request
|
|
842
|
-
|
|
843
|
-
```js
|
|
844
|
-
import { createServer } from 'http'
|
|
608
|
+
### `dispatcher.upgrade(options[, callback])`
|
|
609
|
+
|
|
610
|
+
<!-- YAML
|
|
611
|
+
added: v4.0.0
|
|
612
|
+
-->
|
|
613
|
+
|
|
614
|
+
* `options` {Object}
|
|
615
|
+
* `path` {string} The request path.
|
|
616
|
+
* `method` {string} The HTTP method. **Default:** `'GET'`.
|
|
617
|
+
* `headers` {UndiciHeaders} Request headers. **Default:** `null`.
|
|
618
|
+
* `protocol` {string} A comma-separated list of protocols, in descending
|
|
619
|
+
preference order. **Default:** `'Websocket'`.
|
|
620
|
+
* `signal` {AbortSignal|EventEmitter|null} A signal used to abort the request.
|
|
621
|
+
**Default:** `null`.
|
|
622
|
+
* `responseHeaders` {string|null} Set to `'raw'` to return the response headers
|
|
623
|
+
as a raw array. **Default:** `null`.
|
|
624
|
+
* `callback` {Function} (optional) Invoked when the upgrade completes.
|
|
625
|
+
* `error` {Error|null}
|
|
626
|
+
* `data` {Object}
|
|
627
|
+
* `headers` {Object} The response headers.
|
|
628
|
+
* `socket` {Duplex} The upgraded socket.
|
|
629
|
+
* `opaque` {any}
|
|
630
|
+
* Returns: {Promise} A `Promise` is returned only when `callback` is omitted. It
|
|
631
|
+
resolves with the `data` object described above.
|
|
632
|
+
|
|
633
|
+
Upgrades to a different protocol. See
|
|
634
|
+
[MDN: Protocol upgrade mechanism][] for more details.
|
|
635
|
+
|
|
636
|
+
```mjs
|
|
637
|
+
import { createServer } from 'node:http'
|
|
845
638
|
import { Client } from 'undici'
|
|
846
|
-
import { once } from 'events'
|
|
639
|
+
import { once } from 'node:events'
|
|
847
640
|
|
|
848
641
|
const server = createServer((request, response) => {
|
|
849
642
|
response.statusCode = 101
|
|
@@ -857,9 +650,7 @@ await once(server, 'listening')
|
|
|
857
650
|
const client = new Client(`http://localhost:${server.address().port}`)
|
|
858
651
|
|
|
859
652
|
try {
|
|
860
|
-
const { headers, socket } = await client.upgrade({
|
|
861
|
-
path: '/',
|
|
862
|
-
})
|
|
653
|
+
const { headers, socket } = await client.upgrade({ path: '/' })
|
|
863
654
|
socket.on('end', () => {
|
|
864
655
|
console.log(`upgrade: ${headers.upgrade}`) // upgrade: Websocket
|
|
865
656
|
client.close()
|
|
@@ -873,109 +664,62 @@ try {
|
|
|
873
664
|
}
|
|
874
665
|
```
|
|
875
666
|
|
|
876
|
-
### `
|
|
667
|
+
### `dispatcher.compose(interceptors[, interceptor])`
|
|
877
668
|
|
|
878
|
-
|
|
669
|
+
<!-- YAML
|
|
670
|
+
added: v6.9.0
|
|
671
|
+
-->
|
|
879
672
|
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
> ```
|
|
887
|
-
> compose([interceptor1, interceptor2, interceptor3])
|
|
888
|
-
>
|
|
889
|
-
> Request Flow:
|
|
890
|
-
> ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
|
|
891
|
-
> │ Request │───▶│interceptor3 │───▶│interceptor2 │───▶│interceptor1 │───▶│ dispatcher │
|
|
892
|
-
> └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ │ .dispatch │
|
|
893
|
-
> ▲ ▲ ▲ └─────────────┘
|
|
894
|
-
> │ │ │ ▲
|
|
895
|
-
> (called first) (called second) (called last) │
|
|
896
|
-
> │
|
|
897
|
-
> ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
|
|
898
|
-
> │ Response │◀───│interceptor3 │◀───│interceptor2 │◀───│interceptor1 │◀─────────┘
|
|
899
|
-
> └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘
|
|
900
|
-
>
|
|
901
|
-
> The interceptors are composed in reverse order due to function composition.
|
|
902
|
-
> ```
|
|
673
|
+
* `interceptors` {Array|Function} An array of interceptor functions, or the first
|
|
674
|
+
of several interceptors passed as separate arguments.
|
|
675
|
+
* `...interceptor` {Function} Additional interceptors when passed as separate
|
|
676
|
+
arguments.
|
|
677
|
+
* Returns: {Dispatcher} A new dispatcher that applies the interceptors over the
|
|
678
|
+
current dispatcher's [`dispatch()`][] method.
|
|
903
679
|
|
|
904
|
-
|
|
680
|
+
Composes a new dispatcher from the current dispatcher and the given
|
|
681
|
+
interceptors. Each interceptor is a function that takes a `dispatch` method and
|
|
682
|
+
returns a `dispatch`-like function with the same signature
|
|
683
|
+
(`(options, handler)`).
|
|
905
684
|
|
|
906
|
-
|
|
685
|
+
The order of the interceptors matters: the last interceptor in the chain is the
|
|
686
|
+
first to be called. Forking the chain of interceptors can lead to unexpected
|
|
687
|
+
results.
|
|
907
688
|
|
|
908
|
-
|
|
689
|
+
```text
|
|
690
|
+
compose([interceptor1, interceptor2, interceptor3])
|
|
909
691
|
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
#### Example 1 - Basic Compose
|
|
692
|
+
Request flow:
|
|
693
|
+
Request -> interceptor3 -> interceptor2 -> interceptor1 -> dispatcher.dispatch
|
|
694
|
+
(called first) (called second) (called last)
|
|
695
|
+
```
|
|
915
696
|
|
|
916
|
-
```
|
|
917
|
-
|
|
697
|
+
```mjs
|
|
698
|
+
import { Client, RedirectHandler } from 'undici'
|
|
918
699
|
|
|
919
700
|
const redirectInterceptor = dispatch => {
|
|
920
|
-
|
|
921
|
-
|
|
701
|
+
return (opts, handler) => {
|
|
702
|
+
const { maxRedirections } = opts
|
|
922
703
|
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
}
|
|
926
|
-
|
|
927
|
-
const redirectHandler = new RedirectHandler(
|
|
928
|
-
dispatch,
|
|
929
|
-
maxRedirections,
|
|
930
|
-
opts,
|
|
931
|
-
handler
|
|
932
|
-
)
|
|
933
|
-
opts = { ...opts, maxRedirections: 0 } // Stop sub dispatcher from also redirecting.
|
|
934
|
-
return dispatch(opts, redirectHandler)
|
|
704
|
+
if (!maxRedirections) {
|
|
705
|
+
return dispatch(opts, handler)
|
|
935
706
|
}
|
|
707
|
+
|
|
708
|
+
const redirectHandler = new RedirectHandler(dispatch, maxRedirections, opts, handler)
|
|
709
|
+
opts = { ...opts, maxRedirections: 0 } // Stop the sub-dispatcher from also redirecting.
|
|
710
|
+
return dispatch(opts, redirectHandler)
|
|
711
|
+
}
|
|
936
712
|
}
|
|
937
713
|
|
|
938
|
-
const client = new Client('http://localhost:3000')
|
|
939
|
-
.compose(redirectInterceptor)
|
|
714
|
+
const client = new Client('http://localhost:3000').compose(redirectInterceptor)
|
|
940
715
|
|
|
941
716
|
await client.request({ path: '/', method: 'GET' })
|
|
942
717
|
```
|
|
943
718
|
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
```js
|
|
947
|
-
const { Client, RedirectHandler, RetryHandler } = require('undici')
|
|
948
|
-
|
|
949
|
-
const redirectInterceptor = dispatch => {
|
|
950
|
-
return (opts, handler) => {
|
|
951
|
-
const { maxRedirections } = opts
|
|
952
|
-
|
|
953
|
-
if (!maxRedirections) {
|
|
954
|
-
return dispatch(opts, handler)
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
const redirectHandler = new RedirectHandler(
|
|
958
|
-
dispatch,
|
|
959
|
-
maxRedirections,
|
|
960
|
-
opts,
|
|
961
|
-
handler
|
|
962
|
-
)
|
|
963
|
-
opts = { ...opts, maxRedirections: 0 }
|
|
964
|
-
return dispatch(opts, redirectHandler)
|
|
965
|
-
}
|
|
966
|
-
}
|
|
719
|
+
Interceptors can be chained by calling `compose()` repeatedly:
|
|
967
720
|
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
return dispatch(
|
|
971
|
-
opts,
|
|
972
|
-
new RetryHandler(opts, {
|
|
973
|
-
handler,
|
|
974
|
-
dispatch
|
|
975
|
-
})
|
|
976
|
-
)
|
|
977
|
-
}
|
|
978
|
-
}
|
|
721
|
+
```mjs
|
|
722
|
+
import { Client } from 'undici'
|
|
979
723
|
|
|
980
724
|
const client = new Client('http://localhost:3000')
|
|
981
725
|
.compose(redirectInterceptor)
|
|
@@ -984,452 +728,94 @@ const client = new Client('http://localhost:3000')
|
|
|
984
728
|
await client.request({ path: '/', method: 'GET' })
|
|
985
729
|
```
|
|
986
730
|
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
##### `redirect`
|
|
990
|
-
|
|
991
|
-
The `redirect` interceptor allows you to customize the way your dispatcher handles redirects.
|
|
992
|
-
|
|
993
|
-
It accepts the same arguments as the [`RedirectHandler` constructor](/docs/docs/api/RedirectHandler.md).
|
|
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
|
-
|
|
1002
|
-
**Example - Basic Redirect Interceptor**
|
|
1003
|
-
|
|
1004
|
-
```js
|
|
1005
|
-
const { Client, interceptors } = require("undici");
|
|
1006
|
-
const { redirect } = interceptors;
|
|
1007
|
-
|
|
1008
|
-
const client = new Client("http://service.example").compose(
|
|
1009
|
-
redirect({ maxRedirections: 3, throwOnMaxRedirect: true })
|
|
1010
|
-
);
|
|
1011
|
-
client.request({ path: "/" })
|
|
1012
|
-
```
|
|
1013
|
-
|
|
1014
|
-
##### `retry`
|
|
1015
|
-
|
|
1016
|
-
The `retry` interceptor allows you to customize the way your dispatcher handles retries.
|
|
1017
|
-
|
|
1018
|
-
It accepts the same arguments as the [`RetryHandler` constructor](/docs/docs/api/RetryHandler.md).
|
|
1019
|
-
|
|
1020
|
-
**Example - Basic Retry Interceptor**
|
|
1021
|
-
|
|
1022
|
-
```js
|
|
1023
|
-
const { Client, interceptors } = require("undici");
|
|
1024
|
-
const { retry } = interceptors;
|
|
1025
|
-
|
|
1026
|
-
const client = new Client("http://service.example").compose(
|
|
1027
|
-
retry({
|
|
1028
|
-
maxRetries: 3,
|
|
1029
|
-
minTimeout: 1000,
|
|
1030
|
-
maxTimeout: 10000,
|
|
1031
|
-
timeoutFactor: 2,
|
|
1032
|
-
retryAfter: true,
|
|
1033
|
-
})
|
|
1034
|
-
);
|
|
1035
|
-
```
|
|
1036
|
-
|
|
1037
|
-
##### `dump`
|
|
1038
|
-
|
|
1039
|
-
The `dump` interceptor enables you to dump the response body from a request upon a given limit.
|
|
1040
|
-
|
|
1041
|
-
**Options**
|
|
1042
|
-
- `maxSize` - The maximum size (in bytes) of the response body to dump. If the size of the response's body exceeds this value then the connection will be closed. Default: `1048576`.
|
|
1043
|
-
|
|
1044
|
-
> The `Dispatcher#options` also gets extended with the options `dumpMaxSize`, `abortOnDumped`, and `waitForTrailers` which can be used to configure the interceptor at a request-per-request basis.
|
|
1045
|
-
|
|
1046
|
-
**Example - Basic Dump Interceptor**
|
|
1047
|
-
|
|
1048
|
-
```js
|
|
1049
|
-
const { Client, interceptors } = require("undici");
|
|
1050
|
-
const { dump } = interceptors;
|
|
1051
|
-
|
|
1052
|
-
const client = new Client("http://service.example").compose(
|
|
1053
|
-
dump({
|
|
1054
|
-
maxSize: 1024,
|
|
1055
|
-
})
|
|
1056
|
-
);
|
|
1057
|
-
|
|
1058
|
-
// or
|
|
1059
|
-
client.dispatch(
|
|
1060
|
-
{
|
|
1061
|
-
path: "/",
|
|
1062
|
-
method: "GET",
|
|
1063
|
-
dumpMaxSize: 1024,
|
|
1064
|
-
},
|
|
1065
|
-
handler
|
|
1066
|
-
);
|
|
1067
|
-
```
|
|
1068
|
-
|
|
1069
|
-
##### `dns`
|
|
1070
|
-
|
|
1071
|
-
The `dns` interceptor enables you to cache DNS lookups for a given duration, per origin.
|
|
1072
|
-
|
|
1073
|
-
>It is well suited for scenarios where you want to cache DNS lookups to avoid the overhead of resolving the same domain multiple times
|
|
1074
|
-
|
|
1075
|
-
**Options**
|
|
1076
|
-
- `maxTTL` - The maximum time-to-live (in milliseconds) of the DNS cache. It should be a positive integer. Default: `10000`.
|
|
1077
|
-
- Set `0` to disable TTL.
|
|
1078
|
-
- `maxItems` - The maximum number of items to cache. It should be a positive integer. Default: `Infinity`.
|
|
1079
|
-
- `dualStack` - Whether to resolve both IPv4 and IPv6 addresses. Default: `true`.
|
|
1080
|
-
- It will also attempt a happy-eyeballs-like approach to connect to the available addresses in case of a connection failure.
|
|
1081
|
-
- `affinity` - Whether to use IPv4 or IPv6 addresses. Default: `4`.
|
|
1082
|
-
- It can be either `4` or `6`.
|
|
1083
|
-
- It will only take effect if `dualStack` is `false`.
|
|
1084
|
-
- `lookup: (hostname: string, options: LookupOptions, callback: (err: NodeJS.ErrnoException | null, addresses: DNSInterceptorRecord[]) => void) => void` - Custom lookup function. Default: `dns.lookup`.
|
|
1085
|
-
- For more info see [dns.lookup](https://nodejs.org/api/dns.html#dnslookuphostname-options-callback).
|
|
1086
|
-
- `pick: (origin: URL, records: DNSInterceptorRecords, affinity: 4 | 6) => DNSInterceptorRecord` - Custom pick function. Default: `RoundRobin`.
|
|
1087
|
-
- The function should return a single record from the records array.
|
|
1088
|
-
- By default a simplified version of Round Robin is used.
|
|
1089
|
-
- The `records` property can be mutated to store the state of the balancing algorithm.
|
|
1090
|
-
- `storage: DNSStorage` - Custom storage for resolved DNS records
|
|
1091
|
-
|
|
1092
|
-
> The `Dispatcher#options` also gets extended with the options `dns.affinity`, `dns.dualStack`, `dns.lookup` and `dns.pick` which can be used to configure the interceptor at a request-per-request basis.
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
**DNSInterceptorRecord**
|
|
1096
|
-
It represents a DNS record.
|
|
1097
|
-
- `family` - (`number`) The IP family of the address. It can be either `4` or `6`.
|
|
1098
|
-
- `address` - (`string`) The IP address.
|
|
1099
|
-
|
|
1100
|
-
**DNSInterceptorOriginRecords**
|
|
1101
|
-
It represents a map of DNS IP addresses records for a single origin.
|
|
1102
|
-
- `4.ips` - (`DNSInterceptorRecord[] | null`) The IPv4 addresses.
|
|
1103
|
-
- `6.ips` - (`DNSInterceptorRecord[] | null`) The IPv6 addresses.
|
|
1104
|
-
|
|
1105
|
-
**DNSStorage**
|
|
1106
|
-
It represents a storage object for resolved DNS records.
|
|
1107
|
-
- `size` - (`number`) current size of the storage.
|
|
1108
|
-
- `get` - (`(origin: string) => DNSInterceptorOriginRecords | null`) method to get the records for a given origin.
|
|
1109
|
-
- `set` - (`(origin: string, records: DNSInterceptorOriginRecords | null, options: { ttl: number }) => void`) method to set the records for a given origin.
|
|
1110
|
-
- `delete` - (`(origin: string) => void`) method to delete records for a given origin.
|
|
1111
|
-
- `full` - (`() => boolean`) method to check if the storage is full, if returns `true`, DNS lookup will be skipped in this interceptor and new records will not be stored.
|
|
1112
|
-
|
|
1113
|
-
**Example - Basic DNS Interceptor**
|
|
1114
|
-
|
|
1115
|
-
```js
|
|
1116
|
-
const { Agent, interceptors } = require("undici");
|
|
1117
|
-
const { dns } = interceptors;
|
|
1118
|
-
|
|
1119
|
-
const client = new Agent().compose([
|
|
1120
|
-
dns({ ...opts })
|
|
1121
|
-
])
|
|
1122
|
-
|
|
1123
|
-
const response = await client.request({
|
|
1124
|
-
origin: `http://localhost:3030`,
|
|
1125
|
-
...requestOpts
|
|
1126
|
-
})
|
|
1127
|
-
```
|
|
1128
|
-
|
|
1129
|
-
**Example - DNS Interceptor and LRU cache as a storage**
|
|
1130
|
-
|
|
1131
|
-
```js
|
|
1132
|
-
const { Agent, interceptors } = require("undici");
|
|
1133
|
-
const QuickLRU = require("quick-lru");
|
|
1134
|
-
const { dns } = interceptors;
|
|
1135
|
-
|
|
1136
|
-
const lru = new QuickLRU({ maxSize: 100 });
|
|
1137
|
-
|
|
1138
|
-
const lruAdapter = {
|
|
1139
|
-
get size() {
|
|
1140
|
-
return lru.size;
|
|
1141
|
-
},
|
|
1142
|
-
get(origin) {
|
|
1143
|
-
return lru.get(origin);
|
|
1144
|
-
},
|
|
1145
|
-
set(origin, records, { ttl }) {
|
|
1146
|
-
lru.set(origin, records, { maxAge: ttl });
|
|
1147
|
-
},
|
|
1148
|
-
delete(origin) {
|
|
1149
|
-
lru.delete(origin);
|
|
1150
|
-
},
|
|
1151
|
-
full() {
|
|
1152
|
-
// For LRU cache, we can always store new records,
|
|
1153
|
-
// old records will be evicted automatically
|
|
1154
|
-
return false;
|
|
1155
|
-
}
|
|
1156
|
-
}
|
|
1157
|
-
|
|
1158
|
-
const client = new Agent().compose([
|
|
1159
|
-
dns({ storage: lruAdapter })
|
|
1160
|
-
])
|
|
1161
|
-
|
|
1162
|
-
const response = await client.request({
|
|
1163
|
-
origin: `http://localhost:3030`,
|
|
1164
|
-
...requestOpts
|
|
1165
|
-
})
|
|
1166
|
-
```
|
|
1167
|
-
|
|
1168
|
-
##### `responseError`
|
|
1169
|
-
|
|
1170
|
-
The `responseError` interceptor throws an error for responses with status code errors (>= 400).
|
|
1171
|
-
|
|
1172
|
-
**Example**
|
|
1173
|
-
|
|
1174
|
-
```js
|
|
1175
|
-
const { Client, interceptors } = require("undici");
|
|
1176
|
-
const { responseError } = interceptors;
|
|
1177
|
-
|
|
1178
|
-
const client = new Client("http://service.example").compose(
|
|
1179
|
-
responseError()
|
|
1180
|
-
);
|
|
1181
|
-
|
|
1182
|
-
// Will throw a ResponseError for status codes >= 400
|
|
1183
|
-
await client.request({
|
|
1184
|
-
method: "GET",
|
|
1185
|
-
path: "/"
|
|
1186
|
-
});
|
|
1187
|
-
```
|
|
1188
|
-
|
|
1189
|
-
##### `decompress`
|
|
1190
|
-
|
|
1191
|
-
⚠️ The decompress interceptor is experimental and subject to change.
|
|
1192
|
-
|
|
1193
|
-
The `decompress` interceptor automatically decompresses response bodies that are compressed with gzip, deflate, brotli, or zstd compression. It removes the `content-encoding` and `content-length` headers from decompressed responses and supports RFC-9110 compliant multiple encodings.
|
|
1194
|
-
|
|
1195
|
-
**Options**
|
|
1196
|
-
|
|
1197
|
-
- `skipErrorResponses` - Whether to skip decompression for error responses (status codes >= 400). Default: `true`.
|
|
1198
|
-
- `skipStatusCodes` - Array of status codes to skip decompression for. Default: `[204, 304]`.
|
|
1199
|
-
|
|
1200
|
-
**Example - Basic Decompress Interceptor**
|
|
1201
|
-
|
|
1202
|
-
```js
|
|
1203
|
-
const { Client, interceptors } = require("undici");
|
|
1204
|
-
const { decompress } = interceptors;
|
|
1205
|
-
|
|
1206
|
-
const client = new Client("http://service.example").compose(
|
|
1207
|
-
decompress()
|
|
1208
|
-
);
|
|
1209
|
-
|
|
1210
|
-
// Automatically decompresses gzip/deflate/brotli/zstd responses
|
|
1211
|
-
const response = await client.request({
|
|
1212
|
-
method: "GET",
|
|
1213
|
-
path: "/"
|
|
1214
|
-
});
|
|
1215
|
-
```
|
|
1216
|
-
|
|
1217
|
-
**Example - Custom Options**
|
|
1218
|
-
|
|
1219
|
-
```js
|
|
1220
|
-
const { Client, interceptors } = require("undici");
|
|
1221
|
-
const { decompress } = interceptors;
|
|
1222
|
-
|
|
1223
|
-
const client = new Client("http://service.example").compose(
|
|
1224
|
-
decompress({
|
|
1225
|
-
skipErrorResponses: false, // Decompress 5xx responses
|
|
1226
|
-
skipStatusCodes: [204, 304, 201] // Skip these status codes
|
|
1227
|
-
})
|
|
1228
|
-
);
|
|
1229
|
-
```
|
|
1230
|
-
|
|
1231
|
-
**Supported Encodings**
|
|
1232
|
-
|
|
1233
|
-
- `gzip` / `x-gzip` - GZIP compression
|
|
1234
|
-
- `deflate` / `x-compress` - DEFLATE compression
|
|
1235
|
-
- `br` - Brotli compression
|
|
1236
|
-
- `zstd` - Zstandard compression
|
|
1237
|
-
- Multiple encodings (e.g., `gzip, deflate`) are supported per RFC-9110
|
|
1238
|
-
|
|
1239
|
-
**Behavior**
|
|
1240
|
-
|
|
1241
|
-
- Skips decompression for status codes < 200 or >= 400 (configurable)
|
|
1242
|
-
- Skips decompression for 204 No Content and 304 Not Modified by default
|
|
1243
|
-
- Removes `content-encoding` and `content-length` headers when decompressing
|
|
1244
|
-
- Passes through unsupported encodings unchanged
|
|
1245
|
-
- Handles case-insensitive encoding names
|
|
1246
|
-
- Supports streaming decompression without buffering
|
|
1247
|
-
|
|
1248
|
-
##### `Cache Interceptor`
|
|
1249
|
-
|
|
1250
|
-
The `cache` interceptor implements client-side response caching as described in
|
|
1251
|
-
[RFC9111](https://www.rfc-editor.org/rfc/rfc9111.html).
|
|
1252
|
-
|
|
1253
|
-
**Options**
|
|
1254
|
-
|
|
1255
|
-
- `store` - The [`CacheStore`](/docs/docs/api/CacheStore.md) to store and retrieve responses from. Default is [`MemoryCacheStore`](/docs/docs/api/CacheStore.md#memorycachestore).
|
|
1256
|
-
- `methods` - The [**safe** HTTP methods](https://www.rfc-editor.org/rfc/rfc9110#section-9.2.1) to cache the response of.
|
|
1257
|
-
- `cacheByDefault` - The default expiration time to cache responses by if they don't have an explicit expiration and cannot have an heuristic expiry computed. If this isn't present, responses neither with an explicit expiration nor heuristically cacheable will not be cached. Default `undefined`.
|
|
1258
|
-
- `type` - The [type of cache](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching#types_of_caches) for Undici to act as. Can be `shared` or `private`. Default `shared`. `private` implies privately cacheable responses will be cached and potentially shared with other users of your application.
|
|
1259
|
-
|
|
1260
|
-
**Usage with `fetch`**
|
|
1261
|
-
|
|
1262
|
-
```js
|
|
1263
|
-
const { Agent, cacheStores, interceptors, setGlobalDispatcher } = require('undici')
|
|
1264
|
-
|
|
1265
|
-
const client = new Agent().compose(interceptors.cache({
|
|
1266
|
-
store: new cacheStores.MemoryCacheStore({
|
|
1267
|
-
maxSize: 100 * 1024 * 1024, // 100MB
|
|
1268
|
-
maxCount: 1000,
|
|
1269
|
-
maxEntrySize: 5 * 1024 * 1024 // 5MB
|
|
1270
|
-
})
|
|
1271
|
-
}))
|
|
1272
|
-
|
|
1273
|
-
setGlobalDispatcher(client)
|
|
1274
|
-
|
|
1275
|
-
// First request goes to the network and is cached when cache headers allow it.
|
|
1276
|
-
const first = await fetch('https://example.com/data')
|
|
1277
|
-
|
|
1278
|
-
// Second request can be served from cache according to RFC9111 rules.
|
|
1279
|
-
const second = await fetch('https://example.com/data')
|
|
1280
|
-
```
|
|
1281
|
-
|
|
1282
|
-
##### `Deduplicate Interceptor`
|
|
1283
|
-
|
|
1284
|
-
The `deduplicate` interceptor deduplicates concurrent identical requests. When multiple identical requests are made while one is already in-flight, only one request is sent to the origin server, and all waiting handlers receive the same response. This reduces server load and improves performance.
|
|
1285
|
-
|
|
1286
|
-
**Options**
|
|
1287
|
-
|
|
1288
|
-
- `methods` - The [**safe** HTTP methods](https://www.rfc-editor.org/rfc/rfc9110#section-9.2.1) to deduplicate. Default `['GET']`.
|
|
1289
|
-
- `skipHeaderNames` - Header names that, if present in a request, will cause the request to skip deduplication entirely. Useful for headers like `idempotency-key` where presence indicates unique processing. Header name matching is case-insensitive. Default `[]`.
|
|
1290
|
-
- `excludeHeaderNames` - Header names to exclude from the deduplication key. Requests with different values for these headers will still be deduplicated together. Useful for headers like `x-request-id` that vary per request but shouldn't affect deduplication. Header name matching is case-insensitive. Default `[]`.
|
|
1291
|
-
- `maxBufferSize` - Maximum bytes buffered per paused waiting deduplicated handler. If a waiting handler remains paused and exceeds this threshold, it is failed with an abort error to prevent unbounded memory growth. Default `5 * 1024 * 1024`.
|
|
1292
|
-
|
|
1293
|
-
**Usage**
|
|
1294
|
-
|
|
1295
|
-
```js
|
|
1296
|
-
const { Client, interceptors } = require("undici");
|
|
1297
|
-
const { deduplicate, cache } = interceptors;
|
|
1298
|
-
|
|
1299
|
-
// Deduplicate only
|
|
1300
|
-
const client = new Client("http://service.example").compose(
|
|
1301
|
-
deduplicate()
|
|
1302
|
-
);
|
|
1303
|
-
|
|
1304
|
-
// Deduplicate with caching
|
|
1305
|
-
const clientWithCache = new Client("http://service.example").compose(
|
|
1306
|
-
deduplicate(),
|
|
1307
|
-
cache()
|
|
1308
|
-
);
|
|
1309
|
-
```
|
|
1310
|
-
|
|
1311
|
-
Requests are considered identical if they have the same:
|
|
1312
|
-
- Origin
|
|
1313
|
-
- HTTP method
|
|
1314
|
-
- Path
|
|
1315
|
-
- Request headers (excluding any headers specified in `excludeHeaderNames`)
|
|
1316
|
-
|
|
1317
|
-
All deduplicated requests receive the complete response including status code, headers, and body.
|
|
1318
|
-
|
|
1319
|
-
For observability, request deduplication events are published to the `undici:request:pending-requests` [diagnostic channel](/docs/docs/api/DiagnosticsChannel.md#undicirequestpending-requests).
|
|
1320
|
-
|
|
1321
|
-
## Instance Events
|
|
731
|
+
For the full list of built-in interceptors provided by undici, see [Interceptors](Interceptors.md).
|
|
1322
732
|
|
|
1323
733
|
### Event: `'connect'`
|
|
1324
734
|
|
|
1325
|
-
|
|
735
|
+
<!-- YAML
|
|
736
|
+
added: v4.0.0
|
|
737
|
+
-->
|
|
1326
738
|
|
|
1327
|
-
*
|
|
1328
|
-
*
|
|
739
|
+
* `origin` {URL}
|
|
740
|
+
* `targets` {Array<Dispatcher>}
|
|
741
|
+
|
|
742
|
+
Emitted when the dispatcher has connected to the origin.
|
|
1329
743
|
|
|
1330
744
|
### Event: `'disconnect'`
|
|
1331
745
|
|
|
1332
|
-
|
|
746
|
+
<!-- YAML
|
|
747
|
+
added: v4.0.0
|
|
748
|
+
-->
|
|
1333
749
|
|
|
1334
|
-
*
|
|
1335
|
-
*
|
|
1336
|
-
*
|
|
750
|
+
* `origin` {URL}
|
|
751
|
+
* `targets` {Array<Dispatcher>}
|
|
752
|
+
* `error` {Error}
|
|
1337
753
|
|
|
1338
754
|
Emitted when the dispatcher has been disconnected from the origin.
|
|
1339
755
|
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
756
|
+
For HTTP/2, this event is also emitted when the dispatcher receives a
|
|
757
|
+
[GOAWAY frame][], with an error whose message is `HTTP/2: "GOAWAY" frame received`
|
|
758
|
+
and whose code is `UND_ERR_INFO`. Because of the binary-framing nature of the
|
|
759
|
+
protocol, a request may hang if a frame is received between the `HEADER` and
|
|
760
|
+
`DATA` frames. It is recommended to handle this event and close the dispatcher to
|
|
761
|
+
create a new HTTP/2 session.
|
|
1343
762
|
|
|
1344
763
|
### Event: `'connectionError'`
|
|
1345
764
|
|
|
1346
|
-
|
|
765
|
+
<!-- YAML
|
|
766
|
+
added: v4.0.0
|
|
767
|
+
-->
|
|
1347
768
|
|
|
1348
|
-
*
|
|
1349
|
-
*
|
|
1350
|
-
*
|
|
769
|
+
* `origin` {URL}
|
|
770
|
+
* `targets` {Array<Dispatcher>}
|
|
771
|
+
* `error` {Error}
|
|
1351
772
|
|
|
1352
|
-
Emitted when dispatcher fails to connect to
|
|
1353
|
-
origin.
|
|
773
|
+
Emitted when the dispatcher fails to connect to the origin.
|
|
1354
774
|
|
|
1355
775
|
### Event: `'drain'`
|
|
1356
776
|
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
[
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
]
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
### Example 3 - Iterable
|
|
1404
|
-
|
|
1405
|
-
```js
|
|
1406
|
-
new Headers({
|
|
1407
|
-
'content-length': '123',
|
|
1408
|
-
'content-type': 'text/plain',
|
|
1409
|
-
connection: 'keep-alive',
|
|
1410
|
-
host: 'mysite.com',
|
|
1411
|
-
accept: '*/*'
|
|
1412
|
-
})
|
|
1413
|
-
```
|
|
1414
|
-
or
|
|
1415
|
-
```js
|
|
1416
|
-
new Map([
|
|
1417
|
-
['content-length', '123'],
|
|
1418
|
-
['content-type', 'text/plain'],
|
|
1419
|
-
['connection', 'keep-alive'],
|
|
1420
|
-
['host', 'mysite.com'],
|
|
1421
|
-
['accept', '*/*']
|
|
1422
|
-
])
|
|
1423
|
-
```
|
|
1424
|
-
or
|
|
1425
|
-
```js
|
|
1426
|
-
{
|
|
1427
|
-
*[Symbol.iterator] () {
|
|
1428
|
-
yield ['content-length', '123']
|
|
1429
|
-
yield ['content-type', 'text/plain']
|
|
1430
|
-
yield ['connection', 'keep-alive']
|
|
1431
|
-
yield ['host', 'mysite.com']
|
|
1432
|
-
yield ['accept', '*/*']
|
|
1433
|
-
}
|
|
1434
|
-
}
|
|
1435
|
-
```
|
|
777
|
+
<!-- YAML
|
|
778
|
+
added: v4.0.0
|
|
779
|
+
-->
|
|
780
|
+
|
|
781
|
+
* `origin` {URL}
|
|
782
|
+
|
|
783
|
+
Emitted when the dispatcher is no longer busy and further [`dispatch()`][] calls
|
|
784
|
+
can make progress.
|
|
785
|
+
|
|
786
|
+
## Pre-built interceptors
|
|
787
|
+
|
|
788
|
+
For the full reference of built-in interceptors (`dump`, `retry`, `redirect`,
|
|
789
|
+
`decompress`, `responseError`, `dns`, `cache`, `deduplicate`) and their options,
|
|
790
|
+
see [Interceptors](Interceptors.md).
|
|
791
|
+
|
|
792
|
+
[Fastify]: https://fastify.dev
|
|
793
|
+
[GOAWAY frame]: https://webconcepts.info/concepts/http2-frame-type/0x7
|
|
794
|
+
[HTTP `CONNECT`]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT
|
|
795
|
+
[MDN: Protocol upgrade mechanism]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism
|
|
796
|
+
[`.arrayBuffer()`]: https://fetch.spec.whatwg.org/#dom-body-arraybuffer
|
|
797
|
+
[`.blob()`]: https://fetch.spec.whatwg.org/#dom-body-blob
|
|
798
|
+
[`.bytes()`]: https://fetch.spec.whatwg.org/#dom-body-bytes
|
|
799
|
+
[`.json()`]: https://fetch.spec.whatwg.org/#dom-body-json
|
|
800
|
+
[`.text()`]: https://fetch.spec.whatwg.org/#dom-body-text
|
|
801
|
+
[`'drain'`]: #event-drain
|
|
802
|
+
[`Agent`]: Agent.md#class-agent
|
|
803
|
+
[`BalancedPool`]: BalancedPool.md#class-balancedpool
|
|
804
|
+
[`Client`]: Client.md#class-client
|
|
805
|
+
[`EventEmitter`]: https://nodejs.org/api/events.html#class-eventemitter
|
|
806
|
+
[`Pool`]: Pool.md#class-pool
|
|
807
|
+
[`Readable`]: https://nodejs.org/api/stream.html#class-streamreadable
|
|
808
|
+
[`Writable`]: https://nodejs.org/api/stream.html#class-streamwritable
|
|
809
|
+
[`close()`]: #dispatcherclosecallback
|
|
810
|
+
[`connect()`]: #dispatcherconnectoptions-callback
|
|
811
|
+
[`destroy()`]: #dispatcherdestroyerror-callback
|
|
812
|
+
[`dispatch()`]: #dispatcherdispatchoptions-handler
|
|
813
|
+
[`dispatcher.compose()`]: #dispatchercomposeinterceptors-interceptor
|
|
814
|
+
[`dispatcher.dispatch()`]: #dispatcherdispatchoptions-handler
|
|
815
|
+
[`dispatcher.request()`]: #dispatcherrequestoptions-callback
|
|
816
|
+
[`pipeline()`]: #dispatcherpipelineoptions-handler
|
|
817
|
+
[`request()`]: #dispatcherrequestoptions-callback
|
|
818
|
+
[`stream()`]: #dispatcherstreamoptions-factory-callback
|
|
819
|
+
[`stream.pipeline()`]: https://nodejs.org/api/stream.html#streampipelinesource-transforms-destination-options
|
|
820
|
+
[`upgrade()`]: #dispatcherupgradeoptions-callback
|
|
821
|
+
[body mixin]: https://fetch.spec.whatwg.org/#body-mixin
|