From 7dbad7206ec5bf39d44f0fe3f1ac36cf55f4a08b Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Sat, 11 Jan 2020 12:02:31 +0000 Subject: [PATCH] Drop support for undef input where opts also provided #1768 --- docs/changelog.md | 3 +++ lib/input.js | 6 ++++-- test/unit/io.js | 35 ++++++++++++++++++++++++----------- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 37fbfccd6..d12a72538 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -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) diff --git a/lib/input.js b/lib/input.js index 5aa282785..2d28f4ce6 100644 --- a/lib/input.js +++ b/lib/input.js @@ -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 diff --git a/test/unit/io.js b/test/unit/io.js index 0fd04e9e3..99ff0c551 100644 --- a/test/unit/io.js +++ b/test/unit/io.js @@ -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;