未验证 提交 0ece97c7 编写于 作者: M Mark 提交者: GitHub

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: NJay <jasonsaayman@gmail.com>
上级 a18a0ecc
...@@ -238,11 +238,13 @@ module.exports = function httpAdapter(config) { ...@@ -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));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册