pub(crate) async fn compress(
algorithms: CompressionAlgorithms,
req: Request,
next: Next,
) -> ResponseExpand description
动态响应压缩中间件(axum::middleware::from_fn)。跳过规则同 tower-http 的 DefaultPredicate:
- 已带
Content-Encoding(public 下预压缩的.br、SSR 缓存的预压缩变体)或Content-Range的原样透传; - 跳过
image/*(image/svg+xml除外)、gRPC、text/event-stream与小于 32 字节的响应; - 客户端连 identity 都拒绝时回 406。
与 tower-http 不同的一步:长度未知的正文先缓冲,若上游一口气给完且不超过 BUFFER_LIMIT,
就按总长选 brotli 窗口(brotli_window),省掉默认 4 MiB 环形缓冲区的分配与清零。
上游暂时没有数据(流式 SSR 等后续分块)时与 tower-http 一样先 flush 已收到的部分,
此后按 4 MiB 窗口流式压缩,前段照旧尽早到达浏览器。