Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
Mobile portrait shell: video pinned on top with no layout shift, one status strip, a bottom tab bar Drive / Queue / Chat #108
quill/arm:quill/90-mobile-portrait-shell-video-pinned-on-to → main 2 commits fork
2 files
changed +80
−70
Software/so101/src/admin-routes.ts added +77
| @@ -0,0 +1,77 @@ | ||
| 1 | import type { IncomingMessage, ServerResponse } from "node:http"; | |
| 2 | import { AdminError, changeFromForm } from "./admin.ts"; | |
| 3 | import { ADMIN_CSP, DONE, renderAdmin, renderRefusal, type Done } from "./admin-page.ts"; | |
| 4 | import type { Lobby } from "./lobby.ts"; | |
| 5 | ||
| 6 | const MAX_FORM = 16_384; | |
| 7 | ||
| 8 | /** What the relay lends the routes: its JSON answer and its size-limited body reader. */ | |
| 9 | export interface AdminIo { | |
| 10 | json: (res: ServerResponse, status: number, body: unknown) => void; | |
| 11 | readBody: (req: IncomingMessage, limit?: number) => Promise<Buffer>; | |
| 12 | } | |
| 13 | ||
| 14 | /** The owner's page. Anyone else is told so; a change is a form post, answered with a redirect back. */ | |
| 15 | export const adminRoutes = async (lobby: Lobby, req: IncomingMessage, res: ServerResponse, url: URL, { json, readBody }: AdminIo) => { | |
| 16 | const who = lobby.who(req); | |
| 17 | const html = (status: number, text: string) => { | |
| 18 | res.writeHead(status, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store", "content-security-policy": ADMIN_CSP }); | |
| 19 | res.end(text); | |
| 20 | }; | |
| 21 | const canEdit = Boolean(who?.owner); | |
| 22 | // a moderator is named by forge username and must hold that forge identity: a guest who types the same name is not them | |
| 23 | if (!who || !(canEdit || (lobby.verified(req)?.id === who.id && lobby.admin.isModerator(who.name)))) return html(who ? 403 : 401, renderRefusal(who)); | |
| 24 | const show = (status: number, done: Done | null, error: string | null) => | |
| 25 | html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, done, error })); | |
| 26 | if (req.method === "GET" || req.method === "HEAD") { | |
| 27 | const done = url.searchParams.get("done"); | |
| 28 | return show(200, done && Object.hasOwn(DONE, done) ? (done as Done) : null, null); | |
| 29 | } | |
| 30 | if (req.method !== "POST") return json(res, 405, { error: "GET or POST" }); | |
| 31 | if (!canEdit) return html(403, renderRefusal(who)); | |
| 32 | let body: Buffer; | |
| 33 | try { | |
| 34 | body = await readBody(req, MAX_FORM); | |
| 35 | } catch { | |
| 36 | res.setHeader("connection", "close"); | |
| 37 | return show(413, null, "that form is too large"); | |
| 38 | } | |
| 39 | const form = new URLSearchParams(body.toString("utf8")); | |
| 40 | let done: Done; | |
| 41 | try { | |
| 42 | switch (form.get("action")) { | |
| 43 | case "save": | |
| 44 | lobby.configure(who, changeFromForm(form)); | |
| 45 | done = "saved"; | |
| 46 | break; | |
| 47 | case "mod-add": | |
| 48 | case "mod-remove": { | |
| 49 | const name = (form.get("name") ?? "").trim().toLowerCase(); | |
| 50 | const now = lobby.admin.settings.moderators; | |
| 51 | lobby.configure(who, { moderators: form.get("action") === "mod-add" ? [...now, name] : now.filter((m) => m !== name) }); | |
| 52 | done = "saved"; | |
| 53 | break; | |
| 54 | } | |
| 55 | case "pause": | |
| 56 | case "resume": | |
| 57 | lobby.configure(who, { paused: form.get("action") === "pause" }); | |
| 58 | done = form.get("action") === "pause" ? "paused" : "resumed"; | |
| 59 | break; | |
| 60 | case "end": | |
| 61 | lobby.endTurn(who); | |
| 62 | done = "ended"; | |
| 63 | break; | |
| 64 | case "take": | |
| 65 | lobby.takeControl(who); | |
| 66 | done = "taken"; | |
| 67 | break; | |
| 68 | default: | |
| 69 | throw new AdminError("no such action"); | |
| 70 | } | |
| 71 | } catch (err) { | |
| 72 | if (err instanceof AdminError) return show(400, null, err.message); | |
| 73 | throw err; | |
| 74 | } | |
| 75 | res.writeHead(303, { location: `/admin?done=${done}`, "cache-control": "no-store" }); | |
| 76 | res.end(); | |
| 77 | }; |
Software/so101/src/relay.ts +3 −70
| @@ -35,7 +35,7 @@ | ||
| 35 | 35 | * command in and out with every safety clamp, pushed as it happens (src/machine-feed.ts) |
| 36 | 36 | * POST /join {"name": "…"}: become someone here, no forge account needed (src/accounts.ts); the answer |
| 37 | 37 | * sets the HttpOnly session cookie and names you: {"you": {"id", "name", "owner"}} |
| 38 | * GET /admin, POST /admin the owner's page: pause, end a turn, take control, the settings, the log | |
| 38 | * GET /admin, POST /admin the owner's page: pause, end a turn, take control, the settings, the log (src/admin-routes.ts) | |
| 39 | 39 | * GET /link {"mode": "live"|"reconnecting"|"offline"|"simulated", "host": "<host:port>"|null, "problem": "…"|null} |
| 40 | 40 | * GET /status the arm's own /status, and which arm said it: "source": "live <host:port>", "reconnecting to the arm: |
| 41 | 41 | * <why>" (the last known status, with "reconnecting": true and "seenAt"; 503 before |
| @@ -61,8 +61,7 @@ import { perServo } from "./machine-log.ts"; | ||
| 61 | 61 | import { parseFrame } from "./safety.ts"; |
| 62 | 62 | import { acceptsGzip, GzipCache, worthCompressing } from "./compress.ts"; |
| 63 | 63 | import { AccountError, sessionCookie, visitor } from "./accounts.ts"; |
| 64 | import { AdminError, changeFromForm } from "./admin.ts"; | |
| 65 | import { ADMIN_CSP, DONE, renderAdmin, renderRefusal, type Done } from "./admin-page.ts"; | |
| 64 | import { adminRoutes } from "./admin-routes.ts"; | |
| 66 | 65 | import { parseFps, type FeedOptions } from "./camera-feed.ts"; |
| 67 | 66 | import { CameraError, CameraHub } from "./cameras.ts"; |
| 68 | 67 | import { LOCAL as LOCAL_PERSON } from "./identity.ts"; |
| @@ -135,7 +134,6 @@ const MAX_BODY = 1_000_000; | ||
| 135 | 134 | const CAMERA = /^\/camera\/(\d{1,9})\/(stream|state|snapshot)$/; |
| 136 | 135 | const ADMIN_CAMERA = /^\/admin\/cameras(?:\/(\d{1,9}))?$/; |
| 137 | 136 | const CAMERA_PAGE = readFileSync(new URL("./camera-list.html", import.meta.url), "utf8"); |
| 138 | const MAX_FORM = 16_384; | |
| 139 | 137 | /** Served from this process whichever arm is live: they describe the app, not the arm. */ |
| 140 | 138 | const LOCAL = /^\/(?:index\.html|leader-map\.js)?$|^\/(?:model|vendor|printables|ui|app)(?:\/|$)/; |
| 141 | 139 | /** The page itself, which the relay writes the first view's facts into. */ |
| @@ -461,71 +459,6 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 461 | 459 | return json(res, 405, { error: "GET or POST /admin/cameras, DELETE /admin/cameras/<id>" }); |
| 462 | 460 | }; |
| 463 | 461 | |
| 464 | /** The owner's page. Anyone else is told so; a change is a form post, answered with a redirect back. */ | |
| 465 | const admin = async (lobby: Lobby, req: IncomingMessage, res: ServerResponse, url: URL) => { | |
| 466 | const who = lobby.who(req); | |
| 467 | const html = (status: number, text: string) => { | |
| 468 | res.writeHead(status, { "content-type": "text/html; charset=utf-8", "cache-control": "no-store", "content-security-policy": ADMIN_CSP }); | |
| 469 | res.end(text); | |
| 470 | }; | |
| 471 | const canEdit = Boolean(who?.owner); | |
| 472 | // a moderator is named by forge username and must hold that forge identity: a guest who types the same name is not them | |
| 473 | if (!who || !(canEdit || (lobby.verified(req)?.id === who.id && lobby.admin.isModerator(who.name)))) return html(who ? 403 : 401, renderRefusal(who)); | |
| 474 | const show = (status: number, done: Done | null, error: string | null) => | |
| 475 | html(status, renderAdmin({ who, canEdit, settings: lobby.admin.settings, snapshot: lobby.turns.snapshot(), log: lobby.admin.log, done, error })); | |
| 476 | if (req.method === "GET" || req.method === "HEAD") { | |
| 477 | const done = url.searchParams.get("done"); | |
| 478 | return show(200, done && Object.hasOwn(DONE, done) ? (done as Done) : null, null); | |
| 479 | } | |
| 480 | if (req.method !== "POST") return json(res, 405, { error: "GET or POST" }); | |
| 481 | if (!canEdit) return html(403, renderRefusal(who)); | |
| 482 | let body: Buffer; | |
| 483 | try { | |
| 484 | body = await readBody(req, MAX_FORM); | |
| 485 | } catch { | |
| 486 | res.setHeader("connection", "close"); | |
| 487 | return show(413, null, "that form is too large"); | |
| 488 | } | |
| 489 | const form = new URLSearchParams(body.toString("utf8")); | |
| 490 | let done: Done; | |
| 491 | try { | |
| 492 | switch (form.get("action")) { | |
| 493 | case "save": | |
| 494 | lobby.configure(who, changeFromForm(form)); | |
| 495 | done = "saved"; | |
| 496 | break; | |
| 497 | case "mod-add": | |
| 498 | case "mod-remove": { | |
| 499 | const name = (form.get("name") ?? "").trim().toLowerCase(); | |
| 500 | const now = lobby.admin.settings.moderators; | |
| 501 | lobby.configure(who, { moderators: form.get("action") === "mod-add" ? [...now, name] : now.filter((m) => m !== name) }); | |
| 502 | done = "saved"; | |
| 503 | break; | |
| 504 | } | |
| 505 | case "pause": | |
| 506 | case "resume": | |
| 507 | lobby.configure(who, { paused: form.get("action") === "pause" }); | |
| 508 | done = form.get("action") === "pause" ? "paused" : "resumed"; | |
| 509 | break; | |
| 510 | case "end": | |
| 511 | lobby.endTurn(who); | |
| 512 | done = "ended"; | |
| 513 | break; | |
| 514 | case "take": | |
| 515 | lobby.takeControl(who); | |
| 516 | done = "taken"; | |
| 517 | break; | |
| 518 | default: | |
| 519 | throw new AdminError("no such action"); | |
| 520 | } | |
| 521 | } catch (err) { | |
| 522 | if (err instanceof AdminError) return show(400, null, err.message); | |
| 523 | throw err; | |
| 524 | } | |
| 525 | res.writeHead(303, { location: `/admin?done=${done}`, "cache-control": "no-store" }); | |
| 526 | res.end(); | |
| 527 | }; | |
| 528 | ||
| 529 | 462 | /** A person's own calibration: signed in is enough, it never touches the arm. */ |
| 530 | 463 | const leaderCalibration = async (req: IncomingMessage, res: ServerResponse) => { |
| 531 | 464 | const who = lobby ? lobby.who(req) : LOCAL_PERSON; |
| @@ -605,7 +538,7 @@ export async function startRelay(options: RelayOptions): Promise<RunningRelay> { | ||
| 605 | 538 | } |
| 606 | 539 | const cameraAdmin = ADMIN_CAMERA.exec(url.pathname); |
| 607 | 540 | if (cameraAdmin) return await administer(req, res, cameraAdmin[1] === undefined ? null : Number(cameraAdmin[1])); |
| 608 | if (lobby && url.pathname === "/admin") return await admin(lobby, req, res, url); | |
| 541 | if (lobby && url.pathname === "/admin") return await adminRoutes(lobby, req, res, url, { json, readBody }); | |
| 609 | 542 | if (url.pathname === LEADER) return await leaderCalibration(req, res); |
| 610 | 543 | if (url.pathname === "/join") { |
| 611 | 544 | if (req.method === "POST") return await join(req, res); |
new above
offline