Skip to content

Commit 099ebdb

Browse files
nodejs-github-bottargos
authored andcommitted
deps: update undici to 5.28.1
PR-URL: #50975 Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Matthew Aitken <[email protected]>
1 parent 9bc7917 commit 099ebdb

File tree

9 files changed

+216
-146
lines changed

9 files changed

+216
-146
lines changed

deps/undici/src/lib/client.js

+12-19
Original file line numberDiff line numberDiff line change
@@ -917,11 +917,9 @@ class Parser {
917917
socket[kReset] = true
918918
}
919919

920-
let pause
921-
try {
922-
pause = request.onHeaders(statusCode, headers, this.resume, statusText) === false
923-
} catch (err) {
924-
util.destroy(socket, err)
920+
const pause = request.onHeaders(statusCode, headers, this.resume, statusText) === false
921+
922+
if (request.aborted) {
925923
return -1
926924
}
927925

@@ -968,13 +966,8 @@ class Parser {
968966

969967
this.bytesRead += buf.length
970968

971-
try {
972-
if (request.onData(buf) === false) {
973-
return constants.ERROR.PAUSED
974-
}
975-
} catch (err) {
976-
util.destroy(socket, err)
977-
return -1
969+
if (request.onData(buf) === false) {
970+
return constants.ERROR.PAUSED
978971
}
979972
}
980973

@@ -1015,11 +1008,7 @@ class Parser {
10151008
return -1
10161009
}
10171010

1018-
try {
1019-
request.onComplete(headers)
1020-
} catch (err) {
1021-
errorRequest(client, request, err)
1022-
}
1011+
request.onComplete(headers)
10231012

10241013
client[kQueue][client[kRunningIdx]++] = null
10251014

@@ -1805,13 +1794,17 @@ function writeH2 (client, session, request) {
18051794
})
18061795

18071796
stream.on('data', (chunk) => {
1808-
if (request.onData(chunk) === false) stream.pause()
1797+
if (request.onData(chunk) === false) {
1798+
stream.pause()
1799+
}
18091800
})
18101801

18111802
stream.once('close', () => {
18121803
h2State.openStreams -= 1
18131804
// TODO(HTTP/2): unref only if current streams count is 0
1814-
if (h2State.openStreams === 0) session.unref()
1805+
if (h2State.openStreams === 0) {
1806+
session.unref()
1807+
}
18151808
})
18161809

18171810
stream.once('error', function (err) {

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

+29-5
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,11 @@ class Request {
229229

230230
onBodySent (chunk) {
231231
if (this[kHandler].onBodySent) {
232-
return this[kHandler].onBodySent(chunk)
232+
try {
233+
return this[kHandler].onBodySent(chunk)
234+
} catch (err) {
235+
this.abort(err)
236+
}
233237
}
234238
}
235239

@@ -239,7 +243,11 @@ class Request {
239243
}
240244

241245
if (this[kHandler].onRequestSent) {
242-
return this[kHandler].onRequestSent()
246+
try {
247+
return this[kHandler].onRequestSent()
248+
} catch (err) {
249+
this.abort(err)
250+
}
243251
}
244252
}
245253

@@ -263,14 +271,23 @@ class Request {
263271
channels.headers.publish({ request: this, response: { statusCode, headers, statusText } })
264272
}
265273

266-
return this[kHandler].onHeaders(statusCode, headers, resume, statusText)
274+
try {
275+
return this[kHandler].onHeaders(statusCode, headers, resume, statusText)
276+
} catch (err) {
277+
this.abort(err)
278+
}
267279
}
268280

269281
onData (chunk) {
270282
assert(!this.aborted)
271283
assert(!this.completed)
272284

273-
return this[kHandler].onData(chunk)
285+
try {
286+
return this[kHandler].onData(chunk)
287+
} catch (err) {
288+
this.abort(err)
289+
return false
290+
}
274291
}
275292

276293
onUpgrade (statusCode, headers, socket) {
@@ -289,7 +306,13 @@ class Request {
289306
if (channels.trailers.hasSubscribers) {
290307
channels.trailers.publish({ request: this, trailers })
291308
}
292-
return this[kHandler].onComplete(trailers)
309+
310+
try {
311+
return this[kHandler].onComplete(trailers)
312+
} catch (err) {
313+
// TODO (fix): This might be a bad idea?
314+
this.onError(err)
315+
}
293316
}
294317

295318
onError (error) {
@@ -303,6 +326,7 @@ class Request {
303326
return
304327
}
305328
this.aborted = true
329+
306330
return this[kHandler].onError(error)
307331
}
308332

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

+18-15
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const {
1010
isValidHTTPToken,
1111
sameOrigin,
1212
normalizeMethod,
13-
makePolicyContainer
13+
makePolicyContainer,
14+
normalizeMethodRecord
1415
} = require('./util')
1516
const {
1617
forbiddenMethodsSet,
@@ -183,8 +184,10 @@ class Request {
183184
urlList: [...request.urlList]
184185
})
185186

187+
const initHasKey = Object.keys(init).length !== 0
188+
186189
// 13. If init is not empty, then:
187-
if (Object.keys(init).length > 0) {
190+
if (initHasKey) {
188191
// 1. If request’s mode is "navigate", then set it to "same-origin".
189192
if (request.mode === 'navigate') {
190193
request.mode = 'same-origin'
@@ -315,16 +318,16 @@ class Request {
315318

316319
// 2. If method is not a method or method is a forbidden method, then
317320
// throw a TypeError.
318-
if (!isValidHTTPToken(init.method)) {
319-
throw new TypeError(`'${init.method}' is not a valid HTTP method.`)
321+
if (!isValidHTTPToken(method)) {
322+
throw new TypeError(`'${method}' is not a valid HTTP method.`)
320323
}
321324

322325
if (forbiddenMethodsSet.has(method.toUpperCase())) {
323-
throw new TypeError(`'${init.method}' HTTP method is unsupported.`)
326+
throw new TypeError(`'${method}' HTTP method is unsupported.`)
324327
}
325328

326329
// 3. Normalize method.
327-
method = normalizeMethod(init.method)
330+
method = normalizeMethodRecord[method] ?? normalizeMethod(method)
328331

329332
// 4. Set request’s method to method.
330333
request.method = method
@@ -415,25 +418,25 @@ class Request {
415418
}
416419

417420
// 32. If init is not empty, then:
418-
if (Object.keys(init).length !== 0) {
421+
if (initHasKey) {
422+
/** @type {HeadersList} */
423+
const headersList = this[kHeaders][kHeadersList]
419424
// 1. Let headers be a copy of this’s headers and its associated header
420425
// list.
421-
let headers = new Headers(this[kHeaders])
422-
423426
// 2. If init["headers"] exists, then set headers to init["headers"].
424-
if (init.headers !== undefined) {
425-
headers = init.headers
426-
}
427+
const headers = init.headers !== undefined ? init.headers : new HeadersList(headersList)
427428

428429
// 3. Empty this’s headers’s header list.
429-
this[kHeaders][kHeadersList].clear()
430+
headersList.clear()
430431

431432
// 4. If headers is a Headers object, then for each header in its header
432433
// list, append header’s name/header’s value to this’s headers.
433-
if (headers.constructor.name === 'Headers') {
434+
if (headers instanceof HeadersList) {
434435
for (const [key, val] of headers) {
435-
this[kHeaders].append(key, val)
436+
headersList.append(key, val)
436437
}
438+
// Note: Copy the `set-cookie` meta-data.
439+
headersList.cookies = headers.cookies
437440
} else {
438441
// 5. Otherwise, fill this’s headers with headers.
439442
fillHeaders(this[kHeaders], headers)

deps/undici/src/lib/fetch/util.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -698,11 +698,30 @@ function isCancelled (fetchParams) {
698698
fetchParams.controller.state === 'terminated'
699699
}
700700

701-
// https://fetch.spec.whatwg.org/#concept-method-normalize
701+
const normalizeMethodRecord = {
702+
delete: 'DELETE',
703+
DELETE: 'DELETE',
704+
get: 'GET',
705+
GET: 'GET',
706+
head: 'HEAD',
707+
HEAD: 'HEAD',
708+
options: 'OPTIONS',
709+
OPTIONS: 'OPTIONS',
710+
post: 'POST',
711+
POST: 'POST',
712+
put: 'PUT',
713+
PUT: 'PUT'
714+
}
715+
716+
// Note: object prototypes should not be able to be referenced. e.g. `Object#hasOwnProperty`.
717+
Object.setPrototypeOf(normalizeMethodRecord, null)
718+
719+
/**
720+
* @see https://fetch.spec.whatwg.org/#concept-method-normalize
721+
* @param {string} method
722+
*/
702723
function normalizeMethod (method) {
703-
return /^(DELETE|GET|HEAD|OPTIONS|POST|PUT)$/i.test(method)
704-
? method.toUpperCase()
705-
: method
724+
return normalizeMethodRecord[method.toLowerCase()] ?? method
706725
}
707726

708727
// https://infra.spec.whatwg.org/#serialize-a-javascript-value-to-a-json-string
@@ -1047,5 +1066,6 @@ module.exports = {
10471066
urlIsLocal,
10481067
urlHasHttpsScheme,
10491068
urlIsHttpHttpsScheme,
1050-
readAllBytes
1069+
readAllBytes,
1070+
normalizeMethodRecord
10511071
}

0 commit comments

Comments
 (0)