Skip to content

Commit

Permalink
stream: fix pipeline calling end on destination more than once
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
debadree25 authored and juanarbol committed Jan 31, 2023
1 parent 0defe4e commit a87963d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/internal/streams/pipeline.js
Expand Up @@ -353,7 +353,7 @@ function pipe(src, dst, finish, { end }) {
}
});

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

if (end) {
// Compat. Before node v10.12.0 stdio used to throw an error so
Expand Down
35 changes: 35 additions & 0 deletions test/parallel/test-stream-pipeline.js
Expand Up @@ -1556,3 +1556,38 @@ const tsp = require('timers/promises');
})
);
}

{
class CustomReadable extends Readable {
_read() {
this.push('asd');
this.push(null);
}
}

class CustomWritable extends Writable {
constructor() {
super();
this.endCount = 0;
this.str = '';
}

_write(chunk, enc, cb) {
this.str += chunk;
cb();
}

end() {
this.endCount += 1;
super.end();
}
}

const readable = new CustomReadable();
const writable = new CustomWritable();

pipeline(readable, writable, common.mustSucceed(() => {
assert.strictEqual(writable.str, 'asd');
assert.strictEqual(writable.endCount, 1);
}));
}

0 comments on commit a87963d

Please sign in to comment.