Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/internal/streams/duplexpair.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,26 @@ class DuplexSide extends Duplex {
this.#otherSide.on('end', callback);
this.#otherSide.push(null);
}


_destroy(err, callback) {
if (err) {
// Error case: tell the other side to also destroy with that error.
this.#otherSide.destroy(err);
} else if (this.#otherSide && !this.#otherSide.destroyed) {
// Graceful close case (destroy() without error):
// send an EOF to the other side's readable end if it hasn't already closed.
this.#otherSide.push(null);
}
callback(err);
}
}

function duplexPair(options) {
const side0 = new DuplexSide(options);
const side1 = new DuplexSide(options);
side0[kInitOtherSide](side1);
side1[kInitOtherSide](side0);
return [ side0, side1 ];
return [side0, side1];
}
module.exports = duplexPair;
30 changes: 30 additions & 0 deletions test/parallel/test-duplex-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const { duplexPair } = require('stream');

const [sideA, sideB] = duplexPair();

let sideAErrorReceived = false;
let sideBErrorReceived = false;

// Use common.mustCall inside the listeners to ensure they trigger
sideA.on('error', common.mustCall((err) => {
sideAErrorReceived = true;
}));

sideB.on('error', common.mustCall((err) => {
sideBErrorReceived = true;
}));

sideA.resume();
sideB.resume();

sideB.destroy(new Error('Simulated error'));

// Wrap the callback in common.mustCall()
setImmediate(common.mustCall(() => {
assert.strictEqual(sideAErrorReceived, true);
assert.strictEqual(sideBErrorReceived, true);
}));
Loading