Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dns.promises typedef to node.js #8586

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 174 additions & 10 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;

declare function lookup(
domain: string,
options: number | LookupOptions,
Expand All @@ -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,
Expand All @@ -798,39 +852,149 @@ declare module "dns" {
callback: (err: ?Error, addresses: Array<any>) => void
): void;

declare function resolveAny(
domain: string,
callback: (err: ?Error, results: Array<AnyResult>) => void
): void;

declare function resolveCname(
domain: string,
callback: (err: ?Error, addresses: Array<any>) => void
callback: (err: ?Error, addresses: Array<string>) => void
): void;

declare function resolveCaa(
domain: string,
callback: (err: ?Error, results: Array<CaaResult>) => void
): void;

declare function resolveMx(
domain: string,
callback: (err: ?Error, addresses: Array<any>) => void
callback: (err: ?Error, results: Array<MxResult>) => void
): void;

declare function resolveNaptr(
domain: string,
callback: (err: ?Error, results: Array<NaptrResult>) => void
): void;

declare function resolveNs(
domain: string,
callback: (err: ?Error, addresses: Array<any>) => void
callback: (err: ?Error, addresses: Array<string>) => void
): void;

declare function resolvePtr(
domain: string,
callback: (err: ?Error, addresses: Array<string>) => 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<any>) => void
callback: (err: ?Error, addresses: Array<SrvResult>) => void
): void;

declare function resolveTxt(
domain: string,
callback: (err: ?Error, addresses: Array<any>) => void
callback: (err: ?Error, addresses: Array<[string, string]>) => void
): void;

declare function reverse(
ip: string,
callback: (err: ?Error, domains: Array<any>) => void
callback: (err: ?Error, domains: Array<string>) => 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<string>;

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<Array<any>>;

resolve4(
domain: string,
): Promise<Array<any>>;

resolve6(
domain: string,
): Promise<Array<any>>;

resolveAny(
domain: string
): Promise<Array<AnyResult>>;

resolveCaa(
domain: string,
): Promise<Array<CaaResult>>;

resolveCname(
domain: string,
): Promise<Array<string>>;

resolveMx(
domain: string,
): Promise<Array<MxResult>>;

resolveNaptr(
domain: string,
): Promise<Array<NaptrResult>>;

resolveNs(
domain: string,
): Promise<Array<string>>;

resolvePtr(
domain: string,
): Promise<Array<string>>;

resolveSoa(
domain: string,
): Promise<SoaResult>; // Note: not an array

resolveSrv(
domain: string,
): Promise<Array<SrvResult>>;

resolveTxt(
domain: string,
): Promise<Array<[string, string]>>;

reverse(
domain: string,
): Promise<Array<string>>;

setServers(
servers: string[]
): void
}

declare var promises: DNSPromise;
}

declare class events$EventEmitter {
Expand Down
17 changes: 17 additions & 0 deletions tests/node_tests/dns/dnsPromises.js
Original file line number Diff line number Diff line change
@@ -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
}