diff --git a/lib/child_process.js b/lib/child_process.js index 8c686c074c6bf0..09369316de52f1 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -509,9 +509,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. @@ -538,9 +537,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 21654f66712be0..15593dc3e33678 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -872,13 +872,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, @@ -895,13 +890,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/internal/blocklist.js b/lib/internal/blocklist.js index 641eb37ae2b748..ecd1eec5bb92fc 100644 --- a/lib/internal/blocklist.js +++ b/lib/internal/blocklist.js @@ -28,11 +28,10 @@ const kHandle = Symbol('kHandle'); const { owner_symbol } = internalBinding('symbols'); const { - ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, } = require('internal/errors').codes; -const { validateInt32 } = require('internal/validators'); +const { validateInt32, validateString } = require('internal/validators'); class BlockList extends JSTransferable { constructor() { @@ -56,10 +55,8 @@ class BlockList extends JSTransferable { } 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); @@ -68,12 +65,9 @@ class BlockList extends JSTransferable { } 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); @@ -84,10 +78,8 @@ class BlockList extends JSTransferable { } 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) { @@ -106,10 +98,8 @@ class BlockList extends JSTransferable { } 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/dns/utils.js b/lib/internal/dns/utils.js index cef6032e09579e..f8e4bc38f9e7d1 100644 --- a/lib/internal/dns/utils.js +++ b/lib/internal/dns/utils.js @@ -14,7 +14,10 @@ const { const errors = require('internal/errors'); const { isIP } = require('internal/net'); -const { validateInt32 } = require('internal/validators'); +const { + validateInt32, + validateString, +} = require('internal/validators'); const { ChannelWrap, strerror, @@ -71,9 +74,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) @@ -121,9 +122,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 67ddc0dc129f8e..f5bc4844166342 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -28,7 +28,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'); @@ -487,9 +487,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 f0eec72ae4bd20..37b80216c11ae9 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);