undici 7.9.0 → 7.11.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 (66) hide show
  1. package/README.md +157 -0
  2. package/docs/docs/api/CacheStore.md +23 -3
  3. package/docs/docs/api/Debug.md +13 -13
  4. package/docs/docs/api/DiagnosticsChannel.md +25 -0
  5. package/docs/docs/api/Dispatcher.md +20 -1
  6. package/docs/docs/api/GlobalInstallation.md +91 -0
  7. package/docs/docs/api/MockClient.md +4 -0
  8. package/docs/docs/api/MockPool.md +6 -0
  9. package/docs/docs/api/Pool.md +1 -0
  10. package/docs/docs/api/ProxyAgent.md +3 -0
  11. package/docs/docs/api/RetryAgent.md +6 -1
  12. package/docs/docs/api/RetryHandler.md +1 -0
  13. package/index.js +15 -0
  14. package/lib/api/api-stream.js +1 -1
  15. package/lib/cache/memory-cache-store.js +42 -4
  16. package/lib/cache/sqlite-cache-store.js +1 -1
  17. package/lib/core/connect.js +21 -51
  18. package/lib/core/diagnostics.js +6 -4
  19. package/lib/core/request.js +6 -0
  20. package/lib/core/util.js +0 -45
  21. package/lib/dispatcher/agent.js +25 -15
  22. package/lib/dispatcher/client-h1.js +1 -1
  23. package/lib/dispatcher/pool.js +17 -3
  24. package/lib/dispatcher/proxy-agent.js +90 -3
  25. package/lib/handler/retry-handler.js +110 -56
  26. package/lib/mock/mock-agent.js +8 -8
  27. package/lib/mock/mock-client.js +4 -0
  28. package/lib/mock/mock-pool.js +4 -0
  29. package/lib/util/cache.js +11 -1
  30. package/lib/util/timers.js +11 -9
  31. package/lib/web/cache/cache.js +1 -1
  32. package/lib/web/cache/cachestorage.js +1 -1
  33. package/lib/web/cookies/index.js +1 -1
  34. package/lib/web/eventsource/eventsource.js +3 -6
  35. package/lib/web/eventsource/util.js +1 -1
  36. package/lib/web/fetch/body.js +2 -2
  37. package/lib/web/fetch/dispatcher-weakref.js +0 -41
  38. package/lib/web/fetch/formdata-parser.js +4 -4
  39. package/lib/web/fetch/formdata.js +1 -1
  40. package/lib/web/fetch/headers.js +1 -1
  41. package/lib/web/fetch/index.js +7 -1
  42. package/lib/web/fetch/request.js +1 -1
  43. package/lib/web/fetch/response.js +1 -1
  44. package/lib/web/fetch/util.js +2 -2
  45. package/lib/web/{fetch/webidl.js → webidl/index.js} +57 -9
  46. package/lib/web/websocket/connection.js +4 -3
  47. package/lib/web/websocket/events.js +1 -1
  48. package/lib/web/websocket/frame.js +2 -1
  49. package/lib/web/websocket/stream/websocketerror.js +1 -1
  50. package/lib/web/websocket/stream/websocketstream.js +1 -1
  51. package/lib/web/websocket/websocket.js +4 -4
  52. package/package.json +4 -4
  53. package/types/diagnostics-channel.d.ts +9 -0
  54. package/types/dispatcher.d.ts +3 -2
  55. package/types/env-http-proxy-agent.d.ts +2 -1
  56. package/types/eventsource.d.ts +3 -3
  57. package/types/fetch.d.ts +1 -0
  58. package/types/handlers.d.ts +1 -1
  59. package/types/mock-client.d.ts +2 -0
  60. package/types/mock-interceptor.d.ts +2 -0
  61. package/types/mock-pool.d.ts +2 -0
  62. package/types/pool.d.ts +2 -0
  63. package/types/proxy-agent.d.ts +1 -0
  64. package/types/retry-handler.d.ts +9 -0
  65. package/types/webidl.d.ts +19 -15
  66. package/types/websocket.d.ts +1 -1
