honojs / hono

Web framework built on Web Standards
https://hono.dev
MIT License
20.74k stars 606 forks source link

Support for brotli (`br`) compression in compress middleware #3543

Open spaceemotion opened 1 month ago

spaceemotion commented 1 month ago

What is the feature you are proposing?

We're trying to modernize our app with Hono, but we'd like to use brotli compression for responses, instead of just gzip. Ssome of our calls can be a couple megabytes compressed, and the ratio between gzip and brotli is quite different.

From what I can tell, the list of supported compression algorithms is hard-coded: https://github.com/honojs/hono/blob/f8664b03a66b8e3abf488be0117885137904559b/src/middleware/compress/index.ts#L9 https://github.com/honojs/hono/blob/f8664b03a66b8e3abf488be0117885137904559b/src/middleware/compress/index.ts#L23

The node zlib package supports brotli, which should make it possible to implement it without an additional third party dependency.

Also, if there is a way to use the text mode for brotli for when the content is JSON or other text-only stuff would be great.

EdamAme-x commented 1 month ago

I think it's not bad since edge runtime etc. also supports this feature. (but, it's not supportted on service-worker as browser's features) https://developers.cloudflare.com/workers/runtime-apis/nodejs/zlib/

exoego commented 1 month ago

Compress middleware relies on CompressionStream which does not support Brotli yet unfortunately.

Good news is that W3C TPAC is going to add Brotli to the standard.

https://github.com/whatwg/compression/issues/34#issuecomment-2380671788 To summarise the discussion at TPAC 2024, we are going to add Brotli to the standard, but Chrome still won't be able to ship it for a while. It appears that Safari is ready to ship and Firefox is also positive on shipping.

EdamAme-x commented 1 month ago

planning to ship! Looks good.

yusukebe commented 1 month ago

How about Zstandard? 😉

spaceemotion commented 1 month ago

Brotli coming to the standards is good news indeed! I am guessing our only workaround for now is to clone the middleware, replace the bits and pieces to fit our node runtime, and wait until we can use the 'native' way?

vuolter commented 1 month ago

In the meantime I made my own plugin, with blackjack and hookers! 🤖

https://github.com/vuolter/hono-compress