Skip to content

Commit

Permalink
lib: update according to feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Aug 17, 2022
1 parent 3b6d180 commit 4c4c46a
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ const {
const { signals } = internalBinding('constants').os;

/**
* @callback isInt32
* @param {number} value
* @returns {boolean}
*/
Expand All @@ -44,7 +43,6 @@ function isInt32(value) {
}

/**
* @callback isUint32
* @param {number} value
* @returns {boolean}
*/
Expand Down Expand Up @@ -179,7 +177,7 @@ function validateNumber(value, name, min = undefined, max) {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);

if ((min != null && value < min) || (max != null && value > max) ||
((min != null || max != null) && NumberIsNaN(value))) {
((min != null || max != null) && NumberIsNaN(value))) {
throw new ERR_OUT_OF_RANGE(
name,
`${min != null ? `>= ${min}` : ''}${min != null && max != null ? ' && ' : ''}${max != null ? `<= ${max}` : ''}`,
Expand Down Expand Up @@ -230,7 +228,11 @@ function getOwnPropertyValueOrDefault(options, key, defaultValue) {
* @callback validateObject
* @param {*} value
* @param {string} name
* @param {{allowArray: boolean=, allowFunction: boolean=, nullable: boolean=}} [options]
* @param {{
* allowArray?: boolean,
* allowFunction?: boolean,
* nullable?: boolean
* }} [options]
*/

/** @type {validateObject} */
Expand All @@ -240,10 +242,10 @@ const validateObject = hideStackFrames(
const allowFunction = getOwnPropertyValueOrDefault(options, 'allowFunction', false);
const nullable = getOwnPropertyValueOrDefault(options, 'nullable', false);
if ((!nullable && value === null) ||
(!allowArray && ArrayIsArray(value)) ||
(typeof value !== 'object' && (
!allowFunction || typeof value !== 'function'
))) {
(!allowArray && ArrayIsArray(value)) ||
(typeof value !== 'object' && (
!allowFunction || typeof value !== 'function'
))) {
throw new ERR_INVALID_ARG_TYPE(name, 'Object', value);
}
});
Expand All @@ -253,7 +255,7 @@ const validateObject = hideStackFrames(
* @param {*} value
* @param {string} name
* @param {number} [minLength]
* @returns {asserts value is unknown[]}
* @returns {asserts value is any[]}
*/

/** @type {validateArray} */
Expand All @@ -270,24 +272,23 @@ const validateArray = hideStackFrames((value, name, minLength = 0) => {
});

/**
* @callback validateSignalName
* @param {*} signal
* @param {string} name
* @returns {asserts signal is keyof signals}
*/

/** @type {validateSignalName} */
function validateSignalName(signal, name = 'signal') {
validateString(signal, name);

if (signals[signal] === undefined) {
if (signals[StringPrototypeToUpperCase(signal)] !== undefined) {
throw new ERR_UNKNOWN_SIGNAL(signal +
' (signals must use all capital letters)');
' (signals must use all capital letters)');
}

throw new ERR_UNKNOWN_SIGNAL(signal);
}

return true;
}

/**
Expand All @@ -299,29 +300,28 @@ function validateSignalName(signal, name = 'signal') {
/** @type {validateBuffer} */
const validateBuffer = hideStackFrames((buffer, name = 'buffer') => {
if (!isArrayBufferView(buffer)) {
throw new ERR_INVALID_ARG_TYPE(name, ['Buffer', 'TypedArray', 'DataView'], buffer);
throw new ERR_INVALID_ARG_TYPE(name,
['Buffer', 'TypedArray', 'DataView'],
buffer);
}
});

/**
* @callback validateEncoding
* @param {string} data
* @param {string} encoding
*/

/** @type {validateEncoding} */
function validateEncoding(data, encoding) {
const normalizedEncoding = normalizeEncoding(encoding);
const length = data.length;

if (normalizedEncoding === 'hex' && length % 2 !== 0) {
throw new ERR_INVALID_ARG_VALUE('encoding', encoding, `is invalid for data of length ${length}`);
throw new ERR_INVALID_ARG_VALUE('encoding', encoding,
`is invalid for data of length ${length}`);
}
}

/**
* @callback validatePort
* @description Check that the port number is not NaN when coerced to a number,
* Check that the port number is not NaN when coerced to a number,
* is an integer and that it falls within the legal range of port numbers.
* @param {*} port
* @param {string} [name='Port']
Expand All @@ -330,25 +330,27 @@ function validateEncoding(data, encoding) {
*/
function validatePort(port, name = 'Port', allowZero = true) {
if ((typeof port !== 'number' && typeof port !== 'string') ||
(typeof port === 'string' && StringPrototypeTrim(port).length === 0) ||
+port !== (+port >>> 0) ||
port > 0xFFFF ||
(port === 0 && !allowZero)) {
(typeof port === 'string' && StringPrototypeTrim(port).length === 0) ||
+port !== (+port >>> 0) ||
port > 0xFFFF ||
(port === 0 && !allowZero)) {
throw new ERR_SOCKET_BAD_PORT(name, port, allowZero);
}
return port | 0;
}

/**
* @callback validateAbortSignal
* @param {string} signal
* @param {string=} signal
* @param {string} name
*/

/** @type {validateAbortSignal} */
const validateAbortSignal = hideStackFrames((signal, name) => {
if (signal !== undefined &&
(signal === null ||
typeof signal !== 'object' ||
!('aborted' in signal))) {
(signal === null ||
typeof signal !== 'object' ||
!('aborted' in signal))) {
throw new ERR_INVALID_ARG_TYPE(name, 'AbortSignal', signal);
}
});
Expand Down Expand Up @@ -393,14 +395,11 @@ const validateUndefined = hideStackFrames((value, name) => {
});

/**
* @callback validateUnion
* @template T
* @param {T} value
* @param {string} name
* @param {T[]} union
*/

/** @type {validateUnion} */
function validateUnion(value, name, union) {
if (!ArrayPrototypeIncludes(union, value)) {
throw new ERR_INVALID_ARG_TYPE(name, `('${ArrayPrototypeJoin(union, '|')}')`, value);
Expand Down

0 comments on commit 4c4c46a

Please sign in to comment.