-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
42 lines (33 loc) · 923 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
'use strict';
const execBuffer = require('exec-buffer');
const isPng = require('is-png');
const isJpg = require('is-jpg');
const guetzli = require('guetzli');
module.exports = options => buf => {
options = {...options};
if (!Buffer.isBuffer(buf)) {
return Promise.reject(new TypeError('Expected a buffer'));
}
if (!isPng(buf) && !isJpg(buf)) {
return Promise.resolve(buf);
}
const args = [];
if (Number.isInteger(options.quality) && options.quality >= 0 && options.quality <= 100) {
args.push('--quality', options.quality);
}
if (Number.isInteger(options.memlimit) && options.memlimit > 0) {
args.push('--memlimit', options.memlimit);
}
if (options.nomemlimit) {
args.push('--nomemlimit');
}
args.push(execBuffer.input, execBuffer.output);
return execBuffer({
input: buf,
bin: guetzli,
args
}).catch(error => {
error.message = error.stderr || error.message;
throw error;
});
};