vercel / next.js

The React Framework
https://nextjs.org
MIT License
127.08k stars 27.01k forks source link

Error using server actions in a NodeJS custom server #59266

Open HenriqueLimas opened 11 months ago

HenriqueLimas commented 11 months ago

Link to the code that reproduces this issue

https://github.com/HenriqueLimas/next-server-action-issue

To Reproduce

  1. Use a custom server implementation (with express for example)
  2. Have a middleware in the custom implementation that reads the req readable stream (Like formidable)
  3. Have a server action implemented in a form
  4. Trigger the action by submitting the form

Current vs. Expected behavior

Current The following error is shown: Error: Unexpected end of form

Expected The action is executed successfully

Verify canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 21.6.0: Wed Oct  4 23:56:02 PDT 2023; root:xnu-8020.240.18.704.15~1/RELEASE_ARM64_T6000
Binaries:
  Node: 18.17.1
  npm: 9.6.7
  Yarn: 1.22.19
  pnpm: 8.10.0
Relevant Packages:
  next: 14.0.3
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: N/A
  typescript: N/A
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

App Router

Additional context

On NodeJS env the error is throw by busboy during decoding. This is because the readable stream was already closed by previous middlewares/code logic and the logic as of now assumes that the readable stream is still open. Ideally there should be a different case when the readableStream is completed already. Maybe using some info form NextInternalRequestMeta

HenriqueLimas commented 11 months ago

One solution that can be used is to clone (or tee) the req Readable stream before doing anything with it and using that stream as the input to next handler. Although this is not ideal in cases where security checks (xss prevention) are made to make sure the input are secure or any other treatment of the req data.