mirror of
https://github.com/supabase/supabase.git
synced 2026-10-12 12:55:06 +03:00
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.