-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
55 lines (43 loc) · 1.36 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
'use strict';
const execa = require('execa');
const isPng = require('is-png');
const isStream = require('is-stream');
const oxipng = require('./oxipng-bin');
const ow = require('ow');
const imageminOxipng = (options = {}) => input => {
const isBuffer = Buffer.isBuffer(input);
if (!isBuffer && !isStream(input)) {
return Promise.reject(new TypeError(`Expected a Buffer or Stream, got ${typeof input}`));
}
if (isBuffer && !isPng(input)) {
return Promise.resolve(input);
}
const args = ['-'];
if (typeof options.strip !== 'undefined') {
ow(options.strip, ow.string);
args.push('--strip', options.strip);
}
if (typeof options.quality !== 'undefined') {
ow(options.quality, ow.number.integer.inRange(1, 6));
args.push('-o', options.quality);
}
const subprocess = execa(oxipng, args, {
encoding: null,
maxBuffer: Infinity,
input
});
const promise = subprocess
.then(result => result.stdout) // eslint-disable-line promise/prefer-await-to-then
.catch(error => {
if (error.code === 99) {
return input;
}
error.message = error.stderr || error.message;
throw error;
});
subprocess.stdout.then = promise.then.bind(promise); // eslint-disable-line promise/prefer-await-to-then
subprocess.stdout.catch = promise.catch.bind(promise);
return subprocess.stdout;
};
module.exports = imageminOxipng;
module.exports.default = imageminOxipng;