lib/portscanner.js
function checkPortStatus (port) {
  var args, host, opts, callback

  args = [].slice.call(arguments, 1)

  if (typeof args[0] === 'string') {
    host = args[0]
  } else if (typeof args[0] === 'object') {
    opts = args[0]
  } else if (typeof args[0] === 'function') {
    callback = args[0]
  }

  if (typeof args[1] === 'object') {
    opts = args[1]
  } else if (typeof args[1] === 'function') {
    callback = args[1]
  }

  if (typeof args[2] === 'function') {
    callback = args[2]
  }

  if (!callback) return promisify(checkPortStatus, arguments)

  opts = opts || {}

  host = host || opts.host || '127.0.0.1'

  var timeout = opts.timeout || 400
  var connectionRefused = false

  var socket = new Socket()
  var status = null
  var error = null

  // Socket connection established, port is open
  socket.on('connect', function () {
    status = 'open'
    socket.destroy()
  })

  // If no response, assume port is not listening
  socket.setTimeout(timeout)
  socket.on('timeout', function () {
    status = 'closed'
    error = new Error('Timeout (' + timeout + 'ms) occurred waiting for ' + host + ':' + port + ' to be available')
    socket.destroy()
  })

  // Assuming the port is not open if an error. May need to refine based on
  // exception
  socket.on('error', function (exception) {
    if (exception.code !== 'ECONNREFUSED') {
      error = exception
    } else {
      connectionRefused = true
    }
    status = 'closed'
  })

  // Return after the socket has closed
  socket.on('close', function (exception) {
    if (exception && !connectionRefused) { error = error || exception } else { error = null }
    callback(error, status)
  })

  socket.connect(port, host)
}
function checkPortStatus (port) {
  var args, host, opts, callback

  args = [].slice.call(arguments, 1)

  if (typeof args[0] === 'string') {
    host = args[0]
  } else if (typeof args[0] === 'object') {
    opts = args[0]
  } else if (typeof args[0] === 'function') {
    callback = args[0]
  }

  if (typeof args[1] === 'object') {
    opts = args[1]
  } else if (typeof args[1] === 'function') {
    callback = args[1]
  }

  if (typeof args[2] === 'function') {
    callback = args[2]
  }

  if (!callback) return promisify(checkPortStatus, arguments)

  opts = opts || {}

  host = host || opts.host || '127.0.0.1'

  var timeout = opts.timeout || 400
  var connectionRefused = false

  var socket = new Socket()
  var status = null
  var error = null

  // Socket connection established, port is open
  socket.on('connect', function () {
    status = 'open'
    socket.destroy()
  })

  // If no response, assume port is not listening
  socket.setTimeout(timeout)
  socket.on('timeout', function () {
    status = 'closed'
    error = new Error('Timeout (' + timeout + 'ms) occurred waiting for ' + host + ':' + port + ' to be available')
    socket.destroy()
  })

  // Assuming the port is not open if an error. May need to refine based on
  // exception
  socket.on('error', function (exception) {
    if (exception.code !== 'ECONNREFUSED') {
      error = exception
    } else {
      connectionRefused = true
    }
    status = 'closed'
  })

  // Return after the socket has closed
  socket.on('close', function (exception) {
    if (exception && !connectionRefused) { error = error || exception } else { error = null }
    callback(error, status)
  })

  socket.connect(port, host)
}
function checkPortStatus ( port ) { var args , host , opts , callback args = [ ] . slice . call ( arguments , 1 ) if ( typeof args [ 0 ] === 'string' ) { host = args [ 0 ] } else if ( typeof args [ 0 ] === 'object' ) { opts = args [ 0 ] } else if ( typeof args [ 0 ] === 'function' ) { callback = args [ 0 ] } if ( typeof args [ 1 ] === 'object' ) { opts = args [ 1 ] } else if ( typeof args [ 1 ] === 'function' ) { callback = args [ 1 ] } if ( typeof args [ 2 ] === 'function' ) { callback = args [ 2 ] } if ( ! callback ) return promisify ( checkPortStatus , arguments ) opts = opts || { } host = host || opts . host || '127.0.0.1' var timeout = opts . timeout || 400 var connectionRefused = false var socket = new Socket ( ) var status = null var error = null // Socket connection established, port is open socket . on ( 'connect' , function ( ) { status = 'open' socket . destroy ( ) } ) // If no response, assume port is not listening socket . setTimeout ( timeout ) socket . on ( 'timeout' , function ( ) { status = 'closed' error = new Error ( 'Timeout (' + timeout + 'ms) occurred waiting for ' + host + ':' + port + ' to be available' ) socket . destroy ( ) } ) // Assuming the port is not open if an error. May need to refine based on // exception socket . on ( 'error' , function ( exception ) { if ( exception . code !== 'ECONNREFUSED' ) { error = exception } else { connectionRefused = true } status = 'closed' } ) // Return after the socket has closed socket . on ( 'close' , function ( exception ) { if ( exception && ! connectionRefused ) { error = error || exception } else { error = null } callback ( error , status ) } ) socket . connect ( port , host ) }
Checks the status of an individual port.
 
@param {Number} port - Port to check status on.
@param {String} [host='127.0.0.1'] - Host of where to scan.
@param {checkPortCallback} [callback] - Function to call back with error or results.
@returns {Promise}
 
@param {Number} port - Port to check status on.
@param {Object} [opts={}] - Options object.
@param {String} [opts.host='127.0.0.1'] - Host of where to scan.
@param {Number} [opts.timeout=400] - Connection timeout in ms.
@param {checkPortCallback} [callback] - Function to call back with error or results.
@returns {Promise}
Checks the status of an individual port .