Skip to content

Commit 0a793a2

Browse files
nodejs-github-bottargos
authored andcommitted
deps: update undici to 5.27.0
PR-URL: #50463 Reviewed-By: Matthew Aitken <maitken033380023@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ec7005a commit 0a793a2

15 files changed

+251
-183
lines changed

deps/undici/src/index-fetch.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
const fetchImpl = require('./lib/fetch').fetch
44

5-
module.exports.fetch = async function fetch (resource, init = undefined) {
6-
try {
7-
return await fetchImpl(resource, init)
8-
} catch (err) {
5+
module.exports.fetch = function fetch (resource, init = undefined) {
6+
return fetchImpl(resource, init).catch((err) => {
97
Error.captureStackTrace(err, this)
108
throw err
11-
}
9+
})
1210
}
1311
module.exports.FormData = require('./lib/fetch/formdata').FormData
1412
module.exports.Headers = require('./lib/fetch/headers').Headers

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

+8
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ class Request {
222222
if (channels.bodySent.hasSubscribers) {
223223
channels.bodySent.publish({ request: this })
224224
}
225+
226+
if (this[kHandler].onRequestSent) {
227+
try {
228+
this[kHandler].onRequestSent()
229+
} catch (err) {
230+
this.onError(err)
231+
}
232+
}
225233
}
226234

227235
onConnect (abort) {

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

+12-9
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ let ReadableStream = globalThis.ReadableStream
2626

2727
/** @type {globalThis['File']} */
2828
const File = NativeFile ?? UndiciFile
29+
const textEncoder = new TextEncoder()
30+
const textDecoder = new TextDecoder()
2931

3032
// https://fetch.spec.whatwg.org/#concept-bodyinit-extract
3133
function extractBody (object, keepalive = false) {
@@ -49,7 +51,7 @@ function extractBody (object, keepalive = false) {
4951
stream = new ReadableStream({
5052
async pull (controller) {
5153
controller.enqueue(
52-
typeof source === 'string' ? new TextEncoder().encode(source) : source
54+
typeof source === 'string' ? textEncoder.encode(source) : source
5355
)
5456
queueMicrotask(() => readableStreamClose(controller))
5557
},
@@ -119,21 +121,20 @@ function extractBody (object, keepalive = false) {
119121
// - That the content-length is calculated in advance.
120122
// - And that all parts are pre-encoded and ready to be sent.
121123

122-
const enc = new TextEncoder()
123124
const blobParts = []
124125
const rn = new Uint8Array([13, 10]) // '\r\n'
125126
length = 0
126127
let hasUnknownSizeValue = false
127128

128129
for (const [name, value] of object) {
129130
if (typeof value === 'string') {
130-
const chunk = enc.encode(prefix +
131+
const chunk = textEncoder.encode(prefix +
131132
`; name="${escape(normalizeLinefeeds(name))}"` +
132133
`\r\n\r\n${normalizeLinefeeds(value)}\r\n`)
133134
blobParts.push(chunk)
134135
length += chunk.byteLength
135136
} else {
136-
const chunk = enc.encode(`${prefix}; name="${escape(normalizeLinefeeds(name))}"` +
137+
const chunk = textEncoder.encode(`${prefix}; name="${escape(normalizeLinefeeds(name))}"` +
137138
(value.name ? `; filename="${escape(value.name)}"` : '') + '\r\n' +
138139
`Content-Type: ${
139140
value.type || 'application/octet-stream'
@@ -147,7 +148,7 @@ function extractBody (object, keepalive = false) {
147148
}
148149
}
149150

150-
const chunk = enc.encode(`--${boundary}--`)
151+
const chunk = textEncoder.encode(`--${boundary}--`)
151152
blobParts.push(chunk)
152153
length += chunk.byteLength
153154
if (hasUnknownSizeValue) {
@@ -443,14 +444,16 @@ function bodyMixinMethods (instance) {
443444
let text = ''
444445
// application/x-www-form-urlencoded parser will keep the BOM.
445446
// https://url.spec.whatwg.org/#concept-urlencoded-parser
446-
const textDecoder = new TextDecoder('utf-8', { ignoreBOM: true })
447+
// Note that streaming decoder is stateful and cannot be reused
448+
const streamingDecoder = new TextDecoder('utf-8', { ignoreBOM: true })
449+
447450
for await (const chunk of consumeBody(this[kState].body)) {
448451
if (!isUint8Array(chunk)) {
449452
throw new TypeError('Expected Uint8Array chunk')
450453
}
451-
text += textDecoder.decode(chunk, { stream: true })
454+
text += streamingDecoder.decode(chunk, { stream: true })
452455
}
453-
text += textDecoder.decode()
456+
text += streamingDecoder.decode()
454457
entries = new URLSearchParams(text)
455458
} catch (err) {
456459
// istanbul ignore next: Unclear when new URLSearchParams can fail on a string.
@@ -565,7 +568,7 @@ function utf8DecodeBytes (buffer) {
565568

566569
// 3. Process a queue with an instance of UTF-8’s
567570
// decoder, ioQueue, output, and "replacement".
568-
const output = new TextDecoder().decode(buffer)
571+
const output = textDecoder.decode(buffer)
569572

570573
// 4. Return output.
571574
return output

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

+16-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
const { MessageChannel, receiveMessageOnPort } = require('worker_threads')
44

55
const corsSafeListedMethods = ['GET', 'HEAD', 'POST']
6+
const corsSafeListedMethodsSet = new Set(corsSafeListedMethods)
67

78
const nullBodyStatus = [101, 204, 205, 304]
89

910
const redirectStatus = [301, 302, 303, 307, 308]
11+
const redirectStatusSet = new Set(redirectStatus)
1012

1113
// https://fetch.spec.whatwg.org/#block-bad-port
1214
const badPorts = [
@@ -18,6 +20,8 @@ const badPorts = [
1820
'10080'
1921
]
2022

23+
const badPortsSet = new Set(badPorts)
24+
2125
// https://w3c.github.io/webappsec-referrer-policy/#referrer-policies
2226
const referrerPolicy = [
2327
'',
@@ -30,10 +34,12 @@ const referrerPolicy = [
3034
'strict-origin-when-cross-origin',
3135
'unsafe-url'
3236
]
37+
const referrerPolicySet = new Set(referrerPolicy)
3338

3439
const requestRedirect = ['follow', 'manual', 'error']
3540

3641
const safeMethods = ['GET', 'HEAD', 'OPTIONS', 'TRACE']
42+
const safeMethodsSet = new Set(safeMethods)
3743

3844
const requestMode = ['navigate', 'same-origin', 'no-cors', 'cors']
3945

@@ -68,6 +74,7 @@ const requestDuplex = [
6874

6975
// http://fetch.spec.whatwg.org/#forbidden-method
7076
const forbiddenMethods = ['CONNECT', 'TRACE', 'TRACK']
77+
const forbiddenMethodsSet = new Set(forbiddenMethods)
7178

7279
const subresource = [
7380
'audio',
@@ -83,6 +90,7 @@ const subresource = [
8390
'xslt',
8491
''
8592
]
93+
const subresourceSet = new Set(subresource)
8694

8795
/** @type {globalThis['DOMException']} */
8896
const DOMException = globalThis.DOMException ?? (() => {
@@ -132,5 +140,12 @@ module.exports = {
132140
nullBodyStatus,
133141
safeMethods,
134142
badPorts,
135-
requestDuplex
143+
requestDuplex,
144+
subresourceSet,
145+
badPortsSet,
146+
redirectStatusSet,
147+
corsSafeListedMethodsSet,
148+
safeMethodsSet,
149+
forbiddenMethodsSet,
150+
referrerPolicySet
136151
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const { isBlobLike } = require('./util')
77
const { webidl } = require('./webidl')
88
const { parseMIMEType, serializeAMimeType } = require('./dataURL')
99
const { kEnumerableProperty } = require('../core/util')
10+
const encoder = new TextEncoder()
1011

1112
class File extends Blob {
1213
constructor (fileBits, fileName, options = {}) {
@@ -280,7 +281,7 @@ function processBlobParts (parts, options) {
280281
}
281282

282283
// 3. Append the result of UTF-8 encoding s to bytes.
283-
bytes.push(new TextEncoder().encode(s))
284+
bytes.push(encoder.encode(s))
284285
} else if (
285286
types.isAnyArrayBuffer(element) ||
286287
types.isTypedArray(element)

0 commit comments

Comments
 (0)