Context: a watched answer body stays a byte stream
Jonas Templestein, 2026-11-26 23:19 UTC · 7e2d90a3993d1a2fc630763613e682d0df337805 · parent f22ce82
4 files changed
- M core/itx/src/context/answer-body.test.ts
- M core/itx/src/context/answer-body.ts
- M core/mind-worker/src/library/capnweb-answer-body.test.ts
- M core/mind-worker/src/library/fixtures/capnweb-answer-body.ts
M core/itx/src/context/answer-body.test.ts
@@ -68,6 +68,23 @@ expect(ends).toEqual(["closed"]); }); +test("a watched body is a byte stream, and an empty chunk of its source is passed over", async () => { + const chunks = ["a", "", "b"].map((text) => encoder.encode(text)); + const body = new ReadableStream<Uint8Array>({ + pull(controller) { + const chunk = chunks.shift(); + if (chunk) controller.enqueue(chunk); + else controller.close(); + }, + }); + const { answer, ends } = watchedAnswer(body); + // only a byte stream gives a reader of this kind + const reader = (answer as ReadableStream<Uint8Array>).getReader({ mode: "byob" }); + reader.releaseLock(); + expect(await new Response(answer as ReadableStream).text()).toBe("ab"); + expect(ends).toEqual(["closed"]); +}); + test("a body that its reader drops ends as cancelled, and its source is cancelled", async () => { const { body, seen } = slowBody(1000, 100); const { answer, ends } = watchedAnswer(new Response(body));
M core/itx/src/context/answer-body.ts
@@ -47,13 +47,17 @@ }; nobodyAsks(); // No queue of its own: `pull` runs only while a read waits, so a pull is a reader that asks. - return new ReadableStream<Uint8Array>( + // A byte stream, as the body was: Workers RPC carries no other kind of stream. + return new ReadableStream( { + type: "bytes", async pull(controller) { clearTimeout(unread); let next: ReadableStreamReadResult<Uint8Array>; try { - next = await reader.read(); + // a byte stream takes no empty chunk + do next = await reader.read(); + while (!next.done && next.value.byteLength === 0); } catch (error) { end("errored"); throw error;
M core/mind-worker/src/library/capnweb-answer-body.test.ts
@@ -32,6 +32,9 @@ await using mind = await mindWithUpstream(); const short = await mind.answer({ lines: 3, everyMs: 10 }); expect(await short.text()).toBe(linesOf(3)); + // a bare stream, with no Response around it, crosses Workers RPC as the byte stream it was + const bare = await mind.answer({ lines: 3, everyMs: 10 }, "stream"); + expect(await bare.text()).toBe(linesOf(3)); const dropped = await mind.answer({ lines: 1000, everyMs: 100 }); const reader = dropped.body!.getReader(); @@ -96,8 +99,10 @@ const seen = async () => (await (await runtime.dispatchFetch("http://mind.test/seen")).json()) as Seen; return { - answer: ({ lines, everyMs }: { lines: number; everyMs: number }) => - runtime.dispatchFetch(`http://mind.test/answer?lines=${lines}&everyMs=${everyMs}`), + answer: ( + { lines, everyMs }: { lines: number; everyMs: number }, + as: "answer" | "stream" = "answer", + ) => runtime.dispatchFetch(`http://mind.test/${as}?lines=${lines}&everyMs=${everyMs}`), seen, /** What the upstream saw once `reached` holds: asked each half second, for 45 s at most. */ seenWhen: async (reached: (seen: Seen) => boolean) => {
M core/mind-worker/src/library/fixtures/capnweb-answer-body.ts
@@ -37,6 +37,10 @@ { headers: { "content-type": "text/event-stream" } }, ); } + /** The same lines as a bare stream, with no Response around it. */ + stream(input: { lines: number; everyMs: number }): ReadableStream<Uint8Array> { + return this.answer(input).body!; + } } /** The upstream: a capnweb server on each socket. A Durable Object, so one isolate holds `seen`. */ @@ -75,7 +79,8 @@ async fetch(request: Request) { const url = new URL(request.url); if (url.pathname === "/seen") return upstreamOf().fetch("https://upstream.example/seen"); - if (url.pathname !== "/answer") return new Response("Not found\n", { status: 404 }); + if (url.pathname !== "/answer" && url.pathname !== "/stream") + return new Response("Not found\n", { status: 404 }); // as a member's call: the answer is the upstream's Response, handed on by the root context's // object over Workers RPC const answer = await contextAt("/").invoke( @@ -83,7 +88,7 @@ "itx", ["connectToCapnweb", "wss://upstream.example/api"], [ - "answer", + url.pathname.slice(1), { lines: Number(url.searchParams.get("lines")), everyMs: Number(url.searchParams.get("everyMs")), @@ -93,8 +98,8 @@ [], { principal: { actor: "usr_aaaaabbbbbccccc", email: "member@mind.example" } }, ); - return answer instanceof Response - ? answer - : new Response(`no Response: ${JSON.stringify(answer)}\n`, { status: 500 }); + if (answer instanceof Response) return answer; + if (answer instanceof ReadableStream) return new Response(answer); + return new Response(`no body: ${JSON.stringify(answer)}\n`, { status: 500 }); }, } satisfies ExportedHandler;