From 7a335b1a1178137d1b8123e33351022b39b742bb Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 27 Jun 2026 23:27:01 +0100 Subject: [PATCH] Add TS typed publication node sync --- packages/cultmesh-ts/README.md | 4 ++ packages/cultmesh-ts/src/index.ts | 56 ++++++++++++++++++++++ packages/cultmesh-ts/test/cultmesh.test.ts | 43 +++++++++++++++++ 3 files changed, 103 insertions(+) diff --git a/packages/cultmesh-ts/README.md b/packages/cultmesh-ts/README.md index 714ac2fa..279e271e 100644 --- a/packages/cultmesh-ts/README.md +++ b/packages/cultmesh-ts/README.md @@ -364,6 +364,10 @@ When the caller wants source selection to be configuration, use `CultMesh.documentFromPublication(...)` with a `single-file`, `store`, or `peer-snapshot` source; it returns the same typed handle while hiding whether sync came from local persistence or a remote CultNet snapshot. +Use `node.syncDocumentFromPublication(...)` or +`CultMesh.syncDocumentFromPublication(...)` for the same source-agnostic shape +when the publication should hydrate a local node before the caller continues +through `node.document(...)` or `node.reactiveDocument(...)`. ## Streaming Mode diff --git a/packages/cultmesh-ts/src/index.ts b/packages/cultmesh-ts/src/index.ts index ab1b4779..d817370f 100644 --- a/packages/cultmesh-ts/src/index.ts +++ b/packages/cultmesh-ts/src/index.ts @@ -2756,6 +2756,37 @@ export function cultMeshDocumentFromPublication( } } +export async function cultMeshSyncDocumentFromPublication( + node: CultMeshNode, + source: CultMeshDocumentPublicationSource, + definition: TDefinition, + recordKey: string, + options: { + timeoutMs?: number; + messageIdPrefix?: string; + } = {}, +): Promise> { + if (!source) { + throw new Error("CultMesh publication source is required."); + } + requireNonEmpty(recordKey, "recordKey"); + + if (source.kind === "peer-snapshot") { + return node.syncDocumentFromPeerSnapshot(source.peer, definition, recordKey, options); + } + + const value = await cultMeshDocumentFromPublication( + source, + definition, + recordKey, + { + timeoutMs: options.timeoutMs, + messageIdPrefix: options.messageIdPrefix, + }, + ).latest(); + return node.put(definition, recordKey, value); +} + export function cultMeshGlobalDocumentFromCache( cache: CultCache, definition: TDefinition, @@ -3855,6 +3886,18 @@ export class CultMeshNode { return this.parseDocumentValue(definition, this.cache.getRequired(registered, key)); } + public syncDocumentFromPublication( + source: CultMeshDocumentPublicationSource, + definition: TDefinition, + key: string, + options: { + timeoutMs?: number; + messageIdPrefix?: string; + } = {}, + ): Promise> { + return cultMeshSyncDocumentFromPublication(this, source, definition, key, options); + } + public globalDocument( definition: TDefinition, options: { @@ -4845,6 +4888,19 @@ export class CultMesh { return node.syncDocumentFromPeerSnapshot(peer, definition, key, options); } + public static syncDocumentFromPublication( + node: CultMeshNode, + source: CultMeshDocumentPublicationSource, + definition: TDefinition, + key: string, + options: { + timeoutMs?: number; + messageIdPrefix?: string; + } = {}, + ): Promise> { + return node.syncDocumentFromPublication(source, definition, key, options); + } + public static documents( ...documents: readonly CultMeshDocumentHandle[] ): CultMeshDocumentCatalog { diff --git a/packages/cultmesh-ts/test/cultmesh.test.ts b/packages/cultmesh-ts/test/cultmesh.test.ts index 0a859861..7bd1d8e0 100644 --- a/packages/cultmesh-ts/test/cultmesh.test.ts +++ b/packages/cultmesh-ts/test/cultmesh.test.ts @@ -679,6 +679,49 @@ test("CultMesh TS binds publication document catalogs from source resolvers", as assert.equal(catalog.document(noteDocument).routeHint.description, "publication catalog"); }); +test("CultMesh TS syncs configured publications into local node aliases", async () => { + const sourcePath = join(await mkdtemp(join(tmpdir(), "cultmesh-ts-publication-sync-source-")), "source.ccmp"); + const targetPath = join(await mkdtemp(join(tmpdir(), "cultmesh-ts-publication-sync-target-")), "target.ccmp"); + const source = await CultMesh.startNode(sourcePath, { + documents: [noteDocument], + }); + await source.put(noteDocument, "note:published", { + noteId: "note:published", + body: "publication source hydrates local alias", + }); + await source.flush(); + const target = await CultMesh.startNode(targetPath, { + documents: [noteDocument], + }); + + const synced = await target.syncDocumentFromPublication( + { + kind: "single-file", + path: sourcePath, + }, + noteAliasDocument, + "note:published", + ); + const facadeSynced = await CultMesh.syncDocumentFromPublication( + target, + { + kind: "single-file", + path: sourcePath, + }, + noteAliasDocument, + "note:published", + ); + + assert.deepEqual(synced, { + noteId: "note:published", + body: "publication source hydrates local alias", + }); + assert.deepEqual(facadeSynced, synced); + assert.equal(target.getRequired(noteDocument, "note:published").body, "publication source hydrates local alias"); + assert.equal(target.getRequired(noteAliasDocument, "note:published").body, "publication source hydrates local alias"); + assert.equal((await target.reactiveDocument(noteAliasDocument, "note:published", { watch: false }).ready).body, "publication source hydrates local alias"); +}); + test("CultMesh TS document catalogs resolve semantic schema versions passed as schema ids", async () => { const current = { noteId: "note:semantic",