mirror of
https://github.com/lovell/sharp.git
synced 2025-12-19 07:15:08 +01:00
Add failOnError option to fail-fast on bad input image data (#976)
This commit is contained in:
committed by
Lovell Fuller
parent
382d476271
commit
b4d72bd544
BIN
test/fixtures/expected/truncated.jpg
vendored
Normal file
BIN
test/fixtures/expected/truncated.jpg
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 713 B |
2
test/fixtures/index.js
vendored
2
test/fixtures/index.js
vendored
@@ -66,6 +66,7 @@ module.exports = {
|
||||
inputJpgLarge: getPath('giant-image.jpg'),
|
||||
inputJpg320x240: getPath('320x240.jpg'), // http://www.andrewault.net/2010/01/26/create-a-test-pattern-video-with-perl/
|
||||
inputJpgOverlayLayer2: getPath('alpha-layer-2-ink.jpg'),
|
||||
inputJpgTruncated: getPath('truncated.jpg'), // head -c 10000 2569067123_aca715a2ee_o.jpg > truncated.jpg
|
||||
|
||||
inputPng: getPath('50020484-00001.png'), // http://c.searspartsdirect.com/lis_png/PLDM/50020484-00001.png
|
||||
inputPngWithTransparency: getPath('blackbug.png'), // public domain
|
||||
@@ -81,6 +82,7 @@ module.exports = {
|
||||
inputPngAlphaPremultiplicationLarge: getPath('alpha-premultiply-2048x1536-paper.png'),
|
||||
inputPngBooleanNoAlpha: getPath('bandbool.png'),
|
||||
inputPngTestJoinChannel: getPath('testJoinChannel.png'),
|
||||
inputPngTruncated: getPath('truncated.png'), // gm convert 2569067123_aca715a2ee_o.jpg -resize 320x240 saw.png ; head -c 10000 saw.png > truncated.png
|
||||
|
||||
inputWebP: getPath('4.webp'), // http://www.gstatic.com/webp/gallery/4.webp
|
||||
inputWebPWithTransparency: getPath('5_webp_a.webp'), // http://www.gstatic.com/webp/gallery3/5_webp_a.webp
|
||||
|
||||
BIN
test/fixtures/truncated.jpg
vendored
Normal file
BIN
test/fixtures/truncated.jpg
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.8 KiB |
BIN
test/fixtures/truncated.png
vendored
Normal file
BIN
test/fixtures/truncated.png
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.8 KiB |
77
test/unit/failOnError.js
Normal file
77
test/unit/failOnError.js
Normal file
@@ -0,0 +1,77 @@
|
||||
'use strict';
|
||||
|
||||
const assert = require('assert');
|
||||
|
||||
const sharp = require('../../');
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
describe('failOnError', function () {
|
||||
it('handles truncated JPEG by default', function (done) {
|
||||
sharp(fixtures.inputJpgTruncated)
|
||||
.resize(320, 240)
|
||||
// .toFile(fixtures.expected('truncated.jpg'), done);
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('truncated.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('handles truncated PNG by default', function (done) {
|
||||
sharp(fixtures.inputPngTruncated)
|
||||
.resize(320, 240)
|
||||
// .toFile(fixtures.expected('truncated.png'), done);
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects invalid values', function () {
|
||||
assert.doesNotThrow(function () {
|
||||
sharp(fixtures.inputJpg, { failOnError: true });
|
||||
});
|
||||
|
||||
assert.throws(function () {
|
||||
sharp(fixtures.inputJpg, { failOnError: 'zoinks' });
|
||||
});
|
||||
|
||||
assert.throws(function () {
|
||||
sharp(fixtures.inputJpg, { failOnError: 1 });
|
||||
});
|
||||
});
|
||||
|
||||
it('returns errors to callback for truncated JPEG when failOnError is set', function (done) {
|
||||
sharp(fixtures.inputJpgTruncated, { failOnError: true }).toBuffer(function (err, data, info) {
|
||||
assert.ok(err.message.includes('VipsJpeg: Premature end of JPEG file'), err);
|
||||
assert.equal(data, null);
|
||||
assert.equal(info, null);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns errors to callback for truncated PNG when failOnError is set', function (done) {
|
||||
sharp(fixtures.inputPngTruncated, { failOnError: true }).toBuffer(function (err, data, info) {
|
||||
assert.ok(err.message.includes('vipspng: libpng read error'), err);
|
||||
assert.equal(data, null);
|
||||
assert.equal(info, null);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects promises for truncated JPEG when failOnError is set', function (done) {
|
||||
sharp(fixtures.inputJpgTruncated, { failOnError: true })
|
||||
.toBuffer()
|
||||
.then(() => {
|
||||
throw new Error('Expected rejection');
|
||||
})
|
||||
.catch(err => {
|
||||
done(err.message.includes('VipsJpeg: Premature end of JPEG file') ? undefined : err);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user