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

dope-stream

v0.3.5

Published

A simple and chain-able library to work with streams. It supports back-pressure by default by enforcing function as async.

Downloads

2

Readme

dope-stream

A simple and chain-able library to work with streams. It supports back-pressure by default by enforcing function as async.

Usage

There are 2 different pipes in this library.

1: Pipe

this is a fundamental building block which dictates 3 main functions, map, filter and forEach.

interface Pipe<Value> {
  map<NewValue>(fn: (val: Value) => Promise<NewValue>): Pipe<NewValue>
  filter(fn: (val: Value) => Promise<boolean>): Pipe<Value>
  forEach(fn: (val: Value) => Promise<void>): void
}

2: PushPipe

this is an extended version of Pipe but with the ability to push values into the pipe.

export interface PushPipe<Value> extends Pipe<Value> {
  push(val: Value): Promise<void>
}

push method is a promised method. One interesting feature of push is that if any method inside pipe throws an exception you can catch it.

import { createPushPipe } from 'dope-stream'

const pipe = createPushPipe<number>()

pipe.map(async (val) => {
  if (n === 0) {
    throw "value should not be zero"
  }

  return 1/n
}).forEach(async (val) => {
  console.log(`the value is: ${val}`)
})


try {
  await pipe.push(1) // this is OK
  await pipe.push(2) // this is OK
  await pipe.push(0) // this will catch the error
} catch (e) {
  //
}

There is a special case of PushPipe which works with Readable streams

import { Readable } from 'stream'
import { createSourcePipe } from 'dope-stream'

class DummyReadable extends Readable {
  constructor() {
    super({
      objectMode: true,
      read: () => {
        this.push({ data: new Date() })
      },
      highWaterMark: 10,
    })
  }
}

const source = new DummyReadable()
source.pause() // pause the stream

// create a pipe out of source which is a Readable stream
const pipe = createSourcePipe(source)

pipe.map(async (msg) => {
  return msg
}).forEach(async (msg) => {
  console.log(msg)
})

Note: Because each function in pipe is an async function, the back-pressure automatically created for you behind the scene. So once the forEach returns, then the next message will extract from source and pass down through pipe.