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

File tree

9 files changed

+25
-35
lines changed

9 files changed

+25
-35
lines changed

lib/internal/crypto/keygen.js

+2-3
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
function check(type, options, callback) {
160-
if (typeof type !== 'string')
161-
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
160+
validateString(type, 'type');
162161
if (options == null || typeof options !== 'object')
163162
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
164163

lib/internal/dns/promises.js

+2-3
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
function onlookup(err, addresses) {
@@ -192,9 +193,7 @@ function createResolverPromise(resolver, bindingName, hostname, ttl) {
192193

193194
function resolver(bindingName) {
194195
function query(name, options) {
195-
if (typeof name !== 'string') {
196-
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
197-
}
196+
validateString(name, 'name');
198197

199198
const ttl = !!(options && options.ttl);
200199
return createResolverPromise(this, bindingName, name, ttl);

lib/internal/http2/core.js

+3-5
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 (typeof alt !== 'string')
1376-
throw new ERR_INVALID_ARG_TYPE('alt', 'string', alt);
1375+
validateString(alt, 'alt');
13771376
if (!kQuotedString.test(alt))
13781377
throw new ERR_INVALID_CHAR('alt');
13791378

@@ -1402,8 +1401,7 @@ class ServerHttp2Session extends Http2Session {
14021401
} else if (origin != null && typeof origin === 'object') {
14031402
origin = origin.origin;
14041403
}
1405-
if (typeof origin !== 'string')
1406-
throw new ERR_INVALID_ARG_TYPE('origin', 'string', origin);
1404+
validateString(origin, 'origin');
14071405
if (origin === 'null')
14081406
throw new ERR_HTTP2_INVALID_ORIGIN();
14091407

lib/internal/process/main_thread_only.js

+3-4
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 = function chdir(directory) {
39-
if (typeof directory !== 'string') {
40-
throw new ERR_INVALID_ARG_TYPE('directory', 'string', directory);
41-
}
40+
validateString(directory, 'directory');
4241
return _chdir(directory);
4342
};
4443

lib/internal/vm/source_text_module.js

+7-6
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 (typeof src !== 'string')
58-
throw new ERR_INVALID_ARG_TYPE('src', 'string', src);
61+
validateString(src, 'src');
5962
if (typeof options !== 'object' || options === null)
6063
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
6164

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

8083
let { url } = options;
8184
if (url !== undefined) {
82-
if (typeof url !== 'string') {
83-
throw new ERR_INVALID_ARG_TYPE('options.url', 'string', url);
84-
}
85+
validateString(url, 'options.url');
8586
url = new URL(url).href;
8687
} else if (context === undefined) {
8788
url = `${defaultModuleName}(${globalModuleId++})`;

lib/internal/worker.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ const path = require('path');
66
const util = 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 (typeof filename !== 'string') {
255-
throw new ERR_INVALID_ARG_TYPE('filename', 'string', filename);
256-
}
254+
validateString(filename, 'filename');
257255

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

lib/readline.js

+2-4
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
const EventEmitter = require('events');
@@ -310,9 +310,7 @@ Interface.prototype._onLine = function(line) {
310310
};
311311

312312
Interface.prototype._writeToOutput = function _writeToOutput(stringToWrite) {
313-
if (typeof stringToWrite !== 'string') {
314-
throw new ERR_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

+2-3
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 = function parse(url, parseQueryString, slashesDenoteHost) {
153-
if (typeof url !== 'string') {
154-
throw new ERR_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

+2-3
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
new Float64Array(heapSpaceStatisticsArrayBuffer);
6767

6868
function setFlagsFromString(flags) {
69-
if (typeof flags !== 'string')
70-
throw new ERR_INVALID_ARG_TYPE('flags', 'string', flags);
69+
validateString(flags, 'flags');
7170
_setFlagsFromString(flags);
7271
}
7372

0 commit comments

Comments
 (0)