hyperlane-plugin-compression 23.3.0

A full-duplex opt-in compression middleware plugin for the Hyperlane framework providing bounded request decompression (gzip/deflate/br) and automatic Accept-Encoding response negotiation with MIME filtering and Vary merging.
Documentation
use super::*;

/// Configuration for [`CompressionPlugin`].
#[derive(Clone, Data, Debug, Eq, PartialEq)]
pub struct CompressionConfig {
    /// Whether inbound compressed requests (`gzip`, `deflate`, `br`) are automatically decompressed.
    #[get(type(copy))]
    pub(crate) decompress_requests: bool,
    /// Whether unknown inbound `Content-Encoding` headers return `415 Unsupported Media Type`.
    #[get(type(copy))]
    pub(crate) reject_unknown_encoding: bool,
    /// Upper bound in bytes on decompressed inbound request payloads (`413` when exceeded).
    #[get(type(copy))]
    pub(crate) max_decompressed_bytes: usize,
    /// Whether outgoing responses are automatically negotiated via `Accept-Encoding`.
    #[get(type(copy))]
    pub(crate) compress_responses: bool,
    /// Minimum outgoing body length eligible for compression.
    #[get(type(copy))]
    pub(crate) min_body_bytes: usize,
    /// Enable Brotli (`br`) response compression.
    #[get(type(copy))]
    pub(crate) enable_brotli: bool,
    /// Enable Gzip (`gzip`) response compression.
    #[get(type(copy))]
    pub(crate) enable_gzip: bool,
    /// Enable Deflate (`deflate`) response compression.
    #[get(type(copy))]
    pub(crate) enable_deflate: bool,
    /// MIME type prefixes skipped for response compression.
    pub(crate) skip_mime_prefixes: Vec<String>,
}

/// Precompiled compression engine backing [`CompressionRequestHook`] and [`CompressionResponseHook`].
#[derive(Clone, Debug)]
pub struct CompressionPlugin {
    /// Retained configuration.
    pub(crate) config: CompressionConfig,
    /// Lowercased excluded MIME prefixes.
    pub(crate) skip_mime_lower: Box<[String]>,
    /// Prebuilt `415 Unsupported Media Type` wire response.
    pub(crate) unsupported_wire: Vec<u8>,
    /// Prebuilt `413 Payload Too Large` wire response.
    pub(crate) limit_wire: Vec<u8>,
}

/// Opt-in request middleware performing bounded decompression of compressed request bodies.
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::request_middleware(-104)]
pub struct CompressionRequestHook;

/// Opt-in response middleware negotiating `Content-Encoding` and merging `Vary: Accept-Encoding`.
#[derive(Clone, Copy, Debug, Default)]
#[hyperlane::response_middleware(-55)]
pub struct CompressionResponseHook;