Skip to content

Commit 2f116ed

Browse files
committed
add web stream adapter
1 parent 795c43d commit 2f116ed

4 files changed

Lines changed: 108 additions & 10 deletions

File tree

‎.changeset/fix-2133-bun-deno-stream.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
"@solidjs/start": patch
33
---
44

5-
fix: always pipe SSR output through a `TransformStream` in dev so h3 receives a standard `ReadableStream`. Returning the raw Solid stream only rendered on Node by accident; under Bun and Deno the response body was coerced to the literal string `[object Object]` (#2133)
5+
Return a cancellation-safe web `ReadableStream` for streaming SSR in development. Returning Solid's
6+
raw stream only rendered on Node; Bun and Deno coerced it to `[object Object]`.

‎packages/start/src/server/handler.ts‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { matchAPIRoute } from "./routes.ts";
1212
import { handleServerFunction } from "../fns/handler.ts";
1313
import type { APIEvent, FetchEvent, HandlerOptions, PageEvent } from "./types.ts";
1414
import { getExpectedRedirectStatus } from "./util.ts";
15+
import { toWebReadableStream } from "./web-stream.ts";
1516

1617
const SERVER_FN_BASE = "/_server";
1718

@@ -116,15 +117,9 @@ export function createBaseHandler(
116117

117118
delete (stream as any).then;
118119

119-
// Always pipe through a TransformStream so h3 receives a standard web
120-
// ReadableStream. Returning the raw Solid stream only renders on Node
121-
// by accident (srvx's NodeResponse duck-types `.pipe`); on Bun and
122-
// Deno srvx's FastResponse is the native Response, which coerces the
123-
// object to the string "[object Object]" (#2133). Returning the raw
124-
// stream also breaks Cloudflare Workers.
125-
const { writable, readable } = new TransformStream();
126-
stream.pipeTo(writable);
127-
return readable;
120+
// h3 expects a standard web ReadableStream across runtimes. The adapter
121+
// also tolerates cancellation while Solid finishes outstanding work.
122+
return toWebReadableStream(stream);
128123
}),
129124
});
130125

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { createComponent, createResource, Suspense } from "solid-js";
2+
import { renderToStream } from "solid-js/web";
3+
import { describe, expect, it } from "vitest";
4+
5+
import { toWebReadableStream } from "./web-stream.ts";
6+
7+
describe("toWebReadableStream", () => {
8+
it("returns encoded output as a standard ReadableStream", async () => {
9+
const readable = toWebReadableStream({
10+
pipe(writable) {
11+
writable.write("Hello, ");
12+
writable.write("world!");
13+
writable.end();
14+
},
15+
});
16+
17+
expect(readable).toBeInstanceOf(ReadableStream);
18+
await expect(new Response(readable).text()).resolves.toBe("Hello, world!");
19+
});
20+
21+
it("ignores writes after the consumer cancels", async () => {
22+
let writable!: { write(payload: string): void; end(): void };
23+
const readable = toWebReadableStream({
24+
pipe(value) {
25+
writable = value;
26+
writable.write("shell");
27+
},
28+
});
29+
const reader = readable.getReader();
30+
31+
const shell = await reader.read();
32+
expect(new TextDecoder().decode(shell.value)).toBe("shell");
33+
34+
await reader.cancel("client disconnected");
35+
36+
expect(() => writable.write("late Suspense content")).not.toThrow();
37+
expect(() => writable.end()).not.toThrow();
38+
});
39+
40+
it("allows Solid to finish Suspense work after cancellation", async () => {
41+
let resolveData!: (value: string) => void;
42+
let resolveComplete!: () => void;
43+
const complete = new Promise<void>(resolve => {
44+
resolveComplete = resolve;
45+
});
46+
const stream = renderToStream(
47+
() => {
48+
const [data] = createResource(
49+
() =>
50+
new Promise<string>(resolve => {
51+
resolveData = resolve;
52+
}),
53+
);
54+
return createComponent(Suspense, {
55+
fallback: "loading",
56+
get children() {
57+
return data();
58+
},
59+
});
60+
},
61+
{ onCompleteAll: () => resolveComplete() },
62+
);
63+
const reader = toWebReadableStream(stream).getReader();
64+
65+
const shell = await reader.read();
66+
expect(new TextDecoder().decode(shell.value)).toContain("loading");
67+
68+
await reader.cancel("client disconnected");
69+
resolveData("late Suspense content");
70+
71+
await expect(complete).resolves.toBeUndefined();
72+
});
73+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
type PipeableStream = {
2+
pipe(writable: { write(payload: string): void; end(): void }): void;
3+
};
4+
5+
/** Convert Solid's streaming SSR result into a cancellation-safe web stream. */
6+
export function toWebReadableStream(stream: PipeableStream): ReadableStream<Uint8Array> {
7+
const encoder = new TextEncoder();
8+
let active = true;
9+
10+
return new ReadableStream({
11+
start(controller) {
12+
stream.pipe({
13+
write(payload) {
14+
if (active) controller.enqueue(encoder.encode(payload));
15+
},
16+
end() {
17+
if (!active) return;
18+
active = false;
19+
controller.close();
20+
},
21+
});
22+
},
23+
cancel() {
24+
// Solid may still resolve Suspense resources after the response is
25+
// cancelled. Ignore those writes and let Solid finish its cleanup.
26+
active = false;
27+
},
28+
});
29+
}

0 commit comments

Comments
 (0)