diff --git a/doc/api/stream.md b/doc/api/stream.md index 4f242a5d519da2..1381149568a8c0 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -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` + + +* {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 diff --git a/lib/internal/streams/legacy.js b/lib/internal/streams/legacy.js index 0a0d0571c46378..d3785ea0ab924d 100644 --- a/lib/internal/streams/legacy.js +++ b/lib/internal/streams/legacy.js @@ -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'); @@ -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 }; diff --git a/lib/internal/streams/state.js b/lib/internal/streams/state.js index 83050a62f9cedc..f2e8d96eab4cde 100644 --- a/lib/internal/streams/state.js +++ b/lib/internal/streams/state.js @@ -6,6 +6,7 @@ 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 : @@ -13,7 +14,8 @@ function highWaterMarkFrom(options, isDuplex, duplexKey) { } function getDefaultHighWaterMark(objectMode) { - return objectMode ? 16 : 16 * 1024; + return objectMode ? Stream.defaultObjectModeHighWaterMark : + Stream.defaultHighWaterMark; } function getHighWaterMark(state, options, duplexKey, isDuplex) {