Open History to see the last commit
1import { expect, test } from 'vitest'
2import fc from 'fast-check'
3import { encodeSsePage } from '../../src/sse.js'
4β
5const control = { streamNextOffset: 'checkpoint', streamClosed: true }
6function verify(bytes: Buffer, binary: boolean) {
7 const chunks = [...encodeSsePage(bytes, true, binary, control)]
8 const data = binary ? bytes.toString('base64') : bytes.toString('utf8').replace(/\r\n|\r/g, '\n').replace(/\n/g, '\ndata:')
9 expect(Buffer.concat(chunks).toString()).toBe(`event: data\ndata:${data}\n\nevent: control\ndata:${JSON.stringify(control)}\n\n`)
10 expect(Math.max(...chunks.map((chunk) => chunk.byteLength))).toBeLessThanOrEqual(65536)
11}
12test('SSE fragments preserve arbitrary binary and invalid UTF-8 payloads', () => {
13 fc.assert(fc.property(fc.uint8Array({ maxLength: 180000 }), (value) => { verify(Buffer.from(value), false); verify(Buffer.from(value), true) }), { numRuns: 100 })
14})
15test('SSE handles CRLF, BOM and Unicode across fragment boundaries', () => {
16 for (const prefix of [8190, 8191, 8192, 8193, 49151, 49152]) {
17 for (const tail of ['\r\n\r\n', 'π\r\n\n', '\r\r\n', 'Γ©\n', '\ufeffx\n']) verify(Buffer.from(`${'x'.repeat(prefix)}${tail}${'π'.repeat(10000)}`), false)
18 }
19 verify(Buffer.from('\ufeff[9007199254740993,1e400,-0,"π"]\r\n'), false)
20 expect(Buffer.concat([...encodeSsePage(Buffer.alloc(0), false, true, control)]).toString()).toBe(`event: control\ndata:${JSON.stringify(control)}\n\n`)
21})