Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a667296
stream: preserve mutable chunks in Web Stream adapters
seungwoo505 Jul 18, 2026
16b663d
test: cover mutable Web Stream adapter writes
seungwoo505 Jul 18, 2026
531ada5
stream: simplify mutable chunk fallback
seungwoo505 Sep 28, 2026
72a0cbb
test: refine mutable Web Stream adapter coverage
seungwoo505 Sep 28, 2026
7e150d6
stream: use Float16Array from primordials
seungwoo505 Sep 29, 2026
09d8636
stream: optimize private BufferSource copies
seungwoo505 Oct 1, 2026
13e12b7
stream: optimize Web Stream write completion
seungwoo505 Oct 1, 2026
7ad3b90
stream: preserve native HTTP write validation
seungwoo505 Oct 1, 2026
ac63d9d
stream: preserve errors on premature writable close
seungwoo505 Oct 1, 2026
50ed406
test: deduplicate Web Stream adapter regressions
seungwoo505 Oct 1, 2026
7796966
doc: clarify mutable Web Stream chunk reuse
seungwoo505 Oct 1, 2026
1fa14c0
benchmark: add async writev adapter benchmark
seungwoo505 Oct 2, 2026
ca58486
stream: preserve Writable.toWeb batching and context
seungwoo505 Oct 5, 2026
4be4f8e
test: cover writable-to-web batching and context
seungwoo505 Oct 5, 2026
3830906
stream: update writable-to-web context handling
seungwoo505 Oct 6, 2026
21f40fc
buffer: pool independent backing stores for byte copies
seungwoo505 Nov 20, 2026
12377e8
stream: preserve independent chunks in Writable.toWeb
seungwoo505 Nov 20, 2026
4193b94
stream: streamline writable stream request queuing
seungwoo505 Nov 20, 2026
cd2096b
stream: preserve copy buffers for shared writes
seungwoo505 Nov 21, 2026
9f700c0
stream: remove V8 patches for Writable.toWeb
seungwoo505 Nov 24, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions benchmark/webstreams/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const bench = common.createBenchmark(main, {
'readable-to-web',
'readable-from-web',
'writable-to-web',
'writable-to-web-async-writev',
'writable-from-web',
],
});
Expand Down Expand Up @@ -68,6 +69,26 @@ async function writableToWeb(n) {
bench.end(n);
}

async function writableToWebAsyncWritev(n) {
const chunk = Buffer.alloc(1024);
const streamWritable = new Writable({
highWaterMark: 64 * 1024,
// Defer completion so subsequent writes can be batched in writev().
write(chunk, encoding, callback) {
setImmediate(callback);
},
writev(chunks, callback) {
setImmediate(callback);
},
});
const writer = Writable.toWeb(streamWritable).getWriter();
bench.start();
for (let i = 0; i < n; i++)
await writer.write(chunk);
await writer.close();
bench.end(n);
}

