From 57c9125e10c346085b3987d1cac78faaed7ae45c Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Tue, 23 Jun 2026 22:45:32 +0100 Subject: [PATCH] Add CultMesh RUDP document publisher --- packages/cultmesh-ts/src/index.ts | 41 +++++++++++++ packages/cultmesh-ts/test/cultmesh.test.ts | 70 +++++++++++++++++++++- 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/packages/cultmesh-ts/src/index.ts b/packages/cultmesh-ts/src/index.ts index 65627178..53225705 100644 --- a/packages/cultmesh-ts/src/index.ts +++ b/packages/cultmesh-ts/src/index.ts @@ -148,6 +148,15 @@ export interface CultMeshRudpDocumentServer { close(): void; } +export interface CultMeshRudpDocumentPublishOptions extends CultMeshRudpPeerOptions { + messageId?: string; + sourceRuntimeId?: string; + sourceAgentId?: string; + sourceRole?: string; + tags?: string[]; + flushTimeoutMs?: number; +} + export interface CultMeshRudpEndpoint { host: string; port: number; @@ -800,6 +809,38 @@ export class CultMesh { return { host, port, uri: `rudp://${uriHost}:${port}` }; } + public static async publishRudpDocumentOnce( + runtimeId: string, + connectionId: number, + endpoint: string | CultMeshRudpEndpoint, + binding: CultNetDocumentBinding, + recordKey: string, + value: CultCacheDocumentValue, + options: CultMeshRudpDocumentPublishOptions = {}, + ): Promise { + requireNonEmpty(runtimeId, "runtimeId"); + requireNonEmpty(recordKey, "recordKey"); + const registry = new CultNetDocumentRegistry([binding]); + const peer = await CultMesh.createRudpPeer(runtimeId, connectionId, endpoint, options); + try { + peer.send(registry.createRawDocumentPutMessage( + binding, + options.messageId ?? `${runtimeId}:${binding.definition.type}:${recordKey}`, + recordKey, + value, + { + sourceRuntimeId: options.sourceRuntimeId ?? runtimeId, + sourceAgentId: options.sourceAgentId, + sourceRole: options.sourceRole, + tags: options.tags, + }, + )); + await new Promise((resolve) => setTimeout(resolve, Math.max(0, options.flushTimeoutMs ?? 150))); + } finally { + peer.close(); + } + } + public static async createRudpServer( runtimeId: string, connectionId: number, diff --git a/packages/cultmesh-ts/test/cultmesh.test.ts b/packages/cultmesh-ts/test/cultmesh.test.ts index f8bd4ee1..c14d3a47 100644 --- a/packages/cultmesh-ts/test/cultmesh.test.ts +++ b/packages/cultmesh-ts/test/cultmesh.test.ts @@ -6,7 +6,12 @@ import assert from "node:assert/strict"; import { encode } from "@msgpack/msgpack"; import { z } from "zod"; import { defineDocumentType } from "cultcache-ts"; -import { CultNetDocumentRegistry, CultNetPeer, cultNetBuiltinSchemaRegistry } from "cultnet-ts"; +import { + CultNetDocumentRegistry, + CultNetPeer, + cultNetBuiltinSchemaRegistry, + defineCultNetDocumentBinding, +} from "cultnet-ts"; import { CultMesh } from "../src/index"; const noteDocument = defineDocumentType({ @@ -469,6 +474,69 @@ test("CultMesh TS RUDP document server accepts raw document puts from multiple p } }); +test("CultMesh TS publishes one registered document to a RUDP catalog", async () => { + const connectionId = 0x10203047; + let received: { schemaId: string; recordKey: string; payload: unknown; sourceRuntimeId: string | null } | undefined; + const server = CultMesh.createRudpDocumentServer( + "cultmesh-ts-odin-catalog", + connectionId, + { + documents: new CultNetDocumentRegistry(), + bindHost: "127.0.0.1", + bindPort: 0, + resendDelayMs: 25, + resendPollMs: 5, + maxFragmentBytes: 1024, + maxPendingReliablePackets: 16, + onDocumentPutRaw: (document) => { + received = { + schemaId: document.schemaId, + recordKey: document.recordKey, + payload: document.payload, + sourceRuntimeId: document.sourceRuntimeId, + }; + }, + }, + ); + + try { + await server.start(); + await CultMesh.publishRudpDocumentOnce( + "cultmesh-ts-muninn", + connectionId, + `rudp://127.0.0.1:${server.bind.port}`, + defineCultNetDocumentBinding({ definition: noteDocument }), + "note:odin", + { + noteId: "note:odin", + body: "pay respects once", + }, + { + resendDelayMs: 25, + resendPollMs: 5, + maxFragmentBytes: 1024, + maxPendingReliablePackets: 16, + connectTimeoutMs: 1_000, + flushTimeoutMs: 25, + sourceRole: "test-provider", + }, + ); + + await waitFor(() => received !== undefined, "published RUDP document"); + assert.deepEqual(received, { + schemaId: "cultmesh.note.v0", + recordKey: "note:odin", + payload: { + noteId: "note:odin", + body: "pay respects once", + }, + sourceRuntimeId: "cultmesh-ts-muninn", + }); + } finally { + server.close(); + } +}); + test("CultMesh TS negotiates streaming frame body transports explicitly", () => { const streams = CultMesh.createStreamCatalog(); const streamUpdates: string[] = [];