blob: 2afc284386cf034d261a50383db493bf0a83fd95 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2const execBuffer = require('exec-buffer');
3const isJpg = require('is-jpg');
4const jpegtran = require('jpegtran-bin');
5
6module.exports = opts => buf => {
7 opts = Object.assign({}, opts);
8
9 if (!Buffer.isBuffer(buf)) {
10 return Promise.reject(new TypeError('Expected a buffer'));
11 }
12
13 if (!isJpg(buf)) {
14 return Promise.resolve(buf);
15 }
16
17 const args = ['-copy', 'none'];
18
19 if (opts.progressive) {
20 args.push('-progressive');
21 }
22
23 if (opts.arithmetic) {
24 args.push('-arithmetic');
25 } else {
26 args.push('-optimize');
27 }
28
29 args.push('-outfile', execBuffer.output, execBuffer.input);
30
31 return execBuffer({
32 input: buf,
33 bin: jpegtran,
34 args
35 }).catch(error => {
36 error.message = error.stderr || error.message;
37 throw error;
38 });
39};