|
1 | | -# tsdown-starter |
| 1 | +# @wyatex/event-source-parse |
2 | 2 |
|
3 | | -A starter for creating a TypeScript package. |
| 3 | +[简体中文](./README.zh-CN.md) |
4 | 4 |
|
5 | | -## Development |
| 5 | +[](https://www.npmjs.com/package/@wyatex/event-source-parse) |
| 6 | +[](https://github.com/wyatex/event-source-parse/blob/main/LICENSE) |
| 7 | +[](https://github.com/wyatex/event-source-parse/actions) |
| 8 | +[](https://codecov.io/gh/wyatex/event-source-parse) |
6 | 9 |
|
7 | | -- Install dependencies: |
| 10 | +A robust, zero-dependency parser for **Server-Sent Events (SSE)** streams. |
| 11 | + |
| 12 | +This library is designed to consume `ReadableStream` or `AsyncIterable` and parse them into event messages. It is written in TypeScript and optimized for modern runtimes like Node.js, Bun, Deno, and Browsers. |
| 13 | + |
| 14 | +### ✨ Key Features |
| 15 | + |
| 16 | +* **Universal Support**: Works with standard Web API `ReadableStream` and Node.js streams. |
| 17 | +* **🧩 Azure OpenAI Compatible**: Includes a specific flush mechanism to handle streams that don't end with a newline (common in Azure OpenAI / LangChain scenarios), ensuring no data is lost. |
| 18 | +* **TypeScript**: Fully typed with TS sources included. |
| 19 | +* **Lightweight**: No runtime dependencies. |
| 20 | + |
| 21 | +## 📦 Installation |
8 | 22 |
|
9 | 23 | ```bash |
10 | | -npm install |
| 24 | +# npm |
| 25 | +npm install @wyatex/event-source-parse |
| 26 | + |
| 27 | +# bun |
| 28 | +bun add @wyatex/event-source-parse |
| 29 | + |
| 30 | +# pnpm |
| 31 | +pnpm add @wyatex/event-source-parse |
| 32 | + |
| 33 | +# yarn |
| 34 | +yarn add @wyatex/event-source-parse |
11 | 35 | ``` |
12 | 36 |
|
13 | | -- Run the unit tests: |
| 37 | +## 🚀 Usage |
14 | 38 |
|
15 | | -```bash |
16 | | -npm run test |
| 39 | +### 1. High-Level Usage (Recommended) |
| 40 | + |
| 41 | +The easiest way to consume a stream is using the helper function `convertEventStreamToIterableReadableDataStream`. This converts a raw SSE stream directly into an async iterable of data strings. |
| 42 | + |
| 43 | +```typescript |
| 44 | +import { convertEventStreamToIterableReadableDataStream } from '@wyatex/event-source-parse' |
| 45 | + |
| 46 | +async function consumeStream() { |
| 47 | + const response = await fetch('https://api.openai.com/v1/chat/completions', { |
| 48 | + method: 'POST', |
| 49 | + body: JSON.stringify({ stream: true, /* ... */ }), |
| 50 | + }) |
| 51 | + |
| 52 | + if (!response.body) throw new Error('No body') |
| 53 | + |
| 54 | + // Convert the raw stream into an iterable of data strings |
| 55 | + const stream = convertEventStreamToIterableReadableDataStream(response.body) |
| 56 | + |
| 57 | + for await (const chunk of stream) { |
| 58 | + console.log('Received chunk:', chunk) |
| 59 | + } |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +### 2. Low-Level Control (Pipeline) |
| 64 | + |
| 65 | +If you need full control over the parsing process (e.g., accessing `event` ID, `retry` time, or custom event types), you can compose the parser functions manually. |
| 66 | + |
| 67 | +```typescript |
| 68 | +import { getBytes, getLines, getMessages } from '@wyatex/event-source-parse' |
| 69 | + |
| 70 | +async function parseCustomStream(stream: ReadableStream) { |
| 71 | + // 1. Create a message handler |
| 72 | + const onMessage = (msg) => { |
| 73 | + console.log('Event:', msg.event) |
| 74 | + console.log('Data:', msg.data) |
| 75 | + console.log('ID:', msg.id) |
| 76 | + } |
| 77 | + |
| 78 | + // 2. Create the pipeline |
| 79 | + // getMessages -> processes lines into EventSourceMessage objects |
| 80 | + // getLines -> processes raw bytes into lines |
| 81 | + const processLine = getMessages(onMessage) |
| 82 | + const processChunk = getLines(processLine) |
| 83 | + |
| 84 | + // 3. Start reading bytes from the stream |
| 85 | + await getBytes(stream, processChunk) |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### 3. Handling Metadata Events |
| 90 | + |
| 91 | +The high-level helpers allow you to hook into specific events like `metadata` without disrupting the main data flow. |
| 92 | + |
| 93 | +```typescript |
| 94 | +const stream = convertEventStreamToIterableReadableDataStream( |
| 95 | + response.body, |
| 96 | + (metadata) => { |
| 97 | + console.log('Received metadata:', metadata) |
| 98 | + } |
| 99 | +) |
17 | 100 | ``` |
18 | 101 |
|
19 | | -- Build the library: |
| 102 | +## 🛠️ Development |
| 103 | + |
| 104 | +This project uses **Bun** for development. |
20 | 105 |
|
21 | 106 | ```bash |
22 | | -npm run build |
| 107 | +# Install dependencies |
| 108 | +bun install |
| 109 | + |
| 110 | +# Run tests |
| 111 | +bun run test |
| 112 | + |
| 113 | +# Run tests with coverage |
| 114 | +bun run test:coverage |
| 115 | + |
| 116 | +# Lint code |
| 117 | +bun run lint |
| 118 | + |
| 119 | +# Build library |
| 120 | +bun run build |
23 | 121 | ``` |
| 122 | + |
| 123 | +## 📄 License |
| 124 | + |
| 125 | +MIT |
0 commit comments