diff --git a/lib/internal/streams/iter/broadcast.js b/lib/internal/streams/iter/broadcast.js index fb16f51bd1f..62b5a6e1be7 100644 --- a/lib/internal/streams/iter/broadcast.js +++ b/lib/internal/streams/iter/broadcast.js @@ -615,9 +615,11 @@ class BroadcastWriter { if (this.#canUseWriteFastPath(signal)) { const converted = convertChunks(chunks); const batch = createBatchEntry(converted); - this.#broadcast[kWrite](batch); - this.#totalBytes += batch.byteLength; - return kResolvedPromise; + if (this.#state === 'open' && this.#broadcast[kWrite](batch)) { + this.#totalBytes += batch.byteLength; + return kResolvedPromise; + } + return this.#writeBatchSlow(batch, signal); } return this.#writevSlow(chunks, signal); } @@ -634,6 +636,19 @@ class BroadcastWriter { const batch = createBatchEntry(convertChunks(chunks)); + return this.#writeBatchSlow(batch, signal); + } + + async #writeBatchSlow(batch, signal) { + if (this.#state === 'errored') { + throw this.#error; + } + if (this.#state !== 'open') { + throw new ERR_INVALID_STATE.TypeError('Writer is closed'); + } + + signal?.throwIfAborted(); + if (this.#broadcast[kWrite](batch)) { this.#totalBytes += batch.byteLength; return; diff --git a/lib/internal/streams/iter/push.js b/lib/internal/streams/iter/push.js index a4e53737939..503454f6a69 100644 --- a/lib/internal/streams/iter/push.js +++ b/lib/internal/streams/iter/push.js @@ -665,12 +665,10 @@ class PushWriter { writev(chunks, options) { validateArray(chunks, 'chunks'); const signal = getWriterSignal(options); - if (!signal && this.#queue.canWriteSync()) { - const bytes = convertChunks(chunks); - this.#queue.writeSync(bytes); + const bytes = convertChunks(chunks); + if (!signal && this.#queue.writeSync(bytes)) { return kResolvedPromise; } - const bytes = convertChunks(chunks); return this.#queue.writeAsync(bytes, signal); } diff --git a/lib/internal/streams/iter/utils.js b/lib/internal/streams/iter/utils.js index 53610dbe631..66e831a5852 100644 --- a/lib/internal/streams/iter/utils.js +++ b/lib/internal/streams/iter/utils.js @@ -185,19 +185,6 @@ function toUint8Array(chunk) { return chunk; } -/** - * Check if all chunks in an array are already Uint8Array. - * Short-circuits on the first non-Uint8Array chunk found. - * @param {Array} chunks - * @returns {boolean} - */ -function allUint8Array(chunks) { - for (let i = 0; i < chunks.length; i++) { - if (!isUint8Array(chunks[i])) return false; - } - return true; -} - function snapshotByteView(value) { const buffer = TypedArrayPrototypeGetBuffer(value); const sharedBufferView = isSharedArrayBuffer(buffer) ? @@ -317,9 +304,6 @@ function concatBytes(chunks) { * @returns {Uint8Array[]} */ function convertChunks(chunks) { - if (allUint8Array(chunks)) { - return ArrayPrototypeSlice(chunks); - } const len = chunks.length; const result = new Array(len); for (let i = 0; i < len; i++) { @@ -446,7 +430,6 @@ module.exports = { kMultiConsumerDefaultBudget, kPushDefaultBudget, kResolvedPromise, - allUint8Array, concatBytes, convertChunks, createBatchEntry, diff --git a/test/parallel/test-stream-iter-writer-reentrancy.js b/test/parallel/test-stream-iter-writer-reentrancy.js new file mode 100644 index 00000000000..119bda26923 --- /dev/null +++ b/test/parallel/test-stream-iter-writer-reentrancy.js @@ -0,0 +1,65 @@ +// Flags: --experimental-stream-iter +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const { broadcast, push } = require('stream/iter'); + +const factories = [ + () => push({ budget: 16384 }), + () => { + const { writer, broadcast: bc } = broadcast({ budget: 16384 }); + return { __proto__: null, writer, readable: bc.push() }; + }, +]; + +async function testWritevReentrancy() { + for (const factory of factories) { + const { writer, readable } = factory(); + const chunks = []; + Object.defineProperty(chunks, 0, { + __proto__: null, + enumerable: true, + get: common.mustCall(() => { + assert.strictEqual( + writer.writeSync(new Uint8Array(16384)), true); + return Uint8Array.of(42); + }), + }); + + let resolved = false; + const write = writer.writev(chunks).then(common.mustCall(() => { + resolved = true; + })); + await new Promise(setImmediate); + assert.strictEqual(resolved, false); + + const iterator = readable[Symbol.asyncIterator](); + assert.strictEqual((await iterator.next()).value[0].byteLength, 16384); + await write; + assert.strictEqual((await iterator.next()).value[0][0], 42); + writer.endSync(); + assert.strictEqual((await iterator.next()).done, true); + } + + for (const factory of factories) { + const { writer, readable } = factory(); + const chunks = []; + Object.defineProperty(chunks, 0, { + __proto__: null, + enumerable: true, + get: common.mustCall(() => { + writer.endSync(); + return Uint8Array.of(42); + }), + }); + + await assert.rejects(writer.writev(chunks), { + code: 'ERR_INVALID_STATE', + }); + assert.strictEqual( + (await readable[Symbol.asyncIterator]().next()).done, true); + } +} + +testWritevReentrancy().then(common.mustCall());