Commit 7c692e74e8c for nodejs

commit 7c692e74e8c199968bb2b2934fd1ee27d87f8644
Author: James M Snell <jasnell@gmail.com>
Date:   Wed Oct 7 02:18:38 2026 +0000

    stream: return stream/iter from() results unchanged from from()

    from() returns sources known to yield normalized batches unchanged,
    such as the readable of a push() stream, but not its own results. Since
    pipeTo(), pull() and the consumers call from() on their source, a
    stream created with from() was normalized a second time when passed to
    them, adding a layer to every read and hiding the fast paths that
    pipeTo() uses to read sync and async sources.

    Mark the results of from() as validated sources, so that from() returns
    them unchanged, as the specification allows for async iterables created
    by the implementation that yield only normalized batches.

    Piping from(source) is now as fast as piping source: for a generator
    yielding 16-byte chunks one per batch, about 2.8 times faster when it
    is a sync generator and 1.7 times faster when it is an async one.

    Assisted-by: OpenCode
    Signed-off-by: James M Snell <jasnell@gmail.com>
    PR-URL: https://github.com/nodejs/node/pull/66504
    Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
    Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>

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(),