Use zstd rather than brotli for static file compression.
This commit is contained in:
23
cista/app.py
23
cista/app.py
@@ -9,7 +9,6 @@ from stat import S_IFDIR, S_IFREG
|
|||||||
from urllib.parse import unquote
|
from urllib.parse import unquote
|
||||||
from wsgiref.handlers import format_date_time
|
from wsgiref.handlers import format_date_time
|
||||||
|
|
||||||
import brotli
|
|
||||||
import sanic.helpers
|
import sanic.helpers
|
||||||
from blake3 import blake3
|
from blake3 import blake3
|
||||||
from sanic import Blueprint, Sanic, empty, raw, redirect
|
from sanic import Blueprint, Sanic, empty, raw, redirect
|
||||||
@@ -17,6 +16,7 @@ from sanic.exceptions import Forbidden, NotFound
|
|||||||
from sanic.log import logger
|
from sanic.log import logger
|
||||||
from setproctitle import setproctitle
|
from setproctitle import setproctitle
|
||||||
from stream_zip import ZIP_AUTO, stream_zip
|
from stream_zip import ZIP_AUTO, stream_zip
|
||||||
|
from zstandard import ZstdCompressor
|
||||||
|
|
||||||
from cista import auth, config, preview, session, watching
|
from cista import auth, config, preview, session, watching
|
||||||
from cista.api import bp
|
from cista.api import bp
|
||||||
@@ -95,6 +95,7 @@ def _load_wwwroot(www):
|
|||||||
wwwnew = {}
|
wwwnew = {}
|
||||||
base = Path(__file__).with_name("wwwroot")
|
base = Path(__file__).with_name("wwwroot")
|
||||||
paths = [PurePath()]
|
paths = [PurePath()]
|
||||||
|
zstd = ZstdCompressor(level=10)
|
||||||
while paths:
|
while paths:
|
||||||
path = paths.pop(0)
|
path = paths.pop(0)
|
||||||
current = base / path
|
current = base / path
|
||||||
@@ -126,11 +127,11 @@ def _load_wwwroot(www):
|
|||||||
else "no-cache",
|
else "no-cache",
|
||||||
"content-type": mime,
|
"content-type": mime,
|
||||||
}
|
}
|
||||||
# Precompress with Brotli
|
# Precompress with ZSTD
|
||||||
br = brotli.compress(data)
|
zs = zstd.compress(data)
|
||||||
if len(br) >= len(data):
|
if len(zs) >= len(data):
|
||||||
br = False
|
zs = False
|
||||||
wwwnew[name] = data, br, headers
|
wwwnew[name] = data, zs, headers
|
||||||
if not wwwnew:
|
if not wwwnew:
|
||||||
msg = f"Web frontend missing from {base}\n Did you forget: hatch build\n"
|
msg = f"Web frontend missing from {base}\n Did you forget: hatch build\n"
|
||||||
if not www:
|
if not www:
|
||||||
@@ -196,14 +197,14 @@ async def wwwroot(req, path=""):
|
|||||||
name = unquote(path)
|
name = unquote(path)
|
||||||
if name not in www:
|
if name not in www:
|
||||||
raise NotFound(f"File not found: /{path}", extra={"name": name})
|
raise NotFound(f"File not found: /{path}", extra={"name": name})
|
||||||
data, br, headers = www[name]
|
data, zs, headers = www[name]
|
||||||
if req.headers.if_none_match == headers["etag"]:
|
if req.headers.if_none_match == headers["etag"]:
|
||||||
# The client has it cached, respond 304 Not Modified
|
# The client has it cached, respond 304 Not Modified
|
||||||
return empty(304, headers=headers)
|
return empty(304, headers=headers)
|
||||||
# Brotli compressed?
|
# Zstandard compressed?
|
||||||
if br and "br" in req.headers.accept_encoding.split(", "):
|
if zs and "zstd" in req.headers.accept_encoding.split(", "):
|
||||||
headers = {**headers, "content-encoding": "br"}
|
headers = {**headers, "content-encoding": "zstd"}
|
||||||
data = br
|
data = zs
|
||||||
return raw(data, headers=headers)
|
return raw(data, headers=headers)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ dependencies = [
|
|||||||
"argon2-cffi>=25.1.0",
|
"argon2-cffi>=25.1.0",
|
||||||
"av>=15.0.0",
|
"av>=15.0.0",
|
||||||
"blake3>=1.0.5",
|
"blake3>=1.0.5",
|
||||||
"brotli>=1.1.0",
|
|
||||||
"docopt>=0.6.2",
|
"docopt>=0.6.2",
|
||||||
"inotify>=0.2.12",
|
"inotify>=0.2.12",
|
||||||
"msgspec>=0.19.0",
|
"msgspec>=0.19.0",
|
||||||
@@ -42,6 +41,7 @@ dependencies = [
|
|||||||
"setproctitle>=1.3.6",
|
"setproctitle>=1.3.6",
|
||||||
"stream-zip>=0.0.83",
|
"stream-zip>=0.0.83",
|
||||||
"tomli_w>=1.2.0",
|
"tomli_w>=1.2.0",
|
||||||
|
"zstandard>=0.24.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.urls]
|
[project.urls]
|
||||||
|
|||||||
Reference in New Issue
Block a user