UNPKG

5.08 kBTypeScriptView Raw
1import {Resolver, promises as dnsPromises, lookup} from 'dns';
2import {Agent} from 'http';
3
4type AsyncResolver = dnsPromises.Resolver;
5
6export type IPFamily = 4 | 6;
7export type EntrySource = 'query' | 'cache';
8
9type TPromise<T> = T | Promise<T>;
10
11export interface CacheInstance {
12 set(hostname: string, entries: EntryObject[], ttl: number): TPromise<void | boolean | this>;
13 get(hostname: string): TPromise<EntryObject[] | undefined>;
14 delete(hostname: string): TPromise<boolean>;
15 clear(): TPromise<void>;
16}
17
18export interface Options {
19 /**
20 * Custom cache instance. If `undefined`, it will create a new one.
21 * @default undefined
22 */
23 cache?: CacheInstance;
24 /**
25 * Limits the cache time (TTL). If set to `0`, it will make a new DNS query each time.
26 * @default Infinity
27 */
28 maxTtl?: number;
29 /**
30 * DNS Resolver used to make DNS queries.
31 * @default new dns.promises.Resolver()
32 */
33 resolver?: Resolver | AsyncResolver;
34 /**
35 * When the DNS server responds with `ENOTFOUND` or `ENODATA` and the OS reports that the entry is available,
36 * it will use `dns.lookup(...)` directly for the requested hostnames for the specified amount of time (in seconds).
37 *
38 * If you don't query internal hostnames (such as `localhost`, `database.local` etc.),
39 * it is strongly recommended to set this value to `0`.
40 * @default 3600
41 */
42 fallbackDuration?: number;
43 /**
44 * The time how long it needs to remember failed queries (TTL in seconds).
45 *
46 * **Note**: This option is independent, `options.maxTtl` does not affect this.
47 * @default 0.15
48 */
49 errorTtl?: number;
50 /**
51 * The fallback function to use when the DNS server responds with `ENOTFOUND` or `ENODATA`.
52 *
53 * **Note**: This has no effect if the `fallbackDuration` option is less than `1`.
54 * @default dns.lookup
55 */
56 lookup?: typeof lookup | false;
57}
58
59export interface EntryObject {
60 /**
61 * The IP address (can be an IPv4 or IPv5 address).
62 */
63 readonly address: string;
64 /**
65 * The IP family.
66 */
67 readonly family: IPFamily;
68 /**
69 * The original TTL.
70 */
71 readonly ttl?: number;
72 /**
73 * The expiration timestamp.
74 */
75 readonly expires?: number;
76 /**
77 * Whether this entry comes from the cache or a query
78 */
79 readonly source?: EntrySource;
80}
81
82export interface LookupOptions {
83 /**
84 * One or more supported getaddrinfo flags. Multiple flags may be passed by bitwise ORing their values.
85 */
86 hints?: number;
87 /**
88 * The record family. Must be `4` or `6`. IPv4 and IPv6 addresses are both returned by default.
89 */
90 family?: IPFamily;
91 /**
92 * When `true`, the callback returns all resolved addresses in an array. Otherwise, returns a single address.
93 * @default false
94 */
95 all?: boolean;
96}
97
98export default class CacheableLookup {
99 constructor(options?: Options);
100 /**
101 * The DNS servers used to make queries. Can be overridden - doing so will clear the cache.
102 */
103 servers: string[];
104 /**
105 * @see https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback
106 */
107 lookup(hostname: string, family: IPFamily, callback: (error: NodeJS.ErrnoException | null, address: string, family: IPFamily) => void): void;
108 lookup(hostname: string, callback: (error: NodeJS.ErrnoException | null, address: string, family: IPFamily) => void): void;
109 lookup(hostname: string, options: LookupOptions & {all: true}, callback: (error: NodeJS.ErrnoException | null, result: ReadonlyArray<EntryObject>) => void): void;
110 lookup(hostname: string, options: LookupOptions, callback: (error: NodeJS.ErrnoException | null, address: string, family: IPFamily) => void): void;
111 /**
112 * The asynchronous version of `dns.lookup()`.
113 */
114 lookupAsync(hostname: string, options: LookupOptions & {all: true}): Promise<ReadonlyArray<EntryObject>>;
115 lookupAsync(hostname: string, options: LookupOptions): Promise<EntryObject>;
116 lookupAsync(hostname: string): Promise<EntryObject>;
117 lookupAsync(hostname: string, family: IPFamily): Promise<EntryObject>;
118 /**
119 * An asynchronous function which returns cached DNS lookup entries. This is the base for `lookupAsync(hostname, options)` and `lookup(hostname, options, callback)`.
120 */
121 query(hostname: string): Promise<ReadonlyArray<EntryObject>>;
122 /**
123 * 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.
124 */
125 queryAndCache(hostname: string): Promise<ReadonlyArray<EntryObject>>;
126 /**
127 * Attaches itself to an Agent instance.
128 */
129 install(agent: Agent): void;
130 /**
131 * Removes itself from an Agent instance.
132 */
133 uninstall(agent: Agent): void;
134 /**
135 * Updates interface info. For example, you need to run this when you plug or unplug your WiFi driver.
136 *
137 * **Note:** Running `updateInterfaceInfo()` will trigger `clear()` only on network interface removal.
138 */
139 updateInterfaceInfo(): void;
140 /**
141 * Clears the cache for the given hostname. If the hostname argument is not present, the entire cache will be emptied.
142 */
143 clear(hostname?: string): void;
144}