Skip to content

Commit

Permalink
fix: dont use node request as body in handleServerFunction (#1577)
Browse files Browse the repository at this point in the history
  • Loading branch information
katywings authored Jul 11, 2024
1 parent de1ca02 commit 5997509
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-phones-sin.md
Original file line number Diff line number Diff line change
@@ -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.
19 changes: 14 additions & 5 deletions packages/start/src/runtime/server-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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") ||
Expand All @@ -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
Expand All @@ -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(), {
Expand Down

0 comments on commit 5997509

Please sign in to comment.