Giter Club home page Giter Club logo

streamify's Introduction

http-streamify

Extracted from openai-node

http-streamify is a library for creating and handling streams, especially for Server-Sent Events (SSE) and newline-delimited JSON streams.

http-streamify 是一个用于处理 Server-Sent Events (SSE) 和可读流的 JavaScript 库。该库适用于在浏览器和 Node.js 环境中使用。

Installation

You can install the package using npm:

导入 http-streamify

npm install http-streamify

Usage

Creating a Stream from SSE Response

The fromSSEResponse method creates a Stream object from an SSE response.

从 SSE 响应创建 Stream 的示例

const response = await fetch('/sse-endpoint');
const controller = new AbortController();
const stream = Stream.fromSSEResponse(response, controller);

for await (const data of stream) {
  console.log(data); // { event: 'message', data: 'Hello, world!' }
}

Creating a Stream from Readable Stream

The fromReadableStream method creates a Stream object from a readable stream, where each item is a newline-delimited JSON value.

从可读流创建 Stream 的示例

// const readableStream = new ReadableStream(/* ... */);
const readableStream = fetch('/readable-stream-endpoint');
const controller = new AbortController();
const stream = Stream.fromReadableStream(readableStream, controller);

for await (const data of stream) {
  console.log(data); // { event: 'message', data: 'Hello, world!' }
}

License

MIT

streamify's People

Contributors

fj-auto avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.