Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: refactor to use validateNumber #36993

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const {
} = require('internal/errors');
const {
validateBuffer,
validateNumber,
validateInteger,
validateString
} = require('internal/validators');
Expand Down Expand Up @@ -345,9 +346,7 @@ ObjectSetPrototypeOf(Buffer, Uint8Array);
// occurs. This is done simply to keep the internal details of the
// implementation from bleeding out to users.
const assertSize = hideStackFrames((size) => {
if (typeof size !== 'number') {
throw new ERR_INVALID_ARG_TYPE('size', 'number', size);
}
validateNumber(size, 'size');
if (!(size >= 0 && size <= kMaxLength)) {
throw new ERR_INVALID_ARG_VALUE.RangeError('size', size);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const {

const {
validateArray,
validateNumber,
validateString
} = require('internal/validators');

Expand Down Expand Up @@ -104,8 +105,8 @@ const getCurves = cachedResult(() => filterDuplicateStrings(_getCurves()));

function setEngine(id, flags) {
validateString(id, 'id');
if (flags && typeof flags !== 'number')
throw new ERR_INVALID_ARG_TYPE('flags', 'number', flags);
if (flags)
validateNumber(flags, 'flags');
flags = flags >>> 0;

// Use provided engine for everything by default
Expand Down Expand Up @@ -244,8 +245,7 @@ function hasAnyNotIn(set, ...check) {

function validateBitLength(length, name, required = false) {
if (length !== undefined || required) {
if (typeof length !== 'number')
throw new ERR_INVALID_ARG_TYPE(name, 'number', length);
validateNumber(length, name);
if (length < 0)
throw new ERR_OUT_OF_RANGE(name, '> 0');
if (length % 8) {
Expand Down
9 changes: 3 additions & 6 deletions lib/internal/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ const {
const { format } = require('util');
const { NumberIsNaN, SafeMap, Symbol } = primordials;

const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
} = require('internal/errors').codes;
const { ERR_INVALID_ARG_VALUE } = require('internal/errors').codes;
const { validateNumber } = require('internal/validators');

const kDestroy = Symbol('kDestroy');
const kHandle = Symbol('kHandle');
Expand Down Expand Up @@ -58,8 +56,7 @@ class Histogram {
}

percentile(percentile) {
if (typeof percentile !== 'number')
throw new ERR_INVALID_ARG_TYPE('percentile', 'number', percentile);
validateNumber(percentile, 'percentile');

if (NumberIsNaN(percentile) || percentile <= 0 || percentile > 100)
throw new ERR_INVALID_ARG_VALUE.RangeError('percentile', percentile);
Expand Down
5 changes: 3 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ const { isUint8Array } = require('internal/util/types');
const {
validateAbortSignal,
validateInt32,
validateNumber,
validatePort,
validateString
} = require('internal/validators');
Expand Down Expand Up @@ -987,8 +988,8 @@ function lookupAndConnect(self, options) {
throw new ERR_INVALID_IP_ADDRESS(localAddress);
}

if (localPort && typeof localPort !== 'number') {
throw new ERR_INVALID_ARG_TYPE('options.localPort', 'number', localPort);
if (localPort) {
validateNumber(localPort, 'options.localPort');
}

if (typeof port !== 'undefined') {
Expand Down