Skip to content

fix(assets): hash and serve public assets as streams so files over 2 GiB work - #5216

Open
harshbhx wants to merge 1 commit into
nitrojs:mainfrom
harshbhx:fix/5076-large-public-assets
Open

harshbhx wants to merge 1 commit into
nitrojs:mainfrom
harshbhx:fix/5076-large-public-assets

Conversation

@harshbhx

@harshbhx harshbhx commented Dec 5, 2026

Copy link
Copy Markdown

🔗 Linked issue

Resolves #5076 (follow-up to #2338)

❓ Type of change

  • 🐞 Bug fix (a non-breaking change that fixes an issue)

📚 Description

Public assets over 2 GiB broke the build, and would have broken at runtime too, because fs.promises.readFile refuses files that big (ERR_FS_FILE_TOO_LARGE). Nitro read each public asset fully into memory in two places. This follows the fix described in #5076.

Build (etag). The whole file was read only to feed createEtag(). A new helper getFileEtag(path, size) in src/utils/etag.ts streams the file through sha1 and returns the etag in the same format the etag package uses ("<size in hex>-<sha1 base64, first 27 chars>"), so existing etags stay byte-identical. The file is still read in full for serveStatic: "inline", where the bytes are needed for base64. The helper is used in public-assets.ts and server-assets.ts; the size comes from the stat that was already there.

Runtime (node reader). The non-range branch of public-assets-node returned fsp.readFile(path). Files over 1 GiB (by the build-time size in the manifest) are now opened and streamed, the same way readRange already did, so a file removed after the build still rejects with ENOENT and reads as a 404. I merged both paths into one readStream(path, range). Smaller files are still read into a buffer: my first version streamed everything, but the Bun preset then answers full responses chunked, without Content-Length, and the existing static range test caught it. static.ts already sets Content-Length from the build-time size, which Node keeps for streams.

This PR is against main only. The v2 backport mentioned in the issue is not included.

Testing

  • test/unit/etag.test.ts (new): getFileEtag equals the etag package for empty, text, multi-byte and multi-chunk content, plus the known empty-file etag.
  • test/unit/public-assets.test.ts: the manifest etag/size/data for serveStatic: true and "inline"; the node reader streams a file whose manifest size is over the limit, still returns a buffer for a small one, and resolves undefined for a missing file.
  • On my fork, the new tests fail without the change (etag.test.ts and the large-file reader test) and the full CI passes with it (lint, rollup, rolldown on Ubuntu and Windows, vite, bun). One bun job failed once on an unrelated vite HMR test and passed on a re-run.
  • Not tested: an actual file over 2 GiB (hashing it would read 2 GiB in CI), so the reader test uses a small file with a large manifest size.

📝 Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly. (No docs change needed.)

@harshbhx
harshbhx requested a review from pi0 as a code owner December 5, 2026 06:15
@vercel

vercel Bot commented Dec 5, 2026

Copy link
Copy Markdown

@harshbhx is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Public asset generation now computes ETags by streaming file contents, while buffering content only for inline assets. The Node reader streams ranged requests and files larger than 1 GiB, and buffers smaller non-ranged files.

Changes: Public asset generation and serving

Layer / File(s) Summary
Streamed ETag generation
src/utils/etag.ts, src/build/virtual/public-assets.ts, src/build/virtual/server-assets.ts, test/unit/etag.test.ts, test/unit/public-assets.test.ts
A shared helper hashes file streams to produce ETags. Public and server asset generation use the helper. Tests check ETag compatibility, generated metadata, and inline data behavior.
Node reader buffering and streaming
src/build/virtual/public-assets.ts, test/unit/public-assets.test.ts
The Node reader streams ranged requests and files larger than 1 GiB. It buffers other files. Tests check large-file streaming, small-file buffering, and missing-file handling.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • nitrojs/nitro#5211: Changes the etag package format, which is relevant to this PR’s ETag compatibility.
  • nitrojs/nitro#5065: Adds byte-range reading for public assets, a behavior also handled by this PR’s Node reader.

Suggested labels: bug

