From 7c77db024394e94381c2d4664e1066f794dc33da Mon Sep 17 00:00:00 2001 From: Voltrex Date: Mon, 6 Sep 2021 23:43:01 +0430 Subject: [PATCH] dns: refactor and use validators The logical NOT operator and validators should be used where appropriate. PR-URL: https://github.com/nodejs/node/pull/40022 Reviewed-By: James M Snell Reviewed-By: Qingyu Deng --- lib/internal/dns/promises.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/internal/dns/promises.js b/lib/internal/dns/promises.js index 96330ecd600822..fcff215896ae8f 100644 --- a/lib/internal/dns/promises.js +++ b/lib/internal/dns/promises.js @@ -29,7 +29,6 @@ const { QueryReqWrap } = internalBinding('cares_wrap'); const { - ERR_INVALID_ARG_TYPE, ERR_INVALID_ARG_VALUE, ERR_MISSING_ARGS, } = codes; @@ -45,7 +44,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 }); } @@ -62,7 +61,7 @@ function onlookupall(err, addresses) { addresses[i] = { address, - family: family ? family : isIP(addresses[i]) + family: family || isIP(addresses[i]) }; } @@ -108,9 +107,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') { if (options.hints != null && typeof options.hints !== 'number') { emitTypeCoercionDeprecationWarning(); } @@ -257,15 +258,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]);