From a1000be3b85a03b6a9054d77e66f009d12f6ca6a Mon Sep 17 00:00:00 2001 From: Katja Lutz Date: Thu, 11 Jul 2024 13:43:25 +0200 Subject: [PATCH] fix: dont use node request as body in handleServerFunction --- .changeset/chilled-phones-sin.md | 5 +++++ packages/start/src/runtime/server-handler.ts | 19 ++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 .changeset/chilled-phones-sin.md diff --git a/.changeset/chilled-phones-sin.md b/.changeset/chilled-phones-sin.md new file mode 100644 index 000000000..2c107d822 --- /dev/null +++ b/.changeset/chilled-phones-sin.md @@ -0,0 +1,5 @@ +--- +"@solidjs/start": patch +--- + +Fixed a regression that resulted in an `Response body object should not be disturbed or locked` error during form body parsing. diff --git a/packages/start/src/runtime/server-handler.ts b/packages/start/src/runtime/server-handler.ts index b3cc5ca30..31dd3546a 100644 --- a/packages/start/src/runtime/server-handler.ts +++ b/packages/start/src/runtime/server-handler.ts @@ -119,8 +119,17 @@ 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; + + // Nodes native IncomingMessage doesn't have a body, + // But we need to access it for some reason (#1282) + type EdgeIncomingMessage = typeof h3Event.node.req & { body?: BodyInit }; + const h3Request = h3Event.node.req as EdgeIncomingMessage | ReadableStream; + + // This should never be the case in "proper" Nitro presets since node.req has to be IncomingMessage, + // But the new azure-functions preset for some reason uses a ReadableStream in node.req (#1521) + const isReadableStream = h3Request instanceof ReadableStream; + const isH3EventBodyStreamLocked = isReadableStream && h3Request.locked; + const requestBody = isReadableStream ? h3Request : h3Request.body; if ( contentType?.startsWith("multipart/form-data") || @@ -129,10 +138,10 @@ async function handleServerFunction(h3Event: HTTPEvent) { // workaround for https://github.com/unjs/nitro/issues/1721 // (issue only in edge runtimes) parsed.push( - await ( + await( isH3EventBodyStreamLocked ? request - : new Request(request, { ...request, body: h3EventBody }) + : new Request(request, { ...request, body: requestBody }) ).formData() ); // what should work when #1721 is fixed @@ -142,7 +151,7 @@ async function handleServerFunction(h3Event: HTTPEvent) { // (issue only in edge runtimes) const tmpReq = isH3EventBodyStreamLocked ? request - : new Request(request, { ...request, body: h3EventBody }); + : new Request(request, { ...request, body: requestBody }); // what should work when #1721 is fixed // just use request.json() here parsed = fromJSON(await tmpReq.json(), {