Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/core/streaming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,12 +250,15 @@ export async function* _iterSSEMessages(
}
}

const DOUBLE_NEWLINE_DELIMITER_MAX_OVERLAP_BYTES = 3;

/**
* Given an async iterable iterator, iterates over it and yields full
* SSE chunks, i.e. yields when a double new-line is encountered.
*/
async function* iterSSEChunks(iterator: AsyncIterableIterator<Bytes>): AsyncGenerator<Uint8Array> {
let data = new Uint8Array();
let searchStartIndex = 0;

for await (const chunk of iterator) {
if (chunk == null) {
Expand All @@ -273,10 +276,17 @@ async function* iterSSEChunks(iterator: AsyncIterableIterator<Bytes>): AsyncGene
data = newData;

let patternIndex;
while ((patternIndex = findDoubleNewlineIndex(data)) !== -1) {
while ((patternIndex = findDoubleNewlineIndex(data.subarray(searchStartIndex))) !== -1) {
patternIndex += searchStartIndex;
yield data.slice(0, patternIndex);
data = data.slice(patternIndex);
searchStartIndex = 0;
}

searchStartIndex = Math.max(
0,
data.length - DOUBLE_NEWLINE_DELIMITER_MAX_OVERLAP_BYTES,
);
}

if (data.length > 0) {
Expand Down