Skip to content

Commit

Permalink
lib: extract validateString validator
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
maclover7 authored and targos committed Aug 11, 2018
1 parent fd318e7 commit 193a048
Show file tree
Hide file tree
Showing 17 changed files with 52 additions and 85 deletions.
14 changes: 4 additions & 10 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const {
ERR_STREAM_CANNOT_PIPE,
ERR_STREAM_WRITE_AFTER_END
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');

const { CRLF, debug } = common;

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


OutgoingMessage.prototype.getHeader = function getHeader(name) {
if (typeof name !== 'string') {
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
}
validateString(name, 'name');

const headers = this[outHeadersKey];
if (headers === null)
Expand Down Expand Up @@ -516,19 +515,14 @@ OutgoingMessage.prototype.getHeaders = function getHeaders() {


OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
if (typeof name !== 'string') {
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
}

validateString(name, 'name');
return this[outHeadersKey] !== null &&
!!this[outHeadersKey][name.toLowerCase()];
};


OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
if (typeof name !== 'string') {
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
}
validateString(name, 'name');

if (this._header) {
throw new ERR_HTTP_HEADERS_SENT('remove');
Expand Down
5 changes: 2 additions & 3 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const {
ERR_TLS_SESSION_ATTACK,
ERR_TLS_SNI_FROM_SERVER
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');
const kConnectOptions = Symbol('connect-options');
const kDisableRenegotiation = Symbol('disable-renegotiation');
const kErrorEmitted = Symbol('error-emitted');
Expand Down Expand Up @@ -645,9 +646,7 @@ TLSSocket.prototype._start = function() {
};

TLSSocket.prototype.setServername = function(name) {
if (typeof name !== 'string') {
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
}
validateString(name, 'name');

if (this._tlsOptions.isServer) {
throw new ERR_TLS_SNI_FROM_SERVER();
Expand Down
5 changes: 2 additions & 3 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

const {
ERR_ASYNC_CALLBACK,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ASYNC_ID
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');
const internal_async_hooks = require('internal/async_hooks');

// Get functions
Expand Down Expand Up @@ -140,8 +140,7 @@ function showEmitBeforeAfterWarning() {

class AsyncResource {
constructor(type, opts = {}) {
if (typeof type !== 'string')
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
validateString(type, 'type');

if (typeof opts === 'number') {
opts = { triggerAsyncId: opts, requireManualDestroy: false };
Expand Down
5 changes: 2 additions & 3 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const {
ERR_NO_LONGER_SUPPORTED,
ERR_UNKNOWN_ENCODING
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');

const internalBuffer = require('internal/buffer');

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

const normalizedEncoding = normalizeEncoding(encoding);
if (normalizedEncoding === undefined) {
if (typeof encoding !== 'string') {
throw new ERR_INVALID_ARG_TYPE('encoding', 'string', encoding);
}
validateString(encoding, 'encoding');
throw new ERR_UNKNOWN_ENCODING(encoding);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const {
ERR_INVALID_OPT_VALUE,
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');
const child_process = require('internal/child_process');
const {
_validateStdio,
Expand Down Expand Up @@ -390,8 +391,7 @@ function _convertCustomFds(options) {
}

function normalizeSpawnArguments(file, args, options) {
if (typeof file !== 'string')
throw new ERR_INVALID_ARG_TYPE('file', 'string', file);
validateString(file, 'file');

if (file.length === 0)
throw new ERR_INVALID_ARG_VALUE('file', file, 'cannot be empty');
Expand Down
11 changes: 3 additions & 8 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const {
ERR_SOCKET_CANNOT_SEND,
ERR_SOCKET_DGRAM_NOT_RUNNING
} = errors.codes;
const { validateString } = require('internal/validators');
const { Buffer } = require('buffer');
const util = require('util');
const { isUint8Array } = require('internal/util/types');
Expand Down Expand Up @@ -269,9 +270,7 @@ Socket.prototype.sendto = function(buffer,
throw new ERR_INVALID_ARG_TYPE('port', 'number', port);
}

if (typeof address !== 'string') {
throw new ERR_INVALID_ARG_TYPE('address', 'string', address);
}
validateString(address, 'address');

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

Socket.prototype.setMulticastInterface = function(interfaceAddress) {
healthCheck(this);

if (typeof interfaceAddress !== 'string') {
throw new ERR_INVALID_ARG_TYPE(
'interfaceAddress', 'string', interfaceAddress);
}
validateString(interfaceAddress, 'interfaceAddress');

const err = this[kStateSymbol].handle.setMulticastInterface(interfaceAddress);
if (err) {
Expand Down
6 changes: 3 additions & 3 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const {
ERR_MISSING_ARGS,
ERR_SOCKET_BAD_PORT
} = errors.codes;
const { validateString } = require('internal/validators');

const {
GetAddrInfoReqWrap,
Expand Down Expand Up @@ -206,9 +207,8 @@ function resolver(bindingName) {
callback = arguments[2];
}

if (typeof name !== 'string') {
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
} else if (typeof callback !== 'function') {
validateString(name, 'name');
if (typeof callback !== 'function') {
throw new ERR_INVALID_CALLBACK();
}

Expand Down
5 changes: 2 additions & 3 deletions lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_CALLBACK
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');
const util = require('util');
const { Connection, open, url } = process.binding('inspector');
const { originalConsole } = require('internal/process/per_thread');
Expand Down Expand Up @@ -58,9 +59,7 @@ class Session extends EventEmitter {
}

post(method, params, callback) {
if (typeof method !== 'string') {
throw new ERR_INVALID_ARG_TYPE('method', 'string', method);
}
validateString(method, 'method');
if (!callback && util.isFunction(params)) {
callback = params;
params = null;
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const {
ERR_MISSING_ARGS
}
} = require('internal/errors');
const { validateString } = require('internal/validators');
const EventEmitter = require('events');
const net = require('net');
const dgram = require('dgram');
Expand Down Expand Up @@ -317,9 +318,7 @@ ChildProcess.prototype.spawn = function(options) {
options.envPairs.push('NODE_CHANNEL_FD=' + ipcFd);
}

if (typeof options.file !== 'string') {
throw new ERR_INVALID_ARG_TYPE('options.file', 'string', options.file);
}
validateString(options.file, 'options.file');
this.spawnfile = options.file;

if (Array.isArray(options.args))
Expand Down
9 changes: 3 additions & 6 deletions lib/internal/crypto/cipher.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_OPT_VALUE
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');

const {
getDefaultEncoding,
Expand Down Expand Up @@ -83,9 +84,7 @@ function createCipherBase(cipher, credential, options, decipher, iv) {
}

function createCipher(cipher, password, options, decipher) {
if (typeof cipher !== 'string')
throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);

validateString(cipher, 'cipher');
password = toBuf(password);
if (!isArrayBufferView(password)) {
throw new ERR_INVALID_ARG_TYPE(
Expand All @@ -99,9 +98,7 @@ function createCipher(cipher, password, options, decipher) {
}

function createCipherWithIV(cipher, key, options, decipher, iv) {
if (typeof cipher !== 'string')
throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);

validateString(cipher, 'cipher');
key = toBuf(key);
if (!isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE(
Expand Down
9 changes: 3 additions & 6 deletions lib/internal/crypto/diffiehellman.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY,
ERR_INVALID_ARG_TYPE
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');
const { isArrayBufferView } = require('internal/util/types');
const {
getDefaultEncoding,
Expand Down Expand Up @@ -167,9 +168,7 @@ function ECDH(curve) {
if (!(this instanceof ECDH))
return new ECDH(curve);

if (typeof curve !== 'string')
throw new ERR_INVALID_ARG_TYPE('curve', 'string', curve);

validateString(curve, 'curve');
this._handle = new _ECDH(curve);
}

Expand Down Expand Up @@ -200,9 +199,7 @@ ECDH.convertKey = function convertKey(key, curve, inEnc, outEnc, format) {
);
}

if (typeof curve !== 'string') {
throw new ERR_INVALID_ARG_TYPE('curve', 'string', curve);
}
validateString(curve, 'curve');

const encoding = getDefaultEncoding();
inEnc = inEnc || encoding;
Expand Down
7 changes: 3 additions & 4 deletions lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
ERR_CRYPTO_HASH_UPDATE_FAILED,
ERR_INVALID_ARG_TYPE
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');
const { inherits } = require('util');
const { normalizeEncoding } = require('internal/util');
const { isArrayBufferView } = require('internal/util/types');
Expand All @@ -28,8 +29,7 @@ const kFinalized = Symbol('finalized');
function Hash(algorithm, options) {
if (!(this instanceof Hash))
return new Hash(algorithm, options);
if (typeof algorithm !== 'string')
throw new ERR_INVALID_ARG_TYPE('algorithm', 'string', algorithm);
validateString(algorithm, 'algorithm');
this._handle = new _Hash(algorithm);
this[kState] = {
[kFinalized]: false
Expand Down Expand Up @@ -83,8 +83,7 @@ Hash.prototype.digest = function digest(outputEncoding) {
function Hmac(hmac, key, options) {
if (!(this instanceof Hmac))
return new Hmac(hmac, key, options);
if (typeof hmac !== 'string')
throw new ERR_INVALID_ARG_TYPE('hmac', 'string', hmac);
validateString(hmac, 'hmac');
if (typeof key !== 'string' && !isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE('key',
['string', 'TypedArray', 'DataView'], key);
Expand Down
8 changes: 3 additions & 5 deletions lib/internal/crypto/sig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

const {
ERR_CRYPTO_SIGN_KEY_REQUIRED,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_OPT_VALUE
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');
const {
Sign: _Sign,
Verify: _Verify
Expand All @@ -24,8 +24,7 @@ const { inherits } = require('util');
function Sign(algorithm, options) {
if (!(this instanceof Sign))
return new Sign(algorithm, options);
if (typeof algorithm !== 'string')
throw new ERR_INVALID_ARG_TYPE('algorithm', 'string', algorithm);
validateString(algorithm, 'algorithm');
this._handle = new _Sign();
this._handle.init(algorithm);

Expand Down Expand Up @@ -94,8 +93,7 @@ Sign.prototype.sign = function sign(options, encoding) {
function Verify(algorithm, options) {
if (!(this instanceof Verify))
return new Verify(algorithm, options);
if (typeof algorithm !== 'string')
throw new ERR_INVALID_ARG_TYPE('algorithm', 'string', algorithm);
validateString(algorithm, 'algorithm');
this._handle = new _Verify();
this._handle.init(algorithm);

Expand Down
5 changes: 2 additions & 3 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH,
ERR_INVALID_ARG_TYPE,
} = require('internal/errors').codes;
const { validateString } = require('internal/validators');
const { Buffer } = require('buffer');
const {
cachedResult,
Expand Down Expand Up @@ -53,9 +54,7 @@ const getHashes = cachedResult(() => filterDuplicateStrings(_getHashes()));
const getCurves = cachedResult(() => filterDuplicateStrings(_getCurves()));

function setEngine(id, flags) {
if (typeof id !== 'string')
throw new ERR_INVALID_ARG_TYPE('id', 'string', id);

validateString(id, 'id');
if (flags && typeof flags !== 'number')
throw new ERR_INVALID_ARG_TYPE('flags', 'number', flags);
flags = flags >>> 0;
Expand Down
Loading

0 comments on commit 193a048

Please sign in to comment.