@@ -79,9 +79,9 @@ exports.createServer = function () {
79
79
case 'number' : port = arg ; break ;
80
80
case 'object' : options = arg || { } ; break ;
81
81
case 'function' : callback = arg ; handlers . push ( callback ) ; break ;
82
- }
82
+ } ;
83
83
} ) ;
84
-
84
+
85
85
//
86
86
// Helper function to create intelligent error message(s)
87
87
// for the very liberal arguments parsing performed by
@@ -90,35 +90,36 @@ exports.createServer = function () {
90
90
function validArguments ( ) {
91
91
var conditions = {
92
92
'port and host' : function ( ) {
93
- return port && host ;
93
+ return port && host ;
94
94
} ,
95
95
'options.target or options.router' : function ( ) {
96
- return options && ( options . router ||
96
+ return options && ( options . router ||
97
97
( options . target && options . target . host && options . target . port ) ) ;
98
98
} ,
99
99
'or proxy handlers' : function ( ) {
100
100
return handlers && handlers . length ;
101
101
}
102
- } ;
103
-
102
+ }
103
+
104
104
var missing = Object . keys ( conditions ) . filter ( function ( name ) {
105
105
return ! conditions [ name ] ( ) ;
106
106
} ) ;
107
-
107
+
108
108
if ( missing . length === 3 ) {
109
109
message = 'Cannot proxy without ' + missing . join ( ', ' ) ;
110
110
return false ;
111
111
}
112
-
112
+
113
113
return true ;
114
- }
115
-
114
+ }
115
+
116
116
if ( ! validArguments ( ) ) {
117
117
//
118
- // If `host`, `port` and `options` are all not passed (with valid
118
+ // If `host`, `port` and `options` are all not passed (with valid
119
119
// options) then this server is improperly configured.
120
120
//
121
121
throw new Error ( message ) ;
122
+ return ;
122
123
}
123
124
124
125
//
@@ -129,7 +130,7 @@ exports.createServer = function () {
129
130
options . target = options . target || { } ;
130
131
options . target . port = options . target . port || port ;
131
132
options . target . host = options . target . host || host ;
132
-
133
+
133
134
if ( options . target && options . target . host && options . target . port ) {
134
135
//
135
136
// If an explicit `host` and `port` combination has been passed
@@ -147,31 +148,31 @@ exports.createServer = function () {
147
148
// we have to assume that this is a "go-anywhere" Proxy (i.e. a `RoutingProxy`).
148
149
//
149
150
proxy = new RoutingProxy ( options ) ;
150
-
151
+
151
152
if ( options . router ) {
152
153
//
153
- // If a routing table has been supplied than we assume
154
+ // If a routing table has been supplied than we assume
154
155
// the user intends us to add the "proxy" middleware layer
155
- // for them
156
+ // for them
156
157
//
157
158
handlers . push ( function ( req , res ) {
158
159
proxy . proxyRequest ( req , res ) ;
159
160
} ) ;
160
-
161
+
161
162
proxy . on ( 'routes' , function ( routes ) {
162
163
server . emit ( 'routes' , routes ) ;
163
164
} ) ;
164
- }
165
+ }
165
166
}
166
-
167
+
167
168
//
168
169
// Create the `http[s].Server` instance which will use
169
170
// an instance of `httpProxy.HttpProxy`.
170
171
//
171
- handler = handlers . length > 1
172
+ handler = handlers . length > 1
172
173
? exports . stack ( handlers , proxy )
173
174
: function ( req , res ) { handlers [ 0 ] ( req , res , proxy ) } ;
174
-
175
+
175
176
server = options . https
176
177
? https . createServer ( options . https , handler )
177
178
: http . createServer ( handler ) ;
@@ -183,8 +184,8 @@ exports.createServer = function () {
183
184
if ( ! callback ) {
184
185
//
185
186
// If an explicit callback has not been supplied then
186
- // automagically proxy the request using the `HttpProxy`
187
- // instance we have created.
187
+ // automagically proxy the request using the `HttpProxy`
188
+ // instance we have created.
188
189
//
189
190
server . on ( 'upgrade' , function ( req , socket , head ) {
190
191
proxy . proxyWebSocketRequest ( req , socket , head ) ;
@@ -221,7 +222,7 @@ exports.createServer = function () {
221
222
//
222
223
exports . buffer = function ( obj ) {
223
224
var events = [ ] ,
224
- onData ,
225
+ onData ,
225
226
onEnd ;
226
227
227
228
obj . on ( 'data' , onData = function ( data , encoding ) {
@@ -239,11 +240,11 @@ exports.buffer = function (obj) {
239
240
} ,
240
241
destroy : function ( ) {
241
242
this . end ( ) ;
242
- this . resume = function ( ) {
243
- console . error ( "Cannot resume buffer after destroying it." ) ;
244
- } ;
245
-
246
- onData = onEnd = events = obj = null ;
243
+ this . resume = function ( ) {
244
+ console . error ( "Cannot resume buffer after destroying it." ) ;
245
+ } ;
246
+
247
+ onData = onEnd = events = obj = null ;
247
248
} ,
248
249
resume : function ( ) {
249
250
this . end ( ) ;
@@ -277,10 +278,10 @@ exports.setMaxSockets = function (value) {
277
278
//
278
279
// ### function stack (middlewares, proxy)
279
280
// #### @middlewares {Array} Array of functions to stack.
280
- // #### @proxy {HttpProxy|RoutingProxy} Proxy instance to
281
+ // #### @proxy {HttpProxy|RoutingProxy} Proxy instance to
281
282
// Iteratively build up a single handler to the `http.Server`
282
283
// `request` event (i.e. `function (req, res)`) by wrapping
283
- // each middleware `layer` into a `child` middleware which
284
+ // each middleware `layer` into a `child` middleware which
284
285
// is in invoked by the parent (i.e. predecessor in the Array).
285
286
//
286
287
// adapted from https://github.com/creationix/stack
@@ -294,17 +295,17 @@ exports.stack = function stack (middlewares, proxy) {
294
295
if ( err ) {
295
296
if ( res . _headerSent ) {
296
297
res . destroy ( ) ;
297
- }
298
+ }
298
299
else {
299
300
res . statusCode = 500 ;
300
301
res . setHeader ( 'Content-Type' , 'text/plain' ) ;
301
302
res . end ( 'Internal Server Error' ) ;
302
303
}
303
-
304
+
304
305
console . error ( 'Error in middleware(s): %s' , err . stack ) ;
305
306
return ;
306
307
}
307
-
308
+
308
309
if ( child ) {
309
310
child ( req , res ) ;
310
311
}
@@ -343,29 +344,29 @@ exports._getAgent = function _getAgent (options) {
343
344
if ( ! options || ! options . host ) {
344
345
throw new Error ( '`options.host` is required to create an Agent.' ) ;
345
346
}
346
-
347
+
347
348
if ( ! options . port ) {
348
349
options . port = options . https ? 443 : 80 ;
349
350
}
350
351
351
352
var Agent = options . https ? https . Agent : http . Agent ,
352
353
agent ;
353
354
354
- agent = new Agent ( {
355
- host : options . host ,
355
+ agent = new Agent ( {
356
+ host : options . host ,
356
357
port : options . port
357
358
} ) ;
358
359
359
360
agent . maxSockets = options . maxSockets || maxSockets ;
360
361
361
362
return agent ;
362
- } ;
363
+ }
363
364
364
365
//
365
366
// ### function _getProtocol (options)
366
367
// #### @options {Object} Options for the proxy target.
367
- // Returns the appropriate node.js core protocol module (i.e. `http` or `https`)
368
- // based on the `options` supplied.
368
+ // Returns the appropriate node.js core protocol module (i.e. `http` or `https`)
369
+ // based on the `options` supplied.
369
370
//
370
371
exports . _getProtocol = function _getProtocol ( options ) {
371
372
return options . https ? https : http ;
@@ -381,7 +382,7 @@ exports._getProtocol = function _getProtocol (options) {
381
382
//
382
383
exports . _getBase = function _getBase ( options ) {
383
384
var result = function ( ) { } ;
384
-
385
+
385
386
if ( options . https && typeof options . https === 'object' ) {
386
387
[ 'ca' , 'cert' , 'key' ] . forEach ( function ( key ) {
387
388
if ( options . https [ key ] ) {
0 commit comments