From a29da64b46fe5fcd24d881defa8ce173c86b1a15 Mon Sep 17 00:00:00 2001 From: ZiJian Liu Date: Tue, 19 Jan 2021 19:23:27 +0800 Subject: [PATCH] lib: refactor to use validateNumber PR-URL: https://github.com/nodejs/node/pull/36993 Reviewed-By: Antoine du Hamel Reviewed-By: Luigi Pinca Reviewed-By: Pooja D P Reviewed-By: Darshan Sen Reviewed-By: James M Snell --- lib/buffer.js | 5 ++--- lib/internal/crypto/util.js | 8 ++++---- lib/internal/histogram.js | 9 +++------ lib/net.js | 5 +++-- 4 files changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index 591ddd632383fb..893f5b1915c4d3 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -102,6 +102,7 @@ const { const { validateArray, validateBuffer, + validateNumber, validateInteger, validateString } = require('internal/validators'); @@ -346,9 +347,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); } diff --git a/lib/internal/crypto/util.js b/lib/internal/crypto/util.js index d08249559a81e5..c580ce2571e03e 100644 --- a/lib/internal/crypto/util.js +++ b/lib/internal/crypto/util.js @@ -42,6 +42,7 @@ const { const { validateArray, + validateNumber, validateString } = require('internal/validators'); @@ -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 @@ -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) { diff --git a/lib/internal/histogram.js b/lib/internal/histogram.js index f599e4b3edb5f5..c9c72dc8b3b8b9 100644 --- a/lib/internal/histogram.js +++ b/lib/internal/histogram.js @@ -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'); @@ -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); diff --git a/lib/net.js b/lib/net.js index 911b654e5cbcc2..7bd8d14a35ade5 100644 --- a/lib/net.js +++ b/lib/net.js @@ -111,6 +111,7 @@ const { isUint8Array } = require('internal/util/types'); const { validateAbortSignal, validateInt32, + validateNumber, validatePort, validateString } = require('internal/validators'); @@ -981,8 +982,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') {