1 | 'use strict';
|
2 | const execa = require('execa');
|
3 | const isJpg = require('is-jpg');
|
4 | const mozjpeg = require('mozjpeg');
|
5 |
|
6 | module.exports = options => buffer => {
|
7 | options = Object.assign({
|
8 | trellis: true,
|
9 | trellisDC: true,
|
10 | overshoot: true
|
11 | }, options);
|
12 |
|
13 | if (!Buffer.isBuffer(buffer)) {
|
14 | return Promise.reject(new TypeError('Expected a buffer'));
|
15 | }
|
16 |
|
17 | if (!isJpg(buffer)) {
|
18 | return Promise.resolve(buffer);
|
19 | }
|
20 |
|
21 |
|
22 | if (options.fastcrush) {
|
23 | return Promise.reject(new Error('Option `fastcrush` was renamed to `fastCrush`'));
|
24 | }
|
25 | if (options.maxmemory) {
|
26 | return Promise.reject(new Error('Option `maxmemory` was renamed to `maxMemory`'));
|
27 | }
|
28 | if (options.notrellis) {
|
29 | return Promise.reject(new Error('Option `notrellis` was renamed to `trellis` and inverted'));
|
30 | }
|
31 | if (options.noovershoot) {
|
32 | return Promise.reject(new Error('Option `noovershoot` was renamed to `overshoot` and inverted'));
|
33 | }
|
34 |
|
35 | const args = [];
|
36 |
|
37 | if (typeof options.quality !== 'undefined') {
|
38 | args.push('-quality', options.quality);
|
39 | }
|
40 |
|
41 | if (options.progressive === false) {
|
42 | args.push('-baseline');
|
43 | }
|
44 |
|
45 | if (options.targa) {
|
46 | args.push('-targa');
|
47 | }
|
48 |
|
49 | if (options.revert) {
|
50 | args.push('-revert');
|
51 | }
|
52 |
|
53 | if (options.fastCrush) {
|
54 | args.push('-fastcrush');
|
55 | }
|
56 |
|
57 | if (typeof options.dcScanOpt !== 'undefined') {
|
58 | args.push('-dc-scan-opt', options.dcScanOpt);
|
59 | }
|
60 |
|
61 | if (!options.trellis) {
|
62 | args.push('-notrellis');
|
63 | }
|
64 |
|
65 | if (!options.trellisDC) {
|
66 | args.push('-notrellis-dc');
|
67 | }
|
68 |
|
69 | if (options.tune) {
|
70 | args.push(`-tune-${options.tune}`);
|
71 | }
|
72 |
|
73 | if (!options.overshoot) {
|
74 | args.push('-noovershoot');
|
75 | }
|
76 |
|
77 | if (options.arithmetic) {
|
78 | args.push('-arithmetic');
|
79 | }
|
80 |
|
81 | if (options.dct) {
|
82 | args.push('-dct', options.dct);
|
83 | }
|
84 |
|
85 | if (options.quantBaseline) {
|
86 | args.push('-quant-baseline', options.quantBaseline);
|
87 | }
|
88 |
|
89 | if (typeof options.quantTable !== 'undefined') {
|
90 | args.push('-quant-table', options.quantTable);
|
91 | }
|
92 |
|
93 | if (options.smooth) {
|
94 | args.push('-smooth', options.smooth);
|
95 | }
|
96 |
|
97 | if (options.maxMemory) {
|
98 | args.push('-maxmemory', options.maxMemory);
|
99 | }
|
100 |
|
101 | if (options.sample) {
|
102 | args.push('-sample', options.sample.join(','));
|
103 | }
|
104 |
|
105 | return execa.stdout(mozjpeg, args, {
|
106 | encoding: null,
|
107 | input: buffer,
|
108 | maxBuffer: Infinity
|
109 | });
|
110 | };
|