From 8aa946f1ed9ffc1a79be8706d4a61fa67c125ed1 Mon Sep 17 00:00:00 2001 From: Sacha Reinert Date: Thu, 11 Jul 2024 00:19:34 +0200 Subject: [PATCH] fix: check if a stream is locked before creating a new request in server-handler (#1521) Co-authored-by: Ryan Carniato --- packages/start/src/runtime/server-handler.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/start/src/runtime/server-handler.ts b/packages/start/src/runtime/server-handler.ts index 60b5b8643..b3cc5ca30 100644 --- a/packages/start/src/runtime/server-handler.ts +++ b/packages/start/src/runtime/server-handler.ts @@ -119,6 +119,9 @@ async function handleServerFunction(h3Event: HTTPEvent) { } if (h3Event.method === "POST") { const contentType = request.headers.get("content-type"); + const h3EventBody = h3Event.node.req as any; + const isH3EventBodyStreamLocked = h3EventBody instanceof ReadableStream && h3EventBody.locked; + if ( contentType?.startsWith("multipart/form-data") || contentType?.startsWith("application/x-www-form-urlencoded") @@ -126,14 +129,20 @@ async function handleServerFunction(h3Event: HTTPEvent) { // workaround for https://github.com/unjs/nitro/issues/1721 // (issue only in edge runtimes) parsed.push( - await new Request(request, { ...request, body: (h3Event.node.req as any).body }).formData() + await ( + isH3EventBodyStreamLocked + ? request + : new Request(request, { ...request, body: h3EventBody }) + ).formData() ); // what should work when #1721 is fixed // parsed.push(await request.formData); } else if (contentType?.startsWith("application/json")) { // workaround for https://github.com/unjs/nitro/issues/1721 // (issue only in edge runtimes) - const tmpReq = new Request(request, { ...request, body: (h3Event.node.req as any).body }); + const tmpReq = isH3EventBodyStreamLocked + ? request + : new Request(request, { ...request, body: h3EventBody }); // what should work when #1721 is fixed // just use request.json() here parsed = fromJSON(await tmpReq.json(), {