npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

stream-to-iterator

v3.0.3

Published

Convert a node.js stream into an iterator.

Downloads

3,550

Readme

stream-to-iterator

SOFT DEPRECATION: Please use native Readable async iteration support instead, available from node.js v10.

Converts a node.js stream into an iterator.

Build Status Dependency Status devDependency Status node

Usage

With for-await-of:

(async () => {
  const streamToIterator = require('stream-to-iterator')
  const intoStream = require('into-stream')

  const readable = intoStream.obj([2, 3, 4])
  const iterator = streamToIterator(readable)
  const allValues = []

  for await (let value of iterator) {
    allValues.push(value * value)
  }

  console.log(allValues) // [ 4, 9, 16 ]
})()

API

streamToIterator(readable, opts)

Creates a writable stream that is also an async iterator and pipes the readable stream to it. Chunks are drained from the source stream as objects are requested from the iterator.

Requested values are fulfilled serially. Requesting a value (AsyncIterator#next) from the iterator without waiting for a previous request to finish will result to the resolved value being equal to the resolved value of the previous request.

The underlying writable stream defaults to object mode. This is because the object converted into (the iterator) does not really need to be in a specific object or non-object mode.

For convenience, the iterator also implements the AsyncIterable interface. Be careful though: this only reflects the current state of the iterator.

The iterator will rethrow any error emitted on the stream on the next iteration read from it after the error is handled.

Parameters:

  • readable: Readable<T> - A node.js-style readable stream. Streams v1-3 are supported (via node core Readable.wrap()).
  • opts: Object - Options to pass to the underlying writable stream. Default: { objectMode: true }. No merging is performed.

Returns: AsyncIterator<T> where AsyncIterator<T> is:

interface AsyncIterator<T> {
  next(): Promise<{ done: boolean; value: T }>
}

Migrating from v2

The old mode implementing Iterator<Promise<T>> is still supported. However, there are a few breaking changes:

  • streamToIterator does not return a Promise anymore.
  • To get an instance of the iterator, call [Symbol.iterator]() on the return value.
  • Call await init() on the returned iterator to initialize it. This is the equivalent of calling the removed Promise interface on the original function.
  • As with the new API, calling Iterator#next before waiting for the promise to finish will return the same value as the previous iteration.
const iterator = await streamToIterator(readable).init()
for (let valuePromise of iterator) {
  const value = await valuePromise
  // do something with value
}

(See also the example on the tests.)

The old consumption rules still apply:

  • The iteration value returned will be of type Promise<T>. To get the iteration value, the consumer must wait for the resolution of that promised value.
  • The consumer must not prematurely get the next value of the iterator (i.e. call Iterator#next()) until such time that the current promise value of the iterator is resolved.

The iterator throws if the iterator was called before the first chunk was received.

See also