Versions 175 to 318
new file, 407 lines
1+// Local projects (abso init --local): the open server's own router and project object (server/src) run in this
2+// process, over Node's SQLite, so a project needs no server, account or network. Each project is one SQLite file in a
3+// store folder. Every call runs in one SQLite write transaction, which is the store's lock: another process's call
5+
12+
17+/** The shared token the in-process server is given, and that its calls carry. Not a secret: nothing listens. */
19+
21+
23+
25+ * Where local projects live: the folder's own store (--store, kept in its config), else ABSO_STORE, else the OS's
26+ * per-user data folder. Undefined when there is none to use (no data folder and nothing named), so nothing assumes a
29+export function storeDir(env: EnvMap, configured?: string, cwd = process.cwd()): string | undefined {
47+
49+ "there's no folder for local projects here: set ABSO_STORE to one, or give abso init --local a --store <folder>";
50+
51+// --- The Workers runtime's parts the server uses, over node:sqlite -----------------------------------------------
52+
63+
64+/** A Durable Object's storage: SQL, synchronous transactions, the key-value calls and the alarm, on one database. */
67+ "CREATE TABLE IF NOT EXISTS _local_kv (key TEXT PRIMARY KEY, value BLOB NOT NULL); CREATE TABLE IF NOT EXISTS _local_alarm (id INTEGER PRIMARY KEY CHECK (id = 0), at INTEGER)",
80+ const out = (v: unknown) => (v instanceof Uint8Array ? v.buffer.slice(v.byteOffset, v.byteOffset + v.byteLength) : v);
83+ const row = prepared("SELECT value FROM _local_kv WHERE key = ?").get(key) as { value: Uint8Array } | undefined;
88+ "INSERT INTO _local_kv (key, value) VALUES (?, ?) ON CONFLICT (key) DO UPDATE SET value = excluded.value",
91+ (prepared("DELETE FROM _local_kv WHERE key = ?").run(key) as { changes: number | bigint }).changes > 0;
97+ const rows = statement.columns().length > 0 ? statement.all(...args) : (statement.run(...args), []);
140+ rows.filter((r) => r.key.startsWith(options.prefix ?? "")).map((r) => [r.key, deserialize(r.value)]),
143+ // The alarm is kept, but nothing in a local project needs it: it pushes out to Artifacts, which is off here.
145+ const row = prepared("SELECT at FROM _local_alarm WHERE id = 0").get() as { at: number | null } | undefined;
149+ prepared("INSERT INTO _local_alarm (id, at) VALUES (0, ?) ON CONFLICT (id) DO UPDATE SET at = excluded.at").run(
159+
162+ // Through a cast: abso's worker compiles this file too (tool.lock.json), and its types have no globalThis.crypto.
186+
187+// --- Loading the server's code ------------------------------------------------------------------------------------
188+
194+ issuesFetch: (request: Request, env: unknown, ctx: unknown, route: unknown) => Promise<Response>;
197+
198+/** Node's SQLite says it's experimental on every start; that line would land in every local command's output. */
210+
211+export const NODE_TOO_OLD = `local projects need Node 22.13 or later, for its built-in SQLite; this is Node ${process.versions.node}`;
212+
223+ // Run as .ts files, the server's `import { DurableObject } from "cloudflare:workers"` needs a stand-in. A built
224+ // copy has it already: the build points the import at the same file (scripts/build-client.mjs).
229+ // The hook stays for the rest of the process: deregistering it crashes Node 24 at exit when the compile cache
241+ // Nothing is reached for here: a part of the client is missing (its packages not installed, say).
243+ `local projects couldn't load the server's code: ${e instanceof Error ? e.message : String(e)}`,
256+
259+
260+// --- One store, as a fetch -----------------------------------------------------------------------------------------
261+
266+ /** PRAGMA data_version when this process last used the project: it changes when another process writes. */
268+ /** The project's issues, asks and news: issues.sqlite beside it, opened when a call first reaches them. */
270+ /** The issues object for the call under way: made again for each call, as another process may have written. */
273+
276+
286+
287+/** A project's calls, and its issues, asks and news, which the open server keeps at /issues/p/<project>/. */
289+/** A project's name is also its folder in the store, so it never starts with a dot ("." and ".." would leave it). */
292+
294+ * The fetch a local folder's calls go through: each call is answered by the open server's router, in this process,
297+export function localFetch(dir: string): (input: string, init?: RequestInit) => Promise<Response> {
309+ db.exec("PRAGMA journal_mode = WAL; PRAGMA busy_timeout = 120000; PRAGMA synchronous = NORMAL");
338+ db.exec("PRAGMA journal_mode = WAL; PRAGMA busy_timeout = 120000; PRAGMA synchronous = NORMAL");
350+ // One call at a time, as a Durable Object takes them: a call holds its project's write transaction across awaits,
351+ // and the connection can't start a second (two calls at once come from Promise.all in ci and waiting --mine).
361+
369+ if (!NAME.test(name)) return json(400, { error: "project names use letters, digits, '.', '_' and '-'" });
376+ // Every call, issues ones too, holds the project file's lock, so local writers of the project and of its
381+ const version = (p.db.prepare("PRAGMA data_version").get() as { data_version: number }).data_version;
390+ // The answer is read whole before the write is kept, so a body that fails to arrive keeps nothing either.
- MCP
- compare on this site's /mcp
- CLI
- abso diff 175 318 client/src/local-store.ts --json
- JSON
- /christopher/abso-core/compare/175..318?file=client%2Fsrc%2Flocal-store.ts&format=json