From a5a261e00ac0dba5e6577cb3f992c3f0ad66405f Mon Sep 17 00:00:00 2001 From: Mark <34653278+bimbiltu@users.noreply.github.com> Date: Tue, 4 May 2021 13:48:20 -0400 Subject: [PATCH] Fixing quadratic runtime when setting a maxContentLength (#3738) Previously checking whether a response has exceeded `maxContentLength` was quadratic with respect to the number of chunks in the response stream and also caused unnecessary additional memory usage. Co-authored-by: Jay --- lib/adapters/http.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/adapters/http.js b/lib/adapters/http.js index 5fb5ccd692..eeec94445a 100755 --- a/lib/adapters/http.js +++ b/lib/adapters/http.js @@ -238,11 +238,13 @@ module.exports = function httpAdapter(config) { settle(resolve, reject, response); } else { var responseBuffer = []; + var totalResponseBytes = 0; stream.on('data', function handleStreamData(chunk) { responseBuffer.push(chunk); + totalResponseBytes += chunk.length; // make sure the content length is not over the maxContentLength if specified - if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) { + if (config.maxContentLength > -1 && totalResponseBytes > config.maxContentLength) { stream.destroy(); reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded', config, null, lastRequest));