A framework-independent compression middleware for ASGI applications
Project description
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.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file asgi_compression-0.1.1.tar.gz.
File metadata
- Download URL: asgi_compression-0.1.1.tar.gz
- Upload date:
- Size: 11.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cefb533a3ad3fa8f3a785e179be2c5fac124c54705929ca697692911d987ba91
|
|
| MD5 |
fefa0f6e906811ab9ac7a97eb75b8f06
|
|
| BLAKE2b-256 |
c439f94248fdef1e0155235c346337ab52eb7106bbd3d67bf6186b81f3f1194e
|
File details
Details for the file asgi_compression-0.1.1-py3-none-any.whl.
File metadata
- Download URL: asgi_compression-0.1.1-py3-none-any.whl
- Upload date:
- Size: 11.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
756de28bf2b8c3ddc2d2df68ef20737b85f4f9a0ea3280290aaadc3ec638e954
|
|
| MD5 |
b97c8034334a2b652c1c10b93741f9ea
|
|
| BLAKE2b-256 |
7a52eec1fe6246c0a5cc27c79492ff63bc0709b8bd4aff1541819bf3ab05e5fa
|