insomnia/app/backend/dns.js
Gregory Schier 84e38b5e41 Bump version
2016-10-26 13:20:26 -07:00

43 lines
863 B
JavaScript

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.
*
* @param url
* @returns {Promise}
*/
function lookupIPv6 (url) {
return new Promise((resolve, reject) => {
const {hostname} = urlParse(url);
const options = {
hints: dns.ADDRCONFIG,
family: 6,
};
dns.lookup(hostname, options, (err, results) => {
if (err) {
reject(err);
} else {
resolve(results);
}
});
})
}
export async function swapHost (url) {
let ip;
try {
ip = await lookupIPv6(url);
const parsedUrl = urlParse(url);
delete parsedUrl.host; // So it doesn't build with old host
parsedUrl.hostname = ip;
return urlFormat(parsedUrl);
} catch (e) {
// Fail silently. It's OK
return url;
}
}