Skip to content

Commit

Permalink
fix(compress): don't compress server-sent events (#3833)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dmitri-gb authored Jan 17, 2025
1 parent 2a68c59 commit 49366df
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/middleware/compress/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stream } from '../../helper/streaming'
import { stream, streamSSE } from '../../helper/streaming'
import { Hono } from '../../hono'
import { compress } from '.'

Expand Down Expand Up @@ -65,6 +65,13 @@ describe('Compress Middleware', () => {
}
})
)
app.get('/sse', (c) =>
streamSSE(c, async (stream) => {
for (let i = 0; i < 1000; i++) {
await stream.writeSSE({ data: 'chunk' })
}
})
)
app.notFound((c) => c.text('Custom NotFound', 404))

const testCompression = async (
Expand Down Expand Up @@ -155,6 +162,11 @@ describe('Compress Middleware', () => {
const res = await testCompression('/already-compressed-stream', 'gzip', 'br')
expect((await res.arrayBuffer()).byteLength).toBe(60000)
})

it('should not compress server-sent events', async () => {
const res = await testCompression('/sse', 'gzip', null)
expect((await res.arrayBuffer()).byteLength).toBe(13000)
})
})

describe('Edge Cases', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
* Match for compressible content type.
*/
export const COMPRESSIBLE_CONTENT_TYPE_REGEX =
/^\s*(?:text\/[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i
/^\s*(?:text\/(?!event-stream(?:[;\s]|$))[^;\s]+|application\/(?:javascript|json|xml|xml-dtd|ecmascript|dart|postscript|rtf|tar|toml|vnd\.dart|vnd\.ms-fontobject|vnd\.ms-opentype|wasm|x-httpd-php|x-javascript|x-ns-proxy-autoconfig|x-sh|x-tar|x-virtualbox-hdd|x-virtualbox-ova|x-virtualbox-ovf|x-virtualbox-vbox|x-virtualbox-vdi|x-virtualbox-vhd|x-virtualbox-vmdk|x-www-form-urlencoded)|font\/(?:otf|ttf)|image\/(?:bmp|vnd\.adobe\.photoshop|vnd\.microsoft\.icon|vnd\.ms-dds|x-icon|x-ms-bmp)|message\/rfc822|model\/gltf-binary|x-shader\/x-fragment|x-shader\/x-vertex|[^;\s]+?\+(?:json|text|xml|yaml))(?:[;\s]|$)/i

0 comments on commit 49366df

Please sign in to comment.