diff options
author | ZYSzys <17367077526@163.com> | 2018-12-11 23:24:22 +0800 |
---|---|---|
committer | Rich Trott <rtrott@gmail.com> | 2018-12-13 17:22:43 -0800 |
commit | a35bd62ae17f9f673430dd90a57ef9bf9f42012a (patch) | |
tree | 0d259d81ed2044ba14cd02a688e3c3f2b9e7b3b9 /lib/internal | |
parent | e9892698655af635f665b7250277fa4c68321313 (diff) | |
download | android-node-v8-a35bd62ae17f9f673430dd90a57ef9bf9f42012a.tar.gz android-node-v8-a35bd62ae17f9f673430dd90a57ef9bf9f42012a.tar.bz2 android-node-v8-a35bd62ae17f9f673430dd90a57ef9bf9f42012a.zip |
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>
Diffstat (limited to 'lib/internal')
-rw-r--r-- | lib/internal/crypto/keygen.js | 5 | ||||
-rw-r--r-- | lib/internal/dns/promises.js | 5 | ||||
-rw-r--r-- | lib/internal/http2/core.js | 8 | ||||
-rw-r--r-- | lib/internal/process/main_thread_only.js | 7 | ||||
-rw-r--r-- | lib/internal/vm/source_text_module.js | 13 | ||||
-rw-r--r-- | lib/internal/worker.js | 6 |
6 files changed, 19 insertions, 25 deletions
diff --git a/lib/internal/crypto/keygen.js b/lib/internal/crypto/keygen.js index 61898989b0..7222d301f0 100644 --- a/lib/internal/crypto/keygen.js +++ b/lib/internal/crypto/keygen.js @@ -15,7 +15,7 @@ const { PK_FORMAT_PEM } = internalBinding('crypto'); const { customPromisifyArgs } = require('internal/util'); -const { isUint32 } = require('internal/validators'); +const { isUint32, validateString } = require('internal/validators'); const { ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS, ERR_INVALID_ARG_TYPE, @@ -157,8 +157,7 @@ function parseKeyEncoding(keyType, options) { } function check(type, options, callback) { - if (typeof type !== 'string') - throw new ERR_INVALID_ARG_TYPE('type', 'string', type); + validateString(type, 'type'); if (options == null || typeof options !== 'object') throw new ERR_INVALID_ARG_TYPE('options', 'object', options); diff --git a/lib/internal/dns/promises.js b/lib/internal/dns/promises.js index 4bf5ac0219..be49ebf210 100644 --- a/lib/internal/dns/promises.js +++ b/lib/internal/dns/promises.js @@ -21,6 +21,7 @@ const { ERR_MISSING_ARGS, ERR_SOCKET_BAD_PORT } = codes; +const { validateString } = require('internal/validators'); function onlookup(err, addresses) { @@ -192,9 +193,7 @@ function createResolverPromise(resolver, bindingName, hostname, ttl) { function resolver(bindingName) { function query(name, options) { - if (typeof name !== 'string') { - throw new ERR_INVALID_ARG_TYPE('name', 'string', name); - } + validateString(name, 'name'); const ttl = !!(options && options.ttl); return createResolverPromise(this, bindingName, name, ttl); diff --git a/lib/internal/http2/core.js b/lib/internal/http2/core.js index 4d762611b4..e27d529068 100644 --- a/lib/internal/http2/core.js +++ b/lib/internal/http2/core.js @@ -78,7 +78,7 @@ const { ERR_SOCKET_CLOSED } } = require('internal/errors'); -const { validateNumber } = require('internal/validators'); +const { validateNumber, validateString } = require('internal/validators'); const { utcDate } = require('internal/http'); const { onServerStream, Http2ServerRequest, @@ -1372,8 +1372,7 @@ class ServerHttp2Session extends Http2Session { } } - if (typeof alt !== 'string') - throw new ERR_INVALID_ARG_TYPE('alt', 'string', alt); + validateString(alt, 'alt'); if (!kQuotedString.test(alt)) throw new ERR_INVALID_CHAR('alt'); @@ -1402,8 +1401,7 @@ class ServerHttp2Session extends Http2Session { } else if (origin != null && typeof origin === 'object') { origin = origin.origin; } - if (typeof origin !== 'string') - throw new ERR_INVALID_ARG_TYPE('origin', 'string', origin); + validateString(origin, 'origin'); if (origin === 'null') throw new ERR_HTTP2_INVALID_ORIGIN(); diff --git a/lib/internal/process/main_thread_only.js b/lib/internal/process/main_thread_only.js index 148dfdc0f7..b8b7a454dc 100644 --- a/lib/internal/process/main_thread_only.js +++ b/lib/internal/process/main_thread_only.js @@ -12,7 +12,8 @@ const { } = require('internal/errors'); const { validateMode, - validateUint32 + validateUint32, + validateString } = require('internal/validators'); const { @@ -36,9 +37,7 @@ function setupProcessMethods(_chdir, _umask, _initgroups, _setegid, } process.chdir = function chdir(directory) { - if (typeof directory !== 'string') { - throw new ERR_INVALID_ARG_TYPE('directory', 'string', directory); - } + validateString(directory, 'directory'); return _chdir(directory); }; diff --git a/lib/internal/vm/source_text_module.js b/lib/internal/vm/source_text_module.js index d22db6e914..8b107a9b74 100644 --- a/lib/internal/vm/source_text_module.js +++ b/lib/internal/vm/source_text_module.js @@ -18,7 +18,11 @@ const { emitExperimentalWarning } = require('internal/util'); const { SafePromise } = require('internal/safe_globals'); -const { validateInt32, validateUint32 } = require('internal/validators'); +const { + validateInt32, + validateUint32, + validateString +} = require('internal/validators'); const { ModuleWrap, @@ -54,8 +58,7 @@ class SourceTextModule { constructor(src, options = {}) { emitExperimentalWarning('vm.SourceTextModule'); - if (typeof src !== 'string') - throw new ERR_INVALID_ARG_TYPE('src', 'string', src); + validateString(src, 'src'); if (typeof options !== 'object' || options === null) throw new ERR_INVALID_ARG_TYPE('options', 'Object', options); @@ -79,9 +82,7 @@ class SourceTextModule { let { url } = options; if (url !== undefined) { - if (typeof url !== 'string') { - throw new ERR_INVALID_ARG_TYPE('options.url', 'string', url); - } + validateString(url, 'options.url'); url = new URL(url).href; } else if (context === undefined) { url = `${defaultModuleName}(${globalModuleId++})`; diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 669a86bb23..9e41dba716 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -6,11 +6,11 @@ const path = require('path'); const util = require('util'); const { Readable, Writable } = require('stream'); const { - ERR_INVALID_ARG_TYPE, ERR_WORKER_PATH, ERR_WORKER_UNSERIALIZABLE_ERROR, ERR_WORKER_UNSUPPORTED_EXTENSION, } = require('internal/errors').codes; +const { validateString } = require('internal/validators'); const { MessagePort, MessageChannel } = internalBinding('messaging'); const { @@ -251,9 +251,7 @@ class Worker extends EventEmitter { constructor(filename, options = {}) { super(); debug(`[${threadId}] create new worker`, filename, options); - if (typeof filename !== 'string') { - throw new ERR_INVALID_ARG_TYPE('filename', 'string', filename); - } + validateString(filename, 'filename'); if (!options.eval) { if (!path.isAbsolute(filename) && |