Skip to content

Commit d76400a

Browse files
Lxxyxtargos
authored andcommitted
lib: refactor to use validateString
PR-URL: #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 a29da64 commit d76400a

9 files changed

Lines changed: 38 additions & 65 deletions

File tree

‎lib/_tls_common.js‎

Lines changed: 1 addition & 2 deletions
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(typeofsigalgs!=='string')
232-
thrownewERR_INVALID_ARG_TYPE('options.sigalgs','string',sigalgs);
231+
validateString(sigalgs,'options.sigalgs');
233232

234233
if(sigalgs==='')
235234
thrownewERR_INVALID_ARG_VALUE('options.sigalgs',sigalgs);

‎lib/child_process.js‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,8 @@ function normalizeSpawnArguments(file, args, options) {
474474
thrownewERR_INVALID_ARG_TYPE('options','object',options);
475475

476476
// Validate the cwd, if present.
477-
if(options.cwd!=null&&
478-
typeofoptions.cwd!=='string'){
479-
thrownewERR_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-
typeofoptions.argv0!=='string'){
510-
thrownewERR_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‎

Lines changed: 4 additions & 14 deletions
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(typeofsourceAddress!=='string'){
860-
thrownewERR_INVALID_ARG_TYPE('sourceAddress','string',sourceAddress);
861-
}
862-
863-
if(typeofgroupAddress!=='string'){
864-
thrownewERR_INVALID_ARG_TYPE('groupAddress','string',groupAddress);
865-
}
859+
validateString(sourceAddress,'sourceAddress');
860+
validateString(groupAddress,'groupAddress');
866861

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

882-
if(typeofsourceAddress!=='string'){
883-
thrownewERR_INVALID_ARG_TYPE('sourceAddress','string',sourceAddress);
884-
}
885-
886-
if(typeofgroupAddress!=='string'){
887-
thrownewERR_INVALID_ARG_TYPE('groupAddress','string',groupAddress);
888-
}
877+
validateString(sourceAddress,'sourceAddress');
878+
validateString(groupAddress,'groupAddress');
889879

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

‎lib/dns.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ function lookup(hostname, options, callback) {
9999
letverbatim=false;
100100

101101
// Parse arguments
102-
if(hostname&&typeofhostname!=='string'){
103-
thrownewERR_INVALID_ARG_TYPE('hostname','string',hostname);
102+
if(hostname){
103+
validateString(hostname,'hostname');
104104
}
105105

106106
if(typeofoptions==='function'){

‎lib/internal/blocklist.js‎

Lines changed: 10 additions & 19 deletions
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
classBlockList{
3030
constructor(handle=newBlockListHandle()){
@@ -52,10 +52,8 @@ class BlockList {
5252
}
5353

5454
addAddress(address,family='ipv4'){
55-
if(typeofaddress!=='string')
56-
thrownewERR_INVALID_ARG_TYPE('address','string',address);
57-
if(typeoffamily!=='string')
58-
thrownewERR_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
thrownewERR_INVALID_ARG_VALUE('family',family);
@@ -64,12 +62,9 @@ class BlockList {
6462
}
6563

6664
addRange(start,end,family='ipv4'){
67-
if(typeofstart!=='string')
68-
thrownewERR_INVALID_ARG_TYPE('start','string',start);
69-
if(typeofend!=='string')
70-
thrownewERR_INVALID_ARG_TYPE('end','string',end);
71-
if(typeoffamily!=='string')
72-
thrownewERR_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
thrownewERR_INVALID_ARG_VALUE('family',family);
@@ -80,10 +75,8 @@ class BlockList {
8075
}
8176

8277
addSubnet(network,prefix,family='ipv4'){
83-
if(typeofnetwork!=='string')
84-
thrownewERR_INVALID_ARG_TYPE('network','string',network);
85-
if(typeoffamily!=='string')
86-
thrownewERR_INVALID_ARG_TYPE('family','string',family);
78+
validateString(network,'network');
79+
validateString(family,'family');
8780
family=family.toLowerCase();
8881
lettype;
8982
switch(family){
@@ -102,10 +95,8 @@ class BlockList {
10295
}
10396

10497
check(address,family='ipv4'){
105-
if(typeofaddress!=='string')
106-
thrownewERR_INVALID_ARG_TYPE('address','string',address);
107-
if(typeoffamily!=='string')
108-
thrownewERR_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
thrownewERR_INVALID_ARG_VALUE('family',family);

‎lib/internal/crypto/pbkdf2.js‎

Lines changed: 3 additions & 6 deletions
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
functioncheck(password,salt,iterations,keylen,digest){
89-
if(typeofdigest!=='string')
90-
thrownewERR_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‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ const {
1313

1414
consterrors=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
constnewSet=[];
6973

7074
ArrayPrototypeForEach(servers,(serv,index)=>{
71-
if(typeofserv!=='string'){
72-
thrownewERR_INVALID_ARG_TYPE(`servers[${index}]`,'string',serv);
73-
}
75+
validateString(serv,`servers[${index}]`);
7476
letipVersion=isIP(serv);
7577

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

120122
setLocalAddress(ipv4,ipv6){
121-
if(typeofipv4!=='string'){
122-
thrownewERR_INVALID_ARG_TYPE('ipv4','String',ipv4);
123-
}
123+
validateString(ipv4,'ipv4');
124124

125125
if(typeofipv6!=='string'&&ipv6!==undefined){
126126
thrownewERR_INVALID_ARG_TYPE('ipv6',['String','undefined'],ipv6);

‎lib/internal/event_target.js‎

Lines changed: 2 additions & 4 deletions
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
returnthis;
510510
}
511511
emit(type,arg){
512-
if(typeoftype!=='string'){
513-
thrownewERR_INVALID_ARG_TYPE('type','string',type);
514-
}
512+
validateString(type,'type');
515513
consthadListeners=this.listenerCount(type)>0;
516514
this[kHybridDispatch](arg,type);
517515
returnhadListeners;

‎lib/internal/process/warning.js‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010

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

1415
// Lazily loaded
1516
letfs;
@@ -120,14 +121,13 @@ function emitWarning(warning, type, code, ctor) {
120121
code=undefined;
121122
type='Warning';
122123
}
123-
if(type!==undefined&&typeoftype!=='string'){
124-
thrownewERR_INVALID_ARG_TYPE('type','string',type);
125-
}
124+
if(type!==undefined)
125+
validateString(type,'type');
126126
if(typeofcode==='function'){
127127
ctor=code;
128128
code=undefined;
129-
}elseif(code!==undefined&&typeofcode!=='string'){
130-
thrownewERR_INVALID_ARG_TYPE('code','string',code);
129+
}elseif(code!==undefined){
130+
validateString(code,'code');
131131
}
132132
if(typeofwarning==='string'){
133133
warning=createWarningObject(warning,type,code,ctor,detail);

0 commit comments

Comments
 (0)