function writableFromWeb(n) {
const chunk = Buffer.alloc(1024);
const writableStream = new WritableStream({
Expand Down Expand Up @@ -99,6 +120,9 @@ function main({ n, kind }) {
case 'writable-to-web':
writableToWeb(n);
break;
case 'writable-to-web-async-writev':
writableToWebAsyncWritev(n);
break;
case 'writable-from-web':
writableFromWeb(n);
break;
Expand Down
13 changes: 13 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -3349,6 +3349,14 @@ changes:
* `streamWritable` {stream.Writable}
* Returns: {WritableStream}

For streams not operating in object mode, a mutable {Buffer}, {TypedArray},
{DataView}, {ArrayBuffer}, or {SharedArrayBuffer} passed to the returned stream's
writer can be reused after the promise returned by `writer.write()` is
fulfilled. Do not modify the chunk or its underlying bytes while that promise
is pending. The adapter may copy the chunk, so the underlying Node.js stream is
not guaranteed to receive the same object. Chunks written to streams operating
in object mode are passed by reference without copying.

### `stream.Duplex.from(src)`

<!-- YAML
Expand Down Expand Up @@ -3517,6 +3525,10 @@ changes:
* `readable` {ReadableStream}
* `writable` {WritableStream}

If the writable side is in byte mode, reusing a chunk after `writer.write()`
fulfills does not change any bytes the readable side has retained. See
[`stream.Writable.toWeb()`][] for details on chunk reuse.

```mjs
import { Duplex } from 'node:stream';

Expand Down Expand Up @@ -5098,6 +5110,7 @@ contain multi-byte characters.
[`readable.push('')`]: #readablepush
[`readable.setEncoding()`]: #readablesetencodingencoding
[`stream.Readable.from()`]: #streamreadablefromiterable-options
[`stream.Writable.toWeb()`]: #streamwritabletowebstreamwritable
[`stream.addAbortSignal()`]: #streamaddabortsignalsignal-stream
[`stream.compose(...streams)`]: #streamcomposestreams
[`stream.cork()`]: #writablecork
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/http/outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,8 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
return ret;
};

const outgoingMessagePrototypeWrite = OutgoingMessage.prototype.write;

function onError(msg, err, callback) {
if (msg.destroyed) {
return;
Expand Down Expand Up @@ -1499,4 +1501,5 @@ module.exports = {
validateHeaderName,
validateHeaderValue,
OutgoingMessage,
outgoingMessagePrototypeWrite,
};
45 changes: 30 additions & 15 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ Writable.prototype.pipe = function() {
errorOrDestroy(this, new ERR_STREAM_CANNOT_PIPE());
};

function _write(stream, chunk, encoding, cb) {
function _write(stream, chunk, encoding, cb, onwrite) {
const state = stream._writableState;

if (cb == null || typeof cb !== 'function') {
Expand Down Expand Up @@ -500,7 +500,7 @@ function _write(stream, chunk, encoding, cb) {
}

state.pendingcb++;
return writeOrBuffer(stream, state, chunk, encoding, cb);
return writeOrBuffer(stream, state, chunk, encoding, cb, onwrite);
}

Writable.prototype.write = function(chunk, encoding, cb) {
Expand All @@ -512,6 +512,8 @@ Writable.prototype.write = function(chunk, encoding, cb) {
return _write(this, chunk, encoding, cb) === true;
};

const writablePrototypeWrite = Writable.prototype.write;

Writable.prototype.cork = function() {
const state = this._writableState;

Expand Down Expand Up @@ -547,7 +549,7 @@ Writable.prototype.setDefaultEncoding = function setDefaultEncoding(encoding) {
// If we're already writing something, then just put this
// in the queue, and wait our turn. Otherwise, call _write
// If we return false, then we need a drain event, so set that flag.
function writeOrBuffer(stream, state, chunk, encoding, callback) {
function writeOrBuffer(stream, state, chunk, encoding, callback, onwrite) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not be modifying this function for this

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added onwrite as part of the copied-chunk _writev batching optimization to preserve async context across buffered writes.
Could you elaborate on your concern with changing writeOrBuffer() here?
I’d like to revise the approach accordingly.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

none of what you are talking about here matches the PR title, description or the bug you are trying to fix

const len = (state[kState] & kObjectMode) !== 0 ? 1 : chunk.length;

state.length += len;
Expand All @@ -558,7 +560,7 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {
state[kBufferedValue] = [];
}

state[kBufferedValue].push({ chunk, encoding, callback });
state[kBufferedValue].push({ chunk, encoding, callback, onwrite });
if ((state[kState] & kAllBuffers) !== 0 && encoding !== 'buffer') {
state[kState] &= ~kAllBuffers;
}
Expand All @@ -571,7 +573,7 @@ function writeOrBuffer(stream, state, chunk, encoding, callback) {
state.writecb = callback;
}
state[kState] |= kWriting | kSync | kExpectWriteCb;
stream._write(chunk, encoding, state.onwrite);
stream._write(chunk, encoding, onwrite ?? state.onwrite);
state[kState] &= ~kSync;
}

Expand All @@ -594,7 +596,7 @@ const kWriteFlowBlock = kObjectMode | kDestroyed | kErrored | kSync |
kNeedDrain | kWriteCb | kExpectWriteCb | kBufferProcessing |
kFinalCalled | kPrefinished | kOnFinished | kErrorEmitted;

function writeKnownBuffer(stream, chunk) {
function writeKnownBuffer(stream, chunk, onwrite) {
const state = stream._writableState;
if (state == null || state.length !== 0 || !(chunk instanceof Buffer))
return undefined;
Expand All @@ -608,7 +610,7 @@ function writeKnownBuffer(stream, chunk) {
state.length = len;
state.writelen = len;
state[kState] = bits | kWriting | kSync | kExpectWriteCb;
stream._write(chunk, 'buffer', state.onwrite);
stream._write(chunk, 'buffer', onwrite ?? state.onwrite);
state[kState] &= ~kSync;

const ret = state.length < state.highWaterMark || state.length === 0;
Expand All @@ -623,18 +625,25 @@ ObjectDefineProperty(Writable, 'writeKnownBuffer', {
enumerable: false,
});

function doWrite(stream, state, writev, len, chunk, encoding, cb) {
// Adapter-owned copies can be queued without waiting for their individual
// callbacks. Carry their completion context through native buffering so the
// next batch does not inherit another request's completion context.
function writeWithContext(stream, chunk, onwrite) {
return _write(stream, chunk, undefined, undefined, onwrite) === true;
}

function doWrite(stream, state, writev, len, chunk, encoding, cb, onwrite) {
state.writelen = len;
if (cb !== nop) {
state.writecb = cb;
}
state[kState] |= kWriting | kSync | kExpectWriteCb;
if ((state[kState] & kDestroyed) !== 0)
state.onwrite(new ERR_STREAM_DESTROYED('write'));
(onwrite ?? state.onwrite)(new ERR_STREAM_DESTROYED('write'));
else if (writev)
stream._writev(chunk, state.onwrite);
stream._writev(chunk, onwrite ?? state.onwrite);
else
stream._write(chunk, encoding, state.onwrite);
stream._write(chunk, encoding, onwrite ?? state.onwrite);
state[kState] &= ~kSync;
}

Expand Down Expand Up @@ -811,15 +820,15 @@ function clearBuffer(stream, state) {
buffered : ArrayPrototypeSlice(buffered, i);
chunks.allBuffers = (state[kState] & kAllBuffers) !== 0;

doWrite(stream, state, true, state.length, chunks, '', callback);
doWrite(stream, state, true, state.length, chunks, '', callback, buffered[i].onwrite);

resetBuffer(state);
} else {
do {
const { chunk, encoding, callback } = buffered[i];
const { chunk, encoding, callback, onwrite } = buffered[i];
buffered[i++] = null;
const len = objectMode ? 1 : chunk.length;
doWrite(stream, state, false, len, chunk, encoding, callback);
doWrite(stream, state, false, len, chunk, encoding, callback, onwrite);
} while (i < buffered.length && (state[kState] & kWriting) === 0);

if (i === buffered.length) {
Expand Down Expand Up @@ -1187,7 +1196,13 @@ Writable.fromWeb = function(writableStream, options) {
};

Writable.toWeb = function(streamWritable) {
return lazyWebStreams().newWritableStreamFromStreamWritable(streamWritable);
return lazyWebStreams().newWritableStreamFromStreamWritable(
streamWritable,
undefined,
writablePrototypeWrite,
writeKnownBuffer,
writeWithContext,
);
};

Writable.prototype[SymbolAsyncDispose] = async function() {
Expand Down
Loading