is-reachable
Advanced tools
Comparing version 2.4.0 to 3.0.0
@@ -11,14 +11,12 @@ /* eslint-env browser */ | ||
return new Promise(resolve => { | ||
url = new URL(prependHttp(url)); | ||
let {hostname, protocol, port} = new URL(prependHttp(url)); | ||
protocol = protocol || ''; | ||
port = port ? `:${port}` : ''; | ||
const hostname = url.hostname; | ||
const protocol = url.protocol || ''; | ||
const port = url.port ? `:${url.port}` : ''; | ||
const img = new Image(); | ||
img.onload = () => resolve(true); | ||
img.onerror = () => resolve(false); | ||
img.src = `${protocol}//${hostname}${port}/favicon.ico?${Date.now()}`; | ||
const image = new Image(); | ||
image.addEventListener('load', () => resolve(true)); | ||
image.addEventListener('error', () => resolve(false)); | ||
image.src = `${protocol}//${hostname}${port}/favicon.ico?${Date.now()}`; | ||
}); | ||
})); | ||
}; |
70
index.js
'use strict'; | ||
const {promisify} = require('util'); | ||
const dns = require('dns'); | ||
@@ -8,4 +9,3 @@ const net = require('net'); | ||
const pAny = require('p-any'); | ||
const pify = require('pify'); | ||
const pn = require('port-numbers'); | ||
const portNumbers = require('port-numbers'); | ||
const pTimeout = require('p-timeout'); | ||
@@ -16,43 +16,51 @@ const prependHttp = require('prepend-http'); | ||
const checkRedirection = target => { | ||
return got(target, {rejectUnauthorized: false}).then(res => { | ||
const url = new URL(res.headers.location || 'x://x'); | ||
return !routerIps.has(url.hostname); | ||
}).catch(() => false); | ||
const dnsLookupP = promisify(dns.lookup); | ||
const checkRedirection = async target => { | ||
let response; | ||
try { | ||
response = await got(target, {rejectUnauthorized: false}); | ||
} catch (_) { | ||
return false; | ||
} | ||
const url = new URL(response.headers.location || 'x://x'); | ||
return !routerIps.has(url.hostname); | ||
}; | ||
function isTargetReachable(target) { | ||
const getAddress = async hostname => net.isIP(hostname) ? hostname : (await dnsLookupP(hostname)).address; | ||
const isTargetReachable = async target => { | ||
const url = new URL(prependHttp(target)); | ||
url.port = Number(url.port) || pn.getPort(url.protocol.slice(0, -1)).port || 80; | ||
url.port = Number(url.port) || portNumbers.getPort(url.protocol.slice(0, -1)).port || 80; | ||
if (!/^[a-z]+:\/\//.test(target)) { | ||
url.protocol = pn.getService(url.port).name + ':'; | ||
const service = portNumbers.getService(url.port); | ||
url.protocol = ((service && service.name) ? service.name : 'unknown') + ':'; | ||
} | ||
return getAddress(url.hostname).then(address => { | ||
if (!address || routerIps.has(address)) { | ||
return false; | ||
} | ||
let address; | ||
try { | ||
address = await getAddress(url.hostname); | ||
} catch (_) { | ||
return false; | ||
} | ||
if (url.protocol === 'http:' || url.protocol === 'https:') { | ||
return checkRedirection(url.toString()); | ||
} | ||
if (!address || routerIps.has(address)) { | ||
return false; | ||
} | ||
return isPortReachable(url.port, {host: address}); | ||
}).catch(() => false); | ||
} | ||
function getAddress(hostname) { | ||
if (net.isIP(hostname)) { | ||
return Promise.resolve(hostname); | ||
if (url.protocol === 'http:' || url.protocol === 'https:') { | ||
return checkRedirection(url.toString()); | ||
} | ||
return pify(dns.lookup)(hostname); | ||
} | ||
module.exports = (dests, opts) => { | ||
opts = opts || {}; | ||
opts.timeout = typeof opts.timeout === 'number' ? opts.timeout : 5000; | ||
return isPortReachable(url.port, {host: address}); | ||
}; | ||
const p = pAny(arrify(dests).map(isTargetReachable)); | ||
return pTimeout(p, opts.timeout).catch(() => false); | ||
module.exports = (destinations, options) => { | ||
options = {...options}; | ||
options.timeout = typeof options.timeout === 'number' ? options.timeout : 5000; | ||
const promise = pAny(arrify(destinations).map(isTargetReachable)); | ||
return pTimeout(promise, options.timeout).catch(() => false); | ||
}; |
118
package.json
{ | ||
"name": "is-reachable", | ||
"version": "2.4.0", | ||
"description": "Check if servers are reachable", | ||
"license": "MIT", | ||
"repository": "sindresorhus/is-reachable", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"contributors": [ | ||
"silverwind <me@silverwind.io> (github.com/silverwind)" | ||
], | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava test.js" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"browser.js" | ||
], | ||
"keywords": [ | ||
"browser", | ||
"online", | ||
"offline", | ||
"network", | ||
"connected", | ||
"connectivity", | ||
"internet", | ||
"is", | ||
"has", | ||
"detect", | ||
"reachable", | ||
"reachability", | ||
"server", | ||
"host", | ||
"accessible", | ||
"socket" | ||
], | ||
"dependencies": { | ||
"arrify": "^1.0.1", | ||
"got": "^8.0.3", | ||
"is-port-reachable": "^2.0.0", | ||
"p-any": "^1.1.0", | ||
"p-timeout": "^2.0.1", | ||
"pify": "^3.0.0", | ||
"port-numbers": "^2.0.20", | ||
"prepend-http": "^2.0.0", | ||
"router-ips": "^1.0.0", | ||
"url-parse": "^1.2.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
}, | ||
"browser": "browser.js", | ||
"xo": { | ||
"esnext": true | ||
} | ||
"name": "is-reachable", | ||
"version": "3.0.0", | ||
"description": "Check if servers are reachable", | ||
"license": "MIT", | ||
"repository": "sindresorhus/is-reachable", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"contributors": [ | ||
"silverwind <me@silverwind.io> (github.com/silverwind)" | ||
], | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava test.js" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"browser.js" | ||
], | ||
"keywords": [ | ||
"browser", | ||
"online", | ||
"offline", | ||
"network", | ||
"connected", | ||
"connectivity", | ||
"internet", | ||
"is", | ||
"has", | ||
"detect", | ||
"reachable", | ||
"reachability", | ||
"server", | ||
"host", | ||
"accessible", | ||
"socket" | ||
], | ||
"dependencies": { | ||
"arrify": "^1.0.1", | ||
"got": "^9.3.2", | ||
"is-port-reachable": "^2.0.0", | ||
"p-any": "^1.1.0", | ||
"p-timeout": "^2.0.1", | ||
"port-numbers": "^4.0.4", | ||
"prepend-http": "^2.0.0", | ||
"router-ips": "^1.0.0", | ||
"url-parse": "^1.4.4" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.25.0", | ||
"xo": "^0.23.0" | ||
}, | ||
"browser": "browser.js" | ||
} |
@@ -5,3 +5,3 @@ # is-reachable [![Build Status](https://travis-ci.org/sindresorhus/is-reachable.svg?branch=master)](https://travis-ci.org/sindresorhus/is-reachable) | ||
Works in Node.js and the browser *(with [browserify](http://browserify.org))*. | ||
Works in Node.js and the browser *(with a bundler)*. | ||
@@ -12,6 +12,7 @@ The Node.js version will do a TCP handshake with the target's port. It attempts to detect cases where a router redirects the request to itself. | ||
## Install | ||
``` | ||
$ npm install --save is-reachable | ||
$ npm install is-reachable | ||
``` | ||
@@ -25,11 +26,9 @@ | ||
isReachable('sindresorhus.com').then(reachable => { | ||
console.log(reachable); | ||
(async () => { | ||
console.log(await isReachable('sindresorhus.com')); | ||
//=> true | ||
}); | ||
isReachable('google.com:80').then(reachable => { | ||
console.log(reachable); | ||
console.log(await isReachable('google.com:80')); | ||
//=> true | ||
}); | ||
})(); | ||
``` | ||
@@ -46,3 +45,3 @@ | ||
Type: `string` `Array` | ||
Type: `string` `string[]` | ||
@@ -55,2 +54,4 @@ 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. | ||
Type: `Object` | ||
##### timeout | ||
@@ -62,2 +63,3 @@ | ||
## Contributors | ||
@@ -64,0 +66,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6464
9
71
74
+ Added@sindresorhus/is@0.14.0(transitive)
+ Added@szmarczak/http-timer@1.1.2(transitive)
+ Addedcacheable-request@6.1.0(transitive)
+ Addedclone-response@1.0.3(transitive)
+ Addeddefer-to-connect@1.1.3(transitive)
+ Addedend-of-stream@1.4.4(transitive)
+ Addedget-stream@4.1.05.2.0(transitive)
+ Addedgot@9.6.0(transitive)
+ Addedhttp-cache-semantics@4.1.1(transitive)
+ Addedkeyv@3.1.0(transitive)
+ Addedlowercase-keys@2.0.0(transitive)
+ Addednormalize-url@4.5.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedp-cancelable@1.1.0(transitive)
+ Addedport-numbers@4.0.20(transitive)
+ Addedpump@3.0.2(transitive)
+ Addedto-readable-stream@1.0.0(transitive)
+ Addedwrappy@1.0.2(transitive)
- Removedpify@^3.0.0
- Removed@sindresorhus/is@0.7.0(transitive)
- Removedcacheable-request@2.1.4(transitive)
- Removedclone-response@1.0.2(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddecode-uri-component@0.2.2(transitive)
- Removedfrom2@2.3.0(transitive)
- Removedget-stream@3.0.0(transitive)
- Removedgot@8.3.2(transitive)
- Removedhas-symbol-support-x@1.4.2(transitive)
- Removedhas-to-string-tag-x@1.4.1(transitive)
- Removedhttp-cache-semantics@3.8.1(transitive)
- Removedinherits@2.0.4(transitive)
- Removedinto-stream@3.1.0(transitive)
- Removedis-object@1.0.2(transitive)
- Removedis-plain-obj@1.1.0(transitive)
- Removedis-retry-allowed@1.2.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedisurl@1.0.0(transitive)
- Removedkeyv@3.0.0(transitive)
- Removedlowercase-keys@1.0.0(transitive)
- Removednormalize-url@2.0.1(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedp-cancelable@0.4.1(transitive)
- Removedp-is-promise@1.1.0(transitive)
- Removedpify@3.0.0(transitive)
- Removedport-numbers@2.0.28(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedquery-string@5.1.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedsafe-buffer@5.1.25.2.1(transitive)
- Removedsort-keys@2.0.0(transitive)
- Removedstrict-uri-encode@1.1.0(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedtimed-out@4.0.1(transitive)
- Removedurl-to-options@1.0.1(transitive)
- Removedutil-deprecate@1.0.2(transitive)
Updatedgot@^9.3.2
Updatedport-numbers@^4.0.4
Updatedurl-parse@^1.4.4