is-reachable
Advanced tools
Comparing version 3.1.0 to 4.0.0
@@ -19,3 +19,3 @@ declare namespace isReachable { | ||
@param targets - One or more targets to check. Can either be a full [URL](https://nodejs.org/api/url.html) like `https://hostname`, `hostname:port` or just `hostname`. When the protocol is missing from a target `http` is assumed. [Well-known protocols](http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml) are supported (e.g. `ftp://`, `mysql://`, `redis://` and more). | ||
@param targets - One or more targets to check. Can either be `hostname:port`, a URL like `https://hostname:port` or even just `hostname`. `port` must be specified if protocol is not `http:` or `https:` and defaults to `443`. Protocols other than `http:` and `https:` are not supported. | ||
@returns Whether any of the `targets` are reachable. | ||
@@ -22,0 +22,0 @@ |
24
index.js
@@ -9,3 +9,2 @@ 'use strict'; | ||
const pAny = require('p-any'); | ||
const portNumbers = require('port-numbers'); | ||
const pTimeout = require('p-timeout'); | ||
@@ -18,6 +17,6 @@ const prependHttp = require('prepend-http'); | ||
const checkRedirection = async target => { | ||
const checkHttp = async url => { | ||
let response; | ||
try { | ||
response = await got(target, {rejectUnauthorized: false}); | ||
response = await got(url, {rejectUnauthorized: false}); | ||
} catch (_) { | ||
@@ -27,4 +26,9 @@ return false; | ||
const url = new URL(response.headers.location || 'x://x'); | ||
return !routerIps.has(url.hostname); | ||
if (response.headers && response.headers.location) { | ||
const url = new URL(response.headers.location); | ||
const hostname = url.hostname.replace(/^\[/, '').replace(/\]$/, ''); // Strip [] from IPv6 | ||
return !routerIps.has(hostname); | ||
} | ||
return true; | ||
}; | ||
@@ -36,7 +40,5 @@ | ||
const url = new URL(prependHttp(target)); | ||
url.port = Number(url.port) || portNumbers.getPort(url.protocol.slice(0, -1)).port || 80; | ||
if (!/^[a-z]+:\/\//.test(target)) { | ||
const service = portNumbers.getService(url.port); | ||
url.protocol = ((service && service.name) ? service.name : 'unknown') + ':'; | ||
if (!url.port) { | ||
url.port = url.protocol === 'http:' ? 80 : 443; | ||
} | ||
@@ -55,4 +57,4 @@ | ||
if (url.protocol === 'http:' || url.protocol === 'https:') { | ||
return checkRedirection(url.toString()); | ||
if ([80, 443].includes(url.port)) { | ||
return checkHttp(url.toString()); | ||
} | ||
@@ -59,0 +61,0 @@ |
{ | ||
"name": "is-reachable", | ||
"version": "3.1.0", | ||
"version": "4.0.0", | ||
"description": "Check if servers are reachable", | ||
@@ -46,11 +46,10 @@ "license": "MIT", | ||
"p-any": "^2.1.0", | ||
"p-timeout": "^3.1.0", | ||
"port-numbers": "^4.0.7", | ||
"prepend-http": "^2.0.0", | ||
"p-timeout": "^3.2.0", | ||
"prepend-http": "^3.0.1", | ||
"router-ips": "^1.0.0", | ||
"url-parse": "^1.4.6" | ||
"url-parse": "^1.4.7" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"tsd": "^0.7.2", | ||
"ava": "^2.4.0", | ||
"tsd": "^0.8.0", | ||
"xo": "^0.24.0" | ||
@@ -57,0 +56,0 @@ }, |
@@ -28,3 +28,3 @@ # is-reachable [![Build Status](https://travis-ci.org/sindresorhus/is-reachable.svg?branch=master)](https://travis-ci.org/sindresorhus/is-reachable) | ||
console.log(await isReachable('google.com:80')); | ||
console.log(await isReachable('google.com:443')); | ||
//=> true | ||
@@ -37,3 +37,3 @@ })(); | ||
### isReachable(targets, [options]) | ||
### isReachable(targets, options?) | ||
@@ -46,6 +46,4 @@ Returns a `Promise<boolean>` which is `true` if any of the `targets` are reachable. | ||
One or more targets to check. Can either be a full [URL](https://nodejs.org/api/url.html) like `https://hostname`, `hostname:port` or just `hostname`. When the protocol is missing from a target `http` is assumed. | ||
One or more targets to check. Can either be `hostname:port`, a URL like `https://hostname:port` or even just `hostname`. `port` must be specified if protocol is not `http:` or `https:` and defaults to `443`. Protocols other than `http:` and `https:` are not supported. | ||
[Well-known protocols][] are supported (for example: `ftp://`, `mysql://`, `redis://` and more). | ||
#### options | ||
@@ -72,8 +70,1 @@ | ||
- [silverwind](https://github.com/silverwind) | ||
## License | ||
MIT | ||
[Well-known protocols]: https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml |
8
7339
67
+ Addedprepend-http@3.0.1(transitive)
- Removedport-numbers@^4.0.7
- Removedport-numbers@4.0.20(transitive)
Updatedp-timeout@^3.2.0
Updatedprepend-http@^3.0.1
Updatedurl-parse@^1.4.7