diff options
author | RaindropsSys <raindrops@equestria.dev> | 2023-11-17 23:25:29 +0100 |
---|---|---|
committer | RaindropsSys <raindrops@equestria.dev> | 2023-11-17 23:25:29 +0100 |
commit | 953ddd82e48dd206cef5ac94456549aed13b3ad5 (patch) | |
tree | 8f003106ee2e7f422e5a22d2ee04d0db302e66c0 /includes/external/addressbook/node_modules/cacheable-lookup | |
parent | 62a9199846b0c07c03218703b33e8385764f42d9 (diff) | |
download | pluralconnect-953ddd82e48dd206cef5ac94456549aed13b3ad5.tar.gz pluralconnect-953ddd82e48dd206cef5ac94456549aed13b3ad5.tar.bz2 pluralconnect-953ddd82e48dd206cef5ac94456549aed13b3ad5.zip |
Updated 30 files and deleted 2976 files (automated)
Diffstat (limited to 'includes/external/addressbook/node_modules/cacheable-lookup')
5 files changed, 0 insertions, 923 deletions
diff --git a/includes/external/addressbook/node_modules/cacheable-lookup/LICENSE b/includes/external/addressbook/node_modules/cacheable-lookup/LICENSE deleted file mode 100644 index 92498ed..0000000 --- a/includes/external/addressbook/node_modules/cacheable-lookup/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2019 Szymon Marczak - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/includes/external/addressbook/node_modules/cacheable-lookup/README.md b/includes/external/addressbook/node_modules/cacheable-lookup/README.md deleted file mode 100644 index d04be69..0000000 --- a/includes/external/addressbook/node_modules/cacheable-lookup/README.md +++ /dev/null @@ -1,245 +0,0 @@ -# cacheable-lookup - -> A cacheable [`dns.lookup(…)`](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback) that respects TTL :tada: - -[![Node CI](https://github.com/szmarczak/cacheable-lookup/workflows/Node%20CI/badge.svg)](https://github.com/szmarczak/cacheable-lookup/actions) -[![codecov](https://codecov.io/gh/szmarczak/cacheable-lookup/branch/master/graph/badge.svg)](https://codecov.io/gh/szmarczak/cacheable-lookup) -[![npm](https://img.shields.io/npm/dm/cacheable-lookup.svg)](https://www.npmjs.com/package/cacheable-lookup) -[![install size](https://packagephobia.now.sh/badge?p=cacheable-lookup)](https://packagephobia.now.sh/result?p=cacheable-lookup) - -Making lots of HTTP requests? You can save some time by caching DNS lookups :zap: - -## Usage - -### Using the `lookup` option - -```js -import http from 'node:http'; -import CacheableLookup from 'cacheable-lookup'; - -const cacheable = new CacheableLookup(); - -http.get('http://example.com', {lookup: cacheable.lookup}, response => { - // Handle the response here -}); -``` - -### Attaching CacheableLookup to an Agent - -```js -import http from 'node:http'; -import https from 'node:https'; -import CacheableLookup from 'cacheable-lookup'; - -const cacheable = new CacheableLookup(); - -cacheable.install(http.globalAgent); -cacheable.install(https.globalAgent); - -http.get('http://example.com', response => { - // Handle the response here -}); -``` - -## API - -### new CacheableLookup(options) - -Returns a new instance of `CacheableLookup`. - -#### options - -Type: `object`\ -Default: `{}` - -Options used to cache the DNS lookups. - -##### cache - -Type: `Map` | [`Keyv`](https://github.com/lukechilds/keyv/)\ -Default: `new Map()` - -Custom cache instance. If `undefined`, it will create a new one. - -**Note**: If you decide to use Keyv instead of the native implementation, the performance will drop by 10x. Memory leaks may occur as it doesn't provide any way to remove all the deprecated values at once. - -**Tip**: [`QuickLRU`](https://github.com/sindresorhus/quick-lru) is fully compatible with the Map API, you can use it to limit the amount of cached entries. Example: - -```js -import http from 'node:http'; -import CacheableLookup from 'cacheable-lookup'; -import QuickLRU from 'quick-lru'; - -const cacheable = new CacheableLookup({ - cache: new QuickLRU({maxSize: 1000}) -}); - -http.get('http://example.com', {lookup: cacheable.lookup}, response => { - // Handle the response here -}); -``` - -##### options.maxTtl - -Type: `number`\ -Default: `Infinity` - -The maximum lifetime of the entries received from the specifed DNS server (TTL in seconds). - -If set to `0`, it will make a new DNS query each time. - -**Pro Tip**: This shouldn't be lower than your DNS server response time in order to prevent bottlenecks. For example, if you use Cloudflare, this value should be greater than `0.01`. - -##### options.fallbackDuration - -Type: `number`\ -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). - -**Note**: You should avoid setting this to `0` unless the provided DNS servers' database is limited to few domains. - -##### options.errorTtl - -Type: `number`\ -Default: `0.15` - -The time how long it needs to remember queries that threw `ENOTFOUND` or `ENODATA` (TTL in seconds). - -**Note**: This option is independent, `options.maxTtl` does not affect this. - -**Pro Tip**: This shouldn't be lower than your DNS server response time in order to prevent bottlenecks. For example, if you use Cloudflare, this value should be greater than `0.01`. - -##### options.resolver - -Type: `dns.Resolver | dns.promises.Resolver`\ -Default: [`new dns.promises.Resolver()`](https://nodejs.org/api/dns.html#dns_class_dns_resolver) - -An instance of [DNS Resolver](https://nodejs.org/api/dns.html#dns_class_dns_resolver) used to make DNS queries. - -##### options.lookup - -Type: `Function`\ -Default: [`dns.lookup`](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback) - -The fallback function to use when the DNS server responds with `ENOTFOUND` or `ENODATA`. - -If you don't query internal hostnames (such as `localhost`, `database.local` etc.), it is strongly recommended to set this to `false`. - -### Entry object - -Type: `object` - -#### address - -Type: `string` - -The IP address (can be an IPv4 or IPv6 address). - -#### family - -Type: `number` - -The IP family (`4` or `6`). - -##### expires - -Type: `number` - -**Note**: This is not present when falling back to `dns.lookup(...)`! - -The timestamp (`Date.now() + ttl * 1000`) when the entry expires. - -#### ttl - -**Note**: This is not present when falling back to `dns.lookup(...)`! - -The time in seconds for its lifetime. - -#### source - -**Note**: This is not present when falling back to `dns.lookup(...)`! - -Whether this entry was loaded from the cache or came from a query (`cache` or `query`) - -### Entry object (callback-style) - -When `options.all` is `false`, then `callback(error, address, family, expires, ttl)` is called.\ -When `options.all` is `true`, then `callback(error, entries)` is called. - -### CacheableLookup instance - -#### servers - -Type: `Array` - -The DNS servers used to make queries. Can be overridden - doing so will clear the cache. - -#### [lookup(hostname, options, callback)](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback) - -#### lookupAsync(hostname, options) - -The asynchronous version of `dns.lookup(…)`. - -Returns an [entry object](#entry-object).\ -If `options.all` is true, returns an array of entry objects. - -##### hostname - -Type: `string` - -##### options - -Type: `object` - -The same as the [`dns.lookup(…)`](https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback) options. - -#### query(hostname) - -An asynchronous function which returns cached DNS lookup entries.\ -This is the base for `lookupAsync(hostname, options)` and `lookup(hostname, options, callback)`. - -**Note**: This function has no options. - -Returns an array of objects with `address`, `family`, `ttl` and `expires` properties. - -#### queryAndCache(hostname) - -An asynchronous function which makes two DNS queries: A and AAAA. The result is cached.\ -This is used by `query(hostname)` if no entry in the database is present. - -Returns an array of objects with `address`, `family`, `ttl` and `expires` properties. - -#### updateInterfaceInfo() - -Updates interface info. For example, you need to run this when you plug or unplug your WiFi driver. - -**Note:** Running `updateInterfaceInfo()` will trigger `clear()` only on network interface removal. - -#### clear(hostname?) - -Clears the cache for the given hostname. If the hostname argument is not present, the entire cache will be emptied. - -## High performance - -Performed on: -- Query: `example.com` -- CPU: i7-7700k -- CPU governor: performance - -``` -CacheableLookup#lookupAsync x 2,896,251 ops/sec ±1.07% (85 runs sampled) -CacheableLookup#lookupAsync.all x 2,842,664 ops/sec ±1.11% (88 runs sampled) -CacheableLookup#lookupAsync.all.ADDRCONFIG x 2,598,283 ops/sec ±1.21% (88 runs sampled) -CacheableLookup#lookup x 2,565,913 ops/sec ±1.56% (85 runs sampled) -CacheableLookup#lookup.all x 2,609,039 ops/sec ±1.01% (86 runs sampled) -CacheableLookup#lookup.all.ADDRCONFIG x 2,416,242 ops/sec ±0.89% (85 runs sampled) -dns#lookup x 7,272 ops/sec ±0.36% (86 runs sampled) -dns#lookup.all x 7,249 ops/sec ±0.40% (86 runs sampled) -dns#lookup.all.ADDRCONFIG x 5,693 ops/sec ±0.28% (85 runs sampled) -Fastest is CacheableLookup#lookupAsync.all -``` - -## Related - -- [cacheable-request](https://github.com/lukechilds/cacheable-request) - Wrap native HTTP requests with RFC compliant cache support diff --git a/includes/external/addressbook/node_modules/cacheable-lookup/index.d.ts b/includes/external/addressbook/node_modules/cacheable-lookup/index.d.ts deleted file mode 100644 index e805b70..0000000 --- a/includes/external/addressbook/node_modules/cacheable-lookup/index.d.ts +++ /dev/null @@ -1,144 +0,0 @@ -import {Resolver, promises as dnsPromises, lookup} from 'dns'; -import {Agent} from 'http'; - -type AsyncResolver = dnsPromises.Resolver; - -export type IPFamily = 4 | 6; -export type EntrySource = 'query' | 'cache'; - -type TPromise<T> = T | Promise<T>; - -export interface CacheInstance { - set(hostname: string, entries: EntryObject[], ttl: number): TPromise<void | boolean | this>; - get(hostname: string): TPromise<EntryObject[] | undefined>; - delete(hostname: string): TPromise<boolean>; - clear(): TPromise<void>; -} - -export interface Options { - /** - * Custom cache instance. If `undefined`, it will create a new one. - * @default undefined - */ - cache?: CacheInstance; - /** - * Limits the cache time (TTL). If set to `0`, it will make a new DNS query each time. - * @default Infinity - */ - maxTtl?: number; - /** - * DNS Resolver used to make DNS queries. - * @default new dns.promises.Resolver() - */ - resolver?: Resolver | AsyncResolver; - /** - * 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`. - * @default 3600 - */ - fallbackDuration?: number; - /** - * The time how long it needs to remember failed queries (TTL in seconds). - * - * **Note**: This option is independent, `options.maxTtl` does not affect this. - * @default 0.15 - */ - errorTtl?: number; - /** - * 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`. - * @default dns.lookup - */ - lookup?: typeof lookup | false; -} - -export interface EntryObject { - /** - * The IP address (can be an IPv4 or IPv5 address). - */ - readonly address: string; - /** - * The IP family. - */ - readonly family: IPFamily; - /** - * The original TTL. - */ - readonly ttl?: number; - /** - * The expiration timestamp. - */ - readonly expires?: number; - /** - * Whether this entry comes from the cache or a query - */ - readonly source?: EntrySource; -} - -export interface LookupOptions { - /** - * One or more supported getaddrinfo flags. Multiple flags may be passed by bitwise ORing their values. - */ - hints?: number; - /** - * The record family. Must be `4` or `6`. IPv4 and IPv6 addresses are both returned by default. - */ - family?: IPFamily; - /** - * When `true`, the callback returns all resolved addresses in an array. Otherwise, returns a single address. - * @default false - */ - all?: boolean; -} - -export default class CacheableLookup { - constructor(options?: Options); - /** - * The DNS servers used to make queries. Can be overridden - doing so will clear the cache. - */ - servers: string[]; - /** - * @see https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback - */ - lookup(hostname: string, family: IPFamily, callback: (error: NodeJS.ErrnoException | null, address: string, family: IPFamily) => void): void; - lookup(hostname: string, callback: (error: NodeJS.ErrnoException | null, address: string, family: IPFamily) => void): void; - lookup(hostname: string, options: LookupOptions & {all: true}, callback: (error: NodeJS.ErrnoException | null, result: ReadonlyArray<EntryObject>) => void): void; - lookup(hostname: string, options: LookupOptions, callback: (error: NodeJS.ErrnoException | null, address: string, family: IPFamily) => void): void; - /** - * The asynchronous version of `dns.lookup(…)`. - */ - lookupAsync(hostname: string, options: LookupOptions & {all: true}): Promise<ReadonlyArray<EntryObject>>; - lookupAsync(hostname: string, options: LookupOptions): Promise<EntryObject>; - lookupAsync(hostname: string): Promise<EntryObject>; - lookupAsync(hostname: string, family: IPFamily): Promise<EntryObject>; - /** - * An asynchronous function which returns cached DNS lookup entries. This is the base for `lookupAsync(hostname, options)` and `lookup(hostname, options, callback)`. - */ - query(hostname: string): Promise<ReadonlyArray<EntryObject>>; - /** - * An asynchronous function which makes a new DNS lookup query and updates the database. This is used by `query(hostname, family)` if no entry in the database is present. Returns an array of objects with `address`, `family`, `ttl` and `expires` properties. - */ - queryAndCache(hostname: string): Promise<ReadonlyArray<EntryObject>>; - /** - * Attaches itself to an Agent instance. - */ - install(agent: Agent): void; - /** - * Removes itself from an Agent instance. - */ - uninstall(agent: Agent): void; - /** - * Updates interface info. For example, you need to run this when you plug or unplug your WiFi driver. - * - * **Note:** Running `updateInterfaceInfo()` will trigger `clear()` only on network interface removal. - */ - updateInterfaceInfo(): void; - /** - * Clears the cache for the given hostname. If the hostname argument is not present, the entire cache will be emptied. - */ - clear(hostname?: string): void; -} diff --git a/includes/external/addressbook/node_modules/cacheable-lookup/package.json b/includes/external/addressbook/node_modules/cacheable-lookup/package.json deleted file mode 100644 index d3d696a..0000000 --- a/includes/external/addressbook/node_modules/cacheable-lookup/package.json +++ /dev/null @@ -1,61 +0,0 @@ -{ - "name": "cacheable-lookup", - "version": "7.0.0", - "description": "A cacheable dns.lookup(…) that respects TTL", - "engines": { - "node": ">=14.16" - }, - "files": [ - "source", - "index.d.ts" - ], - "type": "module", - "exports": { - "types": "./index.d.ts", - "default": "./source/index.js" - }, - "scripts": { - "//test": "xo && nyc --reporter=lcovonly --reporter=text ava && tsd", - "test": "tsd" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/szmarczak/cacheable-lookup.git" - }, - "keywords": [ - "dns", - "lookup", - "cacheable", - "ttl" - ], - "author": "Szymon Marczak", - "license": "MIT", - "bugs": { - "url": "https://github.com/szmarczak/cacheable-lookup/issues" - }, - "homepage": "https://github.com/szmarczak/cacheable-lookup#readme", - "devDependencies": { - "@types/keyv": "^3.1.1", - "ava": "^4.3.3", - "benchmark": "^2.1.4", - "coveralls": "^3.0.9", - "keyv": "^4.0.0", - "nyc": "^15.0.0", - "quibble": "^0.6.14", - "quick-lru": "^5.1.0", - "tsd": "^0.11.0", - "xo": "^0.25.3" - }, - "ava": { - "nodeArguments": [ - "--loader=quibble" - ] - }, - "xo": { - "rules": { - "unicorn/import-index": "off", - "import/extensions": "off", - "import/no-useless-path-segments": "off" - } - } -} diff --git a/includes/external/addressbook/node_modules/cacheable-lookup/source/index.js b/includes/external/addressbook/node_modules/cacheable-lookup/source/index.js deleted file mode 100644 index 2accc60..0000000 --- a/includes/external/addressbook/node_modules/cacheable-lookup/source/index.js +++ /dev/null @@ -1,452 +0,0 @@ -import { - V4MAPPED, - ADDRCONFIG, - ALL, - promises as dnsPromises, - lookup as dnsLookup -} from 'node:dns'; -import {promisify} from 'node:util'; -import os from 'node:os'; - -const {Resolver: AsyncResolver} = dnsPromises; - -const kCacheableLookupCreateConnection = Symbol('cacheableLookupCreateConnection'); -const kCacheableLookupInstance = Symbol('cacheableLookupInstance'); -const kExpires = Symbol('expires'); - -const supportsALL = typeof ALL === 'number'; - -const verifyAgent = agent => { - if (!(agent && typeof agent.createConnection === 'function')) { - throw new Error('Expected an Agent instance as the first argument'); - } -}; - -const map4to6 = entries => { - for (const entry of entries) { - if (entry.family === 6) { - continue; - } - - entry.address = `::ffff:${entry.address}`; - entry.family = 6; - } -}; - -const getIfaceInfo = () => { - let has4 = false; - let has6 = false; - - for (const device of Object.values(os.networkInterfaces())) { - for (const iface of device) { - if (iface.internal) { - continue; - } - - if (iface.family === 'IPv6') { - has6 = true; - } else { - has4 = true; - } - - if (has4 && has6) { - return {has4, has6}; - } - } - } - - return {has4, has6}; -}; - -const isIterable = map => { - return Symbol.iterator in map; -}; - -const ignoreNoResultErrors = dnsPromise => { - return dnsPromise.catch(error => { - if ( - error.code === 'ENODATA' || - error.code === 'ENOTFOUND' || - error.code === 'ENOENT' // Windows: name exists, but not this record type - ) { - return []; - } - - throw error; - }); -}; - -const ttl = {ttl: true}; -const all = {all: true}; -const all4 = {all: true, family: 4}; -const all6 = {all: true, family: 6}; - -export default class CacheableLookup { - constructor({ - cache = new Map(), - maxTtl = Infinity, - fallbackDuration = 3600, - errorTtl = 0.15, - resolver = new AsyncResolver(), - lookup = dnsLookup - } = {}) { - this.maxTtl = maxTtl; - this.errorTtl = errorTtl; - - this._cache = cache; - this._resolver = resolver; - this._dnsLookup = lookup && promisify(lookup); - this.stats = { - cache: 0, - query: 0 - }; - - if (this._resolver instanceof AsyncResolver) { - this._resolve4 = this._resolver.resolve4.bind(this._resolver); - this._resolve6 = this._resolver.resolve6.bind(this._resolver); - } else { - this._resolve4 = promisify(this._resolver.resolve4.bind(this._resolver)); - this._resolve6 = promisify(this._resolver.resolve6.bind(this._resolver)); - } - - this._iface = getIfaceInfo(); - - this._pending = {}; - this._nextRemovalTime = false; - this._hostnamesToFallback = new Set(); - - this.fallbackDuration = fallbackDuration; - - if (fallbackDuration > 0) { - const interval = setInterval(() => { - this._hostnamesToFallback.clear(); - }, fallbackDuration * 1000); - - /* istanbul ignore next: There is no `interval.unref()` when running inside an Electron renderer */ - if (interval.unref) { - interval.unref(); - } - - this._fallbackInterval = interval; - } - - this.lookup = this.lookup.bind(this); - this.lookupAsync = this.lookupAsync.bind(this); - } - - set servers(servers) { - this.clear(); - - this._resolver.setServers(servers); - } - - get servers() { - return this._resolver.getServers(); - } - - lookup(hostname, options, callback) { - if (typeof options === 'function') { - callback = options; - options = {}; - } else if (typeof options === 'number') { - options = { - family: options - }; - } - - if (!callback) { - throw new Error('Callback must be a function.'); - } - - // eslint-disable-next-line promise/prefer-await-to-then - this.lookupAsync(hostname, options).then(result => { - if (options.all) { - callback(null, result); - } else { - callback(null, result.address, result.family, result.expires, result.ttl, result.source); - } - }, callback); - } - - async lookupAsync(hostname, options = {}) { - if (typeof options === 'number') { - options = { - family: options - }; - } - - let cached = await this.query(hostname); - - if (options.family === 6) { - const filtered = cached.filter(entry => entry.family === 6); - - if (options.hints & V4MAPPED) { - if ((supportsALL && options.hints & ALL) || filtered.length === 0) { - map4to6(cached); - } else { - cached = filtered; - } - } else { - cached = filtered; - } - } else if (options.family === 4) { - cached = cached.filter(entry => entry.family === 4); - } - - if (options.hints & ADDRCONFIG) { - const {_iface} = this; - cached = cached.filter(entry => entry.family === 6 ? _iface.has6 : _iface.has4); - } - - if (cached.length === 0) { - const error = new Error(`cacheableLookup ENOTFOUND ${hostname}`); - error.code = 'ENOTFOUND'; - error.hostname = hostname; - - throw error; - } - - if (options.all) { - return cached; - } - - return cached[0]; - } - - async query(hostname) { - let source = 'cache'; - let cached = await this._cache.get(hostname); - - if (cached) { - this.stats.cache++; - } - - if (!cached) { - const pending = this._pending[hostname]; - if (pending) { - this.stats.cache++; - cached = await pending; - } else { - source = 'query'; - const newPromise = this.queryAndCache(hostname); - this._pending[hostname] = newPromise; - this.stats.query++; - try { - cached = await newPromise; - } finally { - delete this._pending[hostname]; - } - } - } - - cached = cached.map(entry => { - return {...entry, source}; - }); - - return cached; - } - - async _resolve(hostname) { - // ANY is unsafe as it doesn't trigger new queries in the underlying server. - const [A, AAAA] = await Promise.all([ - ignoreNoResultErrors(this._resolve4(hostname, ttl)), - ignoreNoResultErrors(this._resolve6(hostname, ttl)) - ]); - - let aTtl = 0; - let aaaaTtl = 0; - let cacheTtl = 0; - - const now = Date.now(); - - for (const entry of A) { - entry.family = 4; - entry.expires = now + (entry.ttl * 1000); - - aTtl = Math.max(aTtl, entry.ttl); - } - - for (const entry of AAAA) { - entry.family = 6; - entry.expires = now + (entry.ttl * 1000); - - aaaaTtl = Math.max(aaaaTtl, entry.ttl); - } - - if (A.length > 0) { - if (AAAA.length > 0) { - cacheTtl = Math.min(aTtl, aaaaTtl); - } else { - cacheTtl = aTtl; - } - } else { - cacheTtl = aaaaTtl; - } - - return { - entries: [ - ...A, - ...AAAA - ], - cacheTtl - }; - } - - async _lookup(hostname) { - try { - const [A, AAAA] = await Promise.all([ - // Passing {all: true} doesn't return all IPv4 and IPv6 entries. - // See https://github.com/szmarczak/cacheable-lookup/issues/42 - ignoreNoResultErrors(this._dnsLookup(hostname, all4)), - ignoreNoResultErrors(this._dnsLookup(hostname, all6)) - ]); - - return { - entries: [ - ...A, - ...AAAA - ], - cacheTtl: 0 - }; - } catch { - return { - entries: [], - cacheTtl: 0 - }; - } - } - - async _set(hostname, data, cacheTtl) { - if (this.maxTtl > 0 && cacheTtl > 0) { - cacheTtl = Math.min(cacheTtl, this.maxTtl) * 1000; - data[kExpires] = Date.now() + cacheTtl; - - try { - await this._cache.set(hostname, data, cacheTtl); - } catch (error) { - this.lookupAsync = async () => { - const cacheError = new Error('Cache Error. Please recreate the CacheableLookup instance.'); - cacheError.cause = error; - - throw cacheError; - }; - } - - if (isIterable(this._cache)) { - this._tick(cacheTtl); - } - } - } - - async queryAndCache(hostname) { - if (this._hostnamesToFallback.has(hostname)) { - return this._dnsLookup(hostname, all); - } - - let query = await this._resolve(hostname); - - if (query.entries.length === 0 && this._dnsLookup) { - query = await this._lookup(hostname); - - if (query.entries.length !== 0 && this.fallbackDuration > 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); - - return query.entries; - } - - _tick(ms) { - const nextRemovalTime = this._nextRemovalTime; - - if (!nextRemovalTime || ms < nextRemovalTime) { - clearTimeout(this._removalTimeout); - - this._nextRemovalTime = ms; - - this._removalTimeout = setTimeout(() => { - this._nextRemovalTime = false; - - let nextExpiry = Infinity; - - const now = Date.now(); - - for (const [hostname, entries] of this._cache) { - const expires = entries[kExpires]; - - if (now >= expires) { - this._cache.delete(hostname); - } else if (expires < nextExpiry) { - nextExpiry = expires; - } - } - - if (nextExpiry !== Infinity) { - this._tick(nextExpiry - now); - } - }, ms); - - /* istanbul ignore next: There is no `timeout.unref()` when running inside an Electron renderer */ - if (this._removalTimeout.unref) { - this._removalTimeout.unref(); - } - } - } - - install(agent) { - verifyAgent(agent); - - if (kCacheableLookupCreateConnection in agent) { - throw new Error('CacheableLookup has been already installed'); - } - - agent[kCacheableLookupCreateConnection] = agent.createConnection; - agent[kCacheableLookupInstance] = this; - - agent.createConnection = (options, callback) => { - if (!('lookup' in options)) { - options.lookup = this.lookup; - } - - return agent[kCacheableLookupCreateConnection](options, callback); - }; - } - - uninstall(agent) { - verifyAgent(agent); - - if (agent[kCacheableLookupCreateConnection]) { - if (agent[kCacheableLookupInstance] !== this) { - throw new Error('The agent is not owned by this CacheableLookup instance'); - } - - agent.createConnection = agent[kCacheableLookupCreateConnection]; - - delete agent[kCacheableLookupCreateConnection]; - delete agent[kCacheableLookupInstance]; - } - } - - updateInterfaceInfo() { - const {_iface} = this; - - this._iface = getIfaceInfo(); - - if ((_iface.has4 && !this._iface.has4) || (_iface.has6 && !this._iface.has6)) { - this._cache.clear(); - } - } - - clear(hostname) { - if (hostname) { - this._cache.delete(hostname); - return; - } - - this._cache.clear(); - } -} |