Compression middleware for Starlette - supporting ZStd, Brotli, and GZip
Project description
starlette-compress
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
andtrio
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
Built Distribution
File details
Details for the file starlette_compress-1.3.0.tar.gz
.
File metadata
- Download URL: starlette_compress-1.3.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 038b7e0a8cc8603807a7f506a0cf501da16374e1ed18eb565494dcd71aa43441 |
|
MD5 | 811c35a93a82c2a67abfcbc236382f9a |
|
BLAKE2b-256 | e632d997aaf839b4d35d3bf3fa97834b8cd118eb52e8008052db393e69196134 |
Provenance
The following attestation bundles were made for starlette_compress-1.3.0.tar.gz
:
Publisher:
workflow.yaml
on Zaczero/starlette-compress
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
starlette_compress-1.3.0.tar.gz
- Subject digest:
038b7e0a8cc8603807a7f506a0cf501da16374e1ed18eb565494dcd71aa43441
- Sigstore transparency entry: 149318646
- Sigstore integration time:
- Predicate type:
File details
Details for the file starlette_compress-1.3.0-py3-none-any.whl
.
File metadata
- Download URL: starlette_compress-1.3.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c6c449d77b3934193173b24a97efda49c4d4027e455c8fd84aa0b359c9a461c |
|
MD5 | 467d248955b316c1e5ab792cd3a9744e |
|
BLAKE2b-256 | a983b548bcfd7b67669a1c708f33e1e9fe59bccebb10cb26f5a84dbdb70bcb59 |
Provenance
The following attestation bundles were made for starlette_compress-1.3.0-py3-none-any.whl
:
Publisher:
workflow.yaml
on Zaczero/starlette-compress
-
Statement type:
https://in-toto.io/Statement/v1
- Predicate type:
https://docs.pypi.org/attestations/publish/v1
- Subject name:
starlette_compress-1.3.0-py3-none-any.whl
- Subject digest:
1c6c449d77b3934193173b24a97efda49c4d4027e455c8fd84aa0b359c9a461c
- Sigstore transparency entry: 149318656
- Sigstore integration time:
- Predicate type: