sse-line-parser 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -50
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
# SSE Stream Parser
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A **lightweight, pure, production-ready SSE (Server-Sent Events) stream parser**, implemented based on the standard SSE protocol. It is independent of specific application scenarios and can be widely used in various SSE data stream parsing tasks. It does not involve request management or connection interruption, focusing solely on doing one thing: **parsing streams cleanly**.
|
|
4
4
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
-
## ✨
|
|
7
|
+
## ✨ Features
|
|
8
8
|
|
|
9
|
-
- ✅
|
|
10
|
-
- ✅
|
|
11
|
-
- ✅
|
|
12
|
-
- ✅
|
|
13
|
-
- ✅
|
|
14
|
-
- ✅ TypeScript
|
|
9
|
+
- ✅ **Pure SSE stream parsing**, no concern for request lifecycle
|
|
10
|
+
- ✅ Supports standard `data:` protocol format, compatible with all SSE-based services
|
|
11
|
+
- ✅ Built-in `[DONE]` early identification and quick skipping
|
|
12
|
+
- ✅ Low GC pressure, avoiding unnecessary object creation
|
|
13
|
+
- ✅ Runs on **Node.js ≥ 18** (native `fetch` + `ReadableStream`)
|
|
14
|
+
- ✅ Native TypeScript support with clear types
|
|
15
15
|
|
|
16
16
|
---
|
|
17
17
|
|
|
18
|
-
## 📦
|
|
18
|
+
## 📦 Installation
|
|
19
19
|
|
|
20
20
|
```bash
|
|
21
21
|
pnpm add sse-stream-parser
|
|
@@ -27,29 +27,29 @@ yarn add sse-stream-parser
|
|
|
27
27
|
|
|
28
28
|
---
|
|
29
29
|
|
|
30
|
-
## 🧠
|
|
30
|
+
## 🧠 Design Philosophy
|
|
31
31
|
|
|
32
|
-
>
|
|
32
|
+
> **Parse streams only, no control logic**
|
|
33
33
|
|
|
34
|
-
|
|
34
|
+
This plugin will **NOT**:
|
|
35
35
|
|
|
36
|
-
- ❌
|
|
37
|
-
- ❌
|
|
38
|
-
- ❌
|
|
39
|
-
- ❌
|
|
36
|
+
- ❌ Manage request interruption/abort
|
|
37
|
+
- ❌ Wrap fetch
|
|
38
|
+
- ❌ Maintain connection status
|
|
39
|
+
- ❌ Introduce EventEmitter/Rx/class abstractions
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
This plugin is **only responsible for**:
|
|
42
42
|
|
|
43
|
-
- ✔
|
|
44
|
-
- ✔
|
|
45
|
-
- ✔
|
|
46
|
-
- ✔
|
|
43
|
+
- ✔ Parse `ReadableStreamDefaultReader<Uint8Array<ArrayBuffer>>`
|
|
44
|
+
- ✔ Split SSE lines
|
|
45
|
+
- ✔ Parse `data:` content
|
|
46
|
+
- ✔ Identify `[DONE]`
|
|
47
47
|
|
|
48
48
|
---
|
|
49
49
|
|
|
50
|
-
## 🚀
|
|
50
|
+
## 🚀 Basic Usage
|
|
51
51
|
|
|
52
|
-
### 1️⃣
|
|
52
|
+
### 1️⃣ Basic Example (Node.js / Edge)
|
|
53
53
|
|
|
54
54
|
```ts
|
|
55
55
|
import { parseSSEStream } from "sse-stream-parser";
|
|
@@ -64,7 +64,7 @@ await parseSSEStream({
|
|
|
64
64
|
renderStream: reader,
|
|
65
65
|
options: {
|
|
66
66
|
onMessage(data) {
|
|
67
|
-
// data
|
|
67
|
+
// data is the parsed SSE message
|
|
68
68
|
console.log(data);
|
|
69
69
|
},
|
|
70
70
|
onDone() {
|
|
@@ -79,56 +79,56 @@ await parseSSEStream({
|
|
|
79
79
|
|
|
80
80
|
---
|
|
81
81
|
|
|
82
|
-
## 🔍 `[DONE]`
|
|
82
|
+
## 🔍 `[DONE]` Processing Logic
|
|
83
83
|
|
|
84
|
-
|
|
84
|
+
Plugin internally optimizes for the following case:
|
|
85
85
|
|
|
86
86
|
```txt
|
|
87
87
|
data: [DONE]
|
|
88
88
|
```
|
|
89
89
|
|
|
90
|
-
-
|
|
91
|
-
-
|
|
92
|
-
-
|
|
93
|
-
-
|
|
90
|
+
- Early identification of `[DONE]`
|
|
91
|
+
- **Skip JSON.parse**
|
|
92
|
+
- Immediately trigger `onDone`
|
|
93
|
+
- Subsequent data is skipped directly
|
|
94
94
|
|
|
95
|
-
|
|
95
|
+
Avoid meaningless parsing and exception catching.
|
|
96
96
|
|
|
97
97
|
---
|
|
98
98
|
|
|
99
|
-
## ⚙️ API
|
|
99
|
+
## ⚙️ API Documentation
|
|
100
100
|
|
|
101
101
|
### `parseSSEStream(options)`
|
|
102
102
|
|
|
103
|
-
####
|
|
103
|
+
#### Parameters
|
|
104
104
|
|
|
105
|
-
|
|
|
106
|
-
| ------------------- | ----------------------------------------- |
|
|
107
|
-
| `renderStream` | `ReadableStreamDefaultReader<Uint8Array>` | SSE
|
|
108
|
-
| `options` | `StreamOptions` |
|
|
109
|
-
| `options.onMessage` | `(data: T) => void` |
|
|
110
|
-
| `options.onDone` | `() => void` |
|
|
111
|
-
| `options.onError` | `(err: Error) => void` |
|
|
105
|
+
| Parameter | Type | Description |
|
|
106
|
+
| ------------------- | ----------------------------------------- | ---------------------------------------------- |
|
|
107
|
+
| `renderStream` | `ReadableStreamDefaultReader<Uint8Array>` | Reader for SSE response body |
|
|
108
|
+
| `options` | `StreamOptions` | Options object containing callbacks |
|
|
109
|
+
| `options.onMessage` | `(data: T) => void` | Callback for each message |
|
|
110
|
+
| `options.onDone` | `() => void` | Triggered when `[DONE]` is received (optional) |
|
|
111
|
+
| `options.onError` | `(err: Error) => void` | Parsing error callback (optional) |
|
|
112
112
|
|
|
113
113
|
---
|
|
114
114
|
|
|
115
|
-
## 🌍
|
|
115
|
+
## 🌍 Runtime Environment
|
|
116
116
|
|
|
117
117
|
- Node.js **>= 18**
|
|
118
118
|
- Bun / Deno / Edge Runtime
|
|
119
|
-
-
|
|
119
|
+
- Browser (requires `ReadableStream` support)
|
|
120
120
|
|
|
121
121
|
---
|
|
122
122
|
|
|
123
|
-
## 🧱
|
|
123
|
+
## 🧱 Use Cases
|
|
124
124
|
|
|
125
|
-
-
|
|
126
|
-
-
|
|
127
|
-
-
|
|
128
|
-
-
|
|
129
|
-
-
|
|
130
|
-
-
|
|
131
|
-
-
|
|
125
|
+
- SSE for AI services like OpenAI/Claude/Gemini
|
|
126
|
+
- Real-time data push services
|
|
127
|
+
- Real-time updates for stock quotes, weather data, etc.
|
|
128
|
+
- Real-time log stream monitoring
|
|
129
|
+
- Custom SSE services
|
|
130
|
+
- Streaming consumption on Web/Node/Edge
|
|
131
|
+
- Infrastructure / SDK / Middleware layers
|
|
132
132
|
|
|
133
133
|
---
|
|
134
134
|
|