is-reachable
Advanced tools
Comparing version 2.3.0 to 2.3.1
61
index.js
@@ -9,2 +9,3 @@ 'use strict'; | ||
const pn = require('port-numbers'); | ||
const pTimeout = require('p-timeout'); | ||
const prependHttp = require('prepend-http'); | ||
@@ -15,42 +16,32 @@ const routerIps = require('router-ips'); | ||
const checkRedirection = url => { | ||
return new Promise(resolve => { | ||
got(url).then(res => { | ||
const redirectHostname = (new URL(res.headers.location || '')).hostname; | ||
if (routerIps.has(redirectHostname)) { | ||
resolve(false); | ||
} | ||
resolve(true); | ||
}).catch(() => resolve(false)); | ||
}); | ||
return got(url).then(res => { | ||
return !routerIps.has((new URL(res.headers.location || '')).hostname); | ||
}).catch(() => false); | ||
}; | ||
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 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; | ||
if (!/^[a-z]+:\/\//.test(url) && port !== 80 && port !== 443) { | ||
protocol = pn.getService(port).name + ':'; | ||
if (!/^[a-z]+:\/\//.test(url) && port !== 80 && port !== 443) { | ||
protocol = pn.getService(port).name + ':'; | ||
} | ||
return pify(dns.lookup)(hostname).then(address => { | ||
if (!address) { | ||
return false; | ||
} | ||
pify(dns.lookup)(hostname).then(address => { | ||
if (!address) { | ||
resolve(false); | ||
} | ||
if (routerIps.has(address)) { | ||
return false; | ||
} | ||
if (routerIps.has(address)) { | ||
resolve(false); | ||
} | ||
if (protocol === 'http:' || protocol === 'https:') { | ||
return checkRedirection(url); | ||
} | ||
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); | ||
} | ||
@@ -62,6 +53,4 @@ | ||
return new Promise(resolve => { | ||
setTimeout(() => resolve(false), opts.timeout); | ||
pAny(arrify(dests).map(isTargetReachable)).then(resolve); | ||
}); | ||
const p = pAny(arrify(dests).map(isTargetReachable)); | ||
return pTimeout(p, opts.timeout).catch(() => false); | ||
}; |
{ | ||
"name": "is-reachable", | ||
"version": "2.3.0", | ||
"version": "2.3.1", | ||
"description": "Check if servers are reachable", | ||
@@ -48,2 +48,3 @@ "license": "MIT", | ||
"p-any": "^1.0.0", | ||
"p-timeout": "^1.0.0", | ||
"pify": "^2.3.0", | ||
@@ -50,0 +51,0 @@ "port-numbers": "^1.1.53", |
@@ -43,3 +43,3 @@ # is-reachable [![Build Status](https://travis-ci.org/sindresorhus/is-reachable.svg?branch=master)](https://travis-ci.org/sindresorhus/is-reachable) | ||
Type: `string` `array` | ||
Type: `string` `Array` | ||
@@ -46,0 +46,0 @@ 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. |
6263
10
64
+ Addedp-timeout@^1.0.0
+ Addedp-finally@1.0.0(transitive)
+ Addedp-timeout@1.2.1(transitive)