mpetazzoni/sse.js

Event stream parsing fails when \n\n sequence is split among two network packets

Closed this issue · 1 comments

I opened the following pull request #35 with a new parser implementation that solves this bug.

The bug was found on the following (real) case:

1f50: 0a 0d 0a 35 32 0d 0a 69 64 3a 20 70 72 6f 67 72 ...52..id: progr
1f60: 65 73 73 0a 64 61 74 61 3a 20 7b 22 63 6f 75 6e ess.data: {"coun
1f70: 74 22 3a 33 33 2c 22 74 6f 74 61 6c 22 3a 31 33 t":33,"total":13
1f80: 31 2c 22 74 69 6d 65 73 74 61 6d 70 22 3a 31 36 1,"timestamp":16
1f90: 39 31 31 39 36 39 38 30 7d 0a 74 79 70 65 3a 20 91196980}.type:
1fa0: 6d 65 73 73 61 67 65 0a 0a 0d 0a 35 32 0d 0a 69 message....52..i
1fb0: 64 3a 20 70 72 6f 67 72 65 73 73 0a 64 61 74 61 d: progress.data
1fc0: 3a 20 7b 22 63 6f 75 6e 74 22 3a 33 34 2c 22 74 : {"count":34,"t
1fd0: 6f 74 61 6c 22 3a 31 33 31 2c 22 74 69 6d 65 73 otal":131,"times
1fe0: 74 61 6d 70 22 3a 31 36 39 31 31 39 36 39 38 30 tamp":1691196980
1ff0: 7d 0a 74 79 70 65 3a 20 6d 65 73 73 61 67 65 0a }.type: message.
== Info: TLSv1.2 (IN), TLS header, Supplemental data (23):
<= Recv SSL data, 5 bytes (0x5)
0000: 17 03 03 00 19                                  .....
<= Recv data, 1 bytes (0x1)
0000: 0a                                              .
== Info: TLSv1.2 (IN), TLS header, Supplemental data (23):
<= Recv SSL data, 5 bytes (0x5)
0000: 17 03 03 00 1a                                  .....
<= Recv data, 2 bytes (0x2)
0000: 0d 0a                                           ..
== Info: TLSv1.2 (IN), TLS header, Supplemental data (23):
<= Recv SSL data, 5 bytes (0x5)
0000: 17 03 03 20 18                                  ... .
<= Recv data, 8192 bytes (0x2000)
0000: 35 32 0d 0a 69 64 3a 20 70 72 6f 67 72 65 73 73 52..id: progress
0010: 0a 64 61 74 61 3a 20 7b 22 63 6f 75 6e 74 22 3a .data: {"count":
0020: 33 35 2c 22 74 6f 74 61 6c 22 3a 31 33 31 2c 22 35,"total":131,"
0030: 74 69 6d 65 73 74 61 6d 70 22 3a 31 36 39 31 31 timestamp":16911
0040: 39 36 39 38 30 7d 0a 74 79 70 65 3a 20 6d 65 73 96980}.type: mes
0050: 73 61 67 65 0a 0a 0d 0a 35 32 0d 0a 69 64 3a 20 sage....52..id:
0060: 70 72 6f 67 72 65 73 73 0a 64 61 74 61 3a 20 7b progress.data: {
0070: 22 63 6f 75 6e 74 22 3a 33 36 2c 22 74 6f 74 61 "count":36,"tota

As you can see, there's a single line break (\n) on an single packet.
This causes the parser to merge two adjacent events that should not be merged.

Merged #35. Thanks!