Skip to content
Permalink
Browse files
Merge pull request #63 from Prithvirajbilla/find_in_port_range
Adds stopPort to getPort Api
  • Loading branch information
eriktrom committed Aug 6, 2018
2 parents 24b302d + 9053915 commit 5bed64d495c61e2f466cb2b3391d664135fac5bd
Showing 4 changed files with 76 additions and 11 deletions.
@@ -11,10 +11,21 @@ interface PortFinderOptions{
* Host to find available port on.
*/
host?: string;
/**
* search start port (equals to port when not provided)
* This exists because getPort and getPortPromise mutates port state in
* recursive calls and doesn't have a way to retrieve begininng port while
* searching.
*/
startPort?: number;
/**
* Minimum port (takes precedence over `basePort`).
*/
port?: number;
/**
* Maximum port
*/
stopPort?: number;
}

/**
@@ -27,8 +27,6 @@ internals.testPort = function(options, callback) {
options = {};
}

options.port = Number(options.port) || Number(exports.basePort);
options.host = options.host || null;
options.server = options.server || net.createServer(function () {
//
// Create an empty listener for the port testing server.
@@ -50,7 +48,7 @@ internals.testPort = function(options, callback) {

options.server.removeListener('listening', onListen);

if (err.code !== 'EADDRINUSE' && err.code !== 'EACCES') {
if (!(err.code == 'EADDRINUSE' || err.code == 'EACCES')) {
return callback(err);
}

@@ -72,6 +70,12 @@ internals.testPort = function(options, callback) {
//
exports.basePort = 8000;

//
// ### @highestPort {Number}
// Largest port number is an unsigned short 2**16 -1=65335
//
exports.highestPort = 65535;

//
// ### @basePath {string}
// Default path to begin any socket search from
@@ -88,6 +92,22 @@ exports.getPort = function (options, callback) {
if (!callback) {
callback = options;
options = {};

}

options.port = Number(options.port) || Number(exports.basePort);
options.host = options.host || null;
options.stopPort = Number(options.stopPort) || Number(exports.highestPort);

if(!options.startPort) {
options.startPort = Number(options.port);
if(options.startPort < 1024) {
// ports < 1024 are system/well-known ports - need super user privilege to bind to them.
throw Error('Provided options.startPort(' + options.startPort + ') is less than 1024, which are cannot be bound.');
}
if(options.stopPort < options.startPort) {
throw Error('Provided options.stopPort(' + options.stopPort + 'is less than options.startPort (' + options.startPort + ')');
}
}

if (options.host) {
@@ -137,11 +157,11 @@ exports.getPort = function (options, callback) {
// hosts, without showing them a good error.
var msg = 'Provided host ' + options.host + ' could NOT be bound. Please provide a different host address or hostname';
return callback(Error(msg));
} else {
var idx = exports._defaultHosts.indexOf(currentHost);
exports._defaultHosts.splice(idx, 1);
return exports.getPort(options, callback);
}

var idx = exports._defaultHosts.indexOf(currentHost);
exports._defaultHosts.splice(idx, 1);
return exports.getPort(options, callback);
} else {
// error is not accounted for, file ticket, handle special case
return callback(err);
@@ -157,10 +177,16 @@ exports.getPort = function (options, callback) {

if (openPorts[0] === openPorts[openPorts.length-1]) {
// if first === last, we found an open port
return callback(null, openPorts[0]);
if(openPorts[0] <= options.stopPort) {
return callback(null, openPorts[0]);
}
else {
var msg = 'No open ports found in between '+ options.startPort + ' and ' + options.stopPort;
return callback(Error(msg));
}
} else {
// otherwise, try again, using sorted port, aka, highest open for >= 1 host
return exports.getPort({ port: openPorts.pop(), host: options.host }, callback);
return exports.getPort({ port: openPorts.pop(), host: options.host, startPort: options.startPort, stopPort: options.stopPort }, callback);
}

});
@@ -329,7 +355,6 @@ exports.nextSocket = function (socketPath) {
match = name.match(/^([a-zA-z]+)(\d*)$/i),
index = parseInt(match[2]),
base = match[1];

if (isNaN(index)) {
index = 0;
}
@@ -1,7 +1,7 @@
{
"name": "portfinder",
"description": "A simple tool to find an open port on the current machine",
"version": "1.0.13",
"version": "1.0.14",
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
"repository": {
"type": "git",
@@ -46,6 +46,35 @@ vows.describe('portfinder').addBatch({
assert.isTrue(!err);
assert.equal(port, 32773);
}
},
"the getPort() method with stopPort smaller than available port": {
topic: function() {
// stopPort: 32722 is smaller than available port 32773 (32768 + 5)
setTimeout(function() {
portfinder.getPort({ stopPort: 32772 }, this.callback);
}.bind(this), 6000); //wait for cleanup of bound hosts.
},
"should return error": function(err, port) {
assert.isTrue(!!err);
assert.equal(
err.message,
'No open ports found in between 32768 and 32772'
);
return;
}
},
"the getPort() method with stopPort greater than available port": {
topic: function() {
// stopPort: 32774 is greater than available port 32773 (32768 + 5)
setTimeout(function() {
portfinder.getPort({ stopPort: 32774 }, this.callback);
}.bind(this), 9000); //wait for cleanup of bound hosts.
},
"should respond with the first free port (32773) less than provided stopPort": function(err, port) {
if (err) { debugVows(err); }
assert.isTrue(!err);
assert.equal(port, 32773);
}
}
}
}

0 comments on commit 5bed64d

Please sign in to comment.