Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ const {
} = require('internal/errors');
const {
bindDefaultResolver,
getDefaultResolver,
setDefaultResolver,
validateHints,
getDefaultResultOrder,
setDefaultResultOrder,
getUseCares,
errorCodes: dnsErrorCodes,
validDnsOrders,
validFamilies,
Expand Down Expand Up @@ -227,9 +229,13 @@ function lookup(hostname, options, callback) {
order = DNS_ORDER_IPV6_FIRST;
}

const err = cares.getaddrinfo(
req, hostname, family, hints, order,
);
let err;
if (getUseCares()) {
const resolver = getDefaultResolver();
err = resolver._handle.getaddrinfo(req, hostname, family, hints, order);
} else {
err = cares.getaddrinfo(req, hostname, family, hints, order);
}
if (err) {
process.nextTick(callback, new DNSException(err, 'getaddrinfo', hostname));
return {};
Expand Down
10 changes: 9 additions & 1 deletion lib/internal/dns/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ const {
const {
bindDefaultResolver,
createResolverClass,
getDefaultResolver,
validateHints,
errorCodes: dnsErrorCodes,
getDefaultResultOrder,
setDefaultResultOrder,
setDefaultResolver,
getUseCares,
validDnsOrders,
validFamilies,
} = require('internal/dns/utils');
Expand Down Expand Up @@ -163,7 +165,13 @@ function createLookupPromise(family, hostname, all, hints, dnsOrder) {
order = DNS_ORDER_IPV6_FIRST;
}

const err = getaddrinfo(req, hostname, family, hints, order);
let err;
if (getUseCares()) {
const resolver = getDefaultResolver();
err = resolver._handle.getaddrinfo(req, hostname, family, hints, order);
} else {
err = getaddrinfo(req, hostname, family, hints, order);
}

if (err) {
reject(new DNSException(err, 'getaddrinfo', hostname));
Expand Down
41 changes: 35 additions & 6 deletions lib/internal/dns/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const {
} = require('internal/errors');
const { isIP } = require('internal/net');
const { getOptionValue } = require('internal/options');
const { emitExperimentalWarning } = require('internal/util');
const {
validateArray,
validateInt32,
Expand Down Expand Up @@ -62,6 +63,12 @@ function validateTries(options) {
return tries;
}

function validateCacheMaxTTL(options) {
const { cacheMaxTTL = defaultCacheMaxTTL } = { ...options };
validateUint32(cacheMaxTTL, 'options.cacheMaxTTL');
return cacheMaxTTL;
}

const kSerializeResolver = Symbol('dns:resolver:serialize');
const kDeserializeResolver = Symbol('dns:resolver:deserialize');
const kSnapshotStates = Symbol('dns:resolver:config');
Expand All @@ -75,17 +82,21 @@ class ResolverBase {
const timeout = validateTimeout(options);
const tries = validateTries(options);
const maxTimeout = validateMaxTimeout(options);
const cacheMaxTTL = validateCacheMaxTTL(options);
if (cacheMaxTTL > 0 && options?.cacheMaxTTL !== undefined) {
emitExperimentalWarning('dns.Resolver cacheMaxTTL');
}
// If we are building snapshot, save the states of the resolver along
// the way.
if (isBuildingSnapshot()) {
this[kSnapshotStates] = { timeout, tries, maxTimeout };
this[kSnapshotStates] = { timeout, tries, maxTimeout, cacheMaxTTL };
}
this[kInitializeHandle](timeout, tries, maxTimeout);
this[kInitializeHandle](timeout, tries, maxTimeout, cacheMaxTTL);
}

[kInitializeHandle](timeout, tries, maxTimeout) {
[kInitializeHandle](timeout, tries, maxTimeout, cacheMaxTTL) {
const { ChannelWrap } = lazyBinding();
this._handle = new ChannelWrap(timeout, tries, maxTimeout);
this._handle = new ChannelWrap(timeout, tries, maxTimeout, cacheMaxTTL);
}

cancel() {
Expand Down Expand Up @@ -195,8 +206,8 @@ class ResolverBase {
}

[kDeserializeResolver]() {
const { timeout, tries, maxTimeout, localAddress, servers } = this[kSnapshotStates];
this[kInitializeHandle](timeout, tries, maxTimeout);
const { timeout, tries, maxTimeout, cacheMaxTTL, localAddress, servers } = this[kSnapshotStates];
this[kInitializeHandle](timeout, tries, maxTimeout, cacheMaxTTL);
if (localAddress) {
const { ipv4, ipv6 } = localAddress;
this._handle.setLocalAddress(ipv4, ipv6);
Expand All @@ -209,6 +220,8 @@ class ResolverBase {

let defaultResolver;
let dnsOrder;
let defaultCacheMaxTTL = 0;
let useCares = false;
const validDnsOrders = ['verbatim', 'ipv4first', 'ipv6first'];
const validFamilies = [0, 4, 6];

Expand All @@ -222,6 +235,17 @@ function initializeDns() {
dnsOrder = orderFromCLI;
}

const cacheMaxTTLFromCLI = getOptionValue('--experimental-dns-cache-max-ttl');
if (cacheMaxTTLFromCLI > 0) {
emitExperimentalWarning('--experimental-dns-cache-max-ttl');
defaultCacheMaxTTL = cacheMaxTTLFromCLI;
}

if (getOptionValue('--experimental-dns-lookup-cares')) {
emitExperimentalWarning('--experimental-dns-lookup-cares');
useCares = true;
}

if (!isBuildingSnapshot()) {
return;
}
Expand Down Expand Up @@ -340,6 +364,10 @@ const errorCodes = {
CANCELLED: 'ECANCELLED',
};

function getUseCares() {
return useCares;
}

module.exports = {
bindDefaultResolver,
getDefaultResolver,
Expand All @@ -349,6 +377,7 @@ module.exports = {
validateTries,
getDefaultResultOrder,
setDefaultResultOrder,
getUseCares,
errorCodes,
createResolverClass,
initializeDns,
Expand Down
Loading