Skip to content
Open
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/wise-moons-read.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: stream promised `read` results lazily instead of eagerly buffering them
34 changes: 10 additions & 24 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,17 @@ export class Server {
const result = read(file);
if (result instanceof ReadableStream) {
return result;
} else {
return new ReadableStream({
async start(controller) {
try {
const stream = await Promise.resolve(result);
if (!stream) {
controller.close();
return;
}

const reader = stream.getReader();

while (true) {
const { done, value } = await reader.read();
if (done) break;
controller.enqueue(value);
}

controller.close();
} catch (error) {
controller.error(error);
}
}
});
}

// TODO remove the cast once TypeScript's lib includes `ReadableStream.from`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrapped_read uses ReadableStream.from() and async iteration of a ReadableStream (yield* stream), which are not supported on Cloudflare's workerd runtime, throwing at runtime when server-side asset reads occur on the Cloudflare adapter.

Fix on Vercel

return /** @type {ReadableStream} */ (
/** @type {any} */ (ReadableStream).from(
(async function* () {
const stream = await result;
if (stream) yield* stream;
})()
)
);
};

set_read_implementation(wrapped_read);
Expand Down
Loading