{"draft":null,"from":2,"to":318,"files":[{"path":"client/src/server-choice.ts","added":143,"removed":0,"diff":"--- /dev/null\n+++ b/client/src/server-choice.ts\n@@ -0,0 +1,143 @@\n+// The --server flag (abso-core issue 10). Every command that talks to a server takes --server <url>, parsed and\n+// checked here the same way for all of them:\n+//   - A command that works on a joined folder (\"folder\") talks only to that folder's server. --server there must\n+//     name that same server; another one is refused, never swapped in, so the folder's work and key never go to\n+//     a server it wasn't joined on. Outside a joined folder it is refused too, with how to join one there.\n+//   - A command that picks a server for itself (\"chosen\": join, init, login, whoami and the rest about this box)\n+//     uses the one --server names, as given.\n+//   - A command that works on this machine only (\"local\") takes no --server, and says so.\n+// A value that isn't an http or https URL (a user name, a query or a fragment included) is refused before any\n+// call, as is an empty one.\n+//\n+// When nothing names a server (abso-core issue 9), every command picks it in one order (chooseServer):\n+//   1. --server <url>;\n+//   2. the server of the joined folder the command runs in (its .abso/config.json);\n+//   3. ABSO_SERVER (or the older AVCS_SERVER);\n+//   4. prod, https://api.abso.dev.\n+// Which servers this box is signed in to plays no part: signed in to staging and prod both, or to staging\n+// alone, a command with nothing named goes to prod, and staging is a deliberate --server, folder or ABSO_SERVER.\n+\n+import { readFileSync } from \"node:fs\";\n+import { type Parsed, UsageError } from \"./args.ts\";\n+import { configPath, findRoot } from \"./config.ts\";\n+import { serverBase, TOKEN_SERVERS } from \"./core.ts\";\n+import { fromEnv } from \"./env-names.ts\";\n+\n+/** How a command uses a server: its joined folder's, one it picks, or none. */\n+export type ServerUse = \"folder\" | \"chosen\" | \"local\";\n+\n+/** The server a command goes to when nothing names one: prod. */\n+export const PROD_SERVER = TOKEN_SERVERS[0]!;\n+\n+/** The order of precedence, in the words help uses. */\n+export const SERVER_ORDER = `--server <url>, else the server of the abso folder you're in, else ABSO_SERVER, else prod (${PROD_SERVER})`;\n+\n+/**\n+ * The server a command goes to: `flag` (checked by serverFlag), else the joined folder's, else ABSO_SERVER\n+ * (checked here), else prod. An explicit choice is returned as given, never swapped for another.\n+ */\n+export function chooseServer(\n+\tflag: string | undefined,\n+\tcwd: string,\n+\tenv: Readonly<Record<string, string | undefined>>,\n+): string {\n+\tif (flag !== undefined) return flag;\n+\tconst folder = folderServer(cwd);\n+\tif (folder !== undefined) return folder;\n+\tconst named = fromEnv(env, \"SERVER\");\n+\tif (named !== undefined) return checkedServer(named, env.ABSO_SERVER ? \"ABSO_SERVER\" : \"AVCS_SERVER\");\n+\treturn PROD_SERVER;\n+}\n+\n+/** The commands that pick a server for themselves rather than use a joined folder's. */\n+const CHOSEN: ReadonlySet<string> = new Set([\n+\t\"init\",\n+\t\"join\",\n+\t\"key\",\n+\t\"login\",\n+\t\"box\",\n+\t\"whoami\",\n+\t\"doctor\",\n+\t\"logout\",\n+\t\"orgs\",\n+\t\"org\",\n+]);\n+\n+/** The commands that never talk to a server. */\n+const LOCAL: ReadonlySet<string> = new Set([\"skill\", \"check-ignore\", \"pause\", \"resume\", \"fix-eol\"]);\n+\n+/** How `command` uses a server. Anything not named above works on a joined folder. */\n+export function serverUse(command: string): ServerUse {\n+\treturn CHOSEN.has(command) ? \"chosen\" : LOCAL.has(command) ? \"local\" : \"folder\";\n+}\n+\n+const EXAMPLE = \"https://api.abso.dev\";\n+\n+/** `value` as a server, or a refusal naming where it came from (--server, ABSO_SERVER). */\n+export function checkedServer(value: string, from: string): string {\n+\tconst v = value.trim();\n+\tif (v === \"\") throw new UsageError(`${from} needs a server's URL, like ${EXAMPLE}`);\n+\tif (serverBase(v) === undefined)\n+\t\tthrow new UsageError(\n+\t\t\t`${from} takes an http or https URL with no user name, query or fragment, like ${EXAMPLE} (got ${JSON.stringify(value)})`,\n+\t\t);\n+\treturn v;\n+}\n+\n+/** The server --server names, checked, or undefined when it isn't given. */\n+export function serverFlag(args: Parsed): string | undefined {\n+\tconst v = args.flags.get(\"server\");\n+\treturn typeof v === \"string\" ? checkedServer(v, \"--server\") : undefined;\n+}\n+\n+/** Whether two servers are the same one: the same scheme, host, port and path, ignoring case and a trailing slash. */\n+export function sameServer(a: string, b: string): boolean {\n+\treturn (serverBase(a) ?? a.trim()) === (serverBase(b) ?? b.trim());\n+}\n+\n+/** The server in the .abso/config.json of the folder `cwd` is in, or undefined outside one (or when it can't be read). */\n+export function folderServer(cwd: string): string | undefined {\n+\tconst root = findRoot(cwd);\n+\tif (root === undefined) return undefined;\n+\ttry {\n+\t\tconst server = (JSON.parse(readFileSync(configPath(root), \"utf8\")) as { server?: unknown }).server;\n+\t\treturn typeof server === \"string\" && server !== \"\" ? server : undefined;\n+\t} catch {\n+\t\treturn undefined;\n+\t}\n+}\n+\n+/**\n+ * For a command that works on a joined folder: refuses a --server that isn't the folder's own server, and one\n+ * given outside a joined folder. Nothing is sent before this.\n+ */\n+export function checkFolderServer(command: string, flag: string, cwd: string): void {\n+\tconst own = folderServer(cwd);\n+\tif (own === undefined)\n+\t\tthrow new UsageError(\n+\t\t\t`abso ${command} works on a joined folder, and ${cwd} isn't in one. To work with ${flag}: abso join <project> --server ${flag}, or abso init --server ${flag} for a new project.`,\n+\t\t);\n+\tif (!sameServer(own, flag))\n+\t\tthrow new UsageError(\n+\t\t\t`this folder is joined on ${own}, not ${flag}, so abso ${command} won't send its work or key there. Run it in a folder joined on ${flag} (abso join <project> --server ${flag}).`,\n+\t\t);\n+}\n+\n+/** What help says about --server for a command, the same words for every command of a kind. */\n+export function serverHelp(command: string): string | undefined {\n+\tswitch (serverUse(command)) {\n+\t\tcase \"local\":\n+\t\t\treturn undefined;\n+\t\tcase \"folder\":\n+\t\t\treturn \"--server <url>: this folder's own server, which it uses anyway. Another server is refused, never swapped in, so the folder's work and key go only to the server it was joined on.\";\n+\t\tcase \"chosen\":\n+\t\t\treturn command === \"key\"\n+\t\t\t\t? \"--server <url>: the server the key is for; without it, this folder's. It is used as given, never swapped for another.\"\n+\t\t\t\t: `--server <url>: the server to use, as given, never swapped for another. The order: ${SERVER_ORDER}. This box's sign-in for one server is never sent to another.`;\n+\t}\n+}\n+\n+/** The refusal for --server on a command that works on this machine only. */\n+export function localRefusal(command: string): string {\n+\treturn `abso ${command} works on this machine only and talks to no server, so it takes no --server`;\n+}"}],"unmatched":[]}