From 8758a65e8e7c43849150e234095b31e805dad7cd Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Sat, 27 Jun 2026 15:46:25 +0100 Subject: [PATCH] Add single-file document handles --- .../single-file-messagepack-backing-store.ts | 28 ++++++- packages/cultmesh-ts/src/index.ts | 82 +++++++++++++++++++ packages/cultmesh-ts/test/cultmesh.test.ts | 43 ++++++++++ 3 files changed, 152 insertions(+), 1 deletion(-) diff --git a/packages/cultcache-ts/src/single-file-messagepack-backing-store.ts b/packages/cultcache-ts/src/single-file-messagepack-backing-store.ts index 58dbd48d..4d2bb4f9 100644 --- a/packages/cultcache-ts/src/single-file-messagepack-backing-store.ts +++ b/packages/cultcache-ts/src/single-file-messagepack-backing-store.ts @@ -166,7 +166,7 @@ export class SingleFileMessagePackBackingStore implements CacheBackingStore { try { await writeFile(tempPath, encode(encodeSnapshot(entries))); - await rename(tempPath, this.filePath); + await renameWithRetry(tempPath, this.filePath); } catch (error) { await rm(tempPath, { force: true }).catch(() => undefined); throw error; @@ -174,6 +174,32 @@ export class SingleFileMessagePackBackingStore implements CacheBackingStore { } } +async function renameWithRetry(source: string, destination: string): Promise { + let delayMs = 10; + let lastError: unknown; + for (let attempt = 0; attempt < 10; attempt++) { + try { + await rename(source, destination); + return; + } catch (error) { + const code = (error as NodeJS.ErrnoException).code; + if (code !== "EPERM" && code !== "EBUSY" && code !== "EACCES") { + throw error; + } + + lastError = error; + await delay(delayMs); + delayMs *= 2; + } + } + + throw lastError; +} + +function delay(milliseconds: number): Promise { + return new Promise((resolve) => setTimeout(resolve, milliseconds)); +} + type PersistedRecord = { key: string; schemaId: string; diff --git a/packages/cultmesh-ts/src/index.ts b/packages/cultmesh-ts/src/index.ts index 545a0e14..5ed8de31 100644 --- a/packages/cultmesh-ts/src/index.ts +++ b/packages/cultmesh-ts/src/index.ts @@ -1828,6 +1828,62 @@ export function cultMeshDocumentFromCache { + requireNonEmpty(schemaId, "schemaId"); + const documentId = options.documentId ?? schemaId; + const read = async () => { + const records = await store.pullAll(); + const record = records.find(candidate => candidate.schemaId === schemaId); + if (!record) { + throw new Error(`CultMesh store document '${documentId}' did not contain schema ${schemaId}.`); + } + return decode(record.payload); + }; + + return cultMeshDocument( + documentId, + { schemaId }, + async () => read(), + { + routeHint: options.routeHint ?? cultMeshRouteHint("in-process", "CultCache store"), + sources: [cultMeshProjectionSource(options.sourceId ?? documentId, { schemaId })], + watchDocument: cultMeshPollingDocumentWatcher(read, { + intervalMs: options.pollMs ?? 50, + }), + }, + ); +} + +export function cultMeshDocumentFromSingleFile( + path: string, + schemaId: string, + options: { + documentId?: string; + routeHint?: CultMeshRouteHint; + sourceId?: string; + pollMs?: number; + } = {}, +): CultMeshDocumentHandle { + requireNonEmpty(path, "path"); + return cultMeshDocumentFromStore( + new SingleFileMessagePackBackingStore(path), + schemaId, + { + ...options, + routeHint: options.routeHint ?? cultMeshRouteHint("shared-memory", path), + }, + ); +} + export function cultMeshGlobalDocumentFromCache( cache: CultCache, definition: TDefinition, @@ -3509,6 +3565,32 @@ export class CultMesh { return cultMeshDocumentFromCache(cache, definition, key, options); } + public static documentFromStore( + store: CacheBackingStore, + schemaId: string, + options: { + documentId?: string; + routeHint?: CultMeshRouteHint; + sourceId?: string; + pollMs?: number; + } = {}, + ): CultMeshDocumentHandle { + return cultMeshDocumentFromStore(store, schemaId, options); + } + + public static documentFromSingleFile( + path: string, + schemaId: string, + options: { + documentId?: string; + routeHint?: CultMeshRouteHint; + sourceId?: string; + pollMs?: number; + } = {}, + ): CultMeshDocumentHandle { + return cultMeshDocumentFromSingleFile(path, schemaId, options); + } + public static globalDocumentFromCache( cache: CultCache, definition: TDefinition, diff --git a/packages/cultmesh-ts/test/cultmesh.test.ts b/packages/cultmesh-ts/test/cultmesh.test.ts index 7c79bae7..dbb1e1d2 100644 --- a/packages/cultmesh-ts/test/cultmesh.test.ts +++ b/packages/cultmesh-ts/test/cultmesh.test.ts @@ -115,6 +115,49 @@ test("CultMesh TS document handles hide local cache plumbing behind typed reacti ); }); +test("CultMesh TS document handles read schema publications from single-file stores", async () => { + const filePath = join(await mkdtemp(join(tmpdir(), "cultmesh-ts-publication-")), "publication.ccmp"); + const node = await CultMesh.startNode(filePath, { + documents: [noteDocument], + }); + await node.put(noteDocument, "note:publication", { + noteId: "note:publication", + body: "published", + }); + await node.flush(); + + const document = CultMesh.documentFromSingleFile(filePath, "cultmesh.note.v0", { + documentId: "daemon:cultmesh.note.latest", + sourceId: "daemon:cultmesh.note.latest.v0", + pollMs: 50, + }); + const observed: string[] = []; + const unsubscribe = document.watch(value => { + observed.push(noteDocument.schema.parse(value).body); + }); + + try { + assert.equal(noteDocument.schema.parse(await document.latest()).body, "published"); + + const republisher = await CultMesh.startNode(filePath, { + documents: [noteDocument], + }); + await republisher.put(noteDocument, "note:publication", { + noteId: "note:publication", + body: "republished", + }); + await waitFor(() => observed.includes("republished"), "single-file document watch update"); + } finally { + unsubscribe(); + } + + assert.equal(document.documentId, "daemon:cultmesh.note.latest"); + assert.equal(document.routeHint.kind, "shared-memory"); + assert.deepEqual(document.sources.map(source => source.schemaId), [ + "cultmesh.note.v0", + ]); +}); + test("CultMesh TS collection handles expose typed snapshots and reset watches", async () => { const filePath = join(await mkdtemp(join(tmpdir(), "cultmesh-ts-coll-")), "node.ccmp"); const node = await CultMesh.startNode(filePath, {