check-internet-connected
Advanced tools
Comparing version
74
index.js
const url = require('url'); | ||
const Promise = require('bluebird'); | ||
const net = require('net'); | ||
Promise.config({ | ||
cancellation: true | ||
}); | ||
async function checkInternetConnected (config = {}) { | ||
const {timeout = 5000, retries = 5, domain = 'apple.com', servers} = config; | ||
const dns = require('dns'); | ||
if (servers) { | ||
dns.setServers(servers); | ||
const {timeout = 5000, retries = 5, domain = 'https://apple.com'} = config; | ||
const urlInfo = url.parse(domain); | ||
if (urlInfo.port === null) { | ||
if (urlInfo.protocol === 'ftp') { | ||
urlInfo.port = '21'; | ||
} else if (urlInfo.protocol === 'http') { | ||
urlInfo.port = '80'; | ||
} else if (urlInfo.protocol === 'https') { | ||
urlInfo.port = '443'; | ||
} | ||
} | ||
const urlInfo = url.parse(domain); | ||
const defaultPort = Number.parseInt(urlInfo.port || '80'); | ||
const hostname = urlInfo.hostname || urlInfo.pathname; | ||
let lastErrorV4 = false; | ||
const resolve4 = Promise.promisify(dns.resolve4); | ||
const resolve6 = Promise.promisify(dns.resolve6); | ||
for (let i = 0; i < retries; i++) { | ||
let result4 = null; | ||
let result6 = null; | ||
let promise4 = resolve4(hostname).timeout(timeout); | ||
let promise6 = resolve6(hostname).timeout(timeout); | ||
promise4.catch(() => {}); | ||
promise6.catch(() => {}); | ||
const connectPromise = new Promise(function (resolve, reject, onCancel) { | ||
const client = new net.Socket(); | ||
client.connect({ port: defaultPort, host: hostname }, () => { | ||
client.destroy(); | ||
resolve(true); | ||
}); | ||
client.on('data', (data) => { | ||
}); | ||
client.on('error', (err) => { | ||
reject(err); | ||
}); | ||
client.on('close', () => { | ||
}); | ||
onCancel(() => { | ||
client.destroy(); | ||
}); | ||
}); | ||
try { | ||
result4 = await promise4; | ||
await connectPromise.timeout(timeout); | ||
} catch (ex) { | ||
lastErrorV4 = ex; | ||
} | ||
try { | ||
result6 = await promise6; | ||
} catch (ex) { | ||
/* swallow up v6 errors for now */ | ||
} | ||
if (result4 && result6) { | ||
result4.push(...result6); | ||
return result4; | ||
} else if (result4) { | ||
return result4; | ||
} else if (result6) { | ||
return result6; | ||
} | ||
if (!(result4 || result6)) { | ||
if (i !== (retries - 1)) { | ||
await Promise.delay(timeout); | ||
if (i === (retries - 1)) { | ||
throw ex; | ||
} | ||
} | ||
} | ||
throw lastErrorV4; | ||
} | ||
module.exports = checkInternetConnected; |
{ | ||
"name": "check-internet-connected", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "Utility to check if internet is connected or not", | ||
@@ -17,2 +17,3 @@ "main": "index.js", | ||
"dns", | ||
"tcp", | ||
"internet", | ||
@@ -31,3 +32,3 @@ "connection", | ||
"dependencies": { | ||
"bluebird": "^3.5.2" | ||
"bluebird": "^3.5.4" | ||
}, | ||
@@ -34,0 +35,0 @@ "devDependencies": { |
# check-internet-connected | ||
A node module that queries DNS to check whether we are connected to Internet or not | ||
A node module that does a TCP connection to check whether we are connected to Internet or not | ||
@@ -12,6 +12,6 @@ Usage:- | ||
.then((result) => { | ||
console.log(result);//list of A and AAAA records, connected to internet | ||
console.log(result);//successfully connected to a server | ||
}) | ||
.catch((ex) => { | ||
console.log(ex); // cannot resolve DNS | ||
console.log(ex); // cannot connect to a server or error occurred. | ||
}); | ||
@@ -24,8 +24,7 @@ ``` | ||
const config = { | ||
timeout: 5000, //timeout connecting to each server(A and AAAA), each try | ||
timeout: 5000, //timeout connecting to each server, each try | ||
retries: 5,//number of retries to do before failing | ||
domain: 'apple.com',//the domain to check DNS record of | ||
servers: ['8.8.8.8'],//string array of rfc5952 formatted addresses of DNS servers, default uses the inbuilt system DNS servers. | ||
domain: 'https://apple.com',//the domain to check DNS record of | ||
} | ||
checkInternetConnected(config); | ||
``` |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
14656
255.9%9
125%48
2.13%29
-3.33%Updated