Cotton code
86 lines · 5.4 KB · TypeScriptRaw
/** * Every route a shared space answers, and what its ONE gate needs of the caller (src/space/space.ts `gate`). The door * matches a request against this list and nothing else, and test/shared-space-gate.test.ts generates its matrix from it: * a route added here without a case in the matrix fails that test. * * - `signed_in`: any signed-in phone of a person (the door vouches it by the person's own object). * - `member`: a member now (not one who left or was removed), of a space that is not ending. * - `owner`: the member who is the space's owner (one at a time; DECISIONS.md "Ownership, and what survives"). * - `link`: a live link's holder (not stopped, not expired), by its auth token. * - Any member route may carry a `statement` (a roster statement a phone signed, cotton-core `seal::shared_album`): *   the space keeps the chain in order (each names the hash of the one before) and never reads more of it; the *   phones verify every signature and refuse a member, a wrap or an owner no statement names. * - `epoch`: the body's `epoch` must be the space's current epoch (`current`) or the next (`next`), else 409; and a *   `current` write waits while the epoch is due to move (someone left or a link stopped), 409 `rekey_due`. */export type Need = "signed_in" | "member" | "owner" | "link";export interface Route {  name: string;  method: "GET" | "POST";  /** The path, `:id` the space id and `:link` a link id. */  path: string;  need: Need;  epoch?: "current" | "next";  /** Still answered while a deleted space waits its thirty days (members read and save; nothing changes). */  whileDeleted?: true;  /** A `current` write still taken while the epoch is due to move (handing out wraps, re-wrapping records). */  duringRekey?: true;  /** Adds to the space: refused to a member who can only see (SPACES 1 roles), 403 `see_only`. */  adds?: true;}export const ROUTES = [  { name: "spaces.list", method: "GET", path: "/v1/spaces", need: "signed_in" },  { name: "spaces.found", method: "POST", path: "/v1/spaces", need: "signed_in" },  { name: "space.read", method: "GET", path: "/v1/spaces/:id", need: "member", whileDeleted: true },  { name: "roster.list", method: "GET", path: "/v1/spaces/:id/roster", need: "member", whileDeleted: true },  { name: "roster.add", method: "POST", path: "/v1/spaces/:id/roster", need: "member" },  { name: "roster.drop", method: "POST", path: "/v1/spaces/:id/roster/drop", need: "owner" },  { name: "records.list", method: "GET", path: "/v1/spaces/:id/records", need: "member", whileDeleted: true },  { name: "records.put", method: "POST", path: "/v1/spaces/:id/records", need: "member", epoch: "current", adds: true },  { name: "records.rewrap", method: "POST", path: "/v1/spaces/:id/records/rewrap", need: "member", epoch: "current", duringRekey: true },  { name: "records.delete", method: "POST", path: "/v1/spaces/:id/records/delete", need: "member" },  { name: "records.stale", method: "GET", path: "/v1/spaces/:id/stale", need: "member" },  { name: "upload", method: "POST", path: "/v1/spaces/:id/upload", need: "member", epoch: "current", adds: true },  { name: "media", method: "POST", path: "/v1/spaces/:id/media", need: "member", whileDeleted: true },  { name: "members.add", method: "POST", path: "/v1/spaces/:id/members", need: "owner" },  { name: "roles.set", method: "POST", path: "/v1/spaces/:id/roles", need: "owner" },  { name: "wraps.add", method: "POST", path: "/v1/spaces/:id/wraps", need: "owner", epoch: "current", duringRekey: true },  { name: "epoch", method: "POST", path: "/v1/spaces/:id/epoch", need: "owner", epoch: "next" },  { name: "keep", method: "POST", path: "/v1/spaces/:id/keep", need: "member" },  { name: "leave", method: "POST", path: "/v1/spaces/:id/leave", need: "member", whileDeleted: true },  { name: "space.delete", method: "POST", path: "/v1/spaces/:id/delete", need: "owner" },  { name: "links.make", method: "POST", path: "/v1/spaces/:id/links", need: "owner", epoch: "current" },  { name: "links.stop", method: "POST", path: "/v1/spaces/:id/links/stop", need: "owner" },  { name: "link.read", method: "POST", path: "/v1/space-link/:id/:link", need: "link" },  { name: "link.media", method: "POST", path: "/v1/space-link/:id/:link/media", need: "link" },] as const satisfies readonly Route[];export type RouteName = (typeof ROUTES)[number]["name"];const SPACE_ID = "([A-Za-z0-9_-]{22})";const LINK_ID = "([A-Za-z0-9_-]{8,64})";const MATCHERS = ROUTES.map((route) => ({  route: route as Route,  pattern: new RegExp(`^${route.path.replace(":id", SPACE_ID).replace(":link", LINK_ID)}$`),}));/** The route a request names, with its space and link ids; null when none does. */export function routeOf(method: string, path: string): { route: Route; space: string | null; link: string | null } | null {  for (const { route, pattern } of MATCHERS) {    if (route.method !== method) continue;    const match = pattern.exec(path);    if (match) return { route, space: match[1] ?? null, link: match[2] ?? null };  }  return null;}/** * Whether a path is a shared space's at all (so a wrong method is answered here, not by a person's object). Not * `/v1/space` nor `/v1/space/…`: those are the person's own space for photos and files (src/space.ts, SPACE.md). */export function isSpacePath(path: string): boolean {  return path === "/v1/spaces" || path.startsWith("/v1/spaces/") || path.startsWith("/v1/space-link/") || path.startsWith("/v1/space-media/");}