Skip to content

Commit

Permalink
Fix timeout handling and require Node.js 10 (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshumehta1114 committed Oct 17, 2020
1 parent a575668 commit 50d61e8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -2,4 +2,3 @@ language: node_js
node_js:
- '12'
- '10'
- '8'
16 changes: 11 additions & 5 deletions index.js
Expand Up @@ -13,10 +13,16 @@ const URL = require('url-parse');

const dnsLookupP = promisify(dns.lookup);

const checkHttp = async url => {
const checkHttp = async (url, timeout) => {
let response;
try {
response = await got(url, {rejectUnauthorized: false});
response = await got(url, {
https: {
rejectUnauthorized: false
},
retry: 0,
timeout
});
} catch (_) {
return false;
}
Expand All @@ -32,7 +38,7 @@ const checkHttp = async url => {

const getAddress = async hostname => net.isIP(hostname) ? hostname : (await dnsLookupP(hostname)).address;

const isTargetReachable = async target => {
const isTargetReachable = timeout => async target => {
const url = new URL(prependHttp(target));

if (!url.port) {
Expand All @@ -51,7 +57,7 @@ const isTargetReachable = async target => {
}

if ([80, 443].includes(url.port)) {
return checkHttp(url.toString());
return checkHttp(url.toString(), timeout);
}

return isPortReachable(url.port, {host: address});
Expand All @@ -61,6 +67,6 @@ module.exports = async (destinations, options) => {
options = {...options};
options.timeout = typeof options.timeout === 'number' ? options.timeout : 5000;

const promise = pAny(arrify(destinations).map(isTargetReachable));
const promise = pAny(arrify(destinations).map(isTargetReachable(options.timeout)));
return pTimeout(promise, options.timeout).catch(() => false);
};
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava test.js && tsd"
Expand Down Expand Up @@ -40,7 +40,7 @@
],
"dependencies": {
"arrify": "^2.0.1",
"got": "^9.6.0",
"got": "^11.7.0",
"is-port-reachable": "^2.0.1",
"p-any": "^2.1.0",
"p-timeout": "^3.2.0",
Expand Down

0 comments on commit 50d61e8

Please sign in to comment.