Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] replace async with bluebird promise #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
58 changes: 26 additions & 32 deletions lib/portscanner.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
var net = require('net')
var Socket = net.Socket
var async = require('async')
var Promise = require('bluebird')
var promisify = require('./promisify')

Promise.config({
cancellation: true
})

/**
* Finds the first port with a status of 'open', implying the port is in use and
* there is likely a service listening on it.
Expand Down Expand Up @@ -188,40 +192,30 @@ function findAPortWithStatus (status) {

endPort = endPort || 65535

var foundPort = false
var numberOfPortsChecked = 0
var port = portList ? portList[0] : startPort

// Returns true if a port with matching status has been found or if checked
// the entire range of ports
var hasFoundPort = function () {
return foundPort || numberOfPortsChecked === (portList ? portList.length : endPort - startPort + 1)
if (!portList) {
portList = []
for (var iPort = startPort; iPort <= endPort; iPort++) {
portList.push(iPort)
}
}

// Checks the status of the port
var checkNextPort = function (callback) {
checkPortStatus(port, host, function (error, statusOfPort) {
numberOfPortsChecked++
if (statusOfPort === status) {
foundPort = true
callback(error)
} else {
port = portList ? portList[numberOfPortsChecked] : port + 1
callback(null)
}
var promise = Promise.map(portList, function (portToCheck) {
return new Promise(function (resolve, reject) {
checkPortStatus(portToCheck, host, function (err, statusOfPort) {
if (statusOfPort === status) {
resolve(portToCheck)
promise.cancel()
callback(null, portToCheck)
} else {
resolve(false)
}
})
})
}

// Check the status of each port until one with a matching status has been
// found or the range of ports has been exhausted
async.until(hasFoundPort, checkNextPort, function (error) {
if (error) {
callback(error, port)
} else if (foundPort) {
callback(null, port)
} else {
callback(null, false)
}
}, {
concurrency: 50
}).then(function (ports) {
ports = ports.filter(Boolean)
callback(null, ports[0] || false)
})
}
/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"main": "./lib/portscanner.js",
"dependencies": {
"async": "1.5.2"
"bluebird": "^3.4.6"
},
"devDependencies": {
"ava": "^0.4.2",
Expand Down