@@ -68,19 +68,19 @@ function EventSource (url, eventSourceInitDict) {
68
68
config . jitterRatio ? retryDelay . defaultJitter ( config . jitterRatio ) : null
69
69
)
70
70
71
- function makeRequestOptions ( ) {
72
- var options = parse ( url )
73
- if ( config . skipDefaultHeaders ) {
74
- options . headers = { }
75
- } else {
76
- options . headers = { 'Cache-Control' : 'no-cache' , 'Accept' : 'text/event-stream' }
71
+ function makeRequestUrlAndOptions ( ) {
72
+ // Returns { url, options }; url is null/undefined if the URL properties are in options
73
+ var actualUrl = url
74
+ var options = { headers : { } }
75
+ if ( ! config . skipDefaultHeaders ) {
76
+ options . headers [ 'Cache-Control' ] = 'no-cache'
77
+ options . headers [ 'Accept' ] = 'text/event-stream'
77
78
}
78
79
if ( lastEventId ) options . headers [ 'Last-Event-ID' ] = lastEventId
79
80
if ( config . headers ) {
80
- for ( var i in config . headers ) {
81
- var header = config . headers [ i ]
82
- if ( header ) {
83
- options . headers [ i ] = header
81
+ for ( var key in config . headers ) {
82
+ if ( config . headers . hasOwnProperty ( key ) ) {
83
+ options . headers [ key ] = config . headers [ key ]
84
84
}
85
85
}
86
86
}
@@ -91,15 +91,19 @@ function EventSource (url, eventSourceInitDict) {
91
91
92
92
// If specify http proxy, make the request to sent to the proxy server,
93
93
// and include the original url in path and Host headers
94
- var useProxy = config . proxy
95
- if ( useProxy ) {
94
+ if ( config . proxy ) {
95
+ actualUrl = null
96
+ var parsedUrl = parse ( url )
96
97
var proxy = parse ( config . proxy )
97
98
options . protocol = proxy . protocol === 'https:' ? 'https:' : 'http:'
98
99
options . path = url
99
- options . headers . Host = options . host
100
+ options . headers . Host = parsedUrl . host
100
101
options . hostname = proxy . hostname
101
102
options . host = proxy . host
102
103
options . port = proxy . port
104
+ if ( proxy . username ) {
105
+ options . auth = proxy . username + ':' + proxy . password
106
+ }
103
107
}
104
108
105
109
// When running in Node, proxies can also be specified as an agent
@@ -130,7 +134,7 @@ function EventSource (url, eventSourceInitDict) {
130
134
options . method = config . method
131
135
}
132
136
133
- return options
137
+ return { url : actualUrl , options : options }
134
138
}
135
139
136
140
function defaultErrorFilter ( error ) {
@@ -179,10 +183,11 @@ function EventSource (url, eventSourceInitDict) {
179
183
}
180
184
181
185
function connect ( ) {
182
- var options = makeRequestOptions ( )
183
- var isSecure = options . protocol === 'https:'
186
+ var urlAndOptions = makeRequestUrlAndOptions ( )
187
+ var isSecure = urlAndOptions . options . protocol === 'https:' ||
188
+ ( urlAndOptions . url && urlAndOptions . url . startsWith ( 'https:' ) )
184
189
185
- req = ( isSecure ? https : http ) . request ( options , function ( res ) {
190
+ var callback = function ( res ) {
186
191
// Handle HTTP redirects
187
192
if ( res . statusCode === 301 || res . statusCode === 307 ) {
188
193
if ( ! res . headers . location ) {
@@ -279,7 +284,11 @@ function EventSource (url, eventSourceInitDict) {
279
284
buf = buf . slice ( pos )
280
285
}
281
286
} )
282
- } )
287
+ }
288
+ var api = isSecure ? https : http
289
+ req = urlAndOptions . url ?
290
+ api . request ( urlAndOptions . url , urlAndOptions . options , callback ) :
291
+ api . request ( urlAndOptions . options , callback )
283
292
284
293
if ( config . readTimeoutMillis ) {
285
294
req . setTimeout ( config . readTimeoutMillis )
0 commit comments