cacheable-lookup
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -94,2 +94,6 @@ import Keyv = require('keyv'); | ||
/** | ||
* Removes outdated entries. | ||
*/ | ||
tick(): void; | ||
/** | ||
* Attaches itself to an Agent instance. | ||
@@ -96,0 +100,0 @@ */ |
43
index.js
@@ -8,3 +8,3 @@ 'use strict'; | ||
const kCacheableLookupData = Symbol('cacheableLookupData'); | ||
const kCacheableLookupCreateConnection = Symbol('cacheableLookupCreateConnection'); | ||
const kCacheableLookupInstance = Symbol('cacheableLookupInstance'); | ||
@@ -76,2 +76,7 @@ | ||
delete(key) { | ||
this.values.delete(key); | ||
this.expiries.delete(key); | ||
} | ||
clear() { | ||
@@ -105,2 +110,3 @@ this.values.clear(); | ||
this._iface = getIfaceInfo(); | ||
this._tickLocked = false; | ||
@@ -233,10 +239,30 @@ this.lookup = this.lookup.bind(this); | ||
tick() { | ||
if (this._tickLocked) { | ||
return; | ||
} | ||
const now = Date.now(); | ||
for (const [hostname, expiry] of this._cache.expiries) { | ||
if (now > expiry) { | ||
this._cache.delete(hostname); | ||
} | ||
} | ||
this._tickLocked = true; | ||
setTimeout(() => { | ||
this._tickLocked = false; | ||
}, 1000).unref(); | ||
} | ||
install(agent) { | ||
verifyAgent(agent); | ||
if (kCacheableLookupData in agent) { | ||
if (kCacheableLookupCreateConnection in agent) { | ||
throw new Error('CacheableLookup has been already installed'); | ||
} | ||
agent[kCacheableLookupData] = agent.createConnection; | ||
agent[kCacheableLookupCreateConnection] = agent.createConnection; | ||
agent[kCacheableLookupInstance] = this; | ||
@@ -247,5 +273,8 @@ | ||
options.lookup = this.lookup; | ||
// Make sure the database is up to date | ||
this.tick(); | ||
} | ||
return agent[kCacheableLookupData](options, callback); | ||
return agent[kCacheableLookupCreateConnection](options, callback); | ||
}; | ||
@@ -257,3 +286,3 @@ } | ||
if (agent[kCacheableLookupData]) { | ||
if (agent[kCacheableLookupCreateConnection]) { | ||
if (agent[kCacheableLookupInstance] !== this) { | ||
@@ -263,5 +292,5 @@ throw new Error('The agent is not owned by this CacheableLookup instance'); | ||
agent.createConnection = agent[kCacheableLookupData]; | ||
agent.createConnection = agent[kCacheableLookupCreateConnection]; | ||
delete agent[kCacheableLookupData]; | ||
delete agent[kCacheableLookupCreateConnection]; | ||
delete agent[kCacheableLookupInstance]; | ||
@@ -268,0 +297,0 @@ } |
{ | ||
"name": "cacheable-lookup", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "A cacheable dns.lookup(…) that respects the TTL", | ||
@@ -5,0 +5,0 @@ "engines": { |
@@ -159,2 +159,6 @@ # cacheable-lookup | ||
#### tick() | ||
Removes outdated entries. | ||
#### updateInterfaceInfo() | ||
@@ -161,0 +165,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18694
339
206