compression

class litestar.middleware.compression.CompressionFacade

Bases: Protocol

A unified facade offering a uniform interface for different compression libraries.

encoding: ClassVar[str]

The encoding of the compression.

__init__(buffer: BytesIO, compression_encoding: CompressionEncoding | str, config: CompressionConfig) None

Initialize CompressionFacade.

Parameters:
  • buffer – A bytes IO buffer to write the compressed data into.

  • compression_encoding – The compression encoding used.

  • config – The app compression config.

write(body: bytes) None

Write compressed bytes.

Parameters:

body – Message body to process

Returns:

None

close() None

Close the compression stream.

Returns:

None

class litestar.middleware.compression.CompressionMiddleware

Bases: ASGIMiddleware

Compression Middleware Wrapper.

This is a wrapper allowing for generic compression configuration / handler middleware

__init__(config: CompressionConfig) None

Initialize CompressionMiddleware

Parameters:

config – An instance of CompressionConfig.

async handle(scope: Scope, receive: Receive, send: Send, next_app: ASGIApp) None

Handle ASGI call.

Parameters:
  • scope – The ASGI connection scope.

  • receive – The ASGI receive function.

  • send – The ASGI send function

  • next_app – The next ASGI application in the middleware stack to call

create_compression_send_wrapper(send: Send, compression_encoding: Literal[CompressionEncoding.BROTLI, CompressionEncoding.GZIP] | str, scope: Scope) Send

Wrap send to handle brotli compression.

Parameters:
  • send – The ASGI send function.

  • compression_encoding – The compression encoding used.

  • scope – The ASGI connection scope

Returns:

An ASGI send function.