Skip to content

Commit

Permalink
Attach event listener to clone only for Stream #995
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Dec 12, 2017
1 parent 0894145 commit 9fa04a0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
4 changes: 4 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Requires libvips v8.6.0.
[#977](https://github.com/lovell/sharp/pull/977)
[@jardakotesovec](https://github.com/jardakotesovec)

* Attach finish event listener to a clone only for Stream-based input.
[#995](https://github.com/lovell/sharp/issues/995)
[@whmountains](https://github.com/whmountains)

* Add tilecache before smartcrop to avoid over-computation of previous operations.
[#1028](https://github.com/lovell/sharp/issues/1028)
[@coffeebite](https://github.com/coffeebite)
Expand Down
14 changes: 8 additions & 6 deletions lib/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ function clone () {
const clone = this.constructor.call();
clone.options = Object.assign({}, this.options);
// Pass 'finish' event to clone for Stream-based input
this.on('finish', function () {
// Clone inherits input data
that._flattenBufferIn();
clone.options.bufferIn = that.options.bufferIn;
clone.emit('finish');
});
if (this._isStreamInput()) {
this.on('finish', function () {
// Clone inherits input data
that._flattenBufferIn();
clone.options.bufferIn = that.options.bufferIn;
clone.emit('finish');
});
}
return clone;
}

Expand Down
14 changes: 14 additions & 0 deletions test/unit/clone.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,18 @@ describe('Clone', function () {
// Go
fs.createReadStream(fixtures.inputJpg).pipe(rotator);
});

it('Stream-based input attaches finish event listener to original', function () {
const original = sharp();
const clone = original.clone();
assert.strictEqual(1, original.listenerCount('finish'));
assert.strictEqual(0, clone.listenerCount('finish'));
});

it('Non Stream-based input does not attach finish event listeners', function () {
const original = sharp(fixtures.inputJpg);
const clone = original.clone();
assert.strictEqual(0, original.listenerCount('finish'));
assert.strictEqual(0, clone.listenerCount('finish'));
});
});

0 comments on commit 9fa04a0

Please sign in to comment.