Publish browser subscription wire contracts

This commit is contained in:
Meta Vens 2026-08-17 23:26:51 +02:00
commit 3fe2e9be1b
14 changed files with 462 additions and 2 deletions

View file

@ -0,0 +1,25 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.database-change-raw.schema.json",
"title": "CultNet Raw Database Change Message",
"type": "object",
"required": ["schemaVersion", "messageId", "subscriptionId", "changeKind"],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.database_change_raw.v0" },
"messageId": { "type": "string", "minLength": 1 },
"subscriptionId": { "type": "string", "minLength": 1 },
"changeKind": { "enum": ["added", "updated", "removed"] },
"document": { "$ref": "./cultnet.raw-document-record.schema.json" },
"recordKey": { "type": "string", "minLength": 1 },
"schemaId": { "type": "string", "minLength": 1 }
},
"allOf": [
{
"if": { "properties": { "changeKind": { "const": "removed" } } },
"then": { "required": ["recordKey", "schemaId"] },
"else": { "required": ["document"] }
}
]
}

View file

@ -0,0 +1,20 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.database-subscribe.schema.json",
"title": "CultNet Database Subscribe Message",
"type": "object",
"required": ["schemaVersion", "messageId", "subscriptionId", "includeSnapshot"],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.database_subscribe.v0" },
"messageId": { "type": "string", "minLength": 1 },
"subscriptionId": { "type": "string", "minLength": 1 },
"schemaIds": { "type": "array", "items": { "type": "string", "minLength": 1 } },
"recordKeys": { "type": "array", "items": { "type": "string", "minLength": 1 } },
"includeSnapshot": { "type": "boolean" },
"consumerRuntimeId": { "type": "string", "minLength": 1 },
"bodyIds": { "type": "array", "items": { "type": "string", "minLength": 1 } },
"supportedBodyTransports": { "type": "array", "items": { "type": "string", "minLength": 1 } }
}
}

View file

@ -0,0 +1,14 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/GameCult/cultnet-ts/contracts/cultnet.database-unsubscribe.schema.json",
"title": "CultNet Database Unsubscribe Message",
"type": "object",
"required": ["schemaVersion", "messageId", "subscriptionId"],
"additionalProperties": false,
"properties": {
"schemaVersion": { "const": "cultnet.database_unsubscribe.v0" },
"messageId": { "type": "string", "minLength": 1 },
"subscriptionId": { "type": "string", "minLength": 1 }
}
}

View file

