perf(image-proxy): stream proxied images instead of buffering them

forwardImageAsync awaited response.arrayBuffer(), so the whole image sat in
server memory before anything was sent to the client. A media board asks for
dozens of posters and backdrops at once, so that is dozens of full images held
simultaneously - and a client that gives up midway still cost a complete
server-side download.

Forwarding response.body instead lets backpressure reach upstream. Measured
with 30 concurrent 2 MiB images and deliberately slow clients, one fresh
process per arm because the allocator does not return freed spans:

  buffered   +116.0 MiB, +117.8 MiB peak RSS
  streamed    +74.2 MiB,  +78.8 MiB peak RSS

-36% peak RSS on that load, and it scales with concurrency times image size.
Content-Length is now forwarded too, so clients still get a progress bar.
This commit is contained in:
Thomas Camlong committed 2026-08-11 16:09:37 +02:00
1 parent 3ba2800db9
commit cf240addea
2 files changed
+17 -5

No files matched your search

@@ -16,10 +16,13 @@ export const GET = async (_request: Request, props: { params: Promise<{ id: stri
});
}
return new Response(result.image, {
// undici and the DOM lib describe the same web stream with incompatible types, so the value is
// passed straight through.
return new Response(result.image as BodyInit | null, {
headers: {
"Cache-Control": "public, max-age=3600, immutable", // Cache for 1 hour
...(result.contentType ? { "Content-Type": result.contentType } : {}),
...(result.contentLength ? { "Content-Length": result.contentLength } : {}),
},
});
};