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

stream-switcher

v1.1.1

Published

A utility to dynamically switch between active streams seamlessly.

Downloads

313

Readme

stream-switcher

stream-switcher is a utility function that provides a flexible mechanism to dynamically switch between active streams while maintaining seamless data flow. It allows you to designate one active stream at a time, forwarding data and handling connections without interference from previously connected streams. The utility also supports optional buffering when paused, preventing data loss during stream transitions.

Usage

Importing stream-switcher

import { createStreamSwitcher } from 'stream-switcher';

Basic Example

Initialize createStreamSwitcher with an initial stream, allowing data to flow through it:

import { PassThrough } from 'streamx';
import { createStreamSwitcher } from 'stream-switcher';

const initialStream = new PassThrough();
const switcher = createStreamSwitcher(initialStream);

// Capture data from the active stream
switcher.on('data', (chunk) => {
  console.log('Received:', chunk.toString());
});

// Write data to the initial stream
initialStream.write('Hello from the initial stream!');

Switching to a New Stream

Use the switch method to replace the active stream with a new one, seamlessly transferring data flow to the new stream:

const newStream = new PassThrough();
switcher.switch(newStream);

newStream.write('Hello from the new stream!');

Writing to the Active Stream

The switcher can also function as a writable stream, where data sent to switcher.write() will be forwarded to the current active stream:

switcher.write('Forwarded to active stream');

Handling Buffering When Paused

You can configure createStreamSwitcher to buffer data when no active stream is set:

const switcher = createStreamSwitcher(null, { bufferWhenPaused: true, maxBufferSize: 50 });

// Data written while paused will be buffered
switcher.write('Buffered data');

// Set an active stream to flush buffered data
const activeStream = new PassThrough();
switcher.switch(activeStream);

Features

  • Dynamic Stream Switching: Easily swap active streams without disrupting data flow from previous connections.
  • Bi-directional Data Forwarding: switcher relays data to and from the active stream seamlessly.
  • Configurable Buffering: Optionally buffer data when no active stream is set, with customizable buffer limits.
  • Automatic Listener Management: Only the active stream pushes data to switcher, ensuring data consistency and efficient resource management.

Special Behavior with null or undefined

Calling switcher.switch(null) or switcher.switch(undefined) will pause the stream and, if bufferWhenPaused is enabled, data written to the switcher will be buffered until a new active stream is set.

API

createStreamSwitcher(initialStream, config)

Creates a new Duplex instance output stream that facilitates stream switching.

  • initialStream (optional): The first stream to connect to the switcher.
  • config (optional): The configuration supplied to the output stream.
    • bufferWhenPaused (boolean, default: false): Whether to buffer data when paused.
    • maxBufferSize (number, default: 100): The maximum number of items to buffer when paused.

switcher.switch(newStream)

Changes the active stream to the provided newStream.

  • newStream: The new stream that becomes the active stream for switcher.

License

MIT License. See LICENSE file for full details.

For questions, issues, or contributions, feel free to reach out or create a pull request!