is-reachable
Advanced tools
Comparing version 2.2.0 to 2.3.0
55
index.js
@@ -14,37 +14,52 @@ 'use strict'; | ||
const checkRedirection = url => { | ||
return got(url).then(res => { | ||
const redirectHostname = (new URL(res.headers.location || '')).hostname; | ||
return new Promise(resolve => { | ||
got(url).then(res => { | ||
const redirectHostname = (new URL(res.headers.location || '')).hostname; | ||
if (routerIps.has(redirectHostname)) { | ||
return false; | ||
} | ||
if (routerIps.has(redirectHostname)) { | ||
resolve(false); | ||
} | ||
return true; | ||
resolve(true); | ||
}).catch(() => resolve(false)); | ||
}); | ||
}; | ||
module.exports = dests => { | ||
return pAny(arrify(dests).map(url => { | ||
url = new URL(prependHttp(url)); | ||
function isTargetReachable(url) { | ||
return new Promise(resolve => { | ||
const uri = new URL(prependHttp(url)); | ||
const hostname = uri.hostname; | ||
let protocol = uri.protocol; | ||
const port = Number(uri.port) || pn.getPort(protocol.slice(0, -1)).port || 80; | ||
const hostname = url.hostname; | ||
const protocol = url.protocol; | ||
const port = url.port || pn.getPort(protocol.slice(0, -1)).port || 80; | ||
if (!/^[a-z]+:\/\//.test(url) && port !== 80 && port !== 443) { | ||
protocol = pn.getService(port).name + ':'; | ||
} | ||
return pify(dns.lookup)(hostname).then(address => { | ||
pify(dns.lookup)(hostname).then(address => { | ||
if (!address) { | ||
return false; | ||
resolve(false); | ||
} | ||
if (routerIps.has(address)) { | ||
return false; | ||
resolve(false); | ||
} | ||
if (protocol === 'http' || protocol === 'https') { | ||
return checkRedirection(url.toString()); | ||
if (protocol === 'http:' || protocol === 'https:') { | ||
checkRedirection(url).then(resolve); | ||
} else { | ||
isPortReachable(port, {host: address}).then(resolve); | ||
} | ||
}).catch(() => resolve(false)); | ||
}); | ||
} | ||
return isPortReachable(port, {host: address}); | ||
}); | ||
})).catch(() => false); | ||
module.exports = (dests, opts) => { | ||
opts = opts || {}; | ||
opts.timeout = typeof opts.timeout === 'number' || 5000; | ||
return new Promise(resolve => { | ||
setTimeout(() => resolve(false), opts.timeout); | ||
pAny(arrify(dests).map(isTargetReachable)).then(resolve); | ||
}); | ||
}; |
{ | ||
"name": "is-reachable", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Check if servers are reachable", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -37,3 +37,3 @@ # is-reachable [![Build Status](https://travis-ci.org/sindresorhus/is-reachable.svg?branch=master)](https://travis-ci.org/sindresorhus/is-reachable) | ||
### isReachable(targets) | ||
### isReachable(targets, [options]) | ||
@@ -44,3 +44,3 @@ Returns a `Promise` for a `boolean` which is `true` if any of the `targets` are reachable. | ||
Type: `string` `Array` | ||
Type: `string` `array` | ||
@@ -51,2 +51,10 @@ 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. | ||
#### options | ||
##### timeout | ||
Type: `number` | ||
Timeout in milliseconds after which a request is considered failed. Default: `5000. | ||
## Contributors | ||
@@ -53,0 +61,0 @@ |
6457
74
72