Skip to content

Commit

Permalink
Revert "Smoothen event source parser by feeding in smaller chunks" (#153
Browse files Browse the repository at this point in the history
)

Reverts #151
  • Loading branch information
sachinpad authored Feb 11, 2025
1 parent cd70b7d commit 231d106
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions packages/proxy/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,24 +328,12 @@ export async function proxyV1({
stream = new ReadableStream<Uint8Array>({
start(controller) {
if ("body" in cachedData && cachedData.body) {
for (const line of cachedData.body.split("\n")) {
controller.enqueue(new TextEncoder().encode(line));
}
controller.enqueue(new TextEncoder().encode(cachedData.body));
} else if ("data" in cachedData && cachedData.data) {
const data = Buffer.from(cachedData.data, "base64");
let start = 0;
for (let i = 0; i < data.length; i++) {
if (data[i] === 10) {
// 10 is ASCII/UTF-8 code for \n
controller.enqueue(data.subarray(start, i + 1));
start = i + 1;
}
}
if (start < data.length) {
controller.enqueue(data.subarray(start));
}
const uint8Array = new Uint8Array(data);
controller.enqueue(uint8Array);
}

controller.close();
},
});
Expand Down Expand Up @@ -497,6 +485,7 @@ export async function proxyV1({
cacheKey,
JSON.stringify({ headers: proxyResponseHeaders, data: dataB64 }),
);
controller.terminate();
},
});

Expand Down Expand Up @@ -601,11 +590,7 @@ export async function proxyV1({
});
}
if (isStreaming) {
const start = Date.now();
let textChunks = new TextDecoder().decode(chunk);
eventSourceParser?.feed(textChunks);
const end = Date.now();
console.log(`time to feed ${chunk.length} bytes: ${end - start}ms`);
eventSourceParser?.feed(new TextDecoder().decode(chunk));
} else {
allChunks.push(chunk);
}
Expand Down

0 comments on commit 231d106

Please sign in to comment.