diff --git a/doc/api/stream_iter.md b/doc/api/stream_iter.md index 412a8c468cf..b8fac507cba 100644 --- a/doc/api/stream_iter.md +++ b/doc/api/stream_iter.md @@ -602,9 +602,9 @@ Objects implementing `Symbol.for('Stream.toAsyncStreamable')` or precedence over the iteration protocols (`Symbol.asyncIterator`, `Symbol.iterator`). -The readable of a [`push()`][] stream without transforms and the iterables -returned by [`fromReadable()`][] already yield normalized batches, so `from()` -returns them unchanged. +The readable of a [`push()`][] stream without transforms, the iterables +returned by [`fromReadable()`][], and the results of `from()` itself already +yield normalized batches, so `from()` returns them unchanged. ```mjs import { Buffer } from 'node:buffer'; diff --git a/lib/internal/streams/iter/from.js b/lib/internal/streams/iter/from.js index 51d17c4e087..d882e2c5a78 100644 --- a/lib/internal/streams/iter/from.js +++ b/lib/internal/streams/iter/from.js @@ -180,6 +180,7 @@ function createNormalizationIterator(createIterator) { [SymbolAsyncIterator]() { return this; }, + [kValidatedSource]: true, }, null); } if (iterator[kNextSyncBatch] !== undefined) { @@ -202,6 +203,7 @@ function createNormalizationIterator(createIterator) { [SymbolAsyncIterator]() { return this; }, + [kValidatedSource]: true, }, null); } return ObjectSetPrototypeOf({ @@ -220,6 +222,7 @@ function createNormalizationIterator(createIterator) { [SymbolAsyncIterator]() { return this; }, + [kValidatedSource]: true, }, null); } @@ -229,6 +232,7 @@ function createNormalizationSource(createIterator) { [SymbolAsyncIterator]() { return createNormalizationIterator(createIterator); }, + [kValidatedSource]: true, }; } @@ -1349,6 +1353,7 @@ function from(input) { async *[SymbolAsyncIterator]() { yield [chunk]; }, + [kValidatedSource]: true, }; } @@ -1392,6 +1397,7 @@ function from(input) { async *[SymbolAsyncIterator]() { // Empty - yield nothing }, + [kValidatedSource]: true, }; } if (isUint8Array(input[0])) { @@ -1409,6 +1415,7 @@ function from(input) { } } }, + [kValidatedSource]: true, }; } } diff --git a/test/parallel/test-stream-iter-from-async.js b/test/parallel/test-stream-iter-from-async.js index 356f07353d7..e9199a3654d 100644 --- a/test/parallel/test-stream-iter-from-async.js +++ b/test/parallel/test-stream-iter-from-async.js @@ -801,7 +801,35 @@ async function testFromSyncSourceQueuesBehindReturn() { ]); } +// from() returns its own results unchanged: they already yield normalized +// batches, and normalizing them again would add a layer to every read. +async function testFromReturnsItsOwnResults() { + async function* asyncSource() { + yield 'a'; + yield [new Uint8Array([98])]; + } + + function* syncSource() { + yield 'c'; + yield new Uint8Array([100]); + } + const inputs = [ + [asyncSource(), 'ab'], + [syncSource(), 'cd'], + ['ef', 'ef'], + [[new Uint8Array([103]), new Uint8Array([104])], 'gh'], + [[], ''], + [{ [Symbol.for('Stream.toAsyncStreamable')]() { return 'ij'; } }, 'ij'], + ]; + for (const [input, expected] of inputs) { + const normalized = from(input); + assert.strictEqual(from(normalized), normalized); + assert.strictEqual(await text(from(from(normalized))), expected); + } +} + Promise.all([ + testFromReturnsItsOwnResults(), testFromString(), testFromAsyncGenerator(), testFromAsyncIteratorResultShapes(),