Resolve "Feat(SSE): Add msgpackr parser for SSE Client"

This commit is contained in:
2025-04-07 02:52:23 +00:00
committed by Jim Hamill
parent 8441a19423
commit cec5d128f3
3 changed files with 74 additions and 22 deletions
+19 -22
View File
@@ -26,9 +26,14 @@ export class SSEClient {
// State.
private url: string;
private options: SSEClientOptions;
private controller: AbortController;
private connected: boolean = false;
protected options: SSEClientOptions;
protected messageBuffer: Uint8Array = new Uint8Array();
// Text decoders and encoders for parsing the message buffer.
private textDecoder: TextDecoder = new TextDecoder();
private textEncoder: TextEncoder = new TextEncoder();
/**
* Creates a new SSEClient instance.
@@ -93,8 +98,6 @@ export class SSEClient {
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
// Start the read here so we can resolve and then start handling the stream
// We will await this at the start of the loop, and redeclare it at the end of each loop iteration
@@ -115,16 +118,11 @@ export class SSEClient {
break;
}
// Decode the received chunk and add it to the buffer
const chunk = decoder.decode(value, { stream: true });
buffer += chunk;
// Process any complete events in the buffer
const events = this.parseEvents(buffer);
buffer = events.remainder;
// Decode and parse the chunk
const events = this.parseEvents(value);
// Handle each complete event
for (const event of events.parsed) {
for (const event of events) {
if (this.options.onMessage) {
this.options.onMessage(event);
}
@@ -154,21 +152,17 @@ export class SSEClient {
});
}
/**
* Parses SSE events from a text buffer.
* @param buffer The buffer to parse
* @returns Parsed events and the remaining buffer
*/
private parseEvents(buffer: string): {
parsed: SSEvent[];
remainder: string;
} {
protected parseEvents(chunk: Uint8Array): SSEvent[] {
// Append new chunk to existing buffer
this.messageBuffer = new Uint8Array([...this.messageBuffer, ...chunk]);
const events: SSEvent[] = [];
const lines = buffer.split(/\r\n|\r|\n/);
const lines = this.textDecoder.decode(this.messageBuffer).split(/\r\n|\r|\n/);
let currentEvent: Partial<SSEvent> = {};
let completeEventCount = 0;
// Iterate over the lines to find complete events
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
@@ -210,8 +204,11 @@ export class SSEClient {
}
}
// Store the remainder of the buffer for the next chunk
const remainder = lines.slice(completeEventCount).join('\n');
return { parsed: events, remainder };
this.messageBuffer = this.textEncoder.encode(remainder);
return events;
}
/**