-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ca38bd
commit 981b216
Showing
8 changed files
with
85 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,77 @@ | ||
import {execa} from 'execa'; | ||
import isPng from 'is-png'; | ||
import {isStream} from 'is-stream'; | ||
import pngquant from 'pngquant-bin'; | ||
import ow from 'ow'; | ||
import {isUint8Array} from 'uint8array-extras'; | ||
import {isBrowser} from 'environment'; | ||
|
||
export function imageminPngquant(options = {}) { | ||
return input => { | ||
const isBuffer = Buffer.isBuffer(input); | ||
export default function imageminPngquant(options = {}) { | ||
if (isBrowser) { | ||
throw new Error('This package does not work in the browser.'); | ||
} | ||
|
||
if (!isBuffer && !isStream(input)) { | ||
return Promise.reject(new TypeError(`Expected a Buffer or Stream, got ${typeof input}`)); | ||
return async input => { | ||
const isData = isUint8Array(input); | ||
|
||
if (!isUint8Array(input)) { | ||
throw new TypeError(`Expected a Uint8Array, got ${typeof input}`); | ||
} | ||
|
||
if (isBuffer && !isPng(input)) { | ||
return Promise.resolve(input); | ||
if (isData && !isPng(input)) { | ||
return input; | ||
} | ||
|
||
const args = ['-']; | ||
const arguments_ = ['-']; | ||
|
||
if (options.speed !== undefined) { | ||
ow(options.speed, ow.number.integer.inRange(1, 11)); | ||
args.push('--speed', options.speed); | ||
arguments_.push('--speed', options.speed.toString()); | ||
} | ||
|
||
if (options.strip !== undefined) { | ||
ow(options.strip, ow.boolean); | ||
|
||
if (options.strip) { | ||
args.push('--strip'); | ||
arguments_.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)}`); | ||
arguments_.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 === 'number') { | ||
args.push(`--floyd=${options.dithering}`); | ||
arguments_.push(`--floyd=${options.dithering}`); | ||
} else if (options.dithering === false) { | ||
args.push('--ordered'); | ||
arguments_.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'); | ||
arguments_.push('--posterize', options.posterize.toString()); | ||
} | ||
|
||
const subprocess = execa(pngquant, args, { | ||
encoding: null, | ||
maxBuffer: Number.POSITIVE_INFINITY, | ||
input, | ||
}); | ||
|
||
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; | ||
try { | ||
const {stdout} = await execa(pngquant, arguments_, { | ||
encoding: 'buffer', | ||
maxBuffer: Number.POSITIVE_INFINITY, | ||
input, | ||
}); | ||
|
||
subprocess.stdout.then = promise.then.bind(promise); // eslint-disable-line unicorn/no-thenable | ||
subprocess.stdout.catch = promise.catch.bind(promise); | ||
return stdout; | ||
} catch (error) { | ||
// Handling special condition from pngquant binary (status code 99). | ||
if (error.exitCode === 99) { | ||
return input; | ||
} | ||
|
||
return subprocess.stdout; | ||
throw error; | ||
} | ||
}; | ||
} | ||
|
||
export {imageminPngquant as default}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.