@@ -169,7 +169,7 @@ function ConfigurableProxy (options) {
169
169
}
170
170
171
171
// proxy requests separately
172
- var proxy_callback = log_errors ( that . handle_proxy_web ) ;
172
+ var proxy_callback = log_errors ( this . handle_proxy_web ) ;
173
173
if ( this . options . ssl ) {
174
174
this . proxy_server = https . createServer ( this . options . ssl , proxy_callback ) ;
175
175
} else {
@@ -185,21 +185,33 @@ function ConfigurableProxy (options) {
185
185
186
186
util . inherits ( ConfigurableProxy , EventEmitter ) ;
187
187
188
- ConfigurableProxy . prototype . add_route = function ( path , data ) {
188
+ ConfigurableProxy . prototype . add_route = function ( path , data , cb ) {
189
189
// add a route to the routing table
190
190
path = this . _routes . cleanPath ( path ) ;
191
191
if ( this . host_routing && path !== '/' ) {
192
192
data . host = path . split ( '/' ) [ 1 ] ;
193
193
}
194
- this . _routes . add ( path , data ) ;
195
- this . update_last_activity ( path ) ;
194
+
195
+ var that = this ;
196
+
197
+ this . _routes . add ( path , data , function ( ) {
198
+ that . update_last_activity ( path , function ( ) {
199
+ if ( typeof ( cb ) === "function" ) {
200
+ cb ( ) ;
201
+ }
202
+ } ) ;
203
+ } ) ;
196
204
} ;
197
205
198
- ConfigurableProxy . prototype . remove_route = function ( path ) {
206
+ ConfigurableProxy . prototype . remove_route = function ( path , cb ) {
199
207
// remove a route from the routing table
200
- if ( this . _routes . hasRoute ( path ) ) {
201
- this . _routes . remove ( path ) ;
202
- }
208
+ var routes = this . _routes ;
209
+
210
+ routes . hasRoute ( path , function ( result ) {
211
+ if ( result ) {
212
+ routes . remove ( path , cb ) ;
213
+ }
214
+ } ) ;
203
215
} ;
204
216
205
217
ConfigurableProxy . prototype . get_routes = function ( req , res ) {
@@ -220,20 +232,25 @@ ConfigurableProxy.prototype.get_routes = function (req, res) {
220
232
}
221
233
}
222
234
res . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ;
223
- var routes = { } ;
224
- if ( inactive_since ) {
225
- Object . keys ( this . _routes . getAll ( ) ) . map ( function ( path ) {
226
- var route = that . _routes . get ( path ) ;
227
- if ( route . last_activity < inactive_since ) {
228
- routes [ path ] = route ;
229
- }
230
- } ) ;
231
- } else {
232
- routes = this . _routes . getAll ( ) ;
233
- }
234
- res . write ( JSON . stringify ( routes ) ) ;
235
- res . end ( ) ;
236
- this . statsd . increment ( 'api.route.get' , 1 ) ;
235
+
236
+ this . _routes . getAll ( function ( routes ) {
237
+ var results = { } ;
238
+
239
+ if ( inactive_since ) {
240
+ var keys = Object . keys ( routes ) . filter ( function ( key ) {
241
+ return routes [ key ] . last_activity < inactive_since ;
242
+ } ) ;
243
+
244
+ keys . forEach ( function ( key ) { results [ key ] = routes [ key ] ; } ) ;
245
+ } else {
246
+ results = routes ;
247
+ }
248
+
249
+
250
+ res . write ( JSON . stringify ( results ) ) ;
251
+ res . end ( ) ;
252
+ that . statsd . increment ( 'api.route.get' , 1 ) ;
253
+ } ) ;
237
254
} ;
238
255
239
256
ConfigurableProxy . prototype . post_routes = function ( req , res , path , data ) {
@@ -247,47 +264,63 @@ ConfigurableProxy.prototype.post_routes = function (req, res, path, data) {
247
264
return ;
248
265
}
249
266
250
- this . add_route ( path , data ) ;
251
- res . writeHead ( 201 ) ;
252
- res . end ( ) ;
253
- this . statsd . increment ( 'api.route.add' , 1 ) ;
267
+ var that = this ;
268
+ this . add_route ( path , data , function ( ) {
269
+ res . writeHead ( 201 ) ;
270
+ res . end ( ) ;
271
+ that . statsd . increment ( 'api.route.add' , 1 ) ;
272
+ } ) ;
254
273
} ;
255
274
256
275
ConfigurableProxy . prototype . delete_routes = function ( req , res , path ) {
257
276
// DELETE removes an existing route
258
277
log . debug ( 'DELETE' , path ) ;
259
- if ( this . _routes . hasRoute ( path ) ) {
260
- this . remove_route ( path ) ;
261
- res . writeHead ( 204 ) ;
262
- } else {
263
- res . writeHead ( 404 ) ;
264
- }
265
- res . end ( ) ;
266
- this . statsd . increment ( 'api.route.delete' , 1 ) ;
278
+
279
+ var that = this ;
280
+ this . _routes . hasRoute ( path , function ( result ) {
281
+ if ( result ) {
282
+ that . remove_route ( path , function ( ) {
283
+ res . writeHead ( 204 ) ;
284
+ res . end ( ) ;
285
+ that . statsd . increment ( 'api.route.delete' , 1 ) ;
286
+ } ) ;
287
+ } else {
288
+ res . writeHead ( 404 ) ;
289
+ res . end ( ) ;
290
+ that . statsd . increment ( 'api.route.delete' , 1 ) ;
291
+ }
292
+ } ) ;
267
293
} ;
268
294
269
- ConfigurableProxy . prototype . target_for_req = function ( req ) {
295
+ ConfigurableProxy . prototype . target_for_req = function ( req , cb ) {
270
296
var timer = this . statsd . createTimer ( 'find_target_for_req' ) ;
271
297
// return proxy target for a given url path
272
298
var base_path = ( this . host_routing ) ? '/' + parse_host ( req ) : '' ;
273
- var route = this . _routes . getTarget ( base_path + decodeURIComponent ( req . url ) ) ;
274
- timer . stop ( ) ;
275
- if ( route ) {
276
- return {
277
- prefix : route . prefix ,
278
- target : route . data . target ,
279
- } ;
280
- }
299
+
300
+ this . _routes . getTarget ( base_path + decodeURIComponent ( req . url ) , function ( route ) {
301
+ timer . stop ( ) ;
302
+ var result = route ? { prefix : route . prefix , target : route . data . target } : null ;
303
+ cb ( result ) ;
304
+ } ) ;
281
305
} ;
282
306
283
- ConfigurableProxy . prototype . update_last_activity = function ( prefix ) {
307
+ ConfigurableProxy . prototype . update_last_activity = function ( prefix , cb ) {
284
308
var timer = this . statsd . createTimer ( 'last_activity_updating' ) ;
285
- // note last activity in routing table
286
- if ( this . _routes . hasRoute ( prefix ) ) {
287
- // route may have been deleted with open connections
288
- this . _routes . update ( prefix , { "last_activity" : new Date ( ) } ) ;
289
- }
290
- timer . stop ( ) ;
309
+ var routes = this . _routes ;
310
+
311
+ routes . hasRoute ( prefix , function ( result ) {
312
+ cb = cb || function ( ) { } ;
313
+
314
+ if ( result ) {
315
+ routes . update ( prefix , { "last_activity" : new Date ( ) } , function ( ) {
316
+ timer . stop ( ) ;
317
+ cb ( ) ;
318
+ } ) ;
319
+ } else {
320
+ timer . stop ( ) ;
321
+ cb ( ) ;
322
+ }
323
+ } ) ;
291
324
} ;
292
325
293
326
ConfigurableProxy . prototype . _handle_proxy_error_default = function ( code , kind , req , res ) {
@@ -360,48 +393,47 @@ ConfigurableProxy.prototype.handle_proxy_error = function (code, kind, req, res)
360
393
ConfigurableProxy . prototype . handle_proxy = function ( kind , req , res ) {
361
394
// proxy any request
362
395
var that = this ;
396
+ var args = Array . prototype . slice . call ( arguments , 1 ) ;
397
+
363
398
// get the proxy target
364
- var match = this . target_for_req ( req ) ;
365
- if ( ! match ) {
366
- this . handle_proxy_error ( 404 , kind , req , res ) ;
367
- return ;
368
- }
369
- this . emit ( "proxy_request" , req , res ) ;
370
- var prefix = match . prefix ;
371
- var target = match . target ;
372
- log . debug ( "PROXY" , kind . toUpperCase ( ) , req . url , "to" , target ) ;
373
- if ( ! this . includePrefix ) {
374
- req . url = req . url . slice ( prefix . length ) ;
375
- }
399
+ this . target_for_req ( req , function ( match ) {
400
+ if ( ! match ) {
401
+ that . handle_proxy_error ( 404 , kind , req , res ) ;
402
+ return ;
403
+ }
376
404
377
- // pop method off the front
378
- var args = arguments_array ( arguments ) ;
379
- args . shift ( ) ;
405
+ that . emit ( "proxy_request" , req , res ) ;
406
+ var prefix = match . prefix ;
407
+ var target = match . target ;
408
+ log . debug ( "PROXY" , kind . toUpperCase ( ) , req . url , "to" , target ) ;
409
+ if ( ! that . includePrefix ) {
410
+ req . url = req . url . slice ( prefix . length ) ;
411
+ }
380
412
381
- // add config argument
382
- args . push ( {
383
- target : target
384
- } ) ;
413
+ // add config argument
414
+ args . push ( { target : target } ) ;
385
415
386
- // add error handling
387
- args . push ( function ( e ) {
388
- log . error ( "Proxy error: " , e ) ;
389
- that . handle_proxy_error ( 503 , kind , req , res ) ;
390
- } ) ;
416
+ // add error handling
417
+ args . push ( function ( e ) {
418
+ log . error ( "Proxy error: " , e ) ;
419
+ that . handle_proxy_error ( 503 , kind , req , res ) ;
420
+ } ) ;
391
421
392
- // update last activity timestamp in routing table
393
- this . update_last_activity ( prefix ) ;
422
+ // update timestamp on any reply data as well (this includes websocket data)
423
+ req . on ( 'data' , function ( ) {
424
+ that . update_last_activity ( prefix ) ;
425
+ } ) ;
394
426
395
- // update timestamp on any reply data as well (this includes websocket data)
396
- req . on ( 'data' , function ( ) {
397
- that . update_last_activity ( prefix ) ;
398
- } ) ;
399
- res . on ( 'data' , function ( ) {
400
- that . update_last_activity ( prefix ) ;
401
- } ) ;
427
+ res . on ( 'data' , function ( ) {
428
+ that . update_last_activity ( prefix ) ;
429
+ } ) ;
402
430
403
- // dispatch the actual method
404
- this . proxy [ kind ] . apply ( this . proxy , args ) ;
431
+ // update last activity timestamp in routing table
432
+ that . update_last_activity ( prefix , function ( ) {
433
+ // dispatch the actual method
434
+ that . proxy [ kind ] . apply ( that . proxy , args ) ;
435
+ } ) ;
436
+ } ) ;
405
437
} ;
406
438
407
439
ConfigurableProxy . prototype . handle_proxy_ws = function ( req , res , head ) {
0 commit comments