Skip to content

Commit 7fc075b

Browse files
nodejs-github-bottargos
authored andcommitted
deps: update undici to 5.7.0
PR-URL: #43790 Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Robert Nagy <[email protected]>
1 parent e076955 commit 7fc075b

32 files changed

+2599
-659
lines changed

deps/undici/src/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ Implements [fetch](https://fetch.spec.whatwg.org/#fetch-method).
176176
* https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
177177
* https://fetch.spec.whatwg.org/#fetch-method
178178

179-
Only supported on Node 16.5+.
179+
Only supported on Node 16.8+.
180180

181181
This is [experimental](https://nodejs.org/api/documentation.html#documentation_stability_index) and is not yet fully compliant with the Fetch Standard.
182182
We plan to ship breaking changes to this feature until it is out of experimental.
@@ -283,6 +283,13 @@ const headers = await fetch(url)
283283
.then(res => res.headers)
284284
```
285285

286+
However, if you want to get only headers, it might be better to use `HEAD` request method. Usage of this method will obviate the need for consumption or cancelling of the response body. See [MDN - HTTP - HTTP request methods - HEAD](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD) for more details.
287+
288+
```js
289+
const headers = await fetch(url, { method: 'HEAD' })
290+
.then(res => res.headers)
291+
```
292+
286293
##### Forbidden and Safelisted Header Names
287294

288295
* https://fetch.spec.whatwg.org/#cors-safelisted-response-header-name

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -461,22 +461,22 @@ Arguments:
461461
* **options** `RequestOptions`
462462
* **callback** `(error: Error | null, data: ResponseData) => void` (optional)
463463

464-
Returns: `void | Promise<ResponseData>` - Only returns a `Promise` if no `callback` argument was passed
464+
Returns: `void | Promise<ResponseData>` - Only returns a `Promise` if no `callback` argument was passed.
465465

466466
#### Parameter: `RequestOptions`
467467

468468
Extends: [`DispatchOptions`](#parameter-dispatchoptions)
469469

470-
* **opaque** `unknown` (optional) - Default: `null` - Used for passing through context to `ResponseData`
471-
* **signal** `AbortSignal | events.EventEmitter | null` (optional) - Default: `null`
470+
* **opaque** `unknown` (optional) - Default: `null` - Used for passing through context to `ResponseData`.
471+
* **signal** `AbortSignal | events.EventEmitter | null` (optional) - Default: `null`.
472472
* **onInfo** `({statusCode: number, headers: Record<string, string | string[]>}) => void | null` (optional) - Default: `null` - Callback collecting all the info headers (HTTP 100-199) received.
473473

474474
The `RequestOptions.method` property should not be value `'CONNECT'`.
475475

476476
#### Parameter: `ResponseData`
477477

478478
* **statusCode** `number`
479-
* **headers** `http.IncomingHttpHeaders`
479+
* **headers** `http.IncomingHttpHeaders` - Note that all header keys are lower-cased, e. g. `content-type`.
480480
* **body** `stream.Readable` which also implements [the body mixin from the Fetch Standard](https://fetch.spec.whatwg.org/#body-mixin).
481481
* **trailers** `Record<string, string>` - This object starts out
482482
as empty and will be mutated to contain trailers after `body` has emitted `'end'`.
@@ -497,6 +497,8 @@ The `RequestOptions.method` property should not be value `'CONNECT'`.
497497

498498
- `dump({ limit: Integer })`, dump the response by reading up to `limit` bytes without killing the socket (optional) - Default: 262144.
499499

500+
Note that body will still be a `Readable` even if it is empty, but attempting to deserialize it with `json()` will result in an exception. Recommended way to ensure there is a body to deserialize is to check if status code is not 204, and `content-type` header starts with `application/json`.
501+
500502
#### Example 1 - Basic GET Request
501503

502504
```js

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ agent.disableNetConnect()
465465
agent
466466
.get('https://example.com')
467467
.intercept({ method: 'GET', path: '/' })
468-
.reply(200, '')
468+
.reply(200)
469469

470470
const pendingInterceptors = agent.pendingInterceptors()
471471
// Returns [
@@ -508,7 +508,7 @@ agent.disableNetConnect()
508508
agent
509509
.get('https://example.com')
510510
.intercept({ method: 'GET', path: '/' })
511-
.reply(200, '')
511+
.reply(200)
512512

513513
agent.assertNoPendingInterceptors()
514514
// Throws an UndiciError with the following message:

deps/undici/src/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function makeDispatcher (fn) {
8080
module.exports.setGlobalDispatcher = setGlobalDispatcher
8181
module.exports.getGlobalDispatcher = getGlobalDispatcher
8282

83-
if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 5)) {
83+
if (nodeMajor > 16 || (nodeMajor === 16 && nodeMinor >= 8)) {
8484
let fetchImpl = null
8585
module.exports.fetch = async function fetch (resource) {
8686
if (!fetchImpl) {

deps/undici/src/lib/api/api-connect.js

+3-19
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ConnectHandler extends AsyncResource {
1515
throw new InvalidArgumentError('invalid callback')
1616
}
1717

18-
const { signal, opaque, responseHeaders, httpTunnel } = opts
18+
const { signal, opaque, responseHeaders } = opts
1919

2020
if (signal && typeof signal.on !== 'function' && typeof signal.addEventListener !== 'function') {
2121
throw new InvalidArgumentError('signal must be an EventEmitter or EventTarget')
@@ -27,7 +27,6 @@ class ConnectHandler extends AsyncResource {
2727
this.responseHeaders = responseHeaders || null
2828
this.callback = callback
2929
this.abort = null
30-
this.httpTunnel = httpTunnel
3130

3231
addSignal(this, signal)
3332
}
@@ -41,23 +40,8 @@ class ConnectHandler extends AsyncResource {
4140
this.context = context
4241
}
4342

44-
onHeaders (statusCode) {
45-
// when httpTunnel headers are allowed
46-
if (this.httpTunnel) {
47-
const { callback, opaque } = this
48-
if (statusCode !== 200) {
49-
if (callback) {
50-
this.callback = null
51-
const err = new RequestAbortedError('Proxy response !== 200 when HTTP Tunneling')
52-
queueMicrotask(() => {
53-
this.runInAsyncScope(callback, null, err, { opaque })
54-
})
55-
}
56-
return 1
57-
}
58-
} else {
59-
throw new SocketError('bad connect', null)
60-
}
43+
onHeaders () {
44+
throw new SocketError('bad connect', null)
6145
}
6246

6347
onUpgrade (statusCode, rawHeaders, socket) {

deps/undici/src/lib/api/api-request.js

+31-3
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,17 @@ class RequestHandler extends AsyncResource {
8484
}
8585

8686
const parsedHeaders = util.parseHeaders(rawHeaders)
87-
const body = new Readable(resume, abort, parsedHeaders['content-type'])
87+
const contentType = parsedHeaders['content-type']
88+
const body = new Readable(resume, abort, contentType)
8889

8990
this.callback = null
9091
this.res = body
9192
const headers = this.responseHeaders === 'raw' ? util.parseRawHeaders(rawHeaders) : util.parseHeaders(rawHeaders)
9293

9394
if (callback !== null) {
9495
if (this.throwOnError && statusCode >= 400) {
95-
this.runInAsyncScope(callback, null,
96-
new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers)
96+
this.runInAsyncScope(getResolveErrorBodyCallback, null,
97+
{ callback, body, contentType, statusCode, statusMessage, headers }
9798
)
9899
return
99100
}
@@ -152,6 +153,33 @@ class RequestHandler extends AsyncResource {
152153
}
153154
}
154155

156+
async function getResolveErrorBodyCallback ({ callback, body, contentType, statusCode, statusMessage, headers }) {
157+
if (statusCode === 204 || !contentType) {
158+
body.dump()
159+
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers))
160+
return
161+
}
162+
163+
try {
164+
if (contentType.startsWith('application/json')) {
165+
const payload = await body.json()
166+
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers, payload))
167+
return
168+
}
169+
170+
if (contentType.startsWith('text/')) {
171+
const payload = await body.text()
172+
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers, payload))
173+
return
174+
}
175+
} catch (err) {
176+
// Process in a fallback if error
177+
}
178+
179+
body.dump()
180+
process.nextTick(callback, new ResponseStatusCodeError(`Response status code ${statusCode}${statusMessage ? `: ${statusMessage}` : ''}`, statusCode, headers))
181+
}
182+
155183
function request (opts, callback) {
156184
if (callback === undefined) {
157185
return new Promise((resolve, reject) => {

deps/undici/src/lib/client.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ class Parser {
720720
}
721721
}
722722

723-
if (request.method === 'CONNECT' && statusCode >= 200 && statusCode < 300) {
723+
if (request.method === 'CONNECT') {
724724
assert(client[kRunning] === 1)
725725
this.upgrade = true
726726
return 2
@@ -889,10 +889,8 @@ function onParserTimeout (parser) {
889889

890890
/* istanbul ignore else */
891891
if (timeoutType === TIMEOUT_HEADERS) {
892-
if (!socket[kWriting]) {
893-
assert(!parser.paused, 'cannot be paused while waiting for headers')
894-
util.destroy(socket, new HeadersTimeoutError())
895-
}
892+
assert(!parser.paused, 'cannot be paused while waiting for headers')
893+
util.destroy(socket, new HeadersTimeoutError())
896894
} else if (timeoutType === TIMEOUT_BODY) {
897895
if (!parser.paused) {
898896
util.destroy(socket, new BodyTimeoutError())

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

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
'use strict'
22

3-
class AbortError extends Error {
4-
constructor () {
5-
super('The operation was aborted')
6-
this.code = 'ABORT_ERR'
7-
this.name = 'AbortError'
8-
}
9-
}
10-
113
class UndiciError extends Error {
124
constructor (message) {
135
super(message)
@@ -57,12 +49,13 @@ class BodyTimeoutError extends UndiciError {
5749
}
5850

5951
class ResponseStatusCodeError extends UndiciError {
60-
constructor (message, statusCode, headers) {
52+
constructor (message, statusCode, headers, body) {
6153
super(message)
6254
Error.captureStackTrace(this, ResponseStatusCodeError)
6355
this.name = 'ResponseStatusCodeError'
6456
this.message = message || 'Response Status Code Error'
6557
this.code = 'UND_ERR_RESPONSE_STATUS_CODE'
58+
this.body = body
6659
this.status = statusCode
6760
this.statusCode = statusCode
6861
this.headers = headers
@@ -191,7 +184,6 @@ class HTTPParserError extends Error {
191184
}
192185

193186
module.exports = {
194-
AbortError,
195187
HTTPParserError,
196188
UndiciError,
197189
HeadersTimeoutError,

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ class Request {
140140
}
141141

142142
if (util.isFormDataLike(this.body)) {
143-
if (nodeMajor < 16 || (nodeMajor === 16 && nodeMinor < 5)) {
144-
throw new InvalidArgumentError('Form-Data bodies are only supported in node v16.5 and newer.')
143+
if (nodeMajor < 16 || (nodeMajor === 16 && nodeMinor < 8)) {
144+
throw new InvalidArgumentError('Form-Data bodies are only supported in node v16.8 and newer.')
145145
}
146146

147147
if (!extractBody) {

0 commit comments

Comments
 (0)