TransWikia.com

NodeJS - Custom DNS lookup, fallback to ipv4

Stack Overflow Asked by giyona43 on February 25, 2021

I’m trying to solve an issue with nodejs not being able to lookup the ip address when using ipv6 family: 6 in the Https.Agent options.

Error

Error: getaddrinfo ENOTFOUND raw.githubusercontent.com
  at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:66:26) {
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'raw.githubusercontent.com'

The solution I built is working, but seems very inefficient because I have to repeat the DNS lookup for ipv4 after the first one with ipv6 has failed. Is there a better way to solve this?

Code

function customLookup(hostname, options, callback) {
    return lookup(hostname, options, (error, address, family) => {
        if (error) {
            lookup(hostname, { ...options, family: 4 }, (error, address, family) => {
                callback(error, address, family);
            });
        } else {
            callback(error, address, family);
        }
    });
}
...

return new Https.Agent({
  keepAlive: true,
  family: options.family || 6,
  lookup: customLookup,
});

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP