1
1
/**
2
- * Copyright (C) 2016-2023 Maxime Petazzoni <[email protected] >.
2
+ * Copyright (C) 2016-2024 Maxime Petazzoni <[email protected] >.
3
3
* All rights reserved.
4
4
*/
5
5
@@ -37,7 +37,7 @@ var SSE = function (url, options) {
37
37
/** @type {string } */
38
38
this . FIELD_SEPARATOR = ':' ;
39
39
40
- /** @type { {[key: string]: EventListener} } */
40
+ /** @type { {[key: string]: [ EventListener] } } */
41
41
this . listeners = { } ;
42
42
43
43
/** @type {XMLHttpRequest } */
@@ -70,7 +70,7 @@ var SSE = function (url, options) {
70
70
return ;
71
71
}
72
72
73
- var filtered = [ ] ;
73
+ const filtered = [ ] ;
74
74
this . listeners [ type ] . forEach ( function ( element ) {
75
75
if ( element !== listener ) {
76
76
filtered . push ( element ) ;
@@ -97,7 +97,7 @@ var SSE = function (url, options) {
97
97
98
98
e . source = this ;
99
99
100
- var onHandler = 'on' + e . type ;
100
+ const onHandler = 'on' + e . type ;
101
101
if ( this . hasOwnProperty ( onHandler ) ) {
102
102
this [ onHandler ] . call ( this , e ) ;
103
103
if ( e . defaultPrevented ) {
@@ -117,20 +117,20 @@ var SSE = function (url, options) {
117
117
118
118
/** @private */
119
119
this . _setReadyState = function ( state ) {
120
- var event = new CustomEvent ( 'readystatechange' ) ;
120
+ const event = new CustomEvent ( 'readystatechange' ) ;
121
121
event . readyState = state ;
122
122
this . readyState = state ;
123
123
this . dispatchEvent ( event ) ;
124
124
} ;
125
125
126
126
this . _onStreamFailure = function ( e ) {
127
- var event = new CustomEvent ( 'error' ) ;
127
+ const event = new CustomEvent ( 'error' ) ;
128
128
event . data = e . currentTarget . response ;
129
129
this . dispatchEvent ( event ) ;
130
130
this . close ( ) ;
131
131
}
132
132
133
- this . _onStreamAbort = function ( e ) {
133
+ this . _onStreamAbort = function ( ) {
134
134
this . dispatchEvent ( new CustomEvent ( 'abort' ) ) ;
135
135
this . close ( ) ;
136
136
}
@@ -146,19 +146,21 @@ var SSE = function (url, options) {
146
146
return ;
147
147
}
148
148
149
- if ( this . readyState == this . CONNECTING ) {
149
+ if ( this . readyState === this . CONNECTING ) {
150
150
this . dispatchEvent ( new CustomEvent ( 'open' ) ) ;
151
151
this . _setReadyState ( this . OPEN ) ;
152
152
}
153
153
154
- var data = this . xhr . responseText . substring ( this . progress ) ;
154
+ const data = this . xhr . responseText . substring ( this . progress ) ;
155
155
156
156
this . progress += data . length ;
157
- var parts = ( this . chunk + data ) . split ( / ( \r \n \r \n | \r \r | \n \n ) / g) ;
157
+ const parts = ( this . chunk + data ) . split ( / ( \r \n \r \n | \r \r | \n \n ) / g) ;
158
158
159
- // we assume that the last chunk can be incomplete because of buffering or other network effects
160
- // so we always save the last part to merge it with the next incoming packet
161
- var lastPart = parts . pop ( ) ;
159
+ /*
160
+ * We assume that the last chunk can be incomplete because of buffering or other network effects,
161
+ * so we always save the last part to merge it with the next incoming packet
162
+ */
163
+ const lastPart = parts . pop ( ) ;
162
164
parts . forEach ( function ( part ) {
163
165
if ( part . trim ( ) . length > 0 ) {
164
166
this . dispatchEvent ( this . _parseEventChunk ( part ) ) ;
@@ -190,13 +192,13 @@ var SSE = function (url, options) {
190
192
console . debug ( chunk ) ;
191
193
}
192
194
193
- var e = { 'id' : null , 'retry' : null , 'data' : null , 'event' : null } ;
195
+ const e = { 'id' : null , 'retry' : null , 'data' : null , 'event' : null } ;
194
196
chunk . split ( / \n | \r \n | \r / ) . forEach ( function ( line ) {
195
- var index = line . indexOf ( this . FIELD_SEPARATOR ) ;
196
- var field , value ;
197
+ const index = line . indexOf ( this . FIELD_SEPARATOR ) ;
198
+ let field , value ;
197
199
if ( index > 0 ) {
198
200
// only first whitespace should be trimmed
199
- var skip = ( line [ index + 1 ] === ' ' ) ? 2 : 1 ;
201
+ const skip = ( line [ index + 1 ] === ' ' ) ? 2 : 1 ;
200
202
field = line . substring ( 0 , index ) ;
201
203
value = line . substring ( index + skip ) ;
202
204
} else if ( index < 0 ) {
@@ -256,7 +258,7 @@ var SSE = function (url, options) {
256
258
this . xhr . addEventListener ( 'error' , this . _onStreamFailure . bind ( this ) ) ;
257
259
this . xhr . addEventListener ( 'abort' , this . _onStreamAbort . bind ( this ) ) ;
258
260
this . xhr . open ( this . method , this . url ) ;
259
- for ( var header in this . headers ) {
261
+ for ( let header in this . headers ) {
260
262
this . xhr . setRequestHeader ( header , this . headers [ header ] ) ;
261
263
}
262
264
this . xhr . withCredentials = this . withCredentials ;
@@ -300,6 +302,7 @@ export { SSE };
300
302
* @property {string } [payload] - payload as a string
301
303
* @property {string } [method] - HTTP Method
302
304
* @property {boolean } [withCredentials] - flag, if credentials needed
305
+ * @property {boolean } [start] - flag, if streaming should start automatically
303
306
* @property {boolean } [debug] - debugging flag
304
307
*/
305
308
/**
0 commit comments