cacheable-lookup
Advanced tools
Comparing version 0.2.2 to 1.0.0
@@ -1,3 +0,4 @@ | ||
import * as Keyv from 'keyv'; | ||
import Keyv = require('keyv'); | ||
import {Resolver, LookupAddress} from 'dns'; | ||
import {Agent} from 'http'; | ||
@@ -86,3 +87,3 @@ type IPFamily = 4 | 6; | ||
*/ | ||
lookupAsync(hostname: string, options: LookupOptions & {all: true, details: true}): Promise<ReadonlyArray<EntryObject & {ttl: number, expires: number}>>; | ||
lookupAsync(hostname: string, options: LookupOptions & {all: true, details: true}): Promise<ReadonlyArray<EntryObject & {ttl: number, expires: number}>>; | ||
lookupAsync(hostname: string, options: LookupOptions & {all: true}): Promise<ReadonlyArray<EntryObject>>; | ||
@@ -101,2 +102,10 @@ lookupAsync(hostname: string, options: LookupOptions & {details: true}): Promise<EntryObject & {ttl: number, expires: number}>; | ||
queryAndCache(hostname: string, family: IPFamily): Promise<ReadonlyArray<EntryObject>>; | ||
/** | ||
* Attaches itself to an Agent instance. | ||
*/ | ||
install(agent: Agent): void; | ||
/** | ||
* Removes itself from an Agent instance. | ||
*/ | ||
uninstall(agent: Agent): void; | ||
} |
44
index.js
@@ -7,2 +7,11 @@ 'use strict'; | ||
const kCacheableLookupData = Symbol('cacheableLookupData'); | ||
const kCacheableLookupInstance = Symbol('cacheableLookupInstance'); | ||
const verifyAgent = agent => { | ||
if (!(agent && typeof agent.createConnection === 'function')) { | ||
throw new Error('Expected an Agent instance as the first argument'); | ||
} | ||
}; | ||
const map4to6 = entries => { | ||
@@ -73,2 +82,3 @@ for (const entry of entries) { | ||
// eslint-disable-next-line promise/prefer-await-to-then | ||
this.lookupAsync(hostname, {...options, throwNotFound: true}).then(result => { | ||
@@ -175,2 +185,36 @@ if (options.all) { | ||
} | ||
install(agent) { | ||
verifyAgent(agent); | ||
if (kCacheableLookupData in agent) { | ||
throw new Error('CacheableLookup has been already installed'); | ||
} | ||
agent[kCacheableLookupData] = agent.createConnection; | ||
agent[kCacheableLookupInstance] = this; | ||
agent.createConnection = (options, callback) => { | ||
if (!('lookup' in options)) { | ||
options.lookup = this.lookup; | ||
} | ||
return agent[kCacheableLookupData](options, callback); | ||
}; | ||
} | ||
uninstall(agent) { | ||
verifyAgent(agent); | ||
if (agent[kCacheableLookupData]) { | ||
if (agent[kCacheableLookupInstance] !== this) { | ||
throw new Error('The agent is not owned by this CacheableLookup instance'); | ||
} | ||
agent.createConnection = agent[kCacheableLookupData]; | ||
delete agent[kCacheableLookupData]; | ||
delete agent[kCacheableLookupInstance]; | ||
} | ||
} | ||
} | ||
@@ -177,0 +221,0 @@ |
{ | ||
"name": "cacheable-lookup", | ||
"version": "0.2.2", | ||
"version": "1.0.0", | ||
"description": "A cacheable dns.lookup(…) that respects the TTL", | ||
"engines": { | ||
"node": ">=10" | ||
}, | ||
"files": [ | ||
@@ -30,8 +33,8 @@ "index.js", | ||
"devDependencies": { | ||
"@types/keyv": "^3.1.0", | ||
"ava": "^3.0.0", | ||
"@types/keyv": "^3.1.1", | ||
"ava": "^3.1.0", | ||
"benchmark": "^2.1.4", | ||
"coveralls": "^3.0.2", | ||
"coveralls": "^3.0.9", | ||
"nyc": "^15.0.0", | ||
"proxyquire": "^2.1.0", | ||
"proxyquire": "^2.1.3", | ||
"tsd": "^0.11.0", | ||
@@ -38,0 +41,0 @@ "xo": "^0.25.3" |
@@ -15,3 +15,6 @@ # cacheable-lookup | ||
### Using the `lookup` option | ||
```js | ||
const http = require('http'); | ||
const CacheableLookup = require('cacheable-lookup'); | ||
@@ -25,2 +28,16 @@ const cacheable = new CacheableLookup(); | ||
### Attaching CacheableLookup to an Agent | ||
```js | ||
const http = require('http'); | ||
const CacheableLookup = require('cacheable-lookup'); | ||
const cacheable = new CacheableLookup(); | ||
cacheable.install(http.globalAgent); | ||
http.get('https://example.com', response => { | ||
// Handle the response here | ||
}); | ||
``` | ||
## API | ||
@@ -27,0 +44,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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
16941
276
0
181