mirror of
https://github.com/lovell/sharp.git
synced 2025-07-09 10:30:15 +02:00
Fail fast for Buffer with unsupported format #105
This commit is contained in:
parent
2e61839387
commit
a472adeb74
9
index.js
9
index.js
@ -54,10 +54,15 @@ var Sharp = function(input) {
|
|||||||
this.options.fileIn = input;
|
this.options.fileIn = input;
|
||||||
} else if (typeof input === 'object' && input instanceof Buffer) {
|
} else if (typeof input === 'object' && input instanceof Buffer) {
|
||||||
// input=buffer
|
// input=buffer
|
||||||
if (input.length > 0) {
|
if (
|
||||||
|
(input.length > 1) &&
|
||||||
|
(input[0] === 0xff && input[1] === 0xd8) || // JPEG
|
||||||
|
(input[0] === 0x89 && input[1] === 0x50) || // PNG
|
||||||
|
(input[0] === 0x52 && input[1] === 0x49) // WebP
|
||||||
|
) {
|
||||||
this.options.bufferIn = input;
|
this.options.bufferIn = input;
|
||||||
} else {
|
} else {
|
||||||
throw new Error('Buffer is empty');
|
throw new Error('Buffer contains an unsupported image format. JPEG, PNG and WebP are currently supported.');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// input=stream
|
// input=stream
|
||||||
|
@ -189,23 +189,28 @@ describe('Input/output', function() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('Fail when input is empty Buffer', function(done) {
|
it('Fail when input is empty Buffer', function(done) {
|
||||||
var fail = false;
|
var failed = true;
|
||||||
try {
|
try {
|
||||||
sharp(new Buffer(0));
|
sharp(new Buffer(0));
|
||||||
fail = true;
|
failed = false;
|
||||||
} catch (e) {}
|
} catch (err) {
|
||||||
assert(!fail);
|
assert(err instanceof Error);
|
||||||
|
}
|
||||||
|
assert(failed);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Fail when input is invalid Buffer', function(done) {
|
it('Fail when input is invalid Buffer', function(done) {
|
||||||
sharp(new Buffer([0x1, 0x2, 0x3, 0x4]))
|
var failed = true;
|
||||||
.toBuffer(function (err) {
|
try {
|
||||||
assert.ok(err);
|
sharp(new Buffer([0x1, 0x2, 0x3, 0x4]));
|
||||||
assert.ok(err instanceof Error);
|
failed = false;
|
||||||
|
} catch (err) {
|
||||||
|
assert(err instanceof Error);
|
||||||
|
}
|
||||||
|
assert(failed);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
it('Promises/A+', function(done) {
|
it('Promises/A+', function(done) {
|
||||||
sharp(fixtures.inputJpg).resize(320, 240).toBuffer().then(function(data) {
|
sharp(fixtures.inputJpg).resize(320, 240).toBuffer().then(function(data) {
|
||||||
|
@ -185,11 +185,4 @@ describe('Image metadata', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Report an invalid image as an error', function(done) {
|
|
||||||
sharp(new Buffer([0x1, 0x2, 0x3, 0x4])).metadata(function (err, metadata) {
|
|
||||||
assert.ok(err);
|
|
||||||
assert.ok(err instanceof Error);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user