Skip to content

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

License

Notifications You must be signed in to change notification settings

fj-auto/streamify

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published