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

stronger-typed-streams

v0.2.0

Published

NodeJS Streams, but with stronger types

Downloads

14,098

Readme

stronger-typed-streams

While I am a fan of RxJS there are still advantages to using just plain NodeJS Streams, the main one for me being that back-pressure just works.

The TypeScript bindings for NodeJS Streams are perfectly adequate themselves but I felt that the typings could also ensure that the Readable is outputting to a compatible Writable or Transform.

This mini project, which really is pretty much just subclasses of the NodeJS Streams classes aims to address that deficiency. This means that if you have a model of the following:

`Farm` -> `Bakery` -> `Shop`

Where Flour is the input to the Bakery, which converts it into Bread for selling in the shop. Going from Farm to Shop directly is not allowed as customers of the shop wish to buy bread not flour.

In code the only valid way to join these should be:

let farm = new Farm({highWaterMark: 1});
let bakery = new Bakery({highWaterMark: 1});
let shop = new Shop({highWaterMark: 1}, stock);

let supplyChain = farm.pipe(bakery).pipe(shop);

But the core NodeJS TypeScript bindings would allow going directly from farm to shop.

Installation

npm install --save stronger-typed-streams

Usage

Create a subclass of to plant and sow grain:

class Farm extends Readable<Flour> {

    private i: number = 0;

    constructor(opts = {}) {
        super(Object.assign({objectMode: true}, opts));
    }

    _read() {
        let v = this.i++ < 8 ? new Flour(1) : null;
        process.nextTick(() => {
            this.push(v);
        });
    }
}

And a class that converts Flour to Bread:

class Bakery extends Transform<Flour, Bread> {

    private flour: Flour[] = [];

    constructor(opts = {}) {
        super(Object.assign({objectMode: true}, opts));
    }

    _flush(cb) {
        process.nextTick(() => {
            if (this.flour.length) {
                this.push(new Bread(this.flour));
            }
            this.flour = [];
            cb();
        });
    }

    _transform(flour: Flour, encoding, cb) {
        process.nextTick(() => {
            this.flour.push(flour);
            if (this.flour.length >= 3) {
                this._flush(cb);
                return;
            }
            cb();
        });
    }

}

And something to sell the finished product to the consumers:

class Shop extends Writable<Bread> {

    constructor(opts = {}, out) {
        super(Object.assign({objectMode: true}, opts));
    }

    _write(bread: Bread, encoding, cb) {
        process.nextTick(() => {
            console.log("Bread for sale!);
            cb();
        });
    }
}

Now create a supply chain by joining instances of all the classes together:

let farm = new Farm({highWaterMark: 1});
let bakery = new Bakery({highWaterMark: 1});
let shop = new Shop({highWaterMark: 1}, stock);

let supplyChain = farm.pipe(bakery).pipe(shop);

If you tried to attach a Farm directly to a Shop the TypeScript compiler would tell you:

TS2345: Argument of type 'Shop' is not assignable to parameter of type 'Writable<Flour>'.

Which is absolutely correct and allows you to catch errors at compile time instead of runtime, perhaps in the wild.

There is also a Duplex implementation, but this is only tested in Node versions >= 8 because in the tests I an API method that is only available in Node 8+.