listpage-next 0.0.219 → 0.0.221
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.
|
@@ -4,3 +4,8 @@ export type Callbacks<Event = string, Data = any> = {
|
|
|
4
4
|
onMessage?: (type: Event, data: Data) => void;
|
|
5
5
|
};
|
|
6
6
|
export declare function parseSubjectStream<Event = string, Data = any>(stream: ReadableStream, callbacks: Callbacks<Event, Data>): Promise<void>;
|
|
7
|
+
export type SseMessage<Event = string, Data = unknown> = {
|
|
8
|
+
type: Event;
|
|
9
|
+
data: Data;
|
|
10
|
+
};
|
|
11
|
+
export declare function parseSseStream<Event = string, Data = unknown>(stream: ReadableStream): AsyncGenerator<SseMessage<Event, Data>, void, void>;
|
|
@@ -27,4 +27,40 @@ async function parseSubjectStream(stream, callbacks) {
|
|
|
27
27
|
reader.releaseLock();
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
-
|
|
30
|
+
async function* parseSseStream(stream) {
|
|
31
|
+
const reader = stream.getReader();
|
|
32
|
+
const decoder = new TextDecoder();
|
|
33
|
+
let buffer = '';
|
|
34
|
+
let type = '';
|
|
35
|
+
try {
|
|
36
|
+
while(true){
|
|
37
|
+
const { done, value } = await reader.read();
|
|
38
|
+
if (done) break;
|
|
39
|
+
buffer += decoder.decode(value, {
|
|
40
|
+
stream: true
|
|
41
|
+
});
|
|
42
|
+
let idx = buffer.indexOf('\n');
|
|
43
|
+
while(-1 !== idx){
|
|
44
|
+
const line = buffer.slice(0, idx).trim();
|
|
45
|
+
buffer = buffer.slice(idx + 1);
|
|
46
|
+
if (line) {
|
|
47
|
+
if (line.startsWith('event:')) type = line.slice(6).trim();
|
|
48
|
+
else if (line.startsWith('data:')) {
|
|
49
|
+
const raw = line.slice(5).trim();
|
|
50
|
+
try {
|
|
51
|
+
const data = JSON.parse(raw);
|
|
52
|
+
yield {
|
|
53
|
+
type: type,
|
|
54
|
+
data
|
|
55
|
+
};
|
|
56
|
+
} catch {}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
idx = buffer.indexOf('\n');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
} finally{
|
|
63
|
+
reader.releaseLock();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
export { parseSseStream, parseSubjectStream };
|