Skip to content

ReadableStream.from() rejects callable iterator results #67894

Backlog

Description

@dani7514

Version

v24.21.0 and v26.13.1

Platform

Microsoft Windows NT 10.0.26300.0 x64

Subsystem

Web Streams (node:stream/web)

What steps will reproduce the bug?

Save this as repro.mjs and run node repro.mjs. It uses only built-in APIs.

import { ReadableStream } from 'node:stream/web';

const chunk = Object.assign(() => {}, { done: false, value: 'hello' });
const end = Object.assign(() => {}, { done: true });

const iterable = {
Symbol.iterator {
return {
next: () => chunk,
return: () => end,
};
},
};

const results = await Promise.allSettled([
ReadableStream.from(iterable).getReader().read(),
ReadableStream.from(iterable).cancel(),
]);

for (const [index, result] of results.entries()) {
console.log(index === 0 ? 'read' : 'cancel',
result.status === 'fulfilled' ? result.value : result.reason.message);
}

How often does it reproduce? Is there a required condition?

Every run on both versions tested. The failure occurs when next() or return() returns a function object. It also reproduces with an async iterator.

What is the expected behavior? Why is that the expected behavior?

The read should fulfill with { value: 'hello', done: false }, and cancellation should fulfill with undefined. Functions are objects under ECMAScript, so they satisfy the iterator result object check. Web IDL iterator rules

What do you see instead?

Both operations reject with TypeError [ERR_INVALID_STATE]. The read says iterator.next() must fulfill with an object. Cancellation reports the same error for iterator.return().

Additional information

Node.js main already accepts functions as the iterator itself, but the two checks on the results of next() and return() only accept typeof result === 'object'. Deno 2.9.6 accepted all four read and cancellation cases with sync and async iterators. Node.js source

Run the snippet yourself before submitting and use your own wording for the prose fields. In Additional information, disclose that AI helped investigate the bug and prepare the reproduction, then say what you personally verified. Node.js AI guidelines

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions