Skip to content

Commit 724a0e1

Browse files
authored
Merge pull request #168 from MasterOdin/feat-promise-return
getPort/getPorts return promise if no callback
2 parents d551821 + c061663 commit 724a0e1

File tree

4 files changed

+79
-42
lines changed

4 files changed

+79
-42
lines changed

lib/portfinder.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export function setBasePath(path: string): void;
6161
/**
6262
* Responds with a unbound port on the current machine.
6363
*/
64+
export function getPort(options: PortFinderOptions): Promise<number>;
6465
export function getPort(callback: PortfinderCallback): void;
6566
export function getPort(options: PortFinderOptions, callback: PortfinderCallback): void;
6667

@@ -72,6 +73,7 @@ export function getPortPromise(options?: PortFinderOptions): Promise<number>;
7273
/**
7374
* Responds with an array of unbound ports on the current machine.
7475
*/
76+
export function getPorts(count: number, options: PortFinderOptions): Promise<Array<number>>;
7577
export function getPorts(count: number, callback: (err: Error, ports: Array<number>) => void): void;
7678
export function getPorts(count: number, options: PortFinderOptions, callback: (err: Error, ports: Array<number>) => void): void;
7779

lib/portfinder.js

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,12 @@ exports.basePath = '/tmp/portfinder'
121121
//
122122
exports.setBasePath = function (path) {
123123
exports.basePath = path;
124-
}
124+
};
125125

126-
// ### function getPort (options, callback)
127-
// #### @options {Object} Settings to use when finding the necessary port
128-
// #### @callback {function} Continuation to respond to when complete.
129-
// Responds with a unbound port on the current machine.
130-
//
131-
exports.getPort = function (options, callback) {
126+
internals.getPort = function (options, callback) {
132127
if (!callback) {
133128
callback = options;
134129
options = {};
135-
136130
}
137131

138132
options.port = Number(options.port) || Number(exports.basePort);
@@ -220,33 +214,39 @@ exports.getPort = function (options, callback) {
220214
});
221215
};
222216

223-
//
224-
// ### function getPortPromise (options)
217+
// ### function getPort (options, callback)
225218
// #### @options {Object} Settings to use when finding the necessary port
226-
// Responds a promise to an unbound port on the current machine.
219+
// #### @callback {function} Continuation to respond to when complete.
220+
// Responds with a unbound port on the current machine.
227221
//
228-
exports.getPortPromise = function (options) {
229-
if (!options) {
222+
exports.getPort = function (options, callback) {
223+
if (!callback) {
224+
callback = options;
230225
options = {};
231226
}
232-
return new Promise(function(resolve, reject) {
233-
exports.getPort(options, function(err, port) {
234-
if (err) {
235-
return reject(err);
236-
}
237-
resolve(port);
227+
228+
if (!callback) {
229+
return new Promise(function (resolve, reject) {
230+
internals.getPort(options, function (err, port) {
231+
if (err) {
232+
return reject(err);
233+
}
234+
resolve(port);
235+
});
238236
});
239-
});
240-
}
237+
} else {
238+
return internals.getPort(options, callback);
239+
}
240+
};
241241

242242
//
243-
// ### function getPorts (count, options, callback)
244-
// #### @count {Number} The number of ports to find
243+
// ### function getPortPromise (options)
245244
// #### @options {Object} Settings to use when finding the necessary port
246-
// #### @callback {function} Continuation to respond to when complete.
247-
// Responds with an array of unbound ports on the current machine.
245+
// Responds a promise to an unbound port on the current machine.
248246
//
249-
exports.getPorts = function (count, options, callback) {
247+
exports.getPortPromise = exports.getPort;
248+
249+
internals.getPorts = function (count, options, callback) {
250250
if (!callback) {
251251
callback = options;
252252
options = {};
@@ -270,24 +270,39 @@ exports.getPorts = function (count, options, callback) {
270270
};
271271

272272
//
273-
// ### function getPortPromise (options)
273+
// ### function getPorts (count, options, callback)
274274
// #### @count {Number} The number of ports to find
275275
// #### @options {Object} Settings to use when finding the necessary port
276-
// Responds with a promise that resolves to an array of unbound ports on the current machine.
276+
// #### @callback {function} Continuation to respond to when complete.
277+
// Responds with an array of unbound ports on the current machine.
277278
//
278-
exports.getPortsPromise = function (count, options) {
279-
if (!options) {
279+
exports.getPorts = function (count, options, callback) {
280+
if (!callback) {
281+
callback = options;
280282
options = {};
281283
}
282-
return new Promise(function(resolve, reject) {
283-
exports.getPorts(count, options, function(err, ports) {
284-
if (err) {
285-
return reject(err);
286-
}
287-
resolve(ports);
284+
285+
if (!callback) {
286+
return new Promise(function(resolve, reject) {
287+
internals.getPorts(count, options, function(err, ports) {
288+
if (err) {
289+
return reject(err);
290+
}
291+
resolve(ports);
292+
});
288293
});
289-
});
290-
}
294+
} else {
295+
return internals.getPorts(count, options, callback);
296+
}
297+
};
298+
299+
//
300+
// ### function getPortPromise (options)
301+
// #### @count {Number} The number of ports to find
302+
// #### @options {Object} Settings to use when finding the necessary port
303+
// Responds with a promise that resolves to an array of unbound ports on the current machine.
304+
//
305+
exports.getPortsPromise = exports.getPorts;
291306

292307
//
293308
// ### function getSocket (options, callback)

test/port-finder-multiple.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ describe('with no existing servers', function () {
4040
});
4141
});
4242

43-
test('the getPortPromises() method with an argument of 3 should respond with the first three available ports (32768, 32769, 32770)', function (done) {
44-
portfinder.getPortsPromise(3)
43+
test.each([
44+
['getPorts()', portfinder.getPorts],
45+
['getPortsPromise()', portfinder.getPortsPromise],
46+
])('the %s promise method with an argument of 3 should respond with the first three available ports (32768, 32769, 32770)', function (name, method, done) {
47+
method(3)
4548
.then(function (ports) {
4649
expect(ports).toEqual([32768, 32769, 32770]);
4750
done();

test/port-finder.test.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,20 @@ describe('with 5 existing servers', function () {
6161
done();
6262
});
6363
});
64+
65+
test.each([
66+
['getPort()', portfinder.getPort],
67+
['getPortPromise()', portfinder.getPortPromise],
68+
])('the %s promise method should respond with the first free port (32773)', function (name, method, done) {
69+
method()
70+
.then(function (port) {
71+
expect(port).toEqual(32773);
72+
done();
73+
})
74+
.catch(function (err) {
75+
done(err);
76+
});
77+
});
6478
});
6579

6680
describe('with no existing servers', function () {
@@ -76,8 +90,11 @@ describe('with no existing servers', function () {
7690
});
7791
});
7892

79-
test('the getPortPromise() method should respond with a promise of first free port (32768)', function (done) {
80-
portfinder.getPortPromise()
93+
test.each([
94+
['getPort()', portfinder.getPort],
95+
['getPortPromise()', portfinder.getPortPromise],
96+
])('the %s promise method should respond with a promise of first free port (32768)', function (name, method, done) {
97+
method()
8198
.then(function (port) {
8299
expect(port).toEqual(32768);
83100
done();

0 commit comments

Comments
 (0)