insomnia/app/backend/dns.js
Gregory Schier 318c35c2cb Move a bunch of stuff to async/await (#39)
* Some minor implementations

* Some more

* Lots more

* Removed 'backend' alias

* removed all promises

* Removed a bunch of module exports stuff

* Some morE'

* Fix

* custom DNS

* Tests for DNS

* bug fix

* Some small adjustments

* Small stuff
2016-10-02 13:57:00 -07:00

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;
}
}