forked from Yaffle/EventSource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventsource.js
executable file
·378 lines (313 loc) · 12.3 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
/*jslint sloppy: true, white: true, plusplus: true, indent: 2, regexp: true */
/*global XMLHttpRequest, setTimeout, clearTimeout, XDomainRequest, ActiveXObject*/
(function (global) {
function parseURI(url) {
var m = String(url).replace(/^\s+|\s+$/g, '').match(/^([^:\/?#]+:)?(\/\/(?:[^:@]*(?::[^:@]*)?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);
// authority = '//' + user + ':' + pass '@' + hostname + ':' port
return (m ? {
href : m[0] || '',
protocol : m[1] || '',
authority: m[2] || '',
host : m[3] || '',
hostname : m[4] || '',
port : m[5] || '',
pathname : m[6] || '',
search : m[7] || '',
hash : m[8] || ''
} : null);
}
function absolutizeURI(base, href) {// RFC 3986
function removeDotSegments(input) {
var output = [];
input.replace(/^(\.\.?(\/|$))+/, '')
.replace(/\/(\.(\/|$))+/g, '/')
.replace(/\/\.\.$/, '/../')
.replace(/\/?[^\/]*/g, function (p) {
if (p === '/..') {
output.pop();
} else {
output.push(p);
}
});
return output.join('').replace(/^\//, input.charAt(0) === '/' ? '/' : '');
}
href = parseURI(href || '');
base = parseURI(base || '');
return !href || !base ? null : (href.protocol || base.protocol) +
(href.protocol || href.authority ? href.authority : base.authority) +
removeDotSegments(href.protocol || href.authority || href.pathname.charAt(0) === '/' ? href.pathname : (href.pathname ? ((base.authority && !base.pathname ? '/' : '') + base.pathname.slice(0, base.pathname.lastIndexOf('/') + 1) + href.pathname) : base.pathname)) +
(href.protocol || href.authority || href.pathname ? href.search : (href.search || base.search)) +
href.hash;
}
function loc() {
try {
return global.location.href;
} catch (e) {
var a = document.createElement('a');
a.href = '';
return a.href;
}
}
// http://blogs.msdn.com/b/ieinternals/archive/2010/04/06/comet-streaming-in-internet-explorer-with-xmlhttprequest-and-xdomainrequest.aspx?PageIndex=1#comments
// XDomainRequest does not have a binary interface. To use with non-text, first base64 to string.
// http://cometdaily.com/2008/page/3/
function XDomainRequestWrapper() {
var x = new global.XDomainRequest(),
that = this;
that.readyState = 0;
that.responseText = '';
function onChange(readyState, responseText) {
that.readyState = readyState;
that.responseText = responseText;
that.onreadystatechange();
}
x.onload = function () {
onChange(4, x.responseText);
};
x.onerror = function () {
onChange(4, '');
};
x.onprogress = function () {
onChange(3, x.responseText);
};
that.open = function (method, url) {
return x.open(method, url);
};
that.abort = function () {
return x.abort();
};
that.send = function (postData) {
return x.send(postData);
};
that.setRequestHeader = function () {};
that.getResponseHeader = function (name) {
return (/^content\-type$/i).test(name) ? x.contentType : '';
};
return that;
}
function extendAsEventTarget(obj) {
var listeners = [];
function lastIndexOf(type, callback) {
var i = listeners.length - 1;
while (i >= 0 && !(listeners[i].type === type && listeners[i].callback === callback)) {
i--;
}
return i;
}
obj.dispatchEvent = function (eventObject) {
function a(e) {
return function () {
throw e;
};
}
var type = eventObject.type,
candidates = listeners.slice(0), i;
for (i = 0; i < candidates.length; i++) {
if (candidates[i].type === type) {
try {
candidates[i].callback.call(obj, eventObject);
} catch (e) {
// This identifier is local to the catch clause. But it's not true for IE < 9 ? (so "a" used)
setTimeout(a(e), 0);
}
}
}
};
obj.addEventListener = function (type, callback) {
if (lastIndexOf(type, callback) === -1) {
listeners.push({type: type, callback: callback});
}
};
obj.removeEventListener = function (type, callback) {
var i = lastIndexOf(type, callback);
if (i !== -1) {
listeners.splice(i, 1);
}
};
return obj;
}
function empty() {}
var XHR2CORSSupported = !!(global.XDomainRequest || (global.XMLHttpRequest && ('onprogress' in (new XMLHttpRequest())) && ('withCredentials' in (new XMLHttpRequest()))));
// FF 6 doesn't support SSE + CORS
if (!global.EventSource || XHR2CORSSupported) {
global.EventSource = function (url, options) {
function F() {}
F.prototype = global.EventSource.prototype;
url = absolutizeURI(loc(), String(url));
if (!url) {
throw new Error('');
}
var that = new F(),
retry = 1000,
lastEventId = '',
xhr = null,
reconnectTimeout = null,
realReadyState,
origin = parseURI(url);
origin = origin.protocol + origin.authority;
that.url = url;
that.withCredentials = !!(options && options.withCredentials);
that.CONNECTING = 0;
that.OPEN = 1;
that.CLOSED = 2;
that.readyState = that.CONNECTING;
realReadyState = that.CONNECTING;
// Queue a task which, if the readyState is set to a value other than CLOSED,
// sets the readyState to ... and fires event
function queue(event, readyState) {
if (readyState !== null) {
realReadyState = readyState;
}
setTimeout(function () {
if (that.readyState === that.CLOSED) {
return;// http://www.w3.org/Bugs/Public/show_bug.cgi?id=14331
}
if (readyState !== null) {
that.readyState = readyState;
}
event.target = that;
that.dispatchEvent(event);
try {
if (/^(message|error|open)$/.test(event.type) && typeof that['on' + event.type] === 'function') {
that['on' + event.type](event);
}
} catch (e) {
setTimeout(function () {
throw e;
}, 0);
}
}, 0);
}
function close() {
// http://dev.w3.org/html5/eventsource/ The close() method must close the connection, if any; must abort any instances of the fetch algorithm started for this EventSource object; and must set the readyState attribute to CLOSED.
if (xhr !== null) {
xhr.onreadystatechange = empty;
xhr.abort();
xhr = null;
}
if (reconnectTimeout !== null) {
clearTimeout(reconnectTimeout);
reconnectTimeout = null;
}
if ('\v' === 'v' && global.detachEvent) {
global.detachEvent('onunload', close);
}
that.readyState = that.CLOSED;
realReadyState = that.CLOSED;
}
that.close = close;
extendAsEventTarget(that);
function openConnection() {
reconnectTimeout = null;
var postData = (lastEventId !== '' ? 'Last-Event-ID=' + encodeURIComponent(lastEventId) : ''),
offset = 0,
charOffset = 0,
data = '',
newLastEventId = lastEventId,
name = '';
xhr = global.XDomainRequest ? (new XDomainRequestWrapper()) : (global.XMLHttpRequest ? (new global.XMLHttpRequest()) : (new ActiveXObject('Microsoft.XMLHTTP')));
// with GET method in FF xhr.onreadystatechange with readyState === 3 doesn't work + POST = no-cache
xhr.open('POST', url, true);
// Chrome bug:
// Request header field Cache-Control is not allowed by Access-Control-Allow-Headers.
//xhr.setRequestHeader('Cache-Control', 'no-cache');
// Chrome bug:
// http://code.google.com/p/chromium/issues/detail?id=71694
// If you force Chrome to have a whitelisted content-type, either explicitly with setRequestHeader(), or implicitly by sending a FormData, then no preflight is done.
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
if (!XHR2CORSSupported) {
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');// long-polling
}
// Request header field Last-Event-ID is not allowed by Access-Control-Allow-Headers.
//if (lastEventId !== '') {
// xhr.setRequestHeader('Last-Event-ID', lastEventId);
//}
xhr.withCredentials = that.withCredentials;
xhr.onreadystatechange = function () {
var readyState = +xhr.readyState,
responseText = '', contentType = '', i = 0, line, part, stream;
if (readyState > 2) {
try {
responseText = xhr.responseText || '';
} catch (ex) {}
}
if (readyState > 1) {
try {
contentType = xhr.getResponseHeader('Content-Type') || '';// old FF bug ?
} catch (ex2) {}
}
//use xhr.responseText instead of xhr.status (http://bugs.jquery.com/ticket/8135)
if (realReadyState === that.CONNECTING && /^text\/event\-stream/i.test(contentType) && (readyState > 1) && (readyState !== 4 || responseText)) {
queue({'type': 'open'}, that.OPEN);
}
if (realReadyState === that.OPEN && /\r|\n/.test(responseText.slice(charOffset))) {
part = responseText.slice(offset);
stream = (offset ? part : part.replace(/^\uFEFF/, '')).replace(/\r\n?/g, '\n').split('\n');
offset += part.length - stream[stream.length - 1].length;
while (i < stream.length - 1) {
line = stream[i].match(/([^\:]*)(?:\:\u0020?([\s\S]+))?/);
if (!line[0]) {
// dispatch the event
if (data) {
lastEventId = newLastEventId;
queue({
'type': name || 'message',
origin: origin,
lastEventId: lastEventId,
data: data.replace(/\u000A$/, '')
}, null);
}
// Set the data buffer and the event name buffer to the empty string.
data = '';
name = '';
}
if (line[1] === 'event') {
name = line[2];
}
if (line[1] === 'id') {
newLastEventId = line[2];
//lastEventId = line[2];//!!! see bug http://www.w3.org/Bugs/Public/show_bug.cgi?id=13761
}
if (line[1] === 'retry') {
if (/^\d+$/.test(line[2])) {
retry = +line[2];
}
}
if (line[1] === 'data') {
data += line[2] + '\n';
}
i++;
}
}
charOffset = responseText.length;
if (readyState === 4) {
xhr.onreadystatechange = empty;// old IE bug?
xhr = null;
if (realReadyState === that.OPEN) {
// reestablishes the connection
queue({'type': 'error'}, that.CONNECTING);
// setTimeout will wait before previous setTimeout(0) have completed
reconnectTimeout = setTimeout(openConnection, retry);
} else {
if ('\v' === 'v' && global.detachEvent) {
global.detachEvent('onunload', close);
}
//fail the connection
queue({'type': 'error'}, that.CLOSED);
}
}
};
xhr.send(postData);
}
openConnection();
if ('\v' === 'v' && global.attachEvent) {
global.attachEvent('onunload', close);
}
return that;
};
}
global.EventSource.CONNECTING = 0;
global.EventSource.OPEN = 1;
global.EventSource.CLOSED = 2;
global.EventSource.supportCORS = XHR2CORSSupported;
}(this));