@@ -85,7 +85,7 @@ Object.defineProperty(Harbor.prototype, 'claimed',
85
85
} ) ;
86
86
87
87
/**
88
- * ### .claim (name, cb)
88
+ * ### _claim (name, cb)
89
89
*
90
90
* Find an available port from the pool of open
91
91
* ports and claim it for a given name.
@@ -94,11 +94,11 @@ Object.defineProperty(Harbor.prototype, 'claimed',
94
94
* @param {Function } callback
95
95
* @cb {Error|null}
96
96
* @cb {Number} claimed port
97
- * @name claim
98
- * @api public
97
+ * @name _claim
98
+ * @api private
99
99
*/
100
100
101
- Harbor . prototype . claim = function ( name , cb ) {
101
+ function _claim ( name , cb ) {
102
102
var self = this ;
103
103
104
104
if ( this . ports [ name ] ) {
@@ -113,6 +113,43 @@ Harbor.prototype.claim = function (name, cb) {
113
113
} ) ;
114
114
} ;
115
115
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
+
116
153
/**
117
154
* ### .release (name)
118
155
*
@@ -140,13 +177,19 @@ Harbor.prototype.release = function (name) {
140
177
* Will attempt to connect to a given port. If success,
141
178
* will disconnect and pass that number to a callback.
142
179
*
143
- * @param {Object } range min/max
180
+ * @param {Number } num Port to check
181
+ * @param {Number } max Max port to check
144
182
* @param {Function } callback
145
183
*/
146
184
147
- function checkPort ( num , cb ) {
185
+ function checkPort ( num , max , cb ) {
148
186
var self = this ;
149
187
188
+ if ( 'function' === typeof max ) {
189
+ cb = max ;
190
+ max = this . max ;
191
+ }
192
+
150
193
// if already claimed, skip
151
194
if ( ~ this . claimed . indexOf ( num ) ) {
152
195
return process . nextTick ( function ( ) {
@@ -158,7 +201,7 @@ function checkPort (num, cb) {
158
201
159
202
// if error, we don't want this server
160
203
server . on ( 'error' , function ( err ) {
161
- if ( num == self . max ) {
204
+ if ( num == max ) {
162
205
self . emit ( 'full' ) ;
163
206
return cb ( new Error ( 'No ports available in range.' ) ) ;
164
207
}
0 commit comments