summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/_http_outgoing.js14
-rw-r--r--lib/_tls_wrap.js5
-rw-r--r--lib/async_hooks.js5
-rw-r--r--lib/buffer.js5
-rw-r--r--lib/child_process.js4
-rw-r--r--lib/dgram.js11
-rw-r--r--lib/dns.js6
-rw-r--r--lib/inspector.js5
-rw-r--r--lib/internal/child_process.js5
-rw-r--r--lib/internal/crypto/cipher.js9
-rw-r--r--lib/internal/crypto/diffiehellman.js9
-rw-r--r--lib/internal/crypto/hash.js7
-rw-r--r--lib/internal/crypto/sig.js8
-rw-r--r--lib/internal/crypto/util.js5
-rw-r--r--lib/internal/http2/compat.js25
-rw-r--r--lib/internal/validators.js8
-rw-r--r--lib/net.js6
17 files changed, 52 insertions, 85 deletions
diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js
index d150a9d93c..7fe4e2133b 100644
--- a/lib/_http_outgoing.js
+++ b/lib/_http_outgoing.js
@@ -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;
@@ -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)
@@ -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');
diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js
index 83da2721e8..3f2a2cc52d 100644
--- a/lib/_tls_wrap.js
+++ b/lib/_tls_wrap.js
@@ -51,6 +51,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');
@@ -646,9 +647,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();
diff --git a/lib/async_hooks.js b/lib/async_hooks.js
index 345b003e54..7d16070741 100644
--- a/lib/async_hooks.js
+++ b/lib/async_hooks.js
@@ -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
@@ -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 };
diff --git a/lib/buffer.js b/lib/buffer.js
index 7902fc6fde..398357c684 100644
--- a/lib/buffer.js
+++ b/lib/buffer.js
@@ -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');
@@ -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);
}
diff --git a/lib/child_process.js b/lib/child_process.js
index a790e85ee2..8460a732d9 100644
--- a/lib/child_process.js
+++ b/lib/child_process.js
@@ -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,
@@ -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');
diff --git a/lib/dgram.js b/lib/dgram.js
index 68e11df94c..bc94321117 100644
--- a/lib/dgram.js
+++ b/lib/dgram.js
@@ -39,6 +39,7 @@ const {
ERR_SOCKET_DGRAM_NOT_RUNNING,
ERR_INVALID_FD_TYPE
} = errors.codes;
+const { validateString } = require('internal/validators');
const { Buffer } = require('buffer');
const util = require('util');
const { isUint8Array } = require('internal/util/types');
@@ -322,9 +323,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);
};
@@ -623,11 +622,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) {
diff --git a/lib/dns.js b/lib/dns.js
index 195a018950..7059ed91dd 100644
--- a/lib/dns.js
+++ b/lib/dns.js
@@ -39,6 +39,7 @@ const {
ERR_MISSING_ARGS,
ERR_SOCKET_BAD_PORT
} = errors.codes;
+const { validateString } = require('internal/validators');
const {
GetAddrInfoReqWrap,
@@ -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();
}
diff --git a/lib/inspector.js b/lib/inspector.js
index f4e365b774..8827158757 100644
--- a/lib/inspector.js
+++ b/lib/inspector.js
@@ -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');
@@ -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;
diff --git a/lib/internal/child_process.js b/lib/internal/child_process.js
index 558932d341..32568aa311 100644
--- a/lib/internal/child_process.js
+++ b/lib/internal/child_process.js
@@ -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');
@@ -318,9 +319,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))
diff --git a/lib/internal/crypto/cipher.js b/lib/internal/crypto/cipher.js
index bd80c19697..a37d8044d8 100644
--- a/lib/internal/crypto/cipher.js
+++ b/lib/internal/crypto/cipher.js
@@ -10,6 +10,7 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_OPT_VALUE
} = require('internal/errors').codes;
+const { validateString } = require('internal/validators');
const {
getDefaultEncoding,
@@ -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(
@@ -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(
diff --git a/lib/internal/crypto/diffiehellman.js b/lib/internal/crypto/diffiehellman.js
index dad7a903b2..1daf5df905 100644
--- a/lib/internal/crypto/diffiehellman.js
+++ b/lib/internal/crypto/diffiehellman.js
@@ -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,
@@ -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);
}
@@ -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;
diff --git a/lib/internal/crypto/hash.js b/lib/internal/crypto/hash.js
index d172a6a3c8..7127d4d7ef 100644
--- a/lib/internal/crypto/hash.js
+++ b/lib/internal/crypto/hash.js
@@ -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');
@@ -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
@@ -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);
diff --git a/lib/internal/crypto/sig.js b/lib/internal/crypto/sig.js
index 8aff735473..ebd852402e 100644
--- a/lib/internal/crypto/sig.js
+++ b/lib/internal/crypto/sig.js
@@ -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
@@ -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);
@@ -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);
diff --git a/lib/internal/crypto/util.js b/lib/internal/crypto/util.js
index 32c9eb7f6a..9283e80df4 100644
--- a/lib/internal/crypto/util.js
+++ b/lib/internal/crypto/util.js
@@ -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,
@@ -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;
diff --git a/lib/internal/http2/compat.js b/lib/internal/http2/compat.js
index 4a006107b2..0e9c2832cc 100644
--- a/lib/internal/http2/compat.js
+++ b/lib/internal/http2/compat.js
@@ -12,11 +12,11 @@ const {
ERR_HTTP2_NO_SOCKET_MANIPULATION,
ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED,
ERR_HTTP2_STATUS_INVALID,
- ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_INVALID_CALLBACK,
ERR_INVALID_HTTP_TOKEN
} = require('internal/errors').codes;
+const { validateString } = require('internal/validators');
const { kSocket } = require('internal/http2/util');
const kBeginSend = Symbol('begin-send');
@@ -342,8 +342,7 @@ class Http2ServerRequest extends Readable {
}
set method(method) {
- if (typeof method !== 'string')
- throw new ERR_INVALID_ARG_TYPE('method', 'string', method);
+ validateString(method, 'method');
if (method.trim() === '')
throw new ERR_INVALID_ARG_VALUE('method', method);
@@ -482,9 +481,7 @@ class Http2ServerResponse extends Stream {
}
setTrailer(name, value) {
- if (typeof name !== 'string')
- throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
-
+ validateString(name, 'name');
name = name.trim().toLowerCase();
assertValidHeader(name, value);
this[kTrailers][name] = value;
@@ -500,9 +497,7 @@ class Http2ServerResponse extends Stream {
}
getHeader(name) {
- if (typeof name !== 'string')
- throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
-
+ validateString(name, 'name');
name = name.trim().toLowerCase();
return this[kHeaders][name];
}
@@ -516,17 +511,13 @@ class Http2ServerResponse extends Stream {
}
hasHeader(name) {
- if (typeof name !== 'string')
- throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
-
+ validateString(name, 'name');
name = name.trim().toLowerCase();
return Object.prototype.hasOwnProperty.call(this[kHeaders], name);
}
removeHeader(name) {
- if (typeof name !== 'string')
- throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
-
+ validateString(name, 'name');
if (this[kStream].headersSent)
throw new ERR_HTTP2_HEADERS_SENT();
@@ -535,9 +526,7 @@ class Http2ServerResponse extends Stream {
}
setHeader(name, value) {
- if (typeof name !== 'string')
- throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
-
+ validateString(name, 'name');
if (this[kStream].headersSent)
throw new ERR_HTTP2_HEADERS_SENT();
diff --git a/lib/internal/validators.js b/lib/internal/validators.js
index ab8643b9af..85e69e6125 100644
--- a/lib/internal/validators.js
+++ b/lib/internal/validators.js
@@ -120,11 +120,17 @@ function validateUint32(value, name, positive) {
return value;
}
+function validateString(value, name) {
+ if (typeof value !== 'string')
+ throw new ERR_INVALID_ARG_TYPE(name, 'string', value);
+}
+
module.exports = {
isInt32,
isUint32,
validateMode,
validateInteger,
validateInt32,
- validateUint32
+ validateUint32,
+ validateString
};
diff --git a/lib/net.js b/lib/net.js
index 8c05f2c07b..c49dd350c3 100644
--- a/lib/net.js
+++ b/lib/net.js
@@ -75,7 +75,7 @@ const {
ERR_SOCKET_BAD_PORT,
ERR_SOCKET_CLOSED
} = errors.codes;
-const { validateInt32 } = require('internal/validators');
+const { validateInt32, validateString } = require('internal/validators');
const kLastWriteQueueSize = Symbol('lastWriteQueueSize');
// Lazy loaded to improve startup performance.
@@ -980,9 +980,7 @@ Socket.prototype.connect = function(...args) {
this.writable = true;
if (pipe) {
- if (typeof path !== 'string') {
- throw new ERR_INVALID_ARG_TYPE('options.path', 'string', path);
- }
+ validateString(path, 'options.path');
defaultTriggerAsyncIdScope(
this[async_id_symbol], internalConnect, this, path
);