Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: check type and range of highWaterMark #13065

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
17 changes: 13 additions & 4 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,20 @@ function ReadableState(options, stream) {
// the point at which it stops calling _read() to fill the buffer
// Note: 0 is a valid value, means "don't call _read preemptively ever"
var hwm = options.highWaterMark;
var defaultHwm = this.objectMode ? 16 : 16 * 1024;
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
if (hwm == null) {
this.highWaterMark = this.objectMode ? 16 : 16 * 1024;
} else {
if (typeof hwm !== 'number') {
throw new TypeError('"highWaterMark" must be a number');
}

// cast to ints.
this.highWaterMark = Math.floor(this.highWaterMark);
if (hwm < 0) {
throw new RangeError('"highWaterMark" must not be negative');
}

// cast to ints.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think it's better to start a comment with upper case. The same in the other file.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is a remainder from the old code.

this.highWaterMark = Math.floor(hwm);
}

// A linked list is used to store data chunks instead of an array because the
// linked list can remove elements from the beginning faster than
Expand Down
17 changes: 13 additions & 4 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,20 @@ function WritableState(options, stream) {
// Note: 0 is a valid value, means that we always return false if
// the entire buffer is not flushed immediately on write()
var hwm = options.highWaterMark;
var defaultHwm = this.objectMode ? 16 : 16 * 1024;
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
if (hwm == null) {
this.highWaterMark = this.objectMode ? 16 : 16 * 1024;
} else {
if (typeof hwm !== 'number') {
throw new TypeError('"highWaterMark" must be a number');
}

// cast to ints.
this.highWaterMark = Math.floor(this.highWaterMark);
if (hwm < 0) {
throw new RangeError('"highWaterMark" must not be negative');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if the internal error types would be used for all new errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not actually the case for streams. It's a way bigger chunk of work that we need to do, as we would have to support them in readable-stream as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thanks for the update 👍

}

// cast to ints.
this.highWaterMark = Math.floor(hwm);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend to move this part into a generic function that can be called by readable and writable stream to prevent code duplication. This also keeps it always in line as the limits stay the same in both cases.


// drain event flag.
this.needDrain = false;
Expand Down
21 changes: 20 additions & 1 deletion test/parallel/test-streams-highwatermark.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
require('../common');

// This test ensures that the stream implementation correctly handles values
// for highWaterMark which exceed the range of signed 32 bit integers.
// for highWaterMark which exceed the range of signed 32 bit integers and
// rejects invalid values.

const assert = require('assert');
const stream = require('stream');
Expand All @@ -16,3 +17,21 @@ assert.strictEqual(readable._readableState.highWaterMark, ovfl);

const writable = stream.Writable({ highWaterMark: ovfl });
assert.strictEqual(writable._writableState.highWaterMark, ovfl);

[true, false, '5', {}].forEach((invalidValue) => {
assert.throws(() => {
stream.Readable({ highWaterMark: invalidValue });
}, /^TypeError: "highWaterMark" must be a number$/);

assert.throws(() => {
stream.Writable({ highWaterMark: invalidValue });
}, /^TypeError: "highWaterMark" must be a number$/);
});

assert.throws(() => {
stream.Readable({ highWaterMark: -5 });
}, /^RangeError: "highWaterMark" must not be negative$/);

assert.throws(() => {
stream.Writable({ highWaterMark: -5 });
}, /^RangeError: "highWaterMark" must not be negative$/);