From 18f15b5e86e4c9a83c708e8972f80b751f7d349e Mon Sep 17 00:00:00 2001 From: mark <34653278+bimbiltu@users.noreply.github.com> Date: Fri, 9 Apr 2021 22:11:52 -0400 Subject: [PATCH] Fixing quadratic runtime when setting a maxContentLength 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. --- 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 02be4651f9..9f5eedcf5a 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));