Skip to content

Commit 4844d30

Browse files
committed
stream: avoid unnecessary drain for sync stream
PR-URL: #50014
1 parent 85c09f1 commit 4844d30

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

lib/internal/streams/transform.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,12 @@ Transform.prototype._write = function(chunk, encoding, callback) {
182182
this.push(val);
183183
}
184184

185-
if (
185+
if (rState.ended) {
186+
// If user has called this.push(null) we have to
187+
// delay the callback to properly progate the new
188+
// state.
189+
process.nextTick(callback);
190+
} else if (
186191
wState.ended || // Backwards compat.
187192
length === rState.length || // Backwards compat.
188193
rState.length < rState.highWaterMark

lib/internal/streams/writable.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -513,14 +513,6 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {
513513

514514
state.length += len;
515515

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-
524516
if ((state.state & (kWriting | kErrored | kCorked | kConstructed)) !== kConstructed) {
525517
state.buffered.push({ chunk, encoding, callback });
526518
if ((state.state & kAllBuffers) !== 0 && encoding !== 'buffer') {
@@ -539,6 +531,12 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {
539531
state.state &= ~kSync;
540532
}
541533

534+
const ret = state.length < state.highWaterMark;
535+
536+
if (!ret) {
537+
state.state |= kNeedDrain;
538+
}
539+
542540
// Return false if errored or destroyed in order to break
543541
// any synchronous while(stream.write(data)) loops.
544542
return ret && (state.state & (kDestroyed | kErrored)) === 0;

0 commit comments

Comments
 (0)