Skip to content

Commit aee7f69

Browse files
committed
fix(compress): don't compress server-sent events
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.
1 parent 2a68c59 commit aee7f69

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/middleware/compress/index.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { stream } from '../../helper/streaming'
1+
import { stream, streamSSE } from '../../helper/streaming'
22
import { Hono } from '../../hono'
33
import { compress } from '.'
44

@@ -65,6 +65,13 @@ describe('Compress Middleware', () => {
6565
}
6666
})
6767
)
68+
app.get('/sse', (c) =>
69+
streamSSE(c, async (stream) => {
70+
for (let i = 0; i < 1000; i++) {
71+
await stream.writeSSE({ data: 'chunk' })
72+
}
73+
})
74+
)
6875
app.notFound((c) => c.text('Custom NotFound', 404))
6976

7077
const testCompression = async (
@@ -155,6 +162,11 @@ describe('Compress Middleware', () => {
155162
const res = await testCompression('/already-compressed-stream', 'gzip', 'br')
156163
expect((await res.arrayBuffer()).byteLength).toBe(60000)
157164
})
165+
166+
it('should not compress server-sent events', async () => {
167+
const res = await testCompression('/sse', 'gzip', null)
168+
expect((await res.arrayBuffer()).byteLength).toBe(13000)
169+
})
158170
})
159171

160172
describe('Edge Cases', () => {

src/utils/compress.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
* Match for compressible content type.
88
*/
99
export const COMPRESSIBLE_CONTENT_TYPE_REGEX =
10-
/^\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
10+
/^\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 commit comments

Comments
 (0)