Skip to content

Commit

Permalink
Reduce Buffer copies in HttpClient (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Wallet authored May 4, 2022
1 parent 80adced commit bdc3f95
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class HttpClientResponse implements ifm.IHttpClientResponse {
public message: http.IncomingMessage;
readBody(): Promise<string> {
return new Promise<string>(async (resolve, reject) => {
let buffer: Buffer = Buffer.alloc(0);
const chunks = [];
const encodingCharset = util.obtainContentCharset(this);

// Extract Encoding from header: 'content-encoding'
Expand All @@ -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);

Expand Down

0 comments on commit bdc3f95

Please sign in to comment.