From 86815bc9c49c49ab0a80f5f47de0a6a5f980ec39 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Tue, 1 Mar 2016 20:08:05 +0000 Subject: [PATCH] Emit post-processing 'info' event for Stream-based output --- docs/api.md | 14 ++++++++++++++ docs/changelog.md | 4 ++++ index.js | 6 ++++-- test/unit/io.js | 20 ++++++++++++++++++++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/docs/api.md b/docs/api.md index a69ae6c4..47ccf865 100644 --- a/docs/api.md +++ b/docs/api.md @@ -27,6 +27,7 @@ The object returned by the constructor implements the [stream.Duplex](http://nodejs.org/api/stream.html#stream_class_stream_duplex) class. JPEG, PNG or WebP format image data can be streamed out from this object. +When using Stream based output, derived attributes are available from the `info` event. ```javascript sharp('input.jpg') @@ -37,6 +38,19 @@ sharp('input.jpg') }); ``` +```javascript +// Read image data from readableStream, +// resize to 300 pixels wide, +// emit an 'info' event with calculated dimensions +// and finally write image data to writableStream +var transformer = sharp() + .resize(300) + .on('info', function(info) { + console.log('Image height is ' + info.height); + }); +readableStream.pipe(transformer).pipe(writableStream); +``` + #### metadata([callback]) Fast access to image metadata without decoding any compressed image data. diff --git a/docs/changelog.md b/docs/changelog.md index 8393616c..b8678b41 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -10,6 +10,10 @@ [#338](https://github.com/lovell/sharp/issues/338) [@lookfirst](https://github.com/lookfirst) +* Emit post-processing 'info' event for Stream output. + [#367](https://github.com/lovell/sharp/issues/367) + [@salzhrani](https://github.com/salzhrani) + ### v0.13 - "*mind*" #### v0.13.1 - 27th February 2016 diff --git a/index.js b/index.js index 4a906b4e..39a3aa60 100644 --- a/index.js +++ b/index.js @@ -803,10 +803,11 @@ Sharp.prototype._pipeline = function(callback) { if (this.options.streamIn) { // output=stream, input=stream this.on('finish', function() { - sharp.pipeline(that.options, function(err, data) { + sharp.pipeline(that.options, function(err, data, info) { if (err) { that.emit('error', err); } else { + that.emit('info', info); that.push(data); } that.push(null); @@ -814,10 +815,11 @@ Sharp.prototype._pipeline = function(callback) { }); } else { // output=stream, input=file/buffer - sharp.pipeline(this.options, function(err, data) { + sharp.pipeline(this.options, function(err, data, info) { if (err) { that.emit('error', err); } else { + that.emit('info', info); that.push(data); } that.push(null); diff --git a/test/unit/io.js b/test/unit/io.js index a776dbf5..923b4d9e 100644 --- a/test/unit/io.js +++ b/test/unit/io.js @@ -109,6 +109,26 @@ describe('Input/output', function() { readable.pipe(pipeline).pipe(writable); }); + it('Stream should emit info event', function(done) { + var readable = fs.createReadStream(fixtures.inputJpg); + var writable = fs.createWriteStream(fixtures.outputJpg); + var pipeline = sharp().resize(320, 240); + var infoEventEmitted = false; + pipeline.on('info', function(info) { + assert.strictEqual('jpeg', info.format); + assert.strictEqual(320, info.width); + assert.strictEqual(240, info.height); + assert.strictEqual(3, info.channels); + infoEventEmitted = true; + }); + writable.on('finish', function() { + assert.strictEqual(true, infoEventEmitted); + fs.unlinkSync(fixtures.outputJpg); + done(); + }); + readable.pipe(pipeline).pipe(writable); + }); + it('Handle Stream to Stream error ', function(done) { var pipeline = sharp().resize(320, 240); var anErrorWasEmitted = false;