From 49366df8a925cd8e886ac5b3c317a0f6b0bd3970 Mon Sep 17 00:00:00 2001 From: Dmitri Date: Fri, 17 Jan 2025 12:16:33 +0200 Subject: [PATCH] fix(compress): don't compress server-sent events (#3833) 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. --- src/middleware/compress/index.test.ts | 14 +++++++++++++- src/utils/compress.ts | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/middleware/compress/index.test.ts b/src/middleware/compress/index.test.ts index d446b451d..df8fa7748 100644 --- a/src/middleware/compress/index.test.ts +++ b/src/middleware/compress/index.test.ts @@ -1,4 +1,4 @@ -import { stream } from '../../helper/streaming' +import { stream, streamSSE } from '../../helper/streaming' import { Hono } from '../../hono' import { compress } from '.' @@ -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 ( @@ -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', () => { diff --git a/src/utils/compress.ts b/src/utils/compress.ts index a0a6cd7b5..93a660976 100644 --- a/src/utils/compress.ts +++ b/src/utils/compress.ts @@ -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