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

streamable-tools

v0.3.0

Published

`streamable-tools` is a JS/TS library for easy stream manipulation in web apps, offering utilities like `MultiplyStream` for data propagation, `SplitStream` for data splitting, and tools for stream creation and control.

Downloads

284

Readme

streamable-tools

streamable-tools is a JavaScript/TypeScript library providing a set of utilities designed to simplify working with Streams in web applications. It introduces convenient wrappers and functions to perform common operations on readable and writable streams, such as transforming, splitting, and managing stream data more efficiently. Below are some of the highlights of this library:

MultiplyStream

Allows you to propagate the data from a single ReadableStream to multiple WritableStream instances. This is useful when you need to send the same stream data to different destinations or processes.

import { MultiplyStream } from "streamable-tools/multiply-stream";

new ReadableStream().pipeThrough(
  new MultiplyStream(
    { writable: new WritableStream() },
    { writable: new WritableStream() },
    { writable: new WritableStream() },
  ),
);

SplitStream

Facilitates the splitting of stream data based on a delimiter, such as a newline character. It can be particularly handy when working with text streams that need to be read line by line or split into chunks.

import { SplitStream } from "streamable-tools/split-stream";

const readable = new ReadableStream({
  start: (controller) => {
    controller.enqueue(new TextEncoder().encode("foo\nbiz"));
    controller.enqueue(new TextEncoder().encode("\n"));
    controller.close();
  },
}).pipeThrough(new SplitStream());

const arr = await readableStreamToArray(readable);

expect(arr).toEqual([
  new TextEncoder().encode("foo"),
  new TextEncoder().encode("biz"),
]);

readableStreamWithController

Provides a simpler interface to create a ReadableStream along with its ReadableStreamDefaultController, allowing for direct manipulation of the stream's data flow. This helps in scenarios where dynamic stream content generation or modification is required.

import { readableStreamWithController } from "streamable-tools/readable-stream-with-controller";

const { readable, controller } = readableStreamWithController<Uint8Array>();

controller.enqueue(new TextEncoder().encode("f"));
controller.enqueue(new TextEncoder().encode("o"));
controller.enqueue(new TextEncoder().encode("o"));
controller.close();

const output = await readableStreamToText(readable);

expect(output).toEqual("foo");

iterableToReadableStream

Converts a JavaScript iterable object into a ReadableStream, allowing for the efficient streaming of iterable data as chunks through the Streams API. This is particularly useful for streaming large datasets or dynamic content that is generated on-the-fly, without having to allocate and hold the entire data in memory at once.

import { iterableToReadableStream } from "streamable-tools/iterable-to-readable-stream";

const iterable = function* () {
  yield "Hello ";
  yield "world!";
};

const readable = iterableToReadableStream(iterable());

const text = await readableStreamToText(readable);

expect(text).toEqual("Hello world!");

readableStreamWithPipeStream

import { readableStreamWithPipeStream } from "streamable-tools/readable-stream-with-pipe-stream";

const { readable, pipeStream } = readableStreamWithPipeStream();

await otherReadable.pipeThrough(pipeStream);

otherReadable; // => ReadableStream<string> ['foo', 'biz']
readable; // => ReadableStream<string> ['foo', 'biz']

readableStreamTransformChunk

import { readableStreamTransformChunk } from "streamable-tools/readable-stream-transforms";

const nextReadable = readableStreamTransformChunk(readable, toUint8Array);

readable; // => ReadableStream<string> ['foo', 'biz']
nextReadable; // => ReadableStream<Uint8Array> [ Uint8Array(3) [ 102, 111, 111 ], Uint8Array(3) [ 98, 105, 122 ] ]

readableStreamToIterable

import { readableStreamToIterable } from "streamable-tools/readable-stream-transforms";

const iterable = readableStreamToIterable(readable);

readable; // => ReadableStream<string> [ 'foo', 'biz' ]
iterable.next().value; // => 'foo'
iterable.next().value; // => 'biz'

readableStreamToArray

import { readableStreamToArray } from "streamable-tools/readable-stream-transforms";

const arr = readableStreamToArray(readable);

readable; // => ReadableStream<string> [ 'foo', 'biz' ]
arr; // => [ 'foo', 'biz' ]

readableStreamToText

import { readableStreamToText } from "streamable-tools/readable-stream-transforms";

const text = readableStreamToText(readable);

readable; // => ReadableStream<string> [ 'foo', 'biz' ]
text; // => 'foobiz'

readableStreamToJson

import { readableStreamToJson } from "streamable-tools/readable-stream-transforms";

const obj = readableStreamToJson(readable);

readable; // => ReadableStream<string> [ '{', '"n":1}' ]
obj; // => { n: 1 }

readableStreamToTextList

import { readableStreamToTextList } from "streamable-tools/readable-stream-transforms";

const list = readableStreamToTextList(readable);

readable; // => ReadableStream<string> [ 'foo\nbiz\n', 'taz' ]
list; // => AsyncIterable<string> [ 'foo', 'biz', 'taz' ]

readableStreamToJsonList

import { readableStreamToJsonList } from "streamable-tools/readable-stream-transforms";

const list = readableStreamToJsonList(readable);

readable; // => ReadableStream<string> [ '{"n":1}\n{"n":2}\n', '{"n":', '3}' ]
list; // => AsyncIterable<Object> [ { n: 1 }, { n: 2 }, { n: 3 } ]