-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patch notes - 1. Minor bug fixes. 2. Added a new promise based DNS resolving method. Checkout docs for more info. 3. Improved overall performance.
- Loading branch information
1 parent
9a6510f
commit 22740ef
Showing
11 changed files
with
496 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const dns = require("dns"); | ||
const net = require("net"); | ||
|
||
const { processError } = require("./errors"); | ||
|
||
/** | ||
* | ||
* @param {string} hostname - The hostname you want to resolve | ||
* @param {string} type - The type/format of ip (eg. IPv4, IPv6) | ||
* @returns {Promise<any>} - Returns a promise which resolves with the ip data | ||
*/ | ||
const lookupForIp = async(hostname, type) => { | ||
return await new Promise(async(resolve, reject) => { | ||
|
||
//Define an ip object | ||
const ipObj = { | ||
Format: "", | ||
Addresses: [] | ||
} | ||
|
||
if (!type && typeof type === 'undefined') { | ||
try { | ||
return dns.lookup(hostname, async(err, address) => { | ||
if (err) return reject({ error: await processError(err, hostname, 'Yes') }); | ||
ipObj.Addresses.push(address); | ||
if (net.isIPv4(address)) { | ||
ipObj.Format = "IPv4"; | ||
return resolve(ipObj); | ||
} | ||
else if (net.isIPv6(address)) { | ||
ipObj.Format = "IPv6"; | ||
return resolve(ipObj); | ||
} | ||
else { | ||
ipObj.Format = 'Unknown Format'; | ||
return resolve(ipObj); | ||
} | ||
}) | ||
} catch (error) { | ||
return reject(await processError(error, hostname, 'Yes')); | ||
} | ||
} | ||
else if (type !== '' && typeof type !== 'undefined') { | ||
if (type === 'IPv4') { | ||
try { | ||
return dns.resolve4(hostname, async(err, addresses) => { | ||
if (err) return reject({ error: await processError(err, hostname, 'Yes') }); | ||
ipObj.Format = 'IPv4'; | ||
ipObj.Addresses = addresses; | ||
return resolve(ipObj); | ||
}) | ||
} catch (error) { | ||
return reject(await processError(error, hostname, 'Yes')); | ||
} | ||
} else if (type === "IPv6") { | ||
try { | ||
return dns.resolve6(hostname, async(err, addresses) => { | ||
if (err) return reject({ error: await processError(err, hostname, 'Yes') }); | ||
ipObj.Format = 'IPv6'; | ||
ipObj.Addresses = addresses; | ||
return resolve(ipObj); | ||
}) | ||
} catch (error) { | ||
return reject(await processError(error, hostname, 'Yes')); | ||
} | ||
} else { | ||
return reject(`Unknown ip address format specified! Available formats - IPv4, IPv6`); | ||
} | ||
} | ||
}) | ||
} | ||
|
||
module.exports = { | ||
lookupForIp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
"use strict"; | ||
/** | ||
* Util tool for processing http based common errors. | ||
* @param {Object} error - The HTTP error you want to process. | ||
* @returns {Promise<Object>} returns the processed error object as a promise. | ||
*/ | ||
const processError = (error, url, dns) => { | ||
return new Promise((resolve, reject) => { | ||
if (error.code === 'ENOTFOUND') { | ||
const err = new Error(`DNS Resolution Error`); | ||
err.code = error.code; | ||
err.name = "DNS_Resolution_Error"; | ||
err.hostname = url; | ||
err.message = `Failed to resolve the DNS of '${url}'`; | ||
return reject(err); | ||
} else if (error.code === 'ETIMEOUT') { | ||
const err = new Error(`Request Timeout`); | ||
err.code = error.code; | ||
err.name = "Request_Timeout_Error"; | ||
err.message = dns ? `The DNS request was timed out` : `The HTTP request to ${url} was timed out!`; | ||
err.hostname = url; | ||
return reject(err); | ||
} else if (error.type === 'abort' || error.code === 'ABORT_ERR') { | ||
const err = new Error(`Request Aborted`); | ||
err.code = error.code; | ||
err.name = "Request_Abort_Error"; | ||
err.message = `HTTP ${method} request to ${url} was aborted`; | ||
return reject(err); | ||
} else if (error.code === 'ECONNREFUSED') { | ||
const err = new Error(`Connection Refused`); | ||
err.code = error.code; | ||
err.name = "Connection_Refused_Error" | ||
err.message = dns? `Failed to lookup DNS of '${url}'` : `The server refused the connection for the HTTP ${method} request to ${url}`; | ||
return reject(err); | ||
} else if (error.code === 'ECONNRESET') { | ||
const err = new Error(`Connection Reset`); | ||
err.code = error.code; | ||
err.name = "Connection_Reset_Error"; | ||
err.message = dns? `Connection reset while looking up for the DNS of '${url}'` : `The server reset the connection while sending the HTTP ${method} request to ${url}`; | ||
return reject(err); | ||
} else if (error.code === 'EPIPE') { | ||
const err = new Error(`Broken Pipe`); | ||
err.code = error.code; | ||
err.name = "Broken_Pipe_Error"; | ||
err.url = url; | ||
err.message = `The connection to ${url} was closed unexpectedly while sending the HTTP ${method} request`; | ||
return reject(err); | ||
} else if (error.code === 'EHOSTUNREACH') { | ||
const err = new Error(`Host Unreachable`); | ||
err.code = error.code; | ||
err.name = "Host_Unreachable_Error"; | ||
err.message = `The host '${url}' is unreachable`; | ||
return reject(err); | ||
} else if (error.code === 'ENETUNREACH') { | ||
const err = new Error(`Network Unreachable`); | ||
err.code = error.code; | ||
err.name = "Network_Unreachable_Error"; | ||
err.message = `The network is unreachable`; | ||
return reject(err); | ||
} else if (error.code === 'EHOSTDOWN') { | ||
const err = new Error(`Host is down`); | ||
err.code = error.code; | ||
err.name = "Host_Down_Error"; | ||
err.message = `The host '${url}' is down`; | ||
return reject(err); | ||
} else if (error.code === 'ENETDOWN') { | ||
const err = new Error(`Network is down`); | ||
err.code = error.code; | ||
err.name = "Network_Down_Error"; | ||
err.message = `The network is down`; | ||
return reject(err); | ||
} | ||
else { | ||
return reject(error); | ||
} | ||
}) | ||
} | ||
|
||
module.exports = { | ||
processError | ||
} |
Oops, something went wrong.