Skip to content

Commit b70f227

Browse files
authored
Merge pull request #146 from MasterOdin/feat-getPortsPromise
Add promise version of getPorts function
2 parents e137308 + 7e4a290 commit b70f227

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
@@ -268,6 +268,30 @@ exports.getPorts = function (count, options, callback) {
268268
}, callback);
269269
};
270270

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