Skip to main content

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

Project description

starlette-compress

PyPI - Python Version Liberapay Patrons GitHub Sponsors

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
  • Compatible with asyncio and trio backends
  • ZStd, Brotli, and GZip compression
  • Sensible default configuration
  • The Unlicense — 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")

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.2.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

starlette_compress-1.2.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: starlette_compress-1.2.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for starlette_compress-1.2.0.tar.gz
Algorithm Hash digest
SHA256 ae56fbcbc85117fdc60bee62552ab71c332688bb120423ea51a9b0f777649f87
MD5 f9a98d8116273ffd4cc350d93e17f612
BLAKE2b-256 8cd9ff36196cdf3c6ce73a02257fcb95a2b49b49e4bd8badbf68da001ac2ae83

See more details on using hashes here.

Provenance

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

Publisher: GitHub
  • Repository: Zaczero/starlette-compress
  • Workflow: workflow.yaml
Attestations:
  • Statement type: https://in-toto.io/Statement/v1
    • Predicate type: https://docs.pypi.org/attestations/publish/v1
    • Subject name: starlette_compress-1.2.0.tar.gz
    • Subject digest: ae56fbcbc85117fdc60bee62552ab71c332688bb120423ea51a9b0f777649f87
    • Transparency log index: 145594800
    • Transparency log integration time:

File details

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

File metadata

File hashes

Hashes for starlette_compress-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fb1ed90b48d9aa97494baeb65b0db3f1d66d82f175c83558f7e6a9f5c68ac6e1
MD5 6e8c29a67f6769ff82dec38a33207bfe
BLAKE2b-256 984bccbe2c65b55563fbac449bdbe28a82464f2b7ba97de3e08f29c05434d7e1

See more details on using hashes here.

Provenance

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

Publisher: GitHub
  • Repository: Zaczero/starlette-compress
  • Workflow: workflow.yaml
Attestations:
  • Statement type: https://in-toto.io/Statement/v1
    • Predicate type: https://docs.pypi.org/attestations/publish/v1
    • Subject name: starlette_compress-1.2.0-py3-none-any.whl
    • Subject digest: fb1ed90b48d9aa97494baeb65b0db3f1d66d82f175c83558f7e6a9f5c68ac6e1
    • Transparency log index: 145594802
    • Transparency log integration time:

Supported by

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