Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: http-party/node-portfinder
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.0.32
Choose a base ref
...
head repository: http-party/node-portfinder
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.0.33
Choose a head ref
  • 16 commits
  • 6 files changed
  • 3 contributors

Commits on Sep 12, 2022

  1. Copy the full SHA
    33d870b View commit details
  2. add newline

    MasterOdin authored Sep 12, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    8bcb8c4 View commit details
  3. Update portfinder.d.ts

    MasterOdin authored Sep 12, 2022
    Copy the full SHA
    86eccb1 View commit details

Commits on Jan 8, 2023

  1. Add promise version of getPorts function

    Signed-off-by: Matthew Peveler <matt.peveler@gmail.com>
    MasterOdin committed Jan 8, 2023
    Copy the full SHA
    7e4a290 View commit details

Commits on May 26, 2023

  1. Copy the full SHA
    5f12a95 View commit details

Commits on Jul 18, 2023

  1. Copy the full SHA
    09132c3 View commit details

Commits on Feb 26, 2025

  1. Merge pull request #149 from Pwndrian/resolve-on-close

    Wait until server is closed before invoking callback
    eriktrom authored Feb 26, 2025
    Copy the full SHA
    17107f4 View commit details
  2. Update ci.yml to exclude macOS for old versions of node

    Exclude macos-latest, node-version 0.12.x, 4.x, 6.x, 8.x, 10.x, 12.x, 14.x from running on CI to prevent the failure:
    
    `Unable to find Node version 'y.x' for platform darwin and architecture arm64.`
    eriktrom authored Feb 26, 2025
    Copy the full SHA
    0d2730b View commit details
  3. Merge pull request #151 from http-party/eriktrom-ci-yml-fix-mac-os

    Update .github/workflows/ci.yml to exclude macOS for old versions of node
    eriktrom authored Feb 26, 2025
    Copy the full SHA
    d708601 View commit details
  4. Add CI support for node 20.x and 22.x

    eriktrom authored Feb 26, 2025
    Copy the full SHA
    0bc7f72 View commit details
  5. Merge pull request #152 from http-party/eriktrom-add-new-node-support…

    …-to-ci-2
    
    Add CI support for node 20.x and 22.x
    eriktrom authored Feb 26, 2025
    Copy the full SHA
    e137308 View commit details
  6. Merge pull request #146 from MasterOdin/feat-getPortsPromise

    Add promise version of getPorts function
    eriktrom authored Feb 26, 2025
    Copy the full SHA
    b70f227 View commit details
  7. Merge pull request #148 from MasterOdin/patch-1

    Fix adding options.host to defaultHosts list
    eriktrom authored Feb 26, 2025
    Copy the full SHA
    c87abaf View commit details
  8. Merge branch 'master' into feat-set-base-path

    eriktrom authored Feb 26, 2025
    Copy the full SHA
    d2b81e6 View commit details
  9. Merge pull request #145 from MasterOdin/feat-set-base-path

    Add mutator method for basePath
    eriktrom authored Feb 26, 2025
    Copy the full SHA
    ed9ad94 View commit details
  10. Release v1.0.33

    eriktrom committed Feb 26, 2025
    Copy the full SHA
    1d8fb46 View commit details
Showing with 114 additions and 13 deletions.
  1. +16 −1 .github/workflows/ci.yml
  2. +21 −2 lib/portfinder.d.ts
  3. +37 −6 lib/portfinder.js
  4. +2 −2 package-lock.json
  5. +2 −2 package.json
  6. +36 −0 test/port-finder-multiple-test.js
17 changes: 16 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -7,8 +7,23 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [0.12.x, 4.x, 6.x, 8.x, 10.x, 12.x, 14.x, 16.x, 18.x]
node-version: [0.12.x, 4.x, 6.x, 8.x, 10.x, 12.x, 14.x, 16.x, 18.x, 20.x, 22.x]
os: [ubuntu-latest, macos-latest, windows-latest]
exclude:
- os: macos-latest
node-version: 0.12.x
- os: macos-latest
node-version: 4.x
- os: macos-latest
node-version: 6.x
- os: macos-latest
node-version: 8.x
- os: macos-latest
node-version: 10.x
- os: macos-latest
node-version: 12.x
- os: macos-latest
node-version: 14.x

runs-on: ${{ matrix.os }}

23 changes: 21 additions & 2 deletions lib/portfinder.d.ts
Original file line number Diff line number Diff line change
@@ -48,15 +48,34 @@ export let highestPort: number;
*/
export function setHighestPort(port: number): void;

/**
* Default path to begin any socket search from.
*/
export let basePath: string;

