From 1617a4621ea3230aac80fcb277d33092c339cbcd Mon Sep 17 00:00:00 2001 From: Giacomo Gregoletto Date: Tue, 28 Jun 2022 10:59:24 +0200 Subject: [PATCH] stream: pass error on legacy destroy PR-URL: https://github.com/nodejs/node/pull/43519 Reviewed-By: Robert Nagy Reviewed-By: Luigi Pinca Reviewed-By: Benjamin Gruenbaum Reviewed-By: Matteo Collina Reviewed-By: Minwoo Jung Reviewed-By: James M Snell --- lib/internal/streams/destroy.js | 2 +- test/parallel/test-stream-pipeline.js | 30 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/internal/streams/destroy.js b/lib/internal/streams/destroy.js index 10f5471e21d3eb..5c352135dc3012 100644 --- a/lib/internal/streams/destroy.js +++ b/lib/internal/streams/destroy.js @@ -319,7 +319,7 @@ function destroyer(stream, err) { // TODO: Don't lose err? stream.close(); } else if (err) { - process.nextTick(emitErrorCloseLegacy, stream); + process.nextTick(emitErrorCloseLegacy, stream, err); } else { process.nextTick(emitCloseLegacy, stream); } diff --git a/test/parallel/test-stream-pipeline.js b/test/parallel/test-stream-pipeline.js index eecf836b5bb77f..529b18386e25a6 100644 --- a/test/parallel/test-stream-pipeline.js +++ b/test/parallel/test-stream-pipeline.js @@ -1526,3 +1526,33 @@ const tsp = require('timers/promises'); assert.strictEqual(val, null); })); } + +{ + // Mimics a legacy stream without the .destroy method + class LegacyWritable extends Stream { + write(chunk, encoding, callback) { + callback(); + } + } + + const writable = new LegacyWritable(); + writable.on('error', common.mustCall((err) => { + assert.deepStrictEqual(err, new Error('stop')); + })); + + pipeline( + Readable.from({ + [Symbol.asyncIterator]() { + return { + next() { + return Promise.reject(new Error('stop')); + } + }; + } + }), + writable, + common.mustCall((err) => { + assert.deepStrictEqual(err, new Error('stop')); + }) + ); +}