-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Marcos Candeia <[email protected]>
- Loading branch information
Showing
6 changed files
with
165 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,4 +131,4 @@ dist | |
|
||
|
||
.vscode | ||
kv | ||
kv* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
export const EVENT_STREAM_RESPONSE_HEADER: string = "text/event-stream"; | ||
export async function* readFromStream<T>( | ||
response: Response, | ||
): AsyncIterableIterator<T> { | ||
if (!response.body) { | ||
return; | ||
} | ||
|
||
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader(); | ||
|
||
let buffer = ""; | ||
|
||
while (true) { | ||
const { value, done } = await reader.read(); | ||
|
||
if (done) { | ||
break; | ||
} | ||
|
||
buffer += value; | ||
const lines = buffer.split("\n"); | ||
buffer = lines.pop() ?? ""; | ||
|
||
for (const data of lines) { | ||
if (!data.startsWith("data:")) { | ||
continue; | ||
} | ||
|
||
try { | ||
const chunk = data.replace("data:", ""); | ||
yield JSON.parse(decodeURIComponent(chunk)); | ||
} catch (_err) { | ||
console.log("error parsing data", _err, data); | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
// Process any remaining buffer after the stream ends | ||
if (buffer.length > 0 && buffer.startsWith("data:")) { | ||
try { | ||
yield JSON.parse(decodeURIComponent(buffer.replace("data:", ""))); | ||
} catch (_err) { | ||
console.log("error parsing data", _err, buffer); | ||
} | ||
} | ||
} |