@@ -44,6 +44,7 @@ const EE = require('events');
44
44
const Stream = require ( 'internal/streams/legacy' ) . Stream ;
45
45
const { Buffer } = require ( 'buffer' ) ;
46
46
const destroyImpl = require ( 'internal/streams/destroy' ) ;
47
+ const { kOnConstructed } = require ( 'internal/streams/utils' ) ;
47
48
48
49
const {
49
50
addAbortSignal,
@@ -290,20 +291,15 @@ ObjectDefineProperties(WritableState.prototype, {
290
291
} ) ;
291
292
292
293
function WritableState ( options , stream , isDuplex ) {
293
- // Duplex streams are both readable and writable, but share
294
- // the same options object.
295
- // However, some cases require setting options to different
296
- // values for the readable and the writable sides of the duplex stream,
297
- // e.g. options.readableObjectMode vs. options.writableObjectMode, etc.
298
- if ( typeof isDuplex !== 'boolean' )
299
- isDuplex = stream instanceof Stream . Duplex ;
300
-
301
294
// Bit map field to store WritableState more effciently with 1 bit per field
302
295
// instead of a V8 slot per field.
303
296
this [ kState ] = kSync | kConstructed | kEmitClose | kAutoDestroy ;
304
297
305
- if ( options && options . objectMode ) this [ kState ] |= kObjectMode ;
306
- if ( isDuplex && options && options . writableObjectMode ) this [ kState ] |= kObjectMode ;
298
+ if ( options && options . objectMode )
299
+ this [ kState ] |= kObjectMode ;
300
+
301
+ if ( isDuplex && options && options . writableObjectMode )
302
+ this [ kState ] |= kObjectMode ;
307
303
308
304
// The point at which write() starts returning false
309
305
// Note: 0 is a valid value, means that we always return false if
@@ -323,7 +319,7 @@ function WritableState(options, stream, isDuplex) {
323
319
// Crypto is kind of old and crusty. Historically, its default string
324
320
// encoding is 'binary' so we have to make this configurable.
325
321
// Everything else in the universe uses 'utf8', though.
326
- const defaultEncoding = options ? .defaultEncoding ;
322
+ const defaultEncoding = options ? options . defaultEncoding : null ;
327
323
if ( defaultEncoding == null || defaultEncoding === 'utf8' || defaultEncoding === 'utf-8' ) {
328
324
this [ kState ] |= kDefaultUTF8Encoding ;
329
325
} else if ( Buffer . isEncoding ( defaultEncoding ) ) {
@@ -372,23 +368,21 @@ ObjectDefineProperty(WritableState.prototype, 'bufferedRequestCount', {
372
368
} ,
373
369
} ) ;
374
370
375
- function Writable ( options ) {
376
- // Writable ctor is applied to Duplexes, too.
377
- // `realHasInstance` is necessary because using plain `instanceof`
378
- // would return false, as no `_writableState` property is attached.
379
-
380
- // Trying to use the custom `instanceof` for Writable here will also break the
381
- // Node.js LazyTransform implementation, which has a non-trivial getter for
382
- // `_writableState` that would lead to infinite recursion.
371
+ WritableState . prototype [ kOnConstructed ] = function onConstructed ( stream ) {
372
+ if ( ( this [ kState ] & kWriting ) === 0 ) {
373
+ clearBuffer ( stream , this ) ;
374
+ }
383
375
384
- // Checking for a Stream.Duplex instance is faster here instead of inside
385
- // the WritableState constructor, at least with V8 6.5.
386
- const isDuplex = ( this instanceof Stream . Duplex ) ;
376
+ if ( ( this [ kState ] & kEnding ) !== 0 ) {
377
+ finishMaybe ( stream , this ) ;
378
+ }
379
+ } ;
387
380
388
- if ( ! isDuplex && ! FunctionPrototypeSymbolHasInstance ( Writable , this ) )
381
+ function Writable ( options ) {
382
+ if ( ! ( this instanceof Writable ) )
389
383
return new Writable ( options ) ;
390
384
391
- this . _writableState = new WritableState ( options , this , isDuplex ) ;
385
+ this . _writableState = new WritableState ( options , this , false ) ;
392
386
393
387
if ( options ) {
394
388
if ( typeof options . write === 'function' )
@@ -412,17 +406,11 @@ function Writable(options) {
412
406
413
407
Stream . call ( this , options ) ;
414
408
415
- destroyImpl . construct ( this , ( ) => {
416
- const state = this . _writableState ;
417
-
418
- if ( ( state [ kState ] & kWriting ) === 0 ) {
419
- clearBuffer ( this , state ) ;
420
- }
421
-
422
- if ( ( state [ kState ] & kEnding ) !== 0 ) {
423
- finishMaybe ( this , state ) ;
424
- }
425
- } ) ;
409
+ if ( this . _construct != null ) {
410
+ destroyImpl . construct ( this , ( ) => {
411
+ this . _writableState [ kOnConstructed ] ( this ) ;
412
+ } ) ;
413
+ }
426
414
}
427
415
428
416
ObjectDefineProperty ( Writable , SymbolHasInstance , {
0 commit comments