question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Migrate from `stream` to asyncIterators

See original GitHub issue

Feature 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:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ThaUnknowncommented, Aug 5, 2022

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

0reactions
ThaUnknowncommented, Sep 10, 2022

I had a look to the examples and code, and I honestly fail to see the added value.

the benefit is compatibility with node and browser in one repo, without polyfills, smaller bundle size and higher speeds

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found