Skip to content
This repository was archived by the owner on Apr 15, 2023. It is now read-only.

Commit 174541d

Browse files
authored
Update EventSource to support chunked data
This code doesn't work when a line comes in but it hasn't been terminated with a new line (chunked data). In that case, the split('\n') call will give you some junk data which the rest of the logic can't handle. There might be another edge case where the XHR request finishes before the full line comes in but that's outside the scope of this change
1 parent b9d1f92 commit 174541d

1 file changed

Lines changed: 4 additions & 5 deletions

File tree

EventSource.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var EventSource = function(url, options) {
1010
var eventsource = this,
1111
interval = 500, // polling interval
1212
lastEventId = null,
13-
cache = '',
13+
lastIndexProcessed = 0,
1414
eventType;
1515

1616
if (!url || typeof url != 'string') {
@@ -50,7 +50,7 @@ var EventSource = function(url, options) {
5050

5151
if (lastEventId != null)
5252
xhr.setRequestHeader('Last-Event-ID', lastEventId);
53-
cache = '';
53+
lastIndexProcessed = 0;
5454

5555
xhr.timeout =
5656
this.OPTIONS && this.OPTIONS.timeout !== undefined
@@ -74,13 +74,12 @@ var EventSource = function(url, options) {
7474
} catch (e) {}
7575

7676
// process this.responseText
77-
var parts = responseText.substr(cache.length).split('\n'),
77+
var parts = responseText.substr(lastIndexProcessed).split('\n'),
7878
data = [],
7979
i = 0,
8080
retry = 0,
8181
line = '';
82-
83-
cache = responseText;
82+
lastIndexProcessed = responseText.lastIndexOf('\n\n') + 2;
8483

8584
// TODO handle 'event' (for buffer name), retry
8685
for (; i < parts.length; i++) {

0 commit comments

Comments
 (0)