oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.28k stars 2.69k forks source link

Support `ReadableStream.from()` #3700

Open MattiasBuelens opened 1 year ago

MattiasBuelens commented 1 year ago

What is the problem this feature would solve?

Creating a ReadableStream from an array (or any other iterable) requires a lot of boilerplate, as can be seen in these tests and this example.

What is the feature you are proposing to solve the problem?

The Streams standard now has a new utility method ReadableStream.from(asyncIterable), which adapts any sync or async iterable into a ReadableStream. Once Bun supports this method, creating a ReadableStream from an array would become a one-liner.

Specification: https://streams.spec.whatwg.org/#rs-from Spec change: https://github.com/whatwg/streams/pull/1083 WPT tests: https://github.com/web-platform-tests/wpt/pull/27009

Deno and Node.js also recently implemented this new method, see https://github.com/denoland/deno/pull/19446 and https://github.com/nodejs/node/pull/48395.

Since Bun is powered by JavaScriptCore, you may also want to look into their pull request: https://github.com/WebKit/WebKit/pull/12454

What alternatives have you considered?

No response

paperdave commented 1 year ago

This should be easy for us to add. The implementation of it is just copy pasting what they do into our readable stream files. We use TS instead of JS is the main difference i think.

sravan-s commented 1 year ago

Hey @paperdave is it cool if I work on this?

paperdave commented 1 year ago

go for it. with the builtin functions right now you have to run make regenerate-bindings && make link to see changes reflected. (even though some internal docs say it reloads without this recompile, that doesn't extend to those just yet)

birkskyum commented 1 year ago

Likely fixed by bun 0.8 - @MattiasBuelens is it working for you now?

MattiasBuelens commented 1 year ago

@birkskyum Unfortunately no, bun 0.8 doesn't support this yet.

$ bun --version
0.8.1
$ cat index.ts
let rs = ReadableStream.from(["a", "b"]);
for await (const x of rs) {
        console.log(x);
}
console.log("done!");
$ bun run index.ts
1 | let rs = ReadableStream.from(["a", "b"]);
            ^
TypeError: ReadableStream.from is not a function. (In 'ReadableStream.from(["a", "b"])', 'ReadableStream.from' is undefined)
      at index.ts:1:9
birkskyum commented 1 year ago

Okay - the bun docs state that ReadableStream is fully implemented. Doesn't seem to be the case

MattiasBuelens commented 1 year ago

Hence why I opened this issue. 😅

Bun "fully supports" the Streams standard, but not yet the latest version of the standard. Streams is a living standard, which means new features are still being added. Browsers and runtimes need some time to catch up and implement the latest changes. That's totally fine, and expected.

For browsers and Node.js, you can track their progress on https://wpt.fyi/. For example, at the time of writing, Firefox Nightly and Node.js Nightly already support from(). (It would be cool if Bun had a similar dashboard... 🤔)