undici 7.0.0-alpha.1 → 7.0.0-alpha.10

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 (113) hide show
  1. package/README.md +24 -38
  2. package/docs/docs/api/Agent.md +14 -14
  3. package/docs/docs/api/BalancedPool.md +16 -16
  4. package/docs/docs/api/CacheStore.md +131 -0
  5. package/docs/docs/api/Client.md +12 -12
  6. package/docs/docs/api/Debug.md +1 -1
  7. package/docs/docs/api/Dispatcher.md +98 -193
  8. package/docs/docs/api/EnvHttpProxyAgent.md +12 -12
  9. package/docs/docs/api/MockAgent.md +5 -3
  10. package/docs/docs/api/MockClient.md +5 -5
  11. package/docs/docs/api/MockPool.md +4 -3
  12. package/docs/docs/api/Pool.md +15 -15
  13. package/docs/docs/api/PoolStats.md +1 -1
  14. package/docs/docs/api/ProxyAgent.md +3 -3
  15. package/docs/docs/api/RedirectHandler.md +1 -1
  16. package/docs/docs/api/RetryAgent.md +1 -1
  17. package/docs/docs/api/RetryHandler.md +4 -4
  18. package/docs/docs/api/WebSocket.md +46 -4
  19. package/docs/docs/api/api-lifecycle.md +11 -11
  20. package/docs/docs/best-practices/mocking-request.md +2 -2
  21. package/docs/docs/best-practices/proxy.md +1 -1
  22. package/index.d.ts +1 -1
  23. package/index.js +23 -3
  24. package/lib/api/abort-signal.js +2 -0
  25. package/lib/api/api-pipeline.js +4 -2
  26. package/lib/api/api-request.js +6 -4
  27. package/lib/api/api-stream.js +3 -1
  28. package/lib/api/api-upgrade.js +2 -2
  29. package/lib/api/readable.js +200 -47
  30. package/lib/api/util.js +2 -0
  31. package/lib/cache/memory-cache-store.js +177 -0
  32. package/lib/cache/sqlite-cache-store.js +446 -0
  33. package/lib/core/connect.js +54 -22
  34. package/lib/core/constants.js +35 -10
  35. package/lib/core/diagnostics.js +122 -128
  36. package/lib/core/errors.js +2 -2
  37. package/lib/core/request.js +6 -6
  38. package/lib/core/symbols.js +2 -0
  39. package/lib/core/tree.js +4 -2
  40. package/lib/core/util.js +238 -40
  41. package/lib/dispatcher/client-h1.js +405 -142
  42. package/lib/dispatcher/client-h2.js +212 -109
  43. package/lib/dispatcher/client.js +24 -7
  44. package/lib/dispatcher/dispatcher-base.js +4 -1
  45. package/lib/dispatcher/dispatcher.js +4 -0
  46. package/lib/dispatcher/fixed-queue.js +91 -49
  47. package/lib/dispatcher/pool-base.js +3 -3
  48. package/lib/dispatcher/pool-stats.js +2 -0
  49. package/lib/dispatcher/proxy-agent.js +3 -1
  50. package/lib/handler/cache-handler.js +393 -0
  51. package/lib/handler/cache-revalidation-handler.js +124 -0
  52. package/lib/handler/decorator-handler.js +3 -0
  53. package/lib/handler/redirect-handler.js +45 -59
  54. package/lib/handler/retry-handler.js +68 -109
  55. package/lib/handler/unwrap-handler.js +96 -0
  56. package/lib/handler/wrap-handler.js +98 -0
  57. package/lib/interceptor/cache.js +350 -0
  58. package/lib/interceptor/dns.js +375 -0
  59. package/lib/interceptor/response-error.js +15 -7
  60. package/lib/mock/mock-agent.js +5 -8
  61. package/lib/mock/mock-client.js +7 -2
  62. package/lib/mock/mock-errors.js +3 -1
  63. package/lib/mock/mock-interceptor.js +8 -6
  64. package/lib/mock/mock-pool.js +7 -2
  65. package/lib/mock/mock-symbols.js +2 -1
  66. package/lib/mock/mock-utils.js +33 -5
  67. package/lib/util/cache.js +360 -0
  68. package/lib/util/timers.js +50 -6
  69. package/lib/web/cache/cache.js +25 -21
  70. package/lib/web/cache/cachestorage.js +3 -1
  71. package/lib/web/cookies/index.js +18 -5
  72. package/lib/web/cookies/parse.js +6 -1
  73. package/lib/web/eventsource/eventsource.js +2 -0
  74. package/lib/web/fetch/body.js +43 -39
  75. package/lib/web/fetch/constants.js +45 -29
  76. package/lib/web/fetch/data-url.js +2 -2
  77. package/lib/web/fetch/formdata-parser.js +84 -46
  78. package/lib/web/fetch/formdata.js +42 -20
  79. package/lib/web/fetch/headers.js +119 -85
  80. package/lib/web/fetch/index.js +69 -65
  81. package/lib/web/fetch/request.js +132 -55
  82. package/lib/web/fetch/response.js +81 -36
  83. package/lib/web/fetch/util.js +274 -103
  84. package/lib/web/fetch/webidl.js +54 -18
  85. package/lib/web/websocket/connection.js +92 -15
  86. package/lib/web/websocket/constants.js +69 -9
  87. package/lib/web/websocket/events.js +8 -2
  88. package/lib/web/websocket/receiver.js +20 -26
  89. package/lib/web/websocket/stream/websocketerror.js +83 -0
  90. package/lib/web/websocket/stream/websocketstream.js +485 -0
  91. package/lib/web/websocket/util.js +115 -10
  92. package/lib/web/websocket/websocket.js +47 -170
  93. package/package.json +15 -11
  94. package/types/agent.d.ts +1 -1
  95. package/types/cache-interceptor.d.ts +172 -0
  96. package/types/cookies.d.ts +2 -0
  97. package/types/dispatcher.d.ts +29 -4
  98. package/types/env-http-proxy-agent.d.ts +1 -1
  99. package/types/fetch.d.ts +9 -8
  100. package/types/handlers.d.ts +4 -4
  101. package/types/index.d.ts +3 -1
  102. package/types/interceptors.d.ts +18 -1
  103. package/types/mock-agent.d.ts +4 -1
  104. package/types/mock-client.d.ts +1 -1
  105. package/types/mock-pool.d.ts +1 -1
  106. package/types/proxy-agent.d.ts +1 -1
  107. package/types/readable.d.ts +10 -7
  108. package/types/retry-handler.d.ts +3 -3
  109. package/types/webidl.d.ts +30 -4
  110. package/types/websocket.d.ts +33 -0
  111. package/lib/mock/pluralizer.js +0 -29
  112. package/lib/web/cache/symbols.js +0 -5
  113. package/lib/web/fetch/symbols.js +0 -8
package/README.md CHANGED
@@ -22,41 +22,26 @@ npm i undici
22
22
  ## Benchmarks
23
23
 
24
24
  The benchmark is a simple getting data [example](https://github.com/nodejs/undici/blob/main/benchmarks/benchmark.js) using a
25
- 50 TCP connections with a pipelining depth of 10 running on Node 20.10.0.
26
-
27
- | _Tests_ | _Samples_ | _Result_ | _Tolerance_ | _Difference with slowest_ |
28
- | :-----------------: | :-------: | :--------------: | :---------: | :-----------------------: |
29
- | undici - fetch | 30 | 3704.43 req/sec | ± 2.95 % | - |
30
- | http - no keepalive | 20 | 4275.30 req/sec | ± 2.60 % | + 15.41 % |
31
- | node-fetch | 10 | 4759.42 req/sec | ± 0.87 % | + 28.48 % |
32
- | request | 40 | 4803.37 req/sec | ± 2.77 % | + 29.67 % |
33
- | axios | 45 | 4951.97 req/sec | ± 2.88 % | + 33.68 % |
34
- | got | 10 | 5969.67 req/sec | ± 2.64 % | + 61.15 % |
35
- | superagent | 10 | 9471.48 req/sec | ± 1.50 % | + 155.68 % |
36
- | http - keepalive | 25 | 10327.49 req/sec | ± 2.95 % | + 178.79 % |
37
- | undici - pipeline | 10 | 15053.41 req/sec | ± 1.63 % | + 306.36 % |
38
- | undici - request | 10 | 19264.24 req/sec | ± 1.74 % | + 420.03 % |
39
- | undici - stream | 15 | 20317.29 req/sec | ± 2.13 % | + 448.46 % |
40
- | undici - dispatch | 10 | 24883.28 req/sec | ± 1.54 % | + 571.72 % |
41
-
42
- The benchmark is a simple sending data [example](https://github.com/nodejs/undici/blob/main/benchmarks/post-benchmark.js) using a
43
- 50 TCP connections with a pipelining depth of 10 running on Node 20.10.0.
44
-
45
- | _Tests_ | _Samples_ | _Result_ | _Tolerance_ | _Difference with slowest_ |
46
- | :-----------------: | :-------: | :-------------: | :---------: | :-----------------------: |
47
- | undici - fetch | 20 | 1968.42 req/sec | ± 2.63 % | - |
48
- | http - no keepalive | 25 | 2330.30 req/sec | ± 2.99 % | + 18.38 % |
49
- | node-fetch | 20 | 2485.36 req/sec | ± 2.70 % | + 26.26 % |
50
- | got | 15 | 2787.68 req/sec | ± 2.56 % | + 41.62 % |
51
- | request | 30 | 2805.10 req/sec | ± 2.59 % | + 42.50 % |
52
- | axios | 10 | 3040.45 req/sec | ± 1.72 % | + 54.46 % |
53
- | superagent | 20 | 3358.29 req/sec | ± 2.51 % | + 70.61 % |
54
- | http - keepalive | 20 | 3477.94 req/sec | ± 2.51 % | + 76.69 % |
55
- | undici - pipeline | 25 | 3812.61 req/sec | ± 2.80 % | + 93.69 % |
56
- | undici - request | 10 | 6067.00 req/sec | ± 0.94 % | + 208.22 % |
57
- | undici - stream | 10 | 6391.61 req/sec | ± 1.98 % | + 224.71 % |
58
- | undici - dispatch | 10 | 6397.00 req/sec | ± 1.48 % | + 224.98 % |
25
+ 50 TCP connections with a pipelining depth of 10 running on Node 22.11.0.
59
26
 
27
+ ```
28
+ ┌────────────────────────┬─────────┬────────────────────┬────────────┬─────────────────────────┐
29
+ │ Tests │ Samples │ Result │ Tolerance │ Difference with slowest │
30
+ ├────────────────────────┼─────────┼────────────────────┼────────────┼─────────────────────────┤
31
+ │ 'axios' │ 15 │ '5708.26 req/sec' │ '± 2.91 %' │ '-' │
32
+ │ 'http - no keepalive' │ 10 │ '5809.80 req/sec' │ '± 2.30 %' │ '+ 1.78 %' │
33
+ │ 'request' │ 30 │ '5828.80 req/sec' │ '± 2.91 %' │ '+ 2.11 %' │
34
+ │ 'undici - fetch' │ 40 │ '5903.78 req/sec' │ '± 2.87 %' │ '+ 3.43 %' │
35
+ │ 'node-fetch' │ 10 │ '5945.40 req/sec' │ '± 2.13 %' │ '+ 4.15 %' │
36
+ │ 'got' │ 35 │ '6511.45 req/sec' │ '± 2.84 %' │ '+ 14.07 %' │
37
+ │ 'http - keepalive' │ 65 │ '9193.24 req/sec' │ '± 2.92 %' │ '+ 61.05 %' │
38
+ │ 'superagent' │ 35 │ '9339.43 req/sec' │ '± 2.95 %' │ '+ 63.61 %' │
39
+ │ 'undici - pipeline' │ 50 │ '13364.62 req/sec' │ '± 2.93 %' │ '+ 134.13 %' │
40
+ │ 'undici - stream' │ 95 │ '18245.36 req/sec' │ '± 2.99 %' │ '+ 219.63 %' │
41
+ │ 'undici - request' │ 50 │ '18340.17 req/sec' │ '± 2.84 %' │ '+ 221.29 %' │
42
+ │ 'undici - dispatch' │ 40 │ '22234.42 req/sec' │ '± 2.94 %' │ '+ 289.51 %' │
43
+ └────────────────────────┴─────────┴────────────────────┴────────────┴─────────────────────────┘
44
+ ```
60
45
 
61
46
  ## Quick Start
62
47
 
@@ -132,7 +117,7 @@ Returns a promise with the result of the `Dispatcher.request` method.
132
117
 
133
118
  Calls `options.dispatcher.request(options)`.
134
119
 
135
- See [Dispatcher.request](./docs/docs/api/Dispatcher.md#dispatcherrequestoptions-callback) for more details, and [request examples](./examples/README.md) for examples.
120
+ See [Dispatcher.request](./docs/docs/api/Dispatcher.md#dispatcherrequestoptions-callback) for more details, and [request examples](./docs/examples/README.md) for examples.
136
121
 
137
122
  ### `undici.stream([url, options, ]factory): Promise`
138
123
 
@@ -230,7 +215,7 @@ A body can be of the following types:
230
215
  - URLSearchParams
231
216
  - FormData
232
217
 
233
- In this implementation of fetch, ```request.body``` now accepts ```Async Iterables```. It is not present in the [Fetch Standard.](https://fetch.spec.whatwg.org)
218
+ In this implementation of fetch, ```request.body``` now accepts ```Async Iterables```. It is not present in the [Fetch Standard](https://fetch.spec.whatwg.org).
234
219
 
235
220
  ```js
236
221
  import { fetch } from 'undici'
@@ -261,7 +246,7 @@ await fetch('http://example.com', { method: 'POST', body })
261
246
 
262
247
  - `'half'`
263
248
 
264
- In this implementation of fetch, `request.duplex` must be set if `request.body` is `ReadableStream` or `Async Iterables`, however, even though the value must be set to `'half'`, it is actually a _full_ duplex. For more detail refer to the [Fetch Standard.](https://fetch.spec.whatwg.org/#dom-requestinit-duplex).
249
+ In this implementation of fetch, `request.duplex` must be set if `request.body` is `ReadableStream` or `Async Iterables`, however, even though the value must be set to `'half'`, it is actually a _full_ duplex. For more detail refer to the [Fetch Standard](https://fetch.spec.whatwg.org/#dom-requestinit-duplex).
265
250
 
266
251
  #### `response.body`
267
252
 
@@ -402,7 +387,8 @@ Refs: https://tools.ietf.org/html/rfc7231#section-5.1.1
402
387
  ### Pipelining
403
388
 
404
389
  Undici will only use pipelining if configured with a `pipelining` factor
405
- greater than `1`.
390
+ greater than `1`. Also it is important to pass `blocking: false` to the
391
+ request options to properly pipeline requests.
406
392
 
407
393
  Undici always assumes that connections are persistent and will immediately
408
394
  pipeline requests, without checking whether the connection is persistent.
@@ -16,7 +16,7 @@ Returns: `Agent`
16
16
 
17
17
  ### Parameter: `AgentOptions`
18
18
 
19
- Extends: [`PoolOptions`](Pool.md#parameter-pooloptions)
19
+ Extends: [`PoolOptions`](/docs/docs/api/Pool.md#parameter-pooloptions)
20
20
 
21
21
  * **factory** `(origin: URL, opts: Object) => Dispatcher` - Default: `(origin, opts) => new Pool(origin, opts)`
22
22
 
@@ -24,54 +24,54 @@ Extends: [`PoolOptions`](Pool.md#parameter-pooloptions)
24
24
 
25
25
  ### `Agent.closed`
26
26
 
27
- Implements [Client.closed](Client.md#clientclosed)
27
+ Implements [Client.closed](/docs/docs/api/Client.md#clientclosed)
28
28
 
29
29
  ### `Agent.destroyed`
30
30
 
31
- Implements [Client.destroyed](Client.md#clientdestroyed)
31
+ Implements [Client.destroyed](/docs/docs/api/Client.md#clientdestroyed)
32
32
 
33
33
  ## Instance Methods
34
34
 
35
35
  ### `Agent.close([callback])`
36
36
 
37
- Implements [`Dispatcher.close([callback])`](Dispatcher.md#dispatcherclosecallback-promise).
37
+ Implements [`Dispatcher.close([callback])`](/docs/docs/api/Dispatcher.md#dispatcherclosecallback-promise).
38
38
 
39
39
  ### `Agent.destroy([error, callback])`
40
40
 
41
- Implements [`Dispatcher.destroy([error, callback])`](Dispatcher.md#dispatcherdestroyerror-callback-promise).
41
+ Implements [`Dispatcher.destroy([error, callback])`](/docs/docs/api/Dispatcher.md#dispatcherdestroyerror-callback-promise).
42
42
 
43
43
  ### `Agent.dispatch(options, handler: AgentDispatchOptions)`
44
44
 
45
- Implements [`Dispatcher.dispatch(options, handler)`](Dispatcher.md#dispatcherdispatchoptions-handler).
45
+ Implements [`Dispatcher.dispatch(options, handler)`](/docs/docs/api/Dispatcher.md#dispatcherdispatchoptions-handler).
46
46
 
47
47
  #### Parameter: `AgentDispatchOptions`
48
48
 
49
- Extends: [`DispatchOptions`](Dispatcher.md#parameter-dispatchoptions)
49
+ Extends: [`DispatchOptions`](/docs/docs/api/Dispatcher.md#parameter-dispatchoptions)
50
50
 
51
51
  * **origin** `string | URL`
52
52
 
53
- Implements [`Dispatcher.destroy([error, callback])`](Dispatcher.md#dispatcherdestroyerror-callback-promise).
53
+ Implements [`Dispatcher.destroy([error, callback])`](/docs/docs/api/Dispatcher.md#dispatcherdestroyerror-callback-promise).
54
54
 
55
55
  ### `Agent.connect(options[, callback])`
56
56
 
57
- See [`Dispatcher.connect(options[, callback])`](Dispatcher.md#dispatcherconnectoptions-callback).
57
+ See [`Dispatcher.connect(options[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherconnectoptions-callback).
58
58
 
59
59
  ### `Agent.dispatch(options, handler)`
60
60
 
61
- Implements [`Dispatcher.dispatch(options, handler)`](Dispatcher.md#dispatcherdispatchoptions-handler).
61
+ Implements [`Dispatcher.dispatch(options, handler)`](/docs/docs/api/Dispatcher.md#dispatcherdispatchoptions-handler).
62
62
 
63
63
  ### `Agent.pipeline(options, handler)`
64
64
 
65
- See [`Dispatcher.pipeline(options, handler)`](Dispatcher.md#dispatcherpipelineoptions-handler).
65
+ See [`Dispatcher.pipeline(options, handler)`](/docs/docs/api/Dispatcher.md#dispatcherpipelineoptions-handler).
66
66
 
67
67
  ### `Agent.request(options[, callback])`
68
68
 
69
- See [`Dispatcher.request(options [, callback])`](Dispatcher.md#dispatcherrequestoptions-callback).
69
+ See [`Dispatcher.request(options [, callback])`](/docs/docs/api/Dispatcher.md#dispatcherrequestoptions-callback).
70
70
 
71
71
  ### `Agent.stream(options, factory[, callback])`
72
72
 
73
- See [`Dispatcher.stream(options, factory[, callback])`](Dispatcher.md#dispatcherstreamoptions-factory-callback).
73
+ See [`Dispatcher.stream(options, factory[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherstreamoptions-factory-callback).
74
74
 
75
75
  ### `Agent.upgrade(options[, callback])`
76
76
 
77
- See [`Dispatcher.upgrade(options[, callback])`](Dispatcher.md#dispatcherupgradeoptions-callback).
77
+ See [`Dispatcher.upgrade(options[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherupgradeoptions-callback).
@@ -2,7 +2,7 @@
2
2
 
3
3
  Extends: `undici.Dispatcher`
4
4
 
5
- A pool of [Pool](Pool.md) instances connected to multiple upstreams.
5
+ A pool of [Pool](/docs/docs/api/Pool.md) instances connected to multiple upstreams.
6
6
 
7
7
  Requests are not guaranteed to be dispatched in order of invocation.
8
8
 
@@ -15,7 +15,7 @@ Arguments:
15
15
 
16
16
  ### Parameter: `BalancedPoolOptions`
17
17
 
18
- Extends: [`PoolOptions`](Pool.md#parameter-pooloptions)
18
+ Extends: [`PoolOptions`](/docs/docs/api/Pool.md#parameter-pooloptions)
19
19
 
20
20
  * **factory** `(origin: URL, opts: Object) => Dispatcher` - Default: `(origin, opts) => new Pool(origin, opts)`
21
21
 
@@ -28,15 +28,15 @@ Returns an array of upstreams that were previously added.
28
28
 
29
29
  ### `BalancedPool.closed`
30
30
 
31
- Implements [Client.closed](Client.md#clientclosed)
31
+ Implements [Client.closed](/docs/docs/api/Client.md#clientclosed)
32
32
 
33
33
  ### `BalancedPool.destroyed`
34
34
 
35
- Implements [Client.destroyed](Client.md#clientdestroyed)
35
+ Implements [Client.destroyed](/docs/docs/api/Client.md#clientdestroyed)
36
36
 
37
37
  ### `Pool.stats`
38
38
 
39
- Returns [`PoolStats`](PoolStats.md) instance for this pool.
39
+ Returns [`PoolStats`](/docs/docs/api/PoolStats.md) instance for this pool.
40
40
 
41
41
  ## Instance Methods
42
42
 
@@ -54,46 +54,46 @@ Removes an upstream that was previously added.
54
54
 
55
55
  ### `BalancedPool.close([callback])`
56
56
 
57
- Implements [`Dispatcher.close([callback])`](Dispatcher.md#dispatcherclosecallback-promise).
57
+ Implements [`Dispatcher.close([callback])`](/docs/docs/api/Dispatcher.md#dispatcherclosecallback-promise).
58
58
 
59
59
  ### `BalancedPool.destroy([error, callback])`
60
60
 
61
- Implements [`Dispatcher.destroy([error, callback])`](Dispatcher.md#dispatcherdestroyerror-callback-promise).
61
+ Implements [`Dispatcher.destroy([error, callback])`](/docs/docs/api/Dispatcher.md#dispatcherdestroyerror-callback-promise).
62
62
 
63
63
  ### `BalancedPool.connect(options[, callback])`
64
64
 
65
- See [`Dispatcher.connect(options[, callback])`](Dispatcher.md#dispatcherconnectoptions-callback).
65
+ See [`Dispatcher.connect(options[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherconnectoptions-callback).
66
66
 
67
67
  ### `BalancedPool.dispatch(options, handlers)`
68
68
 
69
- Implements [`Dispatcher.dispatch(options, handlers)`](Dispatcher.md#dispatcherdispatchoptions-handler).
69
+ Implements [`Dispatcher.dispatch(options, handlers)`](/docs/docs/api/Dispatcher.md#dispatcherdispatchoptions-handler).
70
70
 
71
71
  ### `BalancedPool.pipeline(options, handler)`
72
72
 
73
- See [`Dispatcher.pipeline(options, handler)`](Dispatcher.md#dispatcherpipelineoptions-handler).
73
+ See [`Dispatcher.pipeline(options, handler)`](/docs/docs/api/Dispatcher.md#dispatcherpipelineoptions-handler).
74
74
 
75
75
  ### `BalancedPool.request(options[, callback])`
76
76
 
77
- See [`Dispatcher.request(options [, callback])`](Dispatcher.md#dispatcherrequestoptions-callback).
77
+ See [`Dispatcher.request(options [, callback])`](/docs/docs/api/Dispatcher.md#dispatcherrequestoptions-callback).
78
78
 
79
79
  ### `BalancedPool.stream(options, factory[, callback])`
80
80
 
81
- See [`Dispatcher.stream(options, factory[, callback])`](Dispatcher.md#dispatcherstreamoptions-factory-callback).
81
+ See [`Dispatcher.stream(options, factory[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherstreamoptions-factory-callback).
82
82
 
83
83
  ### `BalancedPool.upgrade(options[, callback])`
84
84
 
85
- See [`Dispatcher.upgrade(options[, callback])`](Dispatcher.md#dispatcherupgradeoptions-callback).
85
+ See [`Dispatcher.upgrade(options[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherupgradeoptions-callback).
86
86
 
87
87
  ## Instance Events
88
88
 
89
89
  ### Event: `'connect'`
90
90
 
91
- See [Dispatcher Event: `'connect'`](Dispatcher.md#event-connect).
91
+ See [Dispatcher Event: `'connect'`](/docs/docs/api/Dispatcher.md#event-connect).
92
92
 
93
93
  ### Event: `'disconnect'`
94
94
 
95
- See [Dispatcher Event: `'disconnect'`](Dispatcher.md#event-disconnect).
95
+ See [Dispatcher Event: `'disconnect'`](/docs/docs/api/Dispatcher.md#event-disconnect).
96
96
 
97
97
  ### Event: `'drain'`
98
98
 
99
- See [Dispatcher Event: `'drain'`](Dispatcher.md#event-drain).
99
+ See [Dispatcher Event: `'drain'`](/docs/docs/api/Dispatcher.md#event-drain).
@@ -0,0 +1,131 @@
1
+ # Cache Store
2
+
3
+ A Cache Store is responsible for storing and retrieving cached responses.
4
+ It is also responsible for deciding which specific response to use based off of
5
+ a response's `Vary` header (if present). It is expected to be compliant with
6
+ [RFC-9111](https://www.rfc-editor.org/rfc/rfc9111.html).
7
+
8
+ ## Pre-built Cache Stores
9
+
10
+ ### `MemoryCacheStore`
11
+
12
+ The `MemoryCacheStore` stores the responses in-memory.
13
+
14
+ **Options**
15
+
16
+ - `maxCount` - The maximum amount of responses to store. Default `Infinity`.
17
+ - `maxEntrySize` - The maximum size in bytes that a response's body can be. If a response's body is greater than or equal to this, the response will not be cached.
18
+
19
+ ### `SqliteCacheStore`
20
+
21
+ The `SqliteCacheStore` stores the responses in a SQLite database.
22
+ Under the hood, it uses Node.js' [`node:sqlite`](https://nodejs.org/api/sqlite.html) api.
23
+ The `SqliteCacheStore` is only exposed if the `node:sqlite` api is present.
24
+
25
+ **Options**
26
+
27
+ - `location` - The location of the SQLite database to use. Default `:memory:`.
28
+ - `maxCount` - The maximum number of entries to store in the database. Default `Infinity`.
29
+ - `maxEntrySize` - The maximum size in bytes that a resposne's body can be. If a response's body is greater than or equal to this, the response will not be cached. Default `Infinity`.
30
+
31
+ ## Defining a Custom Cache Store
32
+
33
+ The store must implement the following functions:
34
+
35
+ ### Getter: `isFull`
36
+
37
+ Optional. This tells the cache interceptor if the store is full or not. If this is true,
38
+ the cache interceptor will not attempt to cache the response.
39
+
40
+ ### Function: `get`
41
+
42
+ Parameters:
43
+
44
+ * **req** `Dispatcher.RequestOptions` - Incoming request
45
+
46
+ Returns: `GetResult | Promise<GetResult | undefined> | undefined` - If the request is cached, the cached response is returned. If the request's method is anything other than HEAD, the response is also returned.
47
+ If the request isn't cached, `undefined` is returned.
48
+
49
+ Response properties:
50
+
51
+ * **response** `CacheValue` - The cached response data.
52
+ * **body** `Readable | undefined` - The response's body.
53
+
54
+ ### Function: `createWriteStream`
55
+
56
+ Parameters:
57
+
58
+ * **req** `Dispatcher.RequestOptions` - Incoming request
59
+ * **value** `CacheValue` - Response to store
60
+
61
+ Returns: `Writable | undefined` - If the store is full, return `undefined`. Otherwise, return a writable so that the cache interceptor can stream the body and trailers to the store.
62
+
63
+ ## `CacheValue`
64
+
65
+ This is an interface containing the majority of a response's data (minus the body).
66
+
67
+ ### Property `statusCode`
68
+
69
+ `number` - The response's HTTP status code.
70
+
71
+ ### Property `statusMessage`
72
+
73
+ `string` - The response's HTTP status message.
74
+
75
+ ### Property `rawHeaders`
76
+
77
+ `Buffer[]` - The response's headers.
78
+
79
+ ### Property `vary`
80
+
81
+ `Record<string, string | string[]> | undefined` - The headers defined by the response's `Vary` header
82
+ and their respective values for later comparison
83
+
84
+ For example, for a response like
85
+ ```
86
+ Vary: content-encoding, accepts
87
+ content-encoding: utf8
88
+ accepts: application/json
89
+ ```
90
+
91
+ This would be
92
+ ```js
93
+ {
94
+ 'content-encoding': 'utf8',
95
+ accepts: 'application/json'
96
+ }
97
+ ```
98
+
99
+ ### Property `cachedAt`
100
+
101
+ `number` - Time in millis that this value was cached.
102
+
103
+ ### Property `staleAt`
104
+
105
+ `number` - Time in millis that this value is considered stale.
106
+
107
+ ### Property `deleteAt`
108
+
109
+ `number` - Time in millis that this value is to be deleted from the cache. This
110
+ is either the same sa staleAt or the `max-stale` caching directive.
111
+
112
+ The store must not return a response after the time defined in this property.
113
+
114
+ ## `CacheStoreReadable`
115
+
116
+ This extends Node's [`Readable`](https://nodejs.org/api/stream.html#class-streamreadable)
117
+ and defines extra properties relevant to the cache interceptor.
118
+
119
+ ### Getter: `value`
120
+
121
+ The response's [`CacheStoreValue`](/docs/docs/api/CacheStore.md#cachestorevalue)
122
+
123
+ ## `CacheStoreWriteable`
124
+
125
+ This extends Node's [`Writable`](https://nodejs.org/api/stream.html#class-streamwritable)
126
+ and defines extra properties relevant to the cache interceptor.
127
+
128
+ ### Setter: `rawTrailers`
129
+
130
+ If the response has trailers, the cache interceptor will pass them to the cache
131
+ interceptor through this method.
@@ -19,7 +19,7 @@ Returns: `Client`
19
19
 
20
20
  > ⚠️ Warning: The `H2` support is experimental.
21
21
 
22
- * **bodyTimeout** `number | null` (optional) - Default: `300e3` - The timeout after which a request will time out, in milliseconds. Monitors time between receiving body data. Use `0` to disable it entirely. Defaults to 300 seconds. Please note the `timeout` will be reset if you keep writing data to the scoket everytime.
22
+ * **bodyTimeout** `number | null` (optional) - Default: `300e3` - The timeout after which a request will time out, in milliseconds. Monitors time between receiving body data. Use `0` to disable it entirely. Defaults to 300 seconds. Please note the `timeout` will be reset if you keep writing data to the socket everytime.
23
23
  * **headersTimeout** `number | null` (optional) - Default: `300e3` - The amount of time, in milliseconds, the parser will wait to receive the complete HTTP headers while not sending the request. Defaults to 300 seconds.
24
24
  * **keepAliveMaxTimeout** `number | null` (optional) - Default: `600e3` - The maximum allowed `keepAliveTimeout`, in milliseconds, when overridden by *keep-alive* hints from the server. Defaults to 10 minutes.
25
25
  * **keepAliveTimeout** `number | null` (optional) - Default: `4e3` - The timeout, in milliseconds, after which a socket without active requests will time out. Monitors time between activity on a connected socket. This value may be overridden by *keep-alive* hints from the server. See [MDN: HTTP - Headers - Keep-Alive directives](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive#directives) for more details. Defaults to 4 seconds.
@@ -86,37 +86,37 @@ const client = new Client('https://localhost:3000', {
86
86
 
87
87
  ### `Client.close([callback])`
88
88
 
89
- Implements [`Dispatcher.close([callback])`](Dispatcher.md#dispatcherclosecallback-promise).
89
+ Implements [`Dispatcher.close([callback])`](/docs/docs/api/Dispatcher.md#dispatcherclosecallback-promise).
90
90
 
91
91
  ### `Client.destroy([error, callback])`
92
92
 
93
- Implements [`Dispatcher.destroy([error, callback])`](Dispatcher.md#dispatcherdestroyerror-callback-promise).
93
+ Implements [`Dispatcher.destroy([error, callback])`](/docs/docs/api/Dispatcher.md#dispatcherdestroyerror-callback-promise).
94
94
 
95
95
  Waits until socket is closed before invoking the callback (or returning a promise if no callback is provided).
96
96
 
97
97
  ### `Client.connect(options[, callback])`
98
98
 
99
- See [`Dispatcher.connect(options[, callback])`](Dispatcher.md#dispatcherconnectoptions-callback).
99
+ See [`Dispatcher.connect(options[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherconnectoptions-callback).
100
100
 
101
101
  ### `Client.dispatch(options, handlers)`
102
102
 
103
- Implements [`Dispatcher.dispatch(options, handlers)`](Dispatcher.md#dispatcherdispatchoptions-handler).
103
+ Implements [`Dispatcher.dispatch(options, handlers)`](/docs/docs/api/Dispatcher.md#dispatcherdispatchoptions-handler).
104
104
 
105
105
  ### `Client.pipeline(options, handler)`
106
106
 
107
- See [`Dispatcher.pipeline(options, handler)`](Dispatcher.md#dispatcherpipelineoptions-handler).
107
+ See [`Dispatcher.pipeline(options, handler)`](/docs/docs/api/Dispatcher.md#dispatcherpipelineoptions-handler).
108
108
 
109
109
  ### `Client.request(options[, callback])`
110
110
 
111
- See [`Dispatcher.request(options [, callback])`](Dispatcher.md#dispatcherrequestoptions-callback).
111
+ See [`Dispatcher.request(options [, callback])`](/docs/docs/api/Dispatcher.md#dispatcherrequestoptions-callback).
112
112
 
113
113
  ### `Client.stream(options, factory[, callback])`
114
114
 
115
- See [`Dispatcher.stream(options, factory[, callback])`](Dispatcher.md#dispatcherstreamoptions-factory-callback).
115
+ See [`Dispatcher.stream(options, factory[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherstreamoptions-factory-callback).
116
116
 
117
117
  ### `Client.upgrade(options[, callback])`
118
118
 
119
- See [`Dispatcher.upgrade(options[, callback])`](Dispatcher.md#dispatcherupgradeoptions-callback).
119
+ See [`Dispatcher.upgrade(options[, callback])`](/docs/docs/api/Dispatcher.md#dispatcherupgradeoptions-callback).
120
120
 
121
121
  ## Instance Properties
122
122
 
@@ -142,7 +142,7 @@ Property to get and set the pipelining factor.
142
142
 
143
143
  ### Event: `'connect'`
144
144
 
145
- See [Dispatcher Event: `'connect'`](Dispatcher.md#event-connect).
145
+ See [Dispatcher Event: `'connect'`](/docs/docs/api/Dispatcher.md#event-connect).
146
146
 
147
147
  Parameters:
148
148
 
@@ -188,7 +188,7 @@ try {
188
188
 
189
189
  ### Event: `'disconnect'`
190
190
 
191
- See [Dispatcher Event: `'disconnect'`](Dispatcher.md#event-disconnect).
191
+ See [Dispatcher Event: `'disconnect'`](/docs/docs/api/Dispatcher.md#event-disconnect).
192
192
 
193
193
  Parameters:
194
194
 
@@ -233,7 +233,7 @@ try {
233
233
 
234
234
  Emitted when pipeline is no longer busy.
235
235
 
236
- See [Dispatcher Event: `'drain'`](Dispatcher.md#event-drain).
236
+ See [Dispatcher Event: `'drain'`](/docs/docs/api/Dispatcher.md#event-drain).
237
237
 
238
238
  #### Example - Client drain event
239
239
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Undici (and subsenquently `fetch` and `websocket`) exposes a debug statement that can be enabled by setting `NODE_DEBUG` within the environment.
4
4
 
5
- The flags availabile are:
5
+ The flags available are:
6
6
 
7
7
  ## `undici`
8
8