Skip to content

Commit 4d0ffa7

Browse files
Lxxyxfoxxyz
authored andcommitted
lib: refactor to use validateString
PR-URL: nodejs#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>
1 parent c2d58f3 commit 4d0ffa7

File tree

6 files changed

+31
-56
lines changed

6 files changed

+31
-56
lines changed

lib/child_process.js

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

511511
// Validate the cwd, if present.
512-
if (options.cwd != null &&
513-
typeof options.cwd !== 'string') {
514-
throw new ERR_INVALID_ARG_TYPE('options.cwd', 'string', options.cwd);
512+
if (options.cwd != null) {
513+
validateString(options.cwd, 'options.cwd');
515514
}
516515

517516
// Validate detached, if present.
@@ -538,9 +537,8 @@ function normalizeSpawnArguments(file, args, options) {
538537
}
539538

540539
// Validate argv0, if present.
541-
if (options.argv0 != null &&
542-
typeof options.argv0 !== 'string') {
543-
throw new ERR_INVALID_ARG_TYPE('options.argv0', 'string', options.argv0);
540+
if (options.argv0 != null) {
541+
validateString(options.argv0, 'options.argv0');
544542
}
545543

546544
// Validate windowsHide, if present.

lib/dgram.js

+4-14
Original file line numberDiff line numberDiff line change
@@ -872,13 +872,8 @@ Socket.prototype.addSourceSpecificMembership = function(sourceAddress,
872872
interfaceAddress) {
873873
healthCheck(this);
874874

875-
if (typeof sourceAddress !== 'string') {
876-
throw new ERR_INVALID_ARG_TYPE('sourceAddress', 'string', sourceAddress);
877-
}
878-
879-
if (typeof groupAddress !== 'string') {
880-
throw new ERR_INVALID_ARG_TYPE('groupAddress', 'string', groupAddress);
881-
}
875+
validateString(sourceAddress, 'sourceAddress');
876+
validateString(groupAddress, 'groupAddress');
882877

883878
const err =
884879
this[kStateSymbol].handle.addSourceSpecificMembership(sourceAddress,
@@ -895,13 +890,8 @@ Socket.prototype.dropSourceSpecificMembership = function(sourceAddress,
895890
interfaceAddress) {
896891
healthCheck(this);
897892

898-
if (typeof sourceAddress !== 'string') {
899-
throw new ERR_INVALID_ARG_TYPE('sourceAddress', 'string', sourceAddress);
900-
}
901-
902-
if (typeof groupAddress !== 'string') {
903-
throw new ERR_INVALID_ARG_TYPE('groupAddress', 'string', groupAddress);
904-
}
893+
validateString(sourceAddress, 'sourceAddress');
894+
validateString(groupAddress, 'groupAddress');
905895

906896
const err =
907897
this[kStateSymbol].handle.dropSourceSpecificMembership(sourceAddress,

lib/internal/blocklist.js

+10-20
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ const kHandle = Symbol('kHandle');
2828
const { owner_symbol } = internalBinding('symbols');
2929

3030
const {
31-
ERR_INVALID_ARG_TYPE,
3231
ERR_INVALID_ARG_VALUE,
3332
} = require('internal/errors').codes;
3433

35-
const { validateInt32 } = require('internal/validators');
34+
const { validateInt32, validateString } = require('internal/validators');
3635

3736
class BlockList extends JSTransferable {
3837
constructor() {
@@ -56,10 +55,8 @@ class BlockList extends JSTransferable {
5655
}
5756

5857
addAddress(address, family = 'ipv4') {
59-
if (typeof address !== 'string')
60-
throw new ERR_INVALID_ARG_TYPE('address', 'string', address);
61-
if (typeof family !== 'string')
62-
throw new ERR_INVALID_ARG_TYPE('family', 'string', family);
58+
validateString(address, 'address');
59+
validateString(family, 'family');
6360
family = family.toLowerCase();
6461
if (family !== 'ipv4' && family !== 'ipv6')
6562
throw new ERR_INVALID_ARG_VALUE('family', family);
@@ -68,12 +65,9 @@ class BlockList extends JSTransferable {
6865
}
6966

7067
addRange(start, end, family = 'ipv4') {
71-
if (typeof start !== 'string')
72-
throw new ERR_INVALID_ARG_TYPE('start', 'string', start);
73-
if (typeof end !== 'string')
74-
throw new ERR_INVALID_ARG_TYPE('end', 'string', end);
75-
if (typeof family !== 'string')
76-
throw new ERR_INVALID_ARG_TYPE('family', 'string', family);
68+
validateString(start, 'start');
69+
validateString(end, 'end');
70+
validateString(family, 'family');
7771
family = family.toLowerCase();
7872
if (family !== 'ipv4' && family !== 'ipv6')
7973
throw new ERR_INVALID_ARG_VALUE('family', family);
@@ -84,10 +78,8 @@ class BlockList extends JSTransferable {
8478
}
8579

8680
addSubnet(network, prefix, family = 'ipv4') {
87-
if (typeof network !== 'string')
88-
throw new ERR_INVALID_ARG_TYPE('network', 'string', network);
89-
if (typeof family !== 'string')
90-
throw new ERR_INVALID_ARG_TYPE('family', 'string', family);
81+
validateString(network, 'network');
82+
validateString(family, 'family');
9183
family = family.toLowerCase();
9284
let type;
9385
switch (family) {
@@ -106,10 +98,8 @@ class BlockList extends JSTransferable {
10698
}
10799

108100
check(address, family = 'ipv4') {
109-
if (typeof address !== 'string')
110-
throw new ERR_INVALID_ARG_TYPE('address', 'string', address);
111-
if (typeof family !== 'string')
112-
throw new ERR_INVALID_ARG_TYPE('family', 'string', family);
101+
validateString(address, 'address');
102+
validateString(family, 'family');
113103
family = family.toLowerCase();
114104
if (family !== 'ipv4' && family !== 'ipv6')
115105
throw new ERR_INVALID_ARG_VALUE('family', family);

lib/internal/dns/utils.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const {
1414

1515
const errors = require('internal/errors');
1616
const { isIP } = require('internal/net');
17-
const { validateInt32 } = require('internal/validators');
17+
const {
18+
validateInt32,
19+
validateString,
20+
} = require('internal/validators');
1821
const {
1922
ChannelWrap,
2023
strerror,
@@ -71,9 +74,7 @@ class Resolver {
7174
const newSet = [];
7275

7376
ArrayPrototypeForEach(servers, (serv, index) => {
74-
if (typeof serv !== 'string') {
75-
throw new ERR_INVALID_ARG_TYPE(`servers[${index}]`, 'string', serv);
76-
}
77+
validateString(serv, `servers[${index}]`);
7778
let ipVersion = isIP(serv);
7879

7980
if (ipVersion !== 0)
@@ -121,9 +122,7 @@ class Resolver {
121122
}
122123

123124
setLocalAddress(ipv4, ipv6) {
124-
if (typeof ipv4 !== 'string') {
125-
throw new ERR_INVALID_ARG_TYPE('ipv4', 'String', ipv4);
126-
}
125+
validateString(ipv4, 'ipv4');
127126

128127
if (typeof ipv6 !== 'string' && ipv6 !== undefined) {
129128
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
@@ -28,7 +28,7 @@ const {
2828
ERR_INVALID_THIS,
2929
}
3030
} = require('internal/errors');
31-
const { validateObject } = require('internal/validators');
31+
const { validateObject, validateString } = require('internal/validators');
3232

3333
const { customInspectSymbol } = require('internal/util');
3434
const { inspect } = require('util');
@@ -487,9 +487,7 @@ class NodeEventTarget extends EventTarget {
487487
return this;
488488
}
489489
emit(type, arg) {
490-
if (typeof type !== 'string') {
491-
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
492-
}
490+
validateString(type, 'type');
493491
const hadListeners = this.listenerCount(type) > 0;
494492
this[kHybridDispatch](arg, type);
495493
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)