undici 8.4.1 → 8.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. package/README.md +3 -2
  2. package/docs/docs/api/Agent.md +248 -44
  3. package/docs/docs/api/BalancedPool.md +246 -48
  4. package/docs/docs/api/CacheStorage.md +171 -13
  5. package/docs/docs/api/CacheStore.md +294 -98
  6. package/docs/docs/api/Client.md +365 -178
  7. package/docs/docs/api/ClientStats.md +80 -14
  8. package/docs/docs/api/Connector.md +118 -36
  9. package/docs/docs/api/ContentType.md +65 -25
  10. package/docs/docs/api/Cookies.md +125 -47
  11. package/docs/docs/api/Debug.md +34 -18
  12. package/docs/docs/api/DiagnosticsChannel.md +364 -164
  13. package/docs/docs/api/Dispatcher.md +479 -1093
  14. package/docs/docs/api/EnvHttpProxyAgent.md +101 -87
  15. package/docs/docs/api/Errors.md +503 -40
  16. package/docs/docs/api/EventSource.md +294 -32
  17. package/docs/docs/api/Fetch.md +680 -31
  18. package/docs/docs/api/GlobalInstallation.md +78 -98
  19. package/docs/docs/api/H2CClient.md +298 -176
  20. package/docs/docs/api/Interceptors.md +355 -0
  21. package/docs/docs/api/MockAgent.md +314 -322
  22. package/docs/docs/api/MockCallHistory.md +272 -98
  23. package/docs/docs/api/MockCallHistoryLog.md +189 -26
  24. package/docs/docs/api/MockClient.md +151 -33
  25. package/docs/docs/api/MockErrors.md +56 -5
  26. package/docs/docs/api/MockPool.md +299 -263
  27. package/docs/docs/api/Pool.md +235 -43
  28. package/docs/docs/api/PoolStats.md +119 -13
  29. package/docs/docs/api/ProxyAgent.md +172 -160
  30. package/docs/docs/api/RedirectHandler.md +238 -63
  31. package/docs/docs/api/RetryAgent.md +113 -26
  32. package/docs/docs/api/RetryHandler.md +161 -77
  33. package/docs/docs/api/RoundRobinPool.md +166 -72
  34. package/docs/docs/api/SnapshotAgent.md +264 -542
  35. package/docs/docs/api/Socks5ProxyAgent.md +162 -186
  36. package/docs/docs/api/Util.md +68 -11
  37. package/docs/docs/api/WebSocket.md +475 -80
  38. package/docs/docs/api/api-lifecycle.md +100 -32
  39. package/docs/docs/best-practices/client-certificate.md +2 -0
  40. package/docs/docs/best-practices/crawling.md +3 -1
  41. package/docs/docs/best-practices/migrating-from-v7-to-v8.md +6 -4
  42. package/docs/docs/best-practices/mocking-request.md +8 -6
  43. package/docs/docs/best-practices/proxy.md +3 -1
  44. package/docs/docs/best-practices/undici-vs-builtin-fetch.md +10 -8
  45. package/docs/docs/best-practices/writing-tests.md +2 -0
  46. package/docs/docs/{GettingStarted.md → getting-started.md} +22 -19
  47. package/docs/docs/index.md +779 -0
  48. package/docs/docs/site.json +125 -0
  49. package/docs/docs/type-map.json +79 -0
  50. package/lib/api/api-request.js +7 -1
  51. package/lib/api/readable.js +43 -2
  52. package/lib/core/errors.js +20 -0
  53. package/lib/core/request.js +1 -1
  54. package/lib/core/util.js +3 -1
  55. package/lib/dispatcher/client-h1.js +92 -1
  56. package/lib/dispatcher/client-h2.js +124 -18
  57. package/lib/dispatcher/client.js +6 -2
  58. package/lib/dispatcher/dispatcher-base.js +1 -0
  59. package/lib/dispatcher/proxy-agent.js +11 -2
  60. package/lib/dispatcher/socks5-proxy-agent.js +4 -2
  61. package/lib/handler/redirect-handler.js +1 -0
  62. package/lib/handler/retry-handler.js +57 -19
  63. package/lib/util/cache.js +8 -2
  64. package/lib/web/cookies/parse.js +17 -25
  65. package/lib/web/eventsource/eventsource.js +7 -18
  66. package/lib/web/eventsource/util.js +32 -1
  67. package/lib/web/fetch/body.js +43 -0
  68. package/lib/web/fetch/constants.js +1 -1
  69. package/lib/web/fetch/index.js +11 -2
  70. package/lib/web/fetch/request.js +1 -0
  71. package/lib/web/websocket/receiver.js +20 -3
  72. package/lib/web/websocket/stream/websocketstream.js +8 -1
  73. package/lib/web/websocket/websocket.js +3 -1
  74. package/package.json +1 -1
  75. package/types/client.d.ts +6 -1
  76. package/types/cookies.d.ts +1 -1
  77. package/types/errors.d.ts +10 -0
  78. package/types/fetch.d.ts +1 -0
