From e744aa94c3dd09ea5040894dd4e27f718d108fdc Mon Sep 17 00:00:00 2001 From: Jordan Wallet Date: Tue, 19 Apr 2022 09:04:59 -0700 Subject: [PATCH] Reduce Buffer copies in HttpClient --- lib/HttpClient.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/HttpClient.ts b/lib/HttpClient.ts index 2e0f519c..ed6bc599 100644 --- a/lib/HttpClient.ts +++ b/lib/HttpClient.ts @@ -55,7 +55,7 @@ export class HttpClientResponse implements ifm.IHttpClientResponse { public message: http.IncomingMessage; readBody(): Promise { return new Promise(async (resolve, reject) => { - let buffer: Buffer = Buffer.alloc(0); + const chunks = []; const encodingCharset = util.obtainContentCharset(this); // Extract Encoding from header: 'content-encoding' @@ -65,8 +65,9 @@ export class HttpClientResponse implements ifm.IHttpClientResponse { this.message.on('data', function(data: string|Buffer) { const chunk = (typeof data === 'string') ? Buffer.from(data, encodingCharset) : data; - buffer = Buffer.concat([buffer, chunk]); + chunks.push(chunk); }).on('end', async function() { + const buffer: Buffer = Buffer.concat(chunks); if (isGzippedEncoded) { // Process GZipped Response Body HERE const gunzippedBody = await util.decompressGzippedContent(buffer, encodingCharset);