Skip to content

Commit

Permalink
Fix setting WebP effort to 0 in constructor #3259
Browse files Browse the repository at this point in the history
Setting WebP effort to 0 in the constructor caused the effort to use the
default value of 4.
  • Loading branch information
AlexanderTheGrey committed Jun 14, 2022
1 parent b10d8f8 commit ef7133c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
49 changes: 24 additions & 25 deletions lib/output.js
Expand Up @@ -58,7 +58,7 @@ const bitdepthFromColourCount = (colours) => 1 << 31 - Math.clz32(Math.ceil(Math
* @returns {Promise<Object>} - when no callback is provided
* @throws {Error} Invalid parameters
*/
function toFile (fileOut, callback) {
function toFile(fileOut, callback) {
let err;
if (!is.string(fileOut)) {
err = new Error('Missing output file path');
Expand Down Expand Up @@ -136,7 +136,7 @@ function toFile (fileOut, callback) {
* @param {Function} [callback]
* @returns {Promise<Buffer>} - when no callback is provided
*/
function toBuffer (options, callback) {
function toBuffer(options, callback) {
if (is.object(options)) {
this._setBooleanOption('resolveWithObject', options.resolveWithObject);
} else if (this.options.resolveWithObject) {
Expand Down Expand Up @@ -188,7 +188,7 @@ function toBuffer (options, callback) {
* @returns {Sharp}
* @throws {Error} Invalid parameters
*/
function withMetadata (options) {
function withMetadata(options) {
this.options.withMetadata = is.bool(options) ? options : true;
if (is.object(options)) {
if (is.defined(options.orientation)) {
Expand Down Expand Up @@ -249,7 +249,7 @@ function withMetadata (options) {
* @returns {Sharp}
* @throws {Error} unsupported format or options
*/
function toFormat (format, options) {
function toFormat(format, options) {
const actualFormat = formats.get((is.object(format) && is.string(format.id) ? format.id : format).toLowerCase());
if (!actualFormat) {
throw is.invalidParameterError('format', `one of: ${[...formats.keys()].join(', ')}`, format);
Expand Down Expand Up @@ -292,7 +292,7 @@ function toFormat (format, options) {
* @returns {Sharp}
* @throws {Error} Invalid options
*/
function jpeg (options) {
function jpeg(options) {
if (is.object(options)) {
if (is.defined(options.quality)) {
if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
Expand Down Expand Up @@ -387,7 +387,7 @@ function jpeg (options) {
* @returns {Sharp}
* @throws {Error} Invalid options
*/
function png (options) {
function png(options) {
if (is.object(options)) {
if (is.defined(options.progressive)) {
this._setBooleanOption('pngProgressive', options.progressive);
Expand Down Expand Up @@ -470,7 +470,7 @@ function png (options) {
* @returns {Sharp}
* @throws {Error} Invalid options
*/
function webp (options) {
function webp(options) {
if (is.object(options)) {
if (is.defined(options.quality)) {
if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
Expand All @@ -495,12 +495,11 @@ function webp (options) {
if (is.defined(options.smartSubsample)) {
this._setBooleanOption('webpSmartSubsample', options.smartSubsample);
}
const effort = options.effort || options.reductionEffort;
if (is.defined(effort)) {
if (is.integer(effort) && is.inRange(effort, 0, 6)) {
this.options.webpEffort = effort;
if (is.defined(options.effort)) {
if (is.integer(options.effort) && is.inRange(options.effort, 0, 6)) {
this.options.webpEffort = options.effort;
} else {
throw is.invalidParameterError('effort', 'integer between 0 and 6', effort);
throw is.invalidParameterError('effort', 'integer between 0 and 6', options.effort);
}
}
}
Expand Down Expand Up @@ -544,7 +543,7 @@ function webp (options) {
* @returns {Sharp}
* @throws {Error} Invalid options
*/
function gif (options) {
function gif(options) {
if (is.object(options)) {
const colours = options.colours || options.colors;
if (is.defined(colours)) {
Expand Down Expand Up @@ -607,7 +606,7 @@ function gif (options) {
* @throws {Error} Invalid options
*/
/* istanbul ignore next */
function jp2 (options) {
function jp2(options) {
if (!this.constructor.format.jp2k.output.buffer) {
throw errJp2Save;
}
Expand Down Expand Up @@ -661,7 +660,7 @@ function jp2 (options) {
* @param {Object} [target] - target object for valid options
* @throws {Error} Invalid options
*/
function trySetAnimationOptions (source, target) {
function trySetAnimationOptions(source, target) {
if (is.object(source) && is.defined(source.loop)) {
if (is.integer(source.loop) && is.inRange(source.loop, 0, 65535)) {
target.loop = source.loop;
Expand Down Expand Up @@ -715,7 +714,7 @@ function trySetAnimationOptions (source, target) {
* @returns {Sharp}
* @throws {Error} Invalid options
*/
function tiff (options) {
function tiff(options) {
if (is.object(options)) {
if (is.defined(options.quality)) {
if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
Expand Down Expand Up @@ -814,7 +813,7 @@ function tiff (options) {
* @returns {Sharp}
* @throws {Error} Invalid options
*/
function avif (options) {
function avif(options) {
return this.heif({ ...options, compression: 'av1' });
}

Expand All @@ -835,7 +834,7 @@ function avif (options) {
* @returns {Sharp}
* @throws {Error} Invalid options
*/
function heif (options) {
function heif(options) {
if (is.object(options)) {
if (is.defined(options.quality)) {
if (is.integer(options.quality) && is.inRange(options.quality, 1, 100)) {
Expand Down Expand Up @@ -906,7 +905,7 @@ function heif (options) {
* @param {string} [options.depth='uchar'] - bit depth, one of: char, uchar (default), short, ushort, int, uint, float, complex, double, dpcomplex
* @throws {Error} Invalid options
*/
function raw (options) {
function raw(options) {
if (is.object(options)) {
if (is.defined(options.depth)) {
if (is.string(options.depth) && is.inArray(options.depth,
Expand Down Expand Up @@ -952,7 +951,7 @@ function raw (options) {
* @returns {Sharp}
* @throws {Error} Invalid parameters
*/
function tile (options) {
function tile(options) {
if (is.object(options)) {
// Size of square tiles, in pixels
if (is.defined(options.size)) {
Expand Down Expand Up @@ -1053,7 +1052,7 @@ function tile (options) {
* @param {number} options.seconds - Number of seconds after which processing will be stopped
* @returns {Sharp}
*/
function timeout (options) {
function timeout(options) {
if (!is.plainObject(options)) {
throw is.invalidParameterError('options', 'object', options);
}
Expand All @@ -1074,7 +1073,7 @@ function timeout (options) {
* @param {boolean} [options.force=true] - force output format, otherwise attempt to use input format
* @returns {Sharp}
*/
function _updateFormatOut (formatOut, options) {
function _updateFormatOut(formatOut, options) {
if (!(is.object(options) && options.force === false)) {
this.options.formatOut = formatOut;
}
Expand All @@ -1088,7 +1087,7 @@ function _updateFormatOut (formatOut, options) {
* @param {boolean} val
* @throws {Error} Invalid key
*/
function _setBooleanOption (key, val) {
function _setBooleanOption(key, val) {
if (is.bool(val)) {
this.options[key] = val;
} else {
Expand All @@ -1100,7 +1099,7 @@ function _setBooleanOption (key, val) {
* Called by a WriteableStream to notify us it is ready for data.
* @private
*/
function _read () {
function _read() {
/* istanbul ignore else */
if (!this.options.streamOut) {
this.options.streamOut = true;
Expand All @@ -1113,7 +1112,7 @@ function _read () {
* Supports callback, stream and promise variants
* @private
*/
function _pipeline (callback) {
function _pipeline(callback) {
if (typeof callback === 'function') {
// output=file/buffer
if (this._isStreamInput()) {
Expand Down
6 changes: 6 additions & 0 deletions test/unit/webp.js
Expand Up @@ -133,6 +133,12 @@ describe('WebP', function () {
});
});

it('should set effort to 0', () => {
const effort = sharp().webp({ effort: 0 }).options.webpEffort;

assert.strictEqual(effort, 0)
});

it('invalid loop throws', () => {
assert.throws(() => {
sharp().webp({ loop: -1 });
Expand Down

0 comments on commit ef7133c

Please sign in to comment.