Properly emit close events for duplex streams (#2976)

This commit is contained in:
Drian Naude 2021-11-18 00:19:58 +13:00 committed by GitHub
parent 602f988aba
commit a06b8c296a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -1078,6 +1078,7 @@ function _pipeline (callback) {
this.push(data); this.push(data);
} }
this.push(null); this.push(null);
this.emit('close');
}); });
}); });
if (this.streamInFinished) { if (this.streamInFinished) {
@ -1093,6 +1094,7 @@ function _pipeline (callback) {
this.push(data); this.push(data);
} }
this.push(null); this.push(null);
this.emit('close');
}); });
} }
return this; return this;

View File

@ -201,6 +201,21 @@ describe('Input/output', function () {
readable.pipe(pipeline).pipe(writable); 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) { it('Handle Stream to Stream error ', function (done) {
const pipeline = sharp().resize(320, 240); const pipeline = sharp().resize(320, 240);
let anErrorWasEmitted = false; let anErrorWasEmitted = false;