1 | import {Resolver, promises as dnsPromises, lookup} from 'dns';
|
2 | import {Agent} from 'http';
|
3 |
|
4 | type AsyncResolver = dnsPromises.Resolver;
|
5 |
|
6 | export type IPFamily = 4 | 6;
|
7 | export type EntrySource = 'query' | 'cache';
|
8 |
|
9 | type TPromise<T> = T | Promise<T>;
|
10 |
|
11 | export 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 |
|
18 | export interface Options {
|
19 | |
20 |
|
21 |
|
22 |
|
23 | cache?: CacheInstance;
|
24 | |
25 |
|
26 |
|
27 |
|
28 | maxTtl?: number;
|
29 | |
30 |
|
31 |
|
32 |
|
33 | resolver?: Resolver | AsyncResolver;
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | fallbackDuration?: number;
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | errorTtl?: number;
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | lookup?: typeof lookup | false;
|
57 | }
|
58 |
|
59 | export interface EntryObject {
|
60 | |
61 |
|
62 |
|
63 | readonly address: string;
|
64 | |
65 |
|
66 |
|
67 | readonly family: IPFamily;
|
68 | |
69 |
|
70 |
|
71 | readonly ttl?: number;
|
72 | |
73 |
|
74 |
|
75 | readonly expires?: number;
|
76 | |
77 |
|
78 |
|
79 | readonly source?: EntrySource;
|
80 | }
|
81 |
|
82 | export interface LookupOptions {
|
83 | |
84 |
|
85 |
|
86 | hints?: number;
|
87 | |
88 |
|
89 |
|
90 | family?: IPFamily;
|
91 | |
92 |
|
93 |
|
94 |
|
95 | all?: boolean;
|
96 | }
|
97 |
|
98 | export 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 | }
|