Skip to content

Commit 5aa1fb2

Browse files
committed
Claim a specific port without try other ports
1 parent 0e7f67d commit 5aa1fb2

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

lib/harbor.js

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Object.defineProperty(Harbor.prototype, 'claimed',
8585
});
8686

8787
/**
88-
* ### .claim (name, cb)
88+
* ### _claim (name, cb)
8989
*
9090
* Find an available port from the pool of open
9191
* ports and claim it for a given name.
@@ -94,11 +94,11 @@ Object.defineProperty(Harbor.prototype, 'claimed',
9494
* @param {Function} callback
9595
* @cb {Error|null}
9696
* @cb {Number} claimed port
97-
* @name claim
98-
* @api public
97+
* @name _claim
98+
* @api private
9999
*/
100100

101-
Harbor.prototype.claim = function (name, cb) {
101+
function _claim(name, cb) {
102102
var self = this;
103103

104104
if (this.ports[name]) {
@@ -113,6 +113,43 @@ Harbor.prototype.claim = function (name, cb) {
113113
});
114114
};
115115

116+
/**
117+
* ### .claim (name, cb)
118+
*
119+
* Find an available port from the pool of open
120+
* ports (or use a provided port) and claim it for a given name.
121+
*
122+
* @param {String} name
123+
* @param {Number} [port]
124+
* @param {Function} callback
125+
* @cb {Error|null}
126+
* @cb {Number} claimed port
127+
* @name claim
128+
* @api public
129+
*/
130+
131+
Harbor.prototype.claim = function (name, port, cb) {
132+
var self = this;
133+
134+
if ('function' === typeof port) {
135+
return _claim.call(this, name, port);
136+
}
137+
138+
var _port = parseInt(port, 10);
139+
if (!isNaN(_port) && _port > 0) {
140+
if (this.ports[name]) {
141+
return cb(null, this.ports[name]);
142+
}
143+
144+
checkPort.call(this, _port, _port, function (err, port) {
145+
if (err) return cb(err);
146+
self.ports[name] = port;
147+
self.emit('claim', name, port);
148+
cb(null, port);
149+
});
150+
}
151+
};
152+
116153
/**
117154
* ### .release (name)
118155
*
@@ -140,13 +177,19 @@ Harbor.prototype.release = function (name) {
140177
* Will attempt to connect to a given port. If success,
141178
* will disconnect and pass that number to a callback.
142179
*
143-
* @param {Object} range min/max
180+
* @param {Number} num Port to check
181+
* @param {Number} max Max port to check
144182
* @param {Function} callback
145183
*/
146184

147-
function checkPort (num, cb) {
185+
function checkPort (num, max, cb) {
148186
var self = this;
149187

188+
if ('function' === typeof max) {
189+
cb = max;
190+
max = this.max;
191+
}
192+
150193
// if already claimed, skip
151194
if (~this.claimed.indexOf(num)) {
152195
return process.nextTick(function () {
@@ -158,7 +201,7 @@ function checkPort (num, cb) {
158201

159202
// if error, we don't want this server
160203
server.on('error', function (err) {
161-
if (num == self.max) {
204+
if (num == max) {
162205
self.emit('full');
163206
return cb(new Error('No ports available in range.'));
164207
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "harbor",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "TCP port availability and assignment utility.",
55
"main": "index.js",
66
"scripts": {

test/harbor.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ describe('harbor', function () {
5959
finder.ports.should.not.have.property('http');
6060
});
6161

62+
it('can claim a specific port', function (done) {
63+
var claim = chai.spy(function (name, port) {
64+
name.should.equal('http');
65+
port.should.equal(4300);
66+
});
67+
68+
var full = chai.spy();
69+
70+
finder.on('claim', claim);
71+
finder.on('full', full);
72+
73+
finder.claim('http', 4300, function (err, port) {
74+
Should.not.exist(err);
75+
port.should.equal(4300);
76+
finder.ports.should.have.property('http', 4300);
77+
finder.claimed.should.include(4300);
78+
claim.should.have.been.called.once;
79+
full.should.have.not.been.called();
80+
done();
81+
});
82+
});
6283
});
6384

6485
describe('when port not available', function () {

0 commit comments

Comments
 (0)