Emit post-processing 'info' event for Stream-based output

This commit is contained in:
Lovell Fuller 2016-03-01 20:08:05 +00:00
parent bb37dc1ea6
commit 86815bc9c4
4 changed files with 42 additions and 2 deletions

View File

@ -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.

View File

@ -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 - 27<sup>th</sup> February 2016

View File

@ -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);

View File

@ -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;