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.
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