Skip to content

Commit

Permalink
Fix text parsing when double newline missing at end (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilJunker committed Feb 23, 2024
1 parent d6434ac commit 6cda129
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/EventSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,15 @@ class EventSource {
}

_handleEvent(response) {
const parts = response.substr(this.lastIndexProcessed).split('\n');

const indexOfDoubleNewline = response.lastIndexOf('\n\n');
if (indexOfDoubleNewline != -1) {
this.lastIndexProcessed = indexOfDoubleNewline + 2;
const indexOfDoubleNewline = this._getLastDoubleNewlineIndex(response);

if (indexOfDoubleNewline <= this.lastIndexProcessed) {
return;
}


const parts = response.substring(this.lastIndexProcessed, indexOfDoubleNewline).split('\n');
this.lastIndexProcessed = indexOfDoubleNewline;

let data = [];
let retry = 0;
let line = '';
Expand Down Expand Up @@ -219,6 +221,14 @@ class EventSource {
}
}

_getLastDoubleNewlineIndex(response) {
const lastIndex = response.lastIndexOf('\n\n');
if (lastIndex === -1) {
return -1;
}
return lastIndex + 2;
}

addEventListener(type, listener) {
if (this.eventHandlers[type] === undefined) {
this.eventHandlers[type] = [];
Expand Down

0 comments on commit 6cda129

Please sign in to comment.