-
Notifications
You must be signed in to change notification settings - Fork 81
/
index.js
77 lines (62 loc) · 1.89 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import {execa} from 'execa';
import isPng from 'is-png';
import pngquant from 'pngquant-bin';
import ow from 'ow';
import {isUint8Array} from 'uint8array-extras';
import {isBrowser} from 'environment';
export default function imageminPngquant(options = {}) {
if (isBrowser) {
throw new Error('This package does not work in the browser.');
}
return async input => {
const isData = isUint8Array(input);
if (!isUint8Array(input)) {
throw new TypeError(`Expected a Uint8Array, got ${typeof input}`);
}
if (isData && !isPng(input)) {
return input;
}
const arguments_ = ['-'];
if (options.speed !== undefined) {
ow(options.speed, ow.number.integer.inRange(1, 11));
arguments_.push('--speed', options.speed.toString());
}
if (options.strip !== undefined) {
ow(options.strip, ow.boolean);
if (options.strip) {
arguments_.push('--strip');
}
}
if (options.quality !== undefined) {
ow(options.quality, ow.array.length(2).ofType(ow.number.inRange(0, 1)));
const [min, max] = options.quality;
arguments_.push('--quality', `${Math.round(min * 100)}-${Math.round(max * 100)}`);
}
if (options.dithering !== undefined) {
ow(options.dithering, ow.any(ow.number.inRange(0, 1), ow.boolean.false));
if (typeof options.dithering === 'number') {
arguments_.push(`--floyd=${options.dithering}`);
} else if (options.dithering === false) {
arguments_.push('--ordered');
}
}
if (options.posterize !== undefined) {
ow(options.posterize, ow.number);
arguments_.push('--posterize', options.posterize.toString());
}
try {
const {stdout} = await execa(pngquant, arguments_, {
encoding: 'buffer',
maxBuffer: Number.POSITIVE_INFINITY,
input,
});
return stdout;
} catch (error) {
// Handling special condition from pngquant binary (status code 99).
if (error.exitCode === 99) {
return input;
}
throw error;
}
};
}