Skip to content

Commit bd02775

Browse files
ronagzhangyongsheng
authored and
zhangyongsheng
committed
stream: optimize creation
PR-URL: #29135 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]>
1 parent 8235ffd commit bd02775

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

lib/_stream_readable.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ const { Buffer } = require('buffer');
3333
const debug = require('internal/util/debuglog').debuglog('stream');
3434
const BufferList = require('internal/streams/buffer_list');
3535
const destroyImpl = require('internal/streams/destroy');
36-
const { getHighWaterMark } = require('internal/streams/state');
36+
const {
37+
getHighWaterMark,
38+
getDefaultHighWaterMark
39+
} = require('internal/streams/state');
3740
const {
3841
ERR_INVALID_ARG_TYPE,
3942
ERR_STREAM_PUSH_AFTER_EOF,
@@ -70,8 +73,6 @@ function prependListener(emitter, event, fn) {
7073
}
7174

7275
function ReadableState(options, stream, isDuplex) {
73-
options = options || {};
74-
7576
// Duplex streams are both readable and writable, but share
7677
// the same options object.
7778
// However, some cases require setting options to different
@@ -82,15 +83,17 @@ function ReadableState(options, stream, isDuplex) {
8283

8384
// Object stream flag. Used to make read(n) ignore n and to
8485
// make all the buffer merging and length checks go away
85-
this.objectMode = !!options.objectMode;
86+
this.objectMode = !!(options && options.objectMode);
8687

8788
if (isDuplex)
88-
this.objectMode = this.objectMode || !!options.readableObjectMode;
89+
this.objectMode = this.objectMode ||
90+
!!(options && options.readableObjectMode);
8991

9092
// The point at which it stops calling _read() to fill the buffer
9193
// Note: 0 is a valid value, means "don't call _read preemptively ever"
92-
this.highWaterMark = getHighWaterMark(this, options, 'readableHighWaterMark',
93-
isDuplex);
94+
this.highWaterMark = options ?
95+
getHighWaterMark(this, options, 'readableHighWaterMark', isDuplex) :
96+
getDefaultHighWaterMark(false);
9497

9598
// A linked list is used to store data chunks instead of an array because the
9699
// linked list can remove elements from the beginning faster than
@@ -121,18 +124,18 @@ function ReadableState(options, stream, isDuplex) {
121124
this.errorEmitted = false;
122125

123126
// Should close be emitted on destroy. Defaults to true.
124-
this.emitClose = options.emitClose !== false;
127+
this.emitClose = !options || options.emitClose !== false;
125128

126129
// Should .destroy() be called after 'end' (and potentially 'finish')
127-
this.autoDestroy = !!options.autoDestroy;
130+
this.autoDestroy = !!(options && options.autoDestroy);
128131

129132
// Has it been destroyed
130133
this.destroyed = false;
131134

132135
// Crypto is kind of old and crusty. Historically, its default string
133136
// encoding is 'binary' so we have to make this configurable.
134137
// Everything else in the universe uses 'utf8', though.
135-
this.defaultEncoding = options.defaultEncoding || 'utf8';
138+
this.defaultEncoding = (options && options.defaultEncoding) || 'utf8';
136139

137140
// Ref the piped dest which we need a drain event on it
138141
// type: null | Writable | Set<Writable>
@@ -144,7 +147,7 @@ function ReadableState(options, stream, isDuplex) {
144147

145148
this.decoder = null;
146149
this.encoding = null;
147-
if (options.encoding) {
150+
if (options && options.encoding) {
148151
if (!StringDecoder)
149152
StringDecoder = require('string_decoder').StringDecoder;
150153
this.decoder = new StringDecoder(options.encoding);

lib/_stream_writable.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ const internalUtil = require('internal/util');
3434
const Stream = require('stream');
3535
const { Buffer } = require('buffer');
3636
const destroyImpl = require('internal/streams/destroy');
37-
const { getHighWaterMark } = require('internal/streams/state');
37+
const {
38+
getHighWaterMark,
39+
getDefaultHighWaterMark
40+
} = require('internal/streams/state');
3841
const {
3942
ERR_INVALID_ARG_TYPE,
4043
ERR_METHOD_NOT_IMPLEMENTED,
@@ -55,8 +58,6 @@ Object.setPrototypeOf(Writable, Stream);
5558
function nop() {}
5659

5760
function WritableState(options, stream, isDuplex) {
58-
options = options || {};
59-
6061
// Duplex streams are both readable and writable, but share
6162
// the same options object.
6263
// However, some cases require setting options to different
@@ -67,16 +68,18 @@ function WritableState(options, stream, isDuplex) {
6768

6869
// Object stream flag to indicate whether or not this stream
6970
// contains buffers or objects.
70-
this.objectMode = !!options.objectMode;
71+
this.objectMode = !!(options && options.objectMode);
7172

7273
if (isDuplex)
73-
this.objectMode = this.objectMode || !!options.writableObjectMode;
74+
this.objectMode = this.objectMode ||
75+
!!(options && options.writableObjectMode);
7476

7577
// The point at which write() starts returning false
7678
// Note: 0 is a valid value, means that we always return false if
7779
// the entire buffer is not flushed immediately on write()
78-
this.highWaterMark = getHighWaterMark(this, options, 'writableHighWaterMark',
79-
isDuplex);
80+
this.highWaterMark = options ?
81+
getHighWaterMark(this, options, 'writableHighWaterMark', isDuplex) :
82+
getDefaultHighWaterMark(false);
8083

8184
// if _final has been called
8285
this.finalCalled = false;
@@ -96,13 +99,13 @@ function WritableState(options, stream, isDuplex) {
9699
// Should we decode strings into buffers before passing to _write?
97100
// this is here so that some node-core streams can optimize string
98101
// handling at a lower level.
99-
const noDecode = options.decodeStrings === false;
102+
const noDecode = !!(options && options.decodeStrings === false);
100103
this.decodeStrings = !noDecode;
101104

102105
// Crypto is kind of old and crusty. Historically, its default string
103106
// encoding is 'binary' so we have to make this configurable.
104107
// Everything else in the universe uses 'utf8', though.
105-
this.defaultEncoding = options.defaultEncoding || 'utf8';
108+
this.defaultEncoding = (options && options.defaultEncoding) || 'utf8';
106109

107110
// Not an actual buffer we keep track of, but a measurement
108111
// of how much we're waiting to get pushed to some underlying
@@ -150,10 +153,10 @@ function WritableState(options, stream, isDuplex) {
150153
this.errorEmitted = false;
151154

152155
// Should close be emitted on destroy. Defaults to true.
153-
this.emitClose = options.emitClose !== false;
156+
this.emitClose = !options || options.emitClose !== false;
154157

155158
// Should .destroy() be called after 'finish' (and potentially 'end')
156-
this.autoDestroy = !!options.autoDestroy;
159+
this.autoDestroy = !!(options && options.autoDestroy);
157160

158161
// Count buffered requests
159162
this.bufferedRequestCount = 0;

0 commit comments

Comments
 (0)