mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +02:00
Tighten constructor and quality param checks #221
This commit is contained in:
parent
f19b6c48ca
commit
e2c53b59ce
6
index.js
6
index.js
@ -87,9 +87,11 @@ var Sharp = function(input) {
|
||||
} else if (typeof input === 'object' && input instanceof Buffer) {
|
||||
// input=buffer
|
||||
this.options.bufferIn = input;
|
||||
} else {
|
||||
} else if (typeof input === 'undefined') {
|
||||
// input=stream
|
||||
this.options.streamIn = true;
|
||||
} else {
|
||||
throw new Error('Unsupported input ' + typeof input);
|
||||
}
|
||||
return this;
|
||||
};
|
||||
@ -394,7 +396,7 @@ Sharp.prototype.sequentialRead = function(sequentialRead) {
|
||||
};
|
||||
|
||||
Sharp.prototype.quality = function(quality) {
|
||||
if (!Number.isNaN(quality) && quality >= 1 && quality <= 100) {
|
||||
if (!Number.isNaN(quality) && quality >= 1 && quality <= 100 && quality % 1 === 0) {
|
||||
this.options.quality = quality;
|
||||
} else {
|
||||
throw new Error('Invalid quality (1 to 100) ' + quality);
|
||||
|
@ -221,6 +221,29 @@ describe('Input/output', function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe('Fail for unsupported input', function() {
|
||||
it('Numeric', function() {
|
||||
assert.throws(function() {
|
||||
sharp(1);
|
||||
});
|
||||
});
|
||||
it('Boolean', function() {
|
||||
assert.throws(function() {
|
||||
sharp(true);
|
||||
});
|
||||
});
|
||||
it('Empty Object', function() {
|
||||
assert.throws(function() {
|
||||
sharp({});
|
||||
});
|
||||
});
|
||||
it('Error Object', function() {
|
||||
assert.throws(function() {
|
||||
sharp(new Error());
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Promises/A+', function(done) {
|
||||
sharp(fixtures.inputJpg).resize(320, 240).toBuffer().then(function(data) {
|
||||
sharp(data).toBuffer(function(err, data, info) {
|
||||
@ -252,15 +275,22 @@ describe('Input/output', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid quality', function(done) {
|
||||
var isValid = true;
|
||||
try {
|
||||
sharp(fixtures.inputJpg).quality(-1);
|
||||
} catch (err) {
|
||||
isValid = false;
|
||||
}
|
||||
assert.strictEqual(false, isValid);
|
||||
done();
|
||||
describe('Invalid quality', function() {
|
||||
it('Negative integer', function() {
|
||||
assert.throws(function() {
|
||||
sharp(fixtures.inputJpg).quality(-1);
|
||||
});
|
||||
});
|
||||
it('Non integral', function() {
|
||||
assert.throws(function() {
|
||||
sharp(fixtures.inputJpg).quality(88.2);
|
||||
});
|
||||
});
|
||||
it('String', function() {
|
||||
assert.throws(function() {
|
||||
sharp(fixtures.inputJpg).quality('test');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('Progressive JPEG image', function(done) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user