# starlette-compress
[![PyPI - Python Version](https://shields.monicz.dev/pypi/pyversions/starlette-compress)](https://pypi.org/project/starlette-compress)
[![Liberapay Patrons](https://shields.monicz.dev/liberapay/patrons/Zaczero?logo=liberapay&label=Patrons)](https://liberapay.com/Zaczero/)
[![GitHub Sponsors](https://shields.monicz.dev/github/sponsors/Zaczero?logo=github&label=Sponsors&color=%23db61a2)](https://github.com/sponsors/Zaczero)
**starlette-compress** is a fast and simple middleware for compressing responses in [Starlette](https://www.starlette.io). It supports more compression algorithms than Starlette's built-in GZipMiddleware, and has more sensible defaults.
- Python 3.9+ support
- Compatible with `asyncio` and `trio` backends
- ZStd, Brotli, and GZip compression
- Sensible default configuration
- [The Unlicense](https://unlicense.org) — public domain dedication
- [Semantic Versioning](https://semver.org) compliance
## Installation
```sh
pip install starlette-compress
```
## Basic Usage
### Starlette
```py
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette_compress import CompressMiddleware
middleware = [
Middleware(CompressMiddleware)
]
app = Starlette(routes=..., middleware=middleware)
```
### FastAPI
You can use starlette-compress with [FastAPI](https://fastapi.tiangolo.com) too:
```py
from fastapi import FastAPI
from starlette_compress import CompressMiddleware
app = FastAPI()
app.add_middleware(CompressMiddleware)
```
## Advanced Usage
### Changing Minimum Response Size
Control the minimum size of the response to compress. By default, responses must be at least 500 bytes to be compressed.
```py
# Starlette
middleware = [
Middleware(CompressMiddleware, minimum_size=1000)
]
# FastAPI
app.add_middleware(CompressMiddleware, minimum_size=1000)
```
### Tuning Compression Levels
Adjust the compression levels for each algorithm. Higher levels mean smaller files but slower compression. Default level is 4 for all algorithms.
```py
# Starlette
middleware = [
Middleware(CompressMiddleware, zstd_level=6, brotli_quality=6, gzip_level=6)
]
# FastAPI
app.add_middleware(CompressMiddleware, zstd_level=6, brotli_quality=6, gzip_level=6)
```
### Supporting Custom Content-Types
Manage the supported content-types. Unknown response types are not compressed. [Check here](https://github.com/Zaczero/starlette-compress/blob/main/starlette_compress/__init__.py) for the default configuration.
```py
from starlette_compress import add_compress_type, remove_compress_type
add_compress_type("application/my-custom-type")
remove_compress_type("application/json")
```
Raw data
{
"_id": null,
"home_page": "https://github.com/Zaczero/starlette-compress",
"name": "starlette-compress",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "brotli, compression, fastapi, gzip, http, middleware, starlette, zstd",
"author": "Kamil Monicz",
"author_email": "kamil@monicz.dev",
"download_url": "https://files.pythonhosted.org/packages/5e/78/a77c21ec2dab58e0ef81c74547fe7ace8ac009d16dc776ad137390e55bfc/starlette_compress-1.4.0.tar.gz",
"platform": null,
"description": "# starlette-compress\n\n[![PyPI - Python Version](https://shields.monicz.dev/pypi/pyversions/starlette-compress)](https://pypi.org/project/starlette-compress)\n[![Liberapay Patrons](https://shields.monicz.dev/liberapay/patrons/Zaczero?logo=liberapay&label=Patrons)](https://liberapay.com/Zaczero/)\n[![GitHub Sponsors](https://shields.monicz.dev/github/sponsors/Zaczero?logo=github&label=Sponsors&color=%23db61a2)](https://github.com/sponsors/Zaczero)\n\n**starlette-compress** is a fast and simple middleware for compressing responses in [Starlette](https://www.starlette.io). It supports more compression algorithms than Starlette's built-in GZipMiddleware, and has more sensible defaults.\n\n- Python 3.9+ support\n- Compatible with `asyncio` and `trio` backends\n- ZStd, Brotli, and GZip compression\n- Sensible default configuration\n- [The Unlicense](https://unlicense.org) \u2014 public domain dedication\n- [Semantic Versioning](https://semver.org) compliance\n\n## Installation\n\n```sh\npip install starlette-compress\n```\n\n## Basic Usage\n\n### Starlette\n\n```py\nfrom starlette.applications import Starlette\nfrom starlette.middleware import Middleware\nfrom starlette_compress import CompressMiddleware\n\nmiddleware = [\n Middleware(CompressMiddleware)\n]\n\napp = Starlette(routes=..., middleware=middleware)\n```\n\n### FastAPI\n\nYou can use starlette-compress with [FastAPI](https://fastapi.tiangolo.com) too:\n\n```py\nfrom fastapi import FastAPI\nfrom starlette_compress import CompressMiddleware\n\napp = FastAPI()\napp.add_middleware(CompressMiddleware)\n```\n\n## Advanced Usage\n\n### Changing Minimum Response Size\n\nControl the minimum size of the response to compress. By default, responses must be at least 500 bytes to be compressed.\n\n```py\n# Starlette\nmiddleware = [\n Middleware(CompressMiddleware, minimum_size=1000)\n]\n\n# FastAPI\napp.add_middleware(CompressMiddleware, minimum_size=1000)\n```\n\n### Tuning Compression Levels\n\nAdjust the compression levels for each algorithm. Higher levels mean smaller files but slower compression. Default level is 4 for all algorithms.\n\n```py\n# Starlette\nmiddleware = [\n Middleware(CompressMiddleware, zstd_level=6, brotli_quality=6, gzip_level=6)\n]\n\n# FastAPI\napp.add_middleware(CompressMiddleware, zstd_level=6, brotli_quality=6, gzip_level=6)\n```\n\n### Supporting Custom Content-Types\n\nManage the supported content-types. Unknown response types are not compressed. [Check here](https://github.com/Zaczero/starlette-compress/blob/main/starlette_compress/__init__.py) for the default configuration.\n\n```py\nfrom starlette_compress import add_compress_type, remove_compress_type\n\nadd_compress_type(\"application/my-custom-type\")\nremove_compress_type(\"application/json\")\n```\n\n",
"bugtrack_url": null,
"license": "0BSD",
"summary": "Compression middleware for Starlette - supporting ZStd, Brotli, and GZip",
"version": "1.4.0",
"project_urls": {
"Homepage": "https://github.com/Zaczero/starlette-compress",
"Repository": "https://github.com/Zaczero/starlette-compress"
},
"split_keywords": [
"brotli",
" compression",
" fastapi",
" gzip",
" http",
" middleware",
" starlette",
" zstd"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "54c3e7f80240d617f401ca517995a57df7624d9477247c8f3b8dac799dd75d56",
"md5": "3d6c93b09a508a073ceb840dd71d0b7d",
"sha256": "c2def8af427656d031523e4dddc4dc25b61a64185f52d747f574eb8465c77c40"
},
"downloads": -1,
"filename": "starlette_compress-1.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3d6c93b09a508a073ceb840dd71d0b7d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 6016,
"upload_time": "2024-12-17T16:32:11",
"upload_time_iso_8601": "2024-12-17T16:32:11.375091Z",
"url": "https://files.pythonhosted.org/packages/54/c3/e7f80240d617f401ca517995a57df7624d9477247c8f3b8dac799dd75d56/starlette_compress-1.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5e78a77c21ec2dab58e0ef81c74547fe7ace8ac009d16dc776ad137390e55bfc",
"md5": "bc5715380c38f4c3f9af0d5786586386",
"sha256": "c88823a23e66b38115f8489dfe4a4102cd4825649f2283df693baa9a5e87251d"
},
"downloads": -1,
"filename": "starlette_compress-1.4.0.tar.gz",
"has_sig": false,
"md5_digest": "bc5715380c38f4c3f9af0d5786586386",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 6475,
"upload_time": "2024-12-17T16:32:13",
"upload_time_iso_8601": "2024-12-17T16:32:13.992298Z",
"url": "https://files.pythonhosted.org/packages/5e/78/a77c21ec2dab58e0ef81c74547fe7ace8ac009d16dc776ad137390e55bfc/starlette_compress-1.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-17 16:32:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Zaczero",
"github_project": "starlette-compress",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "starlette-compress"
}