Skip to content

Commit

Permalink
promise support
Browse files Browse the repository at this point in the history
  • Loading branch information
laggingreflex committed Nov 19, 2016
1 parent 3b90e2c commit 84db394
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ portscanner.findAPortInUse([3000, 3005, 3006], '127.0.0.1', function(error, port
portscanner.findAPortNotInUse(3000, 4000, function(error, port) {
console.log('PORT IN USE AT: ' + port)
})

// And use promises
portscanner.findAPortNotInUse(3000, 4000).then(function(port) {
console.log('PORT IN USE AT: ' + port)
})
```

The example directory contains a more detailed example.
Expand Down
20 changes: 16 additions & 4 deletions lib/portscanner.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var net = require('net')
var Socket = net.Socket
var async = require('async')
var promisify = require('./promisify')

/**
* Finds the first port with a status of 'open', implying the port is in use and
Expand All @@ -10,18 +11,22 @@ var async = require('async')
* @param {Number} startPort - Port to begin status check on (inclusive).
* @param {Number} [endPort=65535] - Last port to check status on (inclusive).
* @param {String} [host='127.0.0.1'] - Host of where to scan.
* @param {findPortCallback} callback - Function to call back with error or results.
* @param {findPortCallback} [callback] - Function to call back with error or results.
* @returns {Promise}
* @example
* // scans through 3000 to 3002 (inclusive)
* portscanner.findAPortInUse(3000, 3002, '127.0.0.1', console.log)
* // returns a promise in the absence of a callback
* portscanner.findAPortInUse(3000, 3002, '127.0.0.1').then(console.log)
* @example
* // scans through 3000 to 65535 on '127.0.0.1'
* portscanner.findAPortInUse(3000, console.log)
*/
/**
* @param {Array} postList - Array of ports to check status on.
* @param {String} [host='127.0.0.1'] - Host of where to scan.
* @param {findPortCallback} callback - Function to call back with error or results.
* @param {findPortCallback} [callback] - Function to call back with error or results.
* @returns {Promise}
* @example
* // scans 3000 and 3002 only, not 3001.
* portscanner.findAPortInUse([3000, 3002], console.log)
Expand All @@ -48,14 +53,16 @@ function findAPortNotInUse () {
/**
* @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.
* @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.
* @param {checkPortCallback} [callback] - Function to call back with error or results.
* @returns {Promise}
*/
function checkPortStatus (port) {
var args, host, opts, callback
Expand All @@ -79,6 +86,9 @@ function checkPortStatus (port) {
if (typeof args[2] === 'function') {
callback = args[2]
}

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

opts = opts || {}

host = host || opts.host || '127.0.0.1'
Expand Down Expand Up @@ -167,6 +177,8 @@ function findAPortWithStatus (status) {
callback = params[3]
}

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

endPort = endPort || 65535

var foundPort = false
Expand Down
19 changes: 19 additions & 0 deletions lib/promisify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module.exports = promisify

function promisify (fn, args) {
if (typeof Promise === 'undefined') {
throw new Error('Please run in a Promise supported environment or provide a callback')
}
return new Promise(function (resolve, reject) {
args = [].slice.call(args).concat([callback])
fn.apply(null, args)

function callback (error, port) {
if (error || port === false) {
reject(error || new Error('No open port found'))
} else {
resolve(port)
}
}
})
}
41 changes: 41 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,44 @@ test('findAPortNotInUse - with array as parameter', t => {
t.is(port, 3002)
})
})

test('findAPortNotInUse - promise', t => {
t.plan(1)

portScanner.findAPortNotInUse(3000, 3010, '127.0.0.1').then(port => {
t.is(port, 3001)
})
})

test('findAPortNotInUse - promise (without host)', t => {
t.plan(1)

portScanner.findAPortNotInUse(3000, 3010).then(port => {
t.is(port, 3001)
})
})

test('findAPortNotInUse - promise (without host and endPort)', t => {
t.plan(1)

portScanner.findAPortNotInUse(3000).then(port => {
t.is(port, 3001)
})
})

test('findAPortInUse - promise', t => {
t.plan(1)

portScanner.findAPortInUse(3000, 3010, '127.0.0.1').then(port => {
t.is(port, 3000)
})
})

test('findAPortInUse - promise (error)', t => {
t.plan(1)

portScanner.findAPortInUse(3001, 3010, '127.0.0.1').catch(err => {
t.not(err, null)
})
})

0 comments on commit 84db394

Please sign in to comment.