# starlette-cramjam
<p align="center">
<em>Cramjam integration for Starlette ASGI framework.</em>
</p>
<p align="center">
<a href="https://github.com/developmentseed/starlette-cramjam/actions?query=workflow%3ACI" target="_blank">
<img src="https://github.com/developmentseed/starlette-cramjam/workflows/CI/badge.svg" alt="Test">
</a>
<a href="https://codecov.io/gh/developmentseed/starlette-cramjam" target="_blank">
<img src="https://codecov.io/gh/developmentseed/starlette-cramjam/branch/master/graph/badge.svg" alt="Coverage">
</a>
<a href="https://pypi.org/project/starlette-cramjam" target="_blank">
<img src="https://img.shields.io/pypi/v/starlette-cramjam?color=%2334D058&label=pypi%20package" alt="Package version">
</a>
<a href="https://pypistats.org/packages/starlette-cramjam" target="_blank">
<img src="https://img.shields.io/pypi/dm/starlette-cramjam.svg" alt="Downloads">
</a>
<a href="https://github.com/developmentseed/starlette-cramjam/blob/master/LICENSE" target="_blank">
<img src="https://img.shields.io/github/license/developmentseed/starlette-cramjam.svg" alt="Downloads">
</a>
</p>
---
**Source Code**: <a href="https://github.com/developmentseed/starlette-cramjam" target="_blank">https://github.com/developmentseed/starlette-cramjam</a>
---
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](https://github.com/milesgranger/pyrus-cramjam) an *Extremely thin Python bindings to de/compression algorithms in Rust*.
## Installation
You can install `starlette-cramjam` from pypi
```python
$ pip install -U pip
$ pip install starlette-cramjam
```
or install from source:
```bash
$ pip install -U pip
$ pip install https://github.com/developmentseed/starlette-cramjam.git
```
## Usage
The following arguments are supported:
- **compression** (List of Compression) - List of available compression algorithm. **This list also defines the order of preference**. Defaults to `[Compression.gzip, Compression.deflate, Compression.br]`,
- **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
```python
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
```python
import uvicorn
from starlette.applications import Starlette
from starlette.responses import PlainTextResponse, Response
from starlette_cramjam.compression import Compression
from starlette_cramjam.middleware import CompressionMiddleware
# create application
app = Starlette()
# register the CompressionMiddleware
app.add_middleware(
CompressionMiddleware,
compression=[Compression.gzip], # Only support `gzip`
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](https://github.com/developmentseed/starlette-cramjam/blob/master/CHANGES.md).
## Contribution & Development
See [CONTRIBUTING.md](https://github.com/developmentseed/starlette-cramjam/blob/master/CONTRIBUTING.md)
## License
See [LICENSE](https://github.com/developmentseed/starlette-cramjam/blob/master/LICENSE)
## Authors
Created by [Development Seed](<http://developmentseed.org>)
See [contributors](https://github.com/developmentseed/starlette-cramjam/graphs/contributors) for a listing of individual contributors.
Raw data
{
"_id": null,
"home_page": null,
"name": "starlette-cramjam",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": "Cramjam, Compression, ASGI, Starlette",
"author": null,
"author_email": "Vincent Sarago <vincent@developmentseed.com>",
"download_url": "https://files.pythonhosted.org/packages/19/b1/31021667f3f444fbc84396ae91780c9fad237153e429e1b96fe5f561d2dc/starlette_cramjam-0.3.3.tar.gz",
"platform": null,
"description": "# starlette-cramjam\n\n<p align=\"center\">\n <em>Cramjam integration for Starlette ASGI framework.</em>\n</p>\n<p align=\"center\">\n <a href=\"https://github.com/developmentseed/starlette-cramjam/actions?query=workflow%3ACI\" target=\"_blank\">\n <img src=\"https://github.com/developmentseed/starlette-cramjam/workflows/CI/badge.svg\" alt=\"Test\">\n </a>\n <a href=\"https://codecov.io/gh/developmentseed/starlette-cramjam\" target=\"_blank\">\n <img src=\"https://codecov.io/gh/developmentseed/starlette-cramjam/branch/master/graph/badge.svg\" alt=\"Coverage\">\n </a>\n <a href=\"https://pypi.org/project/starlette-cramjam\" target=\"_blank\">\n <img src=\"https://img.shields.io/pypi/v/starlette-cramjam?color=%2334D058&label=pypi%20package\" alt=\"Package version\">\n </a>\n <a href=\"https://pypistats.org/packages/starlette-cramjam\" target=\"_blank\">\n <img src=\"https://img.shields.io/pypi/dm/starlette-cramjam.svg\" alt=\"Downloads\">\n </a>\n <a href=\"https://github.com/developmentseed/starlette-cramjam/blob/master/LICENSE\" target=\"_blank\">\n <img src=\"https://img.shields.io/github/license/developmentseed/starlette-cramjam.svg\" alt=\"Downloads\">\n </a>\n</p>\n\n---\n\n**Source Code**: <a href=\"https://github.com/developmentseed/starlette-cramjam\" target=\"_blank\">https://github.com/developmentseed/starlette-cramjam</a>\n\n---\n\nThe `starlette-cramjam` middleware aims to provide a unique Compression middleware to support **Brotli**, **GZip** and **Deflate** compression algorithms with a minimal requirement.\n\nThe middleware will compress responses for any request that includes \"br\", \"gzip\" or \"deflate\" in the Accept-Encoding header.\n\nAs for the official `Starlette` middleware, the one provided by `starlette-cramjam` will handle both standard and streaming responses.\n\n`stralette-cramjam` is built on top of [pyrus-cramjam](https://github.com/milesgranger/pyrus-cramjam) an *Extremely thin Python bindings to de/compression algorithms in Rust*.\n\n## Installation\n\nYou can install `starlette-cramjam` from pypi\n\n```python\n$ pip install -U pip\n$ pip install starlette-cramjam\n```\n\nor install from source:\n\n```bash\n$ pip install -U pip\n$ pip install https://github.com/developmentseed/starlette-cramjam.git\n```\n\n## Usage\n\nThe following arguments are supported:\n\n- **compression** (List of Compression) - List of available compression algorithm. **This list also defines the order of preference**. Defaults to `[Compression.gzip, Compression.deflate, Compression.br]`,\n- **minimum_size** (Integer) - Do not compress responses that are smaller than this minimum size in bytes. Defaults to `500`.\n- **exclude_path** (Set of string) - Do not compress responses in response to specific `path` requests. Entries have to be valid regex expressions. Defaults to `{}`.\n- **exclude_mediatype** (Set of string) - Do not compress responses of specific media type (e.g `image/png`). Defaults to `{}`.\n\n#### Minimal (defaults) example\n\n```python\nimport uvicorn\n\nfrom starlette.applications import Starlette\nfrom starlette.responses import PlainTextResponse\n\nfrom starlette_cramjam.middleware import CompressionMiddleware\n\n# create application\napp = Starlette()\n\n# register the CompressionMiddleware\napp.add_middleware(CompressionMiddleware)\n\n\n@app.route(\"/\")\ndef index(request):\n return PlainTextResponse(\"Hello World\")\n\n\nif __name__ == \"__main__\":\n uvicorn.run(app, host=\"0.0.0.0\", port=8000)\n```\n\n#### Using options\n\n```python\nimport uvicorn\n\nfrom starlette.applications import Starlette\nfrom starlette.responses import PlainTextResponse, Response\n\nfrom starlette_cramjam.compression import Compression\nfrom starlette_cramjam.middleware import CompressionMiddleware\n\n# create application\napp = Starlette()\n\n# register the CompressionMiddleware\napp.add_middleware(\n CompressionMiddleware,\n compression=[Compression.gzip], # Only support `gzip`\n minimum_size=0, # should compress everything\n exclude_path={\"^/foo$\"}, # do not compress response for the `/foo` request\n exclude_mediatype={\"image/jpeg\"}, # do not compress jpeg\n)\n\n\n@app.route(\"/\")\ndef index(request):\n return PlainTextResponse(\"Hello World\")\n\n@app.route(\"/image\")\ndef foo(request):\n return Response(b\"This is a fake body\", status_code=200, media_type=\"image/jpeg\")\n\n@app.route(\"/foo\")\ndef foo(request):\n return PlainTextResponse(\"Do not compress me.\")\n\n\nif __name__ == \"__main__\":\n uvicorn.run(app, host=\"0.0.0.0\", port=8000)\n```\n\n## Changes\n\nSee [CHANGES.md](https://github.com/developmentseed/starlette-cramjam/blob/master/CHANGES.md).\n\n## Contribution & Development\n\nSee [CONTRIBUTING.md](https://github.com/developmentseed/starlette-cramjam/blob/master/CONTRIBUTING.md)\n\n## License\n\nSee [LICENSE](https://github.com/developmentseed/starlette-cramjam/blob/master/LICENSE)\n\n## Authors\n\nCreated by [Development Seed](<http://developmentseed.org>)\n\nSee [contributors](https://github.com/developmentseed/starlette-cramjam/graphs/contributors) for a listing of individual contributors.\n",
"bugtrack_url": null,
"license": null,
"summary": "Cramjam integration for Starlette ASGI framework.",
"version": "0.3.3",
"project_urls": {
"Source": "https://github.com/developmentseed/starlette-cramjam"
},
"split_keywords": [
"cramjam",
" compression",
" asgi",
" starlette"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "93827ed3969bccad106c5218592c4657d35d740fa2bad82b138b4cc4151d01b9",
"md5": "b56f08f17584ebed320ef4a3129cba76",
"sha256": "91675232b9847f6d45bbc38937c3beaaec51f80ce31167a5a7941b348967e95c"
},
"downloads": -1,
"filename": "starlette_cramjam-0.3.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b56f08f17584ebed320ef4a3129cba76",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 6652,
"upload_time": "2024-05-24T16:49:04",
"upload_time_iso_8601": "2024-05-24T16:49:04.799793Z",
"url": "https://files.pythonhosted.org/packages/93/82/7ed3969bccad106c5218592c4657d35d740fa2bad82b138b4cc4151d01b9/starlette_cramjam-0.3.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "19b131021667f3f444fbc84396ae91780c9fad237153e429e1b96fe5f561d2dc",
"md5": "54ec066a69cb475c79965e92aa3093ba",
"sha256": "b7d5a95ef85f7e37a972ee1007bbc851c946ecdedbc95f54f9a2f40bed2d5431"
},
"downloads": -1,
"filename": "starlette_cramjam-0.3.3.tar.gz",
"has_sig": false,
"md5_digest": "54ec066a69cb475c79965e92aa3093ba",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 7447,
"upload_time": "2024-05-24T16:49:06",
"upload_time_iso_8601": "2024-05-24T16:49:06.143289Z",
"url": "https://files.pythonhosted.org/packages/19/b1/31021667f3f444fbc84396ae91780c9fad237153e429e1b96fe5f561d2dc/starlette_cramjam-0.3.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-24 16:49:06",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "developmentseed",
"github_project": "starlette-cramjam",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "starlette-cramjam"
}