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