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/server-function-readable-stream-result.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solidjs/web": patch
---

A server function that returns a `ReadableStream` resolves on the client to that `ReadableStream` (`getReader()`, `pipeTo()`, `tee()`), not to a plain async iterable. The top-level abort wrapper applies only to other async iterables, such as an async generator's result. To cancel the call behind a returned stream, call it through `invoke(fn, { signal })` and abort the signal; `cancel()` on the stream stops reading but does not abort the call, the same as for a stream nested in an object.
5 changes: 3 additions & 2 deletions packages/web/server-functions/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1017,8 +1017,9 @@ async function dispatchServerFunction(base, id, options, args, meta, callArgs =
// Streaming result: wrap so stopping consumption stops the CALL. Without
// this, `return()` (a `break` in for-await) only detaches the local
// iterator — the fetch keeps downloading and the server keeps producing.
// Top-level only, matching the server's value-tier teardown scope.
if (controller && result?.[Symbol.asyncIterator]) {
// Top-level only, matching the server's value-tier teardown scope. A
// decoded `ReadableStream` passes through as one, as it does nested.
if (controller && result?.[Symbol.asyncIterator] && !result.getReader) {
return {
[Symbol.asyncIterator]() {
const it = result[Symbol.asyncIterator]();
Expand Down
29 changes: 29 additions & 0 deletions packages/web/test/server/server-functions-invoke.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,35 @@ describe("invoke — per-call invocation (built bundles)", () => {
}
});

it("a streamed ReadableStream result stays a ReadableStream (#5175)", async () => {
let source!: ReadableStreamDefaultController<number>;
registerServerFunction(
"inv-readable-stream",
async () =>
new ReadableStream<number>({
start: c => {
source = c;
c.enqueue(0);
c.enqueue(1);
}
})
);
const transport = connectTransport();
try {
const ref = createServerReference("inv-readable-stream") as () => Promise<
ReadableStream<number>
>;
const stream = await ref();
expect(stream).toBeInstanceOf(ReadableStream);
const reader = stream.getReader();
expect(await reader.read()).toEqual({ done: false, value: 0 });
source.error(new Error("producer failed"));
await expect(reader.closed).rejects.toBeTruthy();
} finally {
transport.restore();
}
});

it("server mirror: in-process call, signal rejects the caller", async () => {
let calls = 0;
let finished = 0;
Expand Down
Loading