GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — stream: more iter streams fixes by jasnell · Pull Request #66030 · nodejs/node

stream: more iter streams fixes #66030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jasnell wants to merge 7 commits into nodejs:main from jasnell:jasnell/moar-iter-streams-2

Uh oh!

There was an error while loading. Please reload this page.

Closed

stream: more iter streams fixes #66030

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit Hold shift + click to select a range
37bec3d
stream: ensure nested async iterators are cancellation aware
jasnell Aug 30, 2026
4acd6f1
stream: make broadcast waiters use SafeSet
jasnell Aug 30, 2026
00f4652
stream: ensure protocol and transform accessors are called once
jasnell Aug 30, 2026
8cc140e
stream: have broadcast clean factory listeners on cancel
jasnell Aug 30, 2026
efc2f37
stream: ensure streamable protocols precede array/validated-source fa…
jasnell Aug 30, 2026
a409c52
stream: make Broadcast.from() only return the Broadcast
jasnell Aug 30, 2026
d0ff99c
stream: update ShareImpl to retain factory abort listener
jasnell Sep 17, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
stream: ensure streamable protocols precede array/validated-source fa…
…st paths

Signed-off-by: James M Snell <[email protected]>
Assisted-by: Opencode
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@jasnell
jasnell committed Sep 17, 2026
commit efc2f379304f6c6501d62398758b7bfc45fec8fd

There are no files selected for viewing

70 changes: 35 additions & 35 deletions 70 lib/internal/streams/iter/from.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,13 @@ function fromSync(input) {
};
}

// Check toStreamable protocol (takes precedence over iteration protocols).
// toAsyncStreamable is ignored entirely in fromSync.
const streamableMethod = getProtocolMethod(input, toStreamable);
if (streamableMethod !== undefined) {
return fromSync(FunctionPrototypeCall(streamableMethod, input));
}

// Fast path: Uint8Array[] - yield in bounded sub-batches.
// Yielding the entire array as one batch forces downstream transforms
// to process all data at once, causing peak memory proportional to total
Expand Down Expand Up @@ -696,13 +703,6 @@ function fromSync(input) {
}
}

// Check toStreamable protocol (takes precedence over iteration protocols).
// toAsyncStreamable is ignored entirely in fromSync.
const streamableMethod = getProtocolMethod(input, toStreamable);
if (streamableMethod !== undefined) {
return fromSync(FunctionPrototypeCall(streamableMethod, input));
}

const isIterable = isSyncIterable(input);

// Reject explicit async-only inputs
Expand Down Expand Up @@ -751,11 +751,6 @@ function from(input) {
throw new ERR_INVALID_ARG_TYPE('input', 'a non-null value', input);
}

// Fast path: validated source already yields valid Uint8Array[] batches
if (input[kValidatedSource]) {
return input;
}

// Check for primitives first (ByteInput)
if (isPrimitiveChunk(input)) {
const chunk = primitiveToUint8Array(input);
Expand All @@ -767,6 +762,34 @@ function from(input) {
};
}

// Check toAsyncStreamable protocol (takes precedence over toStreamable and
// iteration protocols)
const asyncStreamableMethod = getProtocolMethod(input, toAsyncStreamable);
if (asyncStreamableMethod !== undefined) {
let result = FunctionPrototypeCall(asyncStreamableMethod, input);
if (isPromise(result)) {
result = PromisePrototypeThen(result, undefined, undefined);
markPromiseAsHandled(result);
}
// Synchronous validated source (e.g. Readable batched iterator)
if (result?.[kValidatedSource]) {
return result;
}
return createNormalizationSource(
(context) => normalizeAsyncStreamableResult(result, context));
}

// Check toStreamable protocol (takes precedence over iteration protocols)
const streamableMethod = getProtocolMethod(input, toStreamable);
if (streamableMethod !== undefined) {
return from(FunctionPrototypeCall(streamableMethod, input));
}

// Fast path: validated source already yields valid Uint8Array[] batches
if (input[kValidatedSource]) {
return input;
}

