Socket
Socket
Sign inDemoInstall

is-reachable

Package Overview
Dependencies
Maintainers
2
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

is-reachable - npm Package Compare versions

Comparing version 2.4.0 to 3.0.0

16

browser.js

@@ -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()}`;
});
}));
};
'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);
};
{
"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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc