Migrate from `stream` to asyncIterators
See original GitHub issueFeature Request
Migrate from node.js’s stream
library to asyncIterators. This would roughly look like this:
before
function parse (input) {
input.on('data', handleData) // requires nodeJS-like streams
}
after
async function parse (input){
// can be lazy functions returning streams
if (typeof input === 'function') input = input()
for await (const data of input){
handleData(data)
}
}
Why this is nice:
- drops the node only dependencies
- reduces package size
- allows for input to be ANYTHING which has an async iterator [node.js streams have async iterators, but the user can also implement their own async data iterators]
- allows for the library to be ran natively on any platform [node, deno, bun, browser] without transpiling
- doesn’t require any browser specific library
example usage of this kind of implementation in node.js:
import { parse } from 'music-metadata'
const filestream = fs.createReadStream('./song.mp3')
const result = await parse(filestream)
example usage in browser:
import { parse } from 'music-metadata'
import 'fast-readable-async-iterator'
const filestream = blob.stream()
const result = await parse(filestream)
Issue Analytics
- State:
- Created a year ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Easier Node.js streams via async iteration - 2ality
Async iteration provides an elegant alternative to transform streams for processing streamed data in multiple steps: The input is a readable ...
Read more >Async iterators, operators and pull based streams. - Medium
A simple map function replaces a whole transform stream. What is left to you however, is to make sure the platform specific data...
Read more >2996-async-iterator - The Rust RFC Book
Introduce the AsyncIterator trait into the standard library, using the design from futures . Redirect the Stream trait definition in the futures-core crate...
Read more >stream-to-async-iterator - npm
ES async interator wrapper for node streams. Latest version: 1.0.0, last published: 9 months ago. Start using stream-to-async-iterator in ...
Read more >docs: writable streams from async iterators example issue ...
Given the example from https://nodejs.org/api/stream.html#stream_piping_to_writable_streams_from_async_iterators.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
some examples of me using async iterators instead of streams: https://github.com/ThaUnknown/join-async-iterator joins multiple streams into one
https://github.com/ThaUnknown/fast-blob-stream converts blobs and web streams into node streams
https://github.com/ThaUnknown/fast-readable-async-iterator creates async iterators from web streams
the benefit is compatibility with node and browser in one repo, without polyfills, smaller bundle size and higher speeds