diff --git a/docs/changelog.md b/docs/changelog.md index e0709faa..7b3c297d 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -24,6 +24,9 @@ Requires libvips v8.9.1 * Add experimental `sharpness` calculation to `stats()` response. [#2251](https://github.com/lovell/sharp/issues/2251) +* Emit `warning` event for non-critical processing problems. + [#2032](https://github.com/lovell/sharp/issues/2032) + ### v0.25.3 - 17th May 2020 * Ensure libvips is initialised only once, improves worker thread safety. diff --git a/lib/constructor.js b/lib/constructor.js index 3ec946c6..8184c95a 100644 --- a/lib/constructor.js +++ b/lib/constructor.js @@ -45,8 +45,13 @@ const debuglog = util.debuglog('sharp'); * JPEG, PNG, WebP or TIFF format image data can be streamed out from this object. * When using Stream based output, derived attributes are available from the `info` event. * + * Non-critical problems encountered during processing are emitted as `warning` events. + * * Implements the [stream.Duplex](http://nodejs.org/api/stream.html#stream_class_stream_duplex) class. * + * @emits Sharp#info + * @emits Sharp#warning + * * @example * sharp('input.jpg') * .resize(300, 200) @@ -230,7 +235,10 @@ const Sharp = function (input, options) { linearA: 1, linearB: 0, // Function to notify of libvips warnings - debuglog: debuglog, + debuglog: warning => { + this.emit('warning', warning); + debuglog(warning); + }, // Function to notify of queue length changes queueListener: function (queueLength) { Sharp.queue.emit('change', queueLength); diff --git a/test/unit/failOnError.js b/test/unit/failOnError.js index e7e65925..b2f5837e 100644 --- a/test/unit/failOnError.js +++ b/test/unit/failOnError.js @@ -19,11 +19,17 @@ describe('failOnError', function () { }); }); - it('handles truncated PNG', function (done) { + it('handles truncated PNG, emits warnings', function (done) { + let isWarningEmitted = false; sharp(fixtures.inputPngTruncated, { failOnError: false }) + .on('warning', function (warning) { + assert.strictEqual('not enough data', warning); + isWarningEmitted = true; + }) .resize(320, 240) .toBuffer(function (err, data, info) { if (err) throw err; + assert.strictEqual(true, isWarningEmitted); assert.strictEqual('png', info.format); assert.strictEqual(320, info.width); assert.strictEqual(240, info.height);