|
1 | | -'use strict'; |
2 | | -const execBuffer = require('exec-buffer'); |
3 | | -const isJpg = require('is-jpg'); |
4 | | -const jpegtran = require('jpegtran-bin'); |
5 | | - |
6 | | -module.exports = options => buf => { |
7 | | - options = {...options}; |
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 (options.progressive) { |
20 | | - args.push('-progressive'); |
21 | | - } |
22 | | - |
23 | | - if (options.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 | | -}; |
| 1 | +import fs from 'node:fs/promises'; |
| 2 | +import {randomUUID} from 'node:crypto'; |
| 3 | +import {tmpdir} from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import {execa} from 'execa'; |
| 6 | +import jpegtran from 'jpegtran-bin'; |
| 7 | +import {fileTypeFromBuffer} from 'file-type'; |
| 8 | +import {imageDimensionsFromData} from 'image-dimensions'; |
| 9 | +import {assertUint8Array} from 'uint8array-extras'; |
| 10 | + |
| 11 | +export default function imageminJpegtran(options = {}) { |
| 12 | + return async function (data) { |
| 13 | + assertUint8Array(data); |
| 14 | + |
| 15 | + const fileType = await fileTypeFromBuffer(data); |
| 16 | + if (!fileType || fileType.mime !== 'image/jpeg') { |
| 17 | + return data; |
| 18 | + } |
| 19 | + |
| 20 | + const dimensions = imageDimensionsFromData(data); |
| 21 | + if (dimensions?.width === 0 && dimensions?.height === 0) { |
| 22 | + return data; |
| 23 | + } |
| 24 | + |
| 25 | + const arguments_ = ['-copy', 'none']; |
| 26 | + |
| 27 | + if (options.progressive) { |
| 28 | + arguments_.push('-progressive'); |
| 29 | + } |
| 30 | + |
| 31 | + if (options.arithmetic) { |
| 32 | + arguments_.push('-arithmetic'); |
| 33 | + } else { |
| 34 | + arguments_.push('-optimize'); |
| 35 | + } |
| 36 | + |
| 37 | + const inputPath = path.join(tmpdir(), `input-${randomUUID()}.jpg`); |
| 38 | + const outputPath = path.join(tmpdir(), `output-${randomUUID()}.jpg`); |
| 39 | + |
| 40 | + arguments_.push('-outfile', outputPath, inputPath); |
| 41 | + |
| 42 | + await fs.writeFile(inputPath, data); |
| 43 | + |
| 44 | + try { |
| 45 | + await execa(jpegtran, arguments_); |
| 46 | + return await fs.readFile(outputPath); |
| 47 | + } finally { |
| 48 | + // Clean up temporary files |
| 49 | + await Promise.all([ |
| 50 | + fs.unlink(inputPath).catch(() => {}), |
| 51 | + fs.unlink(outputPath).catch(() => {}), |
| 52 | + ]); |
| 53 | + } |
| 54 | + }; |
| 55 | +} |
0 commit comments