Skip to content

Commit

Permalink
zlib: simplify validators
Browse files Browse the repository at this point in the history
PR-URL: #54442
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
anonrig authored Aug 27, 2024
1 parent 5e6aab0 commit ba07067
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 45 deletions.
43 changes: 43 additions & 0 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
ArrayPrototypeIncludes,
ArrayPrototypeJoin,
ArrayPrototypeMap,
NumberIsFinite,
NumberIsInteger,
NumberIsNaN,
NumberMAX_SAFE_INTEGER,
Expand Down Expand Up @@ -567,6 +568,46 @@ const validateLinkHeaderValue = hideStackFrames((hints) => {
);
});

// 1. Returns false for undefined and NaN
// 2. Returns true for finite numbers
// 3. Throws ERR_INVALID_ARG_TYPE for non-numbers
// 4. Throws ERR_OUT_OF_RANGE for infinite numbers
const validateFiniteNumber = hideStackFrames((number, name) => {
// Common case
if (number === undefined) {
return false;
}

if (NumberIsFinite(number)) {
return true; // Is a valid number
}

if (NumberIsNaN(number)) {
return false;
}

validateNumber(number, name);

// Infinite numbers
throw new ERR_OUT_OF_RANGE(name, 'a finite number', number);
});

// 1. Returns def for number when it's undefined or NaN
// 2. Returns number for finite numbers >= lower and <= upper
// 3. Throws ERR_INVALID_ARG_TYPE for non-numbers
// 4. Throws ERR_OUT_OF_RANGE for infinite numbers or numbers > upper or < lower
const checkRangesOrGetDefault = hideStackFrames(
(number, name, lower, upper, def) => {
if (!validateFiniteNumber(number, name)) {
return def;
}
if (number < lower || number > upper) {
throw new ERR_OUT_OF_RANGE(name, `>= ${lower} and <= ${upper}`, number);
}
return number;
},
);

module.exports = {
isInt32,
isUint32,
Expand Down Expand Up @@ -601,4 +642,6 @@ module.exports = {
validateAbortSignal,
validateLinkHeaderValue,
validateInternalField,
validateFiniteNumber,
checkRangesOrGetDefault,
};
48 changes: 3 additions & 45 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const {
ArrayPrototypePush,
FunctionPrototypeBind,
MathMaxApply,
NumberIsFinite,
NumberIsNaN,
ObjectDefineProperties,
ObjectDefineProperty,
Expand All @@ -51,7 +50,6 @@ const {
ERR_ZLIB_INITIALIZATION_FAILED,
},
genericNodeError,
hideStackFrames,
} = require('internal/errors');
const { Transform, finished } = require('stream');
const {
Expand All @@ -71,9 +69,10 @@ const {
} = require('buffer');
const { owner_symbol } = require('internal/async_hooks').symbols;
const {
checkRangesOrGetDefault,
validateFunction,
validateNumber,
validateUint32,
validateFiniteNumber,
} = require('internal/validators');

const kFlushFlag = Symbol('kFlushFlag');
Expand Down Expand Up @@ -195,47 +194,6 @@ function zlibOnError(message, errno, code) {
self[kError] = error;
}

// 1. Returns false for undefined and NaN
// 2. Returns true for finite numbers
// 3. Throws ERR_INVALID_ARG_TYPE for non-numbers
// 4. Throws ERR_OUT_OF_RANGE for infinite numbers
const checkFiniteNumber = hideStackFrames((number, name) => {
// Common case
if (number === undefined) {
return false;
}

if (NumberIsFinite(number)) {
return true; // Is a valid number
}

if (NumberIsNaN(number)) {
return false;
}

validateNumber.withoutStackTrace(number, name);

// Infinite numbers
throw new ERR_OUT_OF_RANGE.HideStackFramesError(name, 'a finite number', number);
});

// 1. Returns def for number when it's undefined or NaN
// 2. Returns number for finite numbers >= lower and <= upper
// 3. Throws ERR_INVALID_ARG_TYPE for non-numbers
// 4. Throws ERR_OUT_OF_RANGE for infinite numbers or numbers > upper or < lower
const checkRangesOrGetDefault = hideStackFrames(
(number, name, lower, upper, def) => {
if (!checkFiniteNumber.withoutStackTrace(number, name)) {
return def;
}
if (number < lower || number > upper) {
throw new ERR_OUT_OF_RANGE.HideStackFramesError(name,
`>= ${lower} and <= ${upper}`, number);
}
return number;
},
);

const FLUSH_BOUND = [
[ Z_NO_FLUSH, Z_BLOCK ],
[ BROTLI_OPERATION_PROCESS, BROTLI_OPERATION_EMIT_METADATA ],
Expand All @@ -261,7 +219,7 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {

if (opts) {
chunkSize = opts.chunkSize;
if (!checkFiniteNumber(chunkSize, 'options.chunkSize')) {
if (!validateFiniteNumber(chunkSize, 'options.chunkSize')) {
chunkSize = Z_DEFAULT_CHUNK;
} else if (chunkSize < Z_MIN_CHUNK) {
throw new ERR_OUT_OF_RANGE('options.chunkSize',
Expand Down

0 comments on commit ba07067

Please sign in to comment.