Sec-ant / torrefy

An ESM package that uses Web Streams API to create v1, v2 or hybrid torrents in your web browser
https://www.npmjs.com/package/torrefy
MIT License
11 stars 1 forks source link

refactor web streams to async iterator/generators #3

Open Sec-ant opened 1 year ago

Sec-ant commented 1 year ago
Some related links - [The async iterator and async iterable protocols](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) - https://streams.spec.whatwg.org/#rs-asynciterator - [MattiasBuelens/web-streams-polyfill](https://github.com/MattiasBuelens/web-streams-polyfill) - [dy/readable-stream-async-iterator.js](https://gist.github.com/dy/8ca154c96e2b2a823c6501d29972b8a8) - [streamxorg/streamx](https://github.com/streamxorg/streamx) - https://github.com/webtorrent/create-torrent/issues/177#issuecomment-1323750843 - https://github.com/webtorrent/webtorrent/issues/1971 - https://github.com/webtorrent/create-torrent/pull/185 - [Some Async Iterators Implemented By @ThaUnknown](https://github.com/ThaUnknown?tab=repositories&q=iterator&type=&language=&sort=) - [Sec-ant/readable-stream](https://github.com/Sec-ant/readable-stream) - [Typescript Module Augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation) - [rauschma/iterable-to-readable-stream.mjs](https://gist.github.com/rauschma/ef61906c15de3f5a2b73c2d480477359)

Basically, there're two functions that need implementing:

Other works left including refactoring transformers to async generators are already done in another branch of this repo. But they still need testing as I also refactored many other places.

ThaUnknown commented 1 year ago

rauschma/iterable-to-readable-stream.mjs is bad as it never closes the iterator if the stream is cancelled see for a safer implementation: https://github.com/webtorrent/webtorrent/pull/2414/files#diff-387904f6b07263914bc1b0cb291a27832cc6963e01b5d44b75460771fc25ee8fR153 this doesn't implement lazy loading iterators via start, but you can pretty much cop-paste that

dy/readable-stream-async-iterator.js, see https://github.com/ThaUnknown/fast-readable-async-iterator/blob/main/index.js which also caches 1 value ahead for streams without hwm, both are good

Sec-ant commented 1 year ago

@ThaUnknown Again, thanks for your tips! Yeah, I simply gathered all the related discussions/implementations as a reminder. I went through some of the links and realized some of them might not be what I need but nevertheless steps guiding me to the right direction😄. I really appreciate that many of your repos have offered me much help! I'm currently using a simple implementation written by myself: Sec-ant/readable-stream.