is-reachable
Advanced tools
Comparing version 4.0.0 to 5.0.0
32
index.js
@@ -16,7 +16,13 @@ 'use strict'; | ||
const checkHttp = async url => { | ||
const checkHttp = async (url, timeout) => { | ||
let response; | ||
try { | ||
response = await got(url, {rejectUnauthorized: false}); | ||
} catch (_) { | ||
response = await got(url, { | ||
https: { | ||
rejectUnauthorized: false | ||
}, | ||
retry: 0, | ||
timeout | ||
}); | ||
} catch { | ||
return false; | ||
@@ -27,3 +33,3 @@ } | ||
const url = new URL(response.headers.location); | ||
const hostname = url.hostname.replace(/^\[/, '').replace(/\]$/, ''); // Strip [] from IPv6 | ||
const hostname = url.hostname.replace(/^\[/, '').replace(/]$/, ''); // Strip [] from IPv6 | ||
return !routerIps.has(hostname); | ||
@@ -37,3 +43,3 @@ } | ||
const isTargetReachable = async target => { | ||
const isTargetReachable = timeout => async target => { | ||
const url = new URL(prependHttp(target)); | ||
@@ -48,3 +54,3 @@ | ||
address = await getAddress(url.hostname); | ||
} catch (_) { | ||
} catch { | ||
return false; | ||
@@ -58,3 +64,3 @@ } | ||
if ([80, 443].includes(url.port)) { | ||
return checkHttp(url.toString()); | ||
return checkHttp(url.toString(), timeout); | ||
} | ||
@@ -65,8 +71,10 @@ | ||
module.exports = async (destinations, options) => { | ||
options = {...options}; | ||
options.timeout = typeof options.timeout === 'number' ? options.timeout : 5000; | ||
module.exports = async (destinations, {timeout = 5000} = {}) => { | ||
const promise = pAny(arrify(destinations).map(isTargetReachable(timeout))); | ||
const promise = pAny(arrify(destinations).map(isTargetReachable)); | ||
return pTimeout(promise, options.timeout).catch(() => false); | ||
try { | ||
return await pTimeout(promise, timeout); | ||
} catch { | ||
return false; | ||
} | ||
}; |
{ | ||
"name": "is-reachable", | ||
"version": "4.0.0", | ||
"version": "5.0.0", | ||
"description": "Check if servers are reachable", | ||
"license": "MIT", | ||
"repository": "sindresorhus/is-reachable", | ||
"funding": "https://github.com/sponsors/sindresorhus", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
@@ -43,5 +44,5 @@ "scripts": { | ||
"arrify": "^2.0.1", | ||
"got": "^9.6.0", | ||
"is-port-reachable": "^2.0.1", | ||
"p-any": "^2.1.0", | ||
"got": "^11.7.0", | ||
"is-port-reachable": "^3.0.0", | ||
"p-any": "^3.0.0", | ||
"p-timeout": "^3.2.0", | ||
@@ -54,6 +55,6 @@ "prepend-http": "^3.0.1", | ||
"ava": "^2.4.0", | ||
"tsd": "^0.8.0", | ||
"xo": "^0.24.0" | ||
"tsd": "^0.13.1", | ||
"xo": "^0.34.0" | ||
}, | ||
"browser": "browser.js" | ||
} |
@@ -1,2 +0,2 @@ | ||
# is-reachable [![Build Status](https://travis-ci.org/sindresorhus/is-reachable.svg?branch=master)](https://travis-ci.org/sindresorhus/is-reachable) | ||
# is-reachable [![Build Status](https://travis-ci.com/sindresorhus/is-reachable.svg?branch=master)](https://travis-ci.com/github/sindresorhus/is-reachable) | ||
@@ -11,3 +11,2 @@ > Check if servers are reachable | ||
## Install | ||
@@ -19,3 +18,2 @@ | ||
## Usage | ||
@@ -35,3 +33,2 @@ | ||
## API | ||
@@ -55,3 +52,3 @@ | ||
Type: `number`<br> | ||
Type: `number`\ | ||
Default: `5000` | ||
@@ -61,3 +58,2 @@ | ||
## Related | ||
@@ -67,3 +63,2 @@ | ||
## Maintainers | ||
@@ -70,0 +65,0 @@ |
Sorry, the diff of this file is not supported yet
7419
110
62
+ Added@sindresorhus/is@4.6.0(transitive)
+ Added@szmarczak/http-timer@4.0.6(transitive)
+ Added@types/cacheable-request@6.0.3(transitive)
+ Added@types/http-cache-semantics@4.0.4(transitive)
+ Added@types/keyv@3.1.4(transitive)
+ Added@types/node@20.12.12(transitive)
+ Added@types/responselike@1.0.3(transitive)
+ Addedcacheable-lookup@5.0.4(transitive)
+ Addedcacheable-request@7.0.4(transitive)
+ Addeddecompress-response@6.0.0(transitive)
+ Addeddefer-to-connect@2.0.1(transitive)
+ Addedgot@11.8.6(transitive)
+ Addedhttp2-wrapper@1.0.3(transitive)
+ Addedis-port-reachable@3.1.0(transitive)
+ Addedjson-buffer@3.0.1(transitive)
+ Addedkeyv@4.5.4(transitive)
+ Addedmimic-response@3.1.0(transitive)
+ Addednormalize-url@6.1.0(transitive)
+ Addedp-any@3.0.0(transitive)
+ Addedp-some@5.0.0(transitive)
+ Addedquick-lru@5.1.1(transitive)
+ Addedresolve-alpn@1.2.1(transitive)
+ Addedresponselike@2.0.1(transitive)
+ Addedundici-types@5.26.5(transitive)
- Removed@sindresorhus/is@0.14.0(transitive)
- Removed@szmarczak/http-timer@1.1.2(transitive)
- Removedcacheable-request@6.1.0(transitive)
- Removeddecompress-response@3.3.0(transitive)
- Removeddefer-to-connect@1.1.3(transitive)
- Removedduplexer3@0.1.5(transitive)
- Removedget-stream@4.1.0(transitive)
- Removedgot@9.6.0(transitive)
- Removedis-port-reachable@2.0.1(transitive)
- Removedjson-buffer@3.0.0(transitive)
- Removedkeyv@3.1.0(transitive)
- Removedlowercase-keys@1.0.1(transitive)
- Removednormalize-url@4.5.1(transitive)
- Removedp-any@2.1.0(transitive)
- Removedp-cancelable@1.1.0(transitive)
- Removedp-some@4.1.0(transitive)
- Removedprepend-http@2.0.0(transitive)
- Removedresponselike@1.0.2(transitive)
- Removedto-readable-stream@1.0.0(transitive)
- Removedtype-fest@0.3.1(transitive)
- Removedurl-parse-lax@3.0.0(transitive)
Updatedgot@^11.7.0
Updatedis-port-reachable@^3.0.0
Updatedp-any@^3.0.0