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

web-stream-util

v0.3.7

Published

Utilities for working with web streams.

Downloads

20

Readme

Web-stream-util

Utilities for working with web streams, including utilities for light-weight stream transforms modeled on the Reader interface.

With ReadableStream now available in ServiceWorkers, streaming transformation & composition of HTML has become an attractive option for server-side and client-side low-latency page composition. To this end, this project provides utilities for working with streams and Reader objects.

A Reader as defined in the stream spec has mainly two methods:

  • read(): Promise<ResultObject>, with ResultObject an object with a done boolean & value mixed property. If done is false, value is undefined. Once done is true, value is undefined & the stream is exhausted.
  • cancel(reason): Cancel the stream.

These are the main methods, but there are also releaseLock() and a closed getter.

Reader transforms

This library defines some utilities for working with transforms modeled on this Reader interface. One such transform for HTML is web-html-stream, a streaming HTML element match & transform exposing a Reader interface. Its HTMLTransformReader constructor is passed an input ReadableStream, Reader, or an Array that can be adapted to a stream:

return fetch('http://example.com')
.then(response => {
    // Transform the incoming HTML
    const htmlReader = new HTMLTransformReader(response.body, transformOptions);
    // Convert Reader to a ReadableStream using Web-stream-util's toStream()
    return webStreamUtil.toStream(htmlReader);
});

Since each transform implements a Reader interface & takes a Reader as input, it is fairly easy to build up transform chains. See this demo ServiceWorker for an example of more interesting transform chains.

API

toReader(input: Reader|ReadableStream|Array|String|Buffer): Reader

Adapts a variety of input types to a Reader.

toStream(input: toReader-able): ReadableStream

Adapts a variety of Reader-convertible types to a ReadableStream. The ReadableStream constructor is expected to be available in the global environment.

readToArray(input: toReader-able): Promise<Array>

Drains anything that can be adapted to a Reader to an Array.

readToString(input: toReader-able): Promise<String>

Drains anything that can be adapted to a Reader to a string. Converts Buffer or ArrayBuffers to String using a TextDecoder instance.

new TextDecodeReader(input: toReader-able): Reader<String>

Decodes a stream of Buffer or ArrayBuffers to a string stream, using a TextDecoder.

new TextEncodeReader(input: toReader-able): Reader<Buffer|ArrayBuffer>

Encodes a stream of Strings into a stream of Buffers (node) or ArrayBuffers (browser).

new FlatStreamReader(input: toReader-able, ctx): Reader

Flattens / evaluates a stream of complex objects into plain values like Strings and Buffers. Evaluation rules:

  • function f: Call f(ctx), evaluate result.
  • Promise: Wait for Promise to resolve, evaluate result.
  • Reader: Splice in / read sub-reader, and evaluate each returned value.
  • ReadableStream: Splice in / read sub-stream, and evaluate each returned value.
  • String, Buffer, Number etc: Pass through.

In a typical streaming composition pipeline, this transform is placed at the end of the pipeline, producing a single, flattened stream of values. An especially interesting use is the pre-compilation of templates into arrays of strings and functions, which can then be efficiently evaluated with FlatStreamReader at runtime.

See also

  • web-html-stream: Streaming HTML transformation, targeting elements with CSS selectors.
  • streaming-serviceworker-playground, a demo ServiceWorker using this library & web-html-stream for streaming HTML composition.
  • node-serviceworker and node-serviceworker-proxy, a server-side (node) ServiceWorker execution environment and -proxy with streaming support, executing ServiceWorker code on behalf of clients without ServiceWorker support.
  • https://github.com/whatwg/streams/issues/461: Light weight TransformStream discussion in the stream spec project.