Skip to content

Commit 3f729aa

Browse files
maclover7rvagg
authored andcommitted
lib: extract validateString validator
Pulls out a common argument validator to `internal/validators` PR-URL: #22101 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ad46cca commit 3f729aa

17 files changed

+52
-85
lines changed

lib/_http_outgoing.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const {
4545
ERR_STREAM_CANNOT_PIPE,
4646
ERR_STREAM_WRITE_AFTER_END
4747
} = require('internal/errors').codes;
48+
const { validateString } = require('internal/validators');
4849

4950
const { CRLF, debug } = common;
5051

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

481482

482483
OutgoingMessage.prototype.getHeader = function getHeader(name) {
483-
if (typeof name !== 'string') {
484-
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
485-
}
484+
validateString(name, 'name');
486485

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

517516

518517
OutgoingMessage.prototype.hasHeader = function hasHeader(name) {
519-
if (typeof name !== 'string') {
520-
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
521-
}
522-
518+
validateString(name, 'name');
523519
return this[outHeadersKey] !== null &&
524520
!!this[outHeadersKey][name.toLowerCase()];
525521
};
526522

527523

528524
OutgoingMessage.prototype.removeHeader = function removeHeader(name) {
529-
if (typeof name !== 'string') {
530-
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
531-
}
525+
validateString(name, 'name');
532526

533527
if (this._header) {
534528
throw new ERR_HTTP_HEADERS_SENT('remove');

lib/_tls_wrap.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const {
5050
ERR_TLS_SESSION_ATTACK,
5151
ERR_TLS_SNI_FROM_SERVER
5252
} = require('internal/errors').codes;
53+
const { validateString } = require('internal/validators');
5354
const kConnectOptions = Symbol('connect-options');
5455
const kDisableRenegotiation = Symbol('disable-renegotiation');
5556
const kErrorEmitted = Symbol('error-emitted');
@@ -645,9 +646,7 @@ TLSSocket.prototype._start = function() {
645646
};
646647

647648
TLSSocket.prototype.setServername = function(name) {
648-
if (typeof name !== 'string') {
649-
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
650-
}
649+
validateString(name, 'name');
651650

652651
if (this._tlsOptions.isServer) {
653652
throw new ERR_TLS_SNI_FROM_SERVER();

lib/async_hooks.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
const {
44
ERR_ASYNC_CALLBACK,
5-
ERR_INVALID_ARG_TYPE,
65
ERR_INVALID_ASYNC_ID
76
} = require('internal/errors').codes;
7+
const { validateString } = require('internal/validators');
88
const internal_async_hooks = require('internal/async_hooks');
99

1010
// Get functions
@@ -140,8 +140,7 @@ function showEmitBeforeAfterWarning() {
140140

141141
class AsyncResource {
142142
constructor(type, opts = {}) {
143-
if (typeof type !== 'string')
144-
throw new ERR_INVALID_ARG_TYPE('type', 'string', type);
143+
validateString(type, 'type');
145144

146145
if (typeof opts === 'number') {
147146
opts = { triggerAsyncId: opts, requireManualDestroy: false };

lib/buffer.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ const {
6969
ERR_NO_LONGER_SUPPORTED,
7070
ERR_UNKNOWN_ENCODING
7171
} = require('internal/errors').codes;
72+
const { validateString } = require('internal/validators');
7273

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

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

842843
const normalizedEncoding = normalizeEncoding(encoding);
843844
if (normalizedEncoding === undefined) {
844-
if (typeof encoding !== 'string') {
845-
throw new ERR_INVALID_ARG_TYPE('encoding', 'string', encoding);
846-
}
845+
validateString(encoding, 'encoding');
847846
throw new ERR_UNKNOWN_ENCODING(encoding);
848847
}
849848

lib/child_process.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const {
3737
ERR_INVALID_OPT_VALUE,
3838
ERR_OUT_OF_RANGE
3939
} = require('internal/errors').codes;
40+
const { validateString } = require('internal/validators');
4041
const child_process = require('internal/child_process');
4142
const {
4243
_validateStdio,
@@ -390,8 +391,7 @@ function _convertCustomFds(options) {
390391
}
391392

392393
function normalizeSpawnArguments(file, args, options) {
393-
if (typeof file !== 'string')
394-
throw new ERR_INVALID_ARG_TYPE('file', 'string', file);
394+
validateString(file, 'file');
395395

396396
if (file.length === 0)
397397
throw new ERR_INVALID_ARG_VALUE('file', file, 'cannot be empty');

lib/dgram.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const {
3737
ERR_SOCKET_CANNOT_SEND,
3838
ERR_SOCKET_DGRAM_NOT_RUNNING
3939
} = errors.codes;
40+
const { validateString } = require('internal/validators');
4041
const { Buffer } = require('buffer');
4142
const util = require('util');
4243
const { isUint8Array } = require('internal/util/types');
@@ -269,9 +270,7 @@ Socket.prototype.sendto = function(buffer,
269270
throw new ERR_INVALID_ARG_TYPE('port', 'number', port);
270271
}
271272

272-
if (typeof address !== 'string') {
273-
throw new ERR_INVALID_ARG_TYPE('address', 'string', address);
274-
}
273+
validateString(address, 'address');
275274

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

571570
Socket.prototype.setMulticastInterface = function(interfaceAddress) {
572571
healthCheck(this);
573-
574-
if (typeof interfaceAddress !== 'string') {
575-
throw new ERR_INVALID_ARG_TYPE(
576-
'interfaceAddress', 'string', interfaceAddress);
577-
}
572+
validateString(interfaceAddress, 'interfaceAddress');
578573

579574
const err = this[kStateSymbol].handle.setMulticastInterface(interfaceAddress);
580575
if (err) {

lib/dns.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const {
3939
ERR_MISSING_ARGS,
4040
ERR_SOCKET_BAD_PORT
4141
} = errors.codes;
42+
const { validateString } = require('internal/validators');
4243

4344
const {
4445
GetAddrInfoReqWrap,
@@ -206,9 +207,8 @@ function resolver(bindingName) {
206207
callback = arguments[2];
207208
}
208209

209-
if (typeof name !== 'string') {
210-
throw new ERR_INVALID_ARG_TYPE('name', 'string', name);
211-
} else if (typeof callback !== 'function') {
210+
validateString(name, 'name');
211+
if (typeof callback !== 'function') {
212212
throw new ERR_INVALID_CALLBACK();
213213
}
214214

lib/inspector.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
ERR_INVALID_ARG_TYPE,
1010
ERR_INVALID_CALLBACK
1111
} = require('internal/errors').codes;
12+
const { validateString } = require('internal/validators');
1213
const util = require('util');
1314
const { Connection, open, url } = process.binding('inspector');
1415
const { originalConsole } = require('internal/process/per_thread');
@@ -58,9 +59,7 @@ class Session extends EventEmitter {
5859
}
5960

6061
post(method, params, callback) {
61-
if (typeof method !== 'string') {
62-
throw new ERR_INVALID_ARG_TYPE('method', 'string', method);
63-
}
62+
validateString(method, 'method');
6463
if (!callback && util.isFunction(params)) {
6564
callback = params;
6665
params = null;

lib/internal/child_process.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const {
1414
ERR_MISSING_ARGS
1515
}
1616
} = require('internal/errors');
17+
const { validateString } = require('internal/validators');
1718
const EventEmitter = require('events');
1819
const net = require('net');
1920
const dgram = require('dgram');
@@ -317,9 +318,7 @@ ChildProcess.prototype.spawn = function(options) {
317318
options.envPairs.push('NODE_CHANNEL_FD=' + ipcFd);
318319
}
319320

320-
if (typeof options.file !== 'string') {
321-
throw new ERR_INVALID_ARG_TYPE('options.file', 'string', options.file);
322-
}
321+
validateString(options.file, 'options.file');
323322
this.spawnfile = options.file;
324323

325324
if (Array.isArray(options.args))

lib/internal/crypto/cipher.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
ERR_INVALID_ARG_TYPE,
1111
ERR_INVALID_OPT_VALUE
1212
} = require('internal/errors').codes;
13+
const { validateString } = require('internal/validators');
1314

1415
const {
1516
getDefaultEncoding,
@@ -83,9 +84,7 @@ function createCipherBase(cipher, credential, options, decipher, iv) {
8384
}
8485

8586
function createCipher(cipher, password, options, decipher) {
86-
if (typeof cipher !== 'string')
87-
throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);
88-
87+
validateString(cipher, 'cipher');
8988
password = toBuf(password);
9089
if (!isArrayBufferView(password)) {
9190
throw new ERR_INVALID_ARG_TYPE(
@@ -99,9 +98,7 @@ function createCipher(cipher, password, options, decipher) {
9998
}
10099

101100
function createCipherWithIV(cipher, key, options, decipher, iv) {
102-
if (typeof cipher !== 'string')
103-
throw new ERR_INVALID_ARG_TYPE('cipher', 'string', cipher);
104-
101+
validateString(cipher, 'cipher');
105102
key = toBuf(key);
106103
if (!isArrayBufferView(key)) {
107104
throw new ERR_INVALID_ARG_TYPE(

lib/internal/crypto/diffiehellman.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const {
66
ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY,
77
ERR_INVALID_ARG_TYPE
88
} = require('internal/errors').codes;
9+
const { validateString } = require('internal/validators');
910
const { isArrayBufferView } = require('internal/util/types');
1011
const {
1112
getDefaultEncoding,
@@ -167,9 +168,7 @@ function ECDH(curve) {
167168
if (!(this instanceof ECDH))
168169
return new ECDH(curve);
169170

170-
if (typeof curve !== 'string')
171-
throw new ERR_INVALID_ARG_TYPE('curve', 'string', curve);
172-
171+
validateString(curve, 'curve');
173172
this._handle = new _ECDH(curve);
174173
}
175174

@@ -200,9 +199,7 @@ ECDH.convertKey = function convertKey(key, curve, inEnc, outEnc, format) {
200199
);
201200
}
202201

203-
if (typeof curve !== 'string') {
204-
throw new ERR_INVALID_ARG_TYPE('curve', 'string', curve);
205-
}
202+
validateString(curve, 'curve');
206203

207204
const encoding = getDefaultEncoding();
208205
inEnc = inEnc || encoding;

lib/internal/crypto/hash.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
ERR_CRYPTO_HASH_UPDATE_FAILED,
1919
ERR_INVALID_ARG_TYPE
2020
} = require('internal/errors').codes;
21+
const { validateString } = require('internal/validators');
2122
const { inherits } = require('util');
2223
const { normalizeEncoding } = require('internal/util');
2324
const { isArrayBufferView } = require('internal/util/types');
@@ -28,8 +29,7 @@ const kFinalized = Symbol('finalized');
2829
function Hash(algorithm, options) {
2930
if (!(this instanceof Hash))
3031
return new Hash(algorithm, options);
31-
if (typeof algorithm !== 'string')
32-
throw new ERR_INVALID_ARG_TYPE('algorithm', 'string', algorithm);
32+
validateString(algorithm, 'algorithm');
3333
this._handle = new _Hash(algorithm);
3434
this[kState] = {
3535
[kFinalized]: false
@@ -83,8 +83,7 @@ Hash.prototype.digest = function digest(outputEncoding) {
8383
function Hmac(hmac, key, options) {
8484
if (!(this instanceof Hmac))
8585
return new Hmac(hmac, key, options);
86-
if (typeof hmac !== 'string')
87-
throw new ERR_INVALID_ARG_TYPE('hmac', 'string', hmac);
86+
validateString(hmac, 'hmac');
8887
if (typeof key !== 'string' && !isArrayBufferView(key)) {
8988
throw new ERR_INVALID_ARG_TYPE('key',
9089
['string', 'TypedArray', 'DataView'], key);

lib/internal/crypto/sig.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
const {
44
ERR_CRYPTO_SIGN_KEY_REQUIRED,
5-
ERR_INVALID_ARG_TYPE,
65
ERR_INVALID_OPT_VALUE
76
} = require('internal/errors').codes;
7+
const { validateString } = require('internal/validators');
88
const {
99
Sign: _Sign,
1010
Verify: _Verify
@@ -24,8 +24,7 @@ const { inherits } = require('util');
2424
function Sign(algorithm, options) {
2525
if (!(this instanceof Sign))
2626
return new Sign(algorithm, options);
27-
if (typeof algorithm !== 'string')
28-
throw new ERR_INVALID_ARG_TYPE('algorithm', 'string', algorithm);
27+
validateString(algorithm, 'algorithm');
2928
this._handle = new _Sign();
3029
this._handle.init(algorithm);
3130

@@ -94,8 +93,7 @@ Sign.prototype.sign = function sign(options, encoding) {
9493
function Verify(algorithm, options) {
9594
if (!(this instanceof Verify))
9695
return new Verify(algorithm, options);
97-
if (typeof algorithm !== 'string')
98-
throw new ERR_INVALID_ARG_TYPE('algorithm', 'string', algorithm);
96+
validateString(algorithm, 'algorithm');
9997
this._handle = new _Verify();
10098
this._handle.init(algorithm);
10199

lib/internal/crypto/util.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
ERR_CRYPTO_TIMING_SAFE_EQUAL_LENGTH,
1818
ERR_INVALID_ARG_TYPE,
1919
} = require('internal/errors').codes;
20+
const { validateString } = require('internal/validators');
2021
const { Buffer } = require('buffer');
2122
const {
2223
cachedResult,
@@ -53,9 +54,7 @@ const getHashes = cachedResult(() => filterDuplicateStrings(_getHashes()));
5354
const getCurves = cachedResult(() => filterDuplicateStrings(_getCurves()));
5455

5556
function setEngine(id, flags) {
56-
if (typeof id !== 'string')
57-
throw new ERR_INVALID_ARG_TYPE('id', 'string', id);
58-
57+
validateString(id, 'id');
5958
if (flags && typeof flags !== 'number')
6059
throw new ERR_INVALID_ARG_TYPE('flags', 'number', flags);
6160
flags = flags >>> 0;

0 commit comments

Comments
 (0)