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: add global Stream.defaultHighwaterMark #40511

Closed
wants to merge 11 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
10 changes: 10 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ const stream = require('stream');
The `stream` module is useful for creating new types of stream instances. It is
usually not necessary to use the `stream` module to consume streams.

### Class property: `Stream.defaultHighWaterMark`
<!-- YAML
added: REPLACEME
-->

* {integer} **Default:** `16 * 1024`

This is the size (in bytes) for the default high water mark for
readable and writable streams. This value may be modified.

## Organization of this document

This document contains two primary sections and a third section for notes. The
Expand Down
44 changes: 44 additions & 0 deletions lib/internal/streams/legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

const {
ArrayIsArray,
ObjectDefineProperty,
ObjectSetPrototypeOf,
NumberIsInteger,
} = primordials;
const {
codes: {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
}
} = require('internal/errors');

const EE = require('events');

Expand Down Expand Up @@ -111,4 +119,40 @@ function prependListener(emitter, event, fn) {
emitter._events[event] = [fn, emitter._events[event]];
}

let defaultHighWaterMark = 16 * 1024;
ObjectDefineProperty(Stream, 'defaultHighWaterMark', {
get() {
return defaultHighWaterMark;
},
set(value) {
if (!NumberIsInteger(value)) {
throw ERR_INVALID_ARG_TYPE('value', 'Integer', value);
}

if (value < 1) {
throw ERR_INVALID_ARG_VALUE('value', value);
}

defaultHighWaterMark = value;
}
});

let defaultObjectModeHighWaterMark = 16;
ObjectDefineProperty(Stream, 'defaultObjectModeHighWaterMark', {
get() {
return defaultObjectModeHighWaterMark;
},
set(value) {
if (!NumberIsInteger(value)) {
throw ERR_INVALID_ARG_TYPE('value', 'Integer', value);
}

if (value < 1) {
throw ERR_INVALID_ARG_VALUE('value', value);
}

defaultObjectModeHighWaterMark = value;
}
});

module.exports = { Stream, prependListener };
4 changes: 3 additions & 1 deletion lib/internal/streams/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ const {
} = primordials;

const { ERR_INVALID_ARG_VALUE } = require('internal/errors').codes;
const { Stream } = require('internal/streams/legacy');

function highWaterMarkFrom(options, isDuplex, duplexKey) {
return options.highWaterMark != null ? options.highWaterMark :
isDuplex ? options[duplexKey] : null;
}

function getDefaultHighWaterMark(objectMode) {
return objectMode ? 16 : 16 * 1024;
return objectMode ? Stream.defaultObjectModeHighWaterMark :
Stream.defaultHighWaterMark;
}

function getHighWaterMark(state, options, duplexKey, isDuplex) {
Expand Down