Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

imagemin-pngquant

Package Overview
Dependencies
Maintainers
6
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

imagemin-pngquant - npm Package Compare versions

Comparing version 9.0.2 to 10.0.0

13

index.d.ts

@@ -1,2 +0,2 @@

export interface Options {
export interface Options { // eslint-disable-line @typescript-eslint/consistent-type-definitions
/**

@@ -46,15 +46,8 @@ Speed `10` has 5% lower quality, but is about 8 times faster than the default. Speed `11` disables dithering and lowers compression level.

posterize?: number;
/**
Print verbose status messages.
@default false
*/
verbose?: boolean;
}
/**
Buffer or stream to optimize.
Image data to optimize.
*/
export type Plugin = (input: Buffer | NodeJS.ReadableStream) => Promise<Buffer>;
export type Plugin = (input: Uint8Array) => Promise<Uint8Array>;

@@ -61,0 +54,0 @@ /**

@@ -1,71 +0,70 @@

'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');
import {execa} from 'execa';
import isPng from 'is-png';
import pngquant from 'pngquant-bin';
import ow from 'ow';
import {isUint8Array} from 'uint8array-extras';
import {isBrowser} from 'environment';
const imageminPngquant = (options = {}) => input => {
const isBuffer = Buffer.isBuffer(input);
if (!isBuffer && !isStream(input)) {
return Promise.reject(new TypeError(`Expected a Buffer or Stream, got ${typeof input}`));
export default function imageminPngquant(options = {}) {
if (isBrowser) {
throw new Error('This package does not work in the browser.');
}
if (isBuffer && !isPng(input)) {
return Promise.resolve(input);
}
return async input => {
const isData = isUint8Array(input);
const args = ['-'];
if (!isUint8Array(input)) {
throw new TypeError(`Expected a Uint8Array, got ${typeof input}`);
}
if (typeof options.speed !== 'undefined') {
ow(options.speed, ow.number.integer.inRange(1, 11));
args.push('--speed', options.speed);
}
if (isData && !isPng(input)) {
return input;
}
if (typeof options.strip !== 'undefined') {
ow(options.strip, ow.boolean);
const arguments_ = ['-'];
if (options.strip) {
args.push('--strip');
if (options.speed !== undefined) {
ow(options.speed, ow.number.integer.inRange(1, 11));
arguments_.push('--speed', options.speed.toString());
}
}
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.strip !== undefined) {
ow(options.strip, ow.boolean);
if (typeof options.dithering !== 'undefined') {
ow(options.dithering, ow.any(ow.number.inRange(0, 1), ow.boolean.false));
if (options.strip) {
arguments_.push('--strip');
}
}
if (typeof options.dithering === 'number') {
args.push(`--floyd=${options.dithering}`);
} else if (options.dithering === false) {
args.push('--ordered');
if (options.quality !== undefined) {
ow(options.quality, ow.array.length(2).ofType(ow.number.inRange(0, 1)));
const [min, max] = options.quality;
arguments_.push('--quality', `${Math.round(min * 100)}-${Math.round(max * 100)}`);
}
}
if (typeof options.posterize !== 'undefined') {
ow(options.posterize, ow.number);
args.push('--posterize', options.posterize);
}
if (options.dithering !== undefined) {
ow(options.dithering, ow.any(ow.number.inRange(0, 1), ow.boolean.false));
if (typeof options.verbose !== 'undefined') {
ow(options.verbose, ow.boolean);
args.push('--verbose');
}
if (typeof options.dithering === 'number') {
arguments_.push(`--floyd=${options.dithering}`);
} else if (options.dithering === false) {
arguments_.push('--ordered');
}
}
const subprocess = execa(pngquant, args, {
encoding: null,
maxBuffer: Infinity,
input
});
if (options.posterize !== undefined) {
ow(options.posterize, ow.number);
arguments_.push('--posterize', options.posterize.toString());
}
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").
try {
const {stdout} = await execa(pngquant, arguments_, {
encoding: 'buffer',
maxBuffer: Number.POSITIVE_INFINITY,
input,
});
return stdout;
} catch (error) {
// Handling special condition from pngquant binary (status code 99).
if (error.exitCode === 99) {

@@ -75,13 +74,5 @@ return input;

error.message = error.stderr || error.message;
throw error;
});
subprocess.stdout.then = promise.then.bind(promise); // eslint-disable-line promise/prefer-await-to-then
subprocess.stdout.catch = promise.catch.bind(promise);
return subprocess.stdout;
};
module.exports = imageminPngquant;
module.exports.default = imageminPngquant;
}
};
}
{
"name": "imagemin-pngquant",
"version": "9.0.2",
"version": "10.0.0",
"description": "Imagemin plugin for `pngquant`",
"license": "MIT",
"repository": "imagemin/imagemin-pngquant",
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"engines": {
"node": ">=10"
"node": ">=18"
},
"scripts": {
"test": "xo && ava && tsd"
"test": "xo && npm run test:cover && tsc --noEmit index.d.ts",
"test:cover": "c8 --check-coverage --statements 90 ava"
},

@@ -27,15 +33,16 @@ "files": [

"dependencies": {
"execa": "^4.0.0",
"is-png": "^2.0.0",
"is-stream": "^2.0.0",
"ow": "^0.17.0",
"pngquant-bin": "^6.0.0"
"environment": "^1.0.0",
"execa": "^8.0.1",
"is-png": "^3.0.1",
"ow": "^2.0.0",
"pngquant-bin": "^9.0.0",
"uint8array-extras": "^1.1.0"
},
"devDependencies": {
"@types/node": "^12.0.3",
"ava": "^3.8.0",
"get-stream": "^5.1.0",
"tsd": "^0.11.0",
"xo": "^0.30.0"
"@types/node": "^20.12.10",
"ava": "^6.1.3",
"c8": "^9.1.0",
"typescript": "^5.4.5",
"xo": "^0.58.0"
}
}

@@ -1,32 +0,35 @@

# imagemin-pngquant ![GitHub Actions Status](https://github.com/imagemin/imagemin-pngquant/workflows/test/badge.svg?branch=master)
# imagemin-pngquant
> [Imagemin](https://github.com/imagemin/imagemin) plugin for [`pngquant`](https://github.com/kornelski/pngquant)
## Install
```sh
npm install imagemin-pngquant
```
$ npm install imagemin-pngquant
### Prerequisites
> **Linux** machines must have the following packages prior to install: `libpng-dev libimagequant-dev`
```sh
sudo apt-get -y install libpng-dev libimagequant-dev
```
## Usage
```js
const imagemin = require('imagemin');
const imageminPngquant = require('imagemin-pngquant');
import imagemin from 'imagemin';
import imageminPngquant from 'imagemin-pngquant';
(async () => {
await imagemin(['images/*.png'], {
destination: 'build/images',
plugins: [
imageminPngquant()
]
});
await imagemin(['images/*.png'], {
destination: 'build/images',
plugins: [
imageminPngquant()
]
});
console.log('Images optimized');
})();
console.log('Images optimized');
```
## API

@@ -36,3 +39,3 @@

Returns `Promise<Buffer>`.
Returns `Promise<Uint8Array>`.

@@ -45,4 +48,4 @@ #### options

Type: `number`<br>
Default: `4`<br>
Type: `number`\
Default: `4`\
Values: `1` (brute-force) to `11` (fastest)

@@ -54,3 +57,3 @@

Type: `boolean`<br>
Type: `boolean`\
Default: `false`

@@ -62,4 +65,4 @@

Type: `Array<min: number, max: number>`<br>
Values: `Array<0...1, 0...1>`<br>
Type: `Array<min: number, max: number>`\
Values: `Array<0...1, 0...1>`\
Example: `[0.3, 0.5]`

@@ -75,4 +78,4 @@

Type: `number | boolean`<br>
Default: `1` (full)<br>
Type: `number | boolean`\
Default: `1` (full)\
Values: `0...1`

@@ -90,13 +93,6 @@

##### verbose
Type: `boolean`<br>
Default: `false`
Print verbose status messages.
#### input
Type: `Buffer | Stream`
Type: `Uint8Array`
Buffer or stream to optimize.
Image data to optimize.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc