Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable _resolve for IP family if not requested #62

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 4 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
29 changes: 19 additions & 10 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class CacheableLookup {
};
}

let cached = await this.query(hostname);
let cached = await this.query(hostname, options);

if (options.family === 6) {
const filtered = cached.filter(entry => entry.family === 6);
Expand Down Expand Up @@ -210,7 +210,7 @@ class CacheableLookup {
return cached[0];
}

async query(hostname) {
async query(hostname, options) {
let cached = await this._cache.get(hostname);

if (!cached) {
Expand All @@ -219,7 +219,7 @@ class CacheableLookup {
if (pending) {
cached = await pending;
} else {
const newPromise = this.queryAndCache(hostname);
const newPromise = this.queryAndCache(hostname, options);
this._pending[hostname] = newPromise;

try {
Expand All @@ -237,12 +237,21 @@ class CacheableLookup {
return cached;
}

async _resolve(hostname) {
async _resolve(hostname, family) {
// ANY is unsafe as it doesn't trigger new queries in the underlying server.
const [A, AAAA] = await Promise.all([
ignoreNoResultErrors(this._resolve4(hostname, ttl)),
ignoreNoResultErrors(this._resolve6(hostname, ttl))
]);
let promiseArray = [];
if (family === 4) {
promiseArray.push(ignoreNoResultErrors(this._resolve4(hostname, ttl)), []);
} else if (family === 6) {
promiseArray.push([], ignoreNoResultErrors(this._resolve6(hostname, ttl)));
} else {
promiseArray.push(
ignoreNoResultErrors(this._resolve4(hostname, ttl)),
ignoreNoResultErrors(this._resolve6(hostname, ttl))
);
szmarczak marked this conversation as resolved.
Show resolved Hide resolved
}

let [A, AAAA] = await Promise.all(promiseArray);
szmarczak marked this conversation as resolved.
Show resolved Hide resolved

let aTtl = 0;
let aaaaTtl = 0;
Expand Down Expand Up @@ -329,12 +338,12 @@ class CacheableLookup {
}
}

async queryAndCache(hostname) {
async queryAndCache(hostname, options) {
if (this._hostnamesToFallback.has(hostname)) {
return this._dnsLookup(hostname, all);
}

let query = await this._resolve(hostname);
let query = await this._resolve(hostname, options.family);

if (query.entries.length === 0 && this._dnsLookup) {
query = await this._lookup(hostname);
Expand Down