@@ -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,14 +291,6 @@ 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 ;
@@ -372,23 +365,21 @@ ObjectDefineProperty(WritableState.prototype, 'bufferedRequestCount', {
372
365
} ,
373
366
} ) ;
374
367
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.
368
+ WritableState . prototype [ kOnConstructed ] = function onConstructed ( stream ) {
369
+ if ( ( this [ kState ] & kWriting ) === 0 ) {
370
+ clearBuffer ( stream , this ) ;
371
+ }
383
372
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 ) ;
373
+ if ( ( this [ kState ] & kEnding ) !== 0 ) {
374
+ finishMaybe ( stream , this ) ;
375
+ }
376
+ }
387
377
388
- if ( ! isDuplex && ! FunctionPrototypeSymbolHasInstance ( Writable , this ) )
378
+ function Writable ( options ) {
379
+ if ( ! ( this instanceof Writable ) )
389
380
return new Writable ( options ) ;
390
381
391
- this . _writableState = new WritableState ( options , this , isDuplex ) ;
382
+ this . _writableState = new WritableState ( options , this , false ) ;
392
383
393
384
if ( options ) {
394
385
if ( typeof options . write === 'function' )
@@ -412,17 +403,11 @@ function Writable(options) {
412
403
413
404
Stream . call ( this , options ) ;
414
405
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
- } ) ;
406
+ if ( this . _construct != null ) {
407
+ destroyImpl . construct ( this , ( ) => {
408
+ this . _writableState [ kOnConstructed ] ( this ) ;
409
+ } ) ;
410
+ }
426
411
}
427
412
428
413
ObjectDefineProperty ( Writable , SymbolHasInstance , {
0 commit comments