mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 23:00:30 +00:00
43 lines
863 B
JavaScript
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;
|
|
}
|
|
}
|