Add single-file document handles

This commit is contained in:
Meta Vens 2026-06-27 15:46:25 +01:00
commit 8758a65e8e
3 changed files with 152 additions and 1 deletions

View file

@ -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<void> {
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<void> {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
type PersistedRecord = {
key: string;
schemaId: string;

View file

@ -1828,6 +1828,62 @@ export function cultMeshDocumentFromCache<TDefinition extends AnyCultCacheDocume
);
}
export function cultMeshDocumentFromStore(
store: CacheBackingStore,
schemaId: string,
options: {
documentId?: string;
routeHint?: CultMeshRouteHint;
sourceId?: string;
pollMs?: number;
} = {},
): CultMeshDocumentHandle<unknown> {
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<unknown> {
requireNonEmpty(path, "path");
return cultMeshDocumentFromStore(
new SingleFileMessagePackBackingStore(path),
schemaId,
{
...options,
routeHint: options.routeHint ?? cultMeshRouteHint("shared-memory", path),
},
);
}
export function cultMeshGlobalDocumentFromCache<TDefinition extends AnyCultCacheDocumentDefinition>(
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<unknown> {
return cultMeshDocumentFromStore(store, schemaId, options);
}
public static documentFromSingleFile(
path: string,
schemaId: string,
options: {
documentId?: string;
routeHint?: CultMeshRouteHint;
sourceId?: string;
pollMs?: number;
} = {},
): CultMeshDocumentHandle<unknown> {
return cultMeshDocumentFromSingleFile(path, schemaId, options);
}
public static globalDocumentFromCache<TDefinition extends AnyCultCacheDocumentDefinition>(
cache: CultCache,
definition: TDefinition,

View file

@ -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, {