From 286ffeae328890b09bb284cf308a566048c3b0ea Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Wed, 30 Nov 2022 06:38:03 -0500 Subject: [PATCH] perf: reduce number of native calls from fetch.body (#1792) --- lib/fetch/body.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/fetch/body.js b/lib/fetch/body.js index cc3fa8b7b75..5a7ee88b9bb 100644 --- a/lib/fetch/body.js +++ b/lib/fetch/body.js @@ -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 } } @@ -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) => {