Skip to main content

compress

Function compress 

Source
pub(crate) async fn compress(
    algorithms: CompressionAlgorithms,
    req: Request,
    next: Next,
) -> Response
Expand 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 窗口流式压缩,前段照旧尽早到达浏览器。