From a0509f223b330eadb91954b2d3bd080721d058f5 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 27 Jun 2026 16:45:14 +0100 Subject: [PATCH] Type TS peer snapshot document handles --- packages/cultmesh-ts/README.md | 6 +- packages/cultmesh-ts/src/index.ts | 101 ++++++++++++++++++--- packages/cultmesh-ts/test/cultmesh.test.ts | 7 +- 3 files changed, 97 insertions(+), 17 deletions(-) diff --git a/packages/cultmesh-ts/README.md b/packages/cultmesh-ts/README.md index b3ebbf8a..25e84c3f 100644 --- a/packages/cultmesh-ts/README.md +++ b/packages/cultmesh-ts/README.md @@ -288,7 +288,7 @@ await client.syncSchemaCatalog(schemas, { kinds: ["document_payload"] }); const health = CultMesh.documentFromPeerSnapshot( () => client, - "gamecult.aetheria.daemon_health.v1", + daemonHealthDocument, "daemon:aetheria.health.v1", { routeHint: CultMesh.routeHint("network", endpoint.uri), @@ -313,7 +313,9 @@ payload binary normalization, and MessagePack decode behind the same `CultMeshDocumentHandle` shape as local documents. The helper prefers an exact schema id match and falls back to the requested record key, so runtimes that alias the same logical document through different generated schema names can -still read the publication with one CultMesh call. +still read the publication with one CultMesh call. Pass a CultCache document +definition instead of a raw schema id when you want a typed handle whose +`latest()` result is parsed through that definition. ## Streaming Mode diff --git a/packages/cultmesh-ts/src/index.ts b/packages/cultmesh-ts/src/index.ts index bd1c6ba9..b00ffaaa 100644 --- a/packages/cultmesh-ts/src/index.ts +++ b/packages/cultmesh-ts/src/index.ts @@ -2003,6 +2003,32 @@ export function cultMeshDocumentFromPeerSnapshot( peer: CultNetPeer | (() => CultNetPeer | Promise), schemaId: string, recordKey: string, + options?: { + documentId?: string; + routeHint?: CultMeshRouteHint; + sourceId?: string; + timeoutMs?: number; + pollMs?: number; + messageIdPrefix?: string; + }, +): CultMeshDocumentHandle; +export function cultMeshDocumentFromPeerSnapshot( + peer: CultNetPeer | (() => CultNetPeer | Promise), + definition: TDefinition, + recordKey: string, + options?: { + documentId?: string; + routeHint?: CultMeshRouteHint; + sourceId?: string; + timeoutMs?: number; + pollMs?: number; + messageIdPrefix?: string; + }, +): CultMeshDocumentHandle>; +export function cultMeshDocumentFromPeerSnapshot( + peer: CultNetPeer | (() => CultNetPeer | Promise), + schemaOrDefinition: string | AnyCultCacheDocumentDefinition, + recordKey: string, options: { documentId?: string; routeHint?: CultMeshRouteHint; @@ -2012,29 +2038,41 @@ export function cultMeshDocumentFromPeerSnapshot( messageIdPrefix?: string; } = {}, ): CultMeshDocumentHandle { - requireNonEmpty(schemaId, "schemaId"); + const schema = typeof schemaOrDefinition === "string" + ? { schemaId: schemaOrDefinition } + : cultMeshSchemaFromDefinition(schemaOrDefinition); + const resolvedSchemaId = schema.schemaId ?? schema.type; + if (!resolvedSchemaId) { + throw new Error("CultMesh peer snapshot document requires a schema id or document type."); + } + if (typeof schemaOrDefinition === "string") { + requireNonEmpty(schemaOrDefinition, "schemaId"); + } requireNonEmpty(recordKey, "recordKey"); const documentId = options.documentId ?? recordKey; const read = async () => - decodeCultNetRawDocumentPayload( - await requestCultNetRawSnapshotDocument( - peer, - schemaId, - recordKey, - { - timeoutMs: options.timeoutMs, - messageIdPrefix: options.messageIdPrefix ?? documentId, - }, + parseCultMeshPeerSnapshotDocument( + schemaOrDefinition, + decodeCultNetRawDocumentPayload( + await requestCultNetRawSnapshotDocument( + peer, + resolvedSchemaId, + recordKey, + { + timeoutMs: options.timeoutMs, + messageIdPrefix: options.messageIdPrefix ?? documentId, + }, + ), ), ); return cultMeshDocument( documentId, - { schemaId }, + schema, async () => read(), { routeHint: options.routeHint ?? cultMeshRouteHint("network", "CultNet snapshot"), - sources: [cultMeshProjectionSource(options.sourceId ?? documentId, { schemaId })], + sources: [cultMeshProjectionSource(options.sourceId ?? documentId, { schemaId: resolvedSchemaId })], watchDocument: cultMeshPollingDocumentWatcher(read, { intervalMs: options.pollMs ?? 250, }), @@ -3754,6 +3792,32 @@ export class CultMesh { peer: CultNetPeer | (() => CultNetPeer | Promise), schemaId: string, recordKey: string, + options?: { + documentId?: string; + routeHint?: CultMeshRouteHint; + sourceId?: string; + timeoutMs?: number; + pollMs?: number; + messageIdPrefix?: string; + }, + ): CultMeshDocumentHandle; + public static documentFromPeerSnapshot( + peer: CultNetPeer | (() => CultNetPeer | Promise), + definition: TDefinition, + recordKey: string, + options?: { + documentId?: string; + routeHint?: CultMeshRouteHint; + sourceId?: string; + timeoutMs?: number; + pollMs?: number; + messageIdPrefix?: string; + }, + ): CultMeshDocumentHandle>; + public static documentFromPeerSnapshot( + peer: CultNetPeer | (() => CultNetPeer | Promise), + schemaOrDefinition: string | AnyCultCacheDocumentDefinition, + recordKey: string, options: { documentId?: string; routeHint?: CultMeshRouteHint; @@ -3763,7 +3827,9 @@ export class CultMesh { messageIdPrefix?: string; } = {}, ): CultMeshDocumentHandle { - return cultMeshDocumentFromPeerSnapshot(peer, schemaId, recordKey, options); + return typeof schemaOrDefinition === "string" + ? cultMeshDocumentFromPeerSnapshot(peer, schemaOrDefinition, recordKey, options) + : cultMeshDocumentFromPeerSnapshot(peer, schemaOrDefinition, recordKey, options); } public static globalDocumentFromCache( @@ -4862,6 +4928,15 @@ function decodeCultNetRawDocumentPayload(document: CultNetRawDocumentRecord): un return decode(toUint8Array(document.payload)); } +function parseCultMeshPeerSnapshotDocument( + schemaOrDefinition: string | TDefinition, + value: unknown, +): unknown | CultCacheDocumentValue { + return typeof schemaOrDefinition === "string" + ? value + : schemaOrDefinition.schema.parse(value); +} + function isCultNetErrorMessage(message: CultNetMessage): message is CultNetErrorMessage { return message.schemaVersion === "cultnet.error.v0"; } diff --git a/packages/cultmesh-ts/test/cultmesh.test.ts b/packages/cultmesh-ts/test/cultmesh.test.ts index 219e3eb5..08196d43 100644 --- a/packages/cultmesh-ts/test/cultmesh.test.ts +++ b/packages/cultmesh-ts/test/cultmesh.test.ts @@ -1950,7 +1950,7 @@ test("CultMesh TS reads remote RUDP snapshots through document handles", async ( const document = CultMesh.documentFromPeerSnapshot( peer, - "cultmesh.note.v0", + noteDocument, "note:remote", { documentId: "note:remote", @@ -1958,10 +1958,13 @@ test("CultMesh TS reads remote RUDP snapshots through document handles", async ( }, ); - assert.deepEqual(await document.latest(), { + const latest = await document.latest(); + assert.equal(latest.body, "remote handles feel local"); + assert.deepEqual(latest, { noteId: "note:remote", body: "remote handles feel local", }); + assert.equal(document.schema.type, "cultmesh.note"); const alias = CultMesh.documentFromPeerSnapshot( peer,