Skip to content

stream: preserve mutable chunks in Web Stream adapters#64579

Open
seungwoo505 wants to merge 2 commits into
nodejs:mainfrom
seungwoo505:fix-writable-to-web-write
Open

stream: preserve mutable chunks in Web Stream adapters#64579
seungwoo505 wants to merge 2 commits into
nodejs:mainfrom
seungwoo505:fix-writable-to-web-write

Conversation

@seungwoo505

Copy link
Copy Markdown

When a Node.js writable is converted with Writable.toWeb(), the Web Streams
write() promise can currently settle as soon as the native write() call
returns true. That return value only represents backpressure, so the native
stream may still retain the supplied mutable BufferSource. Reusing the buffer
after awaiting the Web write can therefore change bytes that have not yet been
consumed.

This change:

  • waits for the native per-write callback when the stream is an uncorked,
    unmodified Writable;
  • coordinates callback completion with drain, aborts, and native stream
    errors;
  • passes a private same-brand BufferSource copy for Duplex, corked, overridden,
    and legacy write paths where waiting for a callback would change existing
    completion behavior;
  • preserves HTTP input validation before fallback copies; and
  • preserves SharedArrayBuffer backing for cloned views.

The original native Writable.prototype.write and
OutgoingMessage.prototype.write methods are captured so patched methods and
accessors are classified and invoked consistently.

Validation included:

  • make -j4
  • the changed Web Streams adapter, Duplex, and compression tests
  • the existing Writable, Duplex, and Web Streams adapter test set
  • CompressionStream WPT tests
  • repeated async regression tests
  • ESLint and git diff --check

Fixes: #64549

Wait for native write callbacks when they can safely represent chunk
consumption. For Duplex streams, corked writes, and custom or legacy
write methods, pass private BufferSource copies to preserve completion
timing.

Coordinate callback completion with backpressure, aborts, and stream
errors so a settled Web Streams write no longer exposes mutable bytes
still retained by the native stream.

Preserve native HTTP validation and SharedArrayBuffer backing when
fallback copies are required.

Signed-off-by: seungwoo <zoozoo1302@gmail.com>
Cover native callback completion, fallback copies, aborts, and error
propagation for mutable BufferSource chunks passed to Node.js Web
Streams adapters.

Verify HTTP validation, SharedArrayBuffer backing, and Duplex and
compression paths.

Signed-off-by: seungwoo <zoozoo1302@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net
  • @nodejs/streams

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Jul 18, 2026
@seungwoo505
seungwoo505 marked this pull request as ready for review July 18, 2026 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stream: Writable.toWeb()/Duplex.toWeb() settles write() before a mutable chunk is consumed

2 participants