@openclaw/discord 2026.6.33 → 2026.6.35
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/action-runtime-api.js +1 -1
- package/dist/{api-CjnGBhM3.js → api-1GXlSrYP.js} +42 -21
- package/dist/api.js +14 -14
- package/dist/{approval-handler.runtime-BWPQrzpP.js → approval-handler.runtime-Dv4p7REU.js} +3 -3
- package/dist/{audit-BwJ_2OjL.js → audit-DEVi9nIs.js} +3 -3
- package/dist/{channel-Ddnonoy4.js → channel-CQIRiosn.js} +15 -15
- package/dist/{channel-actions-CwmIqnYN.js → channel-actions-JziPEcBU.js} +2 -2
- package/dist/{channel-actions.runtime-DvpTfaJ4.js → channel-actions.runtime-D7L47LY3.js} +5 -5
- package/dist/channel-plugin-api.js +1 -1
- package/dist/{channel.setup-glZ5Y98a.js → channel.setup-oMeR19Cc.js} +2 -2
- package/dist/{components-j6lTZBsT.js → components-_s5UPJqU.js} +1 -1
- package/dist/{conversation-identity-36FFBwm5.js → conversation-identity-B0k94-bz.js} +2 -2
- package/dist/{directory-config-BYMFzL29.js → directory-config-CD1svkyN.js} +1 -1
- package/dist/directory-contract-api.js +1 -1
- package/dist/{directory-live-9JtJCiu8.js → directory-live-DBTsSRxl.js} +5 -5
- package/dist/{handle-action.guild-admin-xxGYrp-I.js → handle-action.guild-admin-CYIFMxh-.js} +2 -2
- package/dist/index.js +1 -1
- package/dist/{manager.runtime-BK6cf2Zu.js → manager.runtime-BZVdGX2d.js} +17 -9
- package/dist/{message-handler-BqpJIavO.js → message-handler-DZd9Bpq8.js} +7 -6
- package/dist/{message-handler.preflight-C90MxCcv.js → message-handler.preflight-Axv9J9EO.js} +12 -11
- package/dist/{message-handler.process-CeQ7Bd_3.js → message-handler.process-CG0zFS-Z.js} +10 -10
- package/dist/{message-utils-CJeAoISH.js → message-utils-Dbuw27gd.js} +1 -1
- package/dist/{outbound-adapter-Dxs6iKEq.js → outbound-adapter-CKC_kmUN.js} +6 -6
- package/dist/pluralkit-CfTReLl3.js +40 -0
- package/dist/{probe-esE_Oqdm.js → probe-DcgZIpyF.js} +3 -2
- package/dist/{probe.runtime-C3Atgpvr.js → probe.runtime-CVwM9L5E.js} +1 -1
- package/dist/{provider-C7376XFH.js → provider-CwwSXIIj.js} +102 -37
- package/dist/{provider-session.runtime-CCdqogGf.js → provider-session.runtime-reBWkqHZ.js} +3 -3
- package/dist/provider.runtime-BsmTLYS_.js +2 -0
- package/dist/{resolve-allowlist-common-3gB2Gpmm.js → resolve-allowlist-common-D7bZqAw3.js} +3 -3
- package/dist/{resolve-channels-B3gQf8QL.js → resolve-channels--oE-RnO6.js} +6 -6
- package/dist/{resolve-users-BO10y5fJ.js → resolve-users-QXo8CXBU.js} +5 -5
- package/dist/{runtime-CFlR3L9X.js → runtime-CF7ramAs.js} +7 -7
- package/dist/runtime-api.actions.js +2 -2
- package/dist/runtime-api.js +19 -19
- package/dist/runtime-api.lookup.js +5 -5
- package/dist/runtime-api.monitor-CzF8MUl_.js +6 -0
- package/dist/runtime-api.monitor.js +5 -5
- package/dist/runtime-api.send.js +5 -5
- package/dist/runtime-api.threads.js +3 -3
- package/dist/{send-JHxfT4Pk.js → send-Bb295iHT.js} +5 -5
- package/dist/{send.components-CN35E2Ck.js → send.components-COkDUO3B.js} +4 -4
- package/dist/{send.outbound-B-cA1Esv.js → send.outbound-B_sk0hNe.js} +3 -3
- package/dist/{send.receipt-CGecJDin.js → send.receipt-Cli6_9Sn.js} +19 -7
- package/dist/{send.shared-vcbfThdy.js → send.shared-BCG7MWnP.js} +2 -2
- package/dist/session-binding-contract-api.js +1 -1
- package/dist/setup-plugin-api.js +1 -1
- package/dist/{subagent-hooks-20wXhVzZ.js → subagent-hooks-CtqoWK4x.js} +2 -2
- package/dist/subagent-hooks-api.js +1 -1
- package/dist/{system-events-DR27TE7p.js → system-events-BxGNk_wE.js} +1 -1
- package/dist/{target-resolver-CDi1gI90.js → target-resolver-BcoK6b0g.js} +2 -2
- package/dist/targets-Q-ZoP7K4.js +3 -0
- package/dist/{thread-bindings-DU4T6nDo.js → thread-bindings-DJ_HT6GJ.js} +4 -4
- package/dist/{thread-bindings.discord-api-CYkr-3Gj.js → thread-bindings.discord-api-OPyjvgyz.js} +4 -4
- package/dist/{thread-bindings.manager-BqWs1UE2.js → thread-bindings.manager-Cd2HAVw_.js} +3 -3
- package/dist/{transcripts-source-D-lBI5ZD.js → transcripts-source-CPf5gURy.js} +1 -1
- package/dist/transcripts-source-api.js +1 -1
- package/dist/{typing-xr05bL-r.js → typing-DY0cwLdg.js} +1 -1
- package/node_modules/undici/README.md +8 -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 +386 -179
- 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 +122 -71
- 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 +485 -1102
- package/node_modules/undici/docs/docs/api/EnvHttpProxyAgent.md +102 -87
- package/node_modules/undici/docs/docs/api/Errors.md +503 -40
- package/node_modules/undici/docs/docs/api/EventSource.md +298 -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 +418 -0
- package/node_modules/undici/docs/docs/api/MockAgent.md +333 -314
- 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 +321 -258
- 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 +177 -196
- 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 +784 -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 +42 -7
- package/node_modules/undici/lib/cache/memory-cache-store.js +56 -11
- package/node_modules/undici/lib/cache/sqlite-cache-store.js +7 -1
- package/node_modules/undici/lib/core/connect.js +16 -2
- package/node_modules/undici/lib/core/errors.js +20 -0
- package/node_modules/undici/lib/core/request.js +14 -3
- package/node_modules/undici/lib/core/symbols.js +3 -0
- package/node_modules/undici/lib/core/util.js +30 -2
- package/node_modules/undici/lib/dispatcher/agent.js +10 -5
- package/node_modules/undici/lib/dispatcher/balanced-pool.js +7 -4
- package/node_modules/undici/lib/dispatcher/client-h1.js +74 -9
- package/node_modules/undici/lib/dispatcher/client-h2.js +483 -151
- package/node_modules/undici/lib/dispatcher/client.js +76 -30
- package/node_modules/undici/lib/dispatcher/dispatcher-base.js +13 -1
- package/node_modules/undici/lib/dispatcher/dispatcher.js +15 -1
- package/node_modules/undici/lib/dispatcher/dispatcher1-wrapper.js +3 -0
- package/node_modules/undici/lib/dispatcher/env-http-proxy-agent.js +37 -8
- package/node_modules/undici/lib/dispatcher/proxy-agent.js +58 -7
- package/node_modules/undici/lib/dispatcher/retry-agent.js +3 -0
- package/node_modules/undici/lib/dispatcher/round-robin-pool.js +1 -1
- package/node_modules/undici/lib/dispatcher/socks5-proxy-agent.js +106 -22
- package/node_modules/undici/lib/global.js +36 -16
- package/node_modules/undici/lib/handler/cache-handler.js +297 -65
- package/node_modules/undici/lib/handler/cache-revalidation-handler.js +13 -3
- package/node_modules/undici/lib/handler/decorator-handler.js +7 -1
- package/node_modules/undici/lib/handler/deduplication-handler.js +12 -6
- package/node_modules/undici/lib/handler/redirect-handler.js +26 -5
- package/node_modules/undici/lib/handler/retry-handler.js +238 -51
- package/node_modules/undici/lib/interceptor/cache.js +221 -58
- package/node_modules/undici/lib/interceptor/decompress.js +124 -15
- package/node_modules/undici/lib/interceptor/deduplicate.js +5 -4
- package/node_modules/undici/lib/interceptor/dns.js +7 -0
- package/node_modules/undici/lib/interceptor/dump.js +10 -23
- package/node_modules/undici/lib/mock/mock-agent.js +13 -1
- package/node_modules/undici/lib/mock/mock-interceptor.js +25 -7
- package/node_modules/undici/lib/mock/mock-utils.js +246 -27
- package/node_modules/undici/lib/util/cache.js +415 -71
- package/node_modules/undici/lib/util/date.js +23 -6
- package/node_modules/undici/lib/web/cookies/parse.js +150 -146
- package/node_modules/undici/lib/web/cookies/util.js +79 -9
- package/node_modules/undici/lib/web/eventsource/eventsource-stream.js +28 -1
- package/node_modules/undici/lib/web/eventsource/eventsource.js +13 -3
- package/node_modules/undici/lib/web/eventsource/util.js +1 -1
- package/node_modules/undici/lib/web/fetch/body.js +2 -1
- package/node_modules/undici/lib/web/fetch/constants.js +1 -1
- package/node_modules/undici/lib/web/fetch/index.js +17 -2
- package/node_modules/undici/lib/web/fetch/request.js +2 -3
- package/node_modules/undici/lib/web/fetch/response.js +1 -3
- package/node_modules/undici/lib/web/fetch/util.js +10 -2
- package/node_modules/undici/lib/web/webidl/index.js +3 -3
- package/node_modules/undici/lib/web/websocket/connection.js +1 -1
- package/node_modules/undici/lib/web/websocket/permessage-deflate.js +5 -0
- package/node_modules/undici/lib/web/websocket/stream/websocketstream.js +8 -10
- package/node_modules/undici/lib/web/websocket/websocket.js +22 -0
- package/node_modules/undici/package.json +4 -4
- package/node_modules/undici/types/client.d.ts +53 -3
- package/node_modules/undici/types/cookies.d.ts +1 -1
- package/node_modules/undici/types/dispatcher.d.ts +3 -3
- package/node_modules/undici/types/errors.d.ts +10 -0
- package/node_modules/undici/types/handlers.d.ts +2 -0
- package/node_modules/undici/types/interceptors.d.ts +2 -0
- package/node_modules/undici/types/mock-agent.d.ts +11 -10
- package/node_modules/undici/types/mock-interceptor.d.ts +5 -1
- package/node_modules/undici/types/proxy-agent.d.ts +7 -0
- package/node_modules/undici/types/socks5-proxy-agent.d.ts +2 -0
- package/npm-shrinkwrap.json +7 -7
- package/package.json +6 -5
- package/dist/pluralkit-DI6RYaVj.js +0 -24
- package/dist/provider.runtime-D1m66kDS.js +0 -2
- package/dist/runtime-api.monitor-4oc58TSk.js +0 -6
- package/dist/targets-C6yd3y8R.js +0 -3
|
@@ -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
|
-
### `
|
|
149
|
+
### `dispatcher.destroy([error[, callback]])`
|
|
127
150
|
|
|
128
|
-
|
|
151
|
+
<!-- YAML
|
|
152
|
+
added: v4.0.0
|
|
153
|
+
-->
|
|
129
154
|
|
|
130
|
-
|
|
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.
|
|
131
160
|
|
|
132
|
-
|
|
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.
|
|
133
165
|
|
|
134
|
-
|
|
135
|
-
* **callback** `(error: Error | null, data: null) => void` (optional)
|
|
166
|
+
Both arguments are optional, so the method can be called in four ways:
|
|
136
167
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
```js
|
|
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,111 @@ 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
|
+
HTTP/1.1 headers/body parser timeouts are not guaranteed to fire with exact
|
|
232
|
+
millisecond precision: delays up to 1000ms use native timers, while larger
|
|
233
|
+
delays use undici's lower-overhead fast timers with a target resolution
|
|
234
|
+
around 500ms.
|
|
235
|
+
* `reset` {boolean} Whether the request should establish a keep-alive
|
|
236
|
+
connection. **Default:** `false`.
|
|
237
|
+
* `expectContinue` {boolean} For HTTP/2, appends the `expect: 100-continue`
|
|
238
|
+
header and halts the request body until a `100 Continue` is received from the
|
|
239
|
+
remote server. **Default:** `false`.
|
|
240
|
+
* `handler` {Object} A set of callbacks invoked over the request lifecycle.
|
|
241
|
+
* `onRequestStart` {Function} Invoked before the request is dispatched on the
|
|
242
|
+
socket. May be invoked multiple times when the request is retried.
|
|
243
|
+
* `controller` {DispatchController}
|
|
244
|
+
* `context` {any}
|
|
245
|
+
* `onRequestUpgrade` {Function} (optional) Invoked when the request is upgraded.
|
|
246
|
+
Required when `options.upgrade` is set or `options.method` is `'CONNECT'`.
|
|
247
|
+
* `controller` {DispatchController}
|
|
248
|
+
* `statusCode` {number}
|
|
249
|
+
* `headers` {Object}
|
|
250
|
+
* `socket` {Duplex}
|
|
251
|
+
* `onResponseStart` {Function} (optional) Invoked when the status code and
|
|
252
|
+
headers have been received. May be invoked multiple times for 1xx
|
|
253
|
+
informational responses. Not used for `upgrade` requests.
|
|
254
|
+
* `controller` {DispatchController}
|
|
255
|
+
* `statusCode` {number}
|
|
256
|
+
* `headers` {Object}
|
|
257
|
+
* `statusMessage` {string} (optional)
|
|
258
|
+
* `onResponseData` {Function} (optional) Invoked when response payload data is
|
|
259
|
+
received. Not used for `upgrade` requests.
|
|
260
|
+
* `controller` {DispatchController}
|
|
261
|
+
* `chunk` {Buffer}
|
|
262
|
+
* `onResponseEnd` {Function} (optional) Invoked when the response payload and
|
|
263
|
+
trailers have been received and the request has completed. Not used for
|
|
264
|
+
`upgrade` requests.
|
|
265
|
+
* `controller` {DispatchController}
|
|
266
|
+
* `trailers` {Object}
|
|
267
|
+
* `onResponseError` {Function} (optional) Invoked when an error occurs. Must not
|
|
268
|
+
throw.
|
|
269
|
+
* `controller` {DispatchController}
|
|
270
|
+
* `error` {Error}
|
|
271
|
+
* `onResponseStarted` {Function} (optional) Invoked when the response is
|
|
272
|
+
received, before headers have been read.
|
|
273
|
+
* `onBodySent` {Function} (optional) Invoked when a chunk of the request body is
|
|
274
|
+
sent. May be invoked multiple times for chunked requests.
|
|
275
|
+
* `chunk` {Buffer}
|
|
276
|
+
* `onRequestSent` {Function} (optional) Invoked after the request body is fully
|
|
277
|
+
sent.
|
|
278
|
+
* Returns: {boolean} `false` when the dispatcher is busy and further `dispatch()`
|
|
279
|
+
calls will make no progress until the [`'drain'`][] event is emitted.
|
|
280
|
+
|
|
281
|
+
This is the low-level API on which all the higher-level methods are built. This
|
|
282
|
+
API is expected to evolve through semver-major versions and is less stable than
|
|
283
|
+
the higher-level methods. It is primarily intended for library developers who
|
|
284
|
+
implement higher-level APIs on top of it.
|
|
285
|
+
|
|
286
|
+
The `controller` passed to each handler is a `DispatchController` with the
|
|
287
|
+
following shape:
|
|
288
|
+
|
|
289
|
+
* `aborted` {boolean} Whether the request has been aborted.
|
|
290
|
+
* `paused` {boolean} Whether the request is paused.
|
|
291
|
+
* `reason` {Error|null} The reason the request was aborted, if any.
|
|
292
|
+
* `rawHeaders` {Buffer[]|string[]|Object|null} The raw response headers,
|
|
293
|
+
preserving duplicates and casing.
|
|
294
|
+
* `rawTrailers` {Buffer[]|string[]|Object|null} The raw response trailers.
|
|
295
|
+
* `abort(reason)` Aborts the request with the given `reason`.
|
|
296
|
+
* `pause()` Pauses the response stream.
|
|
297
|
+
* `resume()` Resumes the response stream.
|
|
298
|
+
|
|
299
|
+
Call `controller.pause()` and `controller.resume()` to apply backpressure rather
|
|
300
|
+
than returning `false` from a handler.
|
|
301
|
+
|
|
302
|
+
```mjs
|
|
303
|
+
import { createServer } from 'node:http'
|
|
255
304
|
import { Client } from 'undici'
|
|
256
|
-
import { once } from 'events'
|
|
305
|
+
import { once } from 'node:events'
|
|
257
306
|
|
|
258
307
|
const server = createServer((request, response) => {
|
|
259
308
|
response.end('Hello, World!')
|
|
@@ -268,9 +317,7 @@ const data = []
|
|
|
268
317
|
client.dispatch({
|
|
269
318
|
path: '/',
|
|
270
319
|
method: 'GET',
|
|
271
|
-
headers: {
|
|
272
|
-
'x-foo': 'bar'
|
|
273
|
-
}
|
|
320
|
+
headers: { 'x-foo': 'bar' }
|
|
274
321
|
}, {
|
|
275
322
|
onRequestStart: () => {
|
|
276
323
|
console.log('Connected!')
|
|
@@ -279,158 +326,52 @@ client.dispatch({
|
|
|
279
326
|
console.error(error)
|
|
280
327
|
},
|
|
281
328
|
onResponseStart: (_controller, statusCode, headers) => {
|
|
282
|
-
console.log(`
|
|
329
|
+
console.log(`statusCode: ${statusCode} | headers: ${JSON.stringify(headers)}`)
|
|
283
330
|
},
|
|
284
331
|
onResponseData: (_controller, chunk) => {
|
|
285
|
-
console.log('onResponseData: chunk received')
|
|
286
332
|
data.push(chunk)
|
|
287
333
|
},
|
|
288
334
|
onResponseEnd: (_controller, trailers) => {
|
|
289
|
-
console.log(`
|
|
290
|
-
|
|
291
|
-
console.log(`Data: ${res}`)
|
|
335
|
+
console.log(`trailers: ${JSON.stringify(trailers)}`)
|
|
336
|
+
console.log(`Data: ${Buffer.concat(data).toString('utf8')}`)
|
|
292
337
|
client.close()
|
|
293
338
|
server.close()
|
|
294
339
|
}
|
|
295
340
|
})
|
|
296
341
|
```
|
|
297
342
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
343
|
+
### `dispatcher.pipeline(options, handler)`
|
|
344
|
+
|
|
345
|
+
<!-- YAML
|
|
346
|
+
added: v4.0.0
|
|
347
|
+
-->
|
|
348
|
+
|
|
349
|
+
* `options` {Object} Accepts every field of [`dispatcher.request()`][]'s
|
|
350
|
+
`options`, plus:
|
|
351
|
+
* `objectMode` {boolean} Set to `true` when `handler` returns an object stream.
|
|
352
|
+
**Default:** `false`.
|
|
353
|
+
* `handler` {Function} Returns a `Readable` from which the result is read.
|
|
354
|
+
* `data` {Object}
|
|
355
|
+
* `statusCode` {number}
|
|
356
|
+
* `headers` {Object} The response headers.
|
|
357
|
+
* `opaque` {any} The `opaque` value passed in `options`.
|
|
358
|
+
* `body` {Readable} The response body.
|
|
359
|
+
* `context` {Object}
|
|
360
|
+
* Returns: {Readable}
|
|
361
|
+
* Returns: {Duplex} A duplex stream that writes to the request and reads from the
|
|
362
|
+
response.
|
|
363
|
+
|
|
364
|
+
For easy use with [`stream.pipeline()`][]. The `handler` should return a
|
|
365
|
+
`Readable` from which the result is read. Usually it returns the `body` directly,
|
|
366
|
+
unless a transformation is needed based on, for example, `headers` or
|
|
367
|
+
`statusCode`. The `handler` should validate the response and save any required
|
|
368
|
+
state; if there is an error, it should be thrown.
|
|
369
|
+
|
|
370
|
+
```mjs
|
|
371
|
+
import { Readable, Writable, PassThrough, pipeline } from 'node:stream'
|
|
372
|
+
import { createServer } from 'node:http'
|
|
302
373
|
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' })
|
|
377
|
-
}, {
|
|
378
|
-
onRequestStart: () => {
|
|
379
|
-
console.log('Connected!')
|
|
380
|
-
},
|
|
381
|
-
onResponseError: (_controller, error) => {
|
|
382
|
-
console.error(error)
|
|
383
|
-
},
|
|
384
|
-
onResponseStart: (_controller, statusCode, headers) => {
|
|
385
|
-
console.log(`onResponseStart | statusCode: ${statusCode} | headers: ${JSON.stringify(headers)}`)
|
|
386
|
-
},
|
|
387
|
-
onResponseData: (_controller, chunk) => {
|
|
388
|
-
console.log('onResponseData: chunk received')
|
|
389
|
-
data.push(chunk)
|
|
390
|
-
},
|
|
391
|
-
onResponseEnd: (_controller, trailers) => {
|
|
392
|
-
console.log(`onResponseEnd | trailers: ${JSON.stringify(trailers)}`)
|
|
393
|
-
const res = Buffer.concat(data).toString('utf8')
|
|
394
|
-
console.log(`Response Data: ${res}`)
|
|
395
|
-
client.close()
|
|
396
|
-
server.close()
|
|
397
|
-
}
|
|
398
|
-
})
|
|
399
|
-
```
|
|
400
|
-
|
|
401
|
-
### `Dispatcher.pipeline(options, handler)`
|
|
402
|
-
|
|
403
|
-
For easy use with [stream.pipeline](https://nodejs.org/api/stream.html#streampipelinesource-transforms-destination-options). The `handler` argument should return a `Readable` from which the result will be read. Usually it should just return the `body` argument unless some kind of transformation needs to be performed based on e.g. `headers` or `statusCode`. The `handler` should validate the response and save any required state. If there is an error, it should be thrown. The function returns a `Duplex` which writes to the request and reads from the response.
|
|
404
|
-
|
|
405
|
-
Arguments:
|
|
406
|
-
|
|
407
|
-
* **options** `PipelineOptions`
|
|
408
|
-
* **handler** `(data: PipelineHandlerData) => stream.Readable`
|
|
409
|
-
|
|
410
|
-
Returns: `stream.Duplex`
|
|
411
|
-
|
|
412
|
-
#### Parameter: PipelineOptions
|
|
413
|
-
|
|
414
|
-
Extends: [`RequestOptions`](/docs/docs/api/Dispatcher.md#parameter-requestoptions)
|
|
415
|
-
|
|
416
|
-
* **objectMode** `boolean` (optional) - Default: `false` - Set to `true` if the `handler` will return an object stream.
|
|
417
|
-
|
|
418
|
-
#### Parameter: PipelineHandlerData
|
|
419
|
-
|
|
420
|
-
* **statusCode** `number`
|
|
421
|
-
* **headers** `Record<string, string | string[] | undefined>`
|
|
422
|
-
* **opaque** `unknown`
|
|
423
|
-
* **body** `stream.Readable`
|
|
424
|
-
* **context** `object`
|
|
425
|
-
* **onInfo** `({statusCode: number, headers: Record<string, string | string[]>}) => void | null` (optional) - Default: `null` - Callback collecting all the info headers (HTTP 100-199) received.
|
|
426
|
-
|
|
427
|
-
#### Example 1 - Pipeline Echo
|
|
428
|
-
|
|
429
|
-
```js
|
|
430
|
-
import { Readable, Writable, PassThrough, pipeline } from 'stream'
|
|
431
|
-
import { createServer } from 'http'
|
|
432
|
-
import { Client } from 'undici'
|
|
433
|
-
import { once } from 'events'
|
|
374
|
+
import { once } from 'node:events'
|
|
434
375
|
|
|
435
376
|
const server = createServer((request, response) => {
|
|
436
377
|
request.pipe(response)
|
|
@@ -449,10 +390,7 @@ pipeline(
|
|
|
449
390
|
this.push(null)
|
|
450
391
|
}
|
|
451
392
|
}),
|
|
452
|
-
client.pipeline({
|
|
453
|
-
path: '/',
|
|
454
|
-
method: 'GET'
|
|
455
|
-
}, ({ statusCode, headers, body }) => {
|
|
393
|
+
client.pipeline({ path: '/', method: 'GET' }, ({ statusCode, headers, body }) => {
|
|
456
394
|
console.log(`response received ${statusCode}`)
|
|
457
395
|
console.log('headers', headers)
|
|
458
396
|
return pipeline(body, new PassThrough(), () => {})
|
|
@@ -471,79 +409,72 @@ pipeline(
|
|
|
471
409
|
if (error) {
|
|
472
410
|
console.error(error)
|
|
473
411
|
}
|
|
474
|
-
|
|
475
412
|
client.close()
|
|
476
413
|
server.close()
|
|
477
414
|
}
|
|
478
415
|
)
|
|
479
416
|
```
|
|
480
417
|
|
|
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'
|
|
418
|
+
### `dispatcher.request(options[, callback])`
|
|
419
|
+
|
|
420
|
+
<!-- YAML
|
|
421
|
+
added: v4.0.0
|
|
422
|
+
-->
|
|
423
|
+
|
|
424
|
+
* `options` {Object} Accepts every field of [`dispatcher.dispatch()`][]'s
|
|
425
|
+
`options`, plus:
|
|
426
|
+
* `opaque` {any} A value passed through to the returned response data.
|
|
427
|
+
**Default:** `null`.
|
|
428
|
+
* `signal` {AbortSignal|EventEmitter|null} A signal used to abort the request.
|
|
429
|
+
**Default:** `null`.
|
|
430
|
+
* `onInfo` {Function|null} Invoked for each informational (1xx) response
|
|
431
|
+
received. **Default:** `null`.
|
|
432
|
+
* `info` {Object}
|
|
433
|
+
* `statusCode` {number}
|
|
434
|
+
* `headers` {Object}
|
|
435
|
+
* `responseHeaders` {string|null} Set to `'raw'` to return the response headers
|
|
436
|
+
as a raw array. **Default:** `null`.
|
|
437
|
+
* `highWaterMark` {number} The high water mark of the response body stream.
|
|
438
|
+
**Default:** `65536` (64 KiB).
|
|
439
|
+
* `callback` {Function} (optional) Invoked when the response is received.
|
|
440
|
+
* `error` {Error|null}
|
|
441
|
+
* `data` {Object}
|
|
442
|
+
* `statusCode` {number}
|
|
443
|
+
* `statusText` {string} The status message, e.g. `'OK'` or `'Not Found'`.
|
|
444
|
+
* `headers` {Object} The response headers. All keys are lower-cased.
|
|
445
|
+
* `body` {Readable} The response body, which also implements the
|
|
446
|
+
[body mixin][] from the Fetch Standard.
|
|
447
|
+
* `trailers` {Object} Starts out empty and is mutated to contain the trailers
|
|
448
|
+
after `body` emits `'end'`.
|
|
449
|
+
* `opaque` {any} The `opaque` value passed in `options`.
|
|
450
|
+
* `context` {Object}
|
|
451
|
+
* Returns: {Promise} A `Promise` is returned only when `callback` is omitted. It
|
|
452
|
+
resolves with the `data` object described above.
|
|
453
|
+
|
|
454
|
+
Performs an HTTP request. `options.method` must not be `'CONNECT'`.
|
|
455
|
+
|
|
456
|
+
Non-idempotent requests are not pipelined, to avoid indirect failures. Idempotent
|
|
457
|
+
requests are automatically retried if they fail due to an indirect failure from
|
|
458
|
+
the request at the head of the pipeline; this does not apply to idempotent
|
|
459
|
+
requests with a stream request body. All response bodies must always be fully
|
|
460
|
+
consumed or destroyed.
|
|
461
|
+
|
|
462
|
+
The `body` exposes the following [body mixin][] methods and properties:
|
|
463
|
+
[`.arrayBuffer()`][], [`.blob()`][], [`.bytes()`][], [`.json()`][],
|
|
464
|
+
[`.text()`][], `body`, and `bodyUsed`. A body cannot be consumed twice; for
|
|
465
|
+
example, calling `text()` after `json()` throws a `TypeError`. The body also
|
|
466
|
+
provides `dump({ limit })`, which discards up to `limit` bytes (default
|
|
467
|
+
`131072`) without destroying the socket.
|
|
468
|
+
|
|
469
|
+
The body is always a `Readable`, even when empty. Deserializing an empty body
|
|
470
|
+
with `json()` throws. To guard against this, verify the status code is not `204`
|
|
471
|
+
and the `content-type` header starts with `application/json` before calling
|
|
472
|
+
`json()`.
|
|
473
|
+
|
|
474
|
+
```mjs
|
|
475
|
+
import { createServer } from 'node:http'
|
|
545
476
|
import { Client } from 'undici'
|
|
546
|
-
import { once } from 'events'
|
|
477
|
+
import { once } from 'node:events'
|
|
547
478
|
|
|
548
479
|
const server = createServer((request, response) => {
|
|
549
480
|
response.end('Hello, World!')
|
|
@@ -574,107 +505,28 @@ try {
|
|
|
574
505
|
}
|
|
575
506
|
```
|
|
576
507
|
|
|
577
|
-
|
|
508
|
+
A request can be aborted with an `AbortSignal`, with any `EventEmitter` that
|
|
509
|
+
emits an `'abort'` event, or by destroying the response body:
|
|
578
510
|
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
```js
|
|
582
|
-
import { createServer } from 'http'
|
|
511
|
+
```mjs
|
|
583
512
|
import { Client } from 'undici'
|
|
584
|
-
import { once } from 'events'
|
|
585
|
-
|
|
586
|
-
const server = createServer((request, response) => {
|
|
587
|
-
response.end('Hello, World!')
|
|
588
|
-
}).listen()
|
|
589
|
-
|
|
590
|
-
await once(server, 'listening')
|
|
591
513
|
|
|
592
|
-
const client = new Client(
|
|
514
|
+
const client = new Client('http://localhost:3000')
|
|
593
515
|
const abortController = new AbortController()
|
|
594
516
|
|
|
595
517
|
try {
|
|
596
|
-
client.request({
|
|
597
|
-
path: '/',
|
|
598
|
-
method: 'GET',
|
|
599
|
-
signal: abortController.signal
|
|
600
|
-
})
|
|
518
|
+
client.request({ path: '/', method: 'GET', signal: abortController.signal })
|
|
601
519
|
} catch (error) {
|
|
602
|
-
console.error(error) //
|
|
603
|
-
client.close()
|
|
604
|
-
server.close()
|
|
520
|
+
console.error(error) // RequestAbortedError
|
|
605
521
|
}
|
|
606
522
|
|
|
607
523
|
abortController.abort()
|
|
608
524
|
```
|
|
609
525
|
|
|
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.
|
|
642
|
-
|
|
643
|
-
```js
|
|
644
|
-
import { createServer } from 'http'
|
|
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
|
|
526
|
+
When the response body is conditionally read, always fully consume it otherwise:
|
|
670
527
|
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
```js
|
|
674
|
-
const { body, statusCode } = await client.request({
|
|
675
|
-
path: '/',
|
|
676
|
-
method: 'GET'
|
|
677
|
-
})
|
|
528
|
+
```mjs
|
|
529
|
+
const { body, statusCode } = await client.request({ path: '/', method: 'GET' })
|
|
678
530
|
|
|
679
531
|
if (statusCode === 200) {
|
|
680
532
|
return await body.arrayBuffer()
|
|
@@ -685,40 +537,42 @@ await body.dump()
|
|
|
685
537
|
return null
|
|
686
538
|
```
|
|
687
539
|
|
|
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
|
-
|
|
540
|
+
### `dispatcher.stream(options, factory[, callback])`
|
|
541
|
+
|
|
542
|
+
<!-- YAML
|
|
543
|
+
added: v4.0.0
|
|
544
|
+
-->
|
|
545
|
+
|
|
546
|
+
* `options` {Object} Accepts the same fields as [`dispatcher.request()`][]'s
|
|
547
|
+
`options`.
|
|
548
|
+
* `factory` {Function} Returns the `Writable` to which the response is written.
|
|
549
|
+
* `data` {Object}
|
|
550
|
+
* `statusCode` {number}
|
|
551
|
+
* `headers` {Object} The response headers.
|
|
552
|
+
* `opaque` {any} The `opaque` value passed in `options`.
|
|
553
|
+
* `context` {Object}
|
|
554
|
+
* Returns: {Writable}
|
|
555
|
+
* `callback` {Function} (optional) Invoked when the request has completed.
|
|
556
|
+
* `error` {Error|null}
|
|
557
|
+
* `data` {Object}
|
|
558
|
+
* `opaque` {any} The `opaque` value passed in `options`.
|
|
559
|
+
* `trailers` {Object}
|
|
560
|
+
* Returns: {Promise} A `Promise` is returned only when `callback` is omitted. It
|
|
561
|
+
resolves with the `data` object described above.
|
|
562
|
+
|
|
563
|
+
A faster version of [`dispatcher.request()`][]. The `factory` returns a
|
|
564
|
+
[`Writable`][] to which the response is written, which avoids creating an
|
|
565
|
+
intermediate [`Readable`][] when the caller intends to pipe the response body
|
|
566
|
+
directly to a `Writable`.
|
|
567
|
+
|
|
568
|
+
Use `options.opaque` to avoid creating a closure for the `factory` method. This
|
|
569
|
+
pattern works well with Node.js web frameworks such as [Fastify][].
|
|
570
|
+
|
|
571
|
+
```mjs
|
|
572
|
+
import { createServer } from 'node:http'
|
|
719
573
|
import { Client } from 'undici'
|
|
720
|
-
import { once } from 'events'
|
|
721
|
-
import { Writable } from 'stream'
|
|
574
|
+
import { once } from 'node:events'
|
|
575
|
+
import { Writable } from 'node:stream'
|
|
722
576
|
|
|
723
577
|
const server = createServer((request, response) => {
|
|
724
578
|
response.end('Hello, World!')
|
|
@@ -755,95 +609,38 @@ try {
|
|
|
755
609
|
}
|
|
756
610
|
```
|
|
757
611
|
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
612
|
+
### `dispatcher.upgrade(options[, callback])`
|
|
613
|
+
|
|
614
|
+
<!-- YAML
|
|
615
|
+
added: v4.0.0
|
|
616
|
+
-->
|
|
617
|
+
|
|
618
|
+
* `options` {Object}
|
|
619
|
+
* `path` {string} The request path.
|
|
620
|
+
* `method` {string} The HTTP method. **Default:** `'GET'`.
|
|
621
|
+
* `headers` {UndiciHeaders} Request headers. **Default:** `null`.
|
|
622
|
+
* `protocol` {string} A comma-separated list of protocols, in descending
|
|
623
|
+
preference order. **Default:** `'Websocket'`.
|
|
624
|
+
* `signal` {AbortSignal|EventEmitter|null} A signal used to abort the request.
|
|
625
|
+
**Default:** `null`.
|
|
626
|
+
* `responseHeaders` {string|null} Set to `'raw'` to return the response headers
|
|
627
|
+
as a raw array. **Default:** `null`.
|
|
628
|
+
* `callback` {Function} (optional) Invoked when the upgrade completes.
|
|
629
|
+
* `error` {Error|null}
|
|
630
|
+
* `data` {Object}
|
|
631
|
+
* `headers` {Object} The response headers.
|
|
632
|
+
* `socket` {Duplex} The upgraded socket.
|
|
633
|
+
* `opaque` {any}
|
|
634
|
+
* Returns: {Promise} A `Promise` is returned only when `callback` is omitted. It
|
|
635
|
+
resolves with the `data` object described above.
|
|
636
|
+
|
|
637
|
+
Upgrades to a different protocol. See
|
|
638
|
+
[MDN: Protocol upgrade mechanism][] for more details.
|
|
639
|
+
|
|
640
|
+
```mjs
|
|
641
|
+
import { createServer } from 'node:http'
|
|
764
642
|
import { Client } from 'undici'
|
|
765
|
-
import { once } from 'events'
|
|
766
|
-
import fastify from 'fastify'
|
|
767
|
-
|
|
768
|
-
const nodeServer = createServer((request, response) => {
|
|
769
|
-
response.end('Hello, World! From Node.js HTTP Server')
|
|
770
|
-
}).listen()
|
|
771
|
-
|
|
772
|
-
await once(nodeServer, 'listening')
|
|
773
|
-
|
|
774
|
-
console.log('Node Server listening')
|
|
775
|
-
|
|
776
|
-
const nodeServerUndiciClient = new Client(`http://localhost:${nodeServer.address().port}`)
|
|
777
|
-
|
|
778
|
-
const fastifyServer = fastify()
|
|
779
|
-
|
|
780
|
-
fastifyServer.route({
|
|
781
|
-
url: '/',
|
|
782
|
-
method: 'GET',
|
|
783
|
-
handler: (request, response) => {
|
|
784
|
-
nodeServerUndiciClient.stream({
|
|
785
|
-
path: '/',
|
|
786
|
-
method: 'GET',
|
|
787
|
-
opaque: response
|
|
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'
|
|
845
|
-
import { Client } from 'undici'
|
|
846
|
-
import { once } from 'events'
|
|
643
|
+
import { once } from 'node:events'
|
|
847
644
|
|
|
848
645
|
const server = createServer((request, response) => {
|
|
849
646
|
response.statusCode = 101
|
|
@@ -857,9 +654,7 @@ await once(server, 'listening')
|
|
|
857
654
|
const client = new Client(`http://localhost:${server.address().port}`)
|
|
858
655
|
|
|
859
656
|
try {
|
|
860
|
-
const { headers, socket } = await client.upgrade({
|
|
861
|
-
path: '/',
|
|
862
|
-
})
|
|
657
|
+
const { headers, socket } = await client.upgrade({ path: '/' })
|
|
863
658
|
socket.on('end', () => {
|
|
864
659
|
console.log(`upgrade: ${headers.upgrade}`) // upgrade: Websocket
|
|
865
660
|
client.close()
|
|
@@ -873,109 +668,62 @@ try {
|
|
|
873
668
|
}
|
|
874
669
|
```
|
|
875
670
|
|
|
876
|
-
### `
|
|
877
|
-
|
|
878
|
-
Compose a new dispatcher from the current dispatcher and the given interceptors.
|
|
879
|
-
|
|
880
|
-
> _Notes_:
|
|
881
|
-
> - The order of the interceptors matters. The last interceptor will be the first to be called.
|
|
882
|
-
> - It is important to note that the `interceptor` function should return a function that follows the `Dispatcher.dispatch` signature.
|
|
883
|
-
> - Any fork of the chain of `interceptors` can lead to unexpected results.
|
|
884
|
-
>
|
|
885
|
-
> **Interceptor Stack Visualization:**
|
|
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
|
-
> ```
|
|
671
|
+
### `dispatcher.compose(interceptors[, interceptor])`
|
|
903
672
|
|
|
904
|
-
|
|
673
|
+
<!-- YAML
|
|
674
|
+
added: v6.9.0
|
|
675
|
+
-->
|
|
905
676
|
|
|
906
|
-
*
|
|
677
|
+
* `interceptors` {Array|Function} An array of interceptor functions, or the first
|
|
678
|
+
of several interceptors passed as separate arguments.
|
|
679
|
+
* `...interceptor` {Function} Additional interceptors when passed as separate
|
|
680
|
+
arguments.
|
|
681
|
+
* Returns: {Dispatcher} A new dispatcher that applies the interceptors over the
|
|
682
|
+
current dispatcher's [`dispatch()`][] method.
|
|
907
683
|
|
|
908
|
-
|
|
684
|
+
Composes a new dispatcher from the current dispatcher and the given
|
|
685
|
+
interceptors. Each interceptor is a function that takes a `dispatch` method and
|
|
686
|
+
returns a `dispatch`-like function with the same signature
|
|
687
|
+
(`(options, handler)`).
|
|
909
688
|
|
|
910
|
-
|
|
689
|
+
The order of the interceptors matters: the last interceptor in the chain is the
|
|
690
|
+
first to be called. Forking the chain of interceptors can lead to unexpected
|
|
691
|
+
results.
|
|
911
692
|
|
|
912
|
-
|
|
693
|
+
```text
|
|
694
|
+
compose([interceptor1, interceptor2, interceptor3])
|
|
913
695
|
|
|
914
|
-
|
|
696
|
+
Request flow:
|
|
697
|
+
Request -> interceptor3 -> interceptor2 -> interceptor1 -> dispatcher.dispatch
|
|
698
|
+
(called first) (called second) (called last)
|
|
699
|
+
```
|
|
915
700
|
|
|
916
|
-
```
|
|
917
|
-
|
|
701
|
+
```mjs
|
|
702
|
+
import { Client, RedirectHandler } from 'undici'
|
|
918
703
|
|
|
919
704
|
const redirectInterceptor = dispatch => {
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
if (!maxRedirections) {
|
|
924
|
-
return dispatch(opts, handler)
|
|
925
|
-
}
|
|
705
|
+
return (opts, handler) => {
|
|
706
|
+
const { maxRedirections } = opts
|
|
926
707
|
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
maxRedirections,
|
|
930
|
-
opts,
|
|
931
|
-
handler
|
|
932
|
-
)
|
|
933
|
-
opts = { ...opts, maxRedirections: 0 } // Stop sub dispatcher from also redirecting.
|
|
934
|
-
return dispatch(opts, redirectHandler)
|
|
708
|
+
if (!maxRedirections) {
|
|
709
|
+
return dispatch(opts, handler)
|
|
935
710
|
}
|
|
711
|
+
|
|
712
|
+
const redirectHandler = new RedirectHandler(dispatch, maxRedirections, opts, handler)
|
|
713
|
+
opts = { ...opts, maxRedirections: 0 } // Stop the sub-dispatcher from also redirecting.
|
|
714
|
+
return dispatch(opts, redirectHandler)
|
|
715
|
+
}
|
|
936
716
|
}
|
|
937
717
|
|
|
938
|
-
const client = new Client('http://localhost:3000')
|
|
939
|
-
.compose(redirectInterceptor)
|
|
718
|
+
const client = new Client('http://localhost:3000').compose(redirectInterceptor)
|
|
940
719
|
|
|
941
720
|
await client.request({ path: '/', method: 'GET' })
|
|
942
721
|
```
|
|
943
722
|
|
|
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
|
-
}
|
|
723
|
+
Interceptors can be chained by calling `compose()` repeatedly:
|
|
967
724
|
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
return dispatch(
|
|
971
|
-
opts,
|
|
972
|
-
new RetryHandler(opts, {
|
|
973
|
-
handler,
|
|
974
|
-
dispatch
|
|
975
|
-
})
|
|
976
|
-
)
|
|
977
|
-
}
|
|
978
|
-
}
|
|
725
|
+
```mjs
|
|
726
|
+
import { Client } from 'undici'
|
|
979
727
|
|
|
980
728
|
const client = new Client('http://localhost:3000')
|
|
981
729
|
.compose(redirectInterceptor)
|
|
@@ -984,461 +732,96 @@ const client = new Client('http://localhost:3000')
|
|
|
984
732
|
await client.request({ path: '/', method: 'GET' })
|
|
985
733
|
```
|
|
986
734
|
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
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
|
|
735
|
+
For the full list of built-in interceptors provided by undici, see [Interceptors](Interceptors.md).
|
|
736
|
+
For an example of a custom interceptor that wraps handler callbacks, see
|
|
737
|
+
[Custom interceptors](Interceptors.md#custom-interceptors).
|
|
1322
738
|
|
|
1323
739
|
### Event: `'connect'`
|
|
1324
740
|
|
|
1325
|
-
|
|
741
|
+
<!-- YAML
|
|
742
|
+
added: v4.0.0
|
|
743
|
+
-->
|
|
744
|
+
|
|
745
|
+
* `origin` {URL}
|
|
746
|
+
* `targets` {Array<Dispatcher>}
|
|
1326
747
|
|
|
1327
|
-
|
|
1328
|
-
* **targets** `Array<Dispatcher>`
|
|
748
|
+
Emitted when the dispatcher has connected to the origin.
|
|
1329
749
|
|
|
1330
750
|
### Event: `'disconnect'`
|
|
1331
751
|
|
|
1332
|
-
|
|
752
|
+
<!-- YAML
|
|
753
|
+
added: v4.0.0
|
|
754
|
+
-->
|
|
1333
755
|
|
|
1334
|
-
*
|
|
1335
|
-
*
|
|
1336
|
-
*
|
|
756
|
+
* `origin` {URL}
|
|
757
|
+
* `targets` {Array<Dispatcher>}
|
|
758
|
+
* `error` {Error}
|
|
1337
759
|
|
|
1338
760
|
Emitted when the dispatcher has been disconnected from the origin.
|
|
1339
761
|
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
762
|
+
For HTTP/2, this event is also emitted when the dispatcher receives a
|
|
763
|
+
[GOAWAY frame][], with an error whose message is `HTTP/2: "GOAWAY" frame received`
|
|
764
|
+
and whose code is `UND_ERR_INFO`. Because of the binary-framing nature of the
|
|
765
|
+
protocol, a request may hang if a frame is received between the `HEADER` and
|
|
766
|
+
`DATA` frames. It is recommended to handle this event and close the dispatcher to
|
|
767
|
+
create a new HTTP/2 session.
|
|
1343
768
|
|
|
1344
769
|
### Event: `'connectionError'`
|
|
1345
770
|
|
|
1346
|
-
|
|
771
|
+
<!-- YAML
|
|
772
|
+
added: v4.0.0
|
|
773
|
+
-->
|
|
1347
774
|
|
|
1348
|
-
*
|
|
1349
|
-
*
|
|
1350
|
-
*
|
|
775
|
+
* `origin` {URL}
|
|
776
|
+
* `targets` {Array<Dispatcher>}
|
|
777
|
+
* `error` {Error}
|
|
1351
778
|
|
|
1352
|
-
Emitted when dispatcher fails to connect to
|
|
1353
|
-
origin.
|
|
779
|
+
Emitted when the dispatcher fails to connect to the origin.
|
|
1354
780
|
|
|
1355
781
|
### Event: `'drain'`
|
|
1356
782
|
|
|
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
|
-
```js
|
|
1403
|
-
[
|
|
1404
|
-
'content-length', '123',
|
|
1405
|
-
'content-type', 'text/plain',
|
|
1406
|
-
'connection', 'keep-alive',
|
|
1407
|
-
'host', 'mysite.com',
|
|
1408
|
-
'accept', '*/*'
|
|
1409
|
-
]
|
|
1410
|
-
```
|
|
1411
|
-
|
|
1412
|
-
### Example 3 - Iterable
|
|
1413
|
-
|
|
1414
|
-
```js
|
|
1415
|
-
new Headers({
|
|
1416
|
-
'content-length': '123',
|
|
1417
|
-
'content-type': 'text/plain',
|
|
1418
|
-
connection: 'keep-alive',
|
|
1419
|
-
host: 'mysite.com',
|
|
1420
|
-
accept: '*/*'
|
|
1421
|
-
})
|
|
1422
|
-
```
|
|
1423
|
-
or
|
|
1424
|
-
```js
|
|
1425
|
-
new Map([
|
|
1426
|
-
['content-length', '123'],
|
|
1427
|
-
['content-type', 'text/plain'],
|
|
1428
|
-
['connection', 'keep-alive'],
|
|
1429
|
-
['host', 'mysite.com'],
|
|
1430
|
-
['accept', '*/*']
|
|
1431
|
-
])
|
|
1432
|
-
```
|
|
1433
|
-
or
|
|
1434
|
-
```js
|
|
1435
|
-
{
|
|
1436
|
-
*[Symbol.iterator] () {
|
|
1437
|
-
yield ['content-length', '123']
|
|
1438
|
-
yield ['content-type', 'text/plain']
|
|
1439
|
-
yield ['connection', 'keep-alive']
|
|
1440
|
-
yield ['host', 'mysite.com']
|
|
1441
|
-
yield ['accept', '*/*']
|
|
1442
|
-
}
|
|
1443
|
-
}
|
|
1444
|
-
```
|
|
783
|
+
<!-- YAML
|
|
784
|
+
added: v4.0.0
|
|
785
|
+
-->
|
|
786
|
+
|
|
787
|
+
* `origin` {URL}
|
|
788
|
+
|
|
789
|
+
Emitted when the dispatcher is no longer busy and further [`dispatch()`][] calls
|
|
790
|
+
can make progress.
|
|
791
|
+
|
|
792
|
+
## Pre-built interceptors
|
|
793
|
+
|
|
794
|
+
For the full reference of built-in interceptors (`dump`, `retry`, `redirect`,
|
|
795
|
+
`decompress`, `responseError`, `dns`, `cache`, `deduplicate`) and their options,
|
|
796
|
+
see [Interceptors](Interceptors.md).
|
|
797
|
+
|
|
798
|
+
[Fastify]: https://fastify.dev
|
|
799
|
+
[GOAWAY frame]: https://webconcepts.info/concepts/http2-frame-type/0x7
|
|
800
|
+
[HTTP `CONNECT`]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/CONNECT
|
|
801
|
+
[MDN: Protocol upgrade mechanism]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Protocol_upgrade_mechanism
|
|
802
|
+
[`.arrayBuffer()`]: https://fetch.spec.whatwg.org/#dom-body-arraybuffer
|
|
803
|
+
[`.blob()`]: https://fetch.spec.whatwg.org/#dom-body-blob
|
|
804
|
+
[`.bytes()`]: https://fetch.spec.whatwg.org/#dom-body-bytes
|
|
805
|
+
[`.json()`]: https://fetch.spec.whatwg.org/#dom-body-json
|
|
806
|
+
[`.text()`]: https://fetch.spec.whatwg.org/#dom-body-text
|
|
807
|
+
[`'drain'`]: #event-drain
|
|
808
|
+
[`Agent`]: Agent.md#class-agent
|
|
809
|
+
[`BalancedPool`]: BalancedPool.md#class-balancedpool
|
|
810
|
+
[`Client`]: Client.md#class-client
|
|
811
|
+
[`EventEmitter`]: https://nodejs.org/api/events.html#class-eventemitter
|
|
812
|
+
[`Pool`]: Pool.md#class-pool
|
|
813
|
+
[`Readable`]: https://nodejs.org/api/stream.html#class-streamreadable
|
|
814
|
+
[`Writable`]: https://nodejs.org/api/stream.html#class-streamwritable
|
|
815
|
+
[`close()`]: #dispatcherclosecallback
|
|
816
|
+
[`connect()`]: #dispatcherconnectoptions-callback
|
|
817
|
+
[`destroy()`]: #dispatcherdestroyerror-callback
|
|
818
|
+
[`dispatch()`]: #dispatcherdispatchoptions-handler
|
|
819
|
+
[`dispatcher.compose()`]: #dispatchercomposeinterceptors-interceptor
|
|
820
|
+
[`dispatcher.dispatch()`]: #dispatcherdispatchoptions-handler
|
|
821
|
+
[`dispatcher.request()`]: #dispatcherrequestoptions-callback
|
|
822
|
+
[`pipeline()`]: #dispatcherpipelineoptions-handler
|
|
823
|
+
[`request()`]: #dispatcherrequestoptions-callback
|
|
824
|
+
[`stream()`]: #dispatcherstreamoptions-factory-callback
|
|
825
|
+
[`stream.pipeline()`]: https://nodejs.org/api/stream.html#streampipelinesource-transforms-destination-options
|
|
826
|
+
[`upgrade()`]: #dispatcherupgradeoptions-callback
|
|
827
|
+
[body mixin]: https://fetch.spec.whatwg.org/#body-mixin
|