sse-parse 0.1.0

Incremental, linear-time parser for Server-Sent Events byte streams, with a sans-IO core and a runtime-agnostic Stream adapter
Documentation
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
{
 "schema": "sse-conformance-vectors/1",
 "reference": "WHATWG HTML Living Standard, Server-sent events",
 "encoding": {
  "chunks": "each chunk is {\"text\": s} (UTF-8 bytes of the JSON string) or {\"hex\": h} (raw bytes); the input stream is the concatenation, delivered one chunk per stream item in order",
  "generator.repeat_stream": "unit_text UTF-8-encoded, repeated count times, then delivered in chunk_bytes-sized chunks",
  "generator.long_line": "prefix + fill_char*fill_count + suffix, UTF-8-encoded, delivered in chunk_bytes-sized chunks",
  "expected.events": "ordered dispatched events; event defaults applied ('message' type); id is the last event ID string at dispatch; retry is the reconnection-time attachment (most recent retry value processed since the previous dispatched event, else null)",
  "expected.events.kind=repeat": "the given event repeated count times",
  "expected.events.kind=single_fill_event": "one event whose data is fill_char repeated fill_count times",
  "expected.retry_updates": "ordered reconnection-time updates at field-processing time (WHATWG semantics, independent of dispatch)",
  "expected.final_last_event_id": "last event ID string after EOF (commits happen at every blank line, including non-dispatched blocks)"
 },
 "vectors": [
  {
   "id": "v01-whatwg-stock-ticker",
   "purpose": "spec example: multi-line data joined with LF",
   "chunks": [
    {
     "text": "data: YHOO\ndata: +2\ndata: 10\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "YHOO\n+2\n10",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v02-whatwg-test-stream",
   "purpose": "spec example: comment block, id set to 1, id reset to empty, single leading space stripped",
   "chunks": [
    {
     "text": ": test stream\n\ndata: first event\nid: 1\n\ndata:second event\nid\n\ndata:  third event\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "first event",
      "id": "1",
      "retry": null
     },
     {
      "event": "message",
      "data": "second event",
      "id": "",
      "retry": null
     },
     {
      "event": "message",
      "data": " third event",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v03-whatwg-two-events",
   "purpose": "spec example: bare 'data' lines; empty-string data IS dispatched when buffer held a newline; unterminated last block discarded",
   "chunks": [
    {
     "text": "data\n\ndata\ndata\n\ndata:\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "",
      "id": "",
      "retry": null
     },
     {
      "event": "message",
      "data": "\n",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v04-whatwg-space-strip",
   "purpose": "spec example: 'data:test' and 'data: test' are identical",
   "chunks": [
    {
     "text": "data:test\n\ndata: test\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "test",
      "id": "",
      "retry": null
     },
     {
      "event": "message",
      "data": "test",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v05-event-type-and-empty-value",
   "purpose": "event field sets type; 'event:' with empty value falls back to message",
   "chunks": [
    {
     "text": "event: add\ndata: x\n\nevent:\ndata: y\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "add",
      "data": "x",
      "id": "",
      "retry": null
     },
     {
      "event": "message",
      "data": "y",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v06-event-type-resets-after-dispatch",
   "purpose": "type buffer cleared at dispatch; next block defaults to message",
   "chunks": [
    {
     "text": "event: add\ndata: x\n\ndata: y\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "add",
      "data": "x",
      "id": "",
      "retry": null
     },
     {
      "event": "message",
      "data": "y",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v07-comments-ignored",
   "purpose": "comment-only blocks dispatch nothing; ':' and ': text' both comments",
   "chunks": [
    {
     "text": ": ping\n\n:\n: another\n\n"
    }
   ],
   "expected": {
    "events": [],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v08-unknown-fields-ignored",
   "purpose": "unknown field names ignored; comparison is literal and case-sensitive",
   "chunks": [
    {
     "text": "foo: bar\ndata: keep\nData: nope\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "keep",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v09-colon-in-value",
   "purpose": "field/value split at FIRST colon only",
   "chunks": [
    {
     "text": "data: a:b: c\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "a:b: c",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v10-one-space-only-no-tab",
   "purpose": "exactly one leading space stripped; tab is not stripped",
   "chunks": [
    {
     "text": "data:  two\n\ndata:\tx\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": " two",
      "id": "",
      "retry": null
     },
     {
      "event": "message",
      "data": "\tx",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v11-data-nul-allowed",
   "purpose": "NUL is legal in data (only id rejects it)",
   "chunks": [
    {
     "text": "data: a\u0000b\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "a\u0000b",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v12-crlf-terminators",
   "purpose": "CRLF pairs are single terminators",
   "chunks": [
    {
     "text": "data: a\r\ndata: b\r\n\r\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "a\nb",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v13-cr-terminators-and-eof-cr-flush",
   "purpose": "lone CR terminates a line; a lone CR at EOF is a terminator (empty line -> dispatch), not pending data",
   "chunks": [
    {
     "text": "data: a\rdata: b\r\r"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "a\nb",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v14-mixed-terminators",
   "purpose": "CR, CRLF, LF mixed in one stream",
   "chunks": [
    {
     "text": "data: a\rdata: b\r\ndata: c\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "a\nb\nc",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v15-crlf-split-across-chunks",
   "purpose": "CR at end of one chunk + LF at start of next is ONE terminator (must not synthesize a blank line and dispatch early)",
   "chunks": [
    {
     "text": "data: a\r"
    },
    {
     "text": "\ndata: b\r"
    },
    {
     "text": "\n\r\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "a\nb",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v16-cr-then-non-lf-across-chunks",
   "purpose": "chunk-final CR followed by a non-LF chunk is a lone-CR terminator",
   "chunks": [
    {
     "text": "data: a\r"
    },
    {
     "text": "data: b\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "a\nb",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v17-field-split-across-chunks",
   "purpose": "field name and value arrive in pieces; the incremental parser must not reparse or corrupt",
   "chunks": [
    {
     "text": "da"
    },
    {
     "text": "ta: hel"
    },
    {
     "text": "lo\n"
    },
    {
     "text": "\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "hello",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v18-multibyte-char-split-across-chunks",
   "purpose": "UTF-8 sequence (C3 A9, e-acute) split across chunks decodes intact, no replacement char",
   "chunks": [
    {
     "hex": "646174613a20c3"
    },
    {
     "hex": "a90a0a"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "\u00e9",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v19-bom-stripped-at-start",
   "purpose": "EF BB BF at stream offset 0 stripped exactly once (incumbent 0.2.3 panics here: 1-byte slice into a 3-byte char)",
   "chunks": [
    {
     "hex": "efbbbf"
    },
    {
     "text": "data: hi\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "hi",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v20-bom-split-across-chunks",
   "purpose": "BOM delivered one byte per chunk is still the leading BOM",
   "chunks": [
    {
     "hex": "ef"
    },
    {
     "hex": "bb"
    },
    {
     "hex": "bf"
    },
    {
     "text": "data: hi\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "hi",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v21-double-bom-only-first-stripped",
   "purpose": "only ONE leading BOM stripped; second U+FEFF makes the field name '\\uFEFFdata' (unknown, ignored) so nothing dispatches",
   "chunks": [
    {
     "hex": "efbbbfefbbbf"
    },
    {
     "text": "data: hi\n\n"
    }
   ],
   "expected": {
    "events": [],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v22-bom-mid-stream-not-stripped",
   "purpose": "BOM bytes after stream start are content: '\\uFEFFdata' is an unknown field, second block dispatches nothing",
   "chunks": [
    {
     "text": "data: a\n\n"
    },
    {
     "hex": "efbbbf"
    },
    {
     "text": "data: b\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "a",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v23-bom-inside-value-preserved",
   "purpose": "U+FEFF inside a data value is content",
   "chunks": [
    {
     "text": "data: a\ufeffb\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "a\ufeffb",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v24-empty-data-no-dispatch",
   "purpose": "block with only an event field dispatches nothing; type buffer still resets",
   "chunks": [
    {
     "text": "event: ping\n\ndata: x\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "x",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v25-id-committed-without-dispatch",
   "purpose": "dispatch step 1 commits the id buffer BEFORE the empty-data early return; id buffer is never reset",
   "chunks": [
    {
     "text": "id: 42\n\ndata: hi\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "hi",
      "id": "42",
      "retry": null
     }
    ],
    "final_last_event_id": "42",
    "retry_updates": []
   }
  },
  {
   "id": "v26-id-only-stream",
   "purpose": "a stream of just 'id: 9' + blank line yields no events but the last event ID string is 9 (reconnection state)",
   "chunks": [
    {
     "text": "id: 9\n\n"
    }
   ],
   "expected": {
    "events": [],
    "final_last_event_id": "9",
    "retry_updates": []
   }
  },
  {
   "id": "v27-id-persists-across-events",
   "purpose": "events without an id field inherit the last seen id",
   "chunks": [
    {
     "text": "id: 7\ndata: a\n\ndata: b\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "a",
      "id": "7",
      "retry": null
     },
     {
      "event": "message",
      "data": "b",
      "id": "7",
      "retry": null
     }
    ],
    "final_last_event_id": "7",
    "retry_updates": []
   }
  },
  {
   "id": "v28-id-nul-rejected",
   "purpose": "an id value containing NUL is ignored wholesale; buffer keeps its prior value",
   "chunks": [
    {
     "text": "id: 1\ndata: a\n\nid: x\u0000y\ndata: b\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "a",
      "id": "1",
      "retry": null
     },
     {
      "event": "message",
      "data": "b",
      "id": "1",
      "retry": null
     }
    ],
    "final_last_event_id": "1",
    "retry_updates": []
   }
  },
  {
   "id": "v29-retry-valid-and-invalid",
   "purpose": "all-digits retry accepted; non-digit and trailing-space values ignored",
   "chunks": [
    {
     "text": "retry: 3000\ndata: a\n\nretry: 30x0\ndata: b\n\nretry: 12 \ndata: c\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "a",
      "id": "",
      "retry": 3000
     },
     {
      "event": "message",
      "data": "b",
      "id": "",
      "retry": null
     },
     {
      "event": "message",
      "data": "c",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": [
     3000
    ]
   }
  },
  {
   "id": "v30-retry-in-non-dispatched-block",
   "purpose": "reconnection time is set when the field is PROCESSED, not at dispatch; a retry-only block must not lose the value",
   "chunks": [
    {
     "text": "retry: 5000\n\ndata: x\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "x",
      "id": "",
      "retry": 5000
     }
    ],
    "final_last_event_id": "",
    "retry_updates": [
     5000
    ]
   }
  },
  {
   "id": "v31-retry-empty-value-ignored",
   "purpose": "pinned P2: empty retry value is ignored (no digits)",
   "chunks": [
    {
     "text": "retry\ndata: a\n\nretry: \ndata: b\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "a",
      "id": "",
      "retry": null
     },
     {
      "event": "message",
      "data": "b",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v32-eof-discards-unterminated-block",
   "purpose": "terminated lines but no final blank line: block discarded at EOF",
   "chunks": [
    {
     "text": "data: kept\n\ndata: lost\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "kept",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v33-eof-discards-partial-line",
   "purpose": "stream ends mid-line: partial line and its block discarded",
   "chunks": [
    {
     "text": "data: kept\n\ndata: hal"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "kept",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "v34-invalid-utf8-replacement",
   "purpose": "pinned P1: invalid byte 0xFF becomes U+FFFD, stream keeps parsing, no error and no panic",
   "chunks": [
    {
     "text": "data: a"
    },
    {
     "hex": "ff"
    },
    {
     "text": "b\n\n"
    }
   ],
   "expected": {
    "events": [
     {
      "event": "message",
      "data": "a\ufffdb",
      "id": "",
      "retry": null
     }
    ],
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "g01-many-events-one-byte-chunks",
   "purpose": "throughput shape: 2000 small events delivered a byte at a time; perf gate input (linear time required)",
   "generator": {
    "kind": "repeat_stream",
    "unit_text": "data: 0123456789ab\n\n",
    "count": 2000,
    "chunk_bytes": 1
   },
   "expected": {
    "events": {
     "kind": "repeat",
     "event": {
      "event": "message",
      "data": "0123456789ab",
      "id": "",
      "retry": null
     },
     "count": 2000
    },
    "final_last_event_id": "",
    "retry_updates": []
   }
  },
  {
   "id": "g02-single-huge-line-one-byte-chunks",
   "purpose": "the quadratic killer: one 64 KiB data line delivered a byte at a time; incumbent rescans the whole partial line per chunk",
   "generator": {
    "kind": "long_line",
    "prefix": "data: ",
    "fill_char": "a",
    "fill_count": 65536,
    "suffix": "\n\n",
    "chunk_bytes": 1
   },
   "expected": {
    "events": {
     "kind": "single_fill_event",
     "event": "message",
     "fill_char": "a",
     "fill_count": 65536,
     "id": "",
     "retry": null
    },
    "final_last_event_id": "",
    "retry_updates": []
   }
  }
 ]
}