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

node-web-streams-adapter

v0.0.1

Published

Use the new Web Streams API built on top of Node Streams

Downloads

7

Readme

node-web-streams-adapter

Use the new Web Streams API built on top of Node Streams

Installation

npm install node-web-streams-adapter

Usage

Automatically assigns ReadableStream and WritableStream to global, window or self.

var readable = new ReadableStream({
    start(controller) {
        // initialize the source we're reading from
        this.myDataSource = SomethingWereReadingFrom()
    },
    async pull(controller) {
        // read (next) chunk from the source and add it to the 
        var chunk = await this.myDataSource.getData(controller.desiredSize)
        controller.enqueue(chunk)
    },
    cancel(reason) {
        // safely close our source if the stream gets cancelled
        this.myDataSource.destroy()
    }
}, {highWaterMark: 1024})

var writeable = new WriteableStream({
    // TODO, work in progress
})

readable.pipeTo(writeable)

To learn more about the upcoming Web Streams API standard, read the spec.

Caveats

Node's Readable streams pushes (to the stream) and dispatches (to consumer) data synchronously. E.g. Immediately after adding new chunk to the stream with readable.push(chunk), the stream sends to to consumer, empties the inner buffer and calls _read again even if the previous call is not done yet.

Simply put:

class MyReadable extends stream.Readable {
    _read(size) {
        // get the chunk
        var chunk = somehowGetTheChunk(size)
        // add it to the stream's internal buffer
        this.push(chunk)
        // WARNING: AT THIS POINT, STREAM HAS HANDED THE CHUNK TO CONSUMER AND CALLED THIS _read METHOD AGAIN,
        // BEFORE WE COULD CLOSE THE STREAM.
        if (wasTheLastChunk) {
            // close the stream
            this.push(null)
        }
    }
}

With Web Streams API (and this adapter) this code could be sligthly rewritten

new ReadableStream({
    pull(controller) {
        // get the chunk
        var chunk = somehowGetTheChunk(controller.desiredSize)
        // add it to the stream's internal buffer
        controller.enqueue(chunk)
        // Native ReadableStream's asynchrony let's us finish all we do in this pull() before calling the next one.
        // So we can safely close the stream. However, since this adapter is based on node. The same problem occurs.
        if (wasTheLastChunk) {
            // close the stream
            controller.close()
        }
    }
})

but because this adapter is based on Node's Readable, the same problem occurs. To avoid any problems, the method should be rewritten to close the stream as the first thing.

new ReadableStream({
    pull(controller) {
        // Always ask if the resource has no more data to read from and end this stream if so.
        if (wasTheLastChunk) {
            // close the stream
            controller.close()
        }
        // Only then we can read the next available chunk
        var chunk = somehowGetTheChunk(controller.desiredSize)
        // add it to the stream's internal buffer
        controller.enqueue(chunk)
    }
})

Work in progress

So far only the ReadableStream's basic implementation's done.