Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix timeout handling and require Node.js 10 #48

Merged
merged 2 commits into from Oct 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
Copy link

@jkff jkff Aug 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why we need to disable retries here? This is a pretty draconian way to check that a URL is reachable - many failures, such as ECONNRESET, can be intermittent.

I hit this while using another CLI tool that uses this library and refuses to do anything if one of the services it wants is "unreachable". The service gives me a ECONNRESET about 20% of the time, meaning, I need a retry loop around invoking the whole tool.

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