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

streamx-webstream

v1.2.12

Published

fromWeb/toWeb for streamx

Downloads

55

Readme

streamx-webstream

A wrapper for streamx streams that provides toWeb/fromWeb functions.

Object mode not supported.

Installation

npm install streamx-webstream --save

Import

import { fromWeb, toWeb } from "streamx-webstream";
// or
import { fromWeb } from "streamx-webstream/from";
// or
import { toWeb } from "streamx-webstream/to";
// or CDN
import { fromWeb, toWeb } from "https://esm.run/streamx-webstream";

API

fromWeb

Description:

The fromWeb function wraps Web Streams (ReadableStream & WritableStream) and converts them into streamx streams. This is specifically designed for streamx and is not intended for native Node.js streams.

Syntax:

const streamx = fromWeb({ readable, writable });
// or
const streamx = fromWeb(readable);
// or
const streamx = fromWeb(writable);

Parameters:

  • readable (ReadableStream, optional): The readable stream to be wrapped.
  • writable (WritableStream, optional): The writable stream to be wrapped.

Returns:

  • (Streamx.Duplex | Streamx.Readable | Streamx.Writable): A streamx.Duplex stream if both readable and writable streams are provided. A streamx.Readable or streamx.Writable stream if only one is provided.

Example:

import { fromWeb } from "streamx-webstream";
import b4a from "b4a";

const readableWebStream = new ReadableStream({
    start(controller) {
        controller.enqueue(b4a.from("hello"));
        controller.enqueue(b4a.from("world"));
        controller.close();
    }
});

const writableWebStream = new WritableStream({
    write(chunk) {
        console.log('Received chunk:', b4a.toString(chunk));
    }
});

const duplexStreamx = fromWeb({ readable: readableWebStream, writable: writableWebStream });

duplexStreamx.on('data', (chunk) => {
    console.log('Read data:', b4a.toString(chunk));
});

duplexStreamx.write(b4a.from('hello from write'));
duplexStreamx.end();

toWeb

Description:

The toWeb function converts streamx streams into Web Streams (ReadableStream & WritableStream). This function is tailored for streamx streams and not for native Node.js streams.

Syntax:

const webStream = toWeb(stream, asBytes);
// or
const webStream = toWeb({ readable: streamxReadable, writable: streamxWritable }, asBytes);

Parameters:

  • stream (Streamx stream | Object): The streamx stream or an object containing readable, writable, or both to be converted into Web Streams.
  • asBytes (boolean, optional): Indicates whether the stream should be handled as byte streams. Default is false.

Returns:

  • (ReadableStream | WritableStream | Object with ReadableStream and/or WritableStream): Returns Web Streams corresponding to the provided streamx stream:
    • ReadableStream if the provided streamx stream is readable.
    • WritableStream if the provided streamx stream is writable.
    • Object containing both ReadableStream and WritableStream if both are provided.

Example:

import { toWeb } from "streamx-webstream";
import { Duplex } from "streamx";
import b4a from 'b4a';

const duplexStreamx = new Duplex({
  read(cb) {
    this.push(b4a.from('Streamx Duplex Readable Data'));
    this.push(null);
    cb();
  },
  write(chunk, cb) {
    console.log('Received duplex chunk:', b4a.toString(chunk));
    cb();
  }
});

const { readable, writable } = toWeb(duplexStreamx);
const reader = readable.getReader();
const writer = writable.getWriter();

reader.read().then(({ value, done }) => {
  if (!done) {
    console.log('Duplex Web Readable data:', b4a.toString(value));
  }
});

writer.write(b4a.from('Streamx Duplex Writable Data')).then(() => writer.close());

Detailed API Documentation

For more detailed information on the API, refer to the API Documentation.

License

Distributed under the MIT license. See LICENSE for more information.