mirror of
https://github.com/kuhyx/screen-locker.git
synced 2026-11-24 21:06:23 +01:00
Compare commits
2 Commits
9e7f536f96
...
a7620e37b8
| Author | SHA1 | Date | |
|---|---|---|---|
| a7620e37b8 | |||
| 5f462e42bd |
@ -1,9 +1,11 @@
|
||||
"""Read-only localhost HTTP server for the status web UI.
|
||||
|
||||
Serves the status/decision/health payloads at ``/api/*`` and the built React
|
||||
bundle (``web/dist``) as static files. Binds to loopback only and never exposes
|
||||
a secret: every payload comes from :mod:`screen_locker._web_payload`, which
|
||||
reads state files but never the sync token or the HMAC key.
|
||||
Serves the status/decision/health payloads at ``/api/*``, the built Astro
|
||||
bundle (``web/dist``) as static files, and ``/version``: the short git hash the
|
||||
bundle was stamped with at build time (``web/stamp-version.mjs``), or 404 when
|
||||
the build carries no stamp. Binds to loopback only and never exposes a secret:
|
||||
every payload comes from :mod:`screen_locker._web_payload`, which reads state
|
||||
files but never the sync token or the HMAC key.
|
||||
|
||||
Read-only by construction, like the MCP server it sits beside: there is no
|
||||
route that logs a workout, clears a lock, or writes anything at all. A local
|
||||
@ -51,6 +53,9 @@ DEFAULT_PORT = 8770
|
||||
_API_STATUS = "/api/status"
|
||||
_API_DECISIONS = "/api/decisions"
|
||||
_API_HEALTH = "/api/health"
|
||||
_VERSION = "/version"
|
||||
# Written by web/stamp-version.mjs after `astro build`.
|
||||
_VERSION_FILE = "version.txt"
|
||||
|
||||
# Content types that are text but not under the ``text/`` prefix.
|
||||
_EXTRA_TEXT_TYPES = frozenset(
|
||||
@ -108,6 +113,8 @@ class _Handler(BaseHTTPRequestHandler):
|
||||
self._serve_json("decisions", lambda: build_decisions_payload(limit=limit))
|
||||
elif split.path == _API_HEALTH:
|
||||
self._serve_json("health", build_health_payload)
|
||||
elif split.path == _VERSION:
|
||||
self._serve_version()
|
||||
else:
|
||||
self._serve_static(split.path)
|
||||
|
||||
@ -132,6 +139,23 @@ class _Handler(BaseHTTPRequestHandler):
|
||||
return
|
||||
self._send(HTTPStatus.OK, body, "application/json")
|
||||
|
||||
def _serve_version(self) -> None:
|
||||
"""Send the bundle's build stamp, or a 404 when it has none.
|
||||
|
||||
Never the SPA fallback: a dashboard page at ``/version`` would make an
|
||||
unstamped build look like a live one to a deploy check.
|
||||
"""
|
||||
stamp = WEB_DIST / _VERSION_FILE
|
||||
if not stamp.is_file():
|
||||
logger.warning("No build stamp at %s; /version answers 404", stamp)
|
||||
self._send(
|
||||
HTTPStatus.NOT_FOUND,
|
||||
b"Build not stamped. Run: pnpm --dir web build",
|
||||
"text/plain",
|
||||
)
|
||||
return
|
||||
self._send(HTTPStatus.OK, stamp.read_bytes().strip(), "text/plain")
|
||||
|
||||
def _serve_static(self, path: str) -> None:
|
||||
"""Serve a file from ``WEB_DIST`` with SPA fallback and traversal guard.
|
||||
|
||||
|
||||
@ -79,3 +79,30 @@ class TestStaticServing:
|
||||
status, body, _ = _get(port, "/")
|
||||
assert status == 404
|
||||
assert b"npm --prefix web run build" in body
|
||||
|
||||
|
||||
class TestVersionRoute:
|
||||
"""/version names the deployed build, and never fakes one."""
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def _network(self, real_loopback: None) -> None:
|
||||
"""Every test in this class talks to the server over real HTTP."""
|
||||
|
||||
def test_the_stamped_hash_is_served_as_plain_text(self, tmp_path: Path) -> None:
|
||||
"""The stamp's hash comes back trimmed, as text/plain."""
|
||||
dist = _make_dist(tmp_path)
|
||||
(dist / "version.txt").write_text("abc1234\n", encoding="utf-8")
|
||||
with patch(f"{_PKG}.WEB_DIST", dist), _running() as port:
|
||||
status, body, ctype = _get(port, "/version")
|
||||
assert status == 200
|
||||
assert body == b"abc1234"
|
||||
assert ctype.startswith("text/plain")
|
||||
|
||||
def test_an_unstamped_build_is_a_404_not_the_dashboard(
|
||||
self, tmp_path: Path
|
||||
) -> None:
|
||||
"""No stamp means 404, so an unstamped build never looks live."""
|
||||
with patch(f"{_PKG}.WEB_DIST", _make_dist(tmp_path)), _running() as port:
|
||||
status, body, _ = _get(port, "/version")
|
||||
assert status == 404
|
||||
assert b"INDEX" not in body
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "astro dev",
|
||||
"build": "astro check && astro build",
|
||||
"build": "astro check && astro build && node stamp-version.mjs",
|
||||
"lint": "eslint .",
|
||||
"typecheck": "astro check",
|
||||
"preview": "astro preview",
|
||||
@ -27,13 +27,13 @@
|
||||
"@testing-library/jest-dom": "7.0.1",
|
||||
"@testing-library/user-event": "14.6.12",
|
||||
"@types/node": "26.6.6",
|
||||
"@vitest/coverage-v8": "5.1.0",
|
||||
"@vitest/coverage-v8": "5.1.1",
|
||||
"eslint": "10.15.0",
|
||||
"eslint-plugin-astro": "3.2.1",
|
||||
"globals": "17.14.0",
|
||||
"jsdom": "31.0.1",
|
||||
"typescript": "6.0.3",
|
||||
"typescript-eslint": "8.73.0",
|
||||
"vitest": "5.1.0"
|
||||
"vitest": "5.1.1"
|
||||
}
|
||||
}
|
||||
|
||||
@ -199,7 +199,7 @@ importers:
|
||||
version: 10.4.2
|
||||
'@testing-library/jest-dom':
|
||||
specifier: 7.0.1
|
||||
version: 7.0.1(@testing-library/dom@10.4.2)(vitest@5.1.0)
|
||||
version: 7.0.1(@testing-library/dom@10.4.2)(vitest@5.1.1)
|
||||
'@testing-library/user-event':
|
||||
specifier: 14.6.12
|
||||
version: 14.6.12(@testing-library/dom@10.4.2)
|
||||
@ -207,8 +207,8 @@ importers:
|
||||
specifier: 26.6.6
|
||||
version: 26.6.6
|
||||
'@vitest/coverage-v8':
|
||||
specifier: 5.1.0
|
||||
version: 5.1.0(vitest@5.1.0)
|
||||
specifier: 5.1.1
|
||||
version: 5.1.1(vitest@5.1.1)
|
||||
eslint:
|
||||
specifier: 10.15.0
|
||||
version: 10.15.0
|
||||
@ -228,8 +228,8 @@ importers:
|
||||
specifier: 8.73.0
|
||||
version: 8.73.0(eslint@10.15.0)(typescript@6.0.3)
|
||||
vitest:
|
||||
specifier: 5.1.0
|
||||
version: 5.1.0(@types/node@26.6.6)(@vitest/coverage-v8@5.1.0)(jsdom@31.0.1)(vite@8.3.7(@types/node@26.6.6)(yaml@2.9.1))
|
||||
specifier: 5.1.1
|
||||
version: 5.1.1(@types/node@26.6.6)(@vitest/coverage-v8@5.1.1)(jsdom@31.0.1)(vite@8.3.7(@types/node@26.6.6)(yaml@2.9.1))
|
||||
|
||||
packages:
|
||||
|
||||
@ -1049,11 +1049,11 @@ packages:
|
||||
'@ungap/structured-clone@1.4.0':
|
||||
resolution: {integrity: sha512-1mEZtMKPM09vDmQt5y7YvmN2+DFTP7Tg0EWXdic8/C6VRnpb33e4ghisCIE3WZjsE2N8mf+QV1Zqh7ZFYLWInQ==}
|
||||
|
||||
'@vitest/coverage-v8@5.1.0':
|
||||
resolution: {integrity: sha512-OY9aShBQG5KOIw2zCo9fDoCHI9RHliNHJ//nM7m6jGg41Hyke9JOGWmeMlK74xYErsxMoK8mIB3gAeEyFVA7LA==}
|
||||
'@vitest/coverage-v8@5.1.1':
|
||||
resolution: {integrity: sha512-xL4NkbVx/UqqDHaXSOyvB8qsSoLQ+dVNGR9H2Bh/4ggQtmaPFNPUC9gZQwKZePLOQ6WDcgwXVnPulFW9eCHt0w==}
|
||||
peerDependencies:
|
||||
'@vitest/browser': 5.1.0
|
||||
vitest: 5.1.0
|
||||
'@vitest/browser': 5.1.1
|
||||
vitest: 5.1.1
|
||||
peerDependenciesMeta:
|
||||
'@vitest/browser':
|
||||
optional: true
|
||||
@ -1066,8 +1066,8 @@ packages:
|
||||
resolution: {integrity: sha512-+ZFta25nWtGCOBG9/IET8w1RO5Oawsd6H8Uf6AJ9EA/StFuKVtKAEJEJ6uRt84OH+T4YkI0ASmIHAhRA+0KVxw==}
|
||||
engines: {node: '>=22'}
|
||||
|
||||
'@vitest/mocker@5.1.0':
|
||||
resolution: {integrity: sha512-5umB9/jlWcqVS6fsrFmv5Gdnpu6wAUio/lEOwAmvB7v+lFo/IW2B9FhAD0p19GCULTgMzLCdUmuCDCnAzWgppQ==}
|
||||
'@vitest/mocker@5.1.1':
|
||||
resolution: {integrity: sha512-b2OgTB4+3ImWIVafXKazJXwLKwQxUNnix6b3xUvUTMh7RyXlrtzoxfZr8aEz9zkHOMAS4K5lu3TvFWoiVeVTQg==}
|
||||
peerDependencies:
|
||||
msw: ^2.4.9 || ^3.0.0
|
||||
vite: ^6.0.0 || ^7.0.0 || ^8.0.0
|
||||
@ -1077,8 +1077,8 @@ packages:
|
||||
vite:
|
||||
optional: true
|
||||
|
||||
'@vitest/spy@5.1.0':
|
||||
resolution: {integrity: sha512-2DPO5OMHWKrBngC+fCmTQqemNF0gidRhp3lxbBmRhW+M1XJrfgPpqNqWVevXtn2f2ufD5ZaR1B6B641aAlcX5A==}
|
||||
'@vitest/spy@5.1.1':
|
||||
resolution: {integrity: sha512-6K8C6alQG1/Yc8/ZI22iJS7JEsxPdVzqtqDq+/xM4Z0mGVykemMVjLhqMy/q6/IHYG3kKD8hKSwRen/ZdyI9vQ==}
|
||||
|
||||
'@volar/kit@2.4.30':
|
||||
resolution: {integrity: sha512-VocLtumfXnqnPQEWc9Yx7uC1uOh9gyjVNlrn2+6Zx/z9aM6yTLhPK8lSqM9Ilu81XRa48I00lcYJWLi2BLxx8Q==}
|
||||
@ -1771,9 +1771,6 @@ packages:
|
||||
resolution: {integrity: sha512-h5bgJWpxJNswbU7qCrV0tIKQCaS3blPDrqKWx+QxzuzL1zGUzij9XCWLrSLsJPu5t+eWA/ycetzYAO5IOMcWAQ==}
|
||||
hasBin: true
|
||||
|
||||
magic-string@1.4.3:
|
||||
resolution: {integrity: sha512-z12OxmaPGE0F4xlpGdjuAUckipLpQlTAuAmyGhNoD7ODpYUzvDfvtxUbjM/jwznyP178oju/KDdyi220wNJ0RQ==}
|
||||
|
||||
magic-string@1.4.5:
|
||||
resolution: {integrity: sha512-oLLC2+YvQUfAa+vxoufE/yc1icb87M9iXIRqAoR/2EhE/8cb3SF/+Gi0c2By1B+5Vz2e2vQtUvHiu1+63Znh+w==}
|
||||
|
||||
@ -2094,8 +2091,8 @@ packages:
|
||||
tiny-inflate@1.0.3:
|
||||
resolution: {integrity: sha512-pkY1fj1cKHb2seWDy0B16HeWyczlJA9/WW3u3c4z/NiWDsO3DOU5D7nhTLE9CF0yXv/QZFY7sEJmj24dK+Rrqw==}
|
||||
|
||||
tinybench@6.2.2:
|
||||
resolution: {integrity: sha512-nIZ2N2dFpjBsg2hgitKu7mA+FvsIJuGJOwysRngmVRRmWgR/UyPjwJj8c/fT/dhm8EkHnF5EOogRf+0Z2qrimA==}
|
||||
tinybench@6.2.3:
|
||||
resolution: {integrity: sha512-Al01C2JIBnO2CXCAPbpAl6sUiOZHeThGtHYNTa51JRw9PyWOCdr+9Ong05R4qg/Sk3+H645E8ov1h2c58ityNw==}
|
||||
engines: {node: '>=20.0.0'}
|
||||
|
||||
tinyclip@1.0.3:
|
||||
@ -2335,20 +2332,20 @@ packages:
|
||||
vite:
|
||||
optional: true
|
||||
|
||||
vitest@5.1.0:
|
||||
resolution: {integrity: sha512-MzmNFnrwa8v8lF1bLxw3UJ4EREwVOVb75VqJZmUDRjJ6EluK/x9th7qTbaeDk41WxE1GxWqmTXcdsfobfH5sOA==}
|
||||
vitest@5.1.1:
|
||||
resolution: {integrity: sha512-trVmzUFOSraCZcI3uZIMD5FqBh4atFkC/XuJP/IU7qtyQay0FCs3Ia+0QiQ2JJwGyWG/xrcBoJNel2+O7kEZ0g==}
|
||||
engines: {node: ^22.12.0 || ^24.0.0 || >=26.0.0}
|
||||
hasBin: true
|
||||
peerDependencies:
|
||||
'@edge-runtime/vm': '*'
|
||||
'@opentelemetry/api': ^1.9.0
|
||||
'@types/node': ^22.0.0 || >=24.0.0
|
||||
'@vitest/browser-playwright': 5.1.0
|
||||
'@vitest/browser-preview': 5.1.0
|
||||
'@vitest/browser-playwright': 5.1.1
|
||||
'@vitest/browser-preview': 5.1.1
|
||||
'@vitest/browser-webdriverio': ^5.0.0-beta.5 || >=5.0.0
|
||||
'@vitest/coverage-istanbul': 5.1.0
|
||||
'@vitest/coverage-v8': 5.1.0
|
||||
'@vitest/ui': 5.1.0
|
||||
'@vitest/coverage-istanbul': 5.1.1
|
||||
'@vitest/coverage-v8': 5.1.1
|
||||
'@vitest/ui': 5.1.1
|
||||
happy-dom: '*'
|
||||
jsdom: '*'
|
||||
vite: ^6.4.0 || ^7.0.0 || ^8.0.0
|
||||
@ -3164,7 +3161,7 @@ snapshots:
|
||||
picocolors: 1.1.1
|
||||
pretty-format: 27.5.1
|
||||
|
||||
'@testing-library/jest-dom@7.0.1(@testing-library/dom@10.4.2)(vitest@5.1.0)':
|
||||
'@testing-library/jest-dom@7.0.1(@testing-library/dom@10.4.2)(vitest@5.1.1)':
|
||||
dependencies:
|
||||
'@adobe/css-tools': 4.5.0
|
||||
'@testing-library/dom': 10.4.2
|
||||
@ -3174,7 +3171,7 @@ snapshots:
|
||||
picocolors: 1.1.1
|
||||
redent: 3.0.0
|
||||
optionalDependencies:
|
||||
vitest: 5.1.0(@types/node@26.6.6)(@vitest/coverage-v8@5.1.0)(jsdom@31.0.1)(vite@8.3.7(@types/node@26.6.6)(yaml@2.9.1))
|
||||
vitest: 5.1.1(@types/node@26.6.6)(@vitest/coverage-v8@5.1.1)(jsdom@31.0.1)(vite@8.3.7(@types/node@26.6.6)(yaml@2.9.1))
|
||||
|
||||
'@testing-library/user-event@14.6.12(@testing-library/dom@10.4.2)':
|
||||
dependencies:
|
||||
@ -3330,7 +3327,7 @@ snapshots:
|
||||
|
||||
'@ungap/structured-clone@1.4.0': {}
|
||||
|
||||
'@vitest/coverage-v8@5.1.0(vitest@5.1.0)':
|
||||
'@vitest/coverage-v8@5.1.1(vitest@5.1.1)':
|
||||
dependencies:
|
||||
'@bcoe/v8-coverage': 1.0.2
|
||||
'@vitest/istanbul-lib-coverage': 2.1.0
|
||||
@ -3340,7 +3337,7 @@ snapshots:
|
||||
obug: 3.0.0
|
||||
std-env: 4.3.0
|
||||
tinyrainbow: 3.2.0
|
||||
vitest: 5.1.0(@types/node@26.6.6)(@vitest/coverage-v8@5.1.0)(jsdom@31.0.1)(vite@8.3.7(@types/node@26.6.6)(yaml@2.9.1))
|
||||
vitest: 5.1.1(@types/node@26.6.6)(@vitest/coverage-v8@5.1.1)(jsdom@31.0.1)(vite@8.3.7(@types/node@26.6.6)(yaml@2.9.1))
|
||||
|
||||
'@vitest/istanbul-lib-coverage@2.1.0': {}
|
||||
|
||||
@ -3348,16 +3345,16 @@ snapshots:
|
||||
dependencies:
|
||||
'@vitest/istanbul-lib-coverage': 2.1.0
|
||||
|
||||
'@vitest/mocker@5.1.0(vite@8.3.7(@types/node@26.6.6)(yaml@2.9.1))':
|
||||
'@vitest/mocker@5.1.1(vite@8.3.7(@types/node@26.6.6)(yaml@2.9.1))':
|
||||
dependencies:
|
||||
'@jridgewell/trace-mapping': 0.3.31
|
||||
'@vitest/spy': 5.1.0
|
||||
'@vitest/spy': 5.1.1
|
||||
estree-walker: 3.0.3
|
||||
magic-string: 1.4.3
|
||||
magic-string: 1.4.5
|
||||
optionalDependencies:
|
||||
vite: 8.3.7(@types/node@26.6.6)(yaml@2.9.1)
|
||||
|
||||
'@vitest/spy@5.1.0': {}
|
||||
'@vitest/spy@5.1.1': {}
|
||||
|
||||
'@volar/kit@2.4.30(typescript@6.0.3)':
|
||||
dependencies:
|
||||
@ -4098,10 +4095,6 @@ snapshots:
|
||||
|
||||
lz-string@1.5.0: {}
|
||||
|
||||
magic-string@1.4.3:
|
||||
dependencies:
|
||||
'@jridgewell/sourcemap-codec': 1.6.0
|
||||
|
||||
magic-string@1.4.5:
|
||||
dependencies:
|
||||
'@jridgewell/sourcemap-codec': 1.6.0
|
||||
@ -4466,7 +4459,7 @@ snapshots:
|
||||
|
||||
tiny-inflate@1.0.3: {}
|
||||
|
||||
tinybench@6.2.2: {}
|
||||
tinybench@6.2.3: {}
|
||||
|
||||
tinyclip@1.0.3: {}
|
||||
|
||||
@ -4624,25 +4617,25 @@ snapshots:
|
||||
optionalDependencies:
|
||||
vite: 8.3.7(@types/node@26.6.6)(yaml@2.9.1)
|
||||
|
||||
vitest@5.1.0(@types/node@26.6.6)(@vitest/coverage-v8@5.1.0)(jsdom@31.0.1)(vite@8.3.7(@types/node@26.6.6)(yaml@2.9.1)):
|
||||
vitest@5.1.1(@types/node@26.6.6)(@vitest/coverage-v8@5.1.1)(jsdom@31.0.1)(vite@8.3.7(@types/node@26.6.6)(yaml@2.9.1)):
|
||||
dependencies:
|
||||
'@types/chai': 5.2.3
|
||||
'@vitest/mocker': 5.1.0(vite@8.3.7(@types/node@26.6.6)(yaml@2.9.1))
|
||||
'@vitest/mocker': 5.1.1(vite@8.3.7(@types/node@26.6.6)(yaml@2.9.1))
|
||||
chai: 6.3.0
|
||||
es-module-lexer: 2.3.2
|
||||
expect-type: 1.4.0
|
||||
magic-string: 1.4.3
|
||||
magic-string: 1.4.5
|
||||
obug: 3.0.0
|
||||
picomatch: 4.0.7
|
||||
std-env: 4.3.0
|
||||
tinybench: 6.2.2
|
||||
tinybench: 6.2.3
|
||||
tinyexec: 1.3.1
|
||||
tinyglobby: 0.2.17
|
||||
vite: 8.3.7(@types/node@26.6.6)(yaml@2.9.1)
|
||||
why-is-node-running: 3.2.1
|
||||
optionalDependencies:
|
||||
'@types/node': 26.6.6
|
||||
'@vitest/coverage-v8': 5.1.0(vitest@5.1.0)
|
||||
'@vitest/coverage-v8': 5.1.1(vitest@5.1.1)
|
||||
jsdom: 31.0.1
|
||||
transitivePeerDependencies:
|
||||
- msw
|
||||
|
||||
24
web/stamp-version.mjs
Normal file
24
web/stamp-version.mjs
Normal file
@ -0,0 +1,24 @@
|
||||
// Stamp the built bundle with the commit it was built from.
|
||||
//
|
||||
// Runs after `astro build` and writes `git rev-parse --short HEAD` to
|
||||
// dist/version.txt. The Python server serves that file at /version, so a live
|
||||
// check can compare the running build against HEAD; with no stamp it answers
|
||||
// 404, so a build that skipped this step can never pass for live.
|
||||
import { execFileSync } from 'node:child_process'
|
||||
import { existsSync, writeFileSync } from 'node:fs'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
const dist = fileURLToPath(new URL('./dist/', import.meta.url))
|
||||
if (!existsSync(dist)) {
|
||||
// Fail loudly: a stamp with no bundle beside it would name a build that
|
||||
// does not exist.
|
||||
throw new Error(`stamp-version: ${dist} is missing; run astro build first`)
|
||||
}
|
||||
|
||||
// execFileSync throws (non-zero exit) when git fails, which fails the build.
|
||||
const hash = execFileSync('git', ['rev-parse', '--short', 'HEAD'], {
|
||||
cwd: fileURLToPath(new URL('.', import.meta.url)),
|
||||
encoding: 'utf8',
|
||||
}).trim()
|
||||
writeFileSync(`${dist}version.txt`, `${hash}\n`)
|
||||
console.log(`stamp-version: dist/version.txt = ${hash}`)
|
||||
Loading…
Reference in New Issue
Block a user