Skip to content

Commit a87963d

Browse files
debadree25juanarbol
authored andcommitted
stream: fix pipeline calling end on destination more than once
Fixes: #42866 PR-URL: #46226 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
1 parent 0defe4e commit a87963d

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/internal/streams/pipeline.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ function pipe(src, dst, finish, { end }) {
353353
}
354354
});
355355

356-
src.pipe(dst, { end });
356+
src.pipe(dst, { end: false }); // If end is true we already will have a listener to end dst.
357357

358358
if (end) {
359359
// Compat. Before node v10.12.0 stdio used to throw an error so

test/parallel/test-stream-pipeline.js

+35
Original file line numberDiff line numberDiff line change
@@ -1556,3 +1556,38 @@ const tsp = require('timers/promises');
15561556
})
15571557
);
15581558
}
1559+
1560+
{
1561+
class CustomReadable extends Readable {
1562+
_read() {
1563+
this.push('asd');
1564+
this.push(null);
1565+
}
1566+
}
1567+
1568+
class CustomWritable extends Writable {
1569+
constructor() {
1570+
super();
1571+
this.endCount = 0;
1572+
this.str = '';
1573+
}
1574+
1575+
_write(chunk, enc, cb) {
1576+
this.str += chunk;
1577+
cb();
1578+
}
1579+
1580+
end() {
1581+
this.endCount += 1;
1582+
super.end();
1583+
}
1584+
}
1585+
1586+
const readable = new CustomReadable();
1587+
const writable = new CustomWritable();
1588+
1589+
pipeline(readable, writable, common.mustSucceed(() => {
1590+
assert.strictEqual(writable.str, 'asd');
1591+
assert.strictEqual(writable.endCount, 1);
1592+
}));
1593+
}

0 commit comments

Comments
 (0)