Better DNS

This commit is contained in:
Gregory Schier committed 2016-10-26 13:25:34 -07:00
1 parent 84e38b5e41
commit cd5869ceba
1 file changed
+14 -6
+14 -6
View File
@@ -2,25 +2,33 @@ import dns from 'dns';
import {parse as urlParse, format as urlFormat} from 'url';
/**
* Try to find a supported IPv6 address. Will throw if none found.
* Resolve to IP address and prefer IPv6 (only if supported)
*
* @param url
* @returns {Promise}
*/
function lookupIPv6 (url) {
function lookup (url) {
return new Promise((resolve, reject) => {
const {hostname} = urlParse(url);
const options = {
hints: dns.ADDRCONFIG,
family: 6,
hints: dns.ADDRCONFIG, // Only lookup supported addresses
all: true,
};
dns.lookup(hostname, options, (err, results) => {
if (err) {
reject(err);
return;
}
const v6 = results.find(r => r.family === 6);
const v4 = results.find(r => r.family === 4);
if (v6) {
resolve(v6.address);
} else {
resolve(results);
resolve(v4.address);
}
});
})
@@ -30,7 +38,7 @@ export async function swapHost (url) {
let ip;
try {
ip = await lookupIPv6(url);
ip = await lookup(url);
const parsedUrl = urlParse(url);
delete parsedUrl.host; // So it doesn't build with old host
parsedUrl.hostname = ip;