mirror of
https://github.com/lovell/sharp.git
synced 2025-12-19 07:15:08 +01:00
Control sensitivity to invalid images via failOn
Deprecates failOnError, equivalent to failOn=warning
This commit is contained in:
@@ -3,56 +3,70 @@
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
|
||||
const sharp = require('../../');
|
||||
const sharp = require('../../lib');
|
||||
const fixtures = require('../fixtures');
|
||||
|
||||
describe('failOnError', function () {
|
||||
describe('failOn', () => {
|
||||
it('handles truncated JPEG', function (done) {
|
||||
sharp(fixtures.inputJpgTruncated, { failOnError: false })
|
||||
.resize(320, 240)
|
||||
sharp(fixtures.inputJpgTruncated, { failOn: 'none' })
|
||||
.resize(32, 24)
|
||||
.toBuffer(function (err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(240, info.height);
|
||||
assert.strictEqual(32, info.width);
|
||||
assert.strictEqual(24, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('truncated.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('handles truncated PNG, emits warnings', function (done) {
|
||||
let isWarningEmitted = false;
|
||||
sharp(fixtures.inputPngTruncated, { failOnError: false })
|
||||
sharp(fixtures.inputPngTruncated, { failOn: 'none' })
|
||||
.on('warning', function (warning) {
|
||||
assert.ok(warning.includes('not enough data') || warning.includes('end of stream'));
|
||||
isWarningEmitted = true;
|
||||
})
|
||||
.resize(320, 240)
|
||||
.resize(32, 24)
|
||||
.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);
|
||||
assert.strictEqual(32, info.width);
|
||||
assert.strictEqual(24, info.height);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects invalid values', function () {
|
||||
assert.doesNotThrow(function () {
|
||||
sharp(fixtures.inputJpg, { failOnError: true });
|
||||
});
|
||||
it('throws for invalid options', () => {
|
||||
assert.throws(
|
||||
() => sharp({ failOn: 'zoinks' }),
|
||||
/Expected one of: none, truncated, error, warning for failOn but received zoinks of type string/
|
||||
);
|
||||
assert.throws(
|
||||
() => sharp({ failOn: 1 }),
|
||||
/Expected one of: none, truncated, error, warning for failOn but received 1 of type number/
|
||||
);
|
||||
});
|
||||
|
||||
assert.throws(function () {
|
||||
sharp(fixtures.inputJpg, { failOnError: 'zoinks' });
|
||||
});
|
||||
|
||||
assert.throws(function () {
|
||||
sharp(fixtures.inputJpg, { failOnError: 1 });
|
||||
});
|
||||
it('deprecated failOnError', () => {
|
||||
assert.doesNotThrow(
|
||||
() => sharp({ failOnError: true })
|
||||
);
|
||||
assert.doesNotThrow(
|
||||
() => sharp({ failOnError: false })
|
||||
);
|
||||
assert.throws(
|
||||
() => sharp({ failOnError: 'zoinks' }),
|
||||
/Expected boolean for failOnError but received zoinks of type string/
|
||||
);
|
||||
assert.throws(
|
||||
() => sharp({ failOnError: 1 }),
|
||||
/Expected boolean for failOnError but received 1 of type number/
|
||||
);
|
||||
});
|
||||
|
||||
it('returns errors to callback for truncated JPEG', function (done) {
|
||||
sharp(fixtures.inputJpgTruncated).toBuffer(function (err, data, info) {
|
||||
sharp(fixtures.inputJpgTruncated, { failOn: 'truncated' }).toBuffer(function (err, data, info) {
|
||||
assert.ok(err.message.includes('VipsJpeg: Premature end of'), err);
|
||||
assert.strictEqual(data, undefined);
|
||||
assert.strictEqual(info, undefined);
|
||||
@@ -61,7 +75,7 @@ describe('failOnError', function () {
|
||||
});
|
||||
|
||||
it('returns errors to callback for truncated PNG', function (done) {
|
||||
sharp(fixtures.inputPngTruncated).toBuffer(function (err, data, info) {
|
||||
sharp(fixtures.inputPngTruncated, { failOn: 'truncated' }).toBuffer(function (err, data, info) {
|
||||
assert.ok(err.message.includes('read error'), err);
|
||||
assert.strictEqual(data, undefined);
|
||||
assert.strictEqual(info, undefined);
|
||||
@@ -70,7 +84,7 @@ describe('failOnError', function () {
|
||||
});
|
||||
|
||||
it('rejects promises for truncated JPEG', function (done) {
|
||||
sharp(fixtures.inputJpgTruncated)
|
||||
sharp(fixtures.inputJpgTruncated, { failOn: 'error' })
|
||||
.toBuffer()
|
||||
.then(() => {
|
||||
throw new Error('Expected rejection');
|
||||
@@ -80,8 +94,8 @@ describe('failOnError', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('handles stream-based input', function () {
|
||||
const writable = sharp({ failOnError: false });
|
||||
it('handles stream-based input', async () => {
|
||||
const writable = sharp({ failOn: 'none' }).resize(32, 24);
|
||||
fs.createReadStream(fixtures.inputJpgTruncated).pipe(writable);
|
||||
return writable.toBuffer();
|
||||
});
|
||||
Reference in New Issue
Block a user