Skip to content

fs: add fs.createWebReadable and fs.createWebWritable web streams - #67937

Open
mcollina wants to merge 3 commits into
nodejs:mainfrom
mcollina:feature/fs-webstreams
Open

mcollina wants to merge 3 commits into
nodejs:mainfrom
mcollina:feature/fs-webstreams

Conversation

@mcollina

@mcollina mcollina commented Dec 1, 2026

Copy link
Copy Markdown
Member

Add fs.createWebReadable() and fs.createWebWritable(), which return first-class WHATWG Web Streams for reading and writing files. These are implemented in JS and skip the Node stream + stream.Readable.toWeb() / stream.Writable.toWeb() adapter hop.

The readable is a type: 'bytes' ReadableStream that reads directly into BYOB / auto-allocated views (zero-copy, supports BYOB readers); the writable is a WritableStream sink with partial-write handling. Both accept a path, a FileHandle, or an fd, plus the core options (flags, encoding, start/end, highWaterMark, autoClose).

Includes tests (test/parallel/test-fs-create-web-readable.js and test/parallel/test-fs-create-web-writable.js), read/write benchmarks comparing against the createReadStream()/createWriteStream().toWeb() baseline, and documentation.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-bot nodejs-github-bot added fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run. labels Dec 1, 2026
@mcollina mcollina added semver-minor PRs that contain new features and should be released in the next minor version. notable-change PRs with changes that should be highlighted in changelogs. labels Dec 1, 2026
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

The notable-change PRs with changes that should be highlighted in changelogs. label has been added by @mcollina.

Please suggest a text for the release notes if you'd like to include a more detailed summary, then proceed to update the PR description with the text or a link to the notable change suggested text comment. Otherwise, the commit will be placed in the Other Notable Changes section.

@codecov

codecov Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.48241% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.60%. Comparing base (bdfbee2) to head (68a6ddc).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/fs/webstreams.js 96.21% 10 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67937      +/-   ##
==========================================
- Coverage   92.96%   90.60%   -2.37%     
==========================================
  Files         432      803     +371     
  Lines      201044   287072   +86028     
  Branches    30705    55488   +24783     
==========================================
+ Hits       186903   260093   +73190     
- Misses      13814    17081    +3267     
- Partials      327     9898    +9571     
Files with missing lines Coverage Δ
lib/fs.js 97.13% <100.00%> (+4.03%) ⬆️
lib/internal/fs/webstreams.js 96.21% <96.21%> (ø)

... and 505 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Add first-class WHATWG Web Streams for reading and writing files,
implemented in JS and faster than going through the
createReadStream().toWeb() / createWriteStream() adapter path.

The readable is a type:'bytes' ReadableStream that reads directly into
BYOB / auto-allocated views (zero-copy); the writable is a
WritableStream sink with partial-write handling. Both accept a path,
FileHandle, or fd and the core options (flags, encoding, start/end,
highWaterMark, autoClose).

Includes tests (test/parallel/test-fs-create-web-readable.js and
test-fs-create-web-writable.js), benchmarks comparing against the
createReadStream/createWriteStream().toWeb() baseline, and docs.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
V8 CPU profiling (node --cpu-prof; @platformatic/flame could not
attach to the v28-pre build because its @datadog/pprof native addon
has no ABI 155 build) shows the file web streams are I/O-bound
(majority idle waiting on thread-pool I/O); the non-idle CPU is
dominated by the WHATWG stream machinery and native I/O bindings. In
the write path, createWebWritable allocated a fresh Buffer view
(FastBuffer) on every chunk.

Reuse the chunk directly when it is already a Buffer, avoiding a
per-chunk allocation. This removes the FastBuffer allocation and
roughly halves GC time (7.9% -> 3.0%) in the write profile, with
read/write benchmarks at or above the
createReadStream/createWriteStream().toWeb() baseline.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
Add explicit coverage for the BYOB read path, which the existing read
tests exercise only implicitly through the autoAllocateChunkSize default
reader path:

- reads via a BYOB reader into caller-provided buffers and verifies the
  concatenated bytes match the file;
- verifies a BYOB read detaches (transfers) the caller-provided buffer
  (ArrayBuffer.byteLength becomes 0), the standard web-streams semantic;
- verifies the stream does not reuse the same internal buffer across
  reads (mutating a returned chunk does not corrupt the next chunk).

Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollina force-pushed the feature/fs-webstreams branch from c7c5799 to 68a6ddc Compare December 1, 2026 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fs Issues and PRs related to file-system APIs and the fs module. needs-ci PRs that need a full CI run. notable-change PRs with changes that should be highlighted in changelogs. semver-minor PRs that contain new features and should be released in the next minor version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants