Skip to content

Commit

Permalink
perf: reduce number of native calls from fetch.body (#1792)
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig authored Nov 30, 2022
1 parent 671e05f commit 286ffea
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions lib/fetch/body.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,18 @@ function extractBody (object, keepalive = false) {
if (typeof value === 'string') {
bodyLength +=
prefixLength +
Buffer.byteLength(`; name="${escape(normalizeLinefeeds(name))}"`) +
Buffer.byteLength(`\r\n\r\n${normalizeLinefeeds(value)}\r\n`)
Buffer.byteLength(`; name="${escape(normalizeLinefeeds(name))}"\r\n\r\n${normalizeLinefeeds(value)}\r\n`)
} else {
bodyLength +=
prefixLength +
Buffer.byteLength(`; name="${escape(normalizeLinefeeds(name))}"`) +
(value.name ? Buffer.byteLength(`; filename="${escape(value.name)}"`) : 0) +
Buffer.byteLength(`; name="${escape(normalizeLinefeeds(name))}"` + (value.name ? `; filename="${escape(value.name)}"` : '')) +
2 + // \r\n
`Content-Type: ${
value.type || 'application/octet-stream'
}\r\n\r\n`.length

// value is a Blob or File
bodyLength += value.size

bodyLength += 2 // \r\n
// value is a Blob or File, and \r\n
bodyLength += value.size + 2
}
}

Expand Down Expand Up @@ -393,7 +389,7 @@ function bodyMixinMethods (instance) {
const { filename, encoding, mimeType } = info
const chunks = []

if (encoding.toLowerCase() === 'base64') {
if (encoding === 'base64' || encoding.toLowerCase() === 'base64') {
let base64chunk = ''

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

0 comments on commit 286ffea

Please sign in to comment.