This release is a pre-release and may not be stable for production use.
starlette-cramjam
Cramjam integration for Starlette ASGI framework.
Source Code: https://github.com/developmentseed/starlette-cramjam
The starlette-cramjam middleware aims to provide a unique Compression middleware to support Brotli, GZip and Deflate compression algorithms with a minimal requirement.
The middleware will compress responses for any request that includes "br", "gzip" or "deflate" in the Accept-Encoding header.
As for the official Starlette middleware, the one provided by `starlette-cramjam will handle both standard and streaming responses.
Installation
You can install starlette-cramjam from pypi
$ pip install -U pip
$ pip install starlette-cramjam
or install from source:
$ pip install -U pip
$ pip install https://github.com/developmentseed/starlette-cramjam.git
Usage
The following arguments are supported:
- minimum_size (Integer) - Do not compress responses that are smaller than this minimum size in bytes. Defaults to
500. - exclude_path (Set of string) - Do not compress responses in response to specific
pathrequests. Entries have to be valid regex expressions. Defaults to{}. - exclude_mediatype (Set of string) - Do not compress responses of specific media type (e.g
image/png). Defaults to{}.
Minimal (defaults) example
import uvicorn
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse
from starlette_cramjam.middleware import CompressionMiddleware
# create application
app = Starlette()
# register the CompressionMiddleware
app.add_middleware(CompressionMiddleware)
@app.route("/")
def index(request):
return PlainTextResponse("Hello World")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
Using options
import uvicorn
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse, Response
from starlette_cramjam.middleware import CompressionMiddleware
# create application
app = Starlette()
# register the CompressionMiddleware
app.add_middleware(
CompressionMiddleware,
minimum_size=0, # should compress everything
exclude_path={"^/foo$"}, # do not compress response for the `/foo` request
exclude_mediatype={"image/jpeg"}, # do not compress jpeg
)
@app.route("/")
def index(request):
return PlainTextResponse("Hello World")
@app.route("/image")
def foo(request):
return Response(b"This is a fake body", status_code=200, media_type="image/jpeg")
@app.route("/foo")
def foo(request):
return PlainTextResponse("Do not compress me.")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file starlette_cramjam-0.1.0a0.tar.gz.
File metadata
- Download URL: starlette_cramjam-0.1.0a0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ccb9c49f7bebe82f37ead58b1c94dbcec8df0911f490240efe6914bc5995e9b7
|
|
| MD5 |
98c0e3b5e4325cf4b7115fc9b54a0eec
|
|
| BLAKE2b-256 |
ab6259eb09fa8a7be23b08b45b57880df8716e202939fafaed4cffb631cef7ef
|