diff --git a/lib/node.js b/lib/node.js index ab35b183367..2d4cb04ca0f 100644 --- a/lib/node.js +++ b/lib/node.js @@ -772,6 +772,54 @@ declare module "dns" { ... }; + // Rest of this object mirrors the result types below. + declare type AnyResult = { + type: 'A' | 'AAAA' | 'CNAME' | 'MX' | 'NAPTR' | 'NS' | 'PTR' | 'SOA' | 'SRV' | 'TXT', + ... + }; + + declare type CaaResult = { + critical: number, + issue?: string, + issuewild?: string, + iodef?: string + }; + + declare type MxResult = { + priority: number, + exchange: string + }; + + declare type NaptrResult = { + flags: string, + service: string, + regexp: string, + replacement: string, + order: number, + preference: number + }; + + declare type SoaResult = { + nsName: string, + hostmaster: string, + serial: number, + refresh: number, + retry: number, + expire: number, + minttl: number + }; + + declare type SrvResult = { + priority: number, + weight: number, + port: number, + name: string + }; + + declare function cancel(): void; + + declare function getServers(): Array; + declare function lookup( domain: string, options: number | LookupOptions, @@ -782,6 +830,12 @@ declare module "dns" { callback: (err: ?Error, address: string, family: number) => void ): void; + declare function lookupService( + address: string, + port: number, + callback: (err: ?Error, hostname: string, service: string) => void + ): void; + declare function resolve( domain: string, rrtype?: string, @@ -798,39 +852,149 @@ declare module "dns" { callback: (err: ?Error, addresses: Array) => void ): void; + declare function resolveAny( + domain: string, + callback: (err: ?Error, results: Array) => void + ): void; + declare function resolveCname( domain: string, - callback: (err: ?Error, addresses: Array) => void + callback: (err: ?Error, addresses: Array) => void + ): void; + + declare function resolveCaa( + domain: string, + callback: (err: ?Error, results: Array) => void ): void; declare function resolveMx( domain: string, - callback: (err: ?Error, addresses: Array) => void + callback: (err: ?Error, results: Array) => void + ): void; + + declare function resolveNaptr( + domain: string, + callback: (err: ?Error, results: Array) => void ): void; declare function resolveNs( domain: string, - callback: (err: ?Error, addresses: Array) => void + callback: (err: ?Error, addresses: Array) => void + ): void; + + declare function resolvePtr( + domain: string, + callback: (err: ?Error, addresses: Array) => void + ): void; + + declare function resolveSoa( + domain: string, + // Note: not an array + callback: (err: ?Error, result: SoaResult) => void ): void; declare function resolveSrv( domain: string, - callback: (err: ?Error, addresses: Array) => void + callback: (err: ?Error, addresses: Array) => void ): void; declare function resolveTxt( domain: string, - callback: (err: ?Error, addresses: Array) => void + callback: (err: ?Error, addresses: Array<[string, string]>) => void ): void; declare function reverse( ip: string, - callback: (err: ?Error, domains: Array) => void + callback: (err: ?Error, domains: Array) => void ): void; - declare function timingSafeEqual( - a: Buffer | $TypedArray | DataView, - b: Buffer | $TypedArray | DataView - ): boolean; + + // >= 15.1.0 + declare function setLocalAddress( + ipv4?: string, + ipv6?: string + ): void; + + declare function setServers( + servers: string[] + ): void + + declare class DNSPromise { + + getServers(): Array; + + lookup( + domain: string, + options?: number | LookupOptions + ): Promise<{address: string, family: number}>; + + lookupService( + address: string, + port: number, + ): Promise<{hostname: string, service: string}>; + + resolve( + domain: string, + rrtype?: string + ): Promise>; + + resolve4( + domain: string, + ): Promise>; + + resolve6( + domain: string, + ): Promise>; + + resolveAny( + domain: string + ): Promise>; + + resolveCaa( + domain: string, + ): Promise>; + + resolveCname( + domain: string, + ): Promise>; + + resolveMx( + domain: string, + ): Promise>; + + resolveNaptr( + domain: string, + ): Promise>; + + resolveNs( + domain: string, + ): Promise>; + + resolvePtr( + domain: string, + ): Promise>; + + resolveSoa( + domain: string, + ): Promise; // Note: not an array + + resolveSrv( + domain: string, + ): Promise>; + + resolveTxt( + domain: string, + ): Promise>; + + reverse( + domain: string, + ): Promise>; + + setServers( + servers: string[] + ): void + } + + declare var promises: DNSPromise; } declare class events$EventEmitter { diff --git a/tests/node_tests/dns/dnsPromises.js b/tests/node_tests/dns/dnsPromises.js new file mode 100644 index 00000000000..933c95b1268 --- /dev/null +++ b/tests/node_tests/dns/dnsPromises.js @@ -0,0 +1,17 @@ +var dns = require("dns"); + +/* lookup */ +async function lookup() { + type LookupResult = {address: string, family: number}; + + const result1: LookupResult = await dns.lookup("test.com"); + const result2: LookupResult = await dns.lookup("test.com", 6); + const result2: LookupResult = await dns.lookup("test.com", {family: 6}); + + // errors + await dns.lookup(); // error, hostname expected + await dns.lookup("test.com", (err, address, family) => {}); // error, no callback expected + await dns.lookup("test.com", null); // second param can't be null +} + +