fix(compress): respect Accept-Encoding when encoding option is set (#4951)
* 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. Co-authored-by: Yusuke Wada <yusuke@kamawada.com> Co-authored-by: Taku Amano <taku@taaas.jp>
S
Shaurya Singh committed
f0ed2465913f2a89ebdf65cc54d6254915fc3ff6
Parent: a192df0
Committed by GitHub <noreply@github.com>
on 5/22/2026, 9:05:23 AM