Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ var once = require('once')

/**
* Detect if the network is up (do we have connectivity?)
* @param {(error: boolean)=>void} cb Callback to execute once we know if we have connectivity or not
* @param {Object} options Optional options
* @param {number} options.port Port to connect to. Default: 80.
* @param {string} options.host Host to connect to. Default: nodejs.org
* @param {string} options.timeout The timeout to check in ms. Default: 5000
*
* @return {boolean}
*/
module.exports = function (cb) {
module.exports = function (cb, options = undefined) {
var socket = net.connect({
port: 80,
host: 'nodejs.org'
port: options?.port || 80,
host: options?.host || 'nodejs.org'
})

// If no 'error' or 'connect' event after 5s, assume network is down
var timer = setTimeout(function () {
done(new Error('timeout'))
}, 5000)
}, options?.timeout || 5000)

var done = once(function (err) {
clearTimeout(timer)
Expand Down