ASGI Compression
A framework and platform-independent compression middleware for ASGI applications.
✨ Features
- 🚀 Framework Independent - Works with any ASGI-compatible framework (FastAPI, Starlette, Litestar, Django, Falcon, etc.)
- 📦 Multiple Compression Algorithms - Supports gzip, brotli, and zstandard compression algorithms
- 🔄 Content Negotiation - Automatically selects the best compression algorithm based on the client's Accept-Encoding header
- 🛠️ Fully Configurable - Control minimum size for compression, compression levels, and more
- 📏 Minimal Dependencies - Single external dependency (multidict) apart from optional compression libraries
- 📝 Fully Typed - Complete type annotations for excellent IDE support and code safety
- 🐍 Wide Python Support - Compatible with Python 3.9 to 3.13
- 🔍 Streaming Support - Efficiently compresses both standard and streaming responses
- 🖥️ Platform Independent - Supports macOS, Linux, and Windows.
📥 Installation
Install the package with pip:
# Basic installation (includes gzip compression)
pip install asgi-compression
# With gzip and brotli support
pip install asgi-compression[br]
# With gzip and zstandard support
pip install asgi-compression[zstd]
# With gzip, brotli and zstandard support
pip install asgi-compression[all]
🚀 Usage
Basic Example
from asgi_compression import CompressionMiddleware, GzipAlgorithm
app = ... # Your ASGI application
# Apply gzip compression with default settings
app = CompressionMiddleware(
app=app,
algorithms=[GzipAlgorithm()],
)
Multiple Algorithms Example
from asgi_compression import (
BrotliAlgorithm,
CompressionMiddleware,
GzipAlgorithm,
ZstdAlgorithm
)
app = ... # Your ASGI application
# Apply multiple compression algorithms in order of preference
app = CompressionMiddleware(
app=app,
algorithms=[
BrotliAlgorithm(), # Brotli will be used if the client supports it
ZstdAlgorithm(), # Zstandard will be used as a fallback
GzipAlgorithm(), # Gzip as a last resort
],
minimum_size=1000, # Only compress responses larger than 1KB
)
Framework-Specific Examples
FastAPI
from fastapi import FastAPI
from asgi_compression import CompressionMiddleware, GzipAlgorithm, BrotliAlgorithm
app = FastAPI()
@app.get("/")
async def read_root():
return {"Hello": "World"}
# Apply compression middleware
app.add_middleware(
CompressionMiddleware,
algorithms=[BrotliAlgorithm(), GzipAlgorithm()],
)
Starlette
from starlette.applications import Starlette
from starlette.responses import JSONResponse
from starlette.routing import Route
from asgi_compression import CompressionMiddleware, GzipAlgorithm
async def homepage(request):
return JSONResponse({"hello": "world"})
routes = [
Route("/", homepage)
]
app = Starlette(routes=routes)
app = CompressionMiddleware(app=app, algorithms=[GzipAlgorithm()])
Litestar
from litestar import Litestar, get
from asgi_compression import CompressionMiddleware, BrotliAlgorithm
@get("/")
async def homepage() -> dict:
return {"hello": "world"}
app = Litestar(route_handlers=[homepage])
app = CompressionMiddleware(app=app, algorithms=[BrotliAlgorithm()])
🙌 Inspired by
This project was brought to life thanks to inspiration from:
- Startlette's Gzip middleware
- brotli-asgi
- zstd-asgi
Cudos to devs & maintainers of those amazing libraries!
📜 License
This project is licensed under the terms of the MIT license.
Metadata
Release files for asgi-compression 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| asgi_compression-0.1.1.tar.gz | 11.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| asgi_compression-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.9 kB
Release files / asgi_compression-0.1.1.tar.gz
| Download URL | asgi_compression-0.1.1.tar.gz |
|---|---|
| Size | 11.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cefb533a3ad3fa8f3a785e179be2c5fac124c54705929ca697692911d987ba91
|
|
BLAKE2b-256 checksum How to use checksums |
c439f94248fdef1e0155235c346337ab52eb7106bbd3d67bf6186b81f3f1194e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / asgi_compression-0.1.1-py3-none-any.whl
| Download URL | asgi_compression-0.1.1-py3-none-any.whl |
|---|---|
| Size | 11.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
756de28bf2b8c3ddc2d2df68ef20737b85f4f9a0ea3280290aaadc3ec638e954
|
|
BLAKE2b-256 checksum How to use checksums |
7a52eec1fe6246c0a5cc27c79492ff63bc0709b8bd4aff1541819bf3ab05e5fa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|