Fix greedy regex
When processing an event right now, if a SSE event starts with a space or is just made up of space characters, the regex filtering the data: field will remove all of the spaces before it finds a non-space character.
I read the spec and says the space after the colon should be ignored, but all other characters until the newline.
Example streaming response from ChatGPT showing when this is an issue: when asking What is 10-3, it returns:
handleLLMNewToken { token: '' }
handleLLMNewToken { token: '10' }
handleLLMNewToken { token: ' -' }
handleLLMNewToken { token: ' ' }
handleLLMNewToken { token: '3' }
handleLLMNewToken { token: ' equals' }
handleLLMNewToken { token: ' ' }
handleLLMNewToken { token: '7' }
handleLLMNewToken { token: '.' }
handleLLMNewToken { token: '' }
Which will currently produce:
"10"
"-" // <- missing space
// <-missing event with just a space
"3"
"equals" // <- missing space
// <-missing event with just a space
"7"
"."
@davemurphysf The way you changed the regex, it will now only match the prefix "data: " with a space after the colon. When there is no space after the colon, e.g. "data:Hello", it won't parse the message correctly anymore. That is a big problem.
I think you want to use this regex: /data:?\s?/
@davemurphysf Also, beware that line 208 currently removes all trailing whitespace from the line, so if the data is just a space or ends with a space, you would still handle it incorrectly:
line = parts[i].trim();
For example, consider a token like " equals " with both a leading and trailing space.
It would show up in the response text as "data: equals " with two spaces after "data:" and another space at the end.
But it would be turned into "data: equals" by line 208.
I've fixed.please use https://www.npmjs.com/package/react-native-ssec
@litongjava Why create a new package and not make a pull request to this one so existing users can befenit?
because we need to fix it with on line build
On Tue, Jun 4, 2024 at 2:11 AM Emil Junker @.***> wrote:
@litongjava https://github.com/litongjava Why create a new package and not make a pull request to this one so existing users can befenit?
— Reply to this email directly, view it on GitHub https://github.com/binaryminds/react-native-sse/pull/47#issuecomment-2147378228, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHSKMPMN2N4DJ53C274X7ELZFWVGLAVCNFSM6AAAAABE36I4UCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBXGM3TQMRSHA . You are receiving this because you were mentioned.Message ID: @.***>