Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add promise version of getPorts function #146

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/portfinder.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,18 @@ export function setHighestPort(port: number): void;
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>>;
24 changes: 24 additions & 0 deletions lib/portfinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,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
Expand Down
36 changes: 36 additions & 0 deletions test/port-finder-multiple-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);