@@ -1,99 +1,297 @@
1
- # Class: BalancedPool
1
+ # BalancedPool
2
2
 
3
- Extends: `undici.Dispatcher`
3
+ <!--introduced_in=v4.8.0-->
4
+ <!--type=module-->
5
+ <!-- source_link=lib/dispatcher/balanced-pool.js -->
4
6
 
5
- A pool of [Pool](/docs/docs/api/Pool.md) instances connected to multiple upstreams.
7
+ > Stability: 2 - Stable
6
8
 
7
- Requests are not guaranteed to be dispatched in order of invocation.
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
- ## `new BalancedPool(upstreams [, options])`
15
+ Requests are not guaranteed to be dispatched in the order they are issued.
10
16
 
11
- Arguments:
17
+ ```mjs
18
+ import { BalancedPool } from 'undici'
12
19
 
13
- * **upstreams** `URL | string | string[]` - It should only include the **protocol, hostname, and port**.
14
- * **options** `BalancedPoolOptions` (optional)
20
+ const pool = new BalancedPool([
21
+ 'http://localhost:3000',
22
+ 'http://localhost:3001',
23
+ 'http://localhost:3002',
24
+ ])
15
25
 
16
- ### Parameter: `BalancedPoolOptions`
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
- Extends: [`PoolOptions`](/docs/docs/api/Pool.md#parameter-pooloptions)
36
+ ## Class: `BalancedPool`
19
37
 
20
- * **factory** `(origin: URL, opts: Object) => Dispatcher` - Default: `(origin, opts) => new Pool(origin, opts)`
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
- The `PoolOptions` are passed to each of the `Pool` instances being created.
23
- ## Instance Properties
46
+ * Extends: {Dispatcher}
24
47
 
25
- ### `BalancedPool.upstreams`
48
+ A pool of {Pool} instances connected to multiple upstreams.
26
49
 
27
- Returns an array of upstreams that were previously added.
50
+ ### `new BalancedPool(upstreams[, options])`
28
51
 
29
- ### `BalancedPool.closed`
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
- Implements [Client.closed](/docs/docs/api/Client.md#clientclosed)
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
- ### `BalancedPool.destroyed`
65
+ The options passed to the constructor are forwarded to every {Pool} instance
66
+ that is created for an upstream.
34
67
 
35
- Implements [Client.destroyed](/docs/docs/api/Client.md#clientdestroyed)
68
+ #### Parameter: `BalancedPoolOptions`
36
69
 
37
- ### `BalancedPool.stats`
70
+ Extends: {PoolOptions}
38
71
 
39
- Returns [`PoolStats`](/docs/docs/api/PoolStats.md) instance for this pool.
72
+ In addition to all {PoolOptions}, the following options are supported:
40
73
 
41
- ## Instance Methods
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
- ### `BalancedPool.addUpstream(upstream)`
86
+ ### `balancedPool.addUpstream(upstream)`
44
87
 
45
- Add an upstream.
88
+ <!-- YAML
89
+ added: v4.8.0
90
+ -->
46
91
 
47
- Arguments:
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
- * **upstream** `string` - It should only include the **protocol, hostname, and port**.
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
- ### `BalancedPool.removeUpstream(upstream)`
99
+ ### `balancedPool.removeUpstream(upstream)`
52
100
 
53
- Removes an upstream that was previously added.
101
+ <!-- YAML
102
+ added: v4.8.0
103
+ -->
54
104
 
55
- ### `BalancedPool.close([callback])`
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
- Implements [`Dispatcher.close([callback])`](/docs/docs/api/Dispatcher.md#dispatcherclosecallback-promise).
109
+ Removes an upstream that was previously added. If no matching upstream is found,
110
+ the call is a no-op.
58
111
 
59
- ### `BalancedPool.destroy([error, callback])`
112
+ ### `balancedPool.getUpstream(upstream)`
60
113
 
61
- Implements [`Dispatcher.destroy([error, callback])`](/docs/docs/api/Dispatcher.md#dispatcherdestroyerror-callback-promise).
114
+ <!-- YAML
115
+ added: v7.17.0
116
+ -->
62
117
 
63
- ### `BalancedPool.connect(options[, callback])`
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
- See [`Dispatcher.connect(options[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherconnectoptions-callback).
123
+ Returns the {Pool} instance for the given upstream origin, if it is open and not
124
+ destroyed.
66
125
 
67
- ### `BalancedPool.dispatch(options, handlers)`
126
+ ### `balancedPool.upstreams`
68
127
 
69
- Implements [`Dispatcher.dispatch(options, handlers)`](/docs/docs/api/Dispatcher.md#dispatcherdispatchoptions-handler).
128
+ <!-- YAML
129
+ added: v4.8.0
130
+ -->
70
131
 
71
- ### `BalancedPool.pipeline(options, handler)`
132
+ * Type: {string[]} The origins of the upstreams that are currently open and not
133
+ destroyed.
72
134
 
73
- See [`Dispatcher.pipeline(options, handler)`](/docs/docs/api/Dispatcher.md#dispatcherpipelineoptions-handler).
135
+ ### `balancedPool.closed`
74
136
 
75
- ### `BalancedPool.request(options[, callback])`
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
- See [`Dispatcher.request(options [, callback])`](/docs/docs/api/Dispatcher.md#dispatcherrequestoptions-callback).
145
+ * Type: {boolean} `true` after `balancedPool.close()` has been called.
78
146
 
79
- ### `BalancedPool.stream(options, factory[, callback])`
147
+ Implements [`client.closed`][].
80
148
 
81
- See [`Dispatcher.stream(options, factory[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherstreamoptions-factory-callback).
149
+ ### `balancedPool.destroyed`
82
150
 
83
- ### `BalancedPool.upgrade(options[, callback])`
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
- See [`Dispatcher.upgrade(options[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherupgradeoptions-callback).
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
- ## Instance Events
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
- See [Dispatcher Event: `'connect'`](/docs/docs/api/Dispatcher.md#event-connect).
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
- See [Dispatcher Event: `'disconnect'`](/docs/docs/api/Dispatcher.md#event-disconnect).
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
- See [Dispatcher Event: `'drain'`](/docs/docs/api/Dispatcher.md#event-drain).
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
- Undici exposes a W3C spec-compliant implementation of [CacheStorage](https://developer.mozilla.org/en-US/docs/Web/API/CacheStorage) and [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache).
3
+ <!--introduced_in=v5.22.1-->
4
+ <!--type=module-->
5
+ <!-- source_link=lib/web/cache/cachestorage.js -->
4
6
 
5
- ## Opening a Cache
7
+ > Stability: 2 - Stable
6
8
 
7
- Undici exports a top-level CacheStorage instance. You can open a new Cache, or duplicate a Cache with an existing name, by using `CacheStorage.prototype.open`. If you open a Cache with the same name as an already-existing Cache, its list of cached Responses will be shared between both instances.
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 cache_1 = await caches.open('v1')
13
- const cache_2 = await caches.open('v1')
133
+ const cache1 = await caches.open('v1')
134
+ const cache2 = await caches.open('v1')
14
135
 
15
- // Although .open() creates a new instance,
16
- assert(cache_1 !== cache_2)
17
- // The same Response is matched in both.
18
- assert.deepStrictEqual(await cache_1.match('/req'), await cache_2.match('/req'))
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
- ## Deleting a Cache
142
+ ### `caches.delete(cacheName)`
22
143
 
23
- If a Cache is deleted, the cached Responses/Requests can still be used.
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
- const response = await cache_1.match('/req')
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
- await response.text() // the Response's body
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