mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
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 <jasonsaayman@gmail.com>
This commit is contained in:
@@ -238,11 +238,13 @@ module.exports = function httpAdapter(config) {
|
|||||||
settle(resolve, reject, response);
|
settle(resolve, reject, response);
|
||||||
} else {
|
} else {
|
||||||
var responseBuffer = [];
|
var responseBuffer = [];
|
||||||
|
var totalResponseBytes = 0;
|
||||||
stream.on('data', function handleStreamData(chunk) {
|
stream.on('data', function handleStreamData(chunk) {
|
||||||
responseBuffer.push(chunk);
|
responseBuffer.push(chunk);
|
||||||
|
totalResponseBytes += chunk.length;
|
||||||
|
|
||||||
// make sure the content length is not over the maxContentLength if specified
|
// 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();
|
stream.destroy();
|
||||||
reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
|
reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
|
||||||
config, null, lastRequest));
|
config, null, lastRequest));
|
||||||
|
|||||||
Reference in New Issue
Block a user