Skip to content

Commit e1fec22

Browse files
oppenlanderindutny
authored andcommitted
streams: set default hwm properly for Duplex
Default highWaterMark is now set properly when using stream Duplex's writableObjectMode and readableObjectMode options. Added condition to the already existing split objectMode test to ensure the highWaterMark is being set to the correct default value on both the ReadableState and WritableState for readableObjectMode and writableObjectMode. Signed-off-by: Fedor Indutny <fedor@indutny.com>
1 parent 832d4db commit e1fec22

File tree

3 files changed

+20
-17
lines changed

3 files changed

+20
-17
lines changed

lib/_stream_readable.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,17 @@ util.inherits(Readable, Stream);
3333
function ReadableState(options, stream) {
3434
options = options || {};
3535

36+
// object stream flag. Used to make read(n) ignore n and to
37+
// make all the buffer merging and length checks go away
38+
this.objectMode = !!options.objectMode;
39+
40+
if (stream instanceof Stream.Duplex)
41+
this.objectMode = this.objectMode || !!options.readableObjectMode;
42+
3643
// the point at which it stops calling _read() to fill the buffer
3744
// Note: 0 is a valid value, means "don't call _read preemptively ever"
3845
var hwm = options.highWaterMark;
39-
var defaultHwm = options.objectMode ? 16 : 16 * 1024;
46+
var defaultHwm = this.objectMode ? 16 : 16 * 1024;
4047
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
4148

4249
// cast to ints.
@@ -63,14 +70,6 @@ function ReadableState(options, stream) {
6370
this.emittedReadable = false;
6471
this.readableListening = false;
6572

66-
67-
// object stream flag. Used to make read(n) ignore n and to
68-
// make all the buffer merging and length checks go away
69-
this.objectMode = !!options.objectMode;
70-
71-
if (stream instanceof Stream.Duplex)
72-
this.objectMode = this.objectMode || !!options.readableObjectMode;
73-
7473
// Crypto is kind of old and crusty. Historically, its default string
7574
// encoding is 'binary' so we have to make this configurable.
7675
// Everything else in the universe uses 'utf8', though.

lib/_stream_writable.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ function WriteReq(chunk, encoding, cb) {
4040
function WritableState(options, stream) {
4141
options = options || {};
4242

43-
// the point at which write() starts returning false
44-
// Note: 0 is a valid value, means that we always return false if
45-
// the entire buffer is not flushed immediately on write()
46-
var hwm = options.highWaterMark;
47-
var defaultHwm = options.objectMode ? 16 : 16 * 1024;
48-
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
49-
5043
// object stream flag to indicate whether or not this stream
5144
// contains buffers or objects.
5245
this.objectMode = !!options.objectMode;
5346

5447
if (stream instanceof Stream.Duplex)
5548
this.objectMode = this.objectMode || !!options.writableObjectMode;
5649

50+
// the point at which write() starts returning false
51+
// Note: 0 is a valid value, means that we always return false if
52+
// the entire buffer is not flushed immediately on write()
53+
var hwm = options.highWaterMark;
54+
var defaultHwm = this.objectMode ? 16 : 16 * 1024;
55+
this.highWaterMark = (hwm || hwm === 0) ? hwm : defaultHwm;
56+
5757
// cast to ints.
5858
this.highWaterMark = ~~this.highWaterMark;
5959

test/simple/test-stream-transform-split-objectmode.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ var parser = new Transform({ readableObjectMode : true });
2828

2929
assert(parser._readableState.objectMode);
3030
assert(!parser._writableState.objectMode);
31+
assert(parser._readableState.highWaterMark === 16);
32+
assert(parser._writableState.highWaterMark === (16 * 1024));
3133

3234
parser._transform = function (chunk, enc, callback) {
3335
callback(null, { val : chunk[0] });
@@ -50,10 +52,12 @@ var serializer = new Transform({ writableObjectMode : true });
5052

5153
assert(!serializer._readableState.objectMode);
5254
assert(serializer._writableState.objectMode);
55+
assert(serializer._readableState.highWaterMark === (16 * 1024));
56+
assert(serializer._writableState.highWaterMark === 16);
5357

5458
serializer._transform = function (obj, _, callback) {
5559
callback(null, new Buffer([obj.val]));
56-
}
60+
};
5761

5862
var serialized;
5963

0 commit comments

Comments
 (0)