Skip to content

Commit

Permalink
stream: make Duplex inherit destroy from Writable
Browse files Browse the repository at this point in the history
Make `Duplex` inherit the `destroy` method from `Writable` instead of
`Readable` so that pending write callbacks are correctly invoked when
the stream is destroyed.

PR-URL: #52318
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
lpinca authored Apr 5, 2024
1 parent 461d9d6 commit c02de65
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/internal/streams/duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ ObjectSetPrototypeOf(Duplex, Readable);
}
}

// Use the `destroy` method of `Writable`.
Duplex.prototype.destroy = Writable.prototype.destroy;

function Duplex(options) {
if (!(this instanceof Duplex))
return new Duplex(options);
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-stream-duplex-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ const assert = require('assert');
});
duplex.on('close', common.mustCall());
}

{
// Check abort signal
const controller = new AbortController();
Expand All @@ -255,3 +256,16 @@ const assert = require('assert');
duplex.on('close', common.mustCall());
controller.abort();
}

{
const duplex = new Duplex({
read() {},
write(chunk, enc, cb) { cb(); }
});

duplex.cork();
duplex.write('foo', common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED');
}));
duplex.destroy();
}

0 comments on commit c02de65

Please sign in to comment.