Merge Risk: Minimal · up to 78fc3

This change lets public assets larger than 2 GiB build and be served by computing ETags from streams and streaming large files at runtime. No concrete merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title uses the conventional commits form (type fix, scope assets). It states the main change: public assets are hashed and served as streams so files over 2 GiB work. This matches the changeset, w…
Description check Passed The description links issue #5076, marks the change as a bug fix, and explains the build-time etag streaming via getFileEtag and the runtime node reader change. It also lists the tests added in etag.t…
Linked Issues check Passed Directly linked issue #5076 has two coding objectives. Both are covered by the reviewed changes, as shown by the whole-PR summary.

(1) Build etag hashing [#5076]: src/utils/etag.ts adds getFileEtag(p…
Out of Scope Changes check Passed All changed files relate to the #5076 objectives. src/utils/etag.ts adds the new helper. src/build/virtual/public-assets.ts and src/build/virtual/server-assets.ts switch to the helper and reuse the ex…
Full details: Title check

Explanation

The title uses the conventional commits form (type fix, scope assets). It states the main change: public assets are hashed and served as streams so files over 2 GiB work. This matches the changeset, which adds getFileEtag streaming for build-time etags and a streamed node reader for large files.

Full details: Description check

Explanation

The description links issue #5076, marks the change as a bug fix, and explains the build-time etag streaming via getFileEtag and the runtime node reader change. It also lists the tests added in etag.test.ts and public-assets.test.ts, which match the changeset.

Full details: Linked Issues check

Explanation

Directly linked issue #5076 has two coding objectives. Both are covered by the reviewed changes, as shown by the whole-PR summary.

(1) Build etag hashing [#5076]: src/utils/etag.ts adds getFileEtag(path, size). It streams the file into a SHA-1 hash and returns "<size hex>-<sha1 base64, first 27 chars>", the format the etag package uses. src/build/virtual/public-assets.ts and src/build/virtual/server-assets.ts now call getFileEtag with the size from stat. The full-file read is kept only for serveStatic: "inline", where base64 data is needed. test/unit/etag.test.ts compares getFileEtag with the etag package for empty, text, multi-byte, and multi-chunk content, and checks the known empty-file etag.

(2) Node runtime reader [#5076]: the non-range branch of public-assets-node no longer returns fsp.readFile(path) for large files. Files whose manifest size is over 1 GiB are opened and streamed through a unified readStream(path, range). Smaller files still return buffers. ENOENT is still treated as absent. test/unit/public-assets.test.ts covers the streamed large-file path, the buffered small-file path, and undefined for a missing file.

The issue asks for Readable.toWeb on main. The PR streams large files through the same readStream path that readRange used. This is a deliberate design choice and does not conflict with the issue's objective. The inspection did not cover every hunk, so the exact stream wrapper was not read line by line. The summary is consistent with the objectives.

The v2 backport is out of scope per the PR and the issue. No unmet coding requirement for main was found.

Full details: Out of Scope Changes check

Explanation

All changed files relate to the #5076 objectives. src/utils/etag.ts adds the new helper. src/build/virtual/public-assets.ts and src/build/virtual/server-assets.ts switch to the helper and reuse the existing stat result for the default mtime. The mtime reuse in server-assets.ts is a small adjacent refactor that supports the same stat-based flow, and assetsMtime behavior is unchanged per the summary. The public-assets reader change is the runtime objective. The tests in test/unit/etag.test.ts and test/unit/public-assets.test.ts cover the new behavior. No unrelated change was identified. The PR description notes the v2 backport is excluded.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • 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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 59a50af2-8b41-40a6-8c8c-d79025e6714b
Commits

Reviewing files that changed from the base of the PR and between 68b734e and 78fc38f.

Files selected for processing: 5

  • src/build/virtual/public-assets.ts
  • src/build/virtual/server-assets.ts
  • src/utils/etag.ts
  • test/unit/etag.test.ts
  • test/unit/public-assets.test.ts

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

This branch has not been deployed

No deployments
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.

Public assets larger than 2 GiB fail to build and serve

1 participant