package/README.md CHANGED
@@ -43,6 +43,125 @@ The benchmark is a simple getting data [example](https://github.com/nodejs/undic
43
43
  └────────────────────────┴─────────┴────────────────────┴────────────┴─────────────────────────┘
44
44
  ```
45
45
 
46
+ ## Undici vs. Fetch
47
+
48
+ ### Overview
49
+
50
+ Node.js includes a built-in `fetch()` implementation powered by undici starting from Node.js v18. However, there are important differences between using the built-in fetch and installing undici as a separate module.
51
+
52
+ ### Built-in Fetch (Node.js v18+)
53
+
54
+ Node.js's built-in fetch is powered by a bundled version of undici:
55
+
56
+ ```js
57
+ // Available globally in Node.js v18+
58
+ const response = await fetch('https://api.example.com/data');
59
+ const data = await response.json();
60
+
61
+ // Check the bundled undici version
62
+ console.log(process.versions.undici); // e.g., "5.28.4"
63
+ ```
64
+
65
+ **Pros:**
66
+ - No additional dependencies required
67
+ - Works across different JavaScript runtimes
68
+ - Automatic compression handling (gzip, deflate, br)
69
+ - Built-in caching support (in development)
70
+
71
+ **Cons:**
72
+ - Limited to the undici version bundled with your Node.js version
73
+ - Less control over connection pooling and advanced features
74
+ - Error handling follows Web API standards (errors wrapped in `TypeError`)
75
+ - Performance overhead due to Web Streams implementation
76
+
77
+ ### Undici Module
78
+
79
+ Installing undici as a separate module gives you access to the latest features and APIs:
80
+
81
+ ```bash
82
+ npm install undici
83
+ ```
84
+
85
+ ```js
86
+ import { request, fetch, Agent, setGlobalDispatcher } from 'undici';
87
+
88
+ // Use undici.request for maximum performance
89
+ const { statusCode, headers, body } = await request('https://api.example.com/data');
90
+ const data = await body.json();
91
+
92
+ // Or use undici.fetch with custom configuration
93
+ const agent = new Agent({ keepAliveTimeout: 10000 });
94
+ setGlobalDispatcher(agent);
95
+ const response = await fetch('https://api.example.com/data');
96
+ ```
97
+
98
+ **Pros:**
99
+ - Latest undici features and bug fixes
100
+ - Access to advanced APIs (`request`, `stream`, `pipeline`)
101
+ - Fine-grained control over connection pooling
102
+ - Better error handling with clearer error messages
103
+ - Superior performance, especially with `undici.request`
104
+ - HTTP/1.1 pipelining support
105
+ - Custom interceptors and middleware
106
+ - Advanced features like `ProxyAgent`, `MockAgent`
107
+
108
+ **Cons:**
109
+ - Additional dependency to manage
110
+ - Larger bundle size
111
+
112
+ ### When to Use Each
113
+
114
+ #### Use Built-in Fetch When:
115
+ - You want zero dependencies
116
+ - Building isomorphic code that runs in browsers and Node.js
117
+ - Simple HTTP requests without advanced configuration
118
+ - You're okay with the undici version bundled in your Node.js version
119
+
120
+ #### Use Undici Module When:
121
+ - You need the latest undici features and performance improvements
122
+ - You require advanced connection pooling configuration
123
+ - You need APIs not available in the built-in fetch (`ProxyAgent`, `MockAgent`, etc.)
124
+ - Performance is critical (use `undici.request` for maximum speed)
125
+ - You want better error handling and debugging capabilities
126
+ - You need HTTP/1.1 pipelining or advanced interceptors
127
+ - You prefer decoupled protocol and API interfaces
128
+
129
+ ### Performance Comparison
130
+
131
+ Based on benchmarks, here's the typical performance hierarchy:
132
+
133
+ 1. **`undici.request()`** - Fastest, most efficient
134
+ 2. **`undici.fetch()`** - Good performance, standard compliance
135
+ 3. **Node.js `http`/`https`** - Baseline performance
136
+
137
+ ### Migration Guide
138
+
139
+ If you're currently using built-in fetch and want to migrate to undici:
140
+
141
+ ```js
142
+ // Before: Built-in fetch
143
+ const response = await fetch('https://api.example.com/data');
144
+
145
+ // After: Undici fetch (drop-in replacement)
146
+ import { fetch } from 'undici';
147
+ const response = await fetch('https://api.example.com/data');
148
+
149
+ // Or: Undici request (better performance)
150
+ import { request } from 'undici';
151
+ const { statusCode, body } = await request('https://api.example.com/data');
152
+ const data = await body.json();
153
+ ```
154
+
155
+ ### Version Compatibility
156
+
157
+ You can check which version of undici is bundled with your Node.js version:
158
+
159
+ ```js
160
+ console.log(process.versions.undici);
161
+ ```
162
+
163
+ Installing undici as a module allows you to use a newer version than what's bundled with Node.js, giving you access to the latest features and performance improvements.
164
+
46
165
  ## Quick Start
47
166
 
48
167
  ```js
@@ -63,6 +182,44 @@ for await (const data of body) { console.log('data', data) }
63
182
  console.log('trailers', trailers)
64
183
  ```
65
184
 
185
+ ## Global Installation
186
+
187
+ Undici provides an `install()` function to add all WHATWG fetch classes to `globalThis`, making them available globally:
188
+
189
+ ```js
190
+ import { install } from 'undici'
191
+
192
+ // Install all WHATWG fetch classes globally
193
+ install()
194
+
195
+ // Now you can use fetch classes globally without importing
196
+ const response = await fetch('https://api.example.com/data')
197
+ const data = await response.json()
198
+
199
+ // All classes are available globally:
200
+ const headers = new Headers([['content-type', 'application/json']])
201
+ const request = new Request('https://example.com')
202
+ const formData = new FormData()
203
+ const ws = new WebSocket('wss://example.com')
204
+ const eventSource = new EventSource('https://example.com/events')
205
+ ```
206
+
207
+ The `install()` function adds the following classes to `globalThis`:
208
+
209
+ - `fetch` - The fetch function
210
+ - `Headers` - HTTP headers management
211
+ - `Response` - HTTP response representation
212
+ - `Request` - HTTP request representation
213
+ - `FormData` - Form data handling
214
+ - `WebSocket` - WebSocket client
215
+ - `CloseEvent`, `ErrorEvent`, `MessageEvent` - WebSocket events
216
+ - `EventSource` - Server-sent events client
217
+
218
+ This is useful for:
219
+ - Polyfilling environments that don't have fetch
220
+ - Ensuring consistent fetch behavior across different Node.js versions
221
+ - Making undici's implementations available globally for libraries that expect them
222
+
66
223
  ## Body Mixins
67
224
 
68
225
  The `body` mixins are the most common way to format the request/response body. Mixins include:
@@ -13,8 +13,28 @@ The `MemoryCacheStore` stores the responses in-memory.
13
13
 
14
14
  **Options**
15
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.
16
+ - `maxSize` - The maximum total size in bytes of all stored responses. Default `104857600` (100MB).
17
+ - `maxCount` - The maximum amount of responses to store. Default `1024`.
18
+ - `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. Default `5242880` (5MB).
19
+
20
+ ### Getters
21
+
22
+ #### `MemoryCacheStore.size`
23
+
24
+ Returns the current total size in bytes of all stored responses.
25
+
26
+ ### Methods
27
+
28
+ #### `MemoryCacheStore.isFull()`
29
+
30
+ Returns a boolean indicating whether the cache has reached its maximum size or count.
31
+
32
+ ### Events
33
+
34
+ #### `'maxSizeExceeded'`
35
+
36
+ Emitted when the cache exceeds its maximum size or count limits. The event payload contains `size`, `maxSize`, `count`, and `maxCount` properties.
37
+
18
38
 
19
39
  ### `SqliteCacheStore`
20
40
 
@@ -26,7 +46,7 @@ The `SqliteCacheStore` is only exposed if the `node:sqlite` api is present.
26
46
 
27
47
  - `location` - The location of the SQLite database to use. Default `:memory:`.
28
48
  - `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`.
49
+ - `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. Default `Infinity`.
30
50
 
31
51
  ## Defining a Custom Cache Store
32
52
 
@@ -14,14 +14,14 @@ NODE_DEBUG=undici node script.js
14
14
  UNDICI 16241: connecting to nodejs.org using https:h1
15
15
  UNDICI 16241: connecting to nodejs.org using https:h1
16
16
  UNDICI 16241: connected to nodejs.org using https:h1
17
- UNDICI 16241: sending request to GET https://nodejs.org//
18
- UNDICI 16241: received response to GET https://nodejs.org// - HTTP 307
17
+ UNDICI 16241: sending request to GET https://nodejs.org/
18
+ UNDICI 16241: received response to GET https://nodejs.org/ - HTTP 307
19
19
  UNDICI 16241: connecting to nodejs.org using https:h1
20
- UNDICI 16241: trailers received from GET https://nodejs.org//
20
+ UNDICI 16241: trailers received from GET https://nodejs.org/
21
21
  UNDICI 16241: connected to nodejs.org using https:h1
22
- UNDICI 16241: sending request to GET https://nodejs.org//en
23
- UNDICI 16241: received response to GET https://nodejs.org//en - HTTP 200
24
- UNDICI 16241: trailers received from GET https://nodejs.org//en
22
+ UNDICI 16241: sending request to GET https://nodejs.org/en
23
+ UNDICI 16241: received response to GET https://nodejs.org/en - HTTP 200
24
+ UNDICI 16241: trailers received from GET https://nodejs.org/en
25
25
  ```
26
26
 
27
27
  ## `fetch`
@@ -36,14 +36,14 @@ NODE_DEBUG=fetch node script.js
36
36
  FETCH 16241: connecting to nodejs.org using https:h1
37
37
  FETCH 16241: connecting to nodejs.org using https:h1
38
38
  FETCH 16241: connected to nodejs.org using https:h1
39
- FETCH 16241: sending request to GET https://nodejs.org//
40
- FETCH 16241: received response to GET https://nodejs.org// - HTTP 307
39
+ FETCH 16241: sending request to GET https://nodejs.org/
40
+ FETCH 16241: received response to GET https://nodejs.org/ - HTTP 307
41
41
  FETCH 16241: connecting to nodejs.org using https:h1
42
- FETCH 16241: trailers received from GET https://nodejs.org//
42
+ FETCH 16241: trailers received from GET https://nodejs.org/
43
43
  FETCH 16241: connected to nodejs.org using https:h1
44
- FETCH 16241: sending request to GET https://nodejs.org//en
45
- FETCH 16241: received response to GET https://nodejs.org//en - HTTP 200
46
- FETCH 16241: trailers received from GET https://nodejs.org//en
44
+ FETCH 16241: sending request to GET https://nodejs.org/en
45
+ FETCH 16241: received response to GET https://nodejs.org/en - HTTP 200
46
+ FETCH 16241: trailers received from GET https://nodejs.org/en
47
47
  ```
48
48
 
49
49
  ## `websocket`
@@ -57,6 +57,6 @@ NODE_DEBUG=websocket node script.js
57
57
 
58
58
  WEBSOCKET 18309: connecting to echo.websocket.org using https:h1
59
59
  WEBSOCKET 18309: connected to echo.websocket.org using https:h1
60
- WEBSOCKET 18309: sending request to GET https://echo.websocket.org//
60
+ WEBSOCKET 18309: sending request to GET https://echo.websocket.org/
61
61
  WEBSOCKET 18309: connection opened <ip_address>
62
62
  ```
@@ -27,9 +27,22 @@ diagnosticsChannel.channel('undici:request:create').subscribe(({ request }) => {
27
27
 
28
28
  Note: a request is only loosely completed to a given socket.
29
29
 
30
+ ## `undici:request:bodyChunkSent`
31
+
32
+ This message is published when a chunk of the request body is being sent.
33
+
34
+ ```js
35
+ import diagnosticsChannel from 'diagnostics_channel'
36
+
37
+ diagnosticsChannel.channel('undici:request:bodyChunkSent').subscribe(({ request, chunk }) => {
38
+ // request is the same object undici:request:create
39
+ })
40
+ ```
30
41
 
31
42
  ## `undici:request:bodySent`
32
43
 
44
+ This message is published after the request body has been fully sent.
45
+
33
46
  ```js
34
47
  import diagnosticsChannel from 'diagnostics_channel'
35
48
 
@@ -54,6 +67,18 @@ diagnosticsChannel.channel('undici:request:headers').subscribe(({ request, respo
54
67
  })
55
68
  ```
56
69
 
70
+ ## `undici:request:bodyChunkReceived`
71
+
72
+ This message is published after a chunk of the response body has been received.
73
+
74
+ ```js
75
+ import diagnosticsChannel from 'diagnostics_channel'
76
+
77
+ diagnosticsChannel.channel('undici:request:bodyChunkReceived').subscribe(({ request, chunk }) => {
78
+ // request is the same object undici:request:create
79
+ })
80
+ ```
81
+
57
82
  ## `undici:request:trailers`
58
83
 
59
84
  This message is published after the response body and trailers have been received, i.e. the response has been completed.
@@ -841,9 +841,28 @@ try {
841
841
  Compose a new dispatcher from the current dispatcher and the given interceptors.
842
842
 
843
843
  > _Notes_:
844
- > - The order of the interceptors matters. The first interceptor will be the first to be called.
844
+ > - The order of the interceptors matters. The last interceptor will be the first to be called.
845
845
  > - It is important to note that the `interceptor` function should return a function that follows the `Dispatcher.dispatch` signature.
846
846
  > - Any fork of the chain of `interceptors` can lead to unexpected results.
847
+ >
848
+ > **Interceptor Stack Visualization:**
849
+ > ```
850
+ > compose([interceptor1, interceptor2, interceptor3])
851
+ >
852
+ > Request Flow:
853
+ > ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
854
+ > │ Request │───▶│interceptor3 │───▶│interceptor2 │───▶│interceptor1 │───▶│ dispatcher │
855
+ > └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘ │ .dispatch │
856
+ > ▲ ▲ ▲ └─────────────┘
857
+ > │ │ │ ▲
858
+ > (called first) (called second) (called last) │
859
+ > │
860
+ > ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
861
+ > │ Response │◀───│interceptor3 │◀───│interceptor2 │◀───│interceptor1 │◀─────────┘
862
+ > └─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘
863
+ >
864
+ > The interceptors are composed in reverse order due to function composition.
865
+ > ```
847
866
 
848
867
  Arguments:
849
868
 
@@ -0,0 +1,91 @@
1
+ # Global Installation
2
+
3
+ Undici provides an `install()` function to add all WHATWG fetch classes to `globalThis`, making them available globally without requiring imports.
4
+
5
+ ## `install()`
6
+
7
+ Install all WHATWG fetch classes globally on `globalThis`.
8
+
9
+ **Example:**
10
+
11
+ ```js
12
+ import { install } from 'undici'
13
+
14
+ // Install all WHATWG fetch classes globally
15
+ install()
16
+
17
+ // Now you can use fetch classes globally without importing
18
+ const response = await fetch('https://api.example.com/data')
19
+ const data = await response.json()
20
+
21
+ // All classes are available globally:
22
+ const headers = new Headers([['content-type', 'application/json']])
23
+ const request = new Request('https://example.com')
24
+ const formData = new FormData()
25
+ const ws = new WebSocket('wss://example.com')
26
+ const eventSource = new EventSource('https://example.com/events')
27
+ ```
28
+
29
+ ## Installed Classes
30
+
31
+ The `install()` function adds the following classes to `globalThis`:
32
+
33
+ | Class | Description |
34
+ |-------|-------------|
35
+ | `fetch` | The fetch function for making HTTP requests |
36
+ | `Headers` | HTTP headers management |
37
+ | `Response` | HTTP response representation |
38
+ | `Request` | HTTP request representation |
39
+ | `FormData` | Form data handling |
40
+ | `WebSocket` | WebSocket client |
41
+ | `CloseEvent` | WebSocket close event |
42
+ | `ErrorEvent` | WebSocket error event |
43
+ | `MessageEvent` | WebSocket message event |
44
+ | `EventSource` | Server-sent events client |
45
+
46
+ ## Use Cases
47
+
48
+ Global installation is useful for:
49
+
50
+ - **Polyfilling environments** that don't have native fetch support
51
+ - **Ensuring consistent behavior** across different Node.js versions
52
+ - **Library compatibility** when third-party libraries expect global fetch
53
+ - **Migration scenarios** where you want to replace built-in implementations
54
+ - **Testing environments** where you need predictable fetch behavior
55
+
56
+ ## Example: Polyfilling an Environment
57
+
58
+ ```js
59
+ import { install } from 'undici'
60
+
61
+ // Check if fetch is available and install if needed
62
+ if (typeof globalThis.fetch === 'undefined') {
63
+ install()
64
+ console.log('Undici fetch installed globally')
65
+ }
66
+
67
+ // Now fetch is guaranteed to be available
68
+ const response = await fetch('https://api.example.com')
69
+ ```
70
+
71
+ ## Example: Testing Environment
72
+
73
+ ```js
74
+ import { install } from 'undici'
75
+
76
+ // In test setup, ensure consistent fetch behavior
77
+ install()
78
+
79
+ // Now all tests use undici's implementations
80
+ test('fetch API test', async () => {
81
+ const response = await fetch('https://example.com')
82
+ expect(response).toBeInstanceOf(Response)
83
+ })
84
+ ```
85
+
86
+ ## Notes
87
+
88
+ - The `install()` function overwrites any existing global implementations
89
+ - Classes installed are undici's implementations, not Node.js built-ins
90
+ - This provides access to undici's latest features and performance improvements
91
+ - The global installation persists for the lifetime of the process
@@ -38,6 +38,10 @@ const mockClient = mockAgent.get('http://localhost:3000')
38
38
 
39
39
  Implements: [`MockPool.intercept(options)`](/docs/docs/api/MockPool.md#mockpoolinterceptoptions)
40
40
 
41
+ ### `MockClient.cleanMocks()`
42
+
43
+ Implements: [`MockPool.cleanMocks()`](/docs/docs/api/MockPool.md#mockpoolcleanmocks)
44
+
41
45
  ### `MockClient.close()`
42
46
 
43
47
  Implements: [`MockPool.close()`](/docs/docs/api/MockPool.md#mockpoolclose)
@@ -546,3 +546,9 @@ for await (const data of body) {
546
546
  console.log('data', data.toString('utf8')) // data foo
547
547
  }
548
548
  ```
549
+
550
+ ### `MockPool.cleanMocks()`
551
+
552
+ This method cleans up all the prepared mocks.
553
+
554
+ Returns: `void`
@@ -19,6 +19,7 @@ Extends: [`ClientOptions`](/docs/docs/api/Client.md#parameter-clientoptions)
19
19
 
20
20
  * **factory** `(origin: URL, opts: Object) => Dispatcher` - Default: `(origin, opts) => new Client(origin, opts)`
21
21
  * **connections** `number | null` (optional) - Default: `null` - The number of `Client` instances to create. When set to `null`, the `Pool` instance will create an unlimited amount of `Client` instances.
22
+ * **clientTtl** `number | null` (optional) - Default: `null` - The amount of time before a `Client` instance is removed from the `Pool` and closed. When set to `null`, `Client` instances will not be removed or closed based on age.
22
23
 
23
24
  ## Instance Properties
24
25
 
@@ -17,6 +17,8 @@ Returns: `ProxyAgent`
17
17
  Extends: [`AgentOptions`](/docs/docs/api/Agent.md#parameter-agentoptions)
18
18
  > It ommits `AgentOptions#connect`.
19
19
 
20
+ > **Note:** When `AgentOptions#connections` is set, and different from `0`, the non-standard [`proxy-connection` header](https://udger.com/resources/http-request-headers-detail?header=Proxy-Connection) will be set to `keep-alive` in the request.
21
+
20
22
  * **uri** `string | URL` (required) - The URI of the proxy server. This can be provided as a string, as an instance of the URL class, or as an object with a `uri` property of type string.
21
23
  If the `uri` is provided as a string or `uri` is an object with an `uri` property of type string, then it will be parsed into a `URL` object according to the [WHATWG URL Specification](https://url.spec.whatwg.org).
22
24
  For detailed information on the parsing process and potential validation errors, please refer to the ["Writing" section](https://url.spec.whatwg.org/#writing) of the WHATWG URL Specification.
@@ -25,6 +27,7 @@ For detailed information on the parsing process and potential validation errors,
25
27
  * **clientFactory** `(origin: URL, opts: Object) => Dispatcher` (optional) - Default: `(origin, opts) => new Pool(origin, opts)`
26
28
  * **requestTls** `BuildOptions` (optional) - Options object passed when creating the underlying socket via the connector builder for the request. It extends from [`Client#ConnectOptions`](/docs/docs/api/Client.md#parameter-connectoptions).
27
29
  * **proxyTls** `BuildOptions` (optional) - Options object passed when creating the underlying socket via the connector builder for the proxy server. It extends from [`Client#ConnectOptions`](/docs/docs/api/Client.md#parameter-connectoptions).
30
+ * **proxyTunnel** `boolean` (optional) - By default, ProxyAgent will request that the Proxy facilitate a tunnel between the endpoint and the agent. Setting `proxyTunnel` to false avoids issuing a CONNECT extension, and includes the endpoint domain and path in each request.
28
31
 
29
32
  Examples:
30
33
 
@@ -16,6 +16,7 @@ Returns: `ProxyAgent`
16
16
 
17
17
  ### Parameter: `RetryHandlerOptions`
18
18
 
19
+ - **throwOnError** `boolean` (optional) - Disable to prevent throwing error on last retry attept, useful if you need the body on errors from server or if you have custom error handler. Default: `true`
19
20
  - **retry** `(err: Error, context: RetryContext, callback: (err?: Error | null) => void) => void` (optional) - Function to be called after every retry. It should pass error if no more retries should be performed.
20
21
  - **maxRetries** `number` (optional) - Maximum number of retries. Default: `5`
21
22
  - **maxTimeout** `number` (optional) - Maximum number of milliseconds to wait before retrying. Default: `30000` (30 seconds)
@@ -39,7 +40,11 @@ import { Agent, RetryAgent } from 'undici'
39
40
 
40
41
  const agent = new RetryAgent(new Agent())
41
42
 
42
- const res = await agent.request('http://example.com')
43
+ const res = await agent.request({
44
+ method: 'GET',
45
+ origin: 'http://example.com',
46
+ path: '/',
47
+ })
43
48
  console.log(res.statusCode)
44
49
  console.log(await res.body.text())
45
50
  ```
@@ -19,6 +19,7 @@ Extends: [`Dispatch.DispatchOptions`](/docs/docs/api/Dispatcher.md#parameter-dis
19
19
 
20
20
  #### `RetryOptions`
21
21
 
22
+ - **throwOnError** `boolean` (optional) - Disable to prevent throwing error on last retry attept, useful if you need the body on errors from server or if you have custom error handler.
22
23
  - **retry** `(err: Error, context: RetryContext, callback: (err?: Error | null) => void) => number | null` (optional) - Function to be called after every retry. It should pass error if no more retries should be performed.
23
24
  - **maxRetries** `number` (optional) - Maximum number of retries. Default: `5`
24
25
  - **maxTimeout** `number` (optional) - Maximum number of milliseconds to wait before retrying. Default: `30000` (30 seconds)
package/index.js CHANGED
@@ -181,3 +181,18 @@ module.exports.mockErrors = mockErrors
181
181
  const { EventSource } = require('./lib/web/eventsource/eventsource')
182
182
 
183
183
  module.exports.EventSource = EventSource
184
+
185
+ function install () {
186
+ globalThis.fetch = module.exports.fetch
187
+ globalThis.Headers = module.exports.Headers
188
+ globalThis.Response = module.exports.Response
189
+ globalThis.Request = module.exports.Request
190
+ globalThis.FormData = module.exports.FormData
191
+ globalThis.WebSocket = module.exports.WebSocket
192
+ globalThis.CloseEvent = module.exports.CloseEvent
193
+ globalThis.ErrorEvent = module.exports.ErrorEvent
194
+ globalThis.MessageEvent = module.exports.MessageEvent
195
+ globalThis.EventSource = module.exports.EventSource
196
+ }
197
+
198
+ module.exports.install = install
@@ -117,7 +117,7 @@ class StreamHandler extends AsyncResource {
117
117
  const { callback, res, opaque, trailers, abort } = this
118
118
 
119
119
  this.res = null
120
- if (err || !res.readable) {
120
+ if (err || !res?.readable) {
121
121
  util.destroy(res, err)
122
122
  }
123
123
 
@@ -1,6 +1,7 @@
1
1
  'use strict'
2
2
 
3
3
  const { Writable } = require('node:stream')
4
+ const { EventEmitter } = require('node:events')
4
5
  const { assertCacheKey, assertCacheValue } = require('../util/cache.js')
5
6
 
6
7
  /**
@@ -12,20 +13,23 @@ const { assertCacheKey, assertCacheValue } = require('../util/cache.js')
12
13
 
13
14
  /**
14
15
  * @implements {CacheStore}
16
+ * @extends {EventEmitter}
15
17
  */
16
- class MemoryCacheStore {
17
- #maxCount = Infinity
18
- #maxSize = Infinity
19
- #maxEntrySize = Infinity
18
+ class MemoryCacheStore extends EventEmitter {
19
+ #maxCount = 1024
20
+ #maxSize = 104857600 // 100MB
21
+ #maxEntrySize = 5242880 // 5MB
20
22
 
21
23
  #size = 0
22
24
  #count = 0
23
25
  #entries = new Map()
26
+ #hasEmittedMaxSizeEvent = false
24
27
 
25
28
  /**
26
29
  * @param {import('../../types/cache-interceptor.d.ts').default.MemoryCacheStoreOpts | undefined} [opts]
27
30
  */
28
31
  constructor (opts) {
32
+ super()
29
33
  if (opts) {
30
34
  if (typeof opts !== 'object') {
31
35
  throw new TypeError('MemoryCacheStore options must be an object')
@@ -66,6 +70,22 @@ class MemoryCacheStore {
66
70
  }
67
71
  }
68
72
 
73
+ /**
74
+ * Get the current size of the cache in bytes
75
+ * @returns {number} The current size of the cache in bytes
76
+ */
77
+ get size () {
78
+ return this.#size
79
+ }
80
+
81
+ /**
82
+ * Check if the cache is full (either max size or max count reached)
83
+ * @returns {boolean} True if the cache is full, false otherwise
84
+ */
85
+ isFull () {
86
+ return this.#size >= this.#maxSize || this.#count >= this.#maxCount
87
+ }
88
+
69
89
  /**
70
90
  * @param {import('../../types/cache-interceptor.d.ts').default.CacheKey} req
71
91
  * @returns {import('../../types/cache-interceptor.d.ts').default.GetResult | undefined}
@@ -144,7 +164,20 @@ class MemoryCacheStore {
144
164
 
145
165
  store.#size += entry.size
146
166
 
167
+ // Check if cache is full and emit event if needed
147
168
  if (store.#size > store.#maxSize || store.#count > store.#maxCount) {
169
+ // Emit maxSizeExceeded event if we haven't already
170
+ if (!store.#hasEmittedMaxSizeEvent) {
171
+ store.emit('maxSizeExceeded', {
172
+ size: store.#size,
173
+ maxSize: store.#maxSize,
174
+ count: store.#count,
175
+ maxCount: store.#maxCount
176
+ })
177
+ store.#hasEmittedMaxSizeEvent = true
178
+ }
179
+
180
+ // Perform eviction
148
181
  for (const [key, entries] of store.#entries) {
149
182
  for (const entry of entries.splice(0, entries.length / 2)) {
150
183
  store.#size -= entry.size
@@ -154,6 +187,11 @@ class MemoryCacheStore {
154
187
  store.#entries.delete(key)
155
188
  }
156
189
  }
190
+
191
+ // Reset the event flag after eviction
192
+ if (store.#size < store.#maxSize && store.#count < store.#maxCount) {
193
+ store.#hasEmittedMaxSizeEvent = false
194
+ }
157
195
  }
158
196
 
159
197
  callback(null)
@@ -1,6 +1,6 @@
1
1
  'use strict'
2
2
 
3
- const { Writable } = require('stream')
3
+ const { Writable } = require('node:stream')
4
4
  const { assertCacheKey, assertCacheValue } = require('../util/cache.js')
5
5
 
6
6
  let DatabaseSync