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

fix(compress): don't compress server-sent events #3833

Merged
merged 1 commit into from
Jan 17, 2025

Conversation

dmitri-gb
Copy link
Contributor

Using both compress middleware and the streamSSE helper from hono/streaming doesn't work correctly -- the SSE messages are not streamed, instead they get buffered by the compression mechanism.

According to expressjs/compression it should be possible to compress server-sent events, if the underlying compression mechanism supports manual flushing. As far as I can tell though, CompressionStream (used by Hono's compress middleware) doesn't offer this functionality, so the best we can do is not compress SSE streams at all.

@EdamAme-x
Copy link
Contributor

Nice work
I think need to add tests about this

Copy link

codecov bot commented Jan 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.71%. Comparing base (2a68c59) to head (aee7f69).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3833   +/-   ##
=======================================
  Coverage   91.71%   91.71%           
=======================================
  Files         160      160           
  Lines       10195    10195           
  Branches     2925     2868   -57     
=======================================
  Hits         9350     9350           
  Misses        844      844           
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yusukebe
Copy link
Member

Hi @dmitri-gb

Thank you for the PR. I also realized that the problem and your idea are good! As @EdamAme-x mentioned, you need to add a test. I think it's a little unsuitable if you add it to src/helper/streaming/sse.test.tsx because this fix is about Compress Middleware. So it will be okay to test if it will not be compressed if the content type is text/event-stream in src/middleware/compress/index.test.ts.

Using both `compress` middleware and the `streamSSE` helper from `hono/streaming` doesn't work correctly -- the SSE messages are not streamed, instead they get buffered by the compression mechanism.

According to [expressjs/compression](https://github.com/expressjs/compression#server-sent-events), it should possible to compress server-sent events, if the underlying compression mechanism supports manual _flushing_. As far as I can tell though, `CompressionStream` (used by Hono's `compress` middleware) doesn't offer this functionality, so the best we can do is not compress SSE streams at all.
@dmitri-gb
Copy link
Contributor Author

Hi! I added a test in src/middleware/compress/index.test.ts that uses the streamSSE helper.

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

@dmitri-gb

Great! Thank you for your contribution.

@yusukebe yusukebe merged commit 49366df into honojs:main Jan 17, 2025
16 checks passed
@mfulton26
Copy link

related issues for tracking ability to control flushing on CompressionStream:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants