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

sse-generator

v0.0.8

Published

A simple server-sent events (SSE) request library for browser, for streaming LLM APIs

Downloads

10

Readme

SSE Generator

sse-generator is a lightweight Server-Sent Events (SSE) request library designed to provide an easy way for asynchronously invoking realtime data streams on the client side.

中文文档

for await (const { data, id, lastId } of sse({ ... })) {
  console.log(data, id, lastId);
}

Background: Recently, various online LLM services have widely adopted the SSE format as the response to streaming API requests, and the handling of SSE requests on the frontend has become increasingly common. To simplify the development process, we developed sse-generator, a lightweight SSE request library that can help you quickly implement the handling of SSE requests.

Special thanks to sse.js, sse-generator is developed based on it, extending its functionality to meet the needs of modern frontend development in SSE streaming invocation.

Features

  • Zero Dependencies: Maintaining a lightweight characteristic, easy to integrate and deploy.
  • Ease of Use: Simplified handling of streaming invocation allowing you to focus more on data processing than on the communication mechanism.
  • Highly Configurable: Provides a wealth of configuration options to adapt to different invocation scenarios.

How to Use

Direct inclusion in the HTML page

You can directly include the UMD version of sse-generator in your HTML page:

Note that the sse function will be mounted on the global object ssegenerator.

<script src="https://cdn.jsdelivr.net/npm/sse-generator/dist/umd/index.js"></script>
<script>
  window.ssegenerator.sse({ ... })
</script>

Using module import

You can also import sse-generator using module import:

npm install sse-generator
import { sse } from 'sse-generator';

// Use the sse function
async function test() {
  try {
    for await (const { data } of sse({
      baseURL: 'https://api.openai.com',
      url: '/v1/chat/completions',
      data: {
        model: 'gpt-4-turbo-preview',
        messages: [{ role: 'user', content: 'Hi!' }],
        headers: {
          Authorization: 'Bearer YOUR_API_KEY',
        },
      },
    })) {
      console.log(JSON.parse(data).choices[0].delta.content);
    }
  } catch (error) {
    console.error(error);
  }
}

Otherwise, you can use the getXMLHTTPRequest parameter to get the XMLHTTPRequest object to implement more custom features, such as terminating a request:

import { sse } from 'sse-generator';

async function test(getTerminate) {
  for await (const { data } of sse({
    getXMLHTTPRequest: xhr => {
      getTerminate(() => xhr.abort());
    },
  })) {
    console.log(data);
  }
}

// Terminate the request after 3 seconds
test(terminate => setTimeout(terminate, 3000));

sse by default only listens to the default message event. Server-Sent Event also supports custom events, as an example of the Claude API:

event: message_start
data: {"type": "message_start", "message": {"id": "msg_1nZdL29xx5MUA1yADyHTEsnR8uuvGzszyY", "type": "message", "role": "assistant", "content": [], "model": "claude-3-opus-20240229", "stop_reason": null, "stop_sequence": null, "usage": {"input_tokens": 25, "output_tokens": 1}}}

event: content_block_start
data: {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}}

event: ping
data: {"type": "ping"}

event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "Hello"}}

event: content_block_delta
data: {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "!"}}

event: content_block_stop
data: {"type": "content_block_stop", "index": 0}

event: message_delta
data: {"type": "message_delta", "delta": {"stop_reason": "end_turn", "stop_sequence":null, "usage":{"output_tokens": 15}}}

event: message_stop
data: {"type": "message_stop"}

Therefore, we need to specify additional events to listen to:

for await (const { event, data } of sse({ listen: ['content_block_delta', ...], ... })) { ... }

event will indicate the name of the current event.

API Interface

The table below lists the main interface parameters, parsing, and usage of the sse function.

| Parameter | Type | Description | | ------------------- | ------------------- | -------------------------------------------------------------------- | | baseURL | string | Base URL, all requests will be based on this URL | | url | string | Request URL, if baseURL exists, this URL is relative | | data | any | Request data, non-string will be JSON serialized | | headers | object | Custom request headers | | method | string | Request method, defaults to GET, data not null changes to POST | | withCredentials | boolean | Whether cross-origin requests should include credentials | | debug | boolean | Whether to enable debug mode, printing information to the console | | getXMLHTTPRequest | function | Called after connection, used to get XMLHTTPRequest object | | listen | string[] / string | List of events to listen to |

Generator Payload Type Description

The table below lists the payload types of the sse function generator and their corresponding parsing.

| Payload Type | Type | Description | | ------------ | -------- | ----------------------------------------------- | | data | string | Unparsed message content, usually a JSON string | | id | string | Event ID (if present) | | lastId | string | Previous event ID (if present) | | event | string | Event name |

Development

Install Dependencies

npm install

Build

npm run build

Publish

npm publish

Other commands:

npm run lint         # Lint and fix source files
npm run change       # Add a new changeset
npm run bump         # Update version and changelog via changeset
npm run release      # Release the package