@@ -108,6 +108,7 @@ const kWriteCb = 1 << 26;
108
108
const kExpectWriteCb = 1 << 27 ;
109
109
const kAfterWriteTickInfo = 1 << 28 ;
110
110
const kAfterWritePending = 1 << 29 ;
111
+ const kIsDuplex = 1 << 30 ;
111
112
112
113
// TODO(benjamingr) it is likely slower to do it this way than with free functions
113
114
function makeBitMapDescriptor ( bit ) {
@@ -286,6 +287,7 @@ function WritableState(options, stream, isDuplex) {
286
287
287
288
if ( options && options . objectMode ) this . state |= kObjectMode ;
288
289
if ( isDuplex && options && options . writableObjectMode ) this . state |= kObjectMode ;
290
+ if ( isDuplex ) this . state |= kIsDuplex ;
289
291
290
292
// The point at which write() starts returning false
291
293
// Note: 0 is a valid value, means that we always return false if
@@ -513,14 +515,6 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {
513
515
514
516
state . length += len ;
515
517
516
- // stream._write resets state.length
517
- const ret = state . length < state . highWaterMark ;
518
-
519
- // We must ensure that previous needDrain will not be reset to false.
520
- if ( ! ret ) {
521
- state . state |= kNeedDrain ;
522
- }
523
-
524
518
if ( ( state . state & ( kWriting | kErrored | kCorked | kConstructed ) ) !== kConstructed ) {
525
519
state . buffered . push ( { chunk, encoding, callback } ) ;
526
520
if ( ( state . state & kAllBuffers ) !== 0 && encoding !== 'buffer' ) {
@@ -539,6 +533,15 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {
539
533
state . state &= ~ kSync ;
540
534
}
541
535
536
+ const ret = (
537
+ state . length < state . highWaterMark &&
538
+ ( ( state . state & kIsDuplex ) === 0 || stream . _readableState ?. ended !== true )
539
+ ) ;
540
+
541
+ if ( ! ret ) {
542
+ state . state |= kNeedDrain ;
543
+ }
544
+
542
545
// Return false if errored or destroyed in order to break
543
546
// any synchronous while(stream.write(data)) loops.
544
547
return ret && ( state . state & ( kDestroyed | kErrored ) ) === 0 ;
0 commit comments