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

Migrated from github.com/Trevato/SO-ARM100

workbenchthis machine

Relay's POST /admin actions (save, mod-add, mod-remove, pause, resume, end, take) are handled in src/admin-routes.ts - no behaviour change #169

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/122-relay-s-post-admin-actions-save-mod-add → main 1 commit fork

  • Merged. smith/122-relay-s-post-admin-actions-save-mod-add was merged into main as 4dbea7b.

Review

Sign in to review this change.
2 files changed +68 −60
Software/so101/src/admin-routes.ts +66 −1
@@ -1,7 +1,8 @@
1 /** GET /admin: the owner's page and the refusals. The POST actions stay with the relay, which is handed what they need. */
1 /** /admin: the owner's page, the refusals, and the form actions. */
22 import type { IncomingMessage, ServerResponse } from "node:http";
33 import type { Person } from "./identity.ts";
44 import type { Lobby } from "./lobby.ts";
5 import { AdminError, changeFromForm } from "./admin.ts";
56 import { ADMIN_CSP, DONE, renderAdmin, renderRefusal, type Done } from "./admin-page.ts";
67
78 /** What a POST needs once the caller is known to be allowed on the page. */
@@ -39,3 +40,67 @@ export function serveAdminPage(lobby: Lobby, req: IncomingMessage, res: ServerRe
3940 }
4041 return { who, canEdit, show, refuse: (status) => html(status, renderRefusal(who)) };
4142 }
43
44 /** The largest form the page may post. */
45 const MAX_FORM = 16_384;
46
47 /** Reads a request body, refusing one over the limit. */
48 type ReadBody = (req: IncomingMessage, limit?: number) => Promise<Buffer>;
49
50 /** The owner's page. Anyone else is told so; a change is a form post, answered with a redirect back. */
51 export async function handleAdmin(lobby: Lobby, req: IncomingMessage, res: ServerResponse, url: URL, readBody: ReadBody): Promise<void> {
52 const session = serveAdminPage(lobby, req, res, url);
53 if (!session) return;
54 const { who, canEdit, show } = session;
55 if (req.method !== "POST") {
56 res.writeHead(405, { "content-type": "application/json", "cache-control": "no-store" });
57 res.end(JSON.stringify({ error: "GET or POST" }));
58 return;
59 }
60 if (!canEdit) return session.refuse(403);
61 let body: Buffer;
62 try {
63 body = await readBody(req, MAX_FORM);
64 } catch {
65 res.setHeader("connection", "close");
66 return show(413, null, "that form is too large");
67 }
68 const form = new URLSearchParams(body.toString("utf8"));
69 let done: Done;
70 try {
71 switch (form.get("action")) {
72 case "save":
73 lobby.configure(who, changeFromForm(form));
74 done = "saved";
75 break;
76 case "mod-add":
77 case "mod-remove": {
78 const name = (form.get("name") ?? "").trim().toLowerCase();
79 const now = lobby.admin.settings.moderators;
80 lobby.configure(who, { moderators: form.get("action") === "mod-add" ? [...now, name] : now.filter((m) => m !== name) });
81 done = "saved";
82 break;
83 }
84 case "pause":
85 case "resume":
86 lobby.configure(who, { paused: form.get("action") === "pause" });
87 done = form.get("action") === "pause" ? "paused" : "resumed";
88 break;
89 case "end":
90 lobby.endTurn(who);
91 done = "ended";
92 break;
93 case "take":
94 lobby.takeControl(who);
95 done = "taken";
96 break;
97 default:
98 throw new AdminError("no such action");
99 }
100 } catch (err) {
101 if (err instanceof AdminError) return show(400, null, err.message);
102 throw err;
103 }
104 res.writeHead(303, { location: `/admin?done=${done}`, "cache-control": "no-store" });
105 res.end();
106 }
Software/so101/src/relay.ts +2 −59
@@ -61,9 +61,7 @@ import { perServo } from "./machine-log.ts";
6161 import { parseFrame } from "./safety.ts";
6262 import { acceptsGzip, GzipCache, worthCompressing } from "./compress.ts";
6363 import { AccountError, sessionCookie, visitor } from "./accounts.ts";
64 import { AdminError, changeFromForm } from "./admin.ts";
65 import type { Done } from "./admin-page.ts";
66 import { serveAdminPage } from "./admin-routes.ts";
64 import { handleAdmin } from "./admin-routes.ts";
6765 import { parseFps, type FeedOptions } from "./camera-feed.ts";
6866 import { CameraError, CameraHub } from "./cameras.ts";
6967 import { LOCAL as LOCAL_PERSON } from "./identity.ts";
@@ -136,7 +134,6 @@ const MAX_BODY = 1_000_000;
136134 const CAMERA = /^\/camera\/(\d{1,9})\/(stream|state|snapshot)$/;
137135 const ADMIN_CAMERA = /^\/admin\/cameras(?:\/(\d{1,9}))?$/;
138136 const CAMERA_PAGE = readFileSync(new URL("./camera-list.html", import.meta.url), "utf8");
139 const MAX_FORM = 16_384;
140137 /** Served from this process whichever arm is live: they describe the app, not the arm. */
141138 const LOCAL = /^\/(?:index\.html|leader-map\.js)?$|^\/(?:model|vendor|printables|ui|app)(?:\/|$)/;
142139 /** The page itself, which the relay writes the first view's facts into. */
@@ -462,60 +459,6 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> {
462459 return json(res, 405, { error: "GET or POST /admin/cameras, DELETE /admin/cameras/<id>" });
463460 };
464461
465 /** The owner's page. Anyone else is told so; a change is a form post, answered with a redirect back. */
466 const admin = async (lobby: Lobby, req: IncomingMessage, res: ServerResponse, url: URL) => {
467 const session = serveAdminPage(lobby, req, res, url);
468 if (!session) return;
469 const { who, canEdit, show } = session;
470 if (req.method !== "POST") return json(res, 405, { error: "GET or POST" });
471 if (!canEdit) return session.refuse(403);
472 let body: Buffer;
473 try {
474 body = await readBody(req, MAX_FORM);
475 } catch {
476 res.setHeader("connection", "close");
477 return show(413, null, "that form is too large");
478 }
479 const form = new URLSearchParams(body.toString("utf8"));
480 let done: Done;
481 try {
482 switch (form.get("action")) {
483 case "save":
484 lobby.configure(who, changeFromForm(form));
485 done = "saved";
486 break;
487 case "mod-add":
488 case "mod-remove": {
489 const name = (form.get("name") ?? "").trim().toLowerCase();
490 const now = lobby.admin.settings.moderators;
491 lobby.configure(who, { moderators: form.get("action") === "mod-add" ? [...now, name] : now.filter((m) => m !== name) });
492 done = "saved";
493 break;
494 }
495 case "pause":
496 case "resume":
497 lobby.configure(who, { paused: form.get("action") === "pause" });
498 done = form.get("action") === "pause" ? "paused" : "resumed";
499 break;
500 case "end":
501 lobby.endTurn(who);
502 done = "ended";
503 break;
504 case "take":
505 lobby.takeControl(who);
506 done = "taken";
507 break;
508 default:
509 throw new AdminError("no such action");
510 }
511 } catch (err) {
512 if (err instanceof AdminError) return show(400, null, err.message);
513 throw err;
514 }
515 res.writeHead(303, { location: `/admin?done=${done}`, "cache-control": "no-store" });
516 res.end();
517 };
518
519462 /** A person's own calibration: signed in is enough, it never touches the arm. */
520463 const leaderCalibration = async (req: IncomingMessage, res: ServerResponse) => {
521464 const who = lobby ? lobby.who(req) : LOCAL_PERSON;
@@ -595,7 +538,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> {
595538 }
596539 const cameraAdmin = ADMIN_CAMERA.exec(url.pathname);
597540 if (cameraAdmin) return await administer(req, res, cameraAdmin[1] === undefined ? null : Number(cameraAdmin[1]));
598 if (lobby && url.pathname === "/admin") return await admin(lobby, req, res, url);
541 if (lobby && url.pathname === "/admin") return await handleAdmin(lobby, req, res, url, readBody);
599542 if (url.pathname === LEADER) return await leaderCalibration(req, res);
600543 if (url.pathname === "/join") {
601544 if (req.method === "POST") return await join(req, res);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs