diff options
author | Guy Margalit <guymguym@gmail.com> | 2017-08-05 02:12:24 +0300 |
---|---|---|
committer | Matteo Collina <hello@matteocollina.com> | 2017-08-08 11:18:25 +0200 |
commit | c3c045aea06a4d58b4af5c1bdc4f8aebbe5425e4 (patch) | |
tree | b967030d617a6b388549ce151de6212f2b436924 /lib/_stream_writable.js | |
parent | 4da8b99a74c9d00a3733d50c6348750360bf1c40 (diff) | |
download | node-new-c3c045aea06a4d58b4af5c1bdc4f8aebbe5425e4.tar.gz |
stream: support readable/writableHWM for Duplex
This commits adds support for readableHighWaterMark and
writableHighWaterMark in Duplex stream, so that they can be set without
accessing the internal state.
Fixes: https://github.com/nodejs/node/issues/14555
PR-URL: https://github.com/nodejs/node/pull/14636
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Diffstat (limited to 'lib/_stream_writable.js')
-rw-r--r-- | lib/_stream_writable.js | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 334f492ba6..6e0eaf45b5 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -41,19 +41,33 @@ function nop() {} function WritableState(options, stream) { options = options || {}; + // Duplex streams are both readable and writable, but share + // the same options object. + // However, some cases require setting options to different + // values for the readable and the writable sides of the duplex stream. + // These options can be provided separately as readableXXX and writableXXX. + var isDuplex = stream instanceof Stream.Duplex; + // object stream flag to indicate whether or not this stream // contains buffers or objects. this.objectMode = !!options.objectMode; - if (stream instanceof Stream.Duplex) + if (isDuplex) this.objectMode = this.objectMode || !!options.writableObjectMode; // 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() var hwm = options.highWaterMark; + var writableHwm = options.writableHighWaterMark; var defaultHwm = this.objectMode ? 16 : 16 * 1024; - this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm; + + if (hwm || hwm === 0) + this.highWaterMark = hwm; + else if (isDuplex && (writableHwm || writableHwm === 0)) + this.highWaterMark = writableHwm; + else + this.highWaterMark = defaultHwm; // cast to ints. this.highWaterMark = Math.floor(this.highWaterMark); |