Enhance message parsing to preserve newlines from empty data fields #104
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Preserve newlines from empty data fields in Server-Sent Events parsing
Issue:#50
Description
Problem
The current implementation of the Server-Sent Events (SSE) parser incorrectly handles empty data fields. When multiple consecutive empty
data:
fields are encountered, the parser was not preserving the newline characters that should be added for each empty field, causing data loss and incorrect message formatting.According to the Server-Sent Events specification, each
data:
field should append its value to the data buffer followed by a single U+000A LINE FEED (LF) character. This means that emptydata:
fields should still contribute a newline character to the final message data.Solution
Modified the
getMessages
function insrc/parse.ts
to properly handle empty data fields by ensuring that eachdata:
field (including empty ones) contributes a newline character to the message data buffer.Key changes:
\n
) after each data field value, regardless of whether the value is emptysrc/parse.spec.ts
Example
Before (incorrect behavior):
Would result in:
data: "foo"
(missing newlines from empty data fields)After (correct behavior):
Results in:
data: "\n\nfoo"
(preserving newlines from empty data fields)Testing
should preserve newlines from empty data fields
to verify the fixjasmine.json
to point to the correct spec directoryImpact
This fix ensures full compliance with the Server-Sent Events specification and prevents data loss when processing SSE streams that contain empty data fields, which is a common pattern for formatting multi-line messages or creating visual spacing in SSE data.
Additional Information for the PR
Type of Change
Testing
Checklist