Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont use node request as body in handleServerFunction #1577

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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