Skip to content

Commit

Permalink
perf: reduce number of native calls from fetch.body
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Nov 30, 2022
1 parent 232905f commit 2d926a1
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions lib/fetch/body.js
Expand Up @@ -151,22 +151,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 @@ -394,7 +390,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 2d926a1

Please sign in to comment.