Unverified Commit 59ebfbea authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix request body hanging when middleware is preset (#57381)

Instead of `Readable.toWeb` we're gonna manually convert the Node.js stream to a Web stream. `toWeb` is either having a bug, or not compatible with middleware-cloned `PassThrough` streams.

Closes #56286. The case should be already covered with existing tests.
parent 81582629
...@@ -37,15 +37,6 @@ import { ...@@ -37,15 +37,6 @@ import {
} from '../../lib/constants' } from '../../lib/constants'
import type { AppRenderContext, GenerateFlight } from './app-render' import type { AppRenderContext, GenerateFlight } from './app-render'
function nodeToWebReadableStream(nodeReadable: import('stream').Readable) {
if (process.env.NEXT_RUNTIME !== 'edge') {
const { Readable } = require('stream') as typeof import('stream')
return Readable.toWeb(nodeReadable) as ReadableStream
} else {
throw new Error('Invalid runtime')
}
}
function formDataFromSearchQueryString(query: string) { function formDataFromSearchQueryString(query: string) {
const searchParams = new URLSearchParams(query) const searchParams = new URLSearchParams(query)
const formData = new FormData() const formData = new FormData()
...@@ -389,13 +380,28 @@ export async function handleAction({ ...@@ -389,13 +380,28 @@ export async function handleAction({
bound = await decodeReplyFromBusboy(bb, serverModuleMap) bound = await decodeReplyFromBusboy(bb, serverModuleMap)
} else { } else {
// Convert the Node.js readable stream to a Web Stream.
const readableStream = new ReadableStream({
start(controller) {
req.on('data', (chunk) => {
controller.enqueue(new Uint8Array(chunk))
})
req.on('end', () => {
controller.close()
})
req.on('error', (err) => {
controller.error(err)
})
},
})
// React doesn't yet publish a busboy version of decodeAction // React doesn't yet publish a busboy version of decodeAction
// so we polyfill the parsing of FormData. // so we polyfill the parsing of FormData.
const fakeRequest = new Request('http://localhost', { const fakeRequest = new Request('http://localhost', {
method: 'POST', method: 'POST',
// @ts-expect-error // @ts-expect-error
headers: { 'Content-Type': contentType }, headers: { 'Content-Type': contentType },
body: nodeToWebReadableStream(req), body: readableStream,
duplex: 'half', duplex: 'half',
}) })
const formData = await fakeRequest.formData() const formData = await fakeRequest.formData()
......
...@@ -10,7 +10,7 @@ export function requestToBodyStream( ...@@ -10,7 +10,7 @@ export function requestToBodyStream(
return new context.ReadableStream({ return new context.ReadableStream({
start: async (controller) => { start: async (controller) => {
for await (const chunk of stream) { for await (const chunk of stream) {
controller.enqueue(new KUint8Array([...new Uint8Array(chunk)])) controller.enqueue(new KUint8Array(chunk))
} }
controller.close() controller.close()
}, },
......
// Ensure that https://github.com/vercel/next.js/issues/56286 is fixed.
import { NextResponse } from 'next/server'
export async function middleware() {
return NextResponse.next()
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment