Files
mesh-catalog/modules/gitea/test/fakebus.ts
T
jschoubben 39ed20c5bc C4: the forge module's tokens live in the vault, one process mints at a time
Two processes that minted at once left the forge two tokens by one name, and a token kept in a file in the
runtime state is a plain state. Both accounts' tokens (mesh-tools, mesh-packages) are now the module's own
secrets in the vault, written through the seat mesh-vault's own-put and read through its get, sealed to the
reading process's key. A refused or expired (token-ttl) token is minted again only by the holder of the lease
mint in the state token-lease (mint-lease); each mint is named <account>-<generation>, and the token it
replaces is deleted by id only after own-put landed. The Go bundles read their tokens through get at start
and at secret-changed; the MESH_GITEA_*_TOKEN_FILE words go. forge_token_lease reads the lease and each
token's age, never the token.

Builds: design 47 H2 §808, §822; H-A-407, H-A-422, H-A-426, H-A-430 (1)
2026-11-22 01:41:30 +01:00

97 lines
4.8 KiB
TypeScript

// The bus as the forge module's token needs it, in memory (design 47 H2 §808, §822): the vault seat's `own-put` in its
// two sealed steps and its `get` sealed to the asker's key, and the runtime's state with a create-only key that lives
// its TTL, as the runtime that launched the bundle answers them. `order` records each own-put and each forge delete a
// test also writes into it, so the delete's place after the own-put can be read.
import nacl from "tweetnacl";
import blakejs from "blakejs";
import { seal } from "@novox/mesh-sdk/seal";
import { useBroker, type Broker } from "@novox/mesh-sdk/messaging";
import { useRuntime } from "@novox/mesh-sdk/runtime";
import { useStateChannel, type StateChange } from "@novox/mesh-sdk/state";
export interface Held { value: string; generation: number; changedAt: string }
export function fakeBus(now: () => Date = () => new Date(), order: string[] = []) {
const vault = new Map<string, Held>();
const calls = new Map<string, { name: string; secretKey: Uint8Array }>();
const kv = new Map<string, { value: unknown; until: number | null }>();
const watchers: { state: string; key: string; deliver: (c: StateChange<unknown>) => Promise<void> }[] = [];
let revision = 0;
const keyOf = (state: string, key: string) => `${state}/${key}`;
const live = (k: string) => {
const e = kv.get(k);
if (e && e.until !== null && e.until <= now().getTime()) kv.delete(k);
return kv.get(k);
};
const tell = async (state: string, key: string, op: "put" | "delete", value?: unknown) => {
revision++;
for (const w of watchers) if (w.state === state && (w.key === "" || w.key === key)) await w.deliver({ state, key, op, value, revision, current: false });
};
const broker: Broker = {
async request<Req, Res>(key: string, body: Req): Promise<Res> {
const b = body as Record<string, string>;
if (key === "seat:mesh-vault.own-put" && b.call === undefined) {
const keys = nacl.box.keyPair();
const call = `call-${calls.size + 1}`;
calls.set(call, { name: `module/gitea/${b.name}`, secretKey: keys.secretKey });
return { call, seal_to: Buffer.from(keys.publicKey).toString("base64"), secret: `module/gitea/${b.name}` } as Res;
}
if (key === "seat:mesh-vault.own-put") {
const c = calls.get(b.call);
if (!c) throw new Error(`no own-put call ${b.call}`);
calls.delete(b.call);
const boxed = new Uint8Array(Buffer.from(b.sealed, "base64"));
const eph = boxed.subarray(0, 32);
const pub = nacl.box.keyPair.fromSecretKey(c.secretKey).publicKey;
const nonce = blakejs.blake2b(Buffer.concat([eph, pub]), undefined, 24);
const opened = nacl.box.open(boxed.subarray(32), nonce, eph, c.secretKey);
if (!opened) throw new Error("the sealed value does not open");
const generation = (vault.get(c.name)?.generation ?? 0) + 1;
vault.set(c.name, { value: Buffer.from(opened).toString("utf8"), generation, changedAt: now().toISOString() });
order.push(`own-put ${c.name} ${generation}`);
return { name: c.name, generation } as Res;
}
if (key === "seat:mesh-vault.get") {
const held = vault.get(b.name);
if (!held) throw new Error(`the vault holds no secret named ${b.name}`);
return { name: b.name, generation: held.generation, "changed-at": held.changedAt, sealed: seal(held.value, b.public_key) } as Res;
}
throw new Error(`not played here: ${key}`);
},
handle: async () => () => {},
publish: async () => {},
subscribe: async () => () => {},
close: async () => {},
};
const ask = async (method: string, params: unknown): Promise<unknown> => {
const p = params as { state: string; key: string; value?: unknown; ttl_seconds?: number };
const k = keyOf(p.state, p.key);
switch (method) {
case "mesh/state.create":
if (live(k)) throw Object.assign(new Error("the key has a value"), { code: -32010 });
kv.set(k, { value: p.value, until: p.ttl_seconds ? now().getTime() + p.ttl_seconds * 1000 : null });
await tell(p.state, p.key, "put", p.value);
return { revision };
case "mesh/state.get": {
const e = live(k);
return e ? { key: p.key, value: e.value, revision } : null;
}
case "mesh/state.put":
kv.set(k, { value: p.value, until: null });
await tell(p.state, p.key, "put", p.value);
return { revision };
case "mesh/state.delete":
kv.delete(k);
await tell(p.state, p.key, "delete");
return null;
default:
throw new Error(`not played here: ${method}`);
}
};
useBroker(() => broker);
useRuntime(ask);
useStateChannel({ ask, watch: async (params, deliver) => { const w = params as { state: string; key: string }; watchers.push({ ...w, deliver }); } });
return { vault, kv, order, ask };
}