GeezerVison is showing github.com — lib: refactor to use validateString · nodejs/node@d76400a
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/_tls_common.js +1-2Lines changed: 1 addition & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -228,8 +228,7 @@ exports.createSecureContext = function createSecureContext(options) { | 228 | 228 | } | 229 | 229 |
| 230 | 230 | if (sigalgs !== undefined) { | 231 | | - if (typeof sigalgs !== 'string') | 232 | | - throw new ERR_INVALID_ARG_TYPE('options.sigalgs', 'string', sigalgs); | | 231 | + validateString(sigalgs, 'options.sigalgs'); | 233 | 232 |
| 234 | 233 | if (sigalgs === '') | 235 | 234 | throw new ERR_INVALID_ARG_VALUE('options.sigalgs', sigalgs); | |
Collapse file Copy file name to clipboardExpand all lines: lib/child_process.js +4-6Lines changed: 4 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -474,9 +474,8 @@ function normalizeSpawnArguments(file, args, options) { | 474 | 474 | throw new ERR_INVALID_ARG_TYPE('options', 'object', options); | 475 | 475 |
| 476 | 476 | // Validate the cwd, if present. | 477 | | - if (options.cwd != null && | 478 | | - typeof options.cwd !== 'string') { | 479 | | - throw new ERR_INVALID_ARG_TYPE('options.cwd', 'string', options.cwd); | | 477 | + if (options.cwd != null) { | | 478 | + validateString(options.cwd, 'options.cwd'); | 480 | 479 | } | 481 | 480 |
| 482 | 481 | // Validate detached, if present. | @@ -505,9 +504,8 @@ function normalizeSpawnArguments(file, args, options) { | 505 | 504 | } | 506 | 505 |
| 507 | 506 | // Validate argv0, if present. | 508 | | - if (options.argv0 != null && | 509 | | - typeof options.argv0 !== 'string') { | 510 | | - throw new ERR_INVALID_ARG_TYPE('options.argv0', 'string', options.argv0); | | 507 | + if (options.argv0 != null) { | | 508 | + validateString(options.argv0, 'options.argv0'); | 511 | 509 | } | 512 | 510 |
| 513 | 511 | // Validate windowsHide, if present. | |
Collapse file Copy file name to clipboardExpand all lines: lib/dgram.js +4-14Lines changed: 4 additions & 14 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -856,13 +856,8 @@ Socket.prototype.addSourceSpecificMembership = function(sourceAddress, | 856 | 856 | interfaceAddress) { | 857 | 857 | healthCheck(this); | 858 | 858 |
| 859 | | - if (typeof sourceAddress !== 'string') { | 860 | | - throw new ERR_INVALID_ARG_TYPE('sourceAddress', 'string', sourceAddress); | 861 | | - } | 862 | | - | 863 | | - if (typeof groupAddress !== 'string') { | 864 | | - throw new ERR_INVALID_ARG_TYPE('groupAddress', 'string', groupAddress); | 865 | | - } | | 859 | + validateString(sourceAddress, 'sourceAddress'); | | 860 | + validateString(groupAddress, 'groupAddress'); | 866 | 861 |
| 867 | 862 | const err = | 868 | 863 | this[kStateSymbol].handle.addSourceSpecificMembership(sourceAddress, | @@ -879,13 +874,8 @@ Socket.prototype.dropSourceSpecificMembership = function(sourceAddress, | 879 | 874 | interfaceAddress) { | 880 | 875 | healthCheck(this); | 881 | 876 |
| 882 | | - if (typeof sourceAddress !== 'string') { | 883 | | - throw new ERR_INVALID_ARG_TYPE('sourceAddress', 'string', sourceAddress); | 884 | | - } | 885 | | - | 886 | | - if (typeof groupAddress !== 'string') { | 887 | | - throw new ERR_INVALID_ARG_TYPE('groupAddress', 'string', groupAddress); | 888 | | - } | | 877 | + validateString(sourceAddress, 'sourceAddress'); | | 878 | + validateString(groupAddress, 'groupAddress'); | 889 | 879 |
| 890 | 880 | const err = | 891 | 881 | this[kStateSymbol].handle.dropSourceSpecificMembership(sourceAddress, | |
Collapse file Copy file name to clipboardExpand all lines: lib/dns.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -99,8 +99,8 @@ function lookup(hostname, options, callback) { | 99 | 99 | let verbatim = false; | 100 | 100 |
| 101 | 101 | // Parse arguments | 102 | | - if (hostname && typeof hostname !== 'string') { | 103 | | - throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname); | | 102 | + if (hostname) { | | 103 | + validateString(hostname, 'hostname'); | 104 | 104 | } | 105 | 105 |
| 106 | 106 | if (typeof options === 'function') { | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/blocklist.js +10-19Lines changed: 10 additions & 19 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,7 +24,7 @@ const { | 24 | 24 | ERR_INVALID_ARG_VALUE, | 25 | 25 | } = require('internal/errors').codes; | 26 | 26 |
| 27 | | -const { validateInt32 } = require('internal/validators'); | | 27 | +const { validateInt32, validateString } = require('internal/validators'); | 28 | 28 |
| 29 | 29 | class BlockList { | 30 | 30 | constructor(handle = new BlockListHandle()) { | @@ -52,10 +52,8 @@ class BlockList { | 52 | 52 | } | 53 | 53 |
| 54 | 54 | addAddress(address, family = 'ipv4') { | 55 | | - if (typeof address !== 'string') | 56 | | - throw new ERR_INVALID_ARG_TYPE('address', 'string', address); | 57 | | - if (typeof family !== 'string') | 58 | | - throw new ERR_INVALID_ARG_TYPE('family', 'string', family); | | 55 | + validateString(address, 'address'); | | 56 | + validateString(family, 'family'); | 59 | 57 | family = family.toLowerCase(); | 60 | 58 | if (family !== 'ipv4' && family !== 'ipv6') | 61 | 59 | throw new ERR_INVALID_ARG_VALUE('family', family); | @@ -64,12 +62,9 @@ class BlockList { | 64 | 62 | } | 65 | 63 |
| 66 | 64 | addRange(start, end, family = 'ipv4') { | 67 | | - if (typeof start !== 'string') | 68 | | - throw new ERR_INVALID_ARG_TYPE('start', 'string', start); | 69 | | - if (typeof end !== 'string') | 70 | | - throw new ERR_INVALID_ARG_TYPE('end', 'string', end); | 71 | | - if (typeof family !== 'string') | 72 | | - throw new ERR_INVALID_ARG_TYPE('family', 'string', family); | | 65 | + validateString(start, 'start'); | | 66 | + validateString(end, 'end'); | | 67 | + validateString(family, 'family'); | 73 | 68 | family = family.toLowerCase(); | 74 | 69 | if (family !== 'ipv4' && family !== 'ipv6') | 75 | 70 | throw new ERR_INVALID_ARG_VALUE('family', family); | @@ -80,10 +75,8 @@ class BlockList { | 80 | 75 | } | 81 | 76 |
| 82 | 77 | addSubnet(network, prefix, family = 'ipv4') { | 83 | | - if (typeof network !== 'string') | 84 | | - throw new ERR_INVALID_ARG_TYPE('network', 'string', network); | 85 | | - if (typeof family !== 'string') | 86 | | - throw new ERR_INVALID_ARG_TYPE('family', 'string', family); | | 78 | + validateString(network, 'network'); | | 79 | + validateString(family, 'family'); | 87 | 80 | family = family.toLowerCase(); | 88 | 81 | let type; | 89 | 82 | switch (family) { | @@ -102,10 +95,8 @@ class BlockList { | 102 | 95 | } | 103 | 96 |
| 104 | 97 | check(address, family = 'ipv4') { | 105 | | - if (typeof address !== 'string') | 106 | | - throw new ERR_INVALID_ARG_TYPE('address', 'string', address); | 107 | | - if (typeof family !== 'string') | 108 | | - throw new ERR_INVALID_ARG_TYPE('family', 'string', family); | | 98 | + validateString(address, 'address'); | | 99 | + validateString(family, 'family'); | 109 | 100 | family = family.toLowerCase(); | 110 | 101 | if (family !== 'ipv4' && family !== 'ipv6') | 111 | 102 | throw new ERR_INVALID_ARG_VALUE('family', family); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/pbkdf2.js +3-6Lines changed: 3 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,13 +16,11 @@ const { | 16 | 16 | const { | 17 | 17 | validateCallback, | 18 | 18 | validateInteger, | | 19 | + validateString, | 19 | 20 | validateUint32, | 20 | 21 | } = require('internal/validators'); | 21 | 22 |
| 22 | | -const { | 23 | | - ERR_INVALID_ARG_TYPE, | 24 | | - ERR_MISSING_OPTION, | 25 | | -} = require('internal/errors').codes; | | 23 | +const { ERR_MISSING_OPTION } = require('internal/errors').codes; | 26 | 24 |
| 27 | 25 | const { | 28 | 26 | getArrayBufferOrView, | @@ -86,8 +84,7 @@ function pbkdf2Sync(password, salt, iterations, keylen, digest) { | 86 | 84 | } | 87 | 85 |
| 88 | 86 | function check(password, salt, iterations, keylen, digest) { | 89 | | - if (typeof digest !== 'string') | 90 | | - throw new ERR_INVALID_ARG_TYPE('digest', 'string', digest); | | 87 | + validateString(digest, 'digest'); | 91 | 88 |
| 92 | 89 | password = getArrayBufferOrView(password, 'password'); | 93 | 90 | salt = getArrayBufferOrView(salt, 'salt'); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/dns/utils.js +7-7Lines changed: 7 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,7 +13,11 @@ const { | 13 | 13 |
| 14 | 14 | const errors = require('internal/errors'); | 15 | 15 | const { isIP } = require('internal/net'); | 16 | | -const { validateArray, validateInt32 } = require('internal/validators'); | | 16 | +const { | | 17 | + validateArray, | | 18 | + validateInt32, | | 19 | + validateString, | | 20 | +} = require('internal/validators'); | 17 | 21 | const { | 18 | 22 | ChannelWrap, | 19 | 23 | strerror, | @@ -68,9 +72,7 @@ class Resolver { | 68 | 72 | const newSet = []; | 69 | 73 |
| 70 | 74 | ArrayPrototypeForEach(servers, (serv, index) => { | 71 | | - if (typeof serv !== 'string') { | 72 | | - throw new ERR_INVALID_ARG_TYPE(`servers[${index}]`, 'string', serv); | 73 | | - } | | 75 | + validateString(serv, `servers[${index}]`); | 74 | 76 | let ipVersion = isIP(serv); | 75 | 77 |
| 76 | 78 | if (ipVersion !== 0) | @@ -118,9 +120,7 @@ class Resolver { | 118 | 120 | } | 119 | 121 |
| 120 | 122 | setLocalAddress(ipv4, ipv6) { | 121 | | - if (typeof ipv4 !== 'string') { | 122 | | - throw new ERR_INVALID_ARG_TYPE('ipv4', 'String', ipv4); | 123 | | - } | | 123 | + validateString(ipv4, 'ipv4'); | 124 | 124 |
| 125 | 125 | if (typeof ipv6 !== 'string' && ipv6 !== undefined) { | 126 | 126 | throw new ERR_INVALID_ARG_TYPE('ipv6', ['String', 'undefined'], ipv6); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/event_target.js +2-4Lines changed: 2 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,7 +30,7 @@ const { | 30 | 30 | ERR_INVALID_THIS, | 31 | 31 | } | 32 | 32 | } = require('internal/errors'); | 33 | | -const { validateObject } = require('internal/validators'); | | 33 | +const { validateObject, validateString } = require('internal/validators'); | 34 | 34 |
| 35 | 35 | const { customInspectSymbol } = require('internal/util'); | 36 | 36 | const { inspect } = require('util'); | @@ -509,9 +509,7 @@ class NodeEventTarget extends EventTarget { | 509 | 509 | return this; | 510 | 510 | } | 511 | 511 | emit(type, arg) { | 512 | | - if (typeof type !== 'string') { | 513 | | - throw new ERR_INVALID_ARG_TYPE('type', 'string', type); | 514 | | - } | | 512 | + validateString(type, 'type'); | 515 | 513 | const hadListeners = this.listenerCount(type) > 0; | 516 | 514 | this[kHybridDispatch](arg, type); | 517 | 515 | return hadListeners; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process/warning.js +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,6 +10,7 @@ const { | 10 | 10 |
| 11 | 11 | const assert = require('internal/assert'); | 12 | 12 | const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes; | | 13 | +const { validateString } = require('internal/validators'); | 13 | 14 |
| 14 | 15 | // Lazily loaded | 15 | 16 | let fs; | @@ -120,14 +121,13 @@ function emitWarning(warning, type, code, ctor) { | 120 | 121 | code = undefined; | 121 | 122 | type = 'Warning'; | 122 | 123 | } | 123 | | - if (type !== undefined && typeof type !== 'string') { | 124 | | - throw new ERR_INVALID_ARG_TYPE('type', 'string', type); | 125 | | - } | | 124 | + if (type !== undefined) | | 125 | + validateString(type, 'type'); | 126 | 126 | if (typeof code === 'function') { | 127 | 127 | ctor = code; | 128 | 128 | code = undefined; | 129 | | - } else if (code !== undefined && typeof code !== 'string') { | 130 | | - throw new ERR_INVALID_ARG_TYPE('code', 'string', code); | | 129 | + } else if (code !== undefined) { | | 130 | + validateString(code, 'code'); | 131 | 131 | } | 132 | 132 | if (typeof warning === 'string') { | 133 | 133 | warning = createWarningObject(warning, type, code, ctor, detail); | |
|