Skip to content

Commit

Permalink
stream: fix duplexify premature destroy
Browse files Browse the repository at this point in the history
The duplexified Duplex should be autoDestroyed instead of
prematurely destroyed when the readable and writable sides
have finished without error.

Fixes: #44925

PR-URL: #45133
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
  • Loading branch information
ronag authored and RafaelGSS committed Nov 1, 2022
1 parent 500c15b commit 874ea99
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
2 changes: 0 additions & 2 deletions lib/internal/streams/duplexify.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,6 @@ function _duplexify(pair) {
cb(err);
} else if (err) {
d.destroy(err);
} else if (!readable && !writable) {
d.destroy();
}
}

Expand Down
23 changes: 22 additions & 1 deletion test/parallel/test-stream-duplex-from.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const common = require('../common');
const assert = require('assert');
const { Duplex, Readable, Writable, pipeline } = require('stream');
const { Duplex, Readable, Writable, pipeline, PassThrough } = require('stream');
const { Blob } = require('buffer');

{
Expand Down Expand Up @@ -278,3 +278,24 @@ const { Blob } = require('buffer');

duplex.write('test');
}

{
const through = new PassThrough({ objectMode: true });

let res = '';
const d = Readable.from(['foo', 'bar'], { objectMode: true })
.pipe(Duplex.from({
writable: through,
readable: through
}));

d.on('data', (data) => {
d.pause();
setImmediate(() => {
d.resume();
});
res += data;
}).on('end', common.mustCall(() => {
assert.strictEqual(res, 'foobar');
})).on('close', common.mustCall());
}

0 comments on commit 874ea99

Please sign in to comment.