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

action: add Readable.toWeb "polyfill" for node 16 #49201

Merged
merged 1 commit into from
May 4, 2023
Merged
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
30 changes: 28 additions & 2 deletions packages/next/src/server/app-render/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ import { FlightRenderResult } from './flight-render-result'
import { ActionResult } from './types'
import { ActionAsyncStorage } from '../../client/components/action-async-storage'

function nodeToWebReadableStream(nodeReadable: import('stream').Readable) {
if (process.env.NEXT_RUNTIME !== 'edge') {
const { Readable } = require('stream')
if ('toWeb' in Readable && typeof Readable.toWeb === 'function') {
return Readable.toWeb(nodeReadable)
}

return new ReadableStream({
start(controller) {
nodeReadable.on('data', (chunk) => {
controller.enqueue(chunk)
})

nodeReadable.on('end', () => {
controller.close()
})

nodeReadable.on('error', (error) => {
controller.error(error)
})
},
})
} else {
throw new Error('Invalid runtime')
}
}

function formDataFromSearchQueryString(query: string) {
const searchParams = new URLSearchParams(query)
const formData = new FormData()
Expand Down Expand Up @@ -266,12 +293,11 @@ export async function handleAction({
} else {
// React doesn't yet publish a busboy version of decodeAction
// so we polyfill the parsing of FormData.
const { Readable } = require('stream')
const UndiciRequest = require('next/dist/compiled/undici').Request
const fakeRequest = new UndiciRequest('http://localhost', {
method: 'POST',
headers: { 'Content-Type': req.headers['content-type'] },
body: Readable.toWeb(req),
body: nodeToWebReadableStream(req),
duplex: 'half',
})
const formData = await fakeRequest.formData()
Expand Down