forked from Yaffle/EventSource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventsource.js
executable file
·541 lines (490 loc) · 16 KB
/
eventsource.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/** @license
* eventsource.js
* Available under MIT License (MIT)
* https://github.com/Yaffle/EventSource/
*/
/*jslint indent: 2, vars: true, plusplus: true */
/*global setTimeout, clearTimeout */
(function (global) {
"use strict";
var setTimeout = global.setTimeout;
var clearTimeout = global.clearTimeout;
function Map() {
this.data = {};
}
Map.prototype.get = function (key) {
return this.data[key + "~"];
};
Map.prototype.set = function (key, value) {
this.data[key + "~"] = value;
};
Map.prototype["delete"] = function (key) {
delete this.data[key + "~"];
};
function EventTarget() {
this.listeners = new Map();
}
function throwError(e) {
setTimeout(function () {
throw e;
}, 0);
}
EventTarget.prototype.dispatchEvent = function (event) {
event.target = this;
var type = event.type.toString();
var listeners = this.listeners;
var typeListeners = listeners.get(type);
if (typeListeners == undefined) {
return;
}
var length = typeListeners.length;
var i = -1;
var listener = undefined;
while (++i < length) {
listener = typeListeners[i];
try {
listener.call(this, event);
} catch (e) {
throwError(e);
}
}
};
EventTarget.prototype.addEventListener = function (type, callback) {
type = type.toString();
var listeners = this.listeners;
var typeListeners = listeners.get(type);
if (typeListeners == undefined) {
typeListeners = [];
listeners.set(type, typeListeners);
}
var i = typeListeners.length;
while (--i >= 0) {
if (typeListeners[i] === callback) {
return;
}
}
typeListeners.push(callback);
};
EventTarget.prototype.removeEventListener = function (type, callback) {
type = type.toString();
var listeners = this.listeners;
var typeListeners = listeners.get(type);
if (typeListeners == undefined) {
return;
}
var length = typeListeners.length;
var filtered = [];
var i = -1;
while (++i < length) {
if (typeListeners[i] !== callback) {
filtered.push(typeListeners[i]);
}
}
if (filtered.length === 0) {
listeners["delete"](type);
} else {
listeners.set(type, filtered);
}
};
function Event(type) {
this.type = type;
this.target = undefined;
}
function MessageEvent(type, options) {
Event.call(this, type);
this.data = options.data;
this.lastEventId = options.lastEventId;
}
MessageEvent.prototype = Event.prototype;
var XHR = global.XMLHttpRequest;
var XDR = global.XDomainRequest;
var isCORSSupported = XHR != undefined && (new XHR()).withCredentials != undefined;
var Transport = isCORSSupported || (XHR != undefined && XDR == undefined) ? XHR : XDR;
var WAITING = -1;
var CONNECTING = 0;
var OPEN = 1;
var CLOSED = 2;
var AFTER_CR = 3;
var FIELD_START = 4;
var FIELD = 5;
var VALUE_START = 6;
var VALUE = 7;
var contentTypeRegExp = /^text\/event\-stream;?(\s*charset\=utf\-8)?$/i;
var MINIMUM_DURATION = 1000;
var MAXIMUM_DURATION = 18000000;
function getDuration(value, def) {
var n = value;
if (n !== n) {
n = def;
}
return (n < MINIMUM_DURATION ? MINIMUM_DURATION : (n > MAXIMUM_DURATION ? MAXIMUM_DURATION : n));
}
function fire(that, f, event) {
try {
if (typeof f === "function") {
f.call(that, event);
}
} catch (e) {
throwError(e);
}
}
function EventSource(url, options) {
url = url.toString();
var withCredentials = isCORSSupported && options != undefined && Boolean(options.withCredentials);
var initialRetry = getDuration(1000, 0);
var heartbeatTimeout = getDuration(45000, 0);
var lastEventId = "";
var that = this;
var retry = initialRetry;
var wasActivity = false;
var CurrentTransport = options != undefined && options.Transport != undefined ? options.Transport : Transport;
var xhr = new CurrentTransport();
var timeout = 0;
var timeout0 = 0;
var charOffset = 0;
var currentState = WAITING;
var dataBuffer = [];
var lastEventIdBuffer = "";
var eventTypeBuffer = "";
var onTimeout = undefined;
var state = FIELD_START;
var field = "";
var value = "";
function close() {
currentState = CLOSED;
if (xhr != undefined) {
xhr.abort();
xhr = undefined;
}
if (timeout !== 0) {
clearTimeout(timeout);
timeout = 0;
}
if (timeout0 !== 0) {
clearTimeout(timeout0);
timeout0 = 0;
}
that.readyState = CLOSED;
}
function onEvent(type) {
var responseText = "";
if (currentState === OPEN || currentState === CONNECTING) {
try {
responseText = xhr.responseText;
} catch (error) {
// IE 8 - 9 with XMLHttpRequest
}
}
var event = undefined;
var isWrongStatusCodeOrContentType = false;
if (currentState === CONNECTING) {
var status = 0;
var statusText = "";
var contentType = undefined;
if (!("contentType" in xhr)) {
try {
status = xhr.status;
statusText = xhr.statusText;
contentType = xhr.getResponseHeader("Content-Type");
} catch (error) {
// https://bugs.webkit.org/show_bug.cgi?id=29121
status = 0;
statusText = "";
contentType = undefined;
// FF < 14, WebKit
// https://bugs.webkit.org/show_bug.cgi?id=29658
// https://bugs.webkit.org/show_bug.cgi?id=77854
}
} else if (type !== "" && type !== "error") {
status = 200;
statusText = "OK";
contentType = xhr.contentType;
}
if (contentType == undefined) {
contentType = "";
}
if (status === 0 && statusText === "" && type === "load" && responseText !== "") {
status = 200;
statusText = "OK";
if (contentType === "") { // Opera 12
var tmp = (/^data\:([^,]*?)(?:;base64)?,[\S]*$/).exec(url);
if (tmp != undefined) {
contentType = tmp[1];
}
}
}
if (status === 200 && contentTypeRegExp.test(contentType)) {
currentState = OPEN;
wasActivity = true;
retry = initialRetry;
that.readyState = OPEN;
event = new Event("open");
that.dispatchEvent(event);
fire(that, that.onopen, event);
if (currentState === CLOSED) {
return;
}
} else {
// Opera 12
if (status !== 0 && (status !== 200 || contentType !== "")) {
var message = "";
if (status !== 200) {
message = "EventSource's response has a status " + status + " " + statusText.replace(/\s+/g, " ") + " that is not 200. Aborting the connection.";
} else {
message = "EventSource's response has a Content-Type specifying an unsupported type: " + contentType.replace(/\s+/g, " ") + ". Aborting the connection.";
}
setTimeout(function () {
throw new Error(message);
}, 0);
isWrongStatusCodeOrContentType = true;
}
}
}
if (currentState === OPEN) {
if (responseText.length > charOffset) {
wasActivity = true;
}
var i = charOffset - 1;
var length = responseText.length;
var c = "\n";
while (++i < length) {
c = responseText.charAt(i);
if (state === AFTER_CR && c === "\n") {
state = FIELD_START;
} else {
if (state === AFTER_CR) {
state = FIELD_START;
}
if (c === "\r" || c === "\n") {
if (field === "data") {
dataBuffer.push(value);
} else if (field === "id") {
lastEventIdBuffer = value;
} else if (field === "event") {
eventTypeBuffer = value;
} else if (field === "retry") {
initialRetry = getDuration(Number(value), initialRetry);
retry = initialRetry;
} else if (field === "heartbeatTimeout") {
heartbeatTimeout = getDuration(Number(value), heartbeatTimeout);
if (timeout !== 0) {
clearTimeout(timeout);
timeout = setTimeout(onTimeout, heartbeatTimeout);
}
}
value = "";
field = "";
if (state === FIELD_START) {
if (dataBuffer.length !== 0) {
lastEventId = lastEventIdBuffer;
if (eventTypeBuffer === "") {
eventTypeBuffer = "message";
}
event = new MessageEvent(eventTypeBuffer, {
data: dataBuffer.join("\n"),
lastEventId: lastEventIdBuffer
});
that.dispatchEvent(event);
if (eventTypeBuffer === "message") {
fire(that, that.onmessage, event);
}
if (currentState === CLOSED) {
return;
}
}
dataBuffer.length = 0;
eventTypeBuffer = "";
}
state = c === "\r" ? AFTER_CR : FIELD_START;
} else {
if (state === FIELD_START) {
state = FIELD;
}
if (state === FIELD) {
if (c === ":") {
state = VALUE_START;
} else {
field += c;
}
} else if (state === VALUE_START) {
if (c !== " ") {
value += c;
}
state = VALUE;
} else if (state === VALUE) {
value += c;
}
}
}
}
charOffset = length;
}
if ((currentState === OPEN || currentState === CONNECTING) &&
(type === "load" || type === "error" || isWrongStatusCodeOrContentType || (charOffset > 1024 * 1024) || (timeout === 0 && !wasActivity))) {
if (isWrongStatusCodeOrContentType) {
close();
} else {
if (type === "" && timeout === 0 && !wasActivity) {
setTimeout(function () {
throw new Error("No activity within " + heartbeatTimeout + " milliseconds. Reconnecting.");
}, 0);
}
currentState = WAITING;
xhr.abort();
if (timeout !== 0) {
clearTimeout(timeout);
timeout = 0;
}
if (retry > initialRetry * 16) {
retry = initialRetry * 16;
}
if (retry > MAXIMUM_DURATION) {
retry = MAXIMUM_DURATION;
}
timeout = setTimeout(onTimeout, retry);
retry = retry * 2 + 1;
that.readyState = CONNECTING;
}
event = new Event("error");
that.dispatchEvent(event);
fire(that, that.onerror, event);
} else {
if (timeout === 0) {
wasActivity = false;
timeout = setTimeout(onTimeout, heartbeatTimeout);
}
}
}
function onProgress() {
onEvent("progress");
}
function onLoad() {
onEvent("load");
}
function onError() {
onEvent("error");
}
function onReadyStateChange() {
if (xhr.readyState === 4) {
if (xhr.status === 0) {
onEvent("error");
} else {
onEvent("load");
}
} else {
onEvent("progress");
}
}
if (("readyState" in xhr) && global.opera != undefined) {
// workaround for Opera issue with "progress" events
timeout0 = setTimeout(function f() {
if (xhr.readyState === 3) {
onEvent("progress");
}
timeout0 = setTimeout(f, 500);
}, 0);
}
onTimeout = function () {
timeout = 0;
if (currentState !== WAITING) {
onEvent("");
return;
}
// loading indicator in Safari, Chrome < 14
// loading indicator in Firefox
// https://bugzilla.mozilla.org/show_bug.cgi?id=736723
if ((!("ontimeout" in xhr) || ("sendAsBinary" in xhr) || ("mozAnon" in xhr)) && global.document != undefined && global.document.readyState != undefined && global.document.readyState !== "complete") {
timeout = setTimeout(onTimeout, 4);
return;
}
// XDomainRequest#abort removes onprogress, onerror, onload
xhr.onload = onLoad;
xhr.onerror = onError;
if ("onabort" in xhr) {
// improper fix to match Firefox behaviour, but it is better than just ignore abort
// see https://bugzilla.mozilla.org/show_bug.cgi?id=768596
// https://bugzilla.mozilla.org/show_bug.cgi?id=880200
// https://code.google.com/p/chromium/issues/detail?id=153570
xhr.onabort = onError;
}
if ("onprogress" in xhr) {
xhr.onprogress = onProgress;
}
// IE 8-9 (XMLHTTPRequest)
// Firefox 3.5 - 3.6 - ? < 9.0
// onprogress is not fired sometimes or delayed
// see also #64
if ("onreadystatechange" in xhr) {
xhr.onreadystatechange = onReadyStateChange;
}
wasActivity = false;
timeout = setTimeout(onTimeout, heartbeatTimeout);
charOffset = 0;
currentState = CONNECTING;
dataBuffer.length = 0;
eventTypeBuffer = "";
lastEventIdBuffer = lastEventId;
value = "";
field = "";
state = FIELD_START;
var s = url.slice(0, 5);
if (s !== "data:" && s !== "blob:") {
s = url + ((url.indexOf("?", 0) === -1 ? "?" : "&") + "lastEventId=" + encodeURIComponent(lastEventId) + "&r=" + (Math.random() + 1).toString().slice(2));
} else {
s = url;
}
xhr.open("GET", s, true);
if ("withCredentials" in xhr) {
// withCredentials should be set after "open" for Safari and Chrome (< 19 ?)
xhr.withCredentials = withCredentials;
}
if ("responseType" in xhr) {
xhr.responseType = "text";
}
if ("setRequestHeader" in xhr) {
// Request header field Cache-Control is not allowed by Access-Control-Allow-Headers.
// "Cache-control: no-cache" are not honored in Chrome and Firefox
// https://bugzilla.mozilla.org/show_bug.cgi?id=428916
//xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("Accept", "text/event-stream");
// Request header field Last-Event-ID is not allowed by Access-Control-Allow-Headers.
//xhr.setRequestHeader("Last-Event-ID", lastEventId);
}
xhr.send(undefined);
};
EventTarget.call(this);
this.close = close;
this.url = url;
this.readyState = CONNECTING;
this.withCredentials = withCredentials;
this.onopen = undefined;
this.onmessage = undefined;
this.onerror = undefined;
onTimeout();
}
function F() {
this.CONNECTING = CONNECTING;
this.OPEN = OPEN;
this.CLOSED = CLOSED;
}
F.prototype = EventTarget.prototype;
EventSource.prototype = new F();
F.call(EventSource);
if (isCORSSupported) {
EventSource.prototype.withCredentials = undefined;
}
var isEventSourceSupported = function () {
// Opera 12 fails this test, but this is fine.
return global.EventSource != undefined && ("withCredentials" in global.EventSource.prototype);
};
if (Transport != undefined && (global.EventSource == undefined || (isCORSSupported && !isEventSourceSupported()))) {
// Why replace a native EventSource ?
// https://bugzilla.mozilla.org/show_bug.cgi?id=444328
// https://bugzilla.mozilla.org/show_bug.cgi?id=831392
// https://code.google.com/p/chromium/issues/detail?id=260144
// https://code.google.com/p/chromium/issues/detail?id=225654
// ...
global.NativeEventSource = global.EventSource;
global.EventSource = EventSource;
}
}(typeof window !== 'undefined' ? window : this));