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

smooth-stream

v1.0.6

Published

A library for streaming text responses smoothly with different strategies.

Downloads

256

Readme

Smooth Stream

A library for streaming text responses smoothly with different strategies.

Installation

npm install smooth-stream

Usage

The Smooth Stream library allows you to stream text content in a smooth, controlled manner. It supports different streaming strategies to cater to various needs. Below are examples in both JavaScript and TypeScript to demonstrate how to use the library.

JavaScript

Importing and initializing the smooth stream

const {
  SmoothStreamer,
  StreamingMode,
  getStreamingStrategy,
} = require('smooth-stream');

// Create a SmoothStream instance with a 100ms delay
// between characters
// and prefix matching enabled
const streamer = new SmoothStreamer(
  100, // 100ms delay between updates
  getStreamingStrategy(StreamingMode.CHARACTER),
  true // prefix matching
);

Subscribe to the stream events

// Subscribe to the streamer to handle
// responses, errors, and completion
streamer.subscribe(
  // callback for any new response
  (response) => console.log(response),
  // callback for any error
  (err) => console.error(err),
  // callback for the end of the stream
  () => console.log('Stream ended')
);

// Add a callback for when the entire stream ends
// This executes after streamer.subscribe completes
streamer.onStreamEnd(() => {
  console.log('Streaming ended.');
});

Stream Text

// Start streaming text character by character
streamer.next('Hello'); // Stream "Hello"

// Continue streaming the extended text
streamer.next('Hello, this is a test response.', () => {
  // This code runs after the text from this call is fully streamed
  // and runs before any next stream calls
  console.log('text has been fully streamed.');

  // Flush the stream to reset it and clear pending updates
  streamer.flush();

  // Add new text to the stream after flushing
  streamer.next('New stream started.');
});

streamer.next('This will not be streamed because it got flushed');

TypeScript

The Smooth Stream library has native support for TypeScript, allowing you to take advantage of type checking and autocompletion benefits in TypeScript projects. The usage in TypeScript is identical to the JavaScript example above, with the addition of type annotations.

Streaming Strategies

The library supports three streaming strategies, each suited for different types of text streaming needs:

  • Character: Streams the response one character at a time, providing a smooth, letter-by-letter reveal of the text.
  • Word: Streams the response one word at a time, skipping HTML tags and ensuring a natural word-by-word display.
  • Whole: Streams the entire response at once, delivering the full text immediately without gradual reveal.

What is Prefix Matching?

Prefix match means that when two consecutive next() calls are made, the stream will reset to the longest common prefix of the two strings. For example:

streamer.next('Hello, world');
streamer.next('Hello, everyone');

In this case, after the second call to next(), the stream will reset to "Hello, " which is the longest common prefix of "Hello, world" and "Hello, everyone". The streaming will then continue from "Hello, " to "Hello, everyone".

By choosing the appropriate streaming strategy and utilizing the prefix match feature, you can tailor the text streaming behavior to match the desired user experience.