-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventSourceExtra.js
368 lines (307 loc) · 9.79 KB
/
EventSourceExtra.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
credits: https://github.com/mpetazzoni/sse.js
*/
/**
* EventSource replacement with some extra functionality
*/
class EventSourceExtra {
/**
* Creates a new instance of the SSE browser
* @param {string} url url to fetch
* @param {object} options fetch options
* @param {object} options.method fetch method GET | POST | DELETE | PATCH
* @param {*} options.payload payload to send
* @param {object} options.headers header to apply
* @param {boolean} options.withCredentials if credentials should be included in CORS requests default false
* @param {boolean} options.debug debug logging
*/
constructor(url, options = {}) {
this.INITIALIZING = -1;
this.CONNECTING = 0;
this.OPEN = 1;
this.CLOSED = 2;
this.url = url;
this.headers = options.headers || {};
this.payload = options.payload !== undefined ? options.payload : '';
this.method = options.method || (this.payload && 'POST' || 'GET');
this.withCredentials = !!options.withCredentials;
this.debug = options.debug || false; // debug logging
this.FIELD_SEPARATOR = ':';
this.listeners = {};
this.dataListeners = {};
this.xhr = null;
this.readyState = this.INITIALIZING;
this.progress = 0;
this.chunk = '';
this.retry = null;
}
addEventListener(type, listener) {
if (this.listeners[type] === undefined) {
this.listeners[type] = [];
}
if (this.listeners[type].indexOf(listener) === -1) {
this.listeners[type].push(listener);
}
}
removeEventListener(type, listener) {
if (this.listeners[type] === undefined) {
return;
}
var filtered = [];
this.listeners[type].forEach(function (element) {
if (element !== listener) {
filtered.push(element);
}
});
if (filtered.length === 0) {
delete this.listeners[type];
} else {
this.listeners[type] = filtered;
}
}
/**
* Event listener that emits data field only
* JSON data is parsed
* use addEventListener for complete event
* @param {string} type event name
* @param {function} listener event function to execute
*/
on(type, listener) {
if (this.dataListeners[type] === undefined) {
this.dataListeners[type] = [];
}
if (this.dataListeners[type].indexOf(listener) === -1) {
this.dataListeners[type].push(listener);
}
}
/**
* Removes E
* @param {string} type event name
* @param {function | undefined} listener listener function to remove, if undefined removes all listeners
*/
off(type, listener) {
if (!type) return;
if (this.dataListeners[type] === undefined) return;
// delete all
if (!listener) {
delete this.dataListeners[type];
}
// remove
let idx = this.dataListeners[type].indexOf(element => {
return element !== listener
});
if (idx > -1) this.dataListeners[type].splice(idx, 1)
// cleanup
if (this.dataListeners[type].length === 0) {
delete this.dataListeners[type];
}
}
_dispatchEvent(e) {
if (!e) return
e.source = this;
if (this.debug) {
console.log(e)
}
// retry handler
if (typeof e.retry !== 'undefined') {
this.retry = e.retry
}
// filter empty data messages
if (e.type == 'message' && e.data == '') return
// onhandler
var onHandler = 'on' + e.type;
if (this[onHandler]) {
this[onHandler].call(this, e);
}
// message => data emit
if (e.type == 'message' && this.ondata) {
this['ondata'].call(this, e);
}
// emit event
if (this.listeners[e.type]) {
// addEventListener bindings
this.listeners[e.type].forEach(listener => {
listener(e)
})
}
if (this.dataListeners[e.type]) {
// on(eventname) bindings emit data only
this.dataListeners[e.type].forEach(listener => {
let data
try {
data = JSON.parse(e.data)
} catch (error) {
data = e.data
}
listener(data || e.message)
})
}
// message => data emit
if (e.type == 'message' && this.listeners.data) {
// addEventListener bindings
this.listeners['data'].forEach(listener => {
listener(e)
})
}
if (e.type == 'message' && this.dataListeners.data) {
// on(eventname) bindings emit data only
this.dataListeners['data'].forEach(listener => {
let data
try {
data = JSON.parse(e.data)
} catch (error) {
data = e.data
}
listener(data || e.message)
})
}
return true;
}
_setReadyState(state) {
var event = new CustomEvent('readystatechange');
event.readyState = state;
this.readyState = state;
this._dispatchEvent(event);
}
_onStreamFailure(e) {
let event = new CustomEvent('error')
event.cause = e.type
event.message = 'stream failure'
event.retry = this.retry || false;
this._dispatchEvent(event);
if (this.retry) {
setTimeout(() => {
this.stream()
}, this.retry)
} else {
this.close();
}
}
_onStreamAbort(e) {
let event = new CustomEvent('error')
event.message = 'stream abort'
event.retry = this.retry || false;
this._dispatchEvent(event);
this.close();
}
_onStreamProgress(e) {
if (!this.xhr) {
return;
}
if (this.xhr.status !== 200) {
this._onStreamFailure(e);
return;
}
if (this.readyState == this.CONNECTING) {
this._dispatchEvent(new CustomEvent('open'));
this._setReadyState(this.OPEN);
}
var data = this.xhr.responseText.substring(this.progress);
this.progress += data.length;
data.split(/(\r\n|\r|\n){2}/g).forEach(function (part) {
if (part.trim().length === 0) {
this._dispatchEvent(this._parseEventChunk(this.chunk.trim()));
this.chunk = '';
} else {
this.chunk += part;
}
}.bind(this));
}
_onStreamLoaded(e) {
this._onStreamProgress(e);
// Parse the last chunk.
this._dispatchEvent(this._parseEventChunk(this.chunk));
this.chunk = '';
}
/**
* Parse a received SSE event chunk into a constructed event object.
*/
_parseEventChunk(chunk) {
if (!chunk || chunk.length === 0) {
return null;
}
var e = { 'id': null, 'retry': undefined, 'data': '', 'event': 'message' };
chunk.split(/\n|\r\n|\r/).forEach(function (line) {
line = line.trimRight();
var index = line.indexOf(this.FIELD_SEPARATOR);
if (index <= 0) {
// Line was either empty, or started with a separator and is a comment.
// Either way, ignore.
return;
}
var field = line.substring(0, index);
if (!(field in e)) {
return;
}
var value = line.substring(index + 1).trimLeft();
if (field === 'data') {
e[field] += value;
} else {
e[field] = value;
}
}.bind(this));
var event = new CustomEvent(e.event);
event.data = e.data;
event.id = e.id;
if (e.retry !== undefined) {
event.retry = isNaN(e.retry) ? null : parseInt(e.retry);
}
return event;
}
_checkStreamClosed() {
if (!this.xhr) {
return;
}
if (this.xhr.readyState === XMLHttpRequest.DONE) {
this._setReadyState(this.CLOSED);
}
}
/**
*
* @param {string} url url to fetch
* @param {object} options fetch options
* @param {object} options.method fetch method GET | POST | DELETE | PATCH
* @param {*} options.payload payload to send
* @param {object} options.headers header to apply
* @param {boolean} options.withCredentials if credentials should be included in CORS requests default false
* @param {boolean} options.debug debug logging
*/
fetch(url, options = {}) {
if (this.readyState === this.CONNECTING || this.readyState === this.OPEN) {
throw new Error('Connection in use, wait for close, or close before new fetch')
}
this.headers = options.headers || {};
this.withCredentials = options.withCredentials || false;
this.method = options.method || 'GET';
this.payload = options.payload || '';
this.debug = options.debug || this.debug || false;
this.url = url;
this.stream()
}
stream() {
if (this.readyState === this.CONNECTING || this.readyState === this.OPEN) {
throw new Error('Connection in use, wait for close, or close before new stream')
}
this._setReadyState(this.CONNECTING);
this.xhr = new XMLHttpRequest();
this.xhr.addEventListener('progress', this._onStreamProgress.bind(this));
this.xhr.addEventListener('load', this._onStreamLoaded.bind(this));
this.xhr.addEventListener('readystatechange', this._checkStreamClosed.bind(this));
this.xhr.addEventListener('error', this._onStreamFailure.bind(this));
this.xhr.addEventListener('abort', this._onStreamAbort.bind(this));
this.xhr.open(this.method, this.url);
for (var header in this.headers) {
this.xhr.setRequestHeader(header, this.headers[header]);
}
this.xhr.withCredentials = this.withCredentials;
this.xhr.send(this.payload);
}
close() {
if (this.readyState === this.CLOSED) {
return;
}
this.xhr.abort();
this.xhr = null;
this._setReadyState(this.CLOSED);
}
}