// Fast path: Uint8Array[] - yield in bounded sub-batches.
// Yielding the entire array as one batch forces downstream transforms
// to process all data at once, causing peak memory proportional to total
Expand Down Expand Up @@ -801,29 +824,6 @@ function from(input) {
}
}

// Check toAsyncStreamable protocol (takes precedence over toStreamable and
// iteration protocols)
const asyncStreamableMethod = getProtocolMethod(input, toAsyncStreamable);
if (asyncStreamableMethod !== undefined) {
let result = FunctionPrototypeCall(asyncStreamableMethod, input);
if (isPromise(result)) {
result = PromisePrototypeThen(result, undefined, undefined);
markPromiseAsHandled(result);
}
// Synchronous validated source (e.g. Readable batched iterator)
if (result?.[kValidatedSource]) {
return result;
}
return createNormalizationSource(
(context) => normalizeAsyncStreamableResult(result, context));
}

// Check toStreamable protocol (takes precedence over iteration protocols)
const streamableMethod = getProtocolMethod(input, toStreamable);
if (streamableMethod !== undefined) {
return from(FunctionPrototypeCall(streamableMethod, input));
}

// Must be a Streamable (sync or async iterable)
if (!isSyncIterable(input) && !isAsyncIterable(input)) {
throw new ERR_INVALID_ARG_TYPE(
Expand Down
40 changes: 38 additions & 2 deletions 40 test/parallel/test-stream-iter-property-access.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const common = require('../common');
const assert = require('assert');
const { Readable } = require('stream');
const {
Broadcast,
Share,
Expand All @@ -21,13 +22,14 @@ const {
shareProtocol,
shareSync,
shareSyncProtocol,
text,
textSync,
toAsyncStreamable,
toStreamable,
} = require('stream/iter');

function protocolFixture(symbol, result) {
function protocolFixture(symbol, result, input = {}) {
let accesses = 0;
const input = {};
const method = common.mustCall(function() {
assert.strictEqual(this, input);
return result;
Expand Down Expand Up @@ -83,6 +85,38 @@ async function testFromSyncSnapshotsProtocolMethods() {
assert.strictEqual(fixture.accesses, 1);
}

async function testArrayFastPathsHonorProtocols() {
const asyncInputs = [
protocolFixture(toAsyncStreamable, 'empty-async', []),
protocolFixture(toStreamable, 'batch-async', [new Uint8Array([0])]),
];
for (const fixture of asyncInputs) {
assert.match(await text(from(fixture.input)), /-async$/);
assert.strictEqual(fixture.accesses, 1);
}

const syncInputs = [
protocolFixture(toStreamable, 'empty-sync', []),
protocolFixture(toStreamable, 'batch-sync', [new Uint8Array([0])]),
];
for (const fixture of syncInputs) {
assert.match(textSync(fromSync(fixture.input)), /-sync$/);
assert.strictEqual(fixture.accesses, 1);
}
}

async function testValidatedSourceHonorsProtocol() {
const readable = Readable.from(['ignored']);
const validated = readable[toAsyncStreamable]();
assert.strictEqual(from(validated), validated);

const fixture = protocolFixture(
toAsyncStreamable, 'validated-protocol', validated);
assert.strictEqual(await text(from(fixture.input)), 'validated-protocol');
assert.strictEqual(fixture.accesses, 1);
readable.destroy();
}

async function testPullSnapshotsStatefulTransform() {
const fixture = statefulTransformFixture();
const controller = new AbortController();
Expand Down Expand Up @@ -136,6 +170,8 @@ async function testDrainableProtocolSnapshotMethod() {
Promise.all([
testFromSnapshotsProtocolMethods(),
testFromSyncSnapshotsProtocolMethods(),
testArrayFastPathsHonorProtocols(),
testValidatedSourceHonorsProtocol(),
testPullSnapshotsStatefulTransform(),
testPullSyncSnapshotsStatefulTransform(),
testMultiConsumerProtocolsSnapshotMethods(),
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.