Skip to content

fix(node): read a pre-set rawBody from every body method - #312

Merged
pi0 merged 2 commits into
h3js:mainfrom
nitedani:fix/raw-body-every-method
Oct 17, 2026
Merged

pi0 merged 2 commits into
h3js:mainfrom
nitedani:fix/raw-body-every-method

Conversation

@nitedani

@nitedani nitedani commented Oct 14, 2026 •

Copy link
Copy Markdown

Claude Opus 5.5 (high effort) wrote the fix and this; two more Opus 5.5 (medium effort) runs verified and reviewed it.

On Google Cloud Functions, which reads the request stream into req.rawBody, request.arrayBuffer(), bytes(), blob(), formData() and request.body fail with Body is unusable. text() and json() work, because #295 made them read req.rawBody.

Case Observed Expected
req.rawBody set, then request.arrayBuffer() (also bytes(), blob(), formData(), body) TypeError: Body is unusable the body, under maxRequestBodySize like text()

#readBuffered() reads req.rawBody, but the body getter, which every other method reads from, doesn't. This serves body from a Buffer req.rawBody, so all of them read it through the existing size limit.

What you should see

1. A server that fills req.rawBody the way Google Cloud Functions does, then reads the body with arrayBuffer():

srvx 1.0.5 (also on main at f74d220), Node 26:

// server.mjs
import { createServer } from "node:http";
import { toNodeHandler } from "srvx/node";

const handler = toNodeHandler(async (request) => {
  try {
    return new Response(await request.arrayBuffer());
  } catch (error) {
    return new Response(String(error), { status: 500 });
  }
});

createServer(async (req, res) => {
  // What Google Cloud Functions does before the handler runs
  const chunks = [];
  for await (const chunk of req) chunks.push(chunk);
  req.rawBody = Buffer.concat(chunks);
  handler(req, res);
}).listen(3000);
$ node server.mjs &
$ curl -d hello localhost:3000
TypeError: Body is unusable: Body has already been read

On 8dea2bf it returns hello.

2. The new test reads an 8-byte and a 9-byte rawBody through each of the seven ways (the six methods and body) under maxRequestBodySize: 8. On main, 5 of the 7 fail (text() and json() pass); on 8dea2bf all 7 answer 200 for 8 bytes and 413 ERR_BODY_TOO_LARGE for 9.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Fixed request body handling when middleware has already read and buffered the incoming data. Body-reading methods can now access the buffered content instead of failing because the original request stream was consumed.
    • Request size limits are consistently enforced for buffered bodies: requests within the limit succeed, while oversized requests receive a 413 response. This applies across the available body-reading methods.

`arrayBuffer()`, `bytes()`, `blob()`, `formData()` and `body` rejected with
"Body is unusable" when the stream was drained into `req.rawBody` (Google Cloud
Functions, Firebase), while `text()` and `json()` read it. The `body` stream now
comes from `rawBody` too, so `maxRequestBodySize` applies to all of them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nitedani
nitedani requested a review from pi0 as a code owner October 14, 2026 15:43
@vercel

vercel Bot commented Oct 14, 2026

Copy link
Copy Markdown

@nitedani is attempting to deploy a commit to the unjs Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6a721705-64ea-458d-870d-653e35a824e9
📥 Commits

Reviewing files that changed from the base of the PR and between 8dea2bf and 6d062b1.

📒 Files selected for processing: 1
  • src/adapters/_node/request.ts
🚧 Files skipped from review as they are similar to previous changes: 1
  • src/adapters/_node/request.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

📝 Walkthrough

When req.rawBody contains a Buffer, NodeRequest.body now exposes it as a readable stream. Regression tests cover seven body-reading paths and check the configured size limit.

Changes: Buffered Node request bodies

Layer / File(s) Summary
Buffered body reading and size limits
src/adapters/_node/request.ts, test/node-adapters.test.ts
NodeRequest.body now checks for a buffered req.rawBody before checking whether the request source has finished. Tests cover seven body readers: 8-byte payloads return status 200, while 9-byte payloads return status 413 with ERR_BODY_TOO_LARGE.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 6d062

Buffered request bodies are available to the body readers without an identified merge-blocking regression.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: enabling every body-reading method to read a pre-set rawBody in Node requests.
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files.

✨ Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit found a stream of bytes
It hopped through seven reading paths
Eight fit neatly in the flow
Nine met the limit’s steady no
The buffered stream now joins the show

Comment @coderabbitai help to get the list of available commands.

nitedani added a commit to nitedani/universal-handler that referenced this pull request Oct 14, 2026
…vx alone

Reading `rawBody` directly skipped srvx's `maxRequestBodySize`: a body over the limit answered 200 where main answers 413. srvx serves `text()` and `json()` from `rawBody` under the limit, so a route's json() keeps working; its other reads ignore `rawBody` until h3js/srvx#312 is released.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@pkg-pr-new

pkg-pr-new Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/srvx@312

commit: 6d062b1

@pi0
pi0 merged commit 437596f into h3js:main Oct 17, 2026
14 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants