jsdoc typing round, phases 1-2: tsc as a no-emit checker over plain mjs (checkJs, strict), the shared shapes in server/shapes.mjs (log events as a tagged union, wire messages, manifests, job state, the ctx), all 12 server modules annotated; requireOwner/requireSuperuser as JSDoc assertion functions so the router guarantee narrows without callsite churn; runtime stays zero-dependency (typescript + @types/node as pinned devDeps, node_modules gitignored, QUESTIONS #4 amended: dev tooling exempt); every non-annotation delta audited onto a small intended list (clerk status ?? 1, dead 401 guards, async wraps on enqueue callsites); smoke 105/105 + restart + repo checks green; the clerk and web/ are the next phases

This commit is contained in:
2026-11-15 15:28:56 +00:00
parent d737e47f82
commit f81b188ce1
22 changed files with 594 additions and 90 deletions
+31 -9
View File
@@ -12,6 +12,13 @@
//
// All times are UTC (house rule). Never model-facing: nothing here
// ever enters a bundle or a prompt.
/** @typedef {import('./shapes.mjs').User} User */
/** A users table row (token hashes never leave this module). */
/** @typedef {{ id: string, token_hash: string, name: string, superuser: number, created: string }} UserRow */
/** An invites table row. */
/** @typedef {{ id: number, code_hash: string, kind: 'invite'|'bootstrap', created_by: string, created_at: string, claimed_by: ?string, claimed_at: ?string }} InviteRow */
/** @typedef {{ id: number, kind: 'invite'|'bootstrap', created_by: string, created_at: string, claimed_by: ?string, claimed_at: ?string }} InviteListed */
import { DatabaseSync } from 'node:sqlite';
import { createHash, randomBytes } from 'node:crypto';
import fs from 'node:fs';
@@ -47,48 +54,58 @@ const db = new DatabaseSync(DB_PATH);
db.exec(SCHEMA);
const now = () => new Date().toISOString();
/** @param {string} s @returns {string} */
const sha = (s) => createHash('sha256').update(String(s)).digest('hex');
/** @param {number} n @returns {string} */
const hex = (n) => randomBytes(n).toString('hex');
// --- users ---------------------------------------------------------------------
/** @returns {number} */
export function userCount() {
return db.prepare('SELECT COUNT(*) AS n FROM users').get().n;
return Number(db.prepare('SELECT COUNT(*) AS n FROM users').get()?.n ?? 0);
}
// does this identity exist? A revoked user's id resolves to absent --
// dead ids are ownerless by resolution, never by rewriting history
/** @param {?string} uid @returns {boolean} */
export function exists(uid) {
if (uid == null) return false;
return !!db.prepare('SELECT 1 FROM users WHERE id = ?').get(uid);
}
/** @param {?string} token @returns {?UserRow} */
export function getUserByToken(token) {
const t = String(token ?? '').trim();
if (!t) return null;
const row = db.prepare('SELECT * FROM users WHERE token_hash = ?').get(sha(t));
const row = /** @type {?UserRow} */ (db.prepare('SELECT * FROM users WHERE token_hash = ?').get(sha(t)));
return row ?? null;
}
/** @param {?string|undefined} uid @returns {?string} */
export function userName(uid) {
if (uid == null) return null;
const row = db.prepare('SELECT name FROM users WHERE id = ?').get(uid);
return row ? row.name : null;
return row ? String(row.name) : null;
}
/** @returns {User[]} */
export function listUsers() {
return db.prepare('SELECT id, name, superuser, created FROM users ORDER BY created').all()
.map((u) => ({ ...u, superuser: u.superuser === 1 }));
return /** @type {{ id: string, name: string, superuser: number, created: string }[]} */ (
db.prepare('SELECT id, name, superuser, created FROM users ORDER BY created').all()
).map((u) => ({ ...u, superuser: u.superuser === 1 }));
}
// revocation destroys the identity: no suspend, no restore. Their runs
// and packages stay -- ownerless, readable, forkable.
/** @param {string} id @returns {boolean} */
export function revokeUser(id) {
return db.prepare('DELETE FROM users WHERE id = ?').run(id).changes > 0;
}
// --- invites ---------------------------------------------------------------------
/** @param {string} byUid @param {'invite'|'bootstrap'} [kind] @returns {{ id: number, code: string }} */
export function mintInvite(byUid, kind = 'invite') {
const code = hex(12); // 24 hex chars -- shown once, stored hashed
const res = db.prepare(
@@ -97,26 +114,30 @@ export function mintInvite(byUid, kind = 'invite') {
return { id: Number(res.lastInsertRowid), code };
}
/** @returns {InviteListed[]} */
export function listInvites() {
return db.prepare(
return /** @type {InviteListed[]} */ (db.prepare(
'SELECT id, kind, created_by, created_at, claimed_by, claimed_at FROM invites ORDER BY id'
).all();
).all());
}
// only unclaimed invites can be revoked; spent ones are a permanent record
/** @param {number} id @returns {boolean} */
export function revokeInvite(id) {
return db.prepare('DELETE FROM invites WHERE id = ? AND claimed_by IS NULL').run(id).changes > 0;
}
// exchange a single-use invite code for a persistent token. Unknown,
// spent, and dead-bootstrap codes are each named explicitly.
/** @param {?string} code @param {?string} name @returns {{ token: string, user: User }} */
export function claimInvite(code, name) {
const c = String(code ?? '').trim();
const n = String(name ?? '').trim();
const fail = (msg) => { const e = new Error(msg); e.status = 400; throw e; };
/** @param {string} msg @returns {never} */
const fail = (msg) => { const e = /** @type {import('./shapes.mjs').HttpError} */ (new Error(msg)); e.status = 400; throw e; };
if (!c) fail('the invite code is required');
if (!n) fail('a display name is required');
const row = db.prepare('SELECT * FROM invites WHERE code_hash = ?').get(sha(c));
const row = /** @type {?InviteRow} */ (db.prepare('SELECT * FROM invites WHERE code_hash = ?').get(sha(c)));
if (!row) fail('unknown invite code');
if (row.claimed_by != null) fail('this invite was already used -- ask for a fresh one');
if (row.kind === 'bootstrap' && userCount() > 0) {
@@ -147,6 +168,7 @@ export function claimInvite(code, name) {
// never generated again once any user exists; rejected at claim time
// if somehow presented after that.
/** @returns {?{ reused?: boolean, code?: string }} */
export function bootstrap() {
if (userCount() > 0) return null;
const open = db.prepare("SELECT id FROM invites WHERE kind = 'bootstrap' AND claimed_by IS NULL").get();