From 3ec21f07e902b3e767ca934e898eb0a928fe611e Mon Sep 17 00:00:00 2001 From: wholock Date: Sun, 5 Aug 2018 20:28:31 -0400 Subject: [PATCH] Fixed failing tests --- lib/portfinder.d.ts | 5 ++++- lib/portfinder.js | 13 ++++++++++--- test/port-finder-test.js | 4 ++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/portfinder.d.ts b/lib/portfinder.d.ts index bfa2bc9..b8c9d53 100644 --- a/lib/portfinder.d.ts +++ b/lib/portfinder.d.ts @@ -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; /** diff --git a/lib/portfinder.js b/lib/portfinder.js index f64e4f3..1d40b05 100644 --- a/lib/portfinder.js +++ b/lib/portfinder.js @@ -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) { @@ -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 { diff --git a/test/port-finder-test.js b/test/port-finder-test.js index 9550dea..c8603ed 100644 --- a/test/port-finder-test.js +++ b/test/port-finder-test.js @@ -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); @@ -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); }