Skip to content

Commit

Permalink
lib: refactor to use validate function
Browse files Browse the repository at this point in the history
Throwing error after checking type is repeated. So replace
it with validate function.

PR-URL: #46101
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
deokjinkim authored and RafaelGSS committed Jan 20, 2023
1 parent 1aa4534 commit 1cfa2e6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 21 deletions.
7 changes: 3 additions & 4 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,10 @@ function ClientRequest(input, options, cb) {
this.maxHeaderSize = maxHeaderSize;

const insecureHTTPParser = options.insecureHTTPParser;
if (insecureHTTPParser !== undefined &&
typeof insecureHTTPParser !== 'boolean') {
throw new ERR_INVALID_ARG_TYPE(
'options.insecureHTTPParser', 'boolean', insecureHTTPParser);
if (insecureHTTPParser !== undefined) {
validateBoolean(insecureHTTPParser, 'options.insecureHTTPParser');
}

this.insecureHTTPParser = insecureHTTPParser;

if (options.joinDuplicateHeaders !== undefined) {
Expand Down
19 changes: 7 additions & 12 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -1224,21 +1224,16 @@ function Server(options, listener) {

validateNumber(this[kHandshakeTimeout], 'options.handshakeTimeout');

if (this[kSNICallback] && typeof this[kSNICallback] !== 'function') {
throw new ERR_INVALID_ARG_TYPE(
'options.SNICallback', 'function', options.SNICallback);
if (this[kSNICallback]) {
validateFunction(this[kSNICallback], 'options.SNICallback');
}

if (this[kPskCallback] && typeof this[kPskCallback] !== 'function') {
throw new ERR_INVALID_ARG_TYPE(
'options.pskCallback', 'function', options.pskCallback);
if (this[kPskCallback]) {
validateFunction(this[kPskCallback], 'options.pskCallback');
}
if (this[kPskIdentityHint] && typeof this[kPskIdentityHint] !== 'string') {
throw new ERR_INVALID_ARG_TYPE(
'options.pskIdentityHint',
'string',
options.pskIdentityHint
);

if (this[kPskIdentityHint]) {
validateString(this[kPskIdentityHint], 'options.pskIdentityHint');
}

// constructor call
Expand Down
7 changes: 2 additions & 5 deletions lib/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const {
const {
ERR_ASYNC_CALLBACK,
ERR_ASYNC_TYPE,
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ASYNC_ID
} = require('internal/errors').codes;
const { kEmptyObject } = require('internal/util');
Expand Down Expand Up @@ -280,10 +279,8 @@ class AsyncLocalStorage {
validateObject(options, 'options');

const { onPropagate = null } = options;
if (onPropagate !== null && typeof onPropagate !== 'function') {
throw new ERR_INVALID_ARG_TYPE('options.onPropagate',
'function',
onPropagate);
if (onPropagate !== null) {
validateFunction(onPropagate, 'options.onPropagate');
}

this.kResourceStore = Symbol('kResourceStore');
Expand Down

0 comments on commit 1cfa2e6

Please sign in to comment.