| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | const execBuffer = require('exec-buffer'); |
| 3 | const gifsicle = require('gifsicle'); |
| 4 | const isGif = require('is-gif'); |
| 5 | |
| 6 | module.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 | }; |