File size: 2,432 Bytes
82be208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { parseSseJsonStream } from '$lib/utils/sse';
import { describe, expect, it } from 'vitest';

function makeSseResponse(events: string[]): Response {
	const body = events.join('\n\n') + '\n\n';

	return new Response(body, {
		headers: { 'content-type': 'text/event-stream' },
		status: 200
	});
}

describe('parseSseJsonStream', () => {
	it('yields parsed data for each record', async () => {
		const response = makeSseResponse(['data: {"chunk": "a"}', 'data: {"chunk": "b"}']);
		const collected: unknown[] = [];

		for await (const ev of parseSseJsonStream(response)) {
			collected.push(ev.data);
		}
		expect(collected).toEqual([{ chunk: 'a' }, { chunk: 'b' }]);
	});

	it('stops on [DONE] sentinel', async () => {
		const response = makeSseResponse([
			'data: {"chunk": "a"}',
			'data: [DONE]',
			'data: {"chunk": "after-done"}'
		]);
		const collected: unknown[] = [];

		for await (const ev of parseSseJsonStream(response)) {
			collected.push(ev.data);
		}
		expect(collected).toEqual([{ chunk: 'a' }]);
	});

	it('skips malformed JSON records', async () => {
		const response = makeSseResponse([
			'data: {"chunk": "ok"}',
			'data: {not-json}',
			'data: {"chunk": "also-ok"}'
		]);
		const collected: unknown[] = [];

		for await (const ev of parseSseJsonStream(response)) {
			collected.push(ev.data);
		}
		expect(collected).toEqual([{ chunk: 'ok' }, { chunk: 'also-ok' }]);
	});

	it('handles records split across multiple chunks (partial last line)', async () => {
		const full = 'data: {"chunk": "x"}\n\ndata: {"chunk": "y"}\n\n';
		const stream = new ReadableStream<Uint8Array>({
			start(controller) {
				const enc = new TextEncoder();

				controller.enqueue(enc.encode(full.slice(0, full.length / 2)));
				controller.enqueue(enc.encode(full.slice(full.length / 2)));
				controller.close();
			}
		});
		const response = new Response(stream, {
			headers: { 'content-type': 'text/event-stream' },
			status: 200
		});
		const collected: unknown[] = [];

		for await (const ev of parseSseJsonStream(response)) {
			collected.push(ev.data);
		}
		expect(collected).toEqual([{ chunk: 'x' }, { chunk: 'y' }]);
	});

	it('returns immediately if response has no body', async () => {
		const response = new Response(null, { status: 200 });
		const collected: unknown[] = [];

		for await (const ev of parseSseJsonStream(response)) {
			collected.push(ev.data);
		}
		expect(collected).toEqual([]);
	});
});