undici 8.5.0 → 8.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/docs/docs/api/Agent.md +248 -44
- package/docs/docs/api/BalancedPool.md +246 -48
- package/docs/docs/api/CacheStorage.md +171 -13
- package/docs/docs/api/CacheStore.md +294 -98
- package/docs/docs/api/Client.md +365 -179
- package/docs/docs/api/ClientStats.md +80 -14
- package/docs/docs/api/Connector.md +118 -36
- package/docs/docs/api/ContentType.md +65 -25
- package/docs/docs/api/Cookies.md +122 -71
- package/docs/docs/api/Debug.md +34 -18
- package/docs/docs/api/DiagnosticsChannel.md +364 -164
- package/docs/docs/api/Dispatcher.md +479 -1102
- package/docs/docs/api/EnvHttpProxyAgent.md +101 -87
- package/docs/docs/api/Errors.md +503 -40
- package/docs/docs/api/EventSource.md +294 -32
- package/docs/docs/api/Fetch.md +680 -31
- package/docs/docs/api/GlobalInstallation.md +78 -98
- package/docs/docs/api/H2CClient.md +298 -176
- package/docs/docs/api/Interceptors.md +355 -0
- package/docs/docs/api/MockAgent.md +314 -322
- package/docs/docs/api/MockCallHistory.md +272 -98
- package/docs/docs/api/MockCallHistoryLog.md +189 -26
- package/docs/docs/api/MockClient.md +151 -33
- package/docs/docs/api/MockErrors.md +56 -5
- package/docs/docs/api/MockPool.md +299 -263
- package/docs/docs/api/Pool.md +235 -43
- package/docs/docs/api/PoolStats.md +119 -13
- package/docs/docs/api/ProxyAgent.md +179 -160
- package/docs/docs/api/RedirectHandler.md +238 -63
- package/docs/docs/api/RetryAgent.md +113 -26
- package/docs/docs/api/RetryHandler.md +161 -77
- package/docs/docs/api/RoundRobinPool.md +166 -72
- package/docs/docs/api/SnapshotAgent.md +264 -542
- package/docs/docs/api/Socks5ProxyAgent.md +162 -187
- package/docs/docs/api/Util.md +68 -11
- package/docs/docs/api/WebSocket.md +475 -80
- package/docs/docs/api/api-lifecycle.md +100 -32
- package/docs/docs/best-practices/client-certificate.md +2 -0
- package/docs/docs/best-practices/crawling.md +3 -1
- package/docs/docs/best-practices/migrating-from-v7-to-v8.md +6 -4
- package/docs/docs/best-practices/mocking-request.md +9 -7
- package/docs/docs/best-practices/proxy.md +3 -1
- package/docs/docs/best-practices/undici-vs-builtin-fetch.md +10 -8
- package/docs/docs/best-practices/writing-tests.md +2 -0
- package/docs/docs/{GettingStarted.md → getting-started.md} +22 -19
- package/docs/docs/index.md +779 -0
- package/docs/docs/site.json +125 -0
- package/docs/docs/type-map.json +79 -0
- package/lib/api/api-request.js +7 -1
- package/lib/api/readable.js +47 -2
- package/lib/core/errors.js +20 -0
- package/lib/core/request.js +1 -1
- package/lib/core/util.js +24 -1
- package/lib/dispatcher/client-h1.js +23 -0
- package/lib/dispatcher/client-h2.js +243 -111
- package/lib/dispatcher/proxy-agent.js +40 -6
- package/lib/handler/redirect-handler.js +1 -0
- package/lib/handler/retry-handler.js +57 -19
- package/lib/web/cookies/parse.js +3 -2
- package/lib/web/cookies/util.js +1 -1
- package/lib/web/eventsource/util.js +1 -1
- package/lib/web/fetch/constants.js +1 -1
- package/lib/web/fetch/index.js +11 -2
- package/lib/web/fetch/util.js +4 -1
- package/package.json +1 -1
- package/types/client.d.ts +1 -1
- package/types/cookies.d.ts +1 -1
- package/types/errors.d.ts +10 -0
- package/types/handlers.d.ts +2 -0
- package/types/proxy-agent.d.ts +7 -0
|
@@ -1,99 +1,297 @@
|
|
|
1
|
-
#
|
|
1
|
+
# BalancedPool
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<!--introduced_in=v4.8.0-->
|
|
4
|
+
<!--type=module-->
|
|
5
|
+
<!-- source_link=lib/dispatcher/balanced-pool.js -->
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
> Stability: 2 - Stable
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
A `BalancedPool` distributes requests across a set of {Pool} instances, each
|
|
10
|
+
connected to a different upstream. It uses a weighted round-robin algorithm:
|
|
11
|
+
every upstream starts with the same weight, and the weight is adjusted up on
|
|
12
|
+
successful connections and down on connection errors, so that healthy upstreams
|
|
13
|
+
receive a larger share of traffic.
|
|
8
14
|
|
|
9
|
-
|
|
15
|
+
Requests are not guaranteed to be dispatched in the order they are issued.
|
|
10
16
|
|
|
11
|
-
|
|
17
|
+
```mjs
|
|
18
|
+
import { BalancedPool } from 'undici'
|
|
12
19
|
|
|
13
|
-
|
|
14
|
-
|
|
20
|
+
const pool = new BalancedPool([
|
|
21
|
+
'http://localhost:3000',
|
|
22
|
+
'http://localhost:3001',
|
|
23
|
+
'http://localhost:3002',
|
|
24
|
+
])
|
|
15
25
|
|
|
16
|
-
|
|
26
|
+
const { statusCode, body } = await pool.request({
|
|
27
|
+
path: '/',
|
|
28
|
+
method: 'GET',
|
|
29
|
+
})
|
|
30
|
+
console.log('response received', statusCode)
|
|
31
|
+
for await (const chunk of body) {
|
|
32
|
+
console.log('data', chunk.toString())
|
|
33
|
+
}
|
|
34
|
+
```
|
|
17
35
|
|
|
18
|
-
|
|
36
|
+
## Class: `BalancedPool`
|
|
19
37
|
|
|
20
|
-
|
|
38
|
+
<!-- YAML
|
|
39
|
+
added: v4.8.0
|
|
40
|
+
changes:
|
|
41
|
+
- version: v4.10.4
|
|
42
|
+
pr-url: https://github.com/nodejs/undici/pull/1114
|
|
43
|
+
description: Introduced the `BalancedPool` class.
|
|
44
|
+
-->
|
|
21
45
|
|
|
22
|
-
|
|
23
|
-
## Instance Properties
|
|
46
|
+
* Extends: {Dispatcher}
|
|
24
47
|
|
|
25
|
-
|
|
48
|
+
A pool of {Pool} instances connected to multiple upstreams.
|
|
26
49
|
|
|
27
|
-
|
|
50
|
+
### `new BalancedPool(upstreams[, options])`
|
|
28
51
|
|
|
29
|
-
|
|
52
|
+
<!-- YAML
|
|
53
|
+
added: v4.8.0
|
|
54
|
+
changes:
|
|
55
|
+
- version: v4.15.0
|
|
56
|
+
pr-url: https://github.com/nodejs/undici/pull/1237
|
|
57
|
+
description: Added the `factory` option.
|
|
58
|
+
-->
|
|
30
59
|
|
|
31
|
-
|
|
60
|
+
* `upstreams` {URL|string|URL[]|string[]} One or more upstream origins. Each
|
|
61
|
+
value should only include the **protocol, hostname, and port**. **Default:**
|
|
62
|
+
`[]`.
|
|
63
|
+
* `options` {BalancedPoolOptions} (optional)
|
|
32
64
|
|
|
33
|
-
|
|
65
|
+
The options passed to the constructor are forwarded to every {Pool} instance
|
|
66
|
+
that is created for an upstream.
|
|
34
67
|
|
|
35
|
-
|
|
68
|
+
#### Parameter: `BalancedPoolOptions`
|
|
36
69
|
|
|
37
|
-
|
|
70
|
+
Extends: {PoolOptions}
|
|
38
71
|
|
|
39
|
-
|
|
72
|
+
In addition to all {PoolOptions}, the following options are supported:
|
|
40
73
|
|
|
41
|
-
|
|
74
|
+
* `factory` {Function} A function used to create the {Pool} instance for each
|
|
75
|
+
upstream. **Default:** `(origin, opts) => new Pool(origin, opts)`.
|
|
76
|
+
* `origin` {URL|string} The upstream origin.
|
|
77
|
+
* `opts` {Object} The options object derived from the constructor options.
|
|
78
|
+
* Returns: {Dispatcher}
|
|
79
|
+
* `maxWeightPerServer` {number} The maximum weight any single upstream can
|
|
80
|
+
reach. Successful connections increase an upstream's weight up to this value.
|
|
81
|
+
**Default:** `100`.
|
|
82
|
+
* `errorPenalty` {number} The amount by which an upstream's weight is decreased
|
|
83
|
+
on a connection error and increased on a successful connection. **Default:**
|
|
84
|
+
`15`.
|
|
42
85
|
|
|
43
|
-
### `
|
|
86
|
+
### `balancedPool.addUpstream(upstream)`
|
|
44
87
|
|
|
45
|
-
|
|
88
|
+
<!-- YAML
|
|
89
|
+
added: v4.8.0
|
|
90
|
+
-->
|
|
46
91
|
|
|
47
|
-
|
|
92
|
+
* `upstream` {URL|string} The upstream origin to add. It should only include the
|
|
93
|
+
**protocol, hostname, and port**.
|
|
94
|
+
* Returns: {BalancedPool} The `BalancedPool` instance, for chaining.
|
|
48
95
|
|
|
49
|
-
|
|
96
|
+
Adds an upstream. If an open, non-destroyed upstream with the same origin is
|
|
97
|
+
already present, the call is a no-op.
|
|
50
98
|
|
|
51
|
-
### `
|
|
99
|
+
### `balancedPool.removeUpstream(upstream)`
|
|
52
100
|
|
|
53
|
-
|
|
101
|
+
<!-- YAML
|
|
102
|
+
added: v4.8.0
|
|
103
|
+
-->
|
|
54
104
|
|
|
55
|
-
|
|
105
|
+
* `upstream` {URL|string} The upstream origin to remove. It should only include
|
|
106
|
+
the **protocol, hostname, and port**.
|
|
107
|
+
* Returns: {BalancedPool} The `BalancedPool` instance, for chaining.
|
|
56
108
|
|
|
57
|
-
|
|
109
|
+
Removes an upstream that was previously added. If no matching upstream is found,
|
|
110
|
+
the call is a no-op.
|
|
58
111
|
|
|
59
|
-
### `
|
|
112
|
+
### `balancedPool.getUpstream(upstream)`
|
|
60
113
|
|
|
61
|
-
|
|
114
|
+
<!-- YAML
|
|
115
|
+
added: v7.17.0
|
|
116
|
+
-->
|
|
62
117
|
|
|
63
|
-
|
|
118
|
+
* `upstream` {URL|string} The upstream origin to look up. It should only include
|
|
119
|
+
the **protocol, hostname, and port**.
|
|
120
|
+
* Returns: {Pool|undefined} The {Pool} instance managing the given upstream, or
|
|
121
|
+
`undefined` if it is not present.
|
|
64
122
|
|
|
65
|
-
|
|
123
|
+
Returns the {Pool} instance for the given upstream origin, if it is open and not
|
|
124
|
+
destroyed.
|
|
66
125
|
|
|
67
|
-
### `
|
|
126
|
+
### `balancedPool.upstreams`
|
|
68
127
|
|
|
69
|
-
|
|
128
|
+
<!-- YAML
|
|
129
|
+
added: v4.8.0
|
|
130
|
+
-->
|
|
70
131
|
|
|
71
|
-
|
|
132
|
+
* Type: {string[]} The origins of the upstreams that are currently open and not
|
|
133
|
+
destroyed.
|
|
72
134
|
|
|
73
|
-
|
|
135
|
+
### `balancedPool.closed`
|
|
74
136
|
|
|
75
|
-
|
|
137
|
+
<!-- YAML
|
|
138
|
+
added: v4.8.0
|
|
139
|
+
changes:
|
|
140
|
+
- version: v4.10.4
|
|
141
|
+
pr-url: https://github.com/nodejs/undici/pull/1114
|
|
142
|
+
description: Introduced the `BalancedPool` class.
|
|
143
|
+
-->
|
|
76
144
|
|
|
77
|
-
|
|
145
|
+
* Type: {boolean} `true` after `balancedPool.close()` has been called.
|
|
78
146
|
|
|
79
|
-
|
|
147
|
+
Implements [`client.closed`][].
|
|
80
148
|
|
|
81
|
-
|
|
149
|
+
### `balancedPool.destroyed`
|
|
82
150
|
|
|
83
|
-
|
|
151
|
+
<!-- YAML
|
|
152
|
+
added: v4.8.0
|
|
153
|
+
changes:
|
|
154
|
+
- version: v4.10.4
|
|
155
|
+
pr-url: https://github.com/nodejs/undici/pull/1114
|
|
156
|
+
description: Introduced the `BalancedPool` class.
|
|
157
|
+
-->
|
|
84
158
|
|
|
85
|
-
|
|
159
|
+
* Type: {boolean} `true` after `balancedPool.destroy()` has been called, or after
|
|
160
|
+
`balancedPool.close()` has been called and the shutdown has completed.
|
|
86
161
|
|
|
87
|
-
|
|
162
|
+
Implements [`client.destroyed`][].
|
|
163
|
+
|
|
164
|
+
### `balancedPool.stats`
|
|
165
|
+
|
|
166
|
+
<!-- YAML
|
|
167
|
+
added: v4.8.0
|
|
168
|
+
-->
|
|
169
|
+
|
|
170
|
+
* Type: {PoolStats}
|
|
171
|
+
|
|
172
|
+
Returns a {PoolStats} instance aggregating the statistics of the underlying
|
|
173
|
+
pools.
|
|
174
|
+
|
|
175
|
+
### `balancedPool.close([callback])`
|
|
176
|
+
|
|
177
|
+
<!-- YAML
|
|
178
|
+
added: v4.8.0
|
|
179
|
+
-->
|
|
180
|
+
|
|
181
|
+
Implements [`Dispatcher.close([callback])`][].
|
|
182
|
+
|
|
183
|
+
### `balancedPool.destroy([error[, callback]])`
|
|
184
|
+
|
|
185
|
+
<!-- YAML
|
|
186
|
+
added: v4.8.0
|
|
187
|
+
-->
|
|
188
|
+
|
|
189
|
+
Implements [`Dispatcher.destroy([error, callback])`][].
|
|
190
|
+
|
|
191
|
+
### `balancedPool.connect(options[, callback])`
|
|
192
|
+
|
|
193
|
+
<!-- YAML
|
|
194
|
+
added: v4.8.0
|
|
195
|
+
-->
|
|
196
|
+
|
|
197
|
+
See [`Dispatcher.connect(options[, callback])`][].
|
|
198
|
+
|
|
199
|
+
### `balancedPool.dispatch(options, handlers)`
|
|
200
|
+
|
|
201
|
+
<!-- YAML
|
|
202
|
+
added: v4.8.0
|
|
203
|
+
changes:
|
|
204
|
+
- version: v4.10.4
|
|
205
|
+
pr-url: https://github.com/nodejs/undici/pull/1114
|
|
206
|
+
description: Introduced the `BalancedPool` class.
|
|
207
|
+
-->
|
|
208
|
+
|
|
209
|
+
Implements [`Dispatcher.dispatch(options, handler)`][].
|
|
210
|
+
|
|
211
|
+
### `balancedPool.pipeline(options, handler)`
|
|
212
|
+
|
|
213
|
+
<!-- YAML
|
|
214
|
+
added: v4.8.0
|
|
215
|
+
-->
|
|
216
|
+
|
|
217
|
+
See [`Dispatcher.pipeline(options, handler)`][].
|
|
218
|
+
|
|
219
|
+
### `balancedPool.request(options[, callback])`
|
|
220
|
+
|
|
221
|
+
<!-- YAML
|
|
222
|
+
added: v4.8.0
|
|
223
|
+
-->
|
|
224
|
+
|
|
225
|
+
See [`Dispatcher.request(options[, callback])`][].
|
|
226
|
+
|
|
227
|
+
### `balancedPool.stream(options, factory[, callback])`
|
|
228
|
+
|
|
229
|
+
<!-- YAML
|
|
230
|
+
added: v4.8.0
|
|
231
|
+
-->
|
|
232
|
+
|
|
233
|
+
See [`Dispatcher.stream(options, factory[, callback])`][].
|
|
234
|
+
|
|
235
|
+
### `balancedPool.upgrade(options[, callback])`
|
|
236
|
+
|
|
237
|
+
<!-- YAML
|
|
238
|
+
added: v4.8.0
|
|
239
|
+
-->
|
|
240
|
+
|
|
241
|
+
See [`Dispatcher.upgrade(options[, callback])`][].
|
|
88
242
|
|
|
89
243
|
### Event: `'connect'`
|
|
90
244
|
|
|
91
|
-
|
|
245
|
+
<!-- YAML
|
|
246
|
+
added: v4.8.0
|
|
247
|
+
changes:
|
|
248
|
+
- version: v4.10.4
|
|
249
|
+
pr-url: https://github.com/nodejs/undici/pull/1114
|
|
250
|
+
description: Introduced the `BalancedPool` class.
|
|
251
|
+
- version: v5.8.0
|
|
252
|
+
pr-url: https://github.com/nodejs/undici/pull/1069
|
|
253
|
+
description: Adopted weighted round-robin selection across upstreams.
|
|
254
|
+
-->
|
|
255
|
+
|
|
256
|
+
See [Dispatcher Event: `'connect'`][].
|
|
92
257
|
|
|
93
258
|
### Event: `'disconnect'`
|
|
94
259
|
|
|
95
|
-
|
|
260
|
+
<!-- YAML
|
|
261
|
+
added: v4.8.0
|
|
262
|
+
changes:
|
|
263
|
+
- version: v4.10.4
|
|
264
|
+
pr-url: https://github.com/nodejs/undici/pull/1114
|
|
265
|
+
description: Introduced the `BalancedPool` class.
|
|
266
|
+
- version: v5.8.0
|
|
267
|
+
pr-url: https://github.com/nodejs/undici/pull/1069
|
|
268
|
+
description: Adopted weighted round-robin selection across upstreams.
|
|
269
|
+
-->
|
|
270
|
+
|
|
271
|
+
See [Dispatcher Event: `'disconnect'`][].
|
|
96
272
|
|
|
97
273
|
### Event: `'drain'`
|
|
98
274
|
|
|
99
|
-
|
|
275
|
+
<!-- YAML
|
|
276
|
+
added: v4.8.0
|
|
277
|
+
changes:
|
|
278
|
+
- version: v4.10.4
|
|
279
|
+
pr-url: https://github.com/nodejs/undici/pull/1114
|
|
280
|
+
description: Introduced the `BalancedPool` class.
|
|
281
|
+
-->
|
|
282
|
+
|
|
283
|
+
See [Dispatcher Event: `'drain'`][].
|
|
284
|
+
|
|
285
|
+
[Dispatcher Event: `'connect'`]: Dispatcher.md#event-connect
|
|
286
|
+
[Dispatcher Event: `'disconnect'`]: Dispatcher.md#event-disconnect
|
|
287
|
+
[Dispatcher Event: `'drain'`]: Dispatcher.md#event-drain
|
|
288
|
+
[`Dispatcher.close([callback])`]: Dispatcher.md#dispatcherclosecallback-promise
|
|
289
|
+
[`Dispatcher.connect(options[, callback])`]: Dispatcher.md#dispatcherconnectoptions-callback
|
|
290
|
+
[`Dispatcher.destroy([error, callback])`]: Dispatcher.md#dispatcherdestroyerror-callback-promise
|
|
291
|
+
[`Dispatcher.dispatch(options, handler)`]: Dispatcher.md#dispatcherdispatchoptions-handler
|
|
292
|
+
[`Dispatcher.pipeline(options, handler)`]: Dispatcher.md#dispatcherpipelineoptions-handler
|
|
293
|
+
[`Dispatcher.request(options[, callback])`]: Dispatcher.md#dispatcherrequestoptions-callback
|
|
294
|
+
[`Dispatcher.stream(options, factory[, callback])`]: Dispatcher.md#dispatcherstreamoptions-factory-callback
|
|
295
|
+
[`Dispatcher.upgrade(options[, callback])`]: Dispatcher.md#dispatcherupgradeoptions-callback
|
|
296
|
+
[`client.closed`]: Client.md#clientclosed
|
|
297
|
+
[`client.destroyed`]: Client.md#clientdestroyed
|
|
@@ -1,30 +1,188 @@
|
|
|
1
1
|
# CacheStorage
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<!--introduced_in=v5.22.1-->
|
|
4
|
+
<!--type=module-->
|
|
5
|
+
<!-- source_link=lib/web/cache/cachestorage.js -->
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
> Stability: 2 - Stable
|
|
6
8
|
|
|
7
|
-
|
|
9
|
+
undici provides a spec-compliant implementation of the [W3C Service Worker
|
|
10
|
+
`CacheStorage`][] and [`Cache`][] interfaces. A `CacheStorage` is a named
|
|
11
|
+
collection of [`Cache`][] objects, each of which stores `Request`/`Response`
|
|
12
|
+
pairs.
|
|
13
|
+
|
|
14
|
+
The class itself is not exported; instead, undici exposes a single shared
|
|
15
|
+
`CacheStorage` instance named `caches`. Import it from `'undici'`:
|
|
16
|
+
|
|
17
|
+
```mjs
|
|
18
|
+
import { caches } from 'undici'
|
|
19
|
+
|
|
20
|
+
const cache = await caches.open('v1')
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```cjs
|
|
24
|
+
const { caches } = require('undici')
|
|
25
|
+
|
|
26
|
+
caches.open('v1').then((cache) => {
|
|
27
|
+
// ...
|
|
28
|
+
})
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## `caches`
|
|
32
|
+
|
|
33
|
+
<!-- YAML
|
|
34
|
+
added: v5.22.1
|
|
35
|
+
-->
|
|
36
|
+
|
|
37
|
+
* Type: {CacheStorage}
|
|
38
|
+
|
|
39
|
+
The shared [`CacheStorage`](#class-cachestorage) instance exported by undici.
|
|
40
|
+
All caches opened through `caches` live for the lifetime of the process.
|
|
41
|
+
|
|
42
|
+
```mjs
|
|
43
|
+
import { caches } from 'undici'
|
|
44
|
+
|
|
45
|
+
await caches.open('v1')
|
|
46
|
+
console.log(await caches.keys()) // ['v1']
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Class: `CacheStorage`
|
|
50
|
+
|
|
51
|
+
<!-- YAML
|
|
52
|
+
added: v5.22.1
|
|
53
|
+
-->
|
|
54
|
+
|
|
55
|
+
Represents the storage for [`Cache`][] objects. Each `CacheStorage` keeps a
|
|
56
|
+
mapping of cache names to their backing list of cached responses.
|
|
57
|
+
|
|
58
|
+
The constructor is not public: attempting to instantiate `CacheStorage`
|
|
59
|
+
directly throws a `TypeError`. Use the exported [`caches`](#caches) instance
|
|
60
|
+
instead.
|
|
61
|
+
|
|
62
|
+
### `caches.match(request[, options])`
|
|
63
|
+
|
|
64
|
+
<!-- YAML
|
|
65
|
+
added: v5.22.1
|
|
66
|
+
-->
|
|
67
|
+
|
|
68
|
+
* `request` {Request|string} The request to match against. A string is treated
|
|
69
|
+
as a URL.
|
|
70
|
+
* `options` {Object} (optional)
|
|
71
|
+
* `ignoreSearch` {boolean} When `true`, ignores the query string of the URL
|
|
72
|
+
when matching. **Default:** `false`.
|
|
73
|
+
* `ignoreMethod` {boolean} When `true`, prevents matching operations from
|
|
74
|
+
validating the `Request` `http` method. **Default:** `false`.
|
|
75
|
+
* `ignoreVary` {boolean} When `true`, ignores the `Vary` header when matching.
|
|
76
|
+
**Default:** `false`.
|
|
77
|
+
* `cacheName` {string} When set, restricts the search to the cache with this
|
|
78
|
+
name. **Default:** `undefined`.
|
|
79
|
+
* Returns: {Promise} Fulfills with the first matching {Response}, or
|
|
80
|
+
`undefined` if no response matched.
|
|
81
|
+
|
|
82
|
+
Searches the caches for a response matching `request`. When `options.cacheName`
|
|
83
|
+
is provided, only that cache is searched; otherwise every cache is searched in
|
|
84
|
+
insertion order and the first match is returned.
|
|
85
|
+
|
|
86
|
+
```mjs
|
|
87
|
+
import { caches } from 'undici'
|
|
88
|
+
|
|
89
|
+
const response = await caches.match('https://example.com/data', {
|
|
90
|
+
cacheName: 'v1'
|
|
91
|
+
})
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### `caches.has(cacheName)`
|
|
95
|
+
|
|
96
|
+
<!-- YAML
|
|
97
|
+
added: v5.22.1
|
|
98
|
+
-->
|
|
99
|
+
|
|
100
|
+
* `cacheName` {string} The name of the cache to look up.
|
|
101
|
+
* Returns: {Promise} Fulfills with `true` if a cache named `cacheName` exists,
|
|
102
|
+
otherwise `false`.
|
|
103
|
+
|
|
104
|
+
Checks whether a cache with the given name exists in the storage.
|
|
105
|
+
|
|
106
|
+
```mjs
|
|
107
|
+
import { caches } from 'undici'
|
|
108
|
+
|
|
109
|
+
await caches.open('v1')
|
|
110
|
+
console.log(await caches.has('v1')) // true
|
|
111
|
+
console.log(await caches.has('v2')) // false
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### `caches.open(cacheName)`
|
|
115
|
+
|
|
116
|
+
<!-- YAML
|
|
117
|
+
added: v5.22.1
|
|
118
|
+
-->
|
|
119
|
+
|
|
120
|
+
* `cacheName` {string} The name of the cache to open.
|
|
121
|
+
* Returns: {Promise} Fulfills with the {Cache} named `cacheName`.
|
|
122
|
+
|
|
123
|
+
Opens the cache named `cacheName`, creating it if it does not already exist.
|
|
124
|
+
|
|
125
|
+
Each call returns a new [`Cache`][] object, but caches opened with the same name
|
|
126
|
+
share their underlying list of cached responses. As a result, a response stored
|
|
127
|
+
through one instance is visible through another instance with the same name.
|
|
8
128
|
|
|
9
129
|
```mjs
|
|
10
130
|
import { caches } from 'undici'
|
|
131
|
+
import assert from 'node:assert'
|
|
11
132
|
|
|
12
|
-
const
|
|
13
|
-
const
|
|
133
|
+
const cache1 = await caches.open('v1')
|
|
134
|
+
const cache2 = await caches.open('v1')
|
|
14
135
|
|
|
15
|
-
//
|
|
16
|
-
assert(
|
|
17
|
-
//
|
|
18
|
-
assert.deepStrictEqual(await
|
|
136
|
+
// open() returns distinct objects,
|
|
137
|
+
assert(cache1 !== cache2)
|
|
138
|
+
// but they share the same cached responses.
|
|
139
|
+
assert.deepStrictEqual(await cache1.match('/req'), await cache2.match('/req'))
|
|
19
140
|
```
|
|
20
141
|
|
|
21
|
-
|
|
142
|
+
### `caches.delete(cacheName)`
|
|
22
143
|
|
|
23
|
-
|
|
144
|
+
<!-- YAML
|
|
145
|
+
added: v5.22.1
|
|
146
|
+
-->
|
|
147
|
+
|
|
148
|
+
* `cacheName` {string} The name of the cache to delete.
|
|
149
|
+
* Returns: {Promise} Fulfills with `true` if the cache existed and was deleted,
|
|
150
|
+
otherwise `false`.
|
|
151
|
+
|
|
152
|
+
Removes the cache named `cacheName` from the storage. Any [`Cache`][] objects
|
|
153
|
+
already obtained for that name, along with the `Request`/`Response` pairs they
|
|
154
|
+
returned, remain usable after deletion.
|
|
24
155
|
|
|
25
156
|
```mjs
|
|
26
|
-
|
|
157
|
+
import { caches } from 'undici'
|
|
158
|
+
|
|
159
|
+
const cache = await caches.open('v1')
|
|
160
|
+
const response = await cache.match('/req')
|
|
161
|
+
|
|
27
162
|
await caches.delete('v1')
|
|
28
163
|
|
|
29
|
-
|
|
164
|
+
// The already-retrieved response is still usable.
|
|
165
|
+
await response.text()
|
|
30
166
|
```
|
|
167
|
+
|
|
168
|
+
### `caches.keys()`
|
|
169
|
+
|
|
170
|
+
<!-- YAML
|
|
171
|
+
added: v5.22.1
|
|
172
|
+
-->
|
|
173
|
+
|
|
174
|
+
* Returns: {Promise} Fulfills with a {string[]} of cache names in insertion
|
|
175
|
+
order.
|
|
176
|
+
|
|
177
|
+
Returns the names of all caches currently held by the storage.
|
|
178
|
+
|
|
179
|
+
```mjs
|
|
180
|
+
import { caches } from 'undici'
|
|
181
|
+
|
|
182
|
+
await caches.open('v1')
|
|
183
|
+
await caches.open('v2')
|
|
184
|
+
console.log(await caches.keys()) // ['v1', 'v2']
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
[W3C Service Worker `CacheStorage`]: https://w3c.github.io/ServiceWorker/#cachestorage-interface
|
|
188
|
+
[`Cache`]: https://w3c.github.io/ServiceWorker/#cache-interface
|