Skip to content

Commit

Permalink
fix: check if a stream is locked before creating a new request in ser…
Browse files Browse the repository at this point in the history
…ver-handler (#1521)

Co-authored-by: Ryan Carniato <[email protected]>
  • Loading branch information
supersoniko and ryansolid authored Jul 10, 2024
1 parent e53f086 commit 8aa946f
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions packages/start/src/runtime/server-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,30 @@ 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")
) {
// 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(), {
Expand Down

0 comments on commit 8aa946f

Please sign in to comment.