Skip to content

Commit

Permalink
Fix #36
Browse files Browse the repository at this point in the history
  • Loading branch information
szmarczak committed Feb 17, 2021
1 parent c257b04 commit 41f6229
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -94,7 +94,7 @@ Default: `3600` (1 hour)

When the DNS server responds with `ENOTFOUND` or `ENODATA` and the OS reports that the entry is available, it will use `dns.lookup(...)` directly for the requested hostnames for the specified amount of time (in seconds).

If you don't query internal hostnames (such as `localhost`, `database.local` etc.), it is strongly recommended to set this value to `0`.
**Note**: You should avoid setting this to `0` unless the provided DNS servers' database is limited to few domains.

##### options.errorTtl

Expand All @@ -121,7 +121,7 @@ Default: [`dns.lookup`](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_

The fallback function to use when the DNS server responds with `ENOTFOUND` or `ENODATA`.

**Note**: This has no effect if the `fallbackDuration` option is less than `1`.
If you don't query internal hostnames (such as `localhost`, `database.local` etc.), it is strongly recommended to set this to `false`.

### Entry object

Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -52,7 +52,7 @@ export interface Options {
* **Note**: This has no effect if the `fallbackDuration` option is less than `1`.
* @default dns.lookup
*/
lookup?: typeof lookup;
lookup?: typeof lookup | false;
}

export interface EntryObject {
Expand Down
4 changes: 4 additions & 0 deletions index.test-d.ts
Expand Up @@ -26,6 +26,10 @@ import CacheableLookup, {EntryObject} from '.';
lookup
});

new CacheableLookup({
lookup: false
});

expectType<string[]>(cacheable.servers);

expectType<EntryObject>(await cacheable.lookupAsync('localhost', 4));
Expand Down
14 changes: 7 additions & 7 deletions source/index.js
Expand Up @@ -80,7 +80,7 @@ class CacheableLookup {

this._cache = cache;
this._resolver = resolver;
this._dnsLookup = promisify(lookup);
this._dnsLookup = lookup && promisify(lookup);

if (this._resolver instanceof AsyncResolver) {
this._resolve4 = this._resolver.resolve4.bind(this._resolver);
Expand All @@ -96,11 +96,9 @@ class CacheableLookup {
this._nextRemovalTime = false;
this._hostnamesToFallback = new Set();

if (fallbackDuration < 1) {
this._fallback = false;
} else {
this._fallback = true;
this.fallbackDuration = fallbackDuration;

if (fallbackDuration > 0) {
const interval = setInterval(() => {
this._hostnamesToFallback.clear();
}, fallbackDuration * 1000);
Expand All @@ -109,6 +107,8 @@ class CacheableLookup {
if (interval.unref) {
interval.unref();
}

this._fallbackInterval = interval;
}

this.lookup = this.lookup.bind(this);
Expand Down Expand Up @@ -326,10 +326,10 @@ class CacheableLookup {

let query = await this._resolve(hostname);

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

if (query.entries.length !== 0) {
if (query.entries.length !== 0 && this.fallbackDuration > 0) {
// Use `dns.lookup(...)` for that particular hostname
this._hostnamesToFallback.add(hostname);
}
Expand Down
8 changes: 6 additions & 2 deletions tests/test.js
Expand Up @@ -769,7 +769,7 @@ test('custom cache support', async t => {
});

test.serial('fallback works', async t => {
const cacheable = new CacheableLookup({resolver, fallbackDuration: 3600});
const cacheable = new CacheableLookup({resolver, fallbackDuration: 0.1});
resolver.resetCounter();

const entries = await cacheable.lookupAsync('osHostname', {all: true});
Expand All @@ -790,6 +790,10 @@ test.serial('fallback works', async t => {
4: 1,
lookup: 2
});

await sleep(100);

t.is(cacheable._hostnamesToFallback.size, 0);
});

test.serial('fallback works if ip change', async t => {
Expand Down Expand Up @@ -865,7 +869,7 @@ test('real DNS queries first', async t => {
});

test('fallback can be turned off', async t => {
const cacheable = new CacheableLookup({resolver, fallbackDuration: 0});
const cacheable = new CacheableLookup({resolver, lookup: false});

await t.throwsAsync(cacheable.lookupAsync('osHostname', {all: true}), {
message: 'cacheableLookup ENOTFOUND osHostname'
Expand Down

0 comments on commit 41f6229

Please sign in to comment.