Skip to content

Commit 2bfb340

Browse files
ronagMylesBorins
authored andcommitted
stream: avoid destroying writable source
User might still want to be able to use the writable side of src. This is in the case where e.g. the Duplex input is not directly connected to its output. Such a case could happen when the Duplex is reading from a socket and then echos the data back on the same socket. PR-URL: #32198 Refs: 4d93e10#commitcomment-37751035 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent f8f2089 commit 2bfb340

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

lib/internal/streams/pipeline.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ function destroyer(stream, reading, writing, final, callback) {
5050
return callback();
5151
}
5252

53+
if (!err && reading && !writing && stream.writable) {
54+
return callback();
55+
}
56+
5357
if (err || !final || !stream.readable) {
5458
destroyImpl.destroyer(stream, err);
5559
}

test/parallel/test-stream-pipeline.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,3 +1032,19 @@ const { promisify } = require('util');
10321032
req.on('error', common.mustNotCall());
10331033
});
10341034
}
1035+
1036+
{
1037+
// Might still want to be able to use the writable side
1038+
// of src. This is in the case where e.g. the Duplex input
1039+
// is not directly connected to its output. Such a case could
1040+
// happen when the Duplex is reading from a socket and then echos
1041+
// the data back on the same socket.
1042+
const src = new PassThrough();
1043+
assert.strictEqual(src.writable, true);
1044+
const dst = new PassThrough();
1045+
pipeline(src, dst, common.mustCall((err) => {
1046+
assert.strictEqual(src.writable, true);
1047+
assert.strictEqual(src.destroyed, false);
1048+
}));
1049+
src.push(null);
1050+
}

0 commit comments

Comments
 (0)