Fix .local dns resolution when returning multiple addresses

The supervisor performs its own local resolution for `.local`
addresses due to a limitation in [musl](https://wiki.musl-libc.org/future-ideas.html).
The resolution function was not following exactly the nodejs [dns.lookup
specification](https://nodejs.org/api/dns.html#dnslookuphostname-options-callback)
which could cause certain clients to fail (in this case happy-eyeballs). This
updates the function to follow the specification.

Change-type: patch
This commit is contained in:
Felipe Lalanne 2021-12-16 11:31:55 -03:00
parent 52b47b91f0
commit 39c667803d

View File

@ -11,7 +11,10 @@ class DnsLookupError extends Error {
interface DnsLookupCallback { interface DnsLookupCallback {
(err: any): void; (err: any): void;
(err: undefined | null, address: string, family: number): void; (err: undefined | null, address: string, family: number): void;
(err: undefined | null, addresses: string[]): void; (
err: undefined | null,
addresses: Array<{ address: string; family: number }>,
): void;
} }
interface DnsLookupOpts { interface DnsLookupOpts {
@ -75,9 +78,9 @@ interface DnsLookupOpts {
.catch(() => { .catch(() => {
return ''; return '';
}) })
.then((addr) => { .then((address) => {
return { return {
addr, address,
family, family,
}; };
}); });
@ -87,7 +90,7 @@ interface DnsLookupOpts {
// resolve the addresses... // resolve the addresses...
return Promise.all(getResolvers()).then((results) => { return Promise.all(getResolvers()).then((results) => {
// remove any that didn't resolve... // remove any that didn't resolve...
let allAddresses = results.filter((result) => result.addr !== ''); let allAddresses = results.filter((result) => result.address !== '');
// unless the results should be returned verbatim, sort them so v4 comes first... // unless the results should be returned verbatim, sort them so v4 comes first...
if (opts && typeof opts !== 'number' && !opts.verbatim) { if (opts && typeof opts !== 'number' && !opts.verbatim) {
@ -102,15 +105,12 @@ interface DnsLookupOpts {
// all the addresses were requested... // all the addresses were requested...
if (opts && typeof opts !== 'number' && opts.all) { if (opts && typeof opts !== 'number' && opts.all) {
return cb( return cb(null, allAddresses);
null,
allAddresses.map((r) => r.addr),
);
} }
// only a single address was requested... // only a single address was requested...
const [{ addr, family }] = allAddresses; const [{ address, family }] = allAddresses;
return cb(null, addr, family); return cb(null, address, family);
}); });
} }