Skip to content

Commit

Permalink
implement resolvePtr
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Dec 5, 2024
1 parent 823c35f commit e3adaa7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/node/dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
resolve6,
resolveAny,
resolveNaptr,
resolvePtr,
resolveSoa,
resolveSrv,
setDefaultResultOrder,
Expand All @@ -33,7 +32,6 @@ export {
resolve6,
resolveAny,
resolveNaptr,
resolvePtr,
resolveSoa,
resolveSrv,
setDefaultResultOrder,
Expand All @@ -47,6 +45,7 @@ export const resolveCaa = callbackify(dns.resolveCaa.bind(dns));
export const resolveMx = callbackify(dns.resolveMx.bind(dns));
export const resolveCname = callbackify(dns.resolveCname.bind(dns));
export const resolveNs = callbackify(dns.resolveNs.bind(dns));
export const resolvePtr = callbackify(dns.resolvePtr.bind(dns));

export default {
getServers,
Expand Down
3 changes: 3 additions & 0 deletions src/node/dns/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
resolveMx,
resolveCname,
resolveNs,
resolvePtr,
} from 'node-internal:internal_dns';

export * from 'node-internal:internal_dns_constants';
Expand All @@ -16,6 +17,7 @@ export {
resolveMx,
resolveCname,
resolveNs,
resolvePtr,
} from 'node-internal:internal_dns';

export default {
Expand All @@ -25,5 +27,6 @@ export default {
resolveMx,
resolveCname,
resolveNs,
resolvePtr,
...errorCodes,
};
18 changes: 15 additions & 3 deletions src/node/internal/internal_dns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,28 @@ export function resolveNs(name: string): Promise<string[]> {
return sendDnsRequest(name, 'NS').then((json) => {
if (!('Answer' in json)) {
// DNS request should contain an "Answer" attribute, but it didn't.
throw new DnsError(name, errorCodes.NOTFOUND, 'queryMx');
throw new DnsError(name, errorCodes.NOTFOUND, 'queryNs');
}

// Cloudflare DNS appends "." at the end whereas Node.js doesn't.
return json.Answer.map((a) => a.data.slice(0, -1));
});
}

export function resolvePtr(): void {
throw new Error('Not implemented');
export function resolvePtr(name: string): Promise<string[]> {
validateString(name, 'name');

// Validation errors needs to be sync.
// Return a promise rather than using async qualifier.
return sendDnsRequest(name, 'PTR').then((json) => {
if (!('Answer' in json)) {
// DNS request should contain an "Answer" attribute, but it didn't.
throw new DnsError(name, errorCodes.NOTFOUND, 'queryPtr');
}

// Cloudflare DNS appends "." at the end whereas Node.js doesn't.
return json.Answer.map((a) => a.data.slice(0, -1));
});
}

export function resolveSoa(): void {
Expand Down
17 changes: 17 additions & 0 deletions src/workerd/api/node/tests/dns-nodejs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,20 @@ export const resolveNs = {
validateResult(await dnsPromises.resolveNs(addresses.NS_HOST));
},
};

// Tests are taken from
// https://github.com/nodejs/node/blob/d7fdbb994cda8b2e1da4240eb97270c6abbaa9dd/test/internet/test-dns.js#L268
export const resolvePtr = {
async test() {
function validateResult(result) {
ok(result.length > 0);

for (const item of result) {
ok(item);
strictEqual(typeof item, 'string');
}
}

validateResult(await dnsPromises.resolvePtr(addresses.PTR_HOST));
},
};

0 comments on commit e3adaa7

Please sign in to comment.