- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/client/src/server-choice.ts?version=155&format=json
111 linesspeed 100%Last change speed in version 155, 1 version changed it
You're reading version 155. The newest is 318, and 1 version since then changed this file.
1// The --server flag (abso-core issue 10). Every command that talks to a server takes --server <url>, parsed and
2// checked here the same way for all of them:
3// - A command that works on a joined folder ("folder") talks only to that folder's server. --server there must
4// name that same server; another one is refused, never swapped in, so the folder's work and key never go to
5// a server it wasn't joined on. Outside a joined folder it is refused too, with how to join one there.
6// - A command that picks a server for itself ("chosen": join, init, login, whoami and the rest about this box)
7// uses the one --server names, as given.
8// - A command that works on this machine only ("local") takes no --server, and says so.
9// A value that isn't an http or https URL (a user name, a query or a fragment included) is refused before any
10// call, as is an empty one.
12import { readFileSync } from "node:fs";
13import { type Parsed, UsageError } from "./args.ts";
14import { configPath, findRoot } from "./config.ts";
15import { serverBase } from "./core.ts";
17/** How a command uses a server: its joined folder's, one it picks, or none. */
18export type ServerUse = "folder" | "chosen" | "local";
20/** The commands that pick a server for themselves rather than use a joined folder's. */
21const CHOSEN: ReadonlySet<string> = new Set([
22 "init",
23 "join",
24 "key",
25 "login",
26 "box",
27 "whoami",
28 "doctor",
29 "logout",
30 "orgs",
31 "org",
32]);
34/** The commands that never talk to a server. */
35const LOCAL: ReadonlySet<string> = new Set(["skill", "check-ignore", "pause", "resume", "fix-eol"]);
37/** How `command` uses a server. Anything not named above works on a joined folder. */
38export function serverUse(command: string): ServerUse {
39 return CHOSEN.has(command) ? "chosen" : LOCAL.has(command) ? "local" : "folder";
40}
42const EXAMPLE = "https://api.abso.dev";
44/** `value` as a server, or a refusal naming where it came from (--server, ABSO_SERVER). */
45export function checkedServer(value: string, from: string): string {
46 const v = value.trim();
47 if (v === "") throw new UsageError(`${from} needs a server's URL, like ${EXAMPLE}`);
48 if (serverBase(v) === undefined)
49 throw new UsageError(
50 `${from} takes an http or https URL with no user name, query or fragment, like ${EXAMPLE} (got ${JSON.stringify(value)})`,
51 );
52 return v;
53}
55/** The server --server names, checked, or undefined when it isn't given. */
56export function serverFlag(args: Parsed): string | undefined {
57 const v = args.flags.get("server");
58 return typeof v === "string" ? checkedServer(v, "--server") : undefined;
59}
61/** Whether two servers are the same one: the same scheme, host, port and path, ignoring case and a trailing slash. */
62export function sameServer(a: string, b: string): boolean {
63 return (serverBase(a) ?? a.trim()) === (serverBase(b) ?? b.trim());
64}
66/** The server in the .abso/config.json of the folder `cwd` is in, or undefined outside one (or when it can't be read). */
67export function folderServer(cwd: string): string | undefined {
68 const root = findRoot(cwd);
69 if (root === undefined) return undefined;
70 try {
71 const server = (JSON.parse(readFileSync(configPath(root), "utf8")) as { server?: unknown }).server;
72 return typeof server === "string" && server !== "" ? server : undefined;
73 } catch {
74 return undefined;
75 }
76}
78/**
79 * For a command that works on a joined folder: refuses a --server that isn't the folder's own server, and one
80 * given outside a joined folder. Nothing is sent before this.
81 */
82export function checkFolderServer(command: string, flag: string, cwd: string): void {
83 const own = folderServer(cwd);
84 if (own === undefined)
85 throw new UsageError(
86 `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.`,
87 );
88 if (!sameServer(own, flag))
89 throw new UsageError(
90 `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}).`,
91 );
92}
94/** What help says about --server for a command, the same words for every command of a kind. */
95export function serverHelp(command: string): string | undefined {
96 switch (serverUse(command)) {
97 case "local":
98 return undefined;
99 case "folder":
100 return "--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.";
101 case "chosen":
102 return command === "key"
103 ? "--server <url>: the server the key is for; without it, this folder's. It is used as given, never swapped for another."
104 : "--server <url>: the server to use, as given, never swapped for another. Each server gets only its own sign-in.";
105 }
106}
108/** The refusal for --server on a command that works on this machine only. */
109export function localRefusal(command: string): string {
110 return `abso ${command} works on this machine only and talks to no server, so it takes no --server`;
111}