Skip to content

Commit

Permalink
Adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Prithvirajbilla committed Jul 30, 2018
1 parent 31bfe9f commit 90be7f1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
6 changes: 5 additions & 1 deletion lib/portfinder.d.ts
Expand Up @@ -11,14 +11,18 @@ interface PortFinderOptions{
* Host to find available port on.
*/
host?: string;
/**
* search start port (equals to basePort when not provided)
*/
startPort?: number;
/**
* Minimum port (takes precedence over `basePort`).
*/
port?: number;
/**
* Maximum port
*/
stopPort?: number;
stopPort?: number;
}

/**
Expand Down
24 changes: 17 additions & 7 deletions lib/portfinder.js
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -88,10 +92,16 @@ exports.getPort = function (options, callback) {
if (!callback) {
callback = options;
options = {};

}

// Largest port number is an unsigned short 2**16 -1=65335
options.stopPort = Number(options.stopPort) || 65535
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 = options.port
}

if (options.host) {

Expand Down Expand Up @@ -160,16 +170,16 @@ exports.getPort = function (options, callback) {

if (openPorts[0] === openPorts[openPorts.length-1]) {
// if first === last, we found an open port
if(openPorts[0] < option.stopPort) {
if(openPorts[0] <= options.stopPort) {
return callback(null, openPorts[0]);
}
else {
var msg = 'No open ports found in between '+ options.basePort + ' and ' + options.stopPort;
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);
}

});
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -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",
Expand Down
29 changes: 29 additions & 0 deletions test/port-finder-test.js
Expand Up @@ -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), 3000); //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), 3000); //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);
}
}
}
}
Expand Down

0 comments on commit 90be7f1

Please sign in to comment.