diff --git a/lib/internal/streams/iter/pull.js b/lib/internal/streams/iter/pull.js index b8dcd03c761..9dca617e7f5 100644 --- a/lib/internal/streams/iter/pull.js +++ b/lib/internal/streams/iter/pull.js @@ -70,6 +70,7 @@ const { parsePullArgs, raceSignal, snapshotFixedBatch, + throwByteViewChanged, snapshotTransform, toUint8Array, validateBatchEntry, @@ -1769,6 +1770,46 @@ async function pipeTo(source, ...args) { } } + // Write a FixedBatch with write(), for a writer without writeSync(), + // starting at `index`: returns undefined if every write() returned + // undefined, or a promise for writing the rest once one returns a + // promise. A write() that returns undefined is not awaited. + function writeFixedBatchAsync(fixed, index) { + const { chunks } = fixed; + for (let i = index; i < chunks.length; i++) { + const result = writer.write(checkFixedBatchChunk(fixed, i), writeOptions); + checkFixedBatchChunk(fixed, i); + if (result !== undefined) { + return PromisePrototypeThen(PromiseResolve(result), () => { + checkFixedBatchChunk(fixed, i); + totalBytes += fixed.byteLengths[i]; + return writeFixedBatchAsync(fixed, i + 1); + }); + } + totalBytes += fixed.byteLengths[i]; + } + } + + // Write a single chunk with write(), for a writer without writeSync(), + // as writeFixedBatchAsync() does, without a FixedBatch. + function writeChunkAsync(chunk) { + const byteLength = TypedArrayPrototypeGetByteLength(chunk); + const result = writer.write(chunk, writeOptions); + if (TypedArrayPrototypeGetByteLength(chunk) !== byteLength) { + throwByteViewChanged(); + } + if (result === undefined) { + totalBytes += byteLength; + return; + } + return PromisePrototypeThen(PromiseResolve(result), () => { + if (TypedArrayPrototypeGetByteLength(chunk) !== byteLength) { + throwByteViewChanged(); + } + totalBytes += byteLength; + }); + } + // Write a FixedBatch with writeSync(), like the loop at the end of // writeBatch(), falling back to the async path for the rest of the batch. function writeFixedBatch(fixed) { @@ -1862,6 +1903,12 @@ async function pipeTo(source, ...args) { // Returns undefined on sync success, or a Promise when async fallback // is required. Callers must check: const p = writeBatch(b); if (p) await p; function writeBatch(batch) { + // A writer with only write() (and writev(), used for batches of several + // chunks): no batch entry. + if (!hasWriteSync && (!hasWritev || batch.length === 1)) { + if (batch.length === 1) return writeChunkAsync(batch[0]); + return writeFixedBatchAsync(snapshotFixedBatch(batch), 0); + } // Single chunk, the common case: check the view around writeSync() // without allocating a batch entry, and create one only to fall back to // the async path. diff --git a/test/parallel/test-stream-iter-resizable-buffers.js b/test/parallel/test-stream-iter-resizable-buffers.js index 3704e73e487..80375ffaf2d 100644 --- a/test/parallel/test-stream-iter-resizable-buffers.js +++ b/test/parallel/test-stream-iter-resizable-buffers.js @@ -286,6 +286,32 @@ async function testConsumersRejectDetachedViews() { assert.strictEqual(result, chunk); } +// A writer with only write() is checked the same way, before and after each +// write(), and after the promise it returns, if any. +async function testPipeRejectsDetachWithWriteOnly() { + for (const detachIndex of [1, 0]) { + const buffers = [new ArrayBuffer(2), new ArrayBuffer(2)]; + const written = []; + await assert.rejects(pipeTo([buffers.map((b) => new Uint8Array(b))], { + write(chunk) { + written.push(chunk.byteLength); + if (written.length === 1) buffers[detachIndex].transfer(); + }, + fail: common.mustCall(), + }), kResizeError); + assert.deepStrictEqual(written, [2]); + } + + const buffer = new ArrayBuffer(2); + await assert.rejects(pipeTo([new Uint8Array(buffer)], { + async write() { + await null; + buffer.transfer(); + }, + fail: common.mustCall(), + }), kResizeError); +} + // Only what was accounted for a view is checked: its byteLength. A // fixed-length view of a resizable buffer that stays in bounds is unchanged. async function testFixedLengthViewOfResizedBuffer() { @@ -307,6 +333,7 @@ async function testFixedLengthViewOfResizedBuffer() { } Promise.all([ + testPipeRejectsDetachWithWriteOnly(), testFixedLengthViewOfResizedBuffer(), testBufferedViewMutationRejected(), testDropOldestUsesAcceptedByteLength(),