Skip to content

Commit 96bdd47

Browse files
ZYSzysMylesBorins
authored andcommitted
lib: refactor argument validation using validateString
PR-URL: #24960 Refs: #22101 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 7f34c76 commit 96bdd47

9 files changed

Lines changed: 25 additions & 35 deletions

File tree

‎lib/internal/crypto/keygen.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const {
1515
PK_FORMAT_PEM
1616
}=internalBinding('crypto');
1717
const{ customPromisifyArgs }=require('internal/util');
18-
const{ isUint32 }=require('internal/validators');
18+
const{ isUint32, validateString}=require('internal/validators');
1919
const{
2020
ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS,
2121
ERR_INVALID_ARG_TYPE,
@@ -157,8 +157,7 @@ function parseKeyEncoding(keyType, options) {
157157
}
158158

159159
functioncheck(type,options,callback){
160-
if(typeoftype!=='string')
161-
thrownewERR_INVALID_ARG_TYPE('type','string',type);
160+
validateString(type,'type');
162161
if(options==null||typeofoptions!=='object')
163162
thrownewERR_INVALID_ARG_TYPE('options','object',options);
164163

‎lib/internal/dns/promises.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const {
2121
ERR_MISSING_ARGS,
2222
ERR_SOCKET_BAD_PORT
2323
}=codes;
24+
const{ validateString }=require('internal/validators');
2425

2526

2627
functiononlookup(err,addresses){
@@ -192,9 +193,7 @@ function createResolverPromise(resolver, bindingName, hostname, ttl) {
192193

193194
functionresolver(bindingName){
194195
functionquery(name,options){
195-
if(typeofname!=='string'){
196-
thrownewERR_INVALID_ARG_TYPE('name','string',name);
197-
}
196+
validateString(name,'name');
198197

199198
constttl=!!(options&&options.ttl);
200199
returncreateResolverPromise(this,bindingName,name,ttl);

‎lib/internal/http2/core.js‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const {
7878
ERR_SOCKET_CLOSED
7979
}
8080
}=require('internal/errors');
81-
const{ validateNumber }=require('internal/validators');
81+
const{ validateNumber, validateString}=require('internal/validators');
8282
const{ utcDate }=require('internal/http');
8383
const{ onServerStream,
8484
Http2ServerRequest,
@@ -1372,8 +1372,7 @@ class ServerHttp2Session extends Http2Session {
13721372
}
13731373
}
13741374

1375-
if(typeofalt!=='string')
1376-
thrownewERR_INVALID_ARG_TYPE('alt','string',alt);
1375+
validateString(alt,'alt');
13771376
if(!kQuotedString.test(alt))
13781377
thrownewERR_INVALID_CHAR('alt');
13791378

@@ -1402,8 +1401,7 @@ class ServerHttp2Session extends Http2Session {
14021401
}elseif(origin!=null&&typeoforigin==='object'){
14031402
origin=origin.origin;
14041403
}
1405-
if(typeoforigin!=='string')
1406-
thrownewERR_INVALID_ARG_TYPE('origin','string',origin);
1404+
validateString(origin,'origin');
14071405
if(origin==='null')
14081406
thrownewERR_HTTP2_INVALID_ORIGIN();
14091407

‎lib/internal/process/main_thread_only.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ const {
1212
}=require('internal/errors');
1313
const{
1414
validateMode,
15-
validateUint32
15+
validateUint32,
16+
validateString
1617
}=require('internal/validators');
1718

1819
const{
@@ -36,9 +37,7 @@ function setupProcessMethods(_chdir, _umask, _initgroups, _setegid,
3637
}
3738

3839
process.chdir=functionchdir(directory){
39-
if(typeofdirectory!=='string'){
40-
thrownewERR_INVALID_ARG_TYPE('directory','string',directory);
41-
}
40+
validateString(directory,'directory');
4241
return_chdir(directory);
4342
};
4443

‎lib/internal/vm/source_text_module.js‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@ const {
1818
emitExperimentalWarning
1919
}=require('internal/util');
2020
const{ SafePromise }=require('internal/safe_globals');
21-
const{ validateInt32, validateUint32 }=require('internal/validators');
21+
const{
22+
validateInt32,
23+
validateUint32,
24+
validateString
25+
}=require('internal/validators');
2226

2327
const{
2428
ModuleWrap,
@@ -54,8 +58,7 @@ class SourceTextModule {
5458
constructor(src,options={}){
5559
emitExperimentalWarning('vm.SourceTextModule');
5660

57-
if(typeofsrc!=='string')
58-
thrownewERR_INVALID_ARG_TYPE('src','string',src);
61+
validateString(src,'src');
5962
if(typeofoptions!=='object'||options===null)
6063
thrownewERR_INVALID_ARG_TYPE('options','Object',options);
6164

@@ -79,9 +82,7 @@ class SourceTextModule {
7982

8083
let{ url }=options;
8184
if(url!==undefined){
82-
if(typeofurl!=='string'){
83-
thrownewERR_INVALID_ARG_TYPE('options.url','string',url);
84-
}
85+
validateString(url,'options.url');
8586
url=newURL(url).href;
8687
}elseif(context===undefined){
8788
url=`${defaultModuleName}(${globalModuleId++})`;

‎lib/internal/worker.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const path = require('path');
66
constutil=require('util');
77
const{ Readable, Writable }=require('stream');
88
const{
9-
ERR_INVALID_ARG_TYPE,
109
ERR_WORKER_PATH,
1110
ERR_WORKER_UNSERIALIZABLE_ERROR,
1211
ERR_WORKER_UNSUPPORTED_EXTENSION,
1312
}=require('internal/errors').codes;
13+
const{ validateString }=require('internal/validators');
1414

1515
const{ MessagePort, MessageChannel }=internalBinding('messaging');
1616
const{
@@ -251,9 +251,7 @@ class Worker extends EventEmitter {
251251
constructor(filename,options={}){
252252
super();
253253
debug(`[${threadId}] create new worker`,filename,options);
254-
if(typeoffilename!=='string'){
255-
thrownewERR_INVALID_ARG_TYPE('filename','string',filename);
256-
}
254+
validateString(filename,'filename');
257255

258256
if(!options.eval){
259257
if(!path.isAbsolute(filename)&&

‎lib/readline.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
'use strict';
2929

3030
const{
31-
ERR_INVALID_ARG_TYPE,
3231
ERR_INVALID_CURSOR_POS,
3332
ERR_INVALID_OPT_VALUE
3433
}=require('internal/errors').codes;
3534
const{ debug, inherits }=require('util');
35+
const{ validateString }=require('internal/validators');
3636
const{ emitExperimentalWarning }=require('internal/util');
3737
const{ Buffer }=require('buffer');
3838
constEventEmitter=require('events');
@@ -310,9 +310,7 @@ Interface.prototype._onLine = function(line) {
310310
};
311311

312312
Interface.prototype._writeToOutput=function_writeToOutput(stringToWrite){
313-
if(typeofstringToWrite!=='string'){
314-
thrownewERR_INVALID_ARG_TYPE('stringToWrite','string',stringToWrite);
315-
}
313+
validateString(stringToWrite,'stringToWrite');
316314

317315
if(this.output!==null&&this.output!==undefined){
318316
this.output.write(stringToWrite);

‎lib/url.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const { SafeSet } = require('internal/safe_globals');
3131
const{
3232
ERR_INVALID_ARG_TYPE
3333
}=require('internal/errors').codes;
34+
const{ validateString }=require('internal/validators');
3435

3536
// This ensures setURLConstructor() is called before the native
3637
// URL::ToObject() method is used.
@@ -150,9 +151,7 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
150151
}
151152

152153
Url.prototype.parse=functionparse(url,parseQueryString,slashesDenoteHost){
153-
if(typeofurl!=='string'){
154-
thrownewERR_INVALID_ARG_TYPE('url','string',url);
155-
}
154+
validateString(url,'url');
156155

157156
// Copy chrome, IE, opera backslash-handling behavior.
158157
// Back slashes before the query string get converted to forward slashes

‎lib/v8.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
'use strict';
1616

1717
const{ Buffer }=require('buffer');
18-
const{ERR_INVALID_ARG_TYPE}=require('internal/errors').codes;
18+
const{validateString}=require('internal/validators');
1919
const{
2020
Serializer: _Serializer,
2121
Deserializer: _Deserializer
@@ -66,8 +66,7 @@ const heapSpaceStatisticsBuffer =
6666
newFloat64Array(heapSpaceStatisticsArrayBuffer);
6767

6868
functionsetFlagsFromString(flags){
69-
if(typeofflags!=='string')
70-
thrownewERR_INVALID_ARG_TYPE('flags','string',flags);
69+
validateString(flags,'flags');
7170
_setFlagsFromString(flags);
7271
}
7372

0 commit comments

Comments
 (0)