blob: 5b5a08d0499917726610d3d3b5878ec09a54e216 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2const execBuffer = require('exec-buffer');
3const gifsicle = require('gifsicle');
4const isGif = require('is-gif');
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 (!isGif(buf)) {
14 return Promise.resolve(buf);
15 }
16
17 const args = ['--no-warnings', '--no-app-extensions'];
18
19 if (opts.interlaced) {
20 args.push('--interlace');
21 }
22
23 if (opts.optimizationLevel) {
24 args.push(`--optimize=${opts.optimizationLevel}`);
25 }
26
27 if (opts.colors) {
28 args.push(`--colors=${opts.colors}`);
29 }
30
31 args.push('--output', execBuffer.output, execBuffer.input);
32
33 return execBuffer({
34 input: buf,
35 bin: gifsicle,
36 args
37 }).catch(error => {
38 error.message = error.stderr || error.message;
39 throw error;
40 });
41};