Skip to content

Commit 566491d

Browse files
committed
Merge branch 'master' into chore-jest
* master: Release v1.0.35 broken npm lock npm lock after removing mkdirp dep Fix returning promise with passing options getPort/getPorts return promise if no callback # Conflicts: # package-lock.json
2 parents b3bac4c + 8460965 commit 566491d

File tree

6 files changed

+618
-2694
lines changed

6 files changed

+618
-2694
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: 60 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,9 @@ exports.basePath = '/tmp/portfinder'
121121
//
122122
exports.setBasePath = function (path) {
123123
exports.basePath = path;
124-
}
125-
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) {
132-
if (!callback) {
133-
callback = options;
134-
options = {};
135-
136-
}
124+
};
137125

126+
internals.getPort = function (options, callback) {
138127
options.port = Number(options.port) || Number(exports.basePort);
139128
options.host = options.host || null;
140129
options.stopPort = Number(options.stopPort) || Number(exports.highestPort);
@@ -145,7 +134,7 @@ exports.getPort = function (options, callback) {
145134
throw Error('Provided options.startPort(' + options.startPort + ') is less than 0, which are cannot be bound.');
146135
}
147136
if(options.stopPort < options.startPort) {
148-
throw Error('Provided options.stopPort(' + options.stopPort + 'is less than options.startPort (' + options.startPort + ')');
137+
throw Error('Provided options.stopPort(' + options.stopPort + ') is less than options.startPort (' + options.startPort + ')');
149138
}
150139
}
151140

@@ -188,7 +177,7 @@ exports.getPort = function (options, callback) {
188177
} else {
189178
const idx = exports._defaultHosts.indexOf(currentHost);
190179
exports._defaultHosts.splice(idx, 1);
191-
return exports.getPort(options, callback);
180+
return internals.getPort(options, callback);
192181
}
193182
} else {
194183
// error is not accounted for, file ticket, handle special case
@@ -214,51 +203,54 @@ exports.getPort = function (options, callback) {
214203
}
215204
} else {
216205
// otherwise, try again, using sorted port, aka, highest open for >= 1 host
217-
return exports.getPort({ port: openPorts.pop(), host: options.host, startPort: options.startPort, stopPort: options.stopPort }, callback);
206+
return internals.getPort({ port: openPorts.pop(), host: options.host, startPort: options.startPort, stopPort: options.stopPort }, callback);
218207
}
219208

220209
});
221210
};
222211

223-
//
224-
// ### function getPortPromise (options)
212+
// ### function getPort (options, callback)
225213
// #### @options {Object} Settings to use when finding the necessary port
226-
// Responds a promise to an unbound port on the current machine.
214+
// #### @callback {function} Continuation to respond to when complete.
215+
// Responds with a unbound port on the current machine.
227216
//
228-
exports.getPortPromise = function (options) {
229-
if (!options) {
217+
exports.getPort = function (options, callback) {
218+
if (typeof options === 'function') {
219+
callback = options;
230220
options = {};
231221
}
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);
222+
223+
options = options || {};
224+
225+
if (!callback) {
226+
return new Promise(function (resolve, reject) {
227+
internals.getPort(options, function (err, port) {
228+
if (err) {
229+
return reject(err);
230+
}
231+
resolve(port);
232+
});
238233
});
239-
});
240-
}
234+
} else {
235+
return internals.getPort(options, callback);
236+
}
237+
};
241238

242239
//
243-
// ### function getPorts (count, options, callback)
244-
// #### @count {Number} The number of ports to find
240+
// ### function getPortPromise (options)
245241
// #### @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.
242+
// Responds a promise to an unbound port on the current machine.
248243
//
249-
exports.getPorts = function (count, options, callback) {
250-
if (!callback) {
251-
callback = options;
252-
options = {};
253-
}
244+
exports.getPortPromise = exports.getPort;
254245

246+
internals.getPorts = function (count, options, callback) {
255247
let lastPort = null;
256248
_async.timesSeries(count, function(index, asyncCallback) {
257249
if (lastPort) {
258250
options.port = exports.nextPort(lastPort);
259251
}
260252

261-
exports.getPort(options, function (err, port) {
253+
internals.getPort(options, function (err, port) {
262254
if (err) {
263255
asyncCallback(err);
264256
} else {
@@ -270,24 +262,41 @@ exports.getPorts = function (count, options, callback) {
270262
};
271263

272264
//
273-
// ### function getPortPromise (options)
265+
// ### function getPorts (count, options, callback)
274266
// #### @count {Number} The number of ports to find
275267
// #### @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.
268+
// #### @callback {function} Continuation to respond to when complete.
269+
// Responds with an array of unbound ports on the current machine.
277270
//
278-
exports.getPortsPromise = function (count, options) {
279-
if (!options) {
271+
exports.getPorts = function (count, options, callback) {
272+
if (typeof options === 'function') {
273+
callback = options;
280274
options = {};
281275
}
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);
276+
277+
options = options || {};
278+
279+
if (!callback) {
280+
return new Promise(function(resolve, reject) {
281+
internals.getPorts(count, options, function(err, ports) {
282+
if (err) {
283+
return reject(err);
284+
}
285+
resolve(ports);
286+
});
288287
});
289-
});
290-
}
288+
} else {
289+
return internals.getPorts(count, options, callback);
290+
}
291+
};
292+
293+
//
294+
// ### function getPortPromise (options)
295+
// #### @count {Number} The number of ports to find
296+
// #### @options {Object} Settings to use when finding the necessary port
297+
// Responds with a promise that resolves to an array of unbound ports on the current machine.
298+
//
299+
exports.getPortsPromise = exports.getPorts;
291300

292301
//
293302
// ### function getSocket (options, callback)

0 commit comments

Comments
 (0)