Skip to content

Commit 7c77db0

Browse files
VoltrexKeyvatargos
authored andcommitted
dns: refactor and use validators
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>
1 parent f4292bc commit 7c77db0

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

lib/internal/dns/promises.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const {
2929
QueryReqWrap
3030
} = internalBinding('cares_wrap');
3131
const {
32-
ERR_INVALID_ARG_TYPE,
3332
ERR_INVALID_ARG_VALUE,
3433
ERR_MISSING_ARGS,
3534
} = codes;
@@ -45,7 +44,7 @@ function onlookup(err, addresses) {
4544
return;
4645
}
4746

48-
const family = this.family ? this.family : isIP(addresses[0]);
47+
const family = this.family || isIP(addresses[0]);
4948
this.resolve({ address: addresses[0], family });
5049
}
5150

@@ -62,7 +61,7 @@ function onlookupall(err, addresses) {
6261

6362
addresses[i] = {
6463
address,
65-
family: family ? family : isIP(addresses[i])
64+
family: family || isIP(addresses[i])
6665
};
6766
}
6867

@@ -108,9 +107,11 @@ function lookup(hostname, options) {
108107
var verbatim = getDefaultVerbatim();
109108

110109
// Parse arguments
111-
if (hostname && typeof hostname !== 'string') {
112-
throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname);
113-
} else if (options !== null && typeof options === 'object') {
110+
if (hostname) {
111+
validateString(hostname, 'hostname');
112+
}
113+
114+
if (options !== null && typeof options === 'object') {
114115
if (options.hints != null && typeof options.hints !== 'number') {
115116
emitTypeCoercionDeprecationWarning();
116117
}
@@ -257,15 +258,15 @@ Resolver.prototype.reverse = resolver('getHostByAddr');
257258
Resolver.prototype.resolve = function resolve(hostname, rrtype) {
258259
var resolver;
259260

260-
if (typeof rrtype === 'string') {
261+
if (rrtype !== undefined) {
262+
validateString(rrtype, 'rrtype');
263+
261264
resolver = resolveMap[rrtype];
262265

263266
if (typeof resolver !== 'function')
264267
throw new ERR_INVALID_ARG_VALUE('rrtype', rrtype);
265-
} else if (rrtype === undefined) {
266-
resolver = resolveMap.A;
267268
} else {
268-
throw new ERR_INVALID_ARG_TYPE('rrtype', 'string', rrtype);
269+
resolver = resolveMap.A;
269270
}
270271

271272
return ReflectApply(resolver, this, [hostname]);

0 commit comments

Comments
 (0)