Skip to content

Commit d76400a

Browse files
Lxxyxtargos
authored andcommitted
lib: refactor to use validateString
PR-URL: #37006 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Juan José Arboleda <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a29da64 commit d76400a

File tree

9 files changed

+38
-65
lines changed

9 files changed

+38
-65
lines changed

lib/_tls_common.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,7 @@ exports.createSecureContext = function createSecureContext(options) {
228228
}
229229

230230
if (sigalgs !== undefined) {
231-
if (typeof sigalgs !== 'string')
232-
throw new ERR_INVALID_ARG_TYPE('options.sigalgs', 'string', sigalgs);
231+
validateString(sigalgs, 'options.sigalgs');
233232

234233
if (sigalgs === '')
235234
throw new ERR_INVALID_ARG_VALUE('options.sigalgs', sigalgs);

lib/child_process.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,8 @@ function normalizeSpawnArguments(file, args, options) {
474474
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
475475

476476
// 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');
480479
}
481480

482481
// Validate detached, if present.
@@ -505,9 +504,8 @@ function normalizeSpawnArguments(file, args, options) {
505504
}
506505

507506
// 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');
511509
}
512510

513511
// Validate windowsHide, if present.

lib/dgram.js

+4-14
Original file line numberDiff line numberDiff line change
@@ -856,13 +856,8 @@ Socket.prototype.addSourceSpecificMembership = function(sourceAddress,
856856
interfaceAddress) {
857857
healthCheck(this);
858858

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');
866861

867862
const err =
868863
this[kStateSymbol].handle.addSourceSpecificMembership(sourceAddress,
@@ -879,13 +874,8 @@ Socket.prototype.dropSourceSpecificMembership = function(sourceAddress,
879874
interfaceAddress) {
880875
healthCheck(this);
881876

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');
889879

890880
const err =
891881
this[kStateSymbol].handle.dropSourceSpecificMembership(sourceAddress,

lib/dns.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ function lookup(hostname, options, callback) {
9999
let verbatim = false;
100100

101101
// 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');
104104
}
105105

106106
if (typeof options === 'function') {

lib/internal/blocklist.js

+10-19
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const {
2424
ERR_INVALID_ARG_VALUE,
2525
} = require('internal/errors').codes;
2626

27-
const { validateInt32 } = require('internal/validators');
27+
const { validateInt32, validateString } = require('internal/validators');
2828

2929
class BlockList {
3030
constructor(handle = new BlockListHandle()) {
@@ -52,10 +52,8 @@ class BlockList {
5252
}
5353

5454
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');
5957
family = family.toLowerCase();
6058
if (family !== 'ipv4' && family !== 'ipv6')
6159
throw new ERR_INVALID_ARG_VALUE('family', family);
@@ -64,12 +62,9 @@ class BlockList {
6462
}
6563

6664
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');
7368
family = family.toLowerCase();
7469
if (family !== 'ipv4' && family !== 'ipv6')
7570
throw new ERR_INVALID_ARG_VALUE('family', family);
@@ -80,10 +75,8 @@ class BlockList {
8075
}
8176

8277
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');
8780
family = family.toLowerCase();
8881
let type;
8982
switch (family) {
@@ -102,10 +95,8 @@ class BlockList {
10295
}
10396

10497
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');
109100
family = family.toLowerCase();
110101
if (family !== 'ipv4' && family !== 'ipv6')
111102
throw new ERR_INVALID_ARG_VALUE('family', family);

lib/internal/crypto/pbkdf2.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ const {
1616
const {
1717
validateCallback,
1818
validateInteger,
19+
validateString,
1920
validateUint32,
2021
} = require('internal/validators');
2122

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;
2624

2725
const {
2826
getArrayBufferOrView,
@@ -86,8 +84,7 @@ function pbkdf2Sync(password, salt, iterations, keylen, digest) {
8684
}
8785

8886
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');
9188

9289
password = getArrayBufferOrView(password, 'password');
9390
salt = getArrayBufferOrView(salt, 'salt');

lib/internal/dns/utils.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ const {
1313

1414
const errors = require('internal/errors');
1515
const { isIP } = require('internal/net');
16-
const { validateArray, validateInt32 } = require('internal/validators');
16+
const {
17+
validateArray,
18+
validateInt32,
19+
validateString,
20+
} = require('internal/validators');
1721
const {
1822
ChannelWrap,
1923
strerror,
@@ -68,9 +72,7 @@ class Resolver {
6872
const newSet = [];
6973

7074
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}]`);
7476
let ipVersion = isIP(serv);
7577

7678
if (ipVersion !== 0)
@@ -118,9 +120,7 @@ class Resolver {
118120
}
119121

120122
setLocalAddress(ipv4, ipv6) {
121-
if (typeof ipv4 !== 'string') {
122-
throw new ERR_INVALID_ARG_TYPE('ipv4', 'String', ipv4);
123-
}
123+
validateString(ipv4, 'ipv4');
124124

125125
if (typeof ipv6 !== 'string' && ipv6 !== undefined) {
126126
throw new ERR_INVALID_ARG_TYPE('ipv6', ['String', 'undefined'], ipv6);

lib/internal/event_target.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const {
3030
ERR_INVALID_THIS,
3131
}
3232
} = require('internal/errors');
33-
const { validateObject } = require('internal/validators');
33+
const { validateObject, validateString } = require('internal/validators');
3434

3535
const { customInspectSymbol } = require('internal/util');
3636
const { inspect } = require('util');
@@ -509,9 +509,7 @@ class NodeEventTarget extends EventTarget {
509509
return this;
510510
}
511511
emit(type, arg) {
512-
if (typeof type !== 'string') {
513-
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
514-
}
512+
validateString(type, 'type');
515513
const hadListeners = this.listenerCount(type) > 0;
516514
this[kHybridDispatch](arg, type);
517515
return hadListeners;

lib/internal/process/warning.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010

1111
const assert = require('internal/assert');
1212
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
13+
const { validateString } = require('internal/validators');
1314

1415
// Lazily loaded
1516
let fs;
@@ -120,14 +121,13 @@ function emitWarning(warning, type, code, ctor) {
120121
code = undefined;
121122
type = 'Warning';
122123
}
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');
126126
if (typeof code === 'function') {
127127
ctor = code;
128128
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');
131131
}
132132
if (typeof warning === 'string') {
133133
warning = createWarningObject(warning, type, code, ctor, detail);

0 commit comments

Comments
 (0)