Add TypeScript CultMesh catalog ergonomics

This commit is contained in:
Meta Vens 2026-06-16 00:27:16 +01:00
commit 8c91a5d687
4 changed files with 111 additions and 1 deletions

View file

@ -216,6 +216,10 @@ Current progress:
peer cards may advertise `authorityLeaseId`, but `CultMeshAuthorityLeaseCatalog`
owns role/shard/time authorization so discovery contact cannot impersonate
trust.
- TypeScript CultMesh local catalogs now expose the same practical discovery
ergonomics as the other runtimes: sorted local views, direct `get(...)`
lookup, unsubscribe-able `watch(...)` callbacks for Verse and peer catalogs,
plus lease listing and lookup beside the authority check.
- The remaining parity work is to deepen Python's server-side use of its port
and move each runtime's existing TCP/LiteNetLib/WebSocket bodies behind those
ports, then broaden RUDP interop into reconnect policy and the wider

View file

@ -6,6 +6,33 @@ native CultMesh stream implementation used by Mimir and Fensalir; this package
mirrors the typed declaration and negotiation shapes without owning the hot
frame body path.
## Local Catalogs
The TypeScript projection keeps the same local catalog ergonomics as the other
runtimes: sorted views, direct lookup, and unsubscribe-able watches.
```ts
import { CultMesh } from "cultmesh-ts";
const verses = CultMesh.createVerseCatalog<{ verseId: string; label: string }>();
const stopWatching = verses.watch((verse) => {
console.log("updated", verse.verseId);
});
verses.upsert("public", { verseId: "public", label: "Public Verse" });
stopWatching();
const peers = CultMesh.createPeerCatalog();
peers.upsert({
peerId: "ts-peer",
verseId: "public",
endpoints: ["rudp://127.0.0.1:4100"],
roles: ["read-replica"],
});
const readReplicas = peers.find("public", "read-replica");
```
## Streaming Mode
CultMesh streaming mode is for audio, video, tensor, and opaque byte frames that

View file

@ -61,14 +61,27 @@ export class CultMeshNode {
export class CultMeshVerseCatalog<TVerse = unknown> {
readonly #verses = new Map<string, TVerse>();
readonly #subscribers = new Set<(verse: TVerse) => void>();
public get verses(): readonly TVerse[] {
return [...this.#verses.values()];
return [...this.#verses.entries()]
.sort(([left], [right]) => left.localeCompare(right))
.map(([, verse]) => verse);
}
public watch(callback: (verse: TVerse) => void): () => void {
this.#subscribers.add(callback);
return () => {
this.#subscribers.delete(callback);
};
}
public upsert(verseId: string, verse: TVerse): void {
requireNonEmpty(verseId, "verseId");
this.#verses.set(verseId, verse);
for (const subscriber of [...this.#subscribers]) {
subscriber(verse);
}
}
public get(verseId: string): TVerse | undefined {
@ -88,6 +101,7 @@ export interface CultMeshPeerCard {
export class CultMeshPeerCatalog {
readonly #peers = new Map<string, CultMeshPeerCard>();
readonly #subscribers = new Set<(peer: CultMeshPeerCard) => void>();
public get peers(): readonly CultMeshPeerCard[] {
return [...this.#peers.values()].sort((left, right) =>
@ -95,10 +109,25 @@ export class CultMeshPeerCatalog {
);
}
public watch(callback: (peer: CultMeshPeerCard) => void): () => void {
this.#subscribers.add(callback);
return () => {
this.#subscribers.delete(callback);
};
}
public upsert(peer: CultMeshPeerCard): void {
requireNonEmpty(peer.peerId, "peer.peerId");
requireNonEmpty(peer.verseId, "peer.verseId");
this.#peers.set(peer.peerId, peer);
for (const subscriber of [...this.#subscribers]) {
subscriber(peer);
}
}
public get(peerId: string): CultMeshPeerCard | undefined {
requireNonEmpty(peerId, "peerId");
return this.#peers.get(peerId);
}
public find(verseId: string, role?: string): readonly CultMeshPeerCard[] {
@ -124,6 +153,12 @@ export interface CultMeshAuthorityLease {
export class CultMeshAuthorityLeaseCatalog {
readonly #leases = new Map<string, CultMeshAuthorityLease>();
public get leases(): readonly CultMeshAuthorityLease[] {
return [...this.#leases.values()].sort((left, right) =>
left.leaseId.localeCompare(right.leaseId),
);
}
public upsert(lease: CultMeshAuthorityLease): void {
requireNonEmpty(lease.leaseId, "lease.leaseId");
requireNonEmpty(lease.verseId, "lease.verseId");
@ -135,6 +170,11 @@ export class CultMeshAuthorityLeaseCatalog {
this.#leases.set(lease.leaseId, lease);
}
public get(leaseId: string): CultMeshAuthorityLease | undefined {
requireNonEmpty(leaseId, "leaseId");
return this.#leases.get(leaseId);
}
public isAuthorized(
peer: CultMeshPeerCard,
role: string,

View file

@ -44,6 +44,10 @@ test("CultMesh TS opens a durable local node and registers document bindings", a
test("CultMesh TS local authority leases do not trust peer cards by contact alone", () => {
const peers = CultMesh.createPeerCatalog();
const leases = CultMesh.createAuthorityLeaseCatalog();
const peerUpdates: string[] = [];
const unsubscribePeer = peers.watch((peerCard) => {
peerUpdates.push(peerCard.peerId);
});
const peer = {
peerId: "voidbot-local",
verseId: "local",
@ -53,7 +57,16 @@ test("CultMesh TS local authority leases do not trust peer cards by contact alon
};
peers.upsert(peer);
unsubscribePeer();
peers.upsert({
peerId: "unused-peer",
verseId: "local",
endpoints: ["cultmesh://unused"],
});
assert.deepEqual(peerUpdates, ["voidbot-local"]);
assert.equal(peers.get("voidbot-local"), peer);
assert.deepEqual(peers.find("local", "shard-primary"), [peer]);
assert.equal(leases.isAuthorized(peer, "shard-primary"), false);
leases.upsert({
@ -65,9 +78,35 @@ test("CultMesh TS local authority leases do not trust peer cards by contact alon
expiresAt: new Date(Date.now() + 1000),
});
assert.equal(leases.get("lease:voidbot-local")?.peerId, "voidbot-local");
assert.deepEqual(leases.leases.map((lease) => lease.leaseId), [
"lease:voidbot-local",
]);
assert.equal(leases.isAuthorized(peer, "shard-primary"), true);
});
test("CultMesh TS local Verse catalog exposes sorted views and watch ergonomics", () => {
const verses = CultMesh.createVerseCatalog<{ verseId: string; label: string }>();
const updates: string[] = [];
const unsubscribe = verses.watch((verse) => {
updates.push(verse.verseId);
});
const publicVerse = { verseId: "public", label: "Public Verse" };
const privateVerse = { verseId: "private", label: "Private Verse" };
verses.upsert(publicVerse.verseId, publicVerse);
verses.upsert(privateVerse.verseId, privateVerse);
unsubscribe();
verses.upsert("scratch", { verseId: "scratch", label: "Scratch" });
assert.deepEqual(updates, ["public", "private"]);
assert.equal(verses.get("public"), publicVerse);
assert.deepEqual(
verses.verses.map((verse) => verse.verseId),
["private", "public", "scratch"],
);
});
test("CultMesh TS negotiates streaming frame body transports explicitly", () => {
const streams = CultMesh.createStreamCatalog();
streams.declare({