Skip to content

Commit 2d409ed

Browse files
VoltrexKeyvadanielleadams
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 <[email protected]> Reviewed-By: Qingyu Deng <[email protected]>
1 parent 71a94aa commit 2d409ed

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
@@ -28,7 +28,6 @@ const {
2828
QueryReqWrap
2929
} = internalBinding('cares_wrap');
3030
const {
31-
ERR_INVALID_ARG_TYPE,
3231
ERR_INVALID_ARG_VALUE,
3332
ERR_MISSING_ARGS,
3433
} = codes;
@@ -44,7 +43,7 @@ function onlookup(err, addresses) {
4443
return;
4544
}
4645

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

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

6261
addresses[i] = {
6362
address,
64-
family: family ? family : isIP(addresses[i])
63+
family: family || isIP(addresses[i])
6564
};
6665
}
6766

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

109108
// Parse arguments
110-
if (hostname && typeof hostname !== 'string') {
111-
throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname);
112-
} else if (options !== null && typeof options === 'object') {
109+
if (hostname) {
110+
validateString(hostname, 'hostname');
111+
}
112+
113+
if (options !== null && typeof options === 'object') {
113114
hints = options.hints >>> 0;
114115
family = options.family >>> 0;
115116
all = options.all === true;
@@ -242,15 +243,15 @@ Resolver.prototype.reverse = resolver('getHostByAddr');
242243
Resolver.prototype.resolve = function resolve(hostname, rrtype) {
243244
var resolver;
244245

245-
if (typeof rrtype === 'string') {
246+
if (rrtype !== undefined) {
247+
validateString(rrtype, 'rrtype');
248+
246249
resolver = resolveMap[rrtype];
247250

248251
if (typeof resolver !== 'function')
249252
throw new ERR_INVALID_ARG_VALUE('rrtype', rrtype);
250-
} else if (rrtype === undefined) {
251-
resolver = resolveMap.A;
252253
} else {
253-
throw new ERR_INVALID_ARG_TYPE('rrtype', 'string', rrtype);
254+
resolver = resolveMap.A;
254255
}
255256

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

0 commit comments

Comments
 (0)