Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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.

authored by Dmitri · committed by GitHub · parent 2a68c59 · Browse files · 2 files+14 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+14 added−2 removed

  • tests:+13 added−1 removed
  • other:+1 added−1 removed

2 files+14 added−2 removed

modified:src/middleware/compress/index.test.ts+13 added-1 removed
modified:src/utils/compress.ts+1 added-1 removed
5 unmodified lines
6/**7 * Match for compressible content type.8 */9export 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]|$)/i10 /^\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