Skip to content

Commit

Permalink
lib: refactor argument validation using validateString
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
ZYSzys authored and MylesBorins committed Dec 25, 2018
1 parent 7f34c76 commit 96bdd47
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 35 deletions.
5 changes: 2 additions & 3 deletions lib/internal/crypto/keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 2 additions & 3 deletions lib/internal/dns/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
ERR_MISSING_ARGS,
ERR_SOCKET_BAD_PORT
} = codes;
const { validateString } = require('internal/validators');


function onlookup(err, addresses) {
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 3 additions & 5 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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();

Expand Down
7 changes: 3 additions & 4 deletions lib/internal/process/main_thread_only.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const {
} = require('internal/errors');
const {
validateMode,
validateUint32
validateUint32,
validateString
} = require('internal/validators');

const {
Expand All @@ -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);
};

Expand Down
13 changes: 7 additions & 6 deletions lib/internal/vm/source_text_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand All @@ -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++})`;
Expand Down
6 changes: 2 additions & 4 deletions lib/internal/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) &&
Expand Down
6 changes: 2 additions & 4 deletions lib/readline.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
'use strict';

const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CURSOR_POS,
ERR_INVALID_OPT_VALUE
} = require('internal/errors').codes;
const { debug, inherits } = require('util');
const { validateString } = require('internal/validators');
const { emitExperimentalWarning } = require('internal/util');
const { Buffer } = require('buffer');
const EventEmitter = require('events');
Expand Down Expand Up @@ -310,9 +310,7 @@ Interface.prototype._onLine = function(line) {
};

Interface.prototype._writeToOutput = function _writeToOutput(stringToWrite) {
if (typeof stringToWrite !== 'string') {
throw new ERR_INVALID_ARG_TYPE('stringToWrite', 'string', stringToWrite);
}
validateString(stringToWrite, 'stringToWrite');

if (this.output !== null && this.output !== undefined) {
this.output.write(stringToWrite);
Expand Down
5 changes: 2 additions & 3 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const { SafeSet } = require('internal/safe_globals');
const {
ERR_INVALID_ARG_TYPE
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');

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

Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
if (typeof url !== 'string') {
throw new ERR_INVALID_ARG_TYPE('url', 'string', url);
}
validateString(url, 'url');

// Copy chrome, IE, opera backslash-handling behavior.
// Back slashes before the query string get converted to forward slashes
Expand Down
5 changes: 2 additions & 3 deletions lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'use strict';

const { Buffer } = require('buffer');
const { ERR_INVALID_ARG_TYPE } = require('internal/errors').codes;
const { validateString } = require('internal/validators');
const {
Serializer: _Serializer,
Deserializer: _Deserializer
Expand Down Expand Up @@ -66,8 +66,7 @@ const heapSpaceStatisticsBuffer =
new Float64Array(heapSpaceStatisticsArrayBuffer);

function setFlagsFromString(flags) {
if (typeof flags !== 'string')
throw new ERR_INVALID_ARG_TYPE('flags', 'string', flags);
validateString(flags, 'flags');
_setFlagsFromString(flags);
}

Expand Down

0 comments on commit 96bdd47

Please sign in to comment.