Skip to content

Commit 0d5c202

Browse files
pd4d10RaisinTen
andcommitted
stream: fix highwatermark threshold and add the missing error
1. Fix highwatermark threshold: `< 1GiB` -> `<= 1GiB` 2. Add the missing error: Size out of range Update test/parallel/test-streams-highwatermark.js Co-authored-by: Darshan Sen <[email protected]>
1 parent f26c2ce commit 0d5c202

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/internal/streams/readable.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const {
6262
codes: {
6363
ERR_INVALID_ARG_TYPE,
6464
ERR_METHOD_NOT_IMPLEMENTED,
65+
ERR_OUT_OF_RANGE,
6566
ERR_STREAM_PUSH_AFTER_EOF,
6667
ERR_STREAM_UNSHIFT_AFTER_END_EVENT,
6768
}
@@ -363,9 +364,8 @@ Readable.prototype.setEncoding = function(enc) {
363364
// Don't raise the hwm > 1GB.
364365
const MAX_HWM = 0x40000000;
365366
function computeNewHighWaterMark(n) {
366-
if (n >= MAX_HWM) {
367-
// TODO(ronag): Throw ERR_VALUE_OUT_OF_RANGE.
368-
n = MAX_HWM;
367+
if (n > MAX_HWM) {
368+
throw new ERR_OUT_OF_RANGE('size', '<= 1GiB', n);
369369
} else {
370370
// Get the next highest power of 2 to prevent increasing hwm excessively in
371371
// tiny amounts.

test/parallel/test-streams-highwatermark.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,18 @@ const { inspect } = require('util');
7070
assert.strictEqual(readable._readableState.highWaterMark, Number(size));
7171
});
7272
}
73+
74+
{
75+
// Test highwatermark limit
76+
const hwm = 0x40000000 + 1;
77+
const readable = stream.Readable({
78+
read() {},
79+
});
80+
81+
assert.throws(() => readable.read(hwm), common.expectsError({
82+
code: 'ERR_OUT_OF_RANGE',
83+
message: 'The value of "size" is out of range.' +
84+
' It must be <= 1GiB. Received ' +
85+
hwm,
86+
}));
87+
}

0 commit comments

Comments
 (0)