/**
* Set the base path to begin any socket search from.
*/
export function setBasePath(path: string): void;

/**
* Responds with a unbound port on the current machine.
*/
export function getPort(callback: PortfinderCallback): void;
export function getPort(options: PortFinderOptions, callback: PortfinderCallback): void;

export function getPorts(count: number, options: PortFinderOptions, callback: (err: Error, ports: Array<number>) => void): void;

/**
* Responds a promise of an unbound port on the current machine.
*/
export function getPortPromise(options?: PortFinderOptions): Promise<number>;

/**
* Responds with an array of unbound ports on the current machine.
*/
export function getPorts(count: number, callback: (err: Error, ports: Array<number>) => void): void;
export function getPorts(count: number, options: PortFinderOptions, callback: (err: Error, ports: Array<number>) => void): void;

/**
* Responds a promise that resolves to an array of unbound ports on the current machine.
*/
export function getPortsPromise(count: number, options?: PortFinderOptions): Promise<Array<number>>;
43 changes: 37 additions & 6 deletions lib/portfinder.js
Original file line number Diff line number Diff line change
@@ -39,8 +39,10 @@ internals.testPort = function(options, callback) {
debugTestPort("done w/ testPort(): OK", options.host, "port", options.port);

options.server.removeListener('error', onError);
options.server.close();
callback(null, options.port);
options.server.close(function () {
debugTestPort("done w/ testPort(): Server closed", options.host, "port", options.port);
callback(null, options.port);
});
}

function onError (err) {
@@ -115,6 +117,13 @@ exports.setHighestPort = function (port) {
exports.basePath = '/tmp/portfinder'

//
// ### function setBasePath (path)
// #### @path {String} The new base path
//
exports.setBasePath = function (path) {
exports.basePath = path;
}

// ### function getPort (options, callback)
// #### @options {Object} Settings to use when finding the necessary port
// #### @callback {function} Continuation to respond to when complete.
@@ -141,10 +150,8 @@ exports.getPort = function (options, callback) {
}
}

if (options.host) {
if (exports._defaultHosts.indexOf(options.host) !== -1) {
exports._defaultHosts.push(options.host)
}
if (options.host && exports._defaultHosts.indexOf(options.host) === -1) {
exports._defaultHosts.push(options.host)
}

var openPorts = [], currentHost;
@@ -266,6 +273,30 @@ exports.getPorts = function (count, options, callback) {
}, callback);
};

//
// ### function getPortPromise (options)
// #### @count {Number} The number of ports to find
// #### @options {Object} Settings to use when finding the necessary port
// Responds with a promise that resolves to an array of unbound ports on the current machine.
//
exports.getPortsPromise = function (count, options) {
if (typeof Promise !== 'function') {
throw Error('Native promise support is not available in this version of node.' +
'Please install a polyfill and assign Promise to global.Promise before calling this method');
}
if (!options) {
options = {};
}
return new Promise(function(resolve, reject) {
exports.getPorts(count, options, function(err, ports) {
if (err) {
return reject(err);
}
resolve(ports);
});
});
}

//
// ### function getSocket (options, callback)
// #### @options {Object} Settings to use when finding the necessary port
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "portfinder",
"description": "A simple tool to find an open port on the current machine",
"version": "1.0.32",
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
"version": "1.0.33",
"author": "Charlie Robbins <charlie.robbins@gmail.com>, Erik Trom <erik.trom@gmail.com>",
"repository": {
"type": "git",
"url": "git@github.com:http-party/node-portfinder.git"
36 changes: 36 additions & 0 deletions test/port-finder-multiple-test.js
Original file line number Diff line number Diff line change
@@ -59,4 +59,40 @@ vows.describe('portfinder').addBatch({
}
}
}
}).addBatch({
"When using portfinder module": {
"with no existing servers": {
topic: function () {
closeServers();
return null;
},
"the getPortPromises() method with an argument of 3": {
topic: function () {
var vow = this;

portfinder.getPortsPromise(3)
.then(function (ports) {
vow.callback(null, ports);
})
.catch(function (err) {
vow.callback(err, null);
});
},
"should respond with the first three available ports (32768, 32769, 32770) if Promise are available": function (err, ports) {
if (typeof Promise !== 'function') {
assert.isTrue(!!err);
assert.equal(
err.message,
'Native promise support is not available in this version of node.' +
'Please install a polyfill and assign Promise to global.Promise before calling this method'
);
return;
}
if (err) { debugVows(err); }
assert.isTrue(!err);
assert.deepEqual(ports, [32768, 32769, 32770]);
}
},
}
}
}).export(module);