index.d.ts 4.8 KB

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