deno icon indicating copy to clipboard operation
deno copied to clipboard

fix(ext/web): `ReadableStream.from()` allows `Iterable` instead of `IterableIterator`

Open Milly opened this issue 1 year ago • 7 comments

createAsyncFromSyncIterator(x) which is used in ReadableStream.from() expects x as Iterable but, previous implements specify Iterator or IterableIterator. If it was IterableIterator, it would work, but if it was Iterator, an exception will occur.

Tests have been merged into WPT. https://github.com/web-platform-tests/wpt/pull/46365

Milly avatar May 20 '24 17:05 Milly

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar May 20 '24 17:05 CLAassistant

CC @iuioiua

bartlomieju avatar May 22 '24 23:05 bartlomieju

I'll update this PR once #23910 is merged.

iuioiua avatar May 23 '24 05:05 iuioiua

This change fixes the following (WPT) tests:

/streams/readable-streams/from.any.html - ReadableStream.from accepts a sync iterable of values
/streams/readable-streams/from.any.html - ReadableStream.from accepts a sync iterable of promises

However, it also causes the following (WPT) tests to fail:

/streams/readable-streams/from.any.html - ReadableStream.from throws on invalid iterables; specifically an object with a non-callable @@iterator method
/streams/readable-streams/from.any.html - ReadableStream.from re-throws errors from calling the @@iterator method
/streams/readable-streams/from.any.html - ReadableStream.from ignores a null @@asyncIterator

The 3 failures all show the same error:

assert_throws_js: from() should throw a TypeError function "() => ReadableStream.from(iterable)" did not throw...

iuioiua avatar May 23 '24 22:05 iuioiua

Currently, async generator + yield * is used to create an async iterator from a sync iterable. That is because it implements TC39 GetIterator.

However, the test fails because yield * does not throw an error even if an invalid Iterable is given.

Currently, the getIterator() function is only used to implement ReadableStream.from(). So instead of reproducing TC39 GetIterator (creating an async iterator from a sync iterator), I think it would be better to simply create a function that returns an async iterator or a sync iterator. In that case, the caller of the function must use try-catch and await appropriately for using iterator.

Milly avatar May 24 '24 00:05 Milly

Cool. There's been an improvement. The current failures give the same error:

assert_object_equals: first read should be correct expected property "0" missing

iuioiua avatar May 24 '24 08:05 iuioiua

In that case, the caller of the function must use try-catch and await appropriately for using iterator.

I made a mistake by not keeping up with what I said.

Milly avatar May 24 '24 23:05 Milly