Skip to content

Commit 78cbdf3

Browse files
ronagBridgeAR
authored andcommitted
stream: optimize creation
PR-URL: #29135 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]>
1 parent f61c509 commit 78cbdf3

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

lib/_stream_readable.js

+14-11
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
@@ -119,18 +122,18 @@ function ReadableState(options, stream, isDuplex) {
119122
this.paused = true;
120123

121124
// Should close be emitted on destroy. Defaults to true.
122-
this.emitClose = options.emitClose !== false;
125+
this.emitClose = !options || options.emitClose !== false;
123126

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

127130
// Has it been destroyed
128131
this.destroyed = false;
129132

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

135138
// The number of writers that are awaiting a drain event in .pipe()s
136139
this.awaitDrain = 0;
@@ -140,7 +143,7 @@ function ReadableState(options, stream, isDuplex) {
140143

141144
this.decoder = null;
142145
this.encoding = null;
143-
if (options.encoding) {
146+
if (options && options.encoding) {
144147
if (!StringDecoder)
145148
StringDecoder = require('string_decoder').StringDecoder;
146149
this.decoder = new StringDecoder(options.encoding);

lib/_stream_writable.js

+14-11
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,
@@ -54,8 +57,6 @@ Object.setPrototypeOf(Writable, Stream);
5457
function nop() {}
5558

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

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

7172
if (isDuplex)
72-
this.objectMode = this.objectMode || !!options.writableObjectMode;
73+
this.objectMode = this.objectMode ||
74+
!!(options && options.writableObjectMode);
7375

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

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

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

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

151154
// Should close be emitted on destroy. Defaults to true.
152-
this.emitClose = options.emitClose !== false;
155+
this.emitClose = !options || options.emitClose !== false;
153156

154157
// Should .destroy() be called after 'finish' (and potentially 'end')
155-
this.autoDestroy = !!options.autoDestroy;
158+
this.autoDestroy = !!(options && options.autoDestroy);
156159

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

0 commit comments

Comments
 (0)