Skip to content

Commit

Permalink
add support for { all: true } option on dns.lookup (#2846)
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari authored May 11, 2023
1 parent e125ed2 commit 9c166a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/bun.js/node-dns.exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ function lookup(domain, options, callback) {
}

dns.lookup(domain, options).then(
([{ address, family }]) => {
callback(null, address, family);
res => {
res.sort((a, b) => a.family - b.family);

if (options?.all) {
callback(null, res.map(mapLookupAll));
} else {
const [{ address, family }] = res;
callback(null, address, family);
}
},
error => {
callback(error);
Expand Down Expand Up @@ -453,6 +460,16 @@ const promisifyLookup = res => {
return { address, family };
};

const mapLookupAll = res => {
const { address, family } = res;
return { address, family };
};

const promisifyLookupAll = res => {
res.sort((a, b) => a.family - b.family);
return res.map(mapLookupAll);
};

const mapResolveX = a => a.address;

const promisifyResolveX = res => {
Expand All @@ -462,6 +479,9 @@ const promisifyResolveX = res => {
// promisified versions
export const promises = {
lookup(domain, options) {
if (options?.all) {
return dns.lookup(domain, options).then(promisifyLookupAll);
}
return dns.lookup(domain, options).then(promisifyLookup);
},

Expand Down
8 changes: 8 additions & 0 deletions test/js/node/dns/node-dns.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ test("dns.lookup (example.com)", done => {
});
});

test("dns.lookup (example.com) with { all: true } #2675", done => {
dns.lookup("example.com", { all: true }, (err, address, family) => {
expect(err).toBeNull();
expect(Array.isArray(address)).toBe(true);
done(err);
});
});

test("dns.lookup (localhost)", done => {
dns.lookup("localhost", (err, address, family) => {
expect(err).toBeNull();
Expand Down

0 comments on commit 9c166a1

Please sign in to comment.