Skip to content

Commit

Permalink
Refactor stream reading using Fetch API
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiscs committed Jan 8, 2024
1 parent b3881f9 commit d40d8ec
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions app/clients/grpc.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,25 @@ export async function* fetchStream<I extends Message<I>, O extends Message<O>>(
headers: { "Content-Type": "application/json" },
signal: rs,
});
const encoder = new TextDecoder("utf-8");
const decoder = new TextDecoder("utf-8");
const reader = resp.body?.getReader();
let buffer = "";
while (reader) {
try {
const { done, value } = await reader.read();
if (value && value.length > 0) {
buffer += decoder.decode(value);
}
if (buffer.length > 0) {
const chunks = buffer.split(/\r?\n/);
buffer = chunks.pop() || "";
for (const chunk of chunks) {
yield mt.fromJsonString(chunk);
}
}
if (done) {
break;
}
yield mt.fromJsonString(encoder.decode(value));
} catch (err) {
if (err instanceof DOMException && err.name === "AbortError") {
break;
Expand Down

0 comments on commit d40d8ec

Please sign in to comment.