Skip to content

Commit

Permalink
Unrevert and fix smoothening changes (#155)
Browse files Browse the repository at this point in the history
Unverts #151 and
fixes it. The issue is that when replaying the cache, for the first
`0...n-1` chunks, we need to include a newline.
  • Loading branch information
ankrgyl authored Feb 12, 2025
1 parent 231d106 commit bc75b9c
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions packages/proxy/src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,29 @@ export async function proxyV1({
stream = new ReadableStream<Uint8Array>({
start(controller) {
if ("body" in cachedData && cachedData.body) {
controller.enqueue(new TextEncoder().encode(cachedData.body));
let splits = cachedData.body.split("\n");
for (let i = 0; i < splits.length; i++) {
controller.enqueue(
new TextEncoder().encode(
splits[i] + (i < splits.length - 1 ? "\n" : ""),
),
);
}
} else if ("data" in cachedData && cachedData.data) {
const data = Buffer.from(cachedData.data, "base64");
const uint8Array = new Uint8Array(data);
controller.enqueue(uint8Array);
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));
}
}

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

Expand Down

0 comments on commit bc75b9c

Please sign in to comment.