@ -18,6 +18,9 @@ import transportProfileSchema from "../contracts/cultnet.transport-profile.schem
import snapshotRequestSchema from "../contracts/cultnet.snapshot-request.schema.json";
import snapshotResponseSchema from "../contracts/cultnet.snapshot-response.schema.json";
import snapshotResponseRawSchema from "../contracts/cultnet.snapshot-response-raw.schema.json";
import databaseSubscribeSchema from "../contracts/cultnet.database-subscribe.schema.json";
import databaseUnsubscribeSchema from "../contracts/cultnet.database-unsubscribe.schema.json";
import databaseChangeRawSchema from "../contracts/cultnet.database-change-raw.schema.json";
import schemaDescriptorSchema from "../contracts/cultnet.schema-descriptor.schema.json";
import schemaCatalogRequestSchema from "../contracts/cultnet.schema-catalog-request.schema.json";
import schemaCatalogResponseSchema from "../contracts/cultnet.schema-catalog-response.schema.json";
@ -44,6 +47,9 @@ export type CultNetSchemaVersion =
| "cultnet.snapshot_request.v0"
| "cultnet.snapshot_response.v0"
| "cultnet.snapshot_response_raw.v0"
| "cultnet.database_subscribe.v0"
| "cultnet.database_unsubscribe.v0"
| "cultnet.database_change_raw.v0"
| "cultnet.schema_catalog_request.v0"
| "cultnet.schema_catalog_response.v0"
| "cultnet.shard_catalog_request.v0"
@ -217,6 +223,34 @@ export interface CultNetSnapshotResponseRawMessage {
documents: CultNetRawDocumentRecord[];
}
export interface CultNetDatabaseSubscribeMessage {
schemaVersion: "cultnet.database_subscribe.v0";
messageId: string;
subscriptionId: string;
schemaIds?: string[];
recordKeys?: string[];
includeSnapshot: boolean;
consumerRuntimeId?: string;
bodyIds?: string[];
supportedBodyTransports?: string[];
}
export interface CultNetDatabaseUnsubscribeMessage {
schemaVersion: "cultnet.database_unsubscribe.v0";
messageId: string;
subscriptionId: string;
}
export interface CultNetDatabaseChangeRawMessage {
schemaVersion: "cultnet.database_change_raw.v0";
messageId: string;
subscriptionId: string;
changeKind: "added" | "updated" | "removed";
document?: CultNetRawDocumentRecord;
recordKey?: string;
schemaId?: string;
}
export interface CultNetSchemaDescriptor {
schemaId: string;
kind: CultNetSchemaKind;
@ -535,6 +569,9 @@ export type CultNetMessage =
| CultNetSnapshotRequestMessage
| CultNetSnapshotResponseMessage
| CultNetSnapshotResponseRawMessage
| CultNetDatabaseSubscribeMessage
| CultNetDatabaseUnsubscribeMessage
| CultNetDatabaseChangeRawMessage
| CultNetSchemaCatalogRequestMessage
| CultNetSchemaCatalogResponseMessage
| CultNetShardCatalogRequestMessage
@ -562,6 +599,9 @@ const CULTNET_MESSAGE_SCHEMAS = [
snapshotRequestSchema,
snapshotResponseSchema,
snapshotResponseRawSchema,
databaseSubscribeSchema,
databaseUnsubscribeSchema,
databaseChangeRawSchema,
schemaCatalogRequestSchema,
schemaCatalogResponseSchema,
shardCatalogRequestSchema,
@ -620,6 +660,11 @@ export function parseCultNetMessage(input: unknown, wireContract: CultNetWireCon
return input as CultNetSnapshotResponseRawMessage;
}
if (schemaVersion === "cultnet.database_change_raw.v0") {
validateDatabaseChangeRawMessage(input);
return input as CultNetDatabaseChangeRawMessage;
}
if (!validator(input)) {
const keys = Object.keys(input as Record<string, unknown>).join(",");
throw new Error(`${renderValidationErrors(schemaVersion, validator)}; keys=${keys}`);
@ -951,9 +996,39 @@ function validateSnapshotResponseRawMessage(
});
}
function validateDatabaseChangeRawMessage(
input: unknown,
): asserts input is CultNetDatabaseChangeRawMessage {
const candidate = validateBinaryMessageRoot(input, "cultnet.database_change_raw.v0");
requireNonEmptyString(candidate.subscriptionId, "/subscriptionId");
if (candidate.changeKind !== "added" &&
candidate.changeKind !== "updated" &&
candidate.changeKind !== "removed") {
throw new Error(
"Validation failed for cultnet.database_change_raw.v0: /changeKind: must be added, updated, or removed",
);
}
if (candidate.changeKind === "removed") {
requireNonEmptyString(candidate.recordKey, "/recordKey");
requireNonEmptyString(candidate.schemaId, "/schemaId");
return;
}
if (!("document" in candidate)) {
throw new Error(
"Validation failed for cultnet.database_change_raw.v0: /document: is required for added or updated changes",
);
}
validateRawDocumentRecord(candidate.document, "/document");
}
function validateBinaryMessageRoot(
input: unknown,
schemaVersion: "cultnet.document_put_raw.v0" | "cultnet.snapshot_response_raw.v0",
schemaVersion:
| "cultnet.document_put_raw.v0"
| "cultnet.snapshot_response_raw.v0"
| "cultnet.database_change_raw.v0",
): Record<string, unknown> {
if (!input || typeof input !== "object") {
throw new Error(`Validation failed for ${schemaVersion}: /: must be an object`);
@ -1043,6 +1118,15 @@ function normalizeCultNetOptionalNulls(
acceptLegacyPropertyName(candidate, "ShardEpoch", "shardEpoch");
stripNullProperties(candidate, ["schemaIds", "recordKeys", "shardId", "shardEpoch"]);
return;
case "cultnet.database_subscribe.v0":
stripNullProperties(candidate, [
"schemaIds",
"recordKeys",
"consumerRuntimeId",
"bodyIds",
"supportedBodyTransports",
]);
return;
case "cultnet.schema_catalog_request.v0":
stripNullProperties(candidate, ["schemaIds", "kinds"]);
return;
@ -1092,6 +1176,9 @@ export const cultNetSchemas = {
snapshotRequestSchema,
snapshotResponseSchema,
snapshotResponseRawSchema,
databaseSubscribeSchema,
databaseUnsubscribeSchema,
databaseChangeRawSchema,
schemaDescriptorSchema,
schemaCatalogRequestSchema,
schemaCatalogResponseSchema,

View file

@ -8,6 +8,9 @@ export {
type CultNetDocumentPutMessage,
type CultNetDocumentPutRawMessage,
type CultNetDocumentRecord,
type CultNetDatabaseSubscribeMessage,
type CultNetDatabaseUnsubscribeMessage,
type CultNetDatabaseChangeRawMessage,
type CultNetRawDocumentRecord,
type CultNetRawPayloadEncoding,
type CultNetErrorMessage,

View file

@ -1261,6 +1261,64 @@ test("operation envelopes preserve typed service routing and payload correlation
assert.equal(response.messageId, request.messageId);
});
test("database subscription contracts match the C# live document lane", () => {
const subscribe = parseCultNetMessage({
schemaVersion: "cultnet.database_subscribe.v0",
messageId: "subscribe-counter",
subscriptionId: "counter:browser-1",
schemaIds: ["sample.counter_state.v1"],
recordKeys: ["counter:main"],
includeSnapshot: true,
consumerRuntimeId: "browser-1",
});
assert.equal(subscribe.schemaVersion, "cultnet.database_subscribe.v0");
assert.equal(subscribe.subscriptionId, "counter:browser-1");
const added = parseCultNetMessage({
schemaVersion: "cultnet.database_change_raw.v0",
messageId: "change-counter-1",
subscriptionId: "counter:browser-1",
changeKind: "added",
document: {
schemaId: "sample.counter_state.v1",
recordKey: "counter:main",
storedAt: "2026-08-17T00:00:00Z",
payloadEncoding: "messagepack",
payload: new Uint8Array([0x81, 0xa5, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x01]),
},
});
assert.equal(added.schemaVersion, "cultnet.database_change_raw.v0");
assert.equal(added.document?.recordKey, "counter:main");
const removed = parseCultNetMessage({
schemaVersion: "cultnet.database_change_raw.v0",
messageId: "change-counter-2",
subscriptionId: "counter:browser-1",
changeKind: "removed",
recordKey: "counter:main",
schemaId: "sample.counter_state.v1",
});
assert.equal(removed.schemaVersion, "cultnet.database_change_raw.v0");
assert.equal(removed.recordKey, "counter:main");
const unsubscribe = parseCultNetMessage({
schemaVersion: "cultnet.database_unsubscribe.v0",
messageId: "unsubscribe-counter",
subscriptionId: "counter:browser-1",
});
assert.equal(unsubscribe.schemaVersion, "cultnet.database_unsubscribe.v0");
assert.throws(
() => parseCultNetMessage({
schemaVersion: "cultnet.database_change_raw.v0",
messageId: "invalid-change",
subscriptionId: "counter:browser-1",
changeKind: "updated",
}),
/document: is required/,
);
});
test("RUDP operation service correlates typed payload envelopes", async () => {
const server = await startCultNetOperationServer({
runtimeId: "sai-sidecar",