Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,19 @@ Readable.prototype.setEncoding = function(enc) {
return this;
};

// Don't raise the hwm > 128MB
// Don't raise the hwm > 8MB
const MAX_HWM = 0x800000;
function roundUpToNextPowerOf2(n) {
function computeNewHighWaterMark(n) {
if (n >= MAX_HWM) {
n = MAX_HWM;
} else {
// Get the next highest power of 2
n--;
for (var p = 1; p < 32; p <<= 1) n |= n >> p;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
n++;
}
return n;
Expand Down Expand Up @@ -227,7 +231,7 @@ function howMuchToRead(n, state) {
// power of 2, to prevent increasing it excessively in tiny
// amounts.
if (n > state.highWaterMark)
state.highWaterMark = roundUpToNextPowerOf2(n);
state.highWaterMark = computeNewHighWaterMark(n);

// don't have that much. return null, unless we've ended.
if (n > state.length) {
Expand Down