From a06b8c296ae0253356cf86200a041bc7b4dd0cad Mon Sep 17 00:00:00 2001 From: Drian Naude <4474382+driannaude@users.noreply.github.com> Date: Thu, 18 Nov 2021 00:19:58 +1300 Subject: [PATCH] Properly emit close events for duplex streams (#2976) --- lib/output.js | 2 ++ test/unit/io.js | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/output.js b/lib/output.js index 167667ae6..30443c94f 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1078,6 +1078,7 @@ function _pipeline (callback) { this.push(data); } this.push(null); + this.emit('close'); }); }); if (this.streamInFinished) { @@ -1093,6 +1094,7 @@ function _pipeline (callback) { this.push(data); } this.push(null); + this.emit('close'); }); } return this; diff --git a/test/unit/io.js b/test/unit/io.js index e335fbdf4..ff5a8e37c 100644 --- a/test/unit/io.js +++ b/test/unit/io.js @@ -201,6 +201,21 @@ describe('Input/output', function () { readable.pipe(pipeline).pipe(writable); }); + it('Stream should emit close event', function (done) { + const readable = fs.createReadStream(fixtures.inputJpg); + const writable = fs.createWriteStream(outputJpg); + const pipeline = sharp().resize(320, 240); + let closeEventEmitted = false; + pipeline.on('close', function () { + closeEventEmitted = true; + }); + writable.on('close', function () { + assert.strictEqual(true, closeEventEmitted); + rimraf(outputJpg, done); + }); + readable.pipe(pipeline).pipe(writable); + }); + it('Handle Stream to Stream error ', function (done) { const pipeline = sharp().resize(320, 240); let anErrorWasEmitted = false;