Add TypeScript shard catalog sync helpers
This commit is contained in:
parent
30934fc776
commit
8bc505f0c1
10 changed files with 407 additions and 5 deletions
|
|
@ -307,6 +307,11 @@ Current progress:
|
|||
`cultnet.schema_catalog_request.v0` / `cultnet.schema_catalog_response.v0`.
|
||||
`CultNetSchemaRegistry` remains the local definition owner; the catalog owns
|
||||
imported descriptors so callers do not treat raw response objects as state.
|
||||
- TypeScript now has the matching `CultNetShardCatalog` and peer
|
||||
request/fetch/sync helpers for
|
||||
`cultnet.shard_catalog_request.v0` / `cultnet.shard_catalog_response.v0`.
|
||||
Shard descriptors can be filtered by schema id and record key without
|
||||
requiring callers to inspect raw response maps.
|
||||
- Kotlin now mirrors the CultMesh stream catalog shape for Android-adjacent
|
||||
media, sensor, tensor, and byte streams: stream declaration, clock metadata,
|
||||
preferred body transports, consumer negotiation, copy-budget classification,
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ CultMesh.
|
|||
| --- | --- | --- | --- |
|
||||
| C# | Reference implementation and production game/server substrate | CultCache typed documents and SoA, CultNet schema-v0, LiteNetLib production path with adapter profile, shared transport profiles, shared RUDP socket transport, RUDP reconnect policy/controller and reconnect-loop helper, LiteNetLib client reconnect-controller adoption, CultMesh node/session/catalog/stream/simulation surfaces | No need to abandon LiteNetLib before the CultNet adapter fully owns production service adoption |
|
||||
| Python | Package runtime, local daemon endpoint, raw interop peer, typed state participant | CultCache/CultNet/CultMesh package surface, schema catalogs, shard catalogs/logs, local TCP CultMesh server, session/prediction/simulation fact helpers, authority leases, streams, RUDP codec/session/socket transport, RUDP reconnect policy/controller and reconnect-loop helper, schema-v0 MessagePack over RUDP evidence, benchmark evidence against C# cache paths | Universal C# throughput, full C# daemon/server stack, and RUDP as the only public service pipe |
|
||||
| TypeScript | Node/browser-adjacent projection and interop runtime | CultCache parity tests, CultNet schema-v0 peer, schema catalog request/sync helpers, transport profiles, TCP-framed transport port and peer helper, shared RUDP socket transport, RUDP reconnect policy/controller, RUDP reconnect-loop helper, local CultMesh node/catalog/authority/stream ergonomics | Full C# game session/server stack |
|
||||
| TypeScript | Node/browser-adjacent projection and interop runtime | CultCache parity tests, CultNet schema-v0 peer, schema/shard catalog request/sync helpers, transport profiles, TCP-framed transport port and peer helper, shared RUDP socket transport, RUDP reconnect policy/controller, RUDP reconnect-loop helper, local CultMesh node/catalog/authority/stream ergonomics | Full C# game session/server stack |
|
||||
| Rust | Low-level cache/transport/runtime substrate | CultCache/CultNet interop lane, transport profiles, TCP-framed port, shared RUDP codec/session/socket transport, RUDP reconnect policy/controller and reconnect-loop helper, schema-v0 MessagePack over RUDP evidence, small CultMesh facade for peer cards, authority leases, RUDP endpoint parsing, and peer-card RUDP client construction | Full CultMesh game-session/server facade, stream catalog, or production game-session server |
|
||||
| Kotlin | JVM/Android client substrate | Typed MessagePack cache, schema/shard catalogs with transport fetch/sync helpers, authority leases, stream catalog, branded `CultMesh` facade, WebSocket transport profile/connection helper, RUDP client/server sugar, RUDP reconnect policy/controller and reconnect-loop helper, schema-v0 MessagePack over RUDP evidence | C# SoA store, full C# server daemon, or Unity hot-loop memory layout |
|
||||
|
||||
|
|
|
|||
|
|
@ -183,6 +183,12 @@ responses. `CultNetPeer.requestSchemaCatalog(...)`,
|
|||
message-id-matched request/response cycle so callers do not need to hand-roll
|
||||
listener cleanup around every discovery exchange.
|
||||
|
||||
Shard discovery follows the same split: `CultNetShardCatalog` owns imported
|
||||
`cultnet.shard_catalog_response.v0` descriptors, filters by schema id and
|
||||
record key, and can answer `cultnet.shard_catalog_request.v0`. `CultNetPeer`
|
||||
also exposes `requestShardCatalog(...)`, `fetchShardDescriptors(...)`, and
|
||||
`syncShardCatalog(...)` for the same message-id-matched exchange.
|
||||
|
||||
## Quick Example
|
||||
|
||||
```ts
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "cultnet.shard_catalog_request.v0",
|
||||
"type": "object",
|
||||
"required": ["schemaVersion", "messageId"],
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"schemaVersion": { "const": "cultnet.shard_catalog_request.v0" },
|
||||
"messageId": { "type": "string", "minLength": 1 },
|
||||
"schemaIds": {
|
||||
"type": "array",
|
||||
"items": { "type": "string", "minLength": 1 }
|
||||
},
|
||||
"recordKeys": {
|
||||
"type": "array",
|
||||
"items": { "type": "string", "minLength": 1 }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
{
|
||||
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
||||
"$id": "cultnet.shard_catalog_response.v0",
|
||||
"type": "object",
|
||||
"required": ["schemaVersion", "messageId", "shards"],
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"schemaVersion": { "const": "cultnet.shard_catalog_response.v0" },
|
||||
"messageId": { "type": "string", "minLength": 1 },
|
||||
"shards": {
|
||||
"type": "array",
|
||||
"items": { "$ref": "#/$defs/shardDescriptor" }
|
||||
}
|
||||
},
|
||||
"$defs": {
|
||||
"shardDescriptor": {
|
||||
"type": "object",
|
||||
"required": ["shardId", "ownerRuntimeId", "epoch"],
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"shardId": { "type": "string", "minLength": 1 },
|
||||
"ownerRuntimeId": { "type": "string", "minLength": 1 },
|
||||
"epoch": { "type": "integer", "minimum": 0 },
|
||||
"isPrimary": { "type": "boolean" },
|
||||
"schemaIds": {
|
||||
"type": "array",
|
||||
"items": { "type": "string", "minLength": 1 }
|
||||
},
|
||||
"keyPrefix": { "type": "string", "minLength": 1 },
|
||||
"primaryEndpoints": {
|
||||
"type": "array",
|
||||
"items": { "type": "string", "minLength": 1 }
|
||||
},
|
||||
"replicaEndpoints": {
|
||||
"type": "array",
|
||||
"items": { "type": "string", "minLength": 1 }
|
||||
},
|
||||
"readReplicaEndpoints": {
|
||||
"type": "array",
|
||||
"items": { "type": "string", "minLength": 1 }
|
||||
},
|
||||
"region": { "type": "string", "minLength": 1 },
|
||||
"authorityLeaseId": { "type": "string", "minLength": 1 }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -21,6 +21,8 @@ import snapshotResponseRawSchema from "../contracts/cultnet.snapshot-response-ra
|
|||
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";
|
||||
import shardCatalogRequestSchema from "../contracts/cultnet.shard-catalog-request.schema.json";
|
||||
import shardCatalogResponseSchema from "../contracts/cultnet.shard-catalog-response.schema.json";
|
||||
import ghostlightAgentStateSchema from "../contracts/ghostlight.agent-state.schema.json";
|
||||
|
||||
export type CultNetWireContract = "cultnet.schema.v0" | "gamecult.networking.v0";
|
||||
|
|
@ -41,7 +43,9 @@ export type CultNetSchemaVersion =
|
|||
| "cultnet.snapshot_response.v0"
|
||||
| "cultnet.snapshot_response_raw.v0"
|
||||
| "cultnet.schema_catalog_request.v0"
|
||||
| "cultnet.schema_catalog_response.v0";
|
||||
| "cultnet.schema_catalog_response.v0"
|
||||
| "cultnet.shard_catalog_request.v0"
|
||||
| "cultnet.shard_catalog_response.v0";
|
||||
|
||||
export type CultNetSchemaKind = "wire_message" | "document_payload" | "shared_contract";
|
||||
export type CultNetRawPayloadEncoding = "messagepack";
|
||||
|
|
@ -234,6 +238,33 @@ export interface CultNetSchemaCatalogResponseMessage {
|
|||
schemas: CultNetSchemaDescriptor[];
|
||||
}
|
||||
|
||||
export interface CultNetShardDescriptor {
|
||||
shardId: string;
|
||||
ownerRuntimeId: string;
|
||||
epoch: number;
|
||||
isPrimary?: boolean;
|
||||
schemaIds?: string[];
|
||||
keyPrefix?: string;
|
||||
primaryEndpoints?: string[];
|
||||
replicaEndpoints?: string[];
|
||||
readReplicaEndpoints?: string[];
|
||||
region?: string;
|
||||
authorityLeaseId?: string;
|
||||
}
|
||||
|
||||
export interface CultNetShardCatalogRequestMessage {
|
||||
schemaVersion: "cultnet.shard_catalog_request.v0";
|
||||
messageId: string;
|
||||
schemaIds?: string[];
|
||||
recordKeys?: string[];
|
||||
}
|
||||
|
||||
export interface CultNetShardCatalogResponseMessage {
|
||||
schemaVersion: "cultnet.shard_catalog_response.v0";
|
||||
messageId: string;
|
||||
shards: CultNetShardDescriptor[];
|
||||
}
|
||||
|
||||
export interface GhostlightPressure {
|
||||
pressure_id: string;
|
||||
label: string;
|
||||
|
|
@ -476,7 +507,9 @@ export type CultNetMessage =
|
|||
| CultNetSnapshotResponseMessage
|
||||
| CultNetSnapshotResponseRawMessage
|
||||
| CultNetSchemaCatalogRequestMessage
|
||||
| CultNetSchemaCatalogResponseMessage;
|
||||
| CultNetSchemaCatalogResponseMessage
|
||||
| CultNetShardCatalogRequestMessage
|
||||
| CultNetShardCatalogResponseMessage;
|
||||
|
||||
const ajv = new Ajv2020({
|
||||
allErrors: true,
|
||||
|
|
@ -500,6 +533,8 @@ const CULTNET_MESSAGE_SCHEMAS = [
|
|||
snapshotResponseRawSchema,
|
||||
schemaCatalogRequestSchema,
|
||||
schemaCatalogResponseSchema,
|
||||
shardCatalogRequestSchema,
|
||||
shardCatalogResponseSchema,
|
||||
] as const;
|
||||
|
||||
const cultNetValidators = new Map<CultNetSchemaVersion, ValidateFunction>();
|
||||
|
|
@ -978,6 +1013,9 @@ function normalizeCultNetOptionalNulls(
|
|||
case "cultnet.schema_catalog_request.v0":
|
||||
stripNullProperties(candidate, ["schemaIds", "kinds"]);
|
||||
return;
|
||||
case "cultnet.shard_catalog_request.v0":
|
||||
stripNullProperties(candidate, ["schemaIds", "recordKeys"]);
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
|
@ -1024,5 +1062,7 @@ export const cultNetSchemas = {
|
|||
schemaDescriptorSchema,
|
||||
schemaCatalogRequestSchema,
|
||||
schemaCatalogResponseSchema,
|
||||
shardCatalogRequestSchema,
|
||||
shardCatalogResponseSchema,
|
||||
ghostlightAgentStateSchema,
|
||||
} as const;
|
||||
|
|
|
|||
|
|
@ -23,6 +23,9 @@ export {
|
|||
type CultNetSchemaCatalogResponseMessage,
|
||||
type CultNetSchemaDescriptor,
|
||||
type CultNetSchemaKind,
|
||||
type CultNetShardCatalogRequestMessage,
|
||||
type CultNetShardCatalogResponseMessage,
|
||||
type CultNetShardDescriptor,
|
||||
type CultNetSnapshotRequestMessage,
|
||||
type CultNetSnapshotResponseMessage,
|
||||
type CultNetSnapshotResponseRawMessage,
|
||||
|
|
@ -55,7 +58,7 @@ export {
|
|||
type GhostlightWorldState,
|
||||
} from "./contracts";
|
||||
export { encodeFrame, LengthPrefixedMessageFramer } from "./framing";
|
||||
export { CultNetPeer, createTcpFramedCultNetPeer, type CultNetPeerEvents, type CultNetPeerSchemaCatalogRequestOptions, type TcpFramedCultNetPeerOptions } from "./peer";
|
||||
export { CultNetPeer, createTcpFramedCultNetPeer, type CultNetPeerEvents, type CultNetPeerSchemaCatalogRequestOptions, type CultNetPeerShardCatalogRequestOptions, type TcpFramedCultNetPeerOptions } from "./peer";
|
||||
export {
|
||||
CultNetReconnectController,
|
||||
TcpFramedTransportConnection,
|
||||
|
|
@ -105,6 +108,11 @@ export {
|
|||
type CultNetSchemaCatalogOptions,
|
||||
type CultNetSchemaRegistration,
|
||||
} from "./schema-discovery";
|
||||
export {
|
||||
CultNetShardCatalog,
|
||||
shardServes,
|
||||
type CultNetShardCatalogOptions,
|
||||
} from "./shard-catalog";
|
||||
export {
|
||||
CultNetClientSecurityOptions,
|
||||
CultNetSecret,
|
||||
|
|
|
|||
|
|
@ -3,9 +3,10 @@ import type { Duplex } from "node:stream";
|
|||
|
||||
import { decode, encode } from "@msgpack/msgpack";
|
||||
|
||||
import { encodeCultNetMessageForWire, parseCultNetMessage, type CultNetDocumentDeleteMessage, type CultNetDocumentPutMessage, type CultNetErrorMessage, type CultNetHelloMessage, type CultNetLoginMessage, type CultNetLoginSuccessMessage, type CultNetMessage, type CultNetRegisterMessage, type CultNetSampleChangeNameMessage, type CultNetSampleChatMessage, type CultNetSchemaCatalogRequestMessage, type CultNetSchemaCatalogResponseMessage, type CultNetSnapshotRequestMessage, type CultNetSnapshotResponseMessage, type CultNetTransportProfile, type CultNetVerifyMessage, type CultNetWireContract } from "./contracts";
|
||||
import { encodeCultNetMessageForWire, parseCultNetMessage, type CultNetDocumentDeleteMessage, type CultNetDocumentPutMessage, type CultNetErrorMessage, type CultNetHelloMessage, type CultNetLoginMessage, type CultNetLoginSuccessMessage, type CultNetMessage, type CultNetRegisterMessage, type CultNetSampleChangeNameMessage, type CultNetSampleChatMessage, type CultNetSchemaCatalogRequestMessage, type CultNetSchemaCatalogResponseMessage, type CultNetShardCatalogRequestMessage, type CultNetShardCatalogResponseMessage, type CultNetSnapshotRequestMessage, type CultNetSnapshotResponseMessage, type CultNetTransportProfile, type CultNetVerifyMessage, type CultNetWireContract } from "./contracts";
|
||||
import { encodeFrame, LengthPrefixedMessageFramer } from "./framing";
|
||||
import { CultNetSchemaCatalog, type CultNetSchemaCatalogOptions } from "./schema-discovery";
|
||||
import { CultNetShardCatalog, type CultNetShardCatalogOptions } from "./shard-catalog";
|
||||
import { createTcpFramedTransportProfile, TcpFramedTransportConnection, type CultNetTransportConnection, type TcpFramedTransportProfileOptions } from "./transport";
|
||||
|
||||
export interface CultNetPeerEvents {
|
||||
|
|
@ -28,6 +29,11 @@ export interface CultNetPeerSchemaCatalogRequestOptions extends CultNetSchemaCat
|
|||
timeoutMs?: number;
|
||||
}
|
||||
|
||||
export interface CultNetPeerShardCatalogRequestOptions extends CultNetShardCatalogOptions {
|
||||
messageId?: string;
|
||||
timeoutMs?: number;
|
||||
}
|
||||
|
||||
export function createTcpFramedCultNetPeer(
|
||||
stream: Duplex,
|
||||
options: TcpFramedCultNetPeerOptions,
|
||||
|
|
@ -156,6 +162,14 @@ export class CultNetPeer extends EventEmitter {
|
|||
this.send(message);
|
||||
}
|
||||
|
||||
sendShardCatalogRequest(message: CultNetShardCatalogRequestMessage): void {
|
||||
this.send(message);
|
||||
}
|
||||
|
||||
sendShardCatalogResponse(message: CultNetShardCatalogResponseMessage): void {
|
||||
this.send(message);
|
||||
}
|
||||
|
||||
requestSchemaCatalog(
|
||||
options: CultNetPeerSchemaCatalogRequestOptions = {},
|
||||
): Promise<CultNetSchemaCatalogResponseMessage> {
|
||||
|
|
@ -216,6 +230,65 @@ export class CultNetPeer extends EventEmitter {
|
|||
return catalog.applyResponse(await this.requestSchemaCatalog(options));
|
||||
}
|
||||
|
||||
requestShardCatalog(
|
||||
options: CultNetPeerShardCatalogRequestOptions = {},
|
||||
): Promise<CultNetShardCatalogResponseMessage> {
|
||||
const messageId = options.messageId ?? createMessageId("cultnet-ts-shard-catalog");
|
||||
const timeoutMs = options.timeoutMs ?? 4_000;
|
||||
|
||||
return new Promise<CultNetShardCatalogResponseMessage>((resolve, reject) => {
|
||||
const cleanup = (): void => {
|
||||
clearTimeout(timer);
|
||||
this.off("message", onMessage);
|
||||
this.off("invalidMessage", onInvalidMessage);
|
||||
this.off("error", onError);
|
||||
this.off("close", onClose);
|
||||
};
|
||||
const rejectWith = (error: Error): void => {
|
||||
cleanup();
|
||||
reject(error);
|
||||
};
|
||||
const onMessage = (message: CultNetMessage): void => {
|
||||
if (message.schemaVersion !== "cultnet.shard_catalog_response.v0" || message.messageId !== messageId) {
|
||||
return;
|
||||
}
|
||||
cleanup();
|
||||
resolve(message);
|
||||
};
|
||||
const onInvalidMessage = (error: Error): void => rejectWith(error);
|
||||
const onError = (error: Error): void => rejectWith(error);
|
||||
const onClose = (): void => rejectWith(new Error("CultNet peer closed before shard catalog response."));
|
||||
const timer = setTimeout(
|
||||
() => rejectWith(new Error(`Timed out waiting for CultNet shard catalog response ${messageId}.`)),
|
||||
timeoutMs,
|
||||
);
|
||||
|
||||
this.on("message", onMessage);
|
||||
this.on("invalidMessage", onInvalidMessage);
|
||||
this.on("error", onError);
|
||||
this.on("close", onClose);
|
||||
this.sendShardCatalogRequest({
|
||||
schemaVersion: "cultnet.shard_catalog_request.v0",
|
||||
messageId,
|
||||
schemaIds: options.schemaIds !== undefined ? [...options.schemaIds] : undefined,
|
||||
recordKeys: options.recordKeys !== undefined ? [...options.recordKeys] : undefined,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async fetchShardDescriptors(
|
||||
options: CultNetPeerShardCatalogRequestOptions = {},
|
||||
) {
|
||||
return new CultNetShardCatalog().applyResponse(await this.requestShardCatalog(options));
|
||||
}
|
||||
|
||||
async syncShardCatalog(
|
||||
catalog: CultNetShardCatalog,
|
||||
options: CultNetPeerShardCatalogRequestOptions = {},
|
||||
) {
|
||||
return catalog.applyResponse(await this.requestShardCatalog(options));
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.#transport?.close();
|
||||
this.#stream?.end();
|
||||
|
|
|
|||
112
packages/cultnet-ts/src/shard-catalog.ts
Normal file
112
packages/cultnet-ts/src/shard-catalog.ts
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
import type {
|
||||
CultNetShardCatalogRequestMessage,
|
||||
CultNetShardCatalogResponseMessage,
|
||||
CultNetShardDescriptor,
|
||||
} from "./contracts";
|
||||
|
||||
export interface CultNetShardCatalogOptions {
|
||||
schemaIds?: readonly string[];
|
||||
recordKeys?: readonly string[];
|
||||
}
|
||||
|
||||
export class CultNetShardCatalog {
|
||||
readonly #shards = new Map<string, CultNetShardDescriptor>();
|
||||
readonly #subscribers = new Set<(descriptor: CultNetShardDescriptor) => void>();
|
||||
|
||||
get shards(): CultNetShardDescriptor[] {
|
||||
return Array.from(this.#shards.values())
|
||||
.sort((left, right) => left.shardId.localeCompare(right.shardId))
|
||||
.map(cloneShardDescriptor);
|
||||
}
|
||||
|
||||
watch(callback: (descriptor: CultNetShardDescriptor) => void): () => void {
|
||||
this.#subscribers.add(callback);
|
||||
return () => {
|
||||
this.#subscribers.delete(callback);
|
||||
};
|
||||
}
|
||||
|
||||
upsert(descriptor: CultNetShardDescriptor): CultNetShardDescriptor {
|
||||
if (!descriptor.shardId) {
|
||||
throw new Error("CultNet shard descriptor requires shardId.");
|
||||
}
|
||||
if (!descriptor.ownerRuntimeId) {
|
||||
throw new Error("CultNet shard descriptor requires ownerRuntimeId.");
|
||||
}
|
||||
|
||||
const stored = cloneShardDescriptor(descriptor);
|
||||
this.#shards.set(stored.shardId, stored);
|
||||
for (const subscriber of Array.from(this.#subscribers)) {
|
||||
subscriber(cloneShardDescriptor(stored));
|
||||
}
|
||||
return cloneShardDescriptor(stored);
|
||||
}
|
||||
|
||||
get(shardId: string): CultNetShardDescriptor | undefined {
|
||||
return this.#shards.has(shardId) ? cloneShardDescriptor(this.#shards.get(shardId)!) : undefined;
|
||||
}
|
||||
|
||||
list(options: CultNetShardCatalogOptions = {}): CultNetShardDescriptor[] {
|
||||
const requestedSchemaIds = options.schemaIds ? new Set(options.schemaIds) : null;
|
||||
const requestedRecordKeys = options.recordKeys ?? [];
|
||||
|
||||
return this.shards.filter((shard) => {
|
||||
if (requestedSchemaIds && !shard.schemaIds?.some((schemaId) => requestedSchemaIds.has(schemaId))) {
|
||||
return false;
|
||||
}
|
||||
if (requestedRecordKeys.length > 0 && !requestedRecordKeys.some((recordKey) => shardServes(shard, { recordKey }))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
createCatalogResponse(
|
||||
request: CultNetShardCatalogRequestMessage,
|
||||
): CultNetShardCatalogResponseMessage {
|
||||
return {
|
||||
schemaVersion: "cultnet.shard_catalog_response.v0",
|
||||
messageId: request.messageId,
|
||||
shards: this.list({
|
||||
schemaIds: request.schemaIds,
|
||||
recordKeys: request.recordKeys,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
applyResponse(response: CultNetShardCatalogResponseMessage): CultNetShardDescriptor[] {
|
||||
if (response.schemaVersion !== "cultnet.shard_catalog_response.v0") {
|
||||
throw new Error(`Expected cultnet.shard_catalog_response.v0, received ${response.schemaVersion}.`);
|
||||
}
|
||||
return response.shards.map((descriptor) => this.upsert(descriptor));
|
||||
}
|
||||
}
|
||||
|
||||
export function shardServes(
|
||||
shard: CultNetShardDescriptor,
|
||||
options: { schemaId?: string; recordKey?: string } = {},
|
||||
): boolean {
|
||||
if (options.schemaId !== undefined && shard.schemaIds !== undefined && shard.schemaIds.length > 0 && !shard.schemaIds.includes(options.schemaId)) {
|
||||
return false;
|
||||
}
|
||||
if (options.recordKey !== undefined && shard.keyPrefix !== undefined && !options.recordKey.startsWith(shard.keyPrefix)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function cloneShardDescriptor(descriptor: CultNetShardDescriptor): CultNetShardDescriptor {
|
||||
return {
|
||||
shardId: descriptor.shardId,
|
||||
ownerRuntimeId: descriptor.ownerRuntimeId,
|
||||
epoch: descriptor.epoch,
|
||||
...(descriptor.isPrimary !== undefined ? { isPrimary: descriptor.isPrimary } : {}),
|
||||
...(descriptor.schemaIds !== undefined ? { schemaIds: [...descriptor.schemaIds] } : {}),
|
||||
...(descriptor.keyPrefix !== undefined ? { keyPrefix: descriptor.keyPrefix } : {}),
|
||||
...(descriptor.primaryEndpoints !== undefined ? { primaryEndpoints: [...descriptor.primaryEndpoints] } : {}),
|
||||
...(descriptor.replicaEndpoints !== undefined ? { replicaEndpoints: [...descriptor.replicaEndpoints] } : {}),
|
||||
...(descriptor.readReplicaEndpoints !== undefined ? { readReplicaEndpoints: [...descriptor.readReplicaEndpoints] } : {}),
|
||||
...(descriptor.region !== undefined ? { region: descriptor.region } : {}),
|
||||
...(descriptor.authorityLeaseId !== undefined ? { authorityLeaseId: descriptor.authorityLeaseId } : {}),
|
||||
};
|
||||
}
|
||||
|
|
@ -25,6 +25,7 @@ import {
|
|||
CultNetSchemaRegistry,
|
||||
CultNetSecret,
|
||||
CultNetServerSecurityOptions,
|
||||
CultNetShardCatalog,
|
||||
TcpFramedTransportConnection,
|
||||
CultNetReconnectController,
|
||||
cultNetSchemas,
|
||||
|
|
@ -42,6 +43,7 @@ import {
|
|||
parseCultNetMessage,
|
||||
validateGhostlightAgentStateGenerated,
|
||||
validateGhostlightAgentState,
|
||||
shardServes,
|
||||
type CultNetLoginMessage,
|
||||
type GhostlightAgentStateShape,
|
||||
type GhostlightAgentStateDocument,
|
||||
|
|
@ -899,6 +901,96 @@ test("CultNet peer can request and sync schema catalogs by message id", async ()
|
|||
responder.close();
|
||||
});
|
||||
|
||||
test("CultNet shard catalog filters descriptors and answers catalog requests", () => {
|
||||
const catalog = new CultNetShardCatalog();
|
||||
let watchedShardId: string | undefined;
|
||||
const unsubscribe = catalog.watch((descriptor) => {
|
||||
watchedShardId = descriptor.shardId;
|
||||
});
|
||||
|
||||
catalog.upsert({
|
||||
shardId: "notes-a",
|
||||
ownerRuntimeId: "ts-owner",
|
||||
epoch: 7,
|
||||
isPrimary: true,
|
||||
schemaIds: ["note.v1"],
|
||||
keyPrefix: "note:",
|
||||
primaryEndpoints: ["rudp://127.0.0.1:4100"],
|
||||
authorityLeaseId: "lease-a",
|
||||
});
|
||||
catalog.upsert({
|
||||
shardId: "profiles-a",
|
||||
ownerRuntimeId: "ts-owner",
|
||||
epoch: 3,
|
||||
schemaIds: ["profile.v1"],
|
||||
keyPrefix: "profile:",
|
||||
});
|
||||
|
||||
assert.equal(watchedShardId, "profiles-a");
|
||||
assert.equal(shardServes(catalog.get("notes-a")!, { schemaId: "note.v1", recordKey: "note:1" }), true);
|
||||
assert.equal(shardServes(catalog.get("notes-a")!, { schemaId: "profile.v1", recordKey: "note:1" }), false);
|
||||
assert.deepEqual(catalog.list({ schemaIds: ["note.v1"], recordKeys: ["note:1"] }).map((shard) => shard.shardId), ["notes-a"]);
|
||||
|
||||
const response = catalog.createCatalogResponse({
|
||||
schemaVersion: "cultnet.shard_catalog_request.v0",
|
||||
messageId: "shards",
|
||||
schemaIds: ["note.v1"],
|
||||
recordKeys: ["note:2"],
|
||||
});
|
||||
assert.equal(response.schemaVersion, "cultnet.shard_catalog_response.v0");
|
||||
assert.equal(response.shards[0]?.shardId, "notes-a");
|
||||
|
||||
const parsed = parseCultNetMessage(response);
|
||||
assert.equal(parsed.schemaVersion, "cultnet.shard_catalog_response.v0");
|
||||
if (parsed.schemaVersion === "cultnet.shard_catalog_response.v0") {
|
||||
assert.equal(parsed.shards[0]?.ownerRuntimeId, "ts-owner");
|
||||
}
|
||||
|
||||
unsubscribe();
|
||||
});
|
||||
|
||||
test("CultNet peer can request and sync shard catalogs by message id", async () => {
|
||||
const { a, b } = createDuplexPair();
|
||||
const requester = createTcpFramedCultNetPeer(a, {
|
||||
runtimeId: "shard-requester",
|
||||
wireContract: "cultnet.schema.v0",
|
||||
});
|
||||
const responder = createTcpFramedCultNetPeer(b, {
|
||||
runtimeId: "shard-responder",
|
||||
wireContract: "cultnet.schema.v0",
|
||||
});
|
||||
const remote = new CultNetShardCatalog();
|
||||
remote.upsert({
|
||||
shardId: "notes-peer",
|
||||
ownerRuntimeId: "shard-responder",
|
||||
epoch: 1,
|
||||
isPrimary: true,
|
||||
schemaIds: ["note.v1"],
|
||||
keyPrefix: "note:",
|
||||
primaryEndpoints: ["rudp://127.0.0.1:4200"],
|
||||
});
|
||||
|
||||
responder.on("message", (message) => {
|
||||
if (message.schemaVersion === "cultnet.shard_catalog_request.v0") {
|
||||
responder.sendShardCatalogResponse(remote.createCatalogResponse(message));
|
||||
}
|
||||
});
|
||||
|
||||
const synced = new CultNetShardCatalog();
|
||||
const applied = await requester.syncShardCatalog(synced, {
|
||||
messageId: "peer-shards",
|
||||
schemaIds: ["note.v1"],
|
||||
recordKeys: ["note:local"],
|
||||
timeoutMs: 1_000,
|
||||
});
|
||||
|
||||
assert.equal(applied[0]?.shardId, "notes-peer");
|
||||
assert.equal(synced.get("notes-peer")?.primaryEndpoints?.[0], "rudp://127.0.0.1:4200");
|
||||
|
||||
requester.close();
|
||||
responder.close();
|
||||
});
|
||||
|
||||
test("CultNet schema discovery can round-trip over the legacy wire contract when schemas are requested inline", () => {
|
||||
const registry = new CultNetSchemaRegistry([
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue