Skip to content

Commit

Permalink
Drop support for undef input where opts also provided #1768
Browse files Browse the repository at this point in the history
  • Loading branch information
lovell committed Jan 11, 2020
1 parent a8a0c1e commit 7dbad72
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
3 changes: 3 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 4 additions & 2 deletions lib/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 24 additions & 11 deletions test/unit/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
});
});
Expand Down Expand Up @@ -749,15 +762,15 @@ 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);
const readable = fs.createReadStream(fixtures.inputJPGBig);
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;
Expand Down

0 comments on commit 7dbad72

Please sign in to comment.