Files
supabase/apps
Alaister Young abb7cc80e2 fix(studio): teach toWebHandler to stream when res.writeHead is called
The pages-router AI route uses result.pipeUIMessageStreamToResponse
(res, ...) from the AI SDK, which writes to a Node ServerResponse
in three stages: res.writeHead(status, headers), then async
res.write(chunk) calls, then res.end(). The buffering shim only
exposed status/setHeader/write/end and crashed at writeHead.

Switch the response proxy into streaming mode the moment writeHead
(or flushHeaders) is called: a Web ReadableStream is opened, any
already-buffered chunks flush into it, subsequent writes enqueue
into it, and res.end() closes it. finalize() then returns
new Response(stream, …) instead of the buffered payload, so the
client starts receiving tokens before the handler resolves.

Also stub the writable/EventEmitter surface (on/once/off/emit/
headersSent/writableEnded) as no-ops so pipe helpers that attach
drain/close/error listeners don't crash. Abort is still handled at
the req level via the Request.signal subscription.
2026-05-11 15:10:32 +08:00
..