|
| 1 | +import { createEventSourceParserStream } from './event-source-parser-stream'; |
| 2 | +import { convertReadableStreamToArray } from './test'; |
| 3 | + |
| 4 | +describe('EventSourceParserStream', () => { |
| 5 | + async function parseStream(inputChunks: string[]) { |
| 6 | + const stream = createEventSourceParserStream(); |
| 7 | + const writer = stream.writable.getWriter(); |
| 8 | + |
| 9 | + for (const chunk of inputChunks) { |
| 10 | + writer.write(chunk); |
| 11 | + } |
| 12 | + writer.close(); |
| 13 | + |
| 14 | + return convertReadableStreamToArray(stream.readable); |
| 15 | + } |
| 16 | + |
| 17 | + it('should parse simple data events', async () => { |
| 18 | + expect(await parseStream(['data: hello\n\n'])).toEqual([{ data: 'hello' }]); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should parse events with types', async () => { |
| 22 | + expect(await parseStream(['event: message\ndata: hello\n\n'])).toEqual([ |
| 23 | + { event: 'message', data: 'hello' }, |
| 24 | + ]); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should handle multiple events in one chunk', async () => { |
| 28 | + expect(await parseStream(['data: one\n\ndata: two\n\n'])).toEqual([ |
| 29 | + { data: 'one' }, |
| 30 | + { data: 'two' }, |
| 31 | + ]); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should handle events split across chunks', async () => { |
| 35 | + expect(await parseStream(['data: hel', 'lo\n\n'])).toEqual([ |
| 36 | + { data: 'hello' }, |
| 37 | + ]); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should handle events and data split across chunks', async () => { |
| 41 | + expect(await parseStream(['event: mess', 'age\ndata: hello\n\n'])).toEqual([ |
| 42 | + { event: 'message', data: 'hello' }, |
| 43 | + ]); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should handle CRLF line endings', async () => { |
| 47 | + expect(await parseStream(['data: hello\r\n\r\n'])).toEqual([ |
| 48 | + { data: 'hello' }, |
| 49 | + ]); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should handle empty data lines', async () => { |
| 53 | + expect(await parseStream(['data: \n', 'data: hello\n\n'])).toEqual([ |
| 54 | + { data: '\nhello' }, |
| 55 | + ]); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should carry event type to next data line', async () => { |
| 59 | + expect( |
| 60 | + await parseStream([ |
| 61 | + 'event: message\ndata: one\n\nevent: alert\ndata: two\n\n', |
| 62 | + ]), |
| 63 | + ).toEqual([ |
| 64 | + { event: 'message', data: 'one' }, |
| 65 | + { event: 'alert', data: 'two' }, |
| 66 | + ]); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should reset event type after emitting data', async () => { |
| 70 | + expect( |
| 71 | + await parseStream(['event: message\ndata: hello\n\ndata: world\n\n']), |
| 72 | + ).toEqual([{ event: 'message', data: 'hello' }, { data: 'world' }]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should handle incomplete events at the end of stream', async () => { |
| 76 | + expect(await parseStream(['data: hello\n\ndata:'])).toEqual([ |
| 77 | + { data: 'hello' }, |
| 78 | + { data: '' }, |
| 79 | + ]); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should handle multi-line data', async () => { |
| 83 | + expect( |
| 84 | + await parseStream(['data: line1\ndata: line2\ndata: line3\n\n']), |
| 85 | + ).toEqual([{ data: 'line1\nline2\nline3' }]); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should handle id field', async () => { |
| 89 | + expect(await parseStream(['id: 123\ndata: hello\n\n'])).toEqual([ |
| 90 | + { data: 'hello', id: '123' }, |
| 91 | + ]); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should persist id across events', async () => { |
| 95 | + expect( |
| 96 | + await parseStream(['id: 123\ndata: first\n\ndata: second\n\n']), |
| 97 | + ).toEqual([ |
| 98 | + { data: 'first', id: '123' }, |
| 99 | + { data: 'second', id: '123' }, |
| 100 | + ]); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should update id when a new one is received', async () => { |
| 104 | + expect( |
| 105 | + await parseStream(['id: 123\ndata: first\n\nid: 456\ndata: second\n\n']), |
| 106 | + ).toEqual([ |
| 107 | + { data: 'first', id: '123' }, |
| 108 | + { data: 'second', id: '456' }, |
| 109 | + ]); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should handle retry field', async () => { |
| 113 | + expect(await parseStream(['retry: 5000\ndata: hello\n\n'])).toEqual([ |
| 114 | + { data: 'hello', retry: 5000 }, |
| 115 | + ]); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should ignore invalid retry values', async () => { |
| 119 | + expect(await parseStream(['retry: invalid\ndata: hello\n\n'])).toEqual([ |
| 120 | + { data: 'hello' }, |
| 121 | + ]); |
| 122 | + }); |
| 123 | + |
| 124 | + it('should ignore comment lines', async () => { |
| 125 | + expect(await parseStream([': this is a comment\ndata: hello\n\n'])).toEqual( |
| 126 | + [{ data: 'hello' }], |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should handle fields with no value', async () => { |
| 131 | + expect(await parseStream(['event\ndata: hello\n\n'])).toEqual([ |
| 132 | + { event: '', data: 'hello' }, |
| 133 | + ]); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should ignore unrecognized fields with no colon', async () => { |
| 137 | + expect(await parseStream(['eventmessage\ndata: hello\n\n'])).toEqual([ |
| 138 | + { data: 'hello' }, |
| 139 | + ]); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should handle multiple blank lines between events', async () => { |
| 143 | + expect(await parseStream(['data: first\n\n\n\ndata: second\n\n'])).toEqual([ |
| 144 | + { data: 'first' }, |
| 145 | + { data: 'second' }, |
| 146 | + ]); |
| 147 | + }); |
| 148 | + |
| 149 | + it('should emit event at end of stream even without final newline', async () => { |
| 150 | + expect(await parseStream(['data: hello'])).toEqual([{ data: 'hello' }]); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should correctly handle a complete event source stream example', async () => { |
| 154 | + expect( |
| 155 | + await parseStream([ |
| 156 | + 'event: update\n', |
| 157 | + 'id: 1\n', |
| 158 | + 'data: {"message": "First update"}\n', |
| 159 | + '\n', |
| 160 | + ': this is a comment\n', |
| 161 | + 'event: alert\n', |
| 162 | + 'id: 2\n', |
| 163 | + 'retry: 10000\n', |
| 164 | + 'data: line1\n', |
| 165 | + 'data: line2\n', |
| 166 | + '\n', |
| 167 | + 'data: standalone message\n', |
| 168 | + '\n', |
| 169 | + ]), |
| 170 | + ).toEqual([ |
| 171 | + { |
| 172 | + event: 'update', |
| 173 | + data: '{"message": "First update"}', |
| 174 | + id: '1', |
| 175 | + }, |
| 176 | + { |
| 177 | + event: 'alert', |
| 178 | + data: 'line1\nline2', |
| 179 | + id: '2', |
| 180 | + retry: 10000, |
| 181 | + }, |
| 182 | + { |
| 183 | + data: 'standalone message', |
| 184 | + id: '2', |
| 185 | + }, |
| 186 | + ]); |
| 187 | + }); |
| 188 | +}); |
0 commit comments