Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove encodeURIComponent #64

Closed
aubergene opened this issue Jan 7, 2025 · 4 comments
Closed

Remove encodeURIComponent #64

aubergene opened this issue Jan 7, 2025 · 4 comments
Assignees
Labels
seen I've seen and read this issue and I'll try find some time soon to work on it.

Comments

@aubergene
Copy link

Currently produce splits on new line and then runs encodeURIComponent on each item, which I think could be removed. You stated in #63

The spec itself doesn't say anything about uri encoding, but without uri encoding messages would split into chunks on new lines.

but this is already happening

emit('message', the time is ${Date.now()}\nhere's a new line);

id: 1
event: message
data: the%20time%20is%201736267516899
data: here's%20a%20new%20line

I'm proposing the output should look like below, so newlines produce another data output, which I think meets the spec

id: 1
event: message
data: the time is 1736267516899
data: here's a new line

I'm looking to use this to replace an existing SSE endpoint where we're outputting JSON on a single line so the URI encoding breaks our existing consumers

Big thanks for this library it's great otherwise. Happy to write a PR but wanted to check thoughts first in a issue

@razshare razshare self-assigned this Jan 7, 2025
@razshare razshare added the seen I've seen and read this issue and I'll try find some time soon to work on it. label Jan 7, 2025
@razshare
Copy link
Owner

razshare commented Jan 7, 2025

Hello @aubergene , thanks for the issue.

I believe you're correct.
Reading this I just realized that both id and event are included with each emit call, obviously, which means there should be no ambiguity to which event a data payload is related to.

controller.enqueue(encoder.encode(`id: ${id}\nevent: ${eventName}\n`))
const chunks = data.split('\n')
for (const chunk of chunks) {
controller.enqueue(
encoder.encode(`data: ${encodeURIComponent(chunk)}\n`),
)
}
controller.enqueue(encoder.encode('\n'))
id++
return ok()

That was my main concern initially, but since JS is single threaded there shouldn't be any risk for a different data payload that belongs to a different event to be injected between those splits, unless we're dealing with workers. But that's a matter for another day to solve.

I'll run tests without the uri encoding/decoding and make sure it still behaves the same.

@razshare
Copy link
Owner

razshare commented Jan 7, 2025

but this is already happening

Also yes, it's splitting the string then encoding the chunks, which makes no sense.

@razshare
Copy link
Owner

razshare commented Jan 7, 2025

I'll run tests without the uri encoding/decoding and make sure it still behaves the same.

Everything looks fine without the uri encoding/decoding, I've just release version 0.13.12.
Try update to it and let me know if this fixes your issue.

@aubergene
Copy link
Author

Thank you! That's great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
seen I've seen and read this issue and I'll try find some time soon to work on it.
Projects
None yet
Development

No branches or pull requests

2 participants