diff --git a/packages/cultmesh-ts/src/index.ts b/packages/cultmesh-ts/src/index.ts index 48b2f742..b6147ce2 100644 --- a/packages/cultmesh-ts/src/index.ts +++ b/packages/cultmesh-ts/src/index.ts @@ -4108,6 +4108,11 @@ export interface CultMeshRudpDocumentPut { schemaId: string; recordKey: string; storedAt: string; + /** + * The document, read through the definition `documents` holds for `schemaId`. A schema the registry has no definition + * for is not decoded at all: the payload arrives untouched as the opaque `DocumentPayload`, for a handler that holds + * the definition itself to read with `decodeDocumentPayload`. + */ payload: unknown; sourceRuntimeId: string | null; sourceAgentId: string | null; @@ -6417,14 +6422,11 @@ function normalizeRudpDocumentPut( throw new Error(`Unsupported raw payload encoding ${document.payloadEncoding}.`); } const definition = documents.getBySchemaId(document.schemaId)?.definition; - if (!definition) { - throw new Error("Raw document put names a schema with no registered definition."); - } return { schemaId: document.schemaId, recordKey: document.recordKey, storedAt: document.storedAt ?? new Date().toISOString(), - payload: decodeDocumentPayload(definition, document.payload), + payload: definition ? decodeDocumentPayload(definition, document.payload) : document.payload, sourceRuntimeId: document.sourceRuntimeId ?? null, sourceAgentId: document.sourceAgentId ?? null, sourceRole: document.sourceRole ?? null, diff --git a/packages/cultmesh-ts/test/codec-documents.test.ts b/packages/cultmesh-ts/test/codec-documents.test.ts index 57be7706..3a2fd90c 100644 --- a/packages/cultmesh-ts/test/codec-documents.test.ts +++ b/packages/cultmesh-ts/test/codec-documents.test.ts @@ -4,7 +4,14 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import test from "node:test"; import { z } from "zod"; -import { codecs, defineDocumentType } from "@gamecult/cultcache-ts"; +import { + codecs, + decodeDocumentPayload, + defineDocumentType, + documentPayloadBytes, + encodeDocumentPayload, + type DocumentPayload, +} from "@gamecult/cultcache-ts"; import { CultNetDocumentRegistry, defineCultNetDocumentBinding, @@ -137,23 +144,24 @@ test("the mesh syncs a codec document from a peer snapshot and returns a codec r } }); -test("a RUDP put naming a schema the server has no definition for is refused before the handler, and the refusal does not echo the schema id", async () => { +test("a RUDP put naming a schema the server has no definition for reaches the handler as the untouched opaque payload", async () => { const connectionId = 0x10203070; - const canary = "cultmesh.canary_schema_8c1f.v0"; const stray = defineDocumentType({ - type: "cultmesh.canary_schema", - schemaId: canary, + type: "cultmesh.stray_note", + schemaId: "cultmesh.stray_note.v0", schema: z.object({ noteId: z.string() }), name: "noteId", + codec: codecs.slots<{ noteId: string }>({ 0: ["noteId", codecs.string] }, "Tests.StrayNote"), }); + const strayBinding = defineCultNetDocumentBinding({ definition: stray }); const known = defineCultNetDocumentBinding({ definition: plainNote }); const errors: Error[] = []; - const puts: unknown[] = []; + const puts: Array<{ schemaId: string; payload: unknown }> = []; const server = CultMesh.createRudpDocumentServer("cultmesh-ts-unregistered-server", connectionId, { documents: new CultNetDocumentRegistry([known]), bindHost: "127.0.0.1", bindPort: 0, resendDelayMs: 25, resendPollMs: 5, onError: (error) => errors.push(error), - onDocumentPutRaw: async (document) => { puts.push(document.payload); }, + onDocumentPutRaw: async (document) => { puts.push({ schemaId: document.schemaId, payload: document.payload }); }, }); let peer: CultNetPeer | undefined; try { @@ -161,14 +169,17 @@ test("a RUDP put naming a schema the server has no definition for is refused bef peer = await CultMesh.createRudpPeer("cultmesh-ts-unregistered-client", connectionId, `rudp://127.0.0.1:${server.bind.port}`, { resendDelayMs: 25, resendPollMs: 5, connectTimeoutMs: 1_000, }); - peer.send(new CultNetDocumentRegistry([defineCultNetDocumentBinding({ definition: stray })]) - .createRawDocumentPutMessage(defineCultNetDocumentBinding({ definition: stray }), "unregistered-1", "k", { noteId: "k" })); + peer.send(new CultNetDocumentRegistry([strayBinding]).createRawDocumentPutMessage(strayBinding, "unregistered-1", "k", { noteId: "k" })); const startedAt = Date.now(); - while (errors.length === 0 && Date.now() - startedAt < 2_000) await new Promise((resolve) => setTimeout(resolve, 5)); - assert.equal(errors.length, 1); - assert.match(errors[0]!.message, /no registered definition/); - assert.ok(!errors[0]!.message.includes(canary), "the refusal names no schema id"); - assert.deepEqual(puts, [], "the handler never saw the bytes"); + while (puts.length === 0 && Date.now() - startedAt < 2_000) await new Promise((resolve) => setTimeout(resolve, 5)); + assert.deepEqual(errors, []); + assert.equal(puts.length, 1); + // Not read as plain msgpack: the bytes are the codec's slot record, handed over opaque, and the definition that reads + // them is the handler's to hold. + const payload = puts[0]!.payload as DocumentPayload; + assert.equal(Buffer.from(documentPayloadBytes(payload)).toString("hex"), "91a16b", "a one-slot codec record, not a map"); + assert.deepEqual(Buffer.from(documentPayloadBytes(payload)), Buffer.from(documentPayloadBytes(encodeDocumentPayload(stray, { noteId: "k" })))); + assert.deepEqual(decodeDocumentPayload(stray, payload), { noteId: "k" }); } finally { peer?.close(); server.close(); diff --git a/packages/cultmesh-ts/test/cultmesh.test.ts b/packages/cultmesh-ts/test/cultmesh.test.ts index 6c972731..52e6760f 100644 --- a/packages/cultmesh-ts/test/cultmesh.test.ts +++ b/packages/cultmesh-ts/test/cultmesh.test.ts @@ -6,7 +6,7 @@ import test from "node:test"; import assert from "node:assert/strict"; import { decodeWireValue, encodeWireValue } from "cultnet-ts/wire"; import { z } from "zod"; -import { defineDocumentType, documentPayloadBytes, documentPayloadFromBytes } from "@gamecult/cultcache-ts"; +import { defineDocumentType, documentPayloadBytes, documentPayloadFromBytes, type DocumentPayload } from "@gamecult/cultcache-ts"; import { CultNetDocumentRegistry, CultNetPeer, @@ -2781,7 +2781,8 @@ test("CultMesh TS RUDP document server accepts raw document puts from multiple p maxFragmentBytes: 1024, maxPendingReliablePackets: 16, onDocumentPutRaw: (document) => { - received.push(`${document.sourceRuntimeId}:${document.recordKey}:${document.payload}`); + // The registry holds no definition, so the payload arrives untouched and is read here by name. + received.push(`${document.sourceRuntimeId}:${document.recordKey}:${decodeWireValue(documentPayloadBytes(document.payload as DocumentPayload))}`); }, }, ); @@ -2870,7 +2871,7 @@ test("CultMesh TS publishes one registered document to a RUDP catalog", async () received = { schemaId: document.schemaId, recordKey: document.recordKey, - payload: document.payload, + payload: decodeWireValue(documentPayloadBytes(document.payload as DocumentPayload)), sourceRuntimeId: document.sourceRuntimeId, }; },