-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
62 lines (51 loc) · 1.07 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
const execBuffer = require('exec-buffer');
const isPng = require('is-png');
const optipng = require('optipng-bin');
module.exports = options => async buffer => {
options = {
optimizationLevel: 3,
bitDepthReduction: true,
colorTypeReduction: true,
paletteReduction: true,
interlaced: false,
errorRecovery: true,
...options
};
if (!Buffer.isBuffer(buffer)) {
throw new TypeError('Expected a buffer');
}
if (!isPng(buffer)) {
return buffer;
}
const arguments_ = [
'-strip',
'all',
'-clobber',
'-o',
options.optimizationLevel,
'-out',
execBuffer.output
];
if (options.errorRecovery) {
arguments_.push('-fix');
}
if (!options.bitDepthReduction) {
arguments_.push('-nb');
}
if (typeof options.interlaced === 'boolean') {
arguments_.push('-i', options.interlaced ? '1' : '0');
}
if (!options.colorTypeReduction) {
arguments_.push('-nc');
}
if (!options.paletteReduction) {
arguments_.push('-np');
}
arguments_.push(execBuffer.input);
return execBuffer({
input: buffer,
bin: optipng,
args: arguments_
});
};