{"draft":null,"from":116,"to":318,"files":[{"path":"src/caller.ts","added":75,"removed":0,"diff":"--- /dev/null\n+++ b/src/caller.ts\n@@ -0,0 +1,75 @@\n+// Who is calling a seat: the chat's session, the computer and the folder, so a seat can tell its callers when\n+// someone else picks it up. It is information, never authority: the agent key decides who the caller is, and nothing\n+// secret goes in it. Sent as one header, base64url of its JSON.\n+\n+export const CALLER_HEADER = \"x-abso-caller\";\n+/** The header's most bytes. A folder that would go over keeps its end, which is the part that tells folders apart. */\n+export const CALLER_MAX = 512;\n+\n+export interface Caller {\n+\t/** The chat's session, when abso runs in one: the client sends a short SHA-256 of the id, never the id itself. */\n+\treadonly session?: string;\n+\treadonly machine: string;\n+\t/** The folder's last part: the client sends no more of its path. */\n+\treadonly folder: string;\n+}\n+\n+const SESSION = /^[A-Za-z0-9_-]{1,64}$/;\n+const MACHINE = /^[\\w.-]{1,64}$/;\n+const FOLDER_MAX = 300;\n+\n+const b64u = (text: string): string =>\n+\tbtoa(String.fromCodePoint(...new TextEncoder().encode(text)))\n+\t\t.replace(/\\+/g, \"-\")\n+\t\t.replace(/\\//g, \"_\")\n+\t\t.replace(/=+$/, \"\");\n+\n+function fromB64u(text: string): string {\n+\tconst bin = atob(text.replace(/-/g, \"+\").replace(/_/g, \"/\"));\n+\treturn new TextDecoder(\"utf-8\", { fatal: true, ignoreBOM: false }).decode(\n+\t\tUint8Array.from(bin, (c) => c.codePointAt(0)!),\n+\t);\n+}\n+\n+/** The header for this caller, or undefined when there's nothing usable to say. */\n+export function callerHeader(caller: {\n+\tsession?: string | undefined;\n+\tmachine: string;\n+\tfolder: string;\n+}): string | undefined {\n+\tconst machine = caller.machine.replace(/[^\\w.-]/g, \"-\").slice(0, 64);\n+\tif (!MACHINE.test(machine)) return undefined;\n+\tconst session = caller.session !== undefined && SESSION.test(caller.session) ? caller.session : undefined;\n+\tlet folder = caller.folder.slice(-FOLDER_MAX);\n+\tfor (;;) {\n+\t\tconst out = b64u(JSON.stringify({ ...(session === undefined ? {} : { session }), machine, folder }));\n+\t\tif (out.length <= CALLER_MAX) return out;\n+\t\tif (folder.length === 0) return undefined;\n+\t\tfolder = folder.slice(Math.ceil(folder.length / 4));\n+\t}\n+}\n+\n+/** The caller a header names, or null when it is missing or isn't one (a bad header never guesses). */\n+export function parseCaller(header: string | null | undefined): Caller | null {\n+\tif (header === null || header === undefined || header === \"\" || header.length > CALLER_MAX) return null;\n+\tif (!/^[A-Za-z0-9_-]+$/.test(header)) return null;\n+\tlet raw: unknown;\n+\ttry {\n+\t\traw = JSON.parse(fromB64u(header));\n+\t} catch {\n+\t\treturn null;\n+\t}\n+\tif (typeof raw !== \"object\" || raw === null || Array.isArray(raw)) return null;\n+\tconst { session, machine, folder, ...rest } = raw as Record<string, unknown>;\n+\tif (Object.keys(rest).length > 0) return null;\n+\tif (typeof machine !== \"string\" || !MACHINE.test(machine)) return null;\n+\tif (typeof folder !== \"string\" || folder.length > FOLDER_MAX || /[\\u0000-\\u001f]/.test(folder)) return null;\n+\tif (session !== undefined && (typeof session !== \"string\" || !SESSION.test(session))) return null;\n+\treturn { ...(session === undefined ? {} : { session }), machine, folder };\n+}\n+\n+/** How a caller reads in a sentence: \"<folder> on <machine>\", with only the folder's last part. */\n+export function callerPlace(caller: Caller): string {\n+\tconst parts = caller.folder.split(/[\\\\/]+/).filter((p) => p !== \"\");\n+\treturn `${parts.at(-1) ?? \"a folder\"} on ${caller.machine}`;\n+}"}],"unmatched":[]}