Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rc 10.0.4 #85

Closed
wants to merge 13 commits into from
5 changes: 5 additions & 0 deletions .c8rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"reporter": ["lcov", "json"],
"reports-dir": "coverage",
"exclude": ["tmp", "coverage", "node_modules", ".github"]
}
34 changes: 19 additions & 15 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@ name: test

on:
push:
branches:
- master
branches: [main, public-package]
pull_request:
branches:
- master
branches: [main, public-package]

jobs:
linux:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- name: Install Prerequisites
run: sudo apt-get -y install libpng-dev libimagequant-dev
- name: Npm Install
run: npm install
- name: Run Test
run: npm test
env:
CI: true

Expand All @@ -32,14 +34,16 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Install Prerequisites
run: brew install libimagequant
- run: npm install
- run: npm test
env:
Expand All @@ -50,12 +54,12 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
yarn.lock
coverage
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
2 changes: 1 addition & 1 deletion .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
package-lock=false
package-lock=true
5 changes: 5 additions & 0 deletions .xo-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"n/prefer-global/buffer": 0
}
}
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface Options {
export interface Options { // eslint-disable-line @typescript-eslint/consistent-type-definitions
/**
Speed `10` has 5% lower quality, but is about 8 times faster than the default. Speed `11` disables dithering and lowers compression level.

Expand Down
140 changes: 70 additions & 70 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,86 @@
'use strict';
const execa = require('execa');
const isPng = require('is-png');
const isStream = require('is-stream');
const pngquant = require('pngquant-bin');
const ow = require('ow');

const imageminPngquant = (options = {}) => input => {
const isBuffer = Buffer.isBuffer(input);
import {execa} from 'execa';
import isPng from 'is-png';
import {isStream} from 'is-stream';
import pngquant from 'pngquant-bin';
import ow from 'ow';

export function imageminPngquant(options = {}) {
return input => {
const isBuffer = Buffer.isBuffer(input);

if (!isBuffer && !isStream(input)) {
return Promise.reject(new TypeError(`Expected a Buffer or Stream, got ${typeof input}`));
}

if (!isBuffer && !isStream(input)) {
return Promise.reject(new TypeError(`Expected a Buffer or Stream, got ${typeof input}`));
}
if (isBuffer && !isPng(input)) {
return Promise.resolve(input);
}

if (isBuffer && !isPng(input)) {
return Promise.resolve(input);
}
const args = ['-'];

const args = ['-'];
if (options.speed !== undefined) {
ow(options.speed, ow.number.integer.inRange(1, 11));
args.push('--speed', options.speed);
}

if (typeof options.speed !== 'undefined') {
ow(options.speed, ow.number.integer.inRange(1, 11));
args.push('--speed', options.speed);
}
if (options.strip !== undefined) {
ow(options.strip, ow.boolean);

if (typeof options.strip !== 'undefined') {
ow(options.strip, ow.boolean);
if (options.strip) {
args.push('--strip');
}
}

if (options.strip) {
args.push('--strip');
if (options.quality !== undefined) {
ow(options.quality, ow.array.length(2).ofType(ow.number.inRange(0, 1)));
const [min, max] = options.quality;
args.push('--quality', `${Math.round(min * 100)}-${Math.round(max * 100)}`);
}
}

if (typeof options.quality !== 'undefined') {
ow(options.quality, ow.array.length(2).ofType(ow.number.inRange(0, 1)));
const [min, max] = options.quality;
args.push('--quality', `${Math.round(min * 100)}-${Math.round(max * 100)}`);
}
if (options.dithering !== undefined) {
ow(options.dithering, ow.any(ow.number.inRange(0, 1), ow.boolean.false));

if (typeof options.dithering !== 'undefined') {
ow(options.dithering, ow.any(ow.number.inRange(0, 1), ow.boolean.false));
if (typeof options.dithering === 'number') {
args.push(`--floyd=${options.dithering}`);
} else if (options.dithering === false) {
args.push('--ordered');
}
}

if (typeof options.dithering === 'number') {
args.push(`--floyd=${options.dithering}`);
} else if (options.dithering === false) {
args.push('--ordered');
if (options.posterize !== undefined) {
ow(options.posterize, ow.number);
args.push('--posterize', options.posterize);
}

if (options.verbose !== undefined) {
ow(options.verbose, ow.boolean);
args.push('--verbose');
}
}

if (typeof options.posterize !== 'undefined') {
ow(options.posterize, ow.number);
args.push('--posterize', options.posterize);
}

if (typeof options.verbose !== 'undefined') {
ow(options.verbose, ow.boolean);
args.push('--verbose');
}

const subprocess = execa(pngquant, args, {
encoding: null,
maxBuffer: Infinity,
input
});

const promise = subprocess
.then(result => result.stdout) // eslint-disable-line promise/prefer-await-to-then
.catch(error => {
// We use `error.exitCode` to check for a special condition when running the pngquant binary.
// See details on handling of "99" code at https://pngquant.org (search for "status code 99").
if (error.exitCode === 99) {
return input;
}

error.message = error.stderr || error.message;
throw error;
const subprocess = execa(pngquant, args, {
encoding: null,
maxBuffer: Number.POSITIVE_INFINITY,
input,
});

subprocess.stdout.then = promise.then.bind(promise); // eslint-disable-line promise/prefer-await-to-then
subprocess.stdout.catch = promise.catch.bind(promise);
const promise = subprocess
.then(result => result.stdout)
.catch(error => {
// We use `error.exitCode` to check for a special condition when running the pngquant binary.
// See details on handling of "99" code at https://pngquant.org (search for "status code 99").
if (error.exitCode === 99) {
return input;
}

error.message = error.stderr || error.message;
throw error;
});

subprocess.stdout.then = promise.then.bind(promise); // eslint-disable-line unicorn/no-thenable
subprocess.stdout.catch = promise.catch.bind(promise);

return subprocess.stdout;
};
return subprocess.stdout;
};
}

module.exports = imageminPngquant;
module.exports.default = imageminPngquant;
export {imageminPngquant as default};
22 changes: 13 additions & 9 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import * as fs from 'fs';
import * as path from 'path';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as url from 'node:url';
import {expectType} from 'tsd';
import imageminPngquant from '.';
import pngquant from './index.js';

const buffer = fs.readFileSync(path.join(__dirname, 'fixture.png'));
const thisDirname = url.fileURLToPath(new URL('.', import.meta.url));
const buffer = fs.readFileSync(path.join(thisDirname, 'fixture.png'));

(async () => {
expectType<Buffer>(await imageminPngquant()(buffer));
expectType<Buffer>(await imageminPngquant({
async function test() {
expectType<Buffer>(await pngquant()(buffer));
expectType<Buffer>(await pngquant({
speed: 10,
quality: [0.8, 1]
quality: [0.8, 1],
})(buffer));
})();
}

await test();
Loading