summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZiJian Liu <Lxxyxzj@gmail.com>2021-01-20 22:07:39 +0800
committerJames M Snell <jasnell@gmail.com>2021-01-22 19:56:34 -0800
commit08dd4b1723b20d56fbedf37d52e736fe09715f80 (patch)
treecd0dd6af843997853b6b777d0766fb8f8e3daac2
parente884fd7d3481357b1f36a50da6f62d1abd5a66aa (diff)
downloadnode-new-08dd4b1723b20d56fbedf37d52e736fe09715f80.tar.gz
lib: refactor to use validateString
PR-URL: https://github.com/nodejs/node/pull/37006 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r--lib/_tls_common.js3
-rw-r--r--lib/child_process.js10
-rw-r--r--lib/dgram.js18
-rw-r--r--lib/dns.js4
-rw-r--r--lib/internal/blocklist.js29
-rw-r--r--lib/internal/crypto/pbkdf2.js9
-rw-r--r--lib/internal/dns/utils.js14
-rw-r--r--lib/internal/event_target.js6
-rw-r--r--lib/internal/process/warning.js10
9 files changed, 38 insertions, 65 deletions
diff --git a/lib/_tls_common.js b/lib/_tls_common.js
index 7cda18be31..b8dce1ee15 100644
--- a/lib/_tls_common.js
+++ b/lib/_tls_common.js
@@ -228,8 +228,7 @@ exports.createSecureContext = function createSecureContext(options) {
}
if (sigalgs !== undefined) {
- if (typeof sigalgs !== 'string')
- throw new ERR_INVALID_ARG_TYPE('options.sigalgs', 'string', sigalgs);
+ validateString(sigalgs, 'options.sigalgs');
if (sigalgs === '')
throw new ERR_INVALID_ARG_VALUE('options.sigalgs', sigalgs);
diff --git a/lib/child_process.js b/lib/child_process.js
index daa1d44e89..42d197342b 100644
--- a/lib/child_process.js
+++ b/lib/child_process.js
@@ -474,9 +474,8 @@ function normalizeSpawnArguments(file, args, options) {
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
// Validate the cwd, if present.
- if (options.cwd != null &&
- typeof options.cwd !== 'string') {
- throw new ERR_INVALID_ARG_TYPE('options.cwd', 'string', options.cwd);
+ if (options.cwd != null) {
+ validateString(options.cwd, 'options.cwd');
}
// Validate detached, if present.
@@ -505,9 +504,8 @@ function normalizeSpawnArguments(file, args, options) {
}
// Validate argv0, if present.
- if (options.argv0 != null &&
- typeof options.argv0 !== 'string') {
- throw new ERR_INVALID_ARG_TYPE('options.argv0', 'string', options.argv0);
+ if (options.argv0 != null) {
+ validateString(options.argv0, 'options.argv0');
}
// Validate windowsHide, if present.
diff --git a/lib/dgram.js b/lib/dgram.js
index ea1aba522f..4d14f5db70 100644
--- a/lib/dgram.js
+++ b/lib/dgram.js
@@ -856,13 +856,8 @@ Socket.prototype.addSourceSpecificMembership = function(sourceAddress,
interfaceAddress) {
healthCheck(this);
- if (typeof sourceAddress !== 'string') {
- throw new ERR_INVALID_ARG_TYPE('sourceAddress', 'string', sourceAddress);
- }
-
- if (typeof groupAddress !== 'string') {
- throw new ERR_INVALID_ARG_TYPE('groupAddress', 'string', groupAddress);
- }
+ validateString(sourceAddress, 'sourceAddress');
+ validateString(groupAddress, 'groupAddress');
const err =
this[kStateSymbol].handle.addSourceSpecificMembership(sourceAddress,
@@ -879,13 +874,8 @@ Socket.prototype.dropSourceSpecificMembership = function(sourceAddress,
interfaceAddress) {
healthCheck(this);
- if (typeof sourceAddress !== 'string') {
- throw new ERR_INVALID_ARG_TYPE('sourceAddress', 'string', sourceAddress);
- }
-
- if (typeof groupAddress !== 'string') {
- throw new ERR_INVALID_ARG_TYPE('groupAddress', 'string', groupAddress);
- }
+ validateString(sourceAddress, 'sourceAddress');
+ validateString(groupAddress, 'groupAddress');
const err =
this[kStateSymbol].handle.dropSourceSpecificMembership(sourceAddress,
diff --git a/lib/dns.js b/lib/dns.js
index 1116c73f67..b75ca1ca7d 100644
--- a/lib/dns.js
+++ b/lib/dns.js
@@ -99,8 +99,8 @@ function lookup(hostname, options, callback) {
let verbatim = false;
// Parse arguments
- if (hostname && typeof hostname !== 'string') {
- throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname);
+ if (hostname) {
+ validateString(hostname, 'hostname');
}
if (typeof options === 'function') {
diff --git a/lib/internal/blocklist.js b/lib/internal/blocklist.js
index ba8a9ec450..9a701f208a 100644
--- a/lib/internal/blocklist.js
+++ b/lib/internal/blocklist.js
@@ -24,7 +24,7 @@ const {
ERR_INVALID_ARG_VALUE,
} = require('internal/errors').codes;
-const { validateInt32 } = require('internal/validators');
+const { validateInt32, validateString } = require('internal/validators');
class BlockList {
constructor(handle = new BlockListHandle()) {
@@ -52,10 +52,8 @@ class BlockList {
}
addAddress(address, family = 'ipv4') {
- if (typeof address !== 'string')
- throw new ERR_INVALID_ARG_TYPE('address', 'string', address);
- if (typeof family !== 'string')
- throw new ERR_INVALID_ARG_TYPE('family', 'string', family);
+ validateString(address, 'address');
+ validateString(family, 'family');
family = family.toLowerCase();
if (family !== 'ipv4' && family !== 'ipv6')
throw new ERR_INVALID_ARG_VALUE('family', family);
@@ -64,12 +62,9 @@ class BlockList {
}
addRange(start, end, family = 'ipv4') {
- if (typeof start !== 'string')
- throw new ERR_INVALID_ARG_TYPE('start', 'string', start);
- if (typeof end !== 'string')
- throw new ERR_INVALID_ARG_TYPE('end', 'string', end);
- if (typeof family !== 'string')
- throw new ERR_INVALID_ARG_TYPE('family', 'string', family);
+ validateString(start, 'start');
+ validateString(end, 'end');
+ validateString(family, 'family');
family = family.toLowerCase();
if (family !== 'ipv4' && family !== 'ipv6')
throw new ERR_INVALID_ARG_VALUE('family', family);
@@ -80,10 +75,8 @@ class BlockList {
}
addSubnet(network, prefix, family = 'ipv4') {
- if (typeof network !== 'string')
- throw new ERR_INVALID_ARG_TYPE('network', 'string', network);
- if (typeof family !== 'string')
- throw new ERR_INVALID_ARG_TYPE('family', 'string', family);
+ validateString(network, 'network');
+ validateString(family, 'family');
family = family.toLowerCase();
let type;
switch (family) {
@@ -102,10 +95,8 @@ class BlockList {
}
check(address, family = 'ipv4') {
- if (typeof address !== 'string')
- throw new ERR_INVALID_ARG_TYPE('address', 'string', address);
- if (typeof family !== 'string')
- throw new ERR_INVALID_ARG_TYPE('family', 'string', family);
+ validateString(address, 'address');
+ validateString(family, 'family');
family = family.toLowerCase();
if (family !== 'ipv4' && family !== 'ipv6')
throw new ERR_INVALID_ARG_VALUE('family', family);
diff --git a/lib/internal/crypto/pbkdf2.js b/lib/internal/crypto/pbkdf2.js
index 98f4efb4f3..1017063b07 100644
--- a/lib/internal/crypto/pbkdf2.js
+++ b/lib/internal/crypto/pbkdf2.js
@@ -16,13 +16,11 @@ const {
const {
validateCallback,
validateInteger,
+ validateString,
validateUint32,
} = require('internal/validators');
-const {
- ERR_INVALID_ARG_TYPE,
- ERR_MISSING_OPTION,
-} = require('internal/errors').codes;
+const { ERR_MISSING_OPTION } = require('internal/errors').codes;
const {
getArrayBufferOrView,
@@ -86,8 +84,7 @@ function pbkdf2Sync(password, salt, iterations, keylen, digest) {
}
function check(password, salt, iterations, keylen, digest) {
- if (typeof digest !== 'string')
- throw new ERR_INVALID_ARG_TYPE('digest', 'string', digest);
+ validateString(digest, 'digest');
password = getArrayBufferOrView(password, 'password');
salt = getArrayBufferOrView(salt, 'salt');
diff --git a/lib/internal/dns/utils.js b/lib/internal/dns/utils.js
index 40f5ba0088..f228ef38e2 100644
--- a/lib/internal/dns/utils.js
+++ b/lib/internal/dns/utils.js
@@ -13,7 +13,11 @@ const {
const errors = require('internal/errors');
const { isIP } = require('internal/net');
-const { validateArray, validateInt32 } = require('internal/validators');
+const {
+ validateArray,
+ validateInt32,
+ validateString,
+} = require('internal/validators');
const {
ChannelWrap,
strerror,
@@ -68,9 +72,7 @@ class Resolver {
const newSet = [];
ArrayPrototypeForEach(servers, (serv, index) => {
- if (typeof serv !== 'string') {
- throw new ERR_INVALID_ARG_TYPE(`servers[${index}]`, 'string', serv);
- }
+ validateString(serv, `servers[${index}]`);
let ipVersion = isIP(serv);
if (ipVersion !== 0)
@@ -118,9 +120,7 @@ class Resolver {
}
setLocalAddress(ipv4, ipv6) {
- if (typeof ipv4 !== 'string') {
- throw new ERR_INVALID_ARG_TYPE('ipv4', 'String', ipv4);
- }
+ validateString(ipv4, 'ipv4');
if (typeof ipv6 !== 'string' && ipv6 !== undefined) {
throw new ERR_INVALID_ARG_TYPE('ipv6', ['String', 'undefined'], ipv6);
diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js
index 9616030d9d..fe588088cc 100644
--- a/lib/internal/event_target.js
+++ b/lib/internal/event_target.js
@@ -30,7 +30,7 @@ const {
ERR_INVALID_THIS,
}
} = require('internal/errors');
-const { validateObject } = require('internal/validators');
+const { validateObject, validateString } = require('internal/validators');
const { customInspectSymbol } = require('internal/util');
const { inspect } = require('util');
@@ -509,9 +509,7 @@ class NodeEventTarget extends EventTarget {
return this;
}
emit(type, arg) {
- if (typeof type !== 'string') {
- throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
- }
+ validateString(type, 'type');
const hadListeners = this.listenerCount(type) > 0;
this[kHybridDispatch](arg, type);
return hadListeners;
diff --git a/lib/internal/process/warning.js b/lib/internal/process/warning.js
index eafe8d2203..59fe84a19a 100644
--- a/lib/internal/process/warning.js
+++ b/lib/internal/process/warning.js
@@ -10,6 +10,7 @@ const {
const assert = require('internal/assert');
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
+const { validateString } = require('internal/validators');
// Lazily loaded
let fs;
@@ -120,14 +121,13 @@ function emitWarning(warning, type, code, ctor) {
code = undefined;
type = 'Warning';
}
- if (type !== undefined && typeof type !== 'string') {
- throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
- }
+ if (type !== undefined)
+ validateString(type, 'type');
if (typeof code === 'function') {
ctor = code;
code = undefined;
- } else if (code !== undefined && typeof code !== 'string') {
- throw new ERR_INVALID_ARG_TYPE('code', 'string', code);
+ } else if (code !== undefined) {
+ validateString(code, 'code');
}
if (typeof warning === 'string') {
warning = createWarningObject(warning, type, code, ctor, detail);