Drop support for undef input where opts also provided #1768

This commit is contained in:
Lovell Fuller 2020-01-11 12:02:31 +00:00
parent a8a0c1e935
commit 7dbad7206e
3 changed files with 31 additions and 13 deletions

View File

@ -9,6 +9,9 @@ Requires libvips v8.9.0.
* Drop support for Node.js 8.
[#1910](https://github.com/lovell/sharp/issues/1910)
* Drop support for undefined input where options also provided.
[#1768](https://github.com/lovell/sharp/issues/1768)
* Expose `delay` and `loop` metadata for animated images.
[#1905](https://github.com/lovell/sharp/issues/1905)

View File

@ -23,11 +23,13 @@ function _createInputDescriptor (input, inputOptions, containerOptions) {
// Raw Stream
inputDescriptor.buffer = [];
}
} else if (!is.defined(input) && is.object(containerOptions) && containerOptions.allowStream) {
} else if (!is.defined(input) && !is.defined(inputOptions) && is.object(containerOptions) && containerOptions.allowStream) {
// Stream
inputDescriptor.buffer = [];
} else {
throw new Error('Unsupported input ' + typeof input);
throw new Error(`Unsupported input '${input}' of type ${typeof input}${
is.defined(inputOptions) ? ` when also providing options of type ${typeof inputOptions}` : ''
}`);
}
if (is.object(inputOptions)) {
// Fail on error

View File

@ -635,27 +635,40 @@ describe('Input/output', function () {
});
describe('Input options', function () {
it('Option-less', function () {
sharp();
});
it('Ignore unknown attribute', function () {
sharp({ unknown: true });
});
it('undefined with options fails', function () {
assert.throws(function () {
sharp(undefined, {});
}, /Unsupported input 'undefined' of type undefined when also providing options of type object/);
});
it('null with options fails', function () {
assert.throws(function () {
sharp(null, {});
}, /Unsupported input 'null' of type object when also providing options of type object/);
});
it('Non-Object options fails', function () {
assert.throws(function () {
sharp(null, 'zoinks');
});
sharp('test', 'zoinks');
}, /Invalid input options zoinks/);
});
it('Invalid density: string', function () {
assert.throws(function () {
sharp(null, { density: 'zoinks' });
});
});
it('Ignore unknown attribute', function () {
sharp(null, { unknown: true });
sharp({ density: 'zoinks' });
}, /Expected number between 1 and 2400 for density but received zoinks of type string/);
});
it('Invalid page property throws', function () {
assert.throws(function () {
sharp(null, { page: -1 });
sharp({ page: -1 });
}, /Expected integer between 0 and 100000 for page but received -1 of type number/);
});
it('Invalid pages property throws', function () {
assert.throws(function () {
sharp(null, { pages: '1' });
sharp({ pages: '1' });
}, /Expected integer between -1 and 100000 for pages but received 1 of type string/);
});
});
@ -749,7 +762,7 @@ describe('Input/output', function () {
assert.strictEqual(472, info.height);
assert.strictEqual(3, info.channels);
});
const badPipeline = sharp(null, { raw: { width: 840, height: 500, channels: 3 } })
const badPipeline = sharp({ raw: { width: 840, height: 500, channels: 3 } })
.toFormat('jpeg')
.toBuffer(function (err, data, info) {
assert.strictEqual(err.message.indexOf('memory area too small') > 0, true);
@ -757,7 +770,7 @@ describe('Input/output', function () {
const inPipeline = sharp()
.resize(840, 472)
.raw();
const goodPipeline = sharp(null, { raw: { width: 840, height: 472, channels: 3 } })
const goodPipeline = sharp({ raw: { width: 840, height: 472, channels: 3 } })
.toFormat('jpeg')
.toBuffer(function (err, data, info) {
if (err) throw err;