8000 Fixing quadratic runtime when setting a maxContentLength (#3738) · darth-coder-js/axios@0ece97c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ece97c

Browse files
Fixing quadratic runtime when setting a maxContentLength (axios#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>
1 parent a18a0ec commit 0ece97c

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

lib/adapters/http.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,13 @@ module.exports = function httpAdapter(config) {
238238
settle(resolve, reject, response);
239239
} else {
240240
var responseBuffer = [];
241+
var totalResponseBytes = 0;
241242
stream.on('data', function handleStreamData(chunk) {
242243
responseBuffer.push(chunk);
244+
totalResponseBytes += chunk.length;
243245

244246
// make sure the content length is not over the maxContentLength if specified
245-
if (config.maxContentLength > -1 && Buffer.concat(responseBuffer).length > config.maxContentLength) {
247+
if (config.maxContentLength > -1 && totalResponseBytes > config.maxContentLength) {
246248
stream.destroy();
247249
reject(createError('maxContentLength size of ' + config.maxContentLength + ' exceeded',
248250
config, null, lastRequest));

0 commit comments

Comments
 (0)
0