mirror of
https://github.com/Kong/insomnia
synced 2024-11-08 14:49:53 +00:00
27 lines
601 B
JavaScript
27 lines
601 B
JavaScript
|
import dns from 'dns';
|
||
|
import {parse as urlParse, format as urlFormat} from 'url';
|
||
|
|
||
|
function lookup (url) {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
const {hostname} = urlParse(url);
|
||
|
dns.lookup(hostname, (err, ip) => {
|
||
|
err ? reject(err) : resolve(ip)
|
||
|
});
|
||
|
})
|
||
|
}
|
||
|
|
||
|
export async function swapHost (url) {
|
||
|
let ip;
|
||
|
|
||
|
try {
|
||
|
ip = await lookup(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;
|
||
|
}
|
||
|
}
|