Skip to content

Commit c02de65

Browse files
authored
stream: make Duplex inherit destroy from Writable
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>
1 parent 461d9d6 commit c02de65

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/internal/streams/duplex.js

+3
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ ObjectSetPrototypeOf(Duplex, Readable);
5959
}
6060
}
6161

62+
// Use the `destroy` method of `Writable`.
63+
Duplex.prototype.destroy = Writable.prototype.destroy;
64+
6265
function Duplex(options) {
6366
if (!(this instanceof Duplex))
6467
return new Duplex(options);

test/parallel/test-stream-duplex-destroy.js

+14
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ const assert = require('assert');
238238
});
239239
duplex.on('close', common.mustCall());
240240
}
241+
241242
{
242243
// Check abort signal
243244
const controller = new AbortController();
@@ -255,3 +256,16 @@ const assert = require('assert');
255256
duplex.on('close', common.mustCall());
256257
controller.abort();
257258
}
259+
260+
{
261+
const duplex = new Duplex({
262+
read() {},
263+
write(chunk, enc, cb) { cb(); }
264+
});
265+
266+
duplex.cork();
267+
duplex.write('foo', common.mustCall((err) => {
268+
assert.strictEqual(err.code, 'ERR_STREAM_DESTROYED');
269+
}));
270+
duplex.destroy();
271+
}

0 commit comments

Comments
 (0)