From 41f6229cc09c298b3588d9b5fc81b54b1dfac0d6 Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Wed, 17 Feb 2021 14:10:29 +0100 Subject: [PATCH] Fix #36 --- README.md | 4 ++-- index.d.ts | 2 +- index.test-d.ts | 4 ++++ source/index.js | 14 +++++++------- tests/test.js | 8 ++++++-- 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 2507155..c727c4d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/index.d.ts b/index.d.ts index 528b1e2..bc6ba0b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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 { diff --git a/index.test-d.ts b/index.test-d.ts index 9fdb32a..17547c9 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -26,6 +26,10 @@ import CacheableLookup, {EntryObject} from '.'; lookup }); + new CacheableLookup({ + lookup: false + }); + expectType(cacheable.servers); expectType(await cacheable.lookupAsync('localhost', 4)); diff --git a/source/index.js b/source/index.js index 21f731e..642060b 100644 --- a/source/index.js +++ b/source/index.js @@ -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); @@ -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); @@ -109,6 +107,8 @@ class CacheableLookup { if (interval.unref) { interval.unref(); } + + this._fallbackInterval = interval; } this.lookup = this.lookup.bind(this); @@ -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); } diff --git a/tests/test.js b/tests/test.js index 87cffe0..de49005 100644 --- a/tests/test.js +++ b/tests/test.js @@ -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}); @@ -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 => { @@ -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'