Skip to content
Myr

fix(compress): respect Accept-Encoding when encoding option is set (#4951)

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

* fix(compress): skip compression when client does not accept the configured encoding When the encoding option is set, the middleware previously compressed responses unconditionally, regardless of the Accept-Encoding header. This could send a response in an encoding the client cannot decode (for example, returning gzip when the client only advertised deflate, or when Accept-Encoding is absent). Honor the client's Accept-Encoding header even when an encoding is explicitly configured, falling back to no compression when the configured encoding is not accepted. * fix(compress): use parseAccept to honor q-values, wildcard, and case Address review from @yusukebe. The previous version used String.includes on the raw Accept-Encoding header, which treated x-gzip as gzip, ignored q=0 explicit refusals, and did not understand the wildcard. Switch to parseAccept (already used by accepts middleware) and pick the candidate encoding with the highest non-zero q value, falling back to the wildcard q if the candidate has no explicit entry. Add seven regression tests covering q=0 on the configured encoding, q=0 fallback to another candidate, wildcard \*, case-insensitive tokens, x-gzip non-matching, highest-q wins, and gzip-over-deflate tie-break. * fix(compress): short-circuit selectEncoding when q is 1 Per @usualoma: q values are almost always 1 in practice, so short-circuit and return immediately for the common path instead of continuing to scan the remaining candidates.

authored by Shaurya Singh · committed by GitHub · parent a192df0 · Browse files · 2 files+142 added−3 removed
This commit

State

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

Trailers

  • Co-authored-by: Yusuke Wada <yusuke@kamawada.com>
  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

2 files+142 added−3 removed

  • tests:+115 added−0 removed
  • other:+27 added−3 removed

2 files+142 added−3 removed

modified:src/middleware/compress/index.test.ts+115 added
264 unmodified lines
265 const decompressed = await decompressResponse(res)266 expect(decompressed).toBe('Custom NotFound')267 })268 })269270 describe('Encoding Option', () => {271 const buildApp = (encoding: 'gzip' | 'deflate') => {272 const app = new Hono()273 app.use('*', compress({ encoding }))274 app.get('/large', (c) => {275 c.header('Content-Type', 'text/plain')276 c.header('Content-Length', '1024')277 return c.text('a'.repeat(1024))278 })279 return app280 }281282 it('should compress when configured encoding is accepted by the client', async () => {283 const app = buildApp('gzip')284 const res = await app.request('/large', {285 headers: { 'Accept-Encoding': 'gzip' },286 })287 expect(res.headers.get('Content-Encoding')).toBe('gzip')288 })289290 it('should not compress when configured encoding is not in Accept-Encoding', async () => {291 const app = buildApp('gzip')292 const res = await app.request('/large', {293 headers: { 'Accept-Encoding': 'deflate' },294 })295 expect(res.headers.get('Content-Encoding')).toBeNull()296 })297298 it('should not compress when Accept-Encoding header is absent', async () => {299 const app = buildApp('gzip')300 const res = await app.request('/large')301 expect(res.headers.get('Content-Encoding')).toBeNull()302 })303 })304305 describe('Accept-Encoding parsing', () => {306 const buildAppDefault = () => {307 const app = new Hono()308 app.use('*', compress())309 app.get('/large', (c) => {310 c.header('Content-Type', 'text/plain')311 c.header('Content-Length', '1024')312 return c.text('a'.repeat(1024))313 })314 return app315 }316317 const buildAppWithEncoding = (encoding: 'gzip' | 'deflate') => {318 const app = new Hono()319 app.use('*', compress({ encoding }))320 app.get('/large', (c) => {321 c.header('Content-Type', 'text/plain')322 c.header('Content-Length', '1024')323 return c.text('a'.repeat(1024))324 })325 return app326 }327328 it('should not compress when configured encoding has q=0', async () => {329 const app = buildAppWithEncoding('gzip')330 const res = await app.request('/large', {331 headers: { 'Accept-Encoding': 'gzip;q=0' },332 })333 expect(res.headers.get('Content-Encoding')).toBeNull()334 })335336 it('should fall back to another encoding when the preferred one has q=0', async () => {337 const app = buildAppDefault()338 const res = await app.request('/large', {339 headers: { 'Accept-Encoding': 'gzip;q=0, deflate' },340 })341 expect(res.headers.get('Content-Encoding')).toBe('deflate')342 })343344 it('should compress when Accept-Encoding is the wildcard *', async () => {345 const app = buildAppWithEncoding('gzip')346 const res = await app.request('/large', {347 headers: { 'Accept-Encoding': '*' },348 })349 expect(res.headers.get('Content-Encoding')).toBe('gzip')350 })351352 it('should compress when Accept-Encoding token differs only in case', async () => {353 const app = buildAppWithEncoding('gzip')354 const res = await app.request('/large', {355 headers: { 'Accept-Encoding': 'GZIP' },356 })357 expect(res.headers.get('Content-Encoding')).toBe('gzip')358 })359360 it('should not treat x-gzip as gzip', async () => {361 const app = buildAppWithEncoding('gzip')362 const res = await app.request('/large', {363 headers: { 'Accept-Encoding': 'x-gzip' },364 })365 expect(res.headers.get('Content-Encoding')).toBeNull()366 })367368 it('should pick the encoding with the highest q value', async () => {369 const app = buildAppDefault()370 const res = await app.request('/large', {371 headers: { 'Accept-Encoding': 'gzip;q=0.5, deflate;q=0.9' },372 })373 expect(res.headers.get('Content-Encoding')).toBe('deflate')374 })375376 it('should prefer gzip over deflate when q values are equal', async () => {377 const app = buildAppDefault()378 const res = await app.request('/large', {379 headers: { 'Accept-Encoding': 'gzip;q=0.5, deflate;q=0.5' },380 })381 expect(res.headers.get('Content-Encoding')).toBe('gzip')382 })383 })384})385386async function decompressResponse(res: Response): Promise<string> {387 const decompressedStream = res.body!.pipeThrough(new DecompressionStream('gzip'))
3 unmodified lines
modified:src/middleware/compress/index.ts+27 added-3 removed