compression

class litestar.middleware.compression.CompressionFacade[source]

Bases: Protocol

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

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

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.

close() None[source]

Close the compression stream.

Returns:

None

write(body: bytes | bytearray, final: bool = False) None[source]

Write compressed bytes to the buffer.

Parameters:
  • body – The message body to process. Can be bytes or bytearray.

  • final – Indicates whether this is the last chunk of data. If True, the compressor may flush any remaining internal buffers.

Returns:

None

encoding: ClassVar[str]

The encoding of the compression.

class litestar.middleware.compression.CompressionMiddleware[source]

Bases: ASGIMiddleware

Compression Middleware Wrapper.

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

__init__(config: CompressionConfig) None[source]

Initialize CompressionMiddleware

Parameters:

config – An instance of CompressionConfig.

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

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.

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

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.

Returns:

None

scopes: tuple[ScopeType, ...] = (ScopeType.HTTP, ScopeType.ASGI)

Scope types this middleware should be applied to