Skip to content

Commit 7e4a290

Browse files
committed
Add promise version of getPorts function
Signed-off-by: Matthew Peveler <[email protected]>
1 parent 123a06f commit 7e4a290

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

lib/portfinder.d.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,18 @@ export function setHighestPort(port: number): void;
5454
export function getPort(callback: PortfinderCallback): void;
5555
export function getPort(options: PortFinderOptions, callback: PortfinderCallback): void;
5656

57-
export function getPorts(count: number, options: PortFinderOptions, callback: (err: Error, ports: Array<number>) => void): void;
58-
5957
/**
6058
* Responds a promise of an unbound port on the current machine.
6159
*/
6260
export function getPortPromise(options?: PortFinderOptions): Promise<number>;
61+
62+
/**
63+
* Responds with an array of unbound ports on the current machine.
64+
*/
65+
export function getPorts(count: number, callback: (err: Error, ports: Array<number>) => void): void;
66+
export function getPorts(count: number, options: PortFinderOptions, callback: (err: Error, ports: Array<number>) => void): void;
67+
68+
/**
69+
* Responds a promise that resolves to an array of unbound ports on the current machine.
70+
*/
71+
export function getPortsPromise(count: number, options?: PortFinderOptions): Promise<Array<number>>;

lib/portfinder.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,30 @@ exports.getPorts = function (count, options, callback) {
266266
}, callback);
267267
};
268268

269+
//
270+
// ### function getPortPromise (options)
271+
// #### @count {Number} The number of ports to find
272+
// #### @options {Object} Settings to use when finding the necessary port
273+
// Responds with a promise that resolves to an array of unbound ports on the current machine.
274+
//
275+
exports.getPortsPromise = function (count, options) {
276+
if (typeof Promise !== 'function') {
277+
throw Error('Native promise support is not available in this version of node.' +
278+
'Please install a polyfill and assign Promise to global.Promise before calling this method');
279+
}
280+
if (!options) {
281+
options = {};
282+
}
283+
return new Promise(function(resolve, reject) {
284+
exports.getPorts(count, options, function(err, ports) {
285+
if (err) {
286+
return reject(err);
287+
}
288+
resolve(ports);
289+
});
290+
});
291+
}
292+
269293
//
270294
// ### function getSocket (options, callback)
271295
// #### @options {Object} Settings to use when finding the necessary port

test/port-finder-multiple-test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,40 @@ vows.describe('portfinder').addBatch({
5959
}
6060
}
6161
}
62+
}).addBatch({
63+
"When using portfinder module": {
64+
"with no existing servers": {
65+
topic: function () {
66+
closeServers();
67+
return null;
68+
},
69+
"the getPortPromises() method with an argument of 3": {
70+
topic: function () {
71+
var vow = this;
72+
73+
portfinder.getPortsPromise(3)
74+
.then(function (ports) {
75+
vow.callback(null, ports);
76+
})
77+
.catch(function (err) {
78+
vow.callback(err, null);
79+
});
80+
},
81+
"should respond with the first three available ports (32768, 32769, 32770) if Promise are available": function (err, ports) {
82+
if (typeof Promise !== 'function') {
83+
assert.isTrue(!!err);
84+
assert.equal(
85+
err.message,
86+
'Native promise support is not available in this version of node.' +
87+
'Please install a polyfill and assign Promise to global.Promise before calling this method'
88+
);
89+
return;
90+
}
91+
if (err) { debugVows(err); }
92+
assert.isTrue(!err);
93+
assert.deepEqual(ports, [32768, 32769, 32770]);
94+
}
95+
},
96+
}
97+
}
6298
}).export(module);

0 commit comments

Comments
 (0)