Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed failing tests
  • Loading branch information
Prithvirajbilla committed Aug 6, 2018
1 parent 90be7f1 commit 3ec21f0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
5 changes: 4 additions & 1 deletion lib/portfinder.d.ts
Expand Up @@ -12,7 +12,10 @@ interface PortFinderOptions{
*/
host?: string;
/**
* search start port (equals to basePort when not provided)
* 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;
/**
Expand Down
13 changes: 10 additions & 3 deletions lib/portfinder.js
Expand Up @@ -97,10 +97,17 @@ exports.getPort = function (options, callback) {

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

if(!options.startPort) {
options.startPort = options.port
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) {
Expand Down Expand Up @@ -174,7 +181,7 @@ exports.getPort = function (options, callback) {
return callback(null, openPorts[0]);
}
else {
var msg = 'No open ports found in between '+ options.startPort + ' and ' + options.stopPort;
var msg = `No open ports found in between ${options.startPort} and ${options.stopPort}`;
return callback(Error(msg));
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions test/port-finder-test.js
Expand Up @@ -52,7 +52,7 @@ vows.describe('portfinder').addBatch({
// 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.
}.bind(this), 6000); //wait for cleanup of bound hosts.
},
"should return error": function(err, port) {
assert.isTrue(!!err);
Expand All @@ -68,7 +68,7 @@ vows.describe('portfinder').addBatch({
// 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.
}.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); }
Expand Down

0 comments on commit 3ec21f0

Please sign in to comment.