Public: anyone can read this; the crew can change it.

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

The owner sets the table height (tableOffsetMm, -100..100 mm) in a field on /admin, saved through PUT /settings by the page's one hashed script #332

Merged opened this

Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.

Merged

smith/arm:smith/325-the-owner-sets-the-table-height-tableoff → main 1 commit fork

  • Merged. smith/325-the-owner-sets-the-table-height-tableoff was merged into main as 010a497.

Review

Sign in to review this change.
4 files changed +78 −8
Software/README.md +3 −1
@@ -547,9 +547,11 @@ characters removed, so a chat line cannot drive your terminal. Locally,
547547 `PORT=8102 bun run Software/so101/src/tty.ts` beside `fake-serve.ts`
548548 reaches it as the owner.
549549
550 **Admin.** The owner runs the room from `/admin` (plain forms, no script),
550 **Admin.** The owner runs the room from `/admin` (plain forms, and one pinned script for Health and the table height),
551551 or from the **Admin** tab that only the owner sees:
552552
553 - **Table height** (-100..100 mm, `tableOffsetMm`) is a number field under Table height on `/admin`:
554 Save sends `PUT /settings` and shows the saved value or the server's error.
553555 - **Pause the queue** for maintenance. The driver's turn ends (unless it is
554556 the owner's), nobody new gets a turn, and everyone sees "Paused for
555557 maintenance". People can still join the queue, and it keeps its order.
Software/so101/src/admin-page.test.ts added +32
@@ -0,0 +1,32 @@
1 import { createHash } from "node:crypto";
2 import { expect, test } from "bun:test";
3 import { adminCsp, renderAdmin } from "./admin-page.ts";
4 import { Admin } from "./admin.ts";
5 import { Turns } from "./turns.ts";
6
7 const page = (canEdit: boolean) => renderAdmin({
8 who: { id: "o", name: "owner", owner: canEdit }, canEdit, settings: new Admin().settings, snapshot: new Turns().snapshot(), log: [],
9 });
10
11 for (const canEdit of [true, false]) {
12 test(`${canEdit ? "owner" : "moderator"}: one script, pinned by the CSP`, () => {
13 const html = page(canEdit);
14 expect(html.match(/<script/g)?.length).toBe(1);
15 const code = /<script>([\s\S]*?)<\/script>/.exec(html)![1]!;
16 const hash = createHash("sha256").update(code).digest("base64");
17 const csp = adminCsp(canEdit);
18 expect(csp).toContain(`script-src 'sha256-${hash}'`);
19 expect(csp).not.toContain("unsafe-inline");
20 expect(code.includes("PUT")).toBe(canEdit);
21 });
22 }
23
24 test("the Table height field is the owner's alone", () => {
25 const owner = page(true);
26 expect(owner).toContain('id="table-mm"');
27 expect(owner).toContain('min="-100" max="100" step="1"');
28 expect(owner).toContain('id="table-save"');
29 const mod = page(false);
30 expect(mod).not.toContain("table-mm");
31 expect(mod).not.toContain("tableOffsetMm");
32 });
Software/so101/src/admin-page.ts +39 −3
@@ -83,7 +83,38 @@ async function tick() {
8383 setInterval(tick, 2000);
8484 `;
8585
86 export const ADMIN_CSP = `default-src 'none'; style-src 'sha256-${createHash("sha256").update(STYLE).digest("base64")}'; script-src 'sha256-${createHash("sha256").update(HEALTH_SCRIPT).digest("base64")}'; connect-src 'self'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'`;
86 /** The owner's Table height field: loads GET /settings, saves PUT /settings, shows the saved value or the server's error as text. */
87 const TABLE_SCRIPT = `
88 {
89 const input = document.getElementById("table-mm");
90 const note = document.getElementById("table-note");
91 const say = (text) => { note.textContent = text; };
92 const settings = async (init) => {
93 const r = await fetch("/settings", { cache: "no-store", credentials: "same-origin", ...init });
94 const data = await r.json().catch(() => ({}));
95 if (!r.ok) throw new Error(typeof data.error === "string" ? data.error : "HTTP " + r.status);
96 return data;
97 };
98 settings().then((s) => { input.value = String(s.tableOffsetMm); }).catch((e) => say(e.message));
99 document.getElementById("table-save").addEventListener("click", async () => {
100 if (!input.reportValidity()) return;
101 try {
102 const s = await settings({ method: "PUT", headers: { "content-type": "application/json" }, body: JSON.stringify({ tableOffsetMm: Number(input.value) }) });
103 input.value = String(s.tableOffsetMm);
104 say("Saved: " + s.tableOffsetMm + " mm.");
105 } catch (e) { say(e.message); }
106 });
107 }
108 `;
109
110 /** The page's one script: the Health refresh, and for the owner the table height saver. */
111 const scriptFor = (canEdit: boolean): string => (canEdit ? HEALTH_SCRIPT + TABLE_SCRIPT : HEALTH_SCRIPT);
112
113 const sha = (text: string) => createHash("sha256").update(text).digest("base64");
114
115 /** The page's Content-Security-Policy: its style and its one script, each pinned by hash, and nothing else. */
116 export const adminCsp = (canEdit: boolean): string =>
117 `default-src 'none'; style-src 'sha256-${sha(STYLE)}'; script-src 'sha256-${sha(scriptFor(canEdit))}'; connect-src 'self'; form-action 'self'; frame-ancestors 'none'; base-uri 'none'`;
87118
88119 /** The fixed lines a redirect back may ask for; nothing from the request is echoed. */
89120 export const DONE = { saved: "Saved.", paused: "Paused: nobody new gets a turn.", resumed: "Open again: the next in line has control.", ended: "Turn ended.", taken: "You have control.", restored: "Line restored." } as const;
@@ -104,7 +135,7 @@ export interface AdminPage {
104135 error?: string | null;
105136 }
106137
107 const page = (title: string, body: string) => `<!doctype html>
138 const page = (title: string, body: string, canEdit = false) => `<!doctype html>
108139 <html lang="en">
109140 <head>
110141 <meta charset="utf-8">
@@ -117,7 +148,7 @@ const page = (title: string, body: string) => `<!doctype html>
117148 <main>
118149 ${body}
119150 </main>
120 <script>${HEALTH_SCRIPT}</script>
151 <script>${scriptFor(canEdit)}</script>
121152 </body>
122153 </html>
123154 `;
@@ -193,10 +224,15 @@ ${field("slowModeSeconds", "Slow mode", "seconds between a person's chat message
193224 ${p.canEdit ? `<div class="save"><button class="go">Save settings</button></div>` : ""}
194225 </form>
195226 </section>
227 ${p.canEdit ? `<section aria-labelledby="table"><h2 id="table">Table height</h2>
228 <div class="grid"><label>Table height<small>mm above the model's floor, -100 to 100; the safe zone keeps the arm above it</small><input id="table-mm" type="number" inputmode="numeric" min="-100" max="100" step="1"></label></div>
229 <div class="save"><button type="button" class="go" id="table-save">Save</button> <span id="table-note" role="status"></span></div>
230 </section>` : ""}
196231 <section aria-labelledby="health"><h2 id="health">Health</h2>${health(p.health ?? [])}</section>
197232 <section aria-labelledby="mods"><h2 id="mods">Moderators</h2>${mods}</section>
198233 <section aria-labelledby="moderation"><h2 id="moderation">Moderation</h2>${actions}</section>
199234 <section aria-labelledby="removed"><h2 id="removed">Removed lines</h2>${removedLines(p.removed ?? [], p.canEdit)}</section>
200235 <section aria-labelledby="log"><h2 id="log">Log</h2>${log}</section>`,
236 p.canEdit,
201237 );
202238 }
Software/so101/src/admin-routes.ts +4 −4
@@ -3,7 +3,7 @@ import type { IncomingMessage, ServerResponse } from "node:http";
33 import type { Person } from "./identity.ts";
44 import type { Lobby } from "./lobby.ts";
55 import { AdminError, changeFromForm } from "./admin.ts";
6 import { ADMIN_CSP, DONE, renderAdmin, renderRefusal, type Done } from "./admin-page.ts";
6 import { adminCsp, DONE, renderAdmin, renderRefusal, type Done } from "./admin-page.ts";
77
88 /** What a POST needs once the caller is known to be allowed on the page. */
99 export interface AdminSession {
@@ -21,8 +21,8 @@ export interface AdminSession {
2121 */
2222 export function serveAdminPage(lobby: Lobby, req: IncomingMessage, res: ServerResponse, url: URL): AdminSession | null {
2323 const who = lobby.who(req);
24 const html = (status: number, text: string) => {
25 res.writeHead(status, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store", "content-security-policy": ADMIN_CSP });
24 const html = (status: number, text: string, edit = false) => {
25 res.writeHead(status, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store", "content-security-policy": adminCsp(edit) });
2626 res.end(text);
2727 };
2828 const canEdit = Boolean(who?.owner);
@@ -44,7 +44,7 @@ export function serveAdminPage(lobby: Lobby, req: IncomingMessage, res: ServerRe
4444 return null;
4545 }
4646 const show = (status: number, done: Done | null, error: string | null) =>
47 html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, removed: lobby.chat.removed, health: lobby.reports.rows(), done, error }));
47 html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, removed: lobby.chat.removed, health: lobby.reports.rows(), done, error }), canEdit);
4848 if (req.method === "GET" || req.method === "HEAD") {
4949 const done = url.searchParams.get("done");
5050 show(200, done && Object.hasOwn(DONE, done) ? (done as Done) : null, null);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs