@@ -31,17 +31,30 @@ class EventSource {
31
31
this . timeout = options . timeout ?? 0 ;
32
32
this . timeoutBeforeConnection = options . timeoutBeforeConnection ?? 500 ;
33
33
this . withCredentials = options . withCredentials || false ;
34
- this . headers = options . headers || { } ;
35
34
this . body = options . body || undefined ;
36
35
this . debug = options . debug || false ;
37
36
this . interval = options . pollingInterval ?? 5000 ;
38
37
this . lineEndingCharacter = options . lineEndingCharacter || null ;
39
38
39
+ const defaultHeaders = {
40
+ Accept : 'text/event-stream' ,
41
+ 'Cache-Control' : 'no-cache' ,
42
+ 'X-Requested-With' : 'XMLHttpRequest' ,
43
+ } ;
44
+
45
+ this . headers = {
46
+ ...defaultHeaders ,
47
+ ...options . headers
48
+ } ;
49
+
40
50
this . _xhr = null ;
41
51
this . _pollTimer = null ;
42
52
this . _lastIndexProcessed = 0 ;
43
53
44
- if ( ! url || ( typeof url !== 'string' && typeof url . toString !== 'function' ) ) {
54
+ if (
55
+ ! url ||
56
+ ( typeof url !== 'string' && typeof url . toString !== 'function' )
57
+ ) {
45
58
throw new SyntaxError ( '[EventSource] Invalid URL argument.' ) ;
46
59
}
47
60
@@ -76,12 +89,8 @@ class EventSource {
76
89
this . _xhr . withCredentials = true ;
77
90
}
78
91
79
- this . _xhr . setRequestHeader ( 'Accept' , 'text/event-stream' ) ;
80
-
81
- if ( this . headers ) {
82
- for ( const [ key , value ] of Object . entries ( this . headers ) ) {
83
- this . _xhr . setRequestHeader ( key , value ) ;
84
- }
92
+ for ( const [ key , value ] of Object . entries ( this . headers ) ) {
93
+ if ( value ) this . _xhr . setRequestHeader ( key , value ) ;
85
94
}
86
95
87
96
if ( this . lastEventId !== null ) {
@@ -97,23 +106,34 @@ class EventSource {
97
106
98
107
const xhr = this . _xhr ;
99
108
100
- this . _logDebug ( `[EventSource][onreadystatechange] ReadyState: ${ XMLReadyStateMap [ xhr . readyState ] || 'Unknown' } (${ xhr . readyState } ), status: ${ xhr . status } ` ) ;
109
+ this . _logDebug (
110
+ `[EventSource][onreadystatechange] ReadyState: ${ XMLReadyStateMap [ xhr . readyState ] || 'Unknown'
111
+ } (${ xhr . readyState } ), status: ${ xhr . status } `
112
+ ) ;
101
113
102
- if ( ! [ XMLHttpRequest . DONE , XMLHttpRequest . LOADING ] . includes ( xhr . readyState ) ) {
114
+ if (
115
+ ! [ XMLHttpRequest . DONE , XMLHttpRequest . LOADING ] . includes (
116
+ xhr . readyState
117
+ )
118
+ ) {
103
119
return ;
104
120
}
105
121
106
122
if ( xhr . status >= 200 && xhr . status < 400 ) {
107
123
if ( this . status === this . CONNECTING ) {
108
124
this . status = this . OPEN ;
109
125
this . dispatch ( 'open' , { type : 'open' } ) ;
110
- this . _logDebug ( '[EventSource][onreadystatechange][OPEN] Connection opened.' ) ;
126
+ this . _logDebug (
127
+ '[EventSource][onreadystatechange][OPEN] Connection opened.'
128
+ ) ;
111
129
}
112
130
113
131
this . _handleEvent ( xhr . responseText || '' ) ;
114
132
115
133
if ( xhr . readyState === XMLHttpRequest . DONE ) {
116
- this . _logDebug ( '[EventSource][onreadystatechange][DONE] Operation done.' ) ;
134
+ this . _logDebug (
135
+ '[EventSource][onreadystatechange][DONE] Operation done.'
136
+ ) ;
117
137
this . _pollAgain ( this . interval , false ) ;
118
138
}
119
139
} else if ( xhr . status !== 0 ) {
@@ -126,7 +146,9 @@ class EventSource {
126
146
} ) ;
127
147
128
148
if ( xhr . readyState === XMLHttpRequest . DONE ) {
129
- this . _logDebug ( '[EventSource][onreadystatechange][ERROR] Response status error.' ) ;
149
+ this . _logDebug (
150
+ '[EventSource][onreadystatechange][ERROR] Response status error.'
151
+ ) ;
130
152
this . _pollAgain ( this . interval , false ) ;
131
153
}
132
154
}
@@ -180,10 +202,16 @@ class EventSource {
180
202
if ( this . lineEndingCharacter === null ) {
181
203
const detectedNewlineChar = this . _detectNewlineChar ( response ) ;
182
204
if ( detectedNewlineChar !== null ) {
183
- this . _logDebug ( `[EventSource] Automatically detected lineEndingCharacter: ${ JSON . stringify ( detectedNewlineChar ) . slice ( 1 , - 1 ) } ` ) ;
205
+ this . _logDebug (
206
+ `[EventSource] Automatically detected lineEndingCharacter: ${ JSON . stringify (
207
+ detectedNewlineChar
208
+ ) . slice ( 1 , - 1 ) } `
209
+ ) ;
184
210
this . lineEndingCharacter = detectedNewlineChar ;
185
211
} else {
186
- console . warn ( "[EventSource] Unable to identify the line ending character. Ensure your server delivers a standard line ending character: \\r\\n, \\n, \\r, or specify your custom character using the 'lineEndingCharacter' option." ) ;
212
+ console . warn (
213
+ '[EventSource] Unable to identify the line ending character. Ensure your server delivers a standard line ending character: \\r\\n, \\n, \\r, or specify your custom character using the ' lineEndingCharacter ' option.'
214
+ ) ;
187
215
return ;
188
216
}
189
217
}
@@ -193,7 +221,10 @@ class EventSource {
193
221
return ;
194
222
}
195
223
196
- const parts = response . substring ( this . _lastIndexProcessed , indexOfDoubleNewline ) . split ( this . lineEndingCharacter ) ;
224
+ const parts = response
225
+ . substring ( this . _lastIndexProcessed , indexOfDoubleNewline )
226
+ . split ( this . lineEndingCharacter ) ;
227
+
197
228
this . _lastIndexProcessed = indexOfDoubleNewline ;
198
229
199
230
let type = undefined ;
@@ -250,7 +281,8 @@ class EventSource {
250
281
}
251
282
252
283
_getLastDoubleNewlineIndex ( response ) {
253
- const doubleLineEndingCharacter = this . lineEndingCharacter + this . lineEndingCharacter ;
284
+ const doubleLineEndingCharacter =
285
+ this . lineEndingCharacter + this . lineEndingCharacter ;
254
286
const lastIndex = response . lastIndexOf ( doubleLineEndingCharacter ) ;
255
287
if ( lastIndex === - 1 ) {
256
288
return - 1 ;
@@ -269,7 +301,9 @@ class EventSource {
269
301
270
302
removeEventListener ( type , listener ) {
271
303
if ( this . eventHandlers [ type ] !== undefined ) {
272
- this . eventHandlers [ type ] = this . eventHandlers [ type ] . filter ( ( handler ) => handler !== listener ) ;
304
+ this . eventHandlers [ type ] = this . eventHandlers [ type ] . filter (
305
+ ( handler ) => handler !== listener
306
+ ) ;
273
307
}
274
308
}
275
309
@@ -282,7 +316,9 @@ class EventSource {
282
316
}
283
317
} else {
284
318
if ( ! availableTypes . includes ( type ) ) {
285
- throw Error ( `[EventSource] '${ type } ' type is not supported event type.` ) ;
319
+ throw Error (
320
+ `[EventSource] '${ type } ' type is not supported event type.`
321
+ ) ;
286
322
}
287
323
288
324
this . eventHandlers [ type ] = [ ] ;
0 commit comments