Files
node/test/parallel/test-stream-iter-iterator-result.js
James M Snell 5df8e3ab1d stream: remove async generator layers from stream/iter pull()
pull() read its pipeline through an async generator delegating to it
with yield*, and without transforms the pipeline was another such
generator around the source, each costing several promises per batch.

The pipelines already start lazily and queue calls as an async
generator does, so use the pipeline directly, and write the pipeline
without transforms out by hand: it checks the signal on the first
next(), then passes every call to the iterator reading the source.
Results are now IterResult objects, like those of the other stream/iter
iterators; one test compared them as ordinary objects.

With an async generator yielding 16-byte chunks, one per batch, pull()
is about 42% faster without transforms and 30% faster with a signal,
and about 12% faster through a transform.

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>
2026-11-25 18:00:49 +01:00

87 lines
2.5 KiB
JavaScript

// Flags: --experimental-stream-iter
'use strict';
// Iterator results created by the stream/iter iterators themselves do not
// inherit from Object.prototype, so prototype pollution cannot affect them.
const common = require('../common');
const assert = require('assert');
const { inspect } = require('util');
const {
broadcast,
from,
pull,
push,
share,
shareSync,
} = require('stream/iter');
function assertResult(result, done) {
assert.strictEqual(result instanceof Object, false);
assert.deepStrictEqual(Object.keys(result), ['done', 'value']);
assert.strictEqual(result.done, done);
assert.match(inspect(result), /^IterResult \{ done: (true|false), value: /);
}
async function testAsyncIterators() {
const sources = {
'push()': () => {
const { writer, readable } = push();
writer.writeSync('a');
writer.endSync();
return readable;
},
'share()': () => share(from('a')).pull(),
'pull()': () => pull(from('a')),
'pull() with a signal': () => pull(from('a'), {
signal: new AbortController().signal,
}),
'pull() with a transform': () => pull(from('a'), (chunks) => chunks),
'from() of an async iterable': () => from((async function*() {
yield 'a';
})()),
'broadcast()': () => {
const { writer, broadcast: bc } = broadcast();
const consumer = bc.push();
writer.writeSync('a');
writer.endSync();
return consumer;
},
};
for (const create of Object.values(sources)) {
const iterator = create()[Symbol.asyncIterator]();
assertResult(await iterator.next(), false);
assertResult(await iterator.next(), true);
}
}
function testShareSync() {
const iterator = shareSync(['a']).pull()[Symbol.iterator]();
assertResult(iterator.next(), false);
assertResult(iterator.next(), true);
}
async function testPollutedThen() {
// Resolving an async next() with an object looks up `then`. Results must
// not pick it up from a polluted Object.prototype.
const { writer, readable } = push();
writer.writeSync('ab');
writer.endSync();
const iterator = readable[Symbol.asyncIterator]();
Object.prototype.then = common.mustNotCall('Object.prototype.then');
try {
const first = await iterator.next();
assert.strictEqual(first.done, false);
assert.strictEqual(new TextDecoder().decode(first.value[0]), 'ab');
assert.strictEqual((await iterator.next()).done, true);
} finally {
delete Object.prototype.then;
}
}
(async () => {
await testAsyncIterators();
testShareSync();
await testPollutedThen();
})().then(common.mustCall());