Skip to content

Commit 193a048

Browse files
maclover7targos
authored andcommitted
lib: extract validateString validator
Pulls out a common argument validator to `internal/validators` PR-URL: #22101 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent fd318e7 commit 193a048

17 files changed

Lines changed: 52 additions & 85 deletions

‎lib/_http_outgoing.js‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const {
4545
ERR_STREAM_CANNOT_PIPE,
4646
ERR_STREAM_WRITE_AFTER_END
4747
}=require('internal/errors').codes;
48+
const{ validateString }=require('internal/validators');
4849

4950
const{CRLF, debug }=common;
5051

@@ -480,9 +481,7 @@ OutgoingMessage.prototype.setHeader = function setHeader(name, value) {
480481

481482

482483
OutgoingMessage.prototype.getHeader=functiongetHeader(name){
483-
if(typeofname!=='string'){
484-
thrownewERR_INVALID_ARG_TYPE('name','string',name);
485-
}
484+
validateString(name,'name');
486485

487486
constheaders=this[outHeadersKey];
488487
if(headers===null)
@@ -516,19 +515,14 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() {
516515

517516

518517
OutgoingMessage.prototype.hasHeader=functionhasHeader(name){
519-
if(typeofname!=='string'){
520-
thrownewERR_INVALID_ARG_TYPE('name','string',name);
521-
}
522-
518+
validateString(name,'name');
523519
returnthis[outHeadersKey]!==null&&
524520
!!this[outHeadersKey][name.toLowerCase()];
525521
};
526522

527523

528524
OutgoingMessage.prototype.removeHeader=functionremoveHeader(name){
529-
if(typeofname!=='string'){
530-
thrownewERR_INVALID_ARG_TYPE('name','string',name);
531-
}
525+
validateString(name,'name');
532526

533527
if(this._header){
534528
thrownewERR_HTTP_HEADERS_SENT('remove');

‎lib/_tls_wrap.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const {
5050
ERR_TLS_SESSION_ATTACK,
5151
ERR_TLS_SNI_FROM_SERVER
5252
}=require('internal/errors').codes;
53+
const{ validateString }=require('internal/validators');
5354
constkConnectOptions=Symbol('connect-options');
5455
constkDisableRenegotiation=Symbol('disable-renegotiation');
5556
constkErrorEmitted=Symbol('error-emitted');
@@ -645,9 +646,7 @@ TLSSocket.prototype._start = function() {
645646
};
646647

647648
TLSSocket.prototype.setServername=function(name){
648-
if(typeofname!=='string'){
649-
thrownewERR_INVALID_ARG_TYPE('name','string',name);
650-
}
649+
validateString(name,'name');
651650

652651
if(this._tlsOptions.isServer){
653652
thrownewERR_TLS_SNI_FROM_SERVER();

‎lib/async_hooks.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
const{
44
ERR_ASYNC_CALLBACK,
5-
ERR_INVALID_ARG_TYPE,
65
ERR_INVALID_ASYNC_ID
76
}=require('internal/errors').codes;
7+
const{ validateString }=require('internal/validators');
88
constinternal_async_hooks=require('internal/async_hooks');
99

1010
// Get functions
@@ -140,8 +140,7 @@ function showEmitBeforeAfterWarning() {
140140

141141
classAsyncResource{
142142
constructor(type,opts={}){
143-
if(typeoftype!=='string')
144-
thrownewERR_INVALID_ARG_TYPE('type','string',type);
143+
validateString(type,'type');
145144

146145
if(typeofopts==='number'){
147146
opts={triggerAsyncId: opts,requireManualDestroy: false};

‎lib/buffer.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const {
6969
ERR_NO_LONGER_SUPPORTED,
7070
ERR_UNKNOWN_ENCODING
7171
}=require('internal/errors').codes;
72+
const{ validateString }=require('internal/validators');
7273

7374
constinternalBuffer=require('internal/buffer');
7475

@@ -841,9 +842,7 @@ function _fill(buf, val, start, end, encoding) {
841842

842843
constnormalizedEncoding=normalizeEncoding(encoding);
843844
if(normalizedEncoding===undefined){
844-
if(typeofencoding!=='string'){
845-
thrownewERR_INVALID_ARG_TYPE('encoding','string',encoding);
846-
}
845+
validateString(encoding,'encoding');
847846
thrownewERR_UNKNOWN_ENCODING(encoding);
848847
}
849848

‎lib/child_process.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const {
3737
ERR_INVALID_OPT_VALUE,
3838
ERR_OUT_OF_RANGE
3939
}=require('internal/errors').codes;
40+
const{ validateString }=require('internal/validators');
4041
constchild_process=require('internal/child_process');
4142
const{
4243
_validateStdio,
@@ -390,8 +391,7 @@ function _convertCustomFds(options) {
390391
}
391392

392393
functionnormalizeSpawnArguments(file,args,options){
393-
if(typeoffile!=='string')
394-
thrownewERR_INVALID_ARG_TYPE('file','string',file);
394+
validateString(file,'file');
395395

396396
if(file.length===0)
397397
thrownewERR_INVALID_ARG_VALUE('file',file,'cannot be empty');

‎lib/dgram.js‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const {
3737
ERR_SOCKET_CANNOT_SEND,
3838
ERR_SOCKET_DGRAM_NOT_RUNNING
3939
}=errors.codes;
40+
const{ validateString }=require('internal/validators');
4041
const{ Buffer }=require('buffer');
4142
constutil=require('util');
4243
const{ isUint8Array }=require('internal/util/types');
@@ -269,9 +270,7 @@ Socket.prototype.sendto = function(buffer,
269270
thrownewERR_INVALID_ARG_TYPE('port','number',port);
270271
}
271272

272-
if(typeofaddress!=='string'){
273-
thrownewERR_INVALID_ARG_TYPE('address','string',address);
274-
}
273+
validateString(address,'address');
275274

276275
this.send(buffer,offset,length,port,address,callback);
277276
};
@@ -570,11 +569,7 @@ Socket.prototype.setMulticastLoopback = function(arg) {
570569

571570
Socket.prototype.setMulticastInterface=function(interfaceAddress){
572571
healthCheck(this);
573-
574-
if(typeofinterfaceAddress!=='string'){
575-
thrownewERR_INVALID_ARG_TYPE(
576-
'interfaceAddress','string',interfaceAddress);
577-
}
572+
validateString(interfaceAddress,'interfaceAddress');
578573

579574
consterr=this[kStateSymbol].handle.setMulticastInterface(interfaceAddress);
580575
if(err){

‎lib/dns.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const {
3939
ERR_MISSING_ARGS,
4040
ERR_SOCKET_BAD_PORT
4141
}=errors.codes;
42+
const{ validateString }=require('internal/validators');
4243

4344
const{
4445
GetAddrInfoReqWrap,
@@ -206,9 +207,8 @@ function resolver(bindingName) {
206207
callback=arguments[2];
207208
}
208209

209-
if(typeofname!=='string'){
210-
thrownewERR_INVALID_ARG_TYPE('name','string',name);
211-
}elseif(typeofcallback!=='function'){
210+
validateString(name,'name');
211+
if(typeofcallback!=='function'){
212212
thrownewERR_INVALID_CALLBACK();
213213
}
214214

‎lib/inspector.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
ERR_INVALID_ARG_TYPE,
1010
ERR_INVALID_CALLBACK
1111
}=require('internal/errors').codes;
12+
const{ validateString }=require('internal/validators');
1213
constutil=require('util');
1314
const{ Connection, open, url }=process.binding('inspector');
1415
const{ originalConsole }=require('internal/process/per_thread');
@@ -58,9 +59,7 @@ class Session extends EventEmitter {
5859
}
5960

6061
post(method,params,callback){
61-
if(typeofmethod!=='string'){
62-
thrownewERR_INVALID_ARG_TYPE('method','string',method);
63-
}
62+
validateString(method,'method');
6463
if(!callback&&util.isFunction(params)){
6564
callback=params;
6665
params=null;

‎lib/internal/child_process.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const {
1414
ERR_MISSING_ARGS
1515
}
1616
}=require('internal/errors');
17+
const{ validateString }=require('internal/validators');
1718
constEventEmitter=require('events');
1819
constnet=require('net');
1920
constdgram=require('dgram');
@@ -317,9 +318,7 @@ ChildProcess.prototype.spawn = function(options) {
317318
options.envPairs.push('NODE_CHANNEL_FD='+ipcFd);
318319
}
319320

320-
if(typeofoptions.file!=='string'){
321-
thrownewERR_INVALID_ARG_TYPE('options.file','string',options.file);
322-
}
321+
validateString(options.file,'options.file');
323322
this.spawnfile=options.file;
324323

325324
if(Array.isArray(options.args))

‎lib/internal/crypto/cipher.js‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
ERR_INVALID_ARG_TYPE,
1111
ERR_INVALID_OPT_VALUE
1212
}=require('internal/errors').codes;
13+
const{ validateString }=require('internal/validators');
1314

1415
const{
1516
getDefaultEncoding,
@@ -83,9 +84,7 @@ function createCipherBase(cipher, credential, options, decipher, iv) {
8384
}
8485

8586
functioncreateCipher(cipher,password,options,decipher){
86-
if(typeofcipher!=='string')
87-
thrownewERR_INVALID_ARG_TYPE('cipher','string',cipher);
88-
87+
validateString(cipher,'cipher');
8988
password=toBuf(password);
9089
if(!isArrayBufferView(password)){
9190
thrownewERR_INVALID_ARG_TYPE(
@@ -99,9 +98,7 @@ function createCipher(cipher, password, options, decipher) {
9998
}
10099

101100
functioncreateCipherWithIV(cipher,key,options,decipher,iv){
102-
if(typeofcipher!=='string')
103-
thrownewERR_INVALID_ARG_TYPE('cipher','string',cipher);
104-
101+
validateString(cipher,'cipher');
105102
key=toBuf(key);
106103
if(!isArrayBufferView(key)){
107104
thrownewERR_INVALID_ARG_TYPE(

0 commit comments

Comments
 (0)