-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
48 lines (39 loc) · 948 Bytes
/
index.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
'use strict';
const execBuffer = require('exec-buffer');
const advpng = require('advpng-bin');
const isPng = require('is-png');
const tempfile = require('tempfile');
module.exports = options => input => {
options = {
optimizationLevel: 3,
...options
};
if (!Buffer.isBuffer(input)) {
return Promise.reject(new TypeError(`Expected a \`Buffer\`, got \`${typeof input}\``));
}
if (!isPng(input)) {
return Promise.resolve(input);
}
const args = [
'--recompress',
'--quiet'
];
const temporary = tempfile();
if (Number.isInteger(options.optimizationLevel)) {
args.push(`-${options.optimizationLevel}`);
}
if (Number.isInteger(options.iterations)) {
args.push('--iter', options.iterations);
}
args.push(execBuffer.input);
return execBuffer({
input,
bin: advpng,
args,
inputPath: temporary,
outputPath: temporary
}).catch(error => {
error.message = error.stderr || error.message;
throw error;
});
};