Skip to content

Commit 4662a5e

Browse files
deps: update undici to 7.24.6
1 parent cc96741 commit 4662a5e

File tree

21 files changed

+1225
-752
lines changed

21 files changed

+1225
-752
lines changed

deps/undici/src/CONTRIBUTING.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ Create a commit which includes all of the updated files in lib/llhttp.
9696

9797
### Steps:
9898

99+
`npm run test:wpt` and `node test/web-platform-tests/wpt-runner.mjs setup` will initialize the WPT submodule automatically when it is missing.
100+
101+
If you want to prepare the checkout explicitly, run:
102+
99103
```bash
100104
git submodule update --init --recursive
101105
```

deps/undici/src/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,57 @@ const { statusCode, body } = await request('https://api.example.com/data');
154154
const data = await body.json();
155155
```
156156

157+
### Keep `fetch` and `FormData` together
158+
159+
When you send a `FormData` body, keep `fetch` and `FormData` from the same
160+
implementation.
161+
162+
Use one of these patterns:
163+
164+
```js
165+
// Built-in globals
166+
const body = new FormData()
167+
body.set('name', 'some')
168+
await fetch('https://example.com', {
169+
method: 'POST',
170+
body
171+
})
172+
```
173+
174+
```js
175+
// undici module imports
176+
import { fetch, FormData } from 'undici'
177+
178+
const body = new FormData()
179+
body.set('name', 'some')
180+
await fetch('https://example.com', {
181+
method: 'POST',
182+
body
183+
})
184+
```
185+
186+
If you want the installed `undici` package to provide the globals, call
187+
`install()` first:
188+
189+
```js
190+
import { install } from 'undici'
191+
192+
install()
193+
194+
const body = new FormData()
195+
body.set('name', 'some')
196+
await fetch('https://example.com', {
197+
method: 'POST',
198+
body
199+
})
200+
```
201+
202+
`install()` replaces the global `fetch`, `Headers`, `Response`, `Request`, and
203+
`FormData` implementations with undici's versions, so they all match.
204+
205+
Avoid mixing a global `FormData` with `undici.fetch()`, or `undici.FormData`
206+
with the built-in global `fetch()`.
207+
157208
### Version Compatibility
158209

159210
You can check which version of undici is bundled with your Node.js version:
@@ -263,6 +314,11 @@ The `install()` function adds the following classes to `globalThis`:
263314
- `CloseEvent`, `ErrorEvent`, `MessageEvent` - WebSocket events
264315
- `EventSource` - Server-sent events client
265316

317+
When you call `install()`, these globals come from the same undici
318+
implementation. For example, global `fetch` and global `FormData` will both be
319+
undici's versions, which is the recommended setup if you want to use undici
320+
through globals.
321+
266322
This is useful for:
267323
- Polyfilling environments that don't have fetch
268324
- Ensuring consistent fetch behavior across different Node.js versions

deps/undici/src/docs/docs/api/DiagnosticsChannel.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,24 @@ diagnosticsChannel.channel('undici:websocket:open').subscribe(({
182182
console.log(websocket) // the WebSocket instance
183183

184184
// Handshake response details
185-
console.log(handshakeResponse.status) // 101 for successful WebSocket upgrade
186-
console.log(handshakeResponse.statusText) // 'Switching Protocols'
185+
console.log(handshakeResponse.status) // 101 for HTTP/1.1, 200 for HTTP/2 extended CONNECT
186+
console.log(handshakeResponse.statusText) // 'Switching Protocols' for HTTP/1.1, commonly 'OK' for HTTP/2 in Node.js
187187
console.log(handshakeResponse.headers) // Object containing response headers
188188
})
189189
```
190190

191191
### Handshake Response Object
192192

193-
The `handshakeResponse` object contains the HTTP response that upgraded the connection to WebSocket:
193+
The `handshakeResponse` object contains the HTTP response that established the WebSocket connection:
194194

195-
- `status` (number): The HTTP status code (101 for successful WebSocket upgrade)
196-
- `statusText` (string): The HTTP status message ('Switching Protocols' for successful upgrade)
195+
- `status` (number): The HTTP status code (`101` for HTTP/1.1 upgrade, `200` for HTTP/2 extended CONNECT)
196+
- `statusText` (string): The HTTP status message (`'Switching Protocols'` for HTTP/1.1, commonly `'OK'` for HTTP/2 in Node.js)
197197
- `headers` (object): The HTTP response headers from the server, including:
198+
- `sec-websocket-accept` and other WebSocket-related headers
198199
- `upgrade: 'websocket'`
199200
- `connection: 'upgrade'`
200-
- `sec-websocket-accept` and other WebSocket-related headers
201+
202+
The `upgrade` and `connection` headers are only present for HTTP/1.1 handshakes.
201203

202204
This information is particularly useful for debugging and monitoring WebSocket connections, as it provides access to the initial HTTP handshake response that established the WebSocket connection.
203205

