-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
failOn.js
102 lines (92 loc) · 3.31 KB
/
failOn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'use strict';
const assert = require('assert');
const fs = require('fs');
const sharp = require('../../lib');
const fixtures = require('../fixtures');
describe('failOn', () => {
it('handles truncated JPEG', function (done) {
sharp(fixtures.inputJpgTruncated, { failOn: 'none' })
.resize(32, 24)
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual('jpeg', info.format);
assert.strictEqual(32, info.width);
assert.strictEqual(24, info.height);
fixtures.assertSimilar(fixtures.expected('truncated.jpg'), data, done);
});
});
it('handles truncated PNG, emits warnings', function (done) {
let isWarningEmitted = false;
sharp(fixtures.inputPngTruncated, { failOn: 'none' })
.on('warning', function (warning) {
assert.ok(warning.includes('not enough data') || warning.includes('end of stream'));
isWarningEmitted = true;
})
.resize(32, 24)
.toBuffer(function (err, data, info) {
if (err) throw err;
assert.strictEqual(true, isWarningEmitted);
assert.strictEqual('png', info.format);
assert.strictEqual(32, info.width);
assert.strictEqual(24, info.height);
done();
});
});
it('throws for invalid options', () => {
assert.throws(
() => sharp({ failOn: 'zoinks' }),
/Expected one of: none, truncated, error, warning for failOn but received zoinks of type string/
);
assert.throws(
() => sharp({ failOn: 1 }),
/Expected one of: none, truncated, error, warning for failOn but received 1 of type number/
);
});
it('deprecated failOnError', () => {
assert.doesNotThrow(
() => sharp({ failOnError: true })
);
assert.doesNotThrow(
() => sharp({ failOnError: false })
);
assert.throws(
() => sharp({ failOnError: 'zoinks' }),
/Expected boolean for failOnError but received zoinks of type string/
);
assert.throws(
() => sharp({ failOnError: 1 }),
/Expected boolean for failOnError but received 1 of type number/
);
});
it('returns errors to callback for truncated JPEG', function (done) {
sharp(fixtures.inputJpgTruncated, { failOn: 'truncated' }).toBuffer(function (err, data, info) {
assert.ok(err.message.includes('VipsJpeg: Premature end of'), err);
assert.strictEqual(data, undefined);
assert.strictEqual(info, undefined);
done();
});
});
it('returns errors to callback for truncated PNG', function (done) {
sharp(fixtures.inputPngTruncated, { failOn: 'truncated' }).toBuffer(function (err, data, info) {
assert.ok(err.message.includes('read error'), err);
assert.strictEqual(data, undefined);
assert.strictEqual(info, undefined);
done();
});
});
it('rejects promises for truncated JPEG', function (done) {
sharp(fixtures.inputJpgTruncated, { failOn: 'error' })
.toBuffer()
.then(() => {
throw new Error('Expected rejection');
})
.catch(err => {
done(err.message.includes('VipsJpeg: Premature end of') ? undefined : err);
});
});
it('handles stream-based input', async () => {
const writable = sharp({ failOn: 'none' }).resize(32, 24);
fs.createReadStream(fixtures.inputJpgTruncated).pipe(writable);
return writable.toBuffer();
});
});