lib: refactor argument validation using validateString

PR-URL: https://github.com/nodejs/node/pull/24960
Refs: https://github.com/nodejs/node/pull/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>
This commit is contained in:
ZYSzys 2018-12-11 23:24:22 +08:00 committed by Rich Trott
parent e989269865
commit a35bd62ae1
9 changed files with 25 additions and 35 deletions

View File

@ -15,7 +15,7 @@ const {
PK_FORMAT_PEM PK_FORMAT_PEM
} = internalBinding('crypto'); } = internalBinding('crypto');
const { customPromisifyArgs } = require('internal/util'); const { customPromisifyArgs } = require('internal/util');
const { isUint32 } = require('internal/validators'); const { isUint32, validateString } = require('internal/validators');
const { const {
ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS, ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS,
ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_TYPE,
@ -157,8 +157,7 @@ function parseKeyEncoding(keyType, options) {
} }
function check(type, options, callback) { function check(type, options, callback) {
if (typeof type !== 'string') validateString(type, 'type');
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
if (options == null || typeof options !== 'object') if (options == null || typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'object', options); throw new ERR_INVALID_ARG_TYPE('options', 'object', options);

View File

@ -21,6 +21,7 @@ const {
ERR_MISSING_ARGS, ERR_MISSING_ARGS,
ERR_SOCKET_BAD_PORT ERR_SOCKET_BAD_PORT
} = codes; } = codes;
const { validateString } = require('internal/validators');
function onlookup(err, addresses) { function onlookup(err, addresses) {
@ -192,9 +193,7 @@ function createResolverPromise(resolver, bindingName, hostname, ttl) {
function resolver(bindingName) { function resolver(bindingName) {
function query(name, options) { function query(name, options) {
if (typeof name !== 'string') { validateString(name, 'name');
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
}
const ttl = !!(options && options.ttl); const ttl = !!(options && options.ttl);
return createResolverPromise(this, bindingName, name, ttl); return createResolverPromise(this, bindingName, name, ttl);

View File

@ -78,7 +78,7 @@ const {
ERR_SOCKET_CLOSED ERR_SOCKET_CLOSED
} }
} = require('internal/errors'); } = require('internal/errors');
const { validateNumber } = require('internal/validators'); const { validateNumber, validateString } = require('internal/validators');
const { utcDate } = require('internal/http'); const { utcDate } = require('internal/http');
const { onServerStream, const { onServerStream,
Http2ServerRequest, Http2ServerRequest,
@ -1372,8 +1372,7 @@ class ServerHttp2Session extends Http2Session {
} }
} }
if (typeof alt !== 'string') validateString(alt, 'alt');
throw new ERR_INVALID_ARG_TYPE('alt', 'string', alt);
if (!kQuotedString.test(alt)) if (!kQuotedString.test(alt))
throw new ERR_INVALID_CHAR('alt'); throw new ERR_INVALID_CHAR('alt');
@ -1402,8 +1401,7 @@ class ServerHttp2Session extends Http2Session {
} else if (origin != null && typeof origin === 'object') { } else if (origin != null && typeof origin === 'object') {
origin = origin.origin; origin = origin.origin;
} }
if (typeof origin !== 'string') validateString(origin, 'origin');
throw new ERR_INVALID_ARG_TYPE('origin', 'string', origin);
if (origin === 'null') if (origin === 'null')
throw new ERR_HTTP2_INVALID_ORIGIN(); throw new ERR_HTTP2_INVALID_ORIGIN();

View File

@ -12,7 +12,8 @@ const {
} = require('internal/errors'); } = require('internal/errors');
const { const {
validateMode, validateMode,
validateUint32 validateUint32,
validateString
} = require('internal/validators'); } = require('internal/validators');
const { const {
@ -36,9 +37,7 @@ function setupProcessMethods(_chdir, _umask, _initgroups, _setegid,
} }
process.chdir = function chdir(directory) { process.chdir = function chdir(directory) {
if (typeof directory !== 'string') { validateString(directory, 'directory');
throw new ERR_INVALID_ARG_TYPE('directory', 'string', directory);
}
return _chdir(directory); return _chdir(directory);
}; };

View File

@ -18,7 +18,11 @@ const {
emitExperimentalWarning emitExperimentalWarning
} = require('internal/util'); } = require('internal/util');
const { SafePromise } = require('internal/safe_globals'); const { SafePromise } = require('internal/safe_globals');
const { validateInt32, validateUint32 } = require('internal/validators'); const {
validateInt32,
validateUint32,
validateString
} = require('internal/validators');
const { const {
ModuleWrap, ModuleWrap,
@ -54,8 +58,7 @@ class SourceTextModule {
constructor(src, options = {}) { constructor(src, options = {}) {
emitExperimentalWarning('vm.SourceTextModule'); emitExperimentalWarning('vm.SourceTextModule');
if (typeof src !== 'string') validateString(src, 'src');
throw new ERR_INVALID_ARG_TYPE('src', 'string', src);
if (typeof options !== 'object' || options === null) if (typeof options !== 'object' || options === null)
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options); throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
@ -79,9 +82,7 @@ class SourceTextModule {
let { url } = options; let { url } = options;
if (url !== undefined) { if (url !== undefined) {
if (typeof url !== 'string') { validateString(url, 'options.url');
throw new ERR_INVALID_ARG_TYPE('options.url', 'string', url);
}
url = new URL(url).href; url = new URL(url).href;
} else if (context === undefined) { } else if (context === undefined) {
url = `${defaultModuleName}(${globalModuleId++})`; url = `${defaultModuleName}(${globalModuleId++})`;

View File

@ -6,11 +6,11 @@ const path = require('path');
const util = require('util'); const util = require('util');
const { Readable, Writable } = require('stream'); const { Readable, Writable } = require('stream');
const { const {
ERR_INVALID_ARG_TYPE,
ERR_WORKER_PATH, ERR_WORKER_PATH,
ERR_WORKER_UNSERIALIZABLE_ERROR, ERR_WORKER_UNSERIALIZABLE_ERROR,
ERR_WORKER_UNSUPPORTED_EXTENSION, ERR_WORKER_UNSUPPORTED_EXTENSION,
} = require('internal/errors').codes; } = require('internal/errors').codes;
const { validateString } = require('internal/validators');
const { MessagePort, MessageChannel } = internalBinding('messaging'); const { MessagePort, MessageChannel } = internalBinding('messaging');
const { const {
@ -251,9 +251,7 @@ class Worker extends EventEmitter {
constructor(filename, options = {}) { constructor(filename, options = {}) {
super(); super();
debug(`[${threadId}] create new worker`, filename, options); debug(`[${threadId}] create new worker`, filename, options);
if (typeof filename !== 'string') { validateString(filename, 'filename');
throw new ERR_INVALID_ARG_TYPE('filename', 'string', filename);
}
if (!options.eval) { if (!options.eval) {
if (!path.isAbsolute(filename) && if (!path.isAbsolute(filename) &&

View File

@ -28,10 +28,10 @@
'use strict'; 'use strict';
const { const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CURSOR_POS, ERR_INVALID_CURSOR_POS,
ERR_INVALID_OPT_VALUE ERR_INVALID_OPT_VALUE
} = require('internal/errors').codes; } = require('internal/errors').codes;
const { validateString } = require('internal/validators');
const { debug } = require('util'); const { debug } = require('util');
const { emitExperimentalWarning } = require('internal/util'); const { emitExperimentalWarning } = require('internal/util');
const { Buffer } = require('buffer'); const { Buffer } = require('buffer');
@ -311,9 +311,7 @@ Interface.prototype._onLine = function(line) {
}; };
Interface.prototype._writeToOutput = function _writeToOutput(stringToWrite) { Interface.prototype._writeToOutput = function _writeToOutput(stringToWrite) {
if (typeof stringToWrite !== 'string') { validateString(stringToWrite, 'stringToWrite');
throw new ERR_INVALID_ARG_TYPE('stringToWrite', 'string', stringToWrite);
}
if (this.output !== null && this.output !== undefined) { if (this.output !== null && this.output !== undefined) {
this.output.write(stringToWrite); this.output.write(stringToWrite);

View File

@ -31,6 +31,7 @@ const { SafeSet } = require('internal/safe_globals');
const { const {
ERR_INVALID_ARG_TYPE ERR_INVALID_ARG_TYPE
} = require('internal/errors').codes; } = require('internal/errors').codes;
const { validateString } = require('internal/validators');
// This ensures setURLConstructor() is called before the native // This ensures setURLConstructor() is called before the native
// URL::ToObject() method is used. // URL::ToObject() method is used.
@ -150,9 +151,7 @@ function urlParse(url, parseQueryString, slashesDenoteHost) {
} }
Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) { Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
if (typeof url !== 'string') { validateString(url, 'url');
throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
}
// Copy chrome, IE, opera backslash-handling behavior. // Copy chrome, IE, opera backslash-handling behavior.
// Back slashes before the query string get converted to forward slashes // Back slashes before the query string get converted to forward slashes

View File

@ -15,7 +15,7 @@
'use strict'; 'use strict';
const { Buffer } = require('buffer'); const { Buffer } = require('buffer');
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes; const { validateString } = require('internal/validators');
const { const {
Serializer: _Serializer, Serializer: _Serializer,
Deserializer: _Deserializer Deserializer: _Deserializer
@ -66,8 +66,7 @@ const heapSpaceStatisticsBuffer =
new Float64Array(heapSpaceStatisticsArrayBuffer); new Float64Array(heapSpaceStatisticsArrayBuffer);
function setFlagsFromString(flags) { function setFlagsFromString(flags) {
if (typeof flags !== 'string') validateString(flags, 'flags');
throw new ERR_INVALID_ARG_TYPE('flags', 'string', flags);
_setFlagsFromString(flags); _setFlagsFromString(flags);
} }