@kognitivedev/cloud-web-search 0.2.28
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/CHANGELOG.md +13 -0
- package/README.md +186 -0
- package/dist/client.d.ts +33 -0
- package/dist/client.js +516 -0
- package/dist/errors.d.ts +9 -0
- package/dist/errors.js +20 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +13 -0
- package/dist/rich-events.d.ts +2 -0
- package/dist/rich-events.js +122 -0
- package/dist/sse.d.ts +8 -0
- package/dist/sse.js +151 -0
- package/dist/types.d.ts +310 -0
- package/dist/types.js +2 -0
- package/dist/validation.d.ts +39 -0
- package/dist/validation.js +437 -0
- package/package.json +39 -0
- package/src/__tests__/client.test.ts +784 -0
- package/src/__tests__/sse.test.ts +50 -0
- package/src/client.ts +664 -0
- package/src/errors.ts +20 -0
- package/src/index.ts +33 -0
- package/src/rich-events.ts +256 -0
- package/src/sse.ts +173 -0
- package/src/types.ts +362 -0
- package/src/validation.ts +575 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +8 -0
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { readSSEStream } from "../index";
|
|
3
|
+
|
|
4
|
+
function toChunkedStream(chunks: string[]) {
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
return new ReadableStream<Uint8Array>({
|
|
7
|
+
start(controller) {
|
|
8
|
+
for (const chunk of chunks) {
|
|
9
|
+
controller.enqueue(encoder.encode(chunk));
|
|
10
|
+
}
|
|
11
|
+
controller.close();
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function collect<T>(iterable: AsyncIterable<T>): Promise<T[]> {
|
|
17
|
+
const values: T[] = [];
|
|
18
|
+
for await (const value of iterable) {
|
|
19
|
+
values.push(value);
|
|
20
|
+
}
|
|
21
|
+
return values;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
describe("@kognitivedev/cloud-web-search SSE parser", () => {
|
|
25
|
+
it("parses chunked SSE frames, comments, multi-line data, ids, retries, and EOF flushes", async () => {
|
|
26
|
+
const stream = toChunkedStream([
|
|
27
|
+
": keepalive\nid: evt_1\nretry: 1500\neve",
|
|
28
|
+
"nt: custom\ndata: first line\ndata: second line\n\n",
|
|
29
|
+
"event: message\ndata: {\"ok\":true}",
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
const events = await collect(readSSEStream(stream));
|
|
33
|
+
|
|
34
|
+
expect(events).toEqual([
|
|
35
|
+
{
|
|
36
|
+
event: "custom",
|
|
37
|
+
data: "first line\nsecond line",
|
|
38
|
+
dataText: "first line\nsecond line",
|
|
39
|
+
id: "evt_1",
|
|
40
|
+
retry: 1500,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
event: "message",
|
|
44
|
+
data: { ok: true },
|
|
45
|
+
dataText: "{\"ok\":true}",
|
|
46
|
+
id: "evt_1",
|
|
47
|
+
},
|
|
48
|
+
]);
|
|
49
|
+
});
|
|
50
|
+
});
|