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