Skip to content

Commit

Permalink
Fixing quadratic runtime when setting a maxContentLength (#3738)
Browse files Browse the repository at this point in the history
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 <jasonsaayman@gmail.com>
  • Loading branch information
bimbiltu and jasonsaayman committed May 4, 2021
1 parent a18a0ec commit 0ece97c
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/adapters/http.js
Expand Up @@ -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));
Expand Down

0 comments on commit 0ece97c

Please sign in to comment.