From a278a28f5bb84fe020748ad7da20159a0aa9fd2e Mon Sep 17 00:00:00 2001 From: Shu Ding Date: Thu, 4 May 2023 00:34:12 +0200 Subject: [PATCH] fix multipart formdata in the edge runtime --- .../next/src/server/app-render/action-handler.ts | 11 ++++++++--- packages/next/src/server/web-server.ts | 8 +------- test/e2e/app-dir/actions/app-action.test.ts | 13 ++++++++----- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/next/src/server/app-render/action-handler.ts b/packages/next/src/server/app-render/action-handler.ts index c95e01572b894..90578bde6f111 100644 --- a/packages/next/src/server/app-render/action-handler.ts +++ b/packages/next/src/server/app-render/action-handler.ts @@ -1,4 +1,5 @@ import type { IncomingMessage, ServerResponse } from 'http' +import type { WebNextRequest } from '../base-http/web' import { ACTION } from '../../client/components/app-router-headers' import { isNotFoundError } from '../../client/components/not-found' @@ -52,7 +53,9 @@ export async function handleAction({ { get: (_, id: string) => { return { - id: serverActionsManifest.node[id].workers[workerName], + id: serverActionsManifest[ + process.env.NEXT_RUNTIME === 'edge' ? 'edge' : 'node' + ][id].workers[workerName], name: id, chunks: [], } @@ -70,13 +73,15 @@ export async function handleAction({ // Use react-server-dom-webpack/server.edge const { decodeReply } = ComponentMod - const webRequest = req as unknown as Request + const webRequest = req as unknown as WebNextRequest if (!webRequest.body) { throw new Error('invariant: Missing request body.') } if (isMultipartAction) { - throw new Error('invariant: Multipart form data is not supported.') + // TODO-APP: Add streaming support + const formData = await webRequest.request.formData() + bound = await decodeReply(formData, serverModuleMap) } else { let actionData = '' diff --git a/packages/next/src/server/web-server.ts b/packages/next/src/server/web-server.ts index 41a3dabb1264f..aa45feea7e31d 100644 --- a/packages/next/src/server/web-server.ts +++ b/packages/next/src/server/web-server.ts @@ -372,13 +372,7 @@ export default class NextWebServer extends BaseServer { if (curRenderToHTML) { return await curRenderToHTML( - { - url: req.url, - method: req.method, - cookies: req.cookies, - headers: req.headers, - body: req.body, - } as any, + req as any, res as any, pathname, query, diff --git a/test/e2e/app-dir/actions/app-action.test.ts b/test/e2e/app-dir/actions/app-action.test.ts index 88825d005b93e..1f36df1d42fca 100644 --- a/test/e2e/app-dir/actions/app-action.test.ts +++ b/test/e2e/app-dir/actions/app-action.test.ts @@ -203,13 +203,16 @@ createNextDescribe( it('should return error response for hoc auth wrappers in edge runtime', async () => { const browser = await next.browser('/header/edge') await await browser.eval(`document.cookie = 'auth=0'`) + await browser.elementByCss('#authed').click() - await check(async () => { - const text = await browser.elementByCss('h1').text() - console.log('text', text) - return text && text.length > 0 ? text : 'failed' - }, /Multipart form data is not supported/) + await check(() => { + return browser.elementByCss('h1').text() + }, 'Error: Unauthorized request') + + await await browser.eval(`document.cookie = 'auth=1'`) + + await browser.elementByCss('#authed').click() }) }) }