cacheable-lookup
Advanced tools
Comparing version 5.0.2 to 5.0.3
{ | ||
"name": "cacheable-lookup", | ||
"version": "5.0.2", | ||
"version": "5.0.3", | ||
"description": "A cacheable dns.lookup(…) that respects the TTL", | ||
@@ -5,0 +5,0 @@ "engines": { |
@@ -274,4 +274,3 @@ 'use strict'; | ||
], | ||
cacheTtl, | ||
isLookup: false | ||
cacheTtl | ||
}; | ||
@@ -281,12 +280,2 @@ } | ||
async _lookup(hostname) { | ||
const empty = { | ||
entries: [], | ||
cacheTtl: 0, | ||
isLookup: true | ||
}; | ||
if (!this._fallback) { | ||
return empty; | ||
} | ||
try { | ||
@@ -299,7 +288,9 @@ const entries = await this._dnsLookup(hostname, { | ||
entries, | ||
cacheTtl: 0, | ||
isLookup: true | ||
cacheTtl: 0 | ||
}; | ||
} catch (_) { | ||
return empty; | ||
return { | ||
entries: [], | ||
cacheTtl: 0 | ||
}; | ||
} | ||
@@ -335,20 +326,20 @@ } | ||
const resolverPromise = this._resolve(hostname); | ||
const lookupPromise = this._lookup(hostname); | ||
try { | ||
let query = await this._resolve(hostname); | ||
let query; | ||
if (query.entries.length === 0 && this._fallback) { | ||
query = await this._lookup(hostname); | ||
try { | ||
query = await Promise.race([ | ||
resolverPromise, | ||
lookupPromise | ||
]); | ||
if (query.entries.length === 0) { | ||
if (query.isLookup) { | ||
query = await resolverPromise; | ||
} else { | ||
query = await lookupPromise; | ||
if (query.entries.length !== 0) { | ||
// Use `dns.lookup(...)` for that particular hostname | ||
this._hostnamesToFallback.add(hostname); | ||
} | ||
} | ||
const cacheTtl = query.entries.length === 0 ? this.errorTtl : query.cacheTtl; | ||
await this._set(hostname, query.entries, cacheTtl); | ||
delete this._pending[hostname]; | ||
return query.entries; | ||
} catch (error) { | ||
@@ -359,26 +350,2 @@ delete this._pending[hostname]; | ||
} | ||
(async () => { | ||
if (query.isLookup) { | ||
try { | ||
const realDnsQuery = await resolverPromise; | ||
// If no DNS entries found | ||
if (realDnsQuery.entries.length === 0) { | ||
// Use `dns.lookup(...)` for that particular hostname | ||
this._hostnamesToFallback.add(hostname); | ||
} else { | ||
await this._set(hostname, realDnsQuery.entries, realDnsQuery.cacheTtl); | ||
} | ||
} catch (_) {} | ||
} else { | ||
const cacheTtl = query.entries.length === 0 ? this.errorTtl : query.cacheTtl; | ||
await this._set(hostname, query.entries, cacheTtl); | ||
} | ||
delete this._pending[hostname]; | ||
})(); | ||
return query.entries; | ||
} | ||
@@ -385,0 +352,0 @@ |
23976
479