{"file":"server/src/issues/serve.ts","version":318,"total":200,"from":1,"to":200,"lines":[{"n":1,"text":"// How the open server's Worker reaches a project's issues, and what the IssueProject object does with a","atom":"a1520","author":"trials","version":151},{"n":2,"text":"// request, apart from Cloudflare's own types so both run under node --test.","atom":"a1520","author":"trials","version":151},{"n":3,"text":"//","atom":"a1520","author":"trials","version":151},{"n":4,"text":"//   issuesFetch    the Worker's entry for /issues/p/:project/...: the router has checked the token or key;","atom":"a1520","author":"trials","version":151},{"n":5,"text":"//                  this checks that a key acts only as its own agent (and a reader's only reads), hands the","atom":"a1520","author":"trials","version":151},{"n":6,"text":"//                  request to the project's IssueProject, and delivers a write's news to the project.","atom":"a1520","author":"trials","version":151},{"n":7,"text":"//   IssuesObject   one project's issues: migrations, the store, and its routes (routes.ts). An ask's `of` is","atom":"a1520","author":"trials","version":151},{"n":8,"text":"//                  checked against the project's agents.","atom":"a1520","author":"trials","version":151},{"n":9,"text":"//","atom":"a1520","author":"trials","version":151},{"n":10,"text":"// The project object (env.PROJECT, named by the project) is asked two things, both optional: agents(project)","atom":"a1520","author":"trials","version":151},{"n":11,"text":"// for the ask check, and news(project, items) to deliver a write's news as announcements. Without them the","atom":"a1520","author":"trials","version":151},{"n":12,"text":"// check is skipped and the news is dropped with a log line.","atom":"a1520","author":"trials","version":151},{"n":13,"text":"","atom":"a1520","author":"trials","version":151},{"n":14,"text":"import type { News } from \"../../../client/src/issues-wire.ts\";","atom":"a1520","author":"trials","version":151},{"n":15,"text":"import { BODY_MAX, issuesRoute, json, readCapped } from \"./routes.ts\";","atom":"a1520","author":"trials","version":151},{"n":16,"text":"import { seatRefusal } from \"./seats.ts\";","atom":"a1520","author":"trials","version":151},{"n":17,"text":"import { IssuesError, IssuesStore, SCHEMA, type Sql } from \"./store.ts\";","atom":"a1520","author":"trials","version":151},{"n":18,"text":"","atom":"a1520","author":"trials","version":151},{"n":19,"text":"/** A write's news for the project to deliver: the issue's claimers and those in `to` hear it, never `from`. */","atom":"a1520","author":"trials","version":151},{"n":20,"text":"export interface IssueNews extends News {","atom":"a1520","author":"trials","version":151},{"n":21,"text":"\t/** The agent whose write it was. */","atom":"a1520","author":"trials","version":151},{"n":22,"text":"\tfrom: string;","atom":"a1520","author":"trials","version":151},{"n":23,"text":"}","atom":"a1520","author":"trials","version":151},{"n":24,"text":"","atom":"a1520","author":"trials","version":151},{"n":25,"text":"/** What issues asks of the project's own object. Each method is optional: an older project object may lack it. */","atom":"a1520","author":"trials","version":151},{"n":26,"text":"export interface ProjectHooks {","atom":"a1520","author":"trials","version":151},{"n":27,"text":"\t/** The agents in the project, which an ask may go to. */","atom":"a1520","author":"trials","version":151},{"n":28,"text":"\tagents?(project: string): Promise<string[]>;","atom":"a1520","author":"trials","version":151},{"n":29,"text":"\t/** Deliver each item as an announcement from `from` to the issue's claimers and `to`. */","atom":"a1520","author":"trials","version":151},{"n":30,"text":"\tnews?(project: string, items: IssueNews[]): Promise<unknown>;","atom":"a1520","author":"trials","version":151},{"n":31,"text":"}","atom":"a1520","author":"trials","version":151},{"n":32,"text":"","atom":"a1520","author":"trials","version":151},{"n":33,"text":"/** A Durable Object namespace as far as issues uses one; Cloudflare's DurableObjectNamespace fits it. */","atom":"a1520","author":"trials","version":151},{"n":34,"text":"export interface NamespaceLike {","atom":"a1520","author":"trials","version":151},{"n":35,"text":"\tidFromName(name: string): unknown;","atom":"a1520","author":"trials","version":151},{"n":36,"text":"\tget(id: never): object;","atom":"a1520","author":"trials","version":151},{"n":37,"text":"}","atom":"a1520","author":"trials","version":151},{"n":38,"text":"","atom":"a1520","author":"trials","version":151},{"n":39,"text":"/** The bindings issues reads. */","atom":"a1520","author":"trials","version":151},{"n":40,"text":"export interface IssuesEnv {","atom":"a1520","author":"trials","version":151},{"n":41,"text":"\t/** One IssueProject per project, named by the project's name. */","atom":"a1520","author":"trials","version":151},{"n":42,"text":"\tISSUES_DO: NamespaceLike;","atom":"a1520","author":"trials","version":151},{"n":43,"text":"\t/** The project objects, named by the project's name; their stubs answer ProjectHooks. */","atom":"a1520","author":"trials","version":151},{"n":44,"text":"\tPROJECT?: NamespaceLike;","atom":"a1520","author":"trials","version":151},{"n":45,"text":"}","atom":"a1520","author":"trials","version":151},{"n":46,"text":"","atom":"a1520","author":"trials","version":151},{"n":47,"text":"/** How the router let the request in: the shared token acts as any agent; a key only as its own agent. */","atom":"a1520","author":"trials","version":151},{"n":48,"text":"export type IssuesAuth = { kind: \"token\" } | { kind: \"key\"; agent: string; role: string };","atom":"a1520","author":"trials","version":151},{"n":49,"text":"","atom":"a1520","author":"trials","version":151},{"n":50,"text":"/** What issuesFetch needs from the Worker's context. */","atom":"a1520","author":"trials","version":151},{"n":51,"text":"export interface WaitUntil {","atom":"a1520","author":"trials","version":151},{"n":52,"text":"\twaitUntil(promise: Promise<unknown>): void;","atom":"a1520","author":"trials","version":151},{"n":53,"text":"}","atom":"a1520","author":"trials","version":151},{"n":54,"text":"","atom":"a1520","author":"trials","version":151},{"n":55,"text":"const PROJECT_NAME = /^[A-Za-z0-9._-]{1,64}$/;","atom":"a1520","author":"trials","version":151},{"n":56,"text":"const AGENT_NAME = /^[A-Za-z0-9._-]{1,64}$/;","atom":"a1520","author":"trials","version":151},{"n":57,"text":"","atom":"a1520","author":"trials","version":151},{"n":58,"text":"function stubOf<T>(ns: NamespaceLike, name: string): T {","atom":"a1520","author":"trials","version":151},{"n":59,"text":"\tconst n = ns as unknown as { idFromName(name: string): unknown; get(id: unknown): T };","atom":"a1520","author":"trials","version":151},{"n":60,"text":"\treturn n.get(n.idFromName(name));","atom":"a1520","author":"trials","version":151},{"n":61,"text":"}","atom":"a1520","author":"trials","version":151},{"n":62,"text":"","atom":"a1520","author":"trials","version":151},{"n":63,"text":"/** The project object's stub, as far as issues calls it; undefined when PROJECT isn't bound. */","atom":"a1520","author":"trials","version":151},{"n":64,"text":"function hooksOf(env: IssuesEnv, project: string): ProjectHooks | undefined {","atom":"a1520","author":"trials","version":151},{"n":65,"text":"\treturn env.PROJECT === undefined ? undefined : stubOf<ProjectHooks>(env.PROJECT, project);","atom":"a1520","author":"trials","version":151},{"n":66,"text":"}","atom":"a1520","author":"trials","version":151},{"n":67,"text":"","atom":"a1520","author":"trials","version":151},{"n":68,"text":"/**","atom":"a1520","author":"trials","version":151},{"n":69,"text":" * Whether an RPC call failed because the receiver has no such method. On an RPC stub every property reads as a","atom":"a1520","author":"trials","version":151},{"n":70,"text":" * function, so only the call can tell: it fails with this error.","atom":"a1520","author":"trials","version":151},{"n":71,"text":" */","atom":"a1520","author":"trials","version":151},{"n":72,"text":"export function missingMethod(e: unknown): boolean {","atom":"a1520","author":"trials","version":151},{"n":73,"text":"\tconst message = e instanceof Error ? e.message : String(e);","atom":"a1520","author":"trials","version":151},{"n":74,"text":"\treturn /does not implement (the )?method|is not a function/i.test(message);","atom":"a1520","author":"trials","version":151},{"n":75,"text":"}","atom":"a1520","author":"trials","version":151},{"n":76,"text":"","atom":"a1520","author":"trials","version":151},{"n":77,"text":"/**","atom":"a1520","author":"trials","version":151},{"n":78,"text":" * /issues/p/:project<rest> after the router's sign-in check. `rest` is the path after the project, like","atom":"a1520","author":"trials","version":151},{"n":79,"text":" * /a/lead/ask or /issues.","atom":"a1520","author":"trials","version":151},{"n":80,"text":" */","atom":"a1520","author":"trials","version":151},{"n":81,"text":"export async function issuesFetch(","atom":"a1453","author":"speed","version":150},{"n":82,"text":"\trequest: Request,","atom":"a1520","author":"trials","version":151},{"n":83,"text":"\tenv: IssuesEnv,","atom":"a1520","author":"trials","version":151},{"n":84,"text":"\tctx: WaitUntil,","atom":"a1520","author":"trials","version":151},{"n":85,"text":"\t{ project, rest, auth }: { project: string; rest: string; auth: IssuesAuth },","atom":"a1520","author":"trials","version":151},{"n":86,"text":"): Promise<Response> {","atom":"a1520","author":"trials","version":151},{"n":87,"text":"\tif (!PROJECT_NAME.test(project)) return json(404, { error: \"no such project\" });","atom":"a1520","author":"trials","version":151},{"n":88,"text":"\tif (!rest.startsWith(\"/\")) return json(404, { error: \"no such issues route\" });","atom":"a1520","author":"trials","version":151},{"n":89,"text":"\tconst m = /^\\/a\\/([^/]+)\\//.exec(rest);","atom":"a1520","author":"trials","version":151},{"n":90,"text":"\tlet agent: string | undefined;","atom":"a1520","author":"trials","version":151},{"n":91,"text":"\tif (m) {","atom":"a1520","author":"trials","version":151},{"n":92,"text":"\t\ttry {","atom":"a1520","author":"trials","version":151},{"n":93,"text":"\t\t\tagent = decodeURIComponent(m[1]!);","atom":"a1520","author":"trials","version":151},{"n":94,"text":"\t\t} catch {","atom":"a1520","author":"trials","version":151},{"n":95,"text":"\t\t\treturn json(400, { error: \"an agent's name is 1-64 of A-Z a-z 0-9 . _ -\" });","atom":"a1520","author":"trials","version":151},{"n":96,"text":"\t\t}","atom":"a1520","author":"trials","version":151},{"n":97,"text":"\t\tif (!AGENT_NAME.test(agent)) return json(400, { error: \"an agent's name is 1-64 of A-Z a-z 0-9 . _ -\" });","atom":"a1520","author":"trials","version":151},{"n":98,"text":"\t}","atom":"a1520","author":"trials","version":151},{"n":99,"text":"\tconst reading = request.method === \"GET\" || request.method === \"HEAD\";","atom":"a1520","author":"trials","version":151},{"n":100,"text":"\tif (auth.kind === \"key\") {","atom":"a1520","author":"trials","version":151},{"n":101,"text":"\t\tif (agent !== undefined && agent !== auth.agent)","atom":"a1520","author":"trials","version":151},{"n":102,"text":"\t\t\treturn json(403, { error: `this key is ${auth.agent}'s; it acts only as ${auth.agent}` });","atom":"a1520","author":"trials","version":151},{"n":103,"text":"\t\tif (auth.role === \"reader\" && !reading) return json(403, { error: \"a reader's key only reads issues\" });","atom":"a1520","author":"trials","version":151},{"n":104,"text":"\t}","atom":"a1520","author":"trials","version":151},{"n":105,"text":"\tlet body: Uint8Array<ArrayBuffer> | undefined;","atom":"a1520","author":"trials","version":151},{"n":106,"text":"\tif (!reading) {","atom":"a1520","author":"trials","version":151},{"n":107,"text":"\t\ttry {","atom":"a1520","author":"trials","version":151},{"n":108,"text":"\t\t\tbody = await readCapped(request, BODY_MAX);","atom":"a1520","author":"trials","version":151},{"n":109,"text":"\t\t} catch (e) {","atom":"a1520","author":"trials","version":151},{"n":110,"text":"\t\t\tif (e instanceof IssuesError) return json(e.status, { error: e.message });","atom":"a1520","author":"trials","version":151},{"n":111,"text":"\t\t\tthrow e;","atom":"a1520","author":"trials","version":151},{"n":112,"text":"\t\t}","atom":"a1520","author":"trials","version":151},{"n":113,"text":"\t}","atom":"a1520","author":"trials","version":151},{"n":114,"text":"\t// The object sees neither the token nor any header that could name a person.","atom":"a1520","author":"trials","version":151},{"n":115,"text":"\tconst headers = new Headers(request.headers);","atom":"a1520","author":"trials","version":151},{"n":116,"text":"\tfor (const h of [\"Authorization\", \"X-Abso-User\", \"X-Avcs-User\", \"Cookie\"]) headers.delete(h);","atom":"a1520","author":"trials","version":151},{"n":117,"text":"\tconst search = new URL(request.url).search;","atom":"a1520","author":"trials","version":151},{"n":118,"text":"\tconst forwarded = new Request(`https://issues.invalid/p/${encodeURIComponent(project)}${rest}${search}`, {","atom":"a1520","author":"trials","version":151},{"n":119,"text":"\t\tmethod: request.method,","atom":"a1520","author":"trials","version":151},{"n":120,"text":"\t\theaders,","atom":"a1520","author":"trials","version":151},{"n":121,"text":"\t\t...(body !== undefined ? { body } : {}),","atom":"a1520","author":"trials","version":151},{"n":122,"text":"\t});","atom":"a1520","author":"trials","version":151},{"n":123,"text":"\tconst stub = stubOf<{ fetch(request: Request): Promise<Response> }>(env.ISSUES_DO, project);","atom":"a1520","author":"trials","version":151},{"n":124,"text":"\tconst answer = await stub.fetch(forwarded);","atom":"a1520","author":"trials","version":151},{"n":125,"text":"\tif (!reading && agent !== undefined && answer.ok) ctx.waitUntil(tell(env, project, agent, answer.clone()));","atom":"a1520","author":"trials","version":151},{"n":126,"text":"\treturn answer;","atom":"a1520","author":"trials","version":151},{"n":127,"text":"}","atom":"a1520","author":"trials","version":151},{"n":128,"text":"","atom":"a1520","author":"trials","version":151},{"n":129,"text":"/** A write's news to the project object, sent after the answer. A failed send is only logged; the write stands. */","atom":"a1520","author":"trials","version":151},{"n":130,"text":"async function tell(env: IssuesEnv, project: string, from: string, answer: Response): Promise<void> {","atom":"a1520","author":"trials","version":151},{"n":131,"text":"\ttry {","atom":"a1520","author":"trials","version":151},{"n":132,"text":"\t\tconst { news } = (await answer.json()) as { news?: News };","atom":"a1520","author":"trials","version":151},{"n":133,"text":"\t\tif (!news || typeof news.text !== \"string\") return;","atom":"a1520","author":"trials","version":151},{"n":134,"text":"\t\tconst hooks = hooksOf(env, project);","atom":"a1520","author":"trials","version":151},{"n":135,"text":"\t\tif (hooks === undefined) {","atom":"a1520","author":"trials","version":151},{"n":136,"text":"\t\t\tconsole.log(`issues: news for ${project} dropped: PROJECT isn't bound`);","atom":"a1520","author":"trials","version":151},{"n":137,"text":"\t\t\treturn;","atom":"a1520","author":"trials","version":151},{"n":138,"text":"\t\t}","atom":"a1520","author":"trials","version":151},{"n":139,"text":"\t\tawait hooks.news!(project, [{ from, ...news }]);","atom":"a1520","author":"trials","version":151},{"n":140,"text":"\t} catch (err) {","atom":"a1520","author":"trials","version":151},{"n":141,"text":"\t\tif (missingMethod(err)) console.log(`issues: news for ${project} dropped: the project object has no news method`);","atom":"a1520","author":"trials","version":151},{"n":142,"text":"\t\telse console.error(`issues: news for ${project} not sent`, err);","atom":"a1520","author":"trials","version":151},{"n":143,"text":"\t}","atom":"a1520","author":"trials","version":151},{"n":144,"text":"}","atom":"a1520","author":"trials","version":151},{"n":145,"text":"","atom":"a1520","author":"trials","version":151},{"n":146,"text":"/** The storage an IssueProject object has: its SQL, and a synchronous transaction. */","atom":"a1520","author":"trials","version":151},{"n":147,"text":"export interface ObjectStorage {","atom":"a1520","author":"trials","version":151},{"n":148,"text":"\tsql: Sql;","atom":"a1520","author":"trials","version":151},{"n":149,"text":"\ttransactionSync<T>(action: () => T): T;","atom":"a1520","author":"trials","version":151},{"n":150,"text":"}","atom":"a1520","author":"trials","version":151},{"n":151,"text":"","atom":"a1520","author":"trials","version":151},{"n":152,"text":"/** One project's issues: the store over the object's SQLite, migrated, and the routes over it. */","atom":"a1520","author":"trials","version":151},{"n":153,"text":"export class IssuesObject {","atom":"a1520","author":"trials","version":151},{"n":154,"text":"\treadonly store: IssuesStore;","atom":"a1520","author":"trials","version":151},{"n":155,"text":"\tprivate readonly storage: ObjectStorage;","atom":"a1520","author":"trials","version":151},{"n":156,"text":"\tprivate readonly env: IssuesEnv;","atom":"a1520","author":"trials","version":151},{"n":157,"text":"","atom":"a1520","author":"trials","version":151},{"n":158,"text":"\tconstructor(storage: ObjectStorage, env: IssuesEnv, now: () => number = Date.now) {","atom":"a1520","author":"trials","version":151},{"n":159,"text":"\t\tthis.storage = storage;","atom":"a1520","author":"trials","version":151},{"n":160,"text":"\t\tthis.env = env;","atom":"a1520","author":"trials","version":151},{"n":161,"text":"\t\tthis.migrate();","atom":"a1520","author":"trials","version":151},{"n":162,"text":"\t\tthis.store = new IssuesStore(storage.sql, now, (action) => storage.transactionSync(action));","atom":"a1520","author":"trials","version":151},{"n":163,"text":"\t}","atom":"a1520","author":"trials","version":151},{"n":164,"text":"","atom":"a1520","author":"trials","version":151},{"n":165,"text":"\t/** Runs each SCHEMA statement not yet run, once, in order, each in its own transaction. */","atom":"a1520","author":"trials","version":151},{"n":166,"text":"\tprivate migrate(): void {","atom":"a1520","author":"trials","version":151},{"n":167,"text":"\t\tconst sql = this.storage.sql;","atom":"a1520","author":"trials","version":151},{"n":168,"text":"\t\tsql.exec(\"CREATE TABLE IF NOT EXISTS migrations (n INTEGER PRIMARY KEY, at INTEGER NOT NULL)\");","atom":"a1520","author":"trials","version":151},{"n":169,"text":"\t\tconst done = (sql.exec(\"SELECT COALESCE(MAX(n), 0) AS n FROM migrations\").toArray()[0] as { n: number }).n;","atom":"a1520","author":"trials","version":151},{"n":170,"text":"\t\tfor (let n = done + 1; n <= SCHEMA.length; n++)","atom":"a1520","author":"trials","version":151},{"n":171,"text":"\t\t\tthis.storage.transactionSync(() => {","atom":"a1520","author":"trials","version":151},{"n":172,"text":"\t\t\t\tsql.exec(SCHEMA[n - 1]!);","atom":"a1520","author":"trials","version":151},{"n":173,"text":"\t\t\t\tsql.exec(\"INSERT INTO migrations (n, at) VALUES (?, ?)\", n, Date.now());","atom":"a1520","author":"trials","version":151},{"n":174,"text":"\t\t\t});","atom":"a1520","author":"trials","version":151},{"n":175,"text":"\t}","atom":"a1520","author":"trials","version":151},{"n":176,"text":"","atom":"a1520","author":"trials","version":151},{"n":177,"text":"\t/** How many migrations this object has run. */","atom":"a1520","author":"trials","version":151},{"n":178,"text":"\tmigrations(): number {","atom":"a1520","author":"trials","version":151},{"n":179,"text":"\t\treturn (this.storage.sql.exec(\"SELECT COUNT(*) AS n FROM migrations\").toArray()[0] as { n: number }).n;","atom":"a1520","author":"trials","version":151},{"n":180,"text":"\t}","atom":"a1520","author":"trials","version":151},{"n":181,"text":"","atom":"a1520","author":"trials","version":151},{"n":182,"text":"\tasync fetch(request: Request): Promise<Response> {","atom":"a1520","author":"trials","version":151},{"n":183,"text":"\t\tconst name = /^\\/p\\/([^/]+)\\//.exec(new URL(request.url).pathname)?.[1];","atom":"a1520","author":"trials","version":151},{"n":184,"text":"\t\tconst project = name === undefined ? undefined : decodeURIComponent(name);","atom":"a1520","author":"trials","version":151},{"n":185,"text":"\t\tconst seatError = async (of: string): Promise<string | null> => {","atom":"a1520","author":"trials","version":151},{"n":186,"text":"\t\t\tif (project === undefined) return null;","atom":"a1520","author":"trials","version":151},{"n":187,"text":"\t\t\tconst hooks = hooksOf(this.env, project);","atom":"a1520","author":"trials","version":151},{"n":188,"text":"\t\t\tif (hooks === undefined) return null;","atom":"a1520","author":"trials","version":151},{"n":189,"text":"\t\t\tlet seats: string[];","atom":"a1520","author":"trials","version":151},{"n":190,"text":"\t\t\ttry {","atom":"a1520","author":"trials","version":151},{"n":191,"text":"\t\t\t\tseats = await hooks.agents!(project);","atom":"a1520","author":"trials","version":151},{"n":192,"text":"\t\t\t} catch (e) {","atom":"a1520","author":"trials","version":151},{"n":193,"text":"\t\t\t\tif (missingMethod(e)) return null;","atom":"a1520","author":"trials","version":151},{"n":194,"text":"\t\t\t\tthrow e;","atom":"a1520","author":"trials","version":151},{"n":195,"text":"\t\t\t}","atom":"a1520","author":"trials","version":151},{"n":196,"text":"\t\t\treturn Array.isArray(seats) ? seatRefusal(of, project, seats) : null;","atom":"a1520","author":"trials","version":151},{"n":197,"text":"\t\t};","atom":"a1520","author":"trials","version":151},{"n":198,"text":"\t\treturn issuesRoute(this.store, request, seatError);","atom":"a1520","author":"trials","version":151},{"n":199,"text":"\t}","atom":"a1520","author":"trials","version":151},{"n":200,"text":"}","atom":"a1520","author":"trials","version":151}],"atoms":[{"atom":"a1453","author":"speed","message":"","time":1791906745856,"timeIso":"2026-10-13T15:52:25.856Z","publish":{"draft":null,"version":150,"agent":"speed","message":"Open server: the project Worker, Durable Object and Artifacts (issue 17)","time":1791907522179,"timeIso":"2026-10-13T16:05:22.179Z"}},{"atom":"a1520","author":"trials","message":"","time":1791907770251,"timeIso":"2026-10-13T16:09:30.251Z","publish":{"draft":null,"version":151,"agent":"trials","message":"Open server: issues, asks and news, served at <server>/issues, and the client finds them there when no issues server is named","time":1791907770251,"timeIso":"2026-10-13T16:09:30.251Z"}}]}