Add TypeScript CultMesh RUDP peer helpers
This commit is contained in:
parent
53f469cec2
commit
cc0d52bfc5
4 changed files with 218 additions and 6 deletions
|
|
@ -340,7 +340,10 @@ Current progress:
|
|||
leaving packet/session semantics in `cultnet-ts`. TypeScript now also mirrors
|
||||
Kotlin's authorized-peer lookup and RUDP client helper, so peer cards remain
|
||||
contact hints until the authority lease catalog authorizes the requested
|
||||
Verse role.
|
||||
Verse role. The facade can now also create a connected `CultNetPeer` from a
|
||||
direct RUDP endpoint, peer card, or authorized peer lookup, so schema and shard
|
||||
catalog request helpers ride the RUDP transport without callers hand-wrapping
|
||||
the socket transport.
|
||||
- TypeScript now also has a `CultNetSchemaCatalog` for remote schema
|
||||
descriptors plus `CultNetPeer` request/fetch/sync helpers for
|
||||
`cultnet.schema_catalog_request.v0` / `cultnet.schema_catalog_response.v0`.
|
||||
|
|
|
|||
|
|
@ -107,7 +107,7 @@ leases.upsert({
|
|||
expiresAt: new Date(Date.now() + 60_000),
|
||||
});
|
||||
|
||||
const client = await CultMesh.createRudpClientForAuthorizedPeer(
|
||||
const client = await CultMesh.createRudpPeerForAuthorizedPeer(
|
||||
"ts-client",
|
||||
0x1020_3040,
|
||||
peers,
|
||||
|
|
@ -115,12 +115,15 @@ const client = await CultMesh.createRudpClientForAuthorizedPeer(
|
|||
"local",
|
||||
"schema",
|
||||
);
|
||||
client.connect(new TextEncoder().encode("join"));
|
||||
const schemas = CultMesh.createSchemaCatalog();
|
||||
await client.syncSchemaCatalog(schemas, { kinds: ["document_payload"] });
|
||||
```
|
||||
|
||||
`CultMesh.createRudpClientForPeer(...)` remains available for already trusted
|
||||
call sites. Discovery-first paths should prefer
|
||||
`createRudpClientForAuthorizedPeer(...)`, which composes
|
||||
`CultMesh.createRudpPeerForPeer(...)` remains available for already trusted
|
||||
call sites, and `CultMesh.createRudpClientForPeer(...)` still returns the lower
|
||||
level transport when a caller really needs to own handshaking or raw frames.
|
||||
Discovery-first schema/catalog paths should prefer
|
||||
`createRudpPeerForAuthorizedPeer(...)`, which composes
|
||||
`CultMeshPeerCatalog.firstAuthorized(...)` with the authority lease catalog
|
||||
before using the peer-card endpoint as a dial target.
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ import {
|
|||
} from "cultcache-ts";
|
||||
import {
|
||||
CultNetDocumentRegistry,
|
||||
CultNetPeer,
|
||||
CultNetRudpSocketTransportConnection,
|
||||
CultNetSchemaCatalog,
|
||||
CultNetShardCatalog,
|
||||
|
|
@ -17,6 +18,7 @@ import {
|
|||
type CultNetDocumentBinding,
|
||||
type CultNetReconnectPolicy,
|
||||
type CultNetSchemaCatalogOptions,
|
||||
type CultNetWireContract,
|
||||
} from "cultnet-ts";
|
||||
|
||||
export interface CultMeshNodeOptions {
|
||||
|
|
@ -133,6 +135,18 @@ export interface CultMeshAuthorizedRudpSocketOptions
|
|||
at?: Date;
|
||||
}
|
||||
|
||||
export interface CultMeshRudpPeerOptions extends CultMeshRudpSocketOptions {
|
||||
connectPayload?: Uint8Array;
|
||||
connectTimeoutMs?: number;
|
||||
wireContract?: CultNetWireContract;
|
||||
}
|
||||
|
||||
export interface CultMeshAuthorizedRudpPeerOptions
|
||||
extends CultMeshRudpPeerOptions {
|
||||
shardId?: string;
|
||||
at?: Date;
|
||||
}
|
||||
|
||||
export class CultMeshPeerCatalog {
|
||||
readonly #peers = new Map<string, CultMeshPeerCard>();
|
||||
readonly #subscribers = new Set<(peer: CultMeshPeerCard) => void>();
|
||||
|
|
@ -657,6 +671,70 @@ export class CultMesh {
|
|||
}
|
||||
return CultMesh.createRudpClientForPeer(runtimeId, connectionId, peer, options);
|
||||
}
|
||||
|
||||
public static async createRudpPeer(
|
||||
runtimeId: string,
|
||||
connectionId: number,
|
||||
endpoint: string | CultMeshRudpEndpoint,
|
||||
options: CultMeshRudpPeerOptions = {},
|
||||
): Promise<CultNetPeer> {
|
||||
const client = await CultMesh.createRudpClient(
|
||||
runtimeId,
|
||||
connectionId,
|
||||
endpoint,
|
||||
options,
|
||||
);
|
||||
client.connect(
|
||||
options.connectPayload === undefined
|
||||
? undefined
|
||||
: Uint8Array.from(options.connectPayload),
|
||||
);
|
||||
await waitForRudpConnected(
|
||||
client,
|
||||
options.connectTimeoutMs ?? 1_000,
|
||||
`RUDP peer ${runtimeId}`,
|
||||
);
|
||||
return new CultNetPeer(client, {
|
||||
wireContract: options.wireContract ?? "cultnet.schema.v0",
|
||||
});
|
||||
}
|
||||
|
||||
public static async createRudpPeerForPeer(
|
||||
runtimeId: string,
|
||||
connectionId: number,
|
||||
peer: CultMeshPeerCard,
|
||||
options: CultMeshRudpPeerOptions = {},
|
||||
): Promise<CultNetPeer> {
|
||||
const endpoint = peer.endpoints.find((value) =>
|
||||
value.toLowerCase().startsWith("rudp://"),
|
||||
);
|
||||
if (!endpoint) {
|
||||
throw new Error(`Peer ${peer.peerId} does not advertise a RUDP endpoint.`);
|
||||
}
|
||||
return CultMesh.createRudpPeer(runtimeId, connectionId, endpoint, options);
|
||||
}
|
||||
|
||||
public static async createRudpPeerForAuthorizedPeer(
|
||||
runtimeId: string,
|
||||
connectionId: number,
|
||||
peers: CultMeshPeerCatalog,
|
||||
leases: CultMeshAuthorityLeaseCatalog,
|
||||
verseId: string,
|
||||
role: string,
|
||||
options: CultMeshAuthorizedRudpPeerOptions = {},
|
||||
): Promise<CultNetPeer> {
|
||||
const peer = peers.firstAuthorized(
|
||||
verseId,
|
||||
role,
|
||||
leases,
|
||||
options.shardId,
|
||||
options.at,
|
||||
);
|
||||
if (!peer) {
|
||||
throw new Error(`No authorized RUDP peer for role ${role} in Verse ${verseId}.`);
|
||||
}
|
||||
return CultMesh.createRudpPeerForPeer(runtimeId, connectionId, peer, options);
|
||||
}
|
||||
}
|
||||
|
||||
function requireNonEmpty(value: string, name: string): void {
|
||||
|
|
@ -679,6 +757,55 @@ async function bindRudpSocket(options: CultMeshRudpSocketOptions): Promise<Socke
|
|||
return socket;
|
||||
}
|
||||
|
||||
async function waitForRudpConnected(
|
||||
transport: CultNetRudpSocketTransportConnection,
|
||||
timeoutMs: number,
|
||||
description: string,
|
||||
): Promise<void> {
|
||||
if (transport.connected) {
|
||||
return;
|
||||
}
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
let settled = false;
|
||||
const cleanup = (): void => {
|
||||
clearInterval(poll);
|
||||
clearTimeout(timer);
|
||||
transport.off("error", onError);
|
||||
transport.off("close", onClose);
|
||||
};
|
||||
const finish = (error?: Error): void => {
|
||||
if (settled) {
|
||||
return;
|
||||
}
|
||||
settled = true;
|
||||
cleanup();
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
};
|
||||
const check = (): void => {
|
||||
if (transport.connected) {
|
||||
finish();
|
||||
}
|
||||
};
|
||||
const onError = (error: Error): void => finish(error);
|
||||
const onClose = (): void =>
|
||||
finish(new Error(`${description} closed before handshake completed.`));
|
||||
const poll = setInterval(check, 5);
|
||||
const timer = setTimeout(
|
||||
() => finish(new Error(`Timed out waiting for ${description} handshake.`)),
|
||||
timeoutMs,
|
||||
);
|
||||
poll.unref?.();
|
||||
timer.unref?.();
|
||||
transport.once("error", onError);
|
||||
transport.once("close", onClose);
|
||||
check();
|
||||
});
|
||||
}
|
||||
|
||||
function copyBudgetFor(
|
||||
transport: CultMeshStreamBodyTransport,
|
||||
): CultMeshStreamNegotiation["copyBudget"] {
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import test from "node:test";
|
|||
import assert from "node:assert/strict";
|
||||
import { z } from "zod";
|
||||
import { defineDocumentType } from "cultcache-ts";
|
||||
import { CultNetPeer, cultNetBuiltinSchemaRegistry } from "cultnet-ts";
|
||||
import { CultMesh } from "../src/index";
|
||||
|
||||
const noteDocument = defineDocumentType({
|
||||
|
|
@ -303,6 +304,84 @@ test("CultMesh TS branded facade creates RUDP clients from peer endpoints", asyn
|
|||
}
|
||||
});
|
||||
|
||||
test("CultMesh TS creates connected RUDP schema peers for catalog requests", async () => {
|
||||
const connectionId = 0x10203045;
|
||||
const serverTransport = await CultMesh.createRudpServer(
|
||||
"cultmesh-ts-rudp-schema-server",
|
||||
connectionId,
|
||||
{
|
||||
resendDelayMs: 25,
|
||||
resendPollMs: 5,
|
||||
maxFragmentBytes: 1024,
|
||||
maxPendingReliablePackets: 16,
|
||||
},
|
||||
);
|
||||
const serverPort = serverTransport.profile.transports[0]?.port;
|
||||
assert.equal(typeof serverPort, "number");
|
||||
const serverPeer = new CultNetPeer(serverTransport, {
|
||||
wireContract: "cultnet.schema.v0",
|
||||
});
|
||||
serverPeer.on("message", (message) => {
|
||||
if (message.schemaVersion === "cultnet.schema_catalog_request.v0") {
|
||||
serverPeer.sendSchemaCatalogResponse(
|
||||
cultNetBuiltinSchemaRegistry.createCatalogResponse(message),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
const peer = {
|
||||
peerId: "cultmesh-ts-rudp-schema-server",
|
||||
verseId: "local",
|
||||
endpoints: [`rudp://127.0.0.1:${serverPort}`],
|
||||
roles: ["schema"],
|
||||
authorityLeaseId: "lease:cultmesh-ts-rudp-schema-server",
|
||||
};
|
||||
const peers = CultMesh.createPeerCatalog();
|
||||
const leases = CultMesh.createAuthorityLeaseCatalog();
|
||||
peers.upsert(peer);
|
||||
leases.upsert({
|
||||
leaseId: "lease:cultmesh-ts-rudp-schema-server",
|
||||
verseId: "local",
|
||||
peerId: "cultmesh-ts-rudp-schema-server",
|
||||
roles: ["schema"],
|
||||
validFrom: new Date(Date.now() - 1000),
|
||||
expiresAt: new Date(Date.now() + 1000),
|
||||
});
|
||||
|
||||
let clientPeer: CultNetPeer | undefined;
|
||||
try {
|
||||
clientPeer = await CultMesh.createRudpPeerForAuthorizedPeer(
|
||||
"cultmesh-ts-rudp-schema-client",
|
||||
connectionId,
|
||||
peers,
|
||||
leases,
|
||||
"local",
|
||||
"schema",
|
||||
{
|
||||
resendDelayMs: 25,
|
||||
resendPollMs: 5,
|
||||
maxFragmentBytes: 1024,
|
||||
maxPendingReliablePackets: 16,
|
||||
connectTimeoutMs: 1_000,
|
||||
},
|
||||
);
|
||||
const synced = CultMesh.createSchemaCatalog();
|
||||
const applied = await clientPeer.syncSchemaCatalog(synced, {
|
||||
messageId: "cultmesh-ts-rudp-schema-catalog",
|
||||
kinds: ["document_payload"],
|
||||
includeSchemaJson: true,
|
||||
timeoutMs: 1_000,
|
||||
});
|
||||
|
||||
assert.ok(applied.some((descriptor) => descriptor.documentType === "ghostlight.agent-state"));
|
||||
assert.equal(clientPeer.transportProfile?.transports[0]?.protocol, "rudp");
|
||||
assert.equal(serverPeer.transportProfile?.transports[0]?.protocol, "rudp");
|
||||
} finally {
|
||||
clientPeer?.close();
|
||||
serverPeer.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("CultMesh TS negotiates streaming frame body transports explicitly", () => {
|
||||
const streams = CultMesh.createStreamCatalog();
|
||||
const streamUpdates: string[] = [];
|
||||
|
|
|
|||
Loading…
Reference in a new issue