Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/portfinder.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export function setBasePath(path: string): void;
/**
* Responds with a unbound port on the current machine.
*/
export function getPort(options: PortFinderOptions): Promise<number>;
export function getPort(callback: PortfinderCallback): void;
export function getPort(options: PortFinderOptions, callback: PortfinderCallback): void;

Expand All @@ -72,6 +73,7 @@ export function getPortPromise(options?: PortFinderOptions): Promise<number>;
/**
* Responds with an array of unbound ports on the current machine.
*/
export function getPorts(count: number, options: PortFinderOptions): Promise<Array<number>>;
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;

Expand Down
91 changes: 53 additions & 38 deletions lib/portfinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,12 @@ exports.basePath = '/tmp/portfinder'
//
exports.setBasePath = function (path) {
exports.basePath = path;
}
};

// ### function getPort (options, callback)
// #### @options {Object} Settings to use when finding the necessary port
// #### @callback {function} Continuation to respond to when complete.
// Responds with a unbound port on the current machine.
//
exports.getPort = function (options, callback) {
internals.getPort = function (options, callback) {
if (!callback) {
callback = options;
options = {};

}

options.port = Number(options.port) || Number(exports.basePort);
Expand Down Expand Up @@ -220,33 +214,39 @@ exports.getPort = function (options, callback) {
});
};

//
// ### function getPortPromise (options)
// ### function getPort (options, callback)
// #### @options {Object} Settings to use when finding the necessary port
// Responds a promise to an unbound port on the current machine.
// #### @callback {function} Continuation to respond to when complete.
// Responds with a unbound port on the current machine.
//
exports.getPortPromise = function (options) {
if (!options) {
exports.getPort = function (options, callback) {
if (!callback) {
callback = options;
options = {};
}
return new Promise(function(resolve, reject) {
exports.getPort(options, function(err, port) {
if (err) {
return reject(err);
}
resolve(port);

if (!callback) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very good call btw

return new Promise(function (resolve, reject) {
internals.getPort(options, function (err, port) {
if (err) {
return reject(err);
}
resolve(port);
});
});
});
}
} else {
return internals.getPort(options, callback);
}
};

//
// ### function getPorts (count, options, callback)
// #### @count {Number} The number of ports to find
// ### function getPortPromise (options)
// #### @options {Object} Settings to use when finding the necessary port
// #### @callback {function} Continuation to respond to when complete.
// Responds with an array of unbound ports on the current machine.
// Responds a promise to an unbound port on the current machine.
//
exports.getPorts = function (count, options, callback) {
exports.getPortPromise = exports.getPort;

internals.getPorts = function (count, options, callback) {
if (!callback) {
callback = options;
options = {};
Expand All @@ -270,24 +270,39 @@ exports.getPorts = function (count, options, callback) {
};

//
// ### function getPortPromise (options)
// ### function getPorts (count, options, callback)
// #### @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.
// #### @callback {function} Continuation to respond to when complete.
// Responds with an array of unbound ports on the current machine.
//
exports.getPortsPromise = function (count, options) {
if (!options) {
exports.getPorts = function (count, options, callback) {
if (!callback) {
callback = options;
options = {};
}
return new Promise(function(resolve, reject) {
exports.getPorts(count, options, function(err, ports) {
if (err) {
return reject(err);
}
resolve(ports);

if (!callback) {
return new Promise(function(resolve, reject) {
internals.getPorts(count, options, function(err, ports) {
if (err) {
return reject(err);
}
resolve(ports);
});
});
});
}
} else {
return internals.getPorts(count, options, 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 = exports.getPorts;

//
// ### function getSocket (options, callback)
Expand Down
7 changes: 5 additions & 2 deletions test/port-finder-multiple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ describe('with no existing servers', function () {
});
});

test('the getPortPromises() method with an argument of 3 should respond with the first three available ports (32768, 32769, 32770)', function (done) {
portfinder.getPortsPromise(3)
test.each([
['getPorts()', portfinder.getPorts],
['getPortsPromise()', portfinder.getPortsPromise],
])('the %s promise method with an argument of 3 should respond with the first three available ports (32768, 32769, 32770)', function (name, method, done) {
method(3)
.then(function (ports) {
expect(ports).toEqual([32768, 32769, 32770]);
done();
Expand Down
21 changes: 19 additions & 2 deletions test/port-finder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ describe('with 5 existing servers', function () {
done();
});
});

test.each([
['getPort()', portfinder.getPort],
['getPortPromise()', portfinder.getPortPromise],
])('the %s promise method should respond with the first free port (32773)', function (name, method, done) {
method()
.then(function (port) {
expect(port).toEqual(32773);
done();
})
.catch(function (err) {
done(err);
});
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TY

});

describe('with no existing servers', function () {
Expand All @@ -76,8 +90,11 @@ describe('with no existing servers', function () {
});
});

test('the getPortPromise() method should respond with a promise of first free port (32768)', function (done) {
portfinder.getPortPromise()
test.each([
['getPort()', portfinder.getPort],
['getPortPromise()', portfinder.getPortPromise],
])('the %s promise method should respond with a promise of first free port (32768)', function (name, method, done) {
method()
.then(function (port) {
expect(port).toEqual(32768);
done();
Expand Down