Skip to main content

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

Project description

starlette-compress

Support my work Liberapay Patrons 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.8+ 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.0.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

starlette_compress-1.0.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: starlette_compress-1.0.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for starlette_compress-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2b1b1392990bdc4632aac880aa6e275ebbf0297e4ef420ad6da1721af12cb572
MD5 0c049dd3a25582172593f63853bb52bb
BLAKE2b-256 128907ce394c23f8ca54179746410b595d22d21f3f7b209f28ed7c025b3f8837

See more details on using hashes here.

Provenance

File details

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

File metadata

File hashes

Hashes for starlette_compress-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4f2f9094254ba9a9064106fcd574fcb3ff7ab7fe3fbcd1c5eb622d2d7ea0a39e
MD5 87c53f4ed941c83e77b23e13f06a82ee
BLAKE2b-256 82e3e269e698ece473d2c9b501971a5eae1d9460bcdc7f479d4d579281190489

See more details on using hashes here.

Provenance

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