Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/fast-frame-refill.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@workflow/world-vercel': patch
---

Avoid repeatedly copying partial event-frame bodies as network chunks arrive.
25 changes: 17 additions & 8 deletions packages/world-vercel/src/frames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,23 @@ export async function* decodeFrames(
let buffer = new Uint8Array(0);

const refill = async (needed: number): Promise<boolean> => {
while (buffer.byteLength < needed) {
const { done, value } = await chunks.next();
if (done) return false;
if (!value || value.byteLength === 0) continue;
const next = new Uint8Array(buffer.byteLength + value.byteLength);
next.set(buffer, 0);
next.set(value, buffer.byteLength);
buffer = next;
if (buffer.byteLength >= needed) return true;

const parts: Uint8Array[] = [buffer];
let byteLength = buffer.byteLength;
while (byteLength < needed) {
const chunk = await chunks.next();
if (chunk.done) return false;
if (chunk.value.byteLength === 0) continue;
parts.push(chunk.value);
byteLength += chunk.value.byteLength;
}

buffer = new Uint8Array(byteLength);
let offset = 0;
for (const part of parts) {
buffer.set(part, offset);
offset += part.byteLength;
}
return true;
};
Expand Down
Loading