deps/undici/src/docs/docs/api/Fetch.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ This API is implemented as per the standard, you can find documentation on [MDN]
1010

1111
If any parameters are passed to the FormData constructor other than `undefined`, an error will be thrown. Other parameters are ignored.
1212

13+
When you use `FormData` as a request body, keep `fetch` and `FormData` from the
14+
same implementation. Use the built-in global `FormData` with the built-in
15+
global `fetch()`, and use `undici`'s `FormData` with `undici.fetch()`.
16+
17+
If you want the installed `undici` package to provide the globals, call
18+
[`install()`](/docs/api/GlobalInstallation.md) so `fetch`, `Headers`,
19+
`Response`, `Request`, and `FormData` are installed together as a matching set.
20+
1321
## Response
1422

1523
This API is implemented as per the standard, you can find documentation on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Response)

deps/undici/src/docs/docs/api/GlobalInstallation.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,54 @@ The `install()` function adds the following classes to `globalThis`:
4343
| `MessageEvent` | WebSocket message event |
4444
| `EventSource` | Server-sent events client |
4545

46+
## Using `FormData` with `fetch`
47+
48+
If you send a `FormData` body, use matching implementations for `fetch` and
49+
`FormData`.
50+
51+
These two patterns are safe:
52+
53+
```js
54+
// Built-in globals from Node.js
55+
const body = new FormData()
56+
await fetch('https://example.com', {
57+
method: 'POST',
58+
body
59+
})
60+
```
61+
62+
```js
63+
// Globals installed from the undici package
64+
import { install } from 'undici'
65+
66+
install()
67+
68+
const body = new FormData()
69+
await fetch('https://example.com', {
70+
method: 'POST',
71+
body
72+
})
73+
```
74+
75+
After `install()`, `fetch`, `Headers`, `Response`, `Request`, and `FormData`
76+
all come from the installed `undici` package, so they work as a matching set.
77+
78+
If you do not want to install globals, import both from `undici` instead:
79+
80+
```js
81+
import { fetch, FormData } from 'undici'
82+
83+
const body = new FormData()
84+
await fetch('https://example.com', {
85+
method: 'POST',
86+
body
87+
})
88+
```
89+
90+
Avoid mixing a global `FormData` with `undici.fetch()`, or `undici.FormData`
91+
with the built-in global `fetch()`. Keeping them paired avoids surprising
92+
multipart behavior across Node.js and undici versions.
93+
4694
## Use Cases
4795

4896
Global installation is useful for:

deps/undici/src/docs/docs/best-practices/undici-vs-builtin-fetch.md

Lines changed: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,93 @@ When you install undici from npm, you get the full library with all of its
1919
additional APIs, and potentially a newer release than what your Node.js version
2020
bundles.
2121

22+
## Keep `fetch` and `FormData` from the same implementation
23+
24+
When you send a `FormData` body, keep `fetch` and `FormData` together from the
25+
same implementation.
26+
27+
Use one of these patterns:
28+
29+
### Built-in globals
30+
31+
```js
32+
const body = new FormData()
33+
body.set('name', 'some')
34+
body.set('someOtherProperty', '8000')
35+
36+
await fetch('https://example.com', {
37+
method: 'POST',
38+
body
39+
})
40+
```
41+
42+
### `undici` module imports
43+
44+
```js
45+
import { fetch, FormData } from 'undici'
46+
47+
const body = new FormData()
48+
body.set('name', 'some')
49+
body.set('someOtherProperty', '8000')
50+
51+
await fetch('https://example.com', {
52+
method: 'POST',
53+
body
54+
})
55+
```
56+
57+
### `undici.install()` globals
58+
59+
If you want the installed `undici` package to provide the globals, call
60+
[`install()`](/docs/api/GlobalInstallation.md):
61+
62+
```js
63+
import { install } from 'undici'
64+
65+
install()
66+
67+
const body = new FormData()
68+
body.set('name', 'some')
69+
body.set('someOtherProperty', '8000')
70+
71+
await fetch('https://example.com', {
72+
method: 'POST',
73+
body
74+
})
75+
```
76+
77+
`install()` replaces the global `fetch`, `Headers`, `Response`, `Request`, and
78+
`FormData` implementations with undici's versions, and also installs undici's
79+
`WebSocket`, `CloseEvent`, `ErrorEvent`, `MessageEvent`, and `EventSource`
80+
globals.
81+
82+
Avoid mixing implementations in the same request, for example:
83+
84+
```js
85+
import { fetch } from 'undici'
86+
87+
const body = new FormData()
88+
89+
await fetch('https://example.com', {
90+
method: 'POST',
91+
body
92+
})
93+
```
94+
95+
```js
96+
import { FormData } from 'undici'
97+
98+
const body = new FormData()
99+
100+
await fetch('https://example.com', {
101+
method: 'POST',
102+
body
103+
})
104+
```
105+
106+
Those combinations may behave differently across Node.js and undici versions.
107+
Using matching pairs keeps multipart handling predictable.
108+
22109
## When you do NOT need to install undici
23110

24111
If all of the following are true, you can rely on the built-in globals and skip
@@ -119,12 +206,12 @@ You can always check the exact bundled version at runtime with
119206
`process.versions.undici`.
120207

121208
Installing undici from npm does not replace the built-in globals. If you want
122-
your installed version to override the global `fetch`, use
123-
[`setGlobalDispatcher`](/docs/api/GlobalInstallation.md) or import `fetch`
209+
your installed version to replace the global `fetch` and related classes, use
210+
[`install()`](/docs/api/GlobalInstallation.md). Otherwise, import `fetch`
124211
directly from `'undici'`:
125212

126213
```js
127-
import { fetch } from 'undici'; // uses your installed version, not the built-in
214+
import { fetch } from 'undici' // uses your installed version, not the built-in
128215
```
129216

130217
## Further reading

deps/undici/src/docs/docs/best-practices/writing-tests.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,46 @@ const agent = new Agent({
1818

1919
setGlobalDispatcher(agent)
2020
```
21+
22+
## Guarding against unexpected disconnects
23+
24+
Undici's `Client` automatically reconnects after a socket error. This means
25+
a test can silently disconnect, reconnect, and still pass. Unfortunately,
26+
this could mask bugs like unexpected parser errors or protocol violations.
27+
To catch these silent reconnections, add a disconnect guard after creating
28+
a `Client`:
29+
30+
```js
31+
const { Client } = require('undici')
32+
const { test, after } = require('node:test')
33+
const { tspl } = require('@matteo.collina/tspl')
34+
35+
test('example with disconnect guard', async (t) => {
36+
t = tspl(t, { plan: 1 })
37+
38+
const client = new Client('http://localhost:3000')
39+
after(() => client.close())
40+
41+
client.on('disconnect', () => {
42+
if (!client.closed && !client.destroyed) {
43+
t.fail('unexpected disconnect')
44+
}
45+
})
46+
47+
// ... test logic ...
48+
})
49+
```
50+
51+
`client.close()` and `client.destroy()` both emit `'disconnect'` events, but
52+
those are expected. The guard only fails when a disconnect happens during the
53+
active test (i.e., `!client.closed && !client.destroyed` is true).
54+
55+
Skip the guard for tests where a disconnect is expected behavior, such as:
56+
57+
- Signal aborts (`signal.emit('abort')`, `ac.abort()`)
58+
- Server-side destruction (`res.destroy()`, `req.socket.destroy()`)
59+
- Client-side body destruction mid-stream (`data.body.destroy()`)
60+
- Timeout errors (`HeadersTimeoutError`, `BodyTimeoutError`)
61+
- Successful upgrades (the socket is detached from the `Client`)
62+
- Retry/reconnect tests where the disconnect triggers the retry
63+
- HTTP parser errors from malformed responses (`HTTPParserError`)

deps/undici/src/lib/core/diagnostics.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,12 @@ function trackWebSocketEvents (debugLog = websocketDebuglog) {
177177

178178
diagnosticsChannel.subscribe('undici:websocket:open',
179179
evt => {
180-
const {
181-
address: { address, port }
182-
} = evt
183-
debugLog('connection opened %s%s', address, port ? `:${port}` : '')
180+
if (evt.address != null) {
181+
const { address, port } = evt.address
182+
debugLog('connection opened %s%s', address, port ? `:${port}` : '')
183+
} else {
184+
debugLog('connection opened')
185+
}
184186
})
185187

186188
diagnosticsChannel.subscribe('undici:websocket:close',

deps/undici/src/lib/core/request.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -412,13 +412,21 @@ function processHeader (request, key, val) {
412412
} else if (headerName === 'transfer-encoding' || headerName === 'keep-alive' || headerName === 'upgrade') {
413413
throw new InvalidArgumentError(`invalid ${headerName} header`)
414414
} else if (headerName === 'connection') {
415-
const value = typeof val === 'string' ? val.toLowerCase() : null
416-
if (value !== 'close' && value !== 'keep-alive') {
415+
// Per RFC 7230 Section 6.1, Connection header can contain
416+
// a comma-separated list of connection option tokens (header names)
417+
const value = typeof val === 'string' ? val : null
418+
if (value === null) {
417419
throw new InvalidArgumentError('invalid connection header')
418420
}
419421

420-
if (value === 'close') {
421-
request.reset = true
422+
for (const token of value.toLowerCase().split(',')) {
423+
const trimmed = token.trim()
424+
if (!isValidHTTPToken(trimmed)) {
425+
throw new InvalidArgumentError('invalid connection header')
426+
}
427+
if (trimmed === 'close') {
428+
request.reset = true
429+
}
422430
}
423431
} else if (headerName === 'expect') {
424432
throw new NotSupportedError('expect header not supported')

0 commit comments

Comments
 (0)