Skip to content

Commit 286ffea

Browse files
authored
perf: reduce number of native calls from fetch.body (nodejs#1792)
1 parent 671e05f commit 286ffea

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

lib/fetch/body.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,22 +150,18 @@ function extractBody (object, keepalive = false) {
150150
if (typeof value === 'string') {
151151
bodyLength +=
152152
prefixLength +
153-
Buffer.byteLength(`; name="${escape(normalizeLinefeeds(name))}"`) +
154-
Buffer.byteLength(`\r\n\r\n${normalizeLinefeeds(value)}\r\n`)
153+
Buffer.byteLength(`; name="${escape(normalizeLinefeeds(name))}"\r\n\r\n${normalizeLinefeeds(value)}\r\n`)
155154
} else {
156155
bodyLength +=
157156
prefixLength +
158-
Buffer.byteLength(`; name="${escape(normalizeLinefeeds(name))}"`) +
159-
(value.name ? Buffer.byteLength(`; filename="${escape(value.name)}"`) : 0) +
157+
Buffer.byteLength(`; name="${escape(normalizeLinefeeds(name))}"` + (value.name ? `; filename="${escape(value.name)}"` : '')) +
160158
2 + // \r\n
161159
`Content-Type: ${
162160
value.type || 'application/octet-stream'
163161
}\r\n\r\n`.length
164162

165-
// value is a Blob or File
166-
bodyLength += value.size
167-
168-
bodyLength += 2 // \r\n
163+
// value is a Blob or File, and \r\n
164+
bodyLength += value.size + 2
169165
}
170166
}
171167

@@ -393,7 +389,7 @@ function bodyMixinMethods (instance) {
393389
const { filename, encoding, mimeType } = info
394390
const chunks = []
395391

396-
if (encoding.toLowerCase() === 'base64') {
392+
if (encoding === 'base64' || encoding.toLowerCase() === 'base64') {
397393
let base64chunk = ''
398394

399395
value.on('data', (chunk) => {

0 commit comments

Comments
 (0)