Skip to content

Commit

Permalink
Require Node.js 10 (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
1000ch authored May 29, 2020
1 parent 03f1abd commit 96e9427
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ os:
- linux
- windows
- osx
osx_image: xcode8.3
osx_image: xcode9.3
language: node_js
node_js:
- '14'
- '12'
- '10'
- '8'
- '6'
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ const execa = require('execa');
const isJpg = require('is-jpg');
const mozjpeg = require('mozjpeg');

module.exports = options => buffer => {
options = Object.assign({
module.exports = options => async buffer => {
options = {
trellis: true,
trellisDC: true,
overshoot: true
}, options);
overshoot: true,
...options
};

if (!Buffer.isBuffer(buffer)) {
return Promise.reject(new TypeError('Expected a buffer'));
Expand All @@ -22,12 +23,15 @@ module.exports = options => buffer => {
if (options.fastcrush) {
return Promise.reject(new Error('Option `fastcrush` was renamed to `fastCrush`'));
}

if (options.maxmemory) {
return Promise.reject(new Error('Option `maxmemory` was renamed to `maxMemory`'));
}

if (options.notrellis) {
return Promise.reject(new Error('Option `notrellis` was renamed to `trellis` and inverted'));
}

if (options.noovershoot) {
return Promise.reject(new Error('Option `noovershoot` was renamed to `overshoot` and inverted'));
}
Expand Down Expand Up @@ -102,9 +106,11 @@ module.exports = options => buffer => {
args.push('-sample', options.sample.join(','));
}

return execa.stdout(mozjpeg, args, {
const {stdout} = await execa(mozjpeg, args, {
encoding: null,
input: buffer,
maxBuffer: Infinity
});

return stdout;
};
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
],
"engines": {
"node": ">=6"
"node": ">=10"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -41,14 +41,13 @@
"optimize"
],
"dependencies": {
"execa": "^1.0.0",
"execa": "^4.0.0",
"is-jpg": "^2.0.0",
"mozjpeg": "^6.0.0"
"mozjpeg": "^7.0.0"
},
"devDependencies": {
"ava": "^1.0.1",
"ava": "^3.8.0",
"is-progressive": "^3.0.0",
"pify": "^4.0.0",
"xo": "^0.23.0"
"xo": "^0.30.0"
}
}
26 changes: 13 additions & 13 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import fs from 'fs';
import path from 'path';
import isJpg from 'is-jpg';
import isProgressive from 'is-progressive';
import pify from 'pify';
import test from 'ava';
import m from '.';
const {promisify} = require('util');
const fs = require('fs');
const path = require('path');
const isJpg = require('is-jpg');
const isProgressive = require('is-progressive');
const test = require('ava');
const m = require('.');

const fsP = pify(fs);
const readFile = promisify(fs.readFile);

test('optimize a JPG', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixture.jpg'));
const buf = await readFile(path.join(__dirname, 'fixture.jpg'));
const data = await m()(buf);

t.true(data.length < buf.length);
Expand All @@ -18,22 +18,22 @@ test('optimize a JPG', async t => {
});

test('support mozjpeg options', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixture.jpg'));
const buf = await readFile(path.join(__dirname, 'fixture.jpg'));
const data = await m({progressive: false})(buf);

t.false(isProgressive.buffer(data));
});

test('skip optimizing a non-JPG file', async t => {
const buf = await fsP.readFile(__filename);
const buf = await readFile(__filename);
const data = await m()(buf);

t.deepEqual(data, buf);
});

test('throw error when a JPG is corrupt', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixture-corrupt.jpg'));
const buf = await readFile(path.join(__dirname, 'fixture-corrupt.jpg'));
await t.throwsAsync(async () => {
await m()(buf);
}, /Corrupt JPEG data/);
}, {message: /Corrupt JPEG data/});
});

0 comments on commit 96e9427

Please sign in to comment.