Cramjam integration for Starlette ASGI framework.
Project description
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.
stralette-cramjam
is built on top of pyrus-cramjam an Extremely thin Python bindings to de/compression algorithms in Rust.
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
path
requests. 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)
Changes
See CHANGES.md.
Contribution & Development
See CONTRIBUTING.md
License
See LICENSE
Authors
Created by Development Seed
See contributors for a listing of individual contributors.
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
File details
Details for the file starlette-cramjam-0.2.0.tar.gz
.
File metadata
- Download URL: starlette-cramjam-0.2.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cad1a48ef3a93241f23d982e95158ec1c566f39f95be743a77eda68ce12847ee |
|
MD5 | dff9a650017b7a017a2aa6135392c636 |
|
BLAKE2b-256 | c1ec37dc114ab293a84355bd1a7896a64b26f79f5f79f6c8a46ae6000d95be9e |
File details
Details for the file starlette_cramjam-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: starlette_cramjam-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1643a5eeca942cdd99a41b3011207a149cd2aee8c1aa096fa08fe3c0e656ac41 |
|
MD5 | 4787a085befea626178ca117cbcb3b58 |
|
BLAKE2b-256 | abdfe3ecb0d6d484d0c6f2e5eb0d0e0542541f35fff26909c44179a80f61922a |