forked from szmarczak/cacheable-lookup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
144 lines (136 loc) · 4.96 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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;
}