{"id":13598631,"url":"https://github.com/fullonic/brotli-asgi","last_synced_at":"2025-10-21T20:36:45.946Z","repository":{"id":48037053,"uuid":"267262603","full_name":"fullonic/brotli-asgi","owner":"fullonic","description":"A compression AGSI middleware using brotli.","archived":false,"fork":false,"pushed_at":"2023-05-17T04:18:49.000Z","size":42,"stargazers_count":71,"open_issues_count":4,"forks_count":12,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-12T04:35:29.648Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fullonic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-05-27T08:22:34.000Z","updated_at":"2025-02-24T18:33:56.000Z","dependencies_parsed_at":"2024-01-16T23:26:24.572Z","dependency_job_id":"f884766e-ed83-4359-80cf-432fc7eb10bf","html_url":"https://github.com/fullonic/brotli-asgi","commit_stats":{"total_commits":36,"total_committers":8,"mean_commits":4.5,"dds":0.5833333333333333,"last_synced_commit":"5b8cf6b20abea647e0ca24bd20b595bf3ae9171b"},"previous_names":["fullonic/brotli_middleware"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fullonic%2Fbrotli-asgi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fullonic%2Fbrotli-asgi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fullonic%2Fbrotli-asgi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fullonic%2Fbrotli-asgi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fullonic","download_url":"https://codeload.github.com/fullonic/brotli-asgi/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248191750,"owners_count":21062563,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T17:00:54.454Z","updated_at":"2025-10-21T20:36:45.896Z","avatar_url":"https://github.com/fullonic.png","language":"Python","funding_links":[],"categories":["Serialization"],"sub_categories":["Tutorials"],"readme":"# brotli-asgi\n\n[![Packaging status](https://img.shields.io/pypi/v/brotli-asgi?color=%2334D058\u0026label=pypi%20package)](https://pypi.org/project/brotli-asgi)\n[![CI](https://github.com/fullonic/brotli-asgi/workflows/Tests/badge.svg)](https://github.com/fullonic/brotli-asgi/actions?query=workflow%3ATests)\n\n\n\n`BrotliMiddleware` adds [Brotli](https://github.com/google/brotli) response compression to ASGI applications (Starlette, FastAPI, Quart, etc.). It provides faster and more dense compression than GZip, and can be used as a drop in replacement for the `GZipMiddleware` shipped with Starlette.\n\n**Installation**\n\n```bash\npip install brotli-asgi\n```\n\n## Examples\n\n### Starlette\n\n```python\nfrom starlette.applications import Starlette\nfrom starlette.responses import JSONResponse\nfrom starlette.routing import Route\nfrom starlette.middleware import Middleware\n\nfrom brotli_asgi import BrotliMiddleware\n\nasync def homepage(request):\n    return JSONResponse({\"data\": \"a\" * 4000})\n\napp = Starlette(\n  routes=[Route(\"/\", homepage)],\n  middleware=[Middleware(BrotliMiddleware)],\n)\n```\n\n### FastAPI\n\n```python\nfrom fastapi import FastAPI\nfrom brotli_asgi import BrotliMiddleware\n\napp = FastAPI()\napp.add_middleware(BrotliMiddleware)\n\n@app.get(\"/\")\ndef home() -\u003e dict:\n    return {\"data\": \"a\" * 4000}\n```\n\n## API Reference\n\n**Overview**\n\n```python\napp.add_middleware(\n  BrotliMiddleware,\n  quality=4,\n  mode=\"text\",\n  lgwin=22,\n  lgblock=0,\n  minimum_size=400,\n  gzip_fallback=True\n)\n```\n\n**Parameters**:\n\n- _(Optional)_ `quality`: Controls the compression speed vs compression density tradeoff. The higher the quality, the slower the compression. Range is 0 to 11.\n- _(Optional)_ `mode`: The compression mode can be: `\"generic\"`, `\"text\"` (`Default` for UTF-8 format text input) or `\"font\"` (for WOFF 2.0).\n- _(Optional)_ `lgwin`: Base 2 logarithm of the sliding window size. Range is 10 to 24.\n- _(Optional)_ `lgblock`: Base 2 logarithm of the maximum input block size. Range is 16 to 24. If set to 0, the value will be set based on the quality.\n- _(Optional)_ `minimum_size`: Only compress responses that are bigger than this value in bytes.\n- _(Optional)_ `gzip_fallback`: If `True`, uses gzip encoding if `br` is not in the Accept-Encoding header.\n\n**Notes**:\n\n- It won't apply Brotli compression on responses that already have a Content-Encoding set, to prevent them from being encoded twice.\n- If the GZip fallback is applied, and the response already had a Content-Encoding set, the double-encoding-prevention will only happen if you're using Starlette `\u003e=0.22.0` (see [the PR](https://github.com/encode/starlette/pull/1901)).\n\n## Performance\n\nTo better understand the benefits of Brotli over GZip, see, [Gzip vs. Brotli: Comparing Compression Techniques](https://www.coralnodes.com/gzip-vs-brotli/), where detailed information and benchmarks are provided.\n\nA simple comparative example using Python `sys.getsizof()` and `timeit`:\n\n```python\n# ipython console\nimport gzip\nimport sys\n\nimport brotli\nimport requests\n\npage = requests.get(\"https://github.com/fullonic/brotli-asgi\").content\n%timeit brotli.compress(page, quality=4)\n# 1.83 ms ± 43 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\nsys.getsizeof(brotli.compress(page, quality=4))\n# 20081\n%timeit gzip.compress(page, compresslevel=6)\n# 2.75 ms ± 29.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\nsys.getsizeof(gzip.compress(page, compresslevel=6))\n# 20640\n```\n\n## Compatibility\n\nAccording to [caniuse.com](https://caniuse.com/#feat=brotli), Brotli is supported by all major browsers with a global use of over _96.3%_.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffullonic%2Fbrotli-asgi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffullonic%2Fbrotli-asgi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffullonic%2Fbrotli-asgi/lists"}