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 #18098

Closed
wants to merge 7 commits into from
Prev Previous commit
Next Next commit
fixup! stream: add type and range check for highWaterMark
  • Loading branch information
tniessen committed Jan 11, 2018
commit dfa34e5f923e554038e2b5f041e137acdc179653
3 changes: 2 additions & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ 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"
this.highWaterMark = getHighWaterMark(this, options, isDuplex);
this.highWaterMark = getHighWaterMark(this, options, 'readableHighWaterMark',
isDuplex);

// 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
3 changes: 2 additions & 1 deletion lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ function WritableState(options, stream) {
// the point at which write() starts returning false
// Note: 0 is a valid value, means that we always return false if
// the entire buffer is not flushed immediately on write()
this.highWaterMark = getHighWaterMark(this, options, isDuplex);
this.highWaterMark = getHighWaterMark(this, options, 'writableHighWaterMark',
isDuplex);

// if _final has been called
this.finalCalled = false;
Expand Down
14 changes: 3 additions & 11 deletions lib/internal/streams/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,11 @@ function getHighWaterMarkFromProperty(hwm, name) {
return Math.floor(hwm);
}

function getHighWaterMark(state, options, isDuplex) {
function getHighWaterMark(state, options, duplexKey, isDuplex) {
if (options.highWaterMark != null) {
return getHighWaterMarkFromProperty(options.highWaterMark, 'highWaterMark');
} else if (isDuplex) {
if (state instanceof stream.Readable.ReadableState) {
if (options.readableHighWaterMark != null) {
return getHighWaterMarkFromProperty(options.readableHighWaterMark,
'readableHighWaterMark');
}
} else if (options.writableHighWaterMark != null) {
return getHighWaterMarkFromProperty(options.writableHighWaterMark,
'writableHighWaterMark');
}
} else if (isDuplex && options[duplexKey] != null) {
return getHighWaterMarkFromProperty(options[duplexKey], duplexKey);
}

// Default value
Expand Down