Skip to main content

Compression middleware for Starlette - supporting ZStd, Brotli, and GZip

Project description

starlette-compress

PyPI - Python Version

starlette-compress is a fast and simple middleware for compressing responses in Starlette. It supports more compression algorithms than Starlette's built-in GZipMiddleware, and has more sensible defaults.

  • Python 3.9+ support
  • Python 3.14 compression.zstd support
  • Compatible with asyncio and trio backends
  • ZStd, Brotli, and GZip compression
  • Sensible default configuration
  • Zero-Clause BSD — public domain dedication
  • Semantic Versioning compliance

Installation

pip install starlette-compress

Basic Usage

Starlette

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette_compress import CompressMiddleware

middleware = [
    Middleware(CompressMiddleware)
]

app = Starlette(routes=..., middleware=middleware)

FastAPI

You can use starlette-compress with FastAPI too:

from fastapi import FastAPI
from starlette_compress import CompressMiddleware

app = FastAPI()
app.add_middleware(CompressMiddleware)

Advanced Usage

Changing Minimum Response Size

Control the minimum size of the response to compress. By default, responses must be at least 500 bytes to be compressed.

# Starlette
middleware = [
    Middleware(CompressMiddleware, minimum_size=1000)
]

# FastAPI
app.add_middleware(CompressMiddleware, minimum_size=1000)

Tuning Compression Levels

Adjust the compression levels for each algorithm. Higher levels mean smaller files but slower compression. Default level is 4 for all algorithms.

# Starlette
middleware = [
    Middleware(CompressMiddleware, zstd_level=6, brotli_quality=6, gzip_level=6)
]

# FastAPI
app.add_middleware(CompressMiddleware, zstd_level=6, brotli_quality=6, gzip_level=6)

Supporting Custom Content-Types

Manage the supported content-types. Unknown response types are not compressed. Check here for the default configuration.

from starlette_compress import add_compress_type, remove_compress_type

add_compress_type("application/my-custom-type")
remove_compress_type("application/json")

Dropping Accept-Encoding Header

By default, starlette-compress leaves the request Accept-Encoding header intact. Set remove_accept_encoding=True to remove it before calling downstream middleware/application.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette_compress-1.7.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

starlette_compress-1.7.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file starlette_compress-1.7.0.tar.gz.

File metadata

  • Download URL: starlette_compress-1.7.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for starlette_compress-1.7.0.tar.gz
Algorithm Hash digest
SHA256 7ebfb0555109341f192a773c660e66737c82ca42e7c5ff1cb2a057af0aea3716
MD5 29adfb55819ed62324db25c4d6bba543
BLAKE2b-256 a46e204f5ee566b1e3a7461e14698c64facc85a00f3b0aceb67b2af02d16246f

See more details on using hashes here.

Provenance

The following attestation bundles were made for starlette_compress-1.7.0.tar.gz:

Publisher: ci.yaml on Zaczero/pkgs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file starlette_compress-1.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for starlette_compress-1.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ec811d9ca69cfa9d1cefc464223bd1e27e14b1f48a9091f4bd056304765fa6d0
MD5 c3212b78ba1886ca9e6454902043dd8e
BLAKE2b-256 9dde1334ae6a1c011f94a4cfddffdabef4ee3e17496f71f2c4bb8c6b92d089a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for starlette_compress-1.7.0-py3-none-any.whl:

Publisher: ci.yaml on Zaczero/pkgs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page