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

ministreamiterator

v2.0.2

Published

Simple stream API for for await()

Downloads

8

Readme

Async iterable stream

This is a tiny library for creating async iterable objects:

for await (const item of iter) {
  console.log('some async item')
}

from streams:

import { createIterableStream } from 'ministreamiterator'

const stream = createIterableStream()

// Send items into the stream
setInterval(() => stream.append(Date.now()), 1000)

// And consume them using a for-await loop:
for await (const time of stream.iter) {
  console.log('the time is', time)
}

Its similar to streamiterator, except:

  • There is no out-of-the-box iteroperability with nodejs stream objects (because node streams are big and complicated)
  • This library has no external dependancies. Minified and gzipped, this library adds only a couple hundred bytes to your bundle.

Lifecycle

The stream has 2 states: Open and closed. The stream can be closed either from the producer or the consumer.

To close the stream from the producer, call stream.end():

const stream = createIterableStream()
stream.append(1)
stream.append(2)
stream.end() // Producer indicates there are no more events to send into the stream

for await (const item of stream.iter) {
  console.log(item)
}
console.log('done')

// Prints 1, 2, done.

The consumer can indicate they are done consuming the stream by calling iter.return():

When the consumer closes the stream, the stream will call a registered onClosed function. This function is registered by passing it as the first (and only) argument to the streamToIter constructor. (Eg streamToIter(() => console.log('closed')))

NOTE: The onClosed function is only called if the consumer closes the stream via iter.return(). It is not called if the producer closes the stream via stream.end().

let timer
const stream = createIterableStream(() => {
  clearInterval(timer) // Stop firing the interval once the consumer has finished reading from the stream
})
timer = setInterval(() => stream.append(Date.now()), 1000)

for await (const item of stream.iter) {
  if (Math.random() < 0.5) break
}
stream.iter.return()

Handing errors

You can also inject errors into the stream. Errors will cause an exception to be thrown from the for await loop:

const stream = createIterableStream()
stream.throw(new Error('oops!'))

// ...


for await (const item of stream.iter) {

} // throws Error('oops!')!

Once an error is injected into the stream, the stream is considered to be done. No further events can be injected into the stream. The stream currently does call a registered onClose handler here - I'm not sure if thats the right call but this decision will not change without a bump to the major version of this library.