Skip to content

Commit

Permalink
dns: refactor and use validators
Browse files Browse the repository at this point in the history
The logical NOT operator and validators should be used where
appropriate.

PR-URL: #40022
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Qingyu Deng <i@ayase-lab.com>
  • Loading branch information
VoltrexKeyva authored and danielleadams committed Oct 12, 2021
1 parent 71a94aa commit 2d409ed
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions lib/internal/dns/promises.js
Expand Up @@ -28,7 +28,6 @@ const {
QueryReqWrap
} = internalBinding('cares_wrap');
const {
ERR_INVALID_ARG_TYPE,
ERR_INVALID_ARG_VALUE,
ERR_MISSING_ARGS,
} = codes;
Expand All @@ -44,7 +43,7 @@ function onlookup(err, addresses) {
return;
}

const family = this.family ? this.family : isIP(addresses[0]);
const family = this.family || isIP(addresses[0]);
this.resolve({ address: addresses[0], family });
}

Expand All @@ -61,7 +60,7 @@ function onlookupall(err, addresses) {

addresses[i] = {
address,
family: family ? family : isIP(addresses[i])
family: family || isIP(addresses[i])
};
}

Expand Down Expand Up @@ -107,9 +106,11 @@ function lookup(hostname, options) {
var verbatim = getDefaultVerbatim();

// Parse arguments
if (hostname && typeof hostname !== 'string') {
throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname);
} else if (options !== null && typeof options === 'object') {
if (hostname) {
validateString(hostname, 'hostname');
}

if (options !== null && typeof options === 'object') {
hints = options.hints >>> 0;
family = options.family >>> 0;
all = options.all === true;
Expand Down Expand Up @@ -242,15 +243,15 @@ Resolver.prototype.reverse = resolver('getHostByAddr');
Resolver.prototype.resolve = function resolve(hostname, rrtype) {
var resolver;

if (typeof rrtype === 'string') {
if (rrtype !== undefined) {
validateString(rrtype, 'rrtype');

resolver = resolveMap[rrtype];

if (typeof resolver !== 'function')
throw new ERR_INVALID_ARG_VALUE('rrtype', rrtype);
} else if (rrtype === undefined) {
resolver = resolveMap.A;
} else {
throw new ERR_INVALID_ARG_TYPE('rrtype', 'string', rrtype);
resolver = resolveMap.A;
}

return ReflectApply(resolver, this, [hostname]);
Expand Down

0 comments on commit 2d409ed

Please sign in to comment.