Add TypeScript TCP peer helper
This commit is contained in:
parent
49142a78e5
commit
805539384e
6 changed files with 105 additions and 17 deletions
|
|
@ -181,7 +181,7 @@ polymorphic attack surface.
|
|||
import { CultCache, defineDocumentType } from "cultcache-ts";
|
||||
import {
|
||||
CultNetDocumentRegistry,
|
||||
CultNetPeer,
|
||||
createTcpFramedCultNetPeer,
|
||||
defineCultNetDocumentBinding,
|
||||
ghostlightAgentStateGeneratedContract,
|
||||
} from "cultnet-ts";
|
||||
|
|
@ -198,8 +198,10 @@ const registry = new CultNetDocumentRegistry([
|
|||
}),
|
||||
]);
|
||||
|
||||
const peer = new CultNetPeer(stream, {
|
||||
const peer = createTcpFramedCultNetPeer(stream, {
|
||||
runtimeId: "node-worker",
|
||||
wireContract: "cultnet.schema.v0",
|
||||
transportId: "worker-tcp",
|
||||
});
|
||||
```
|
||||
|
||||
|
|
@ -231,8 +233,10 @@ maps them back to the exact C# union payload shape at the wire boundary.
|
|||
- The Ghostlight agent-state mirror is an exact copy of Ghostlight's current
|
||||
canonical schema. If Ghostlight changes the contract, this mirror must be
|
||||
updated in lockstep instead of playing coy with partial validation.
|
||||
- The direct transport is any Node `Duplex`; named-pipe/socket server wrappers
|
||||
can be added without changing the message contract.
|
||||
- The preferred direct transport path is `CultNetTransportConnection`.
|
||||
`createTcpFramedCultNetPeer(...)` wraps a Node `Duplex` in the shared
|
||||
`tcp_framed` profile before schema messages reach `CultNetPeer`. Passing a
|
||||
raw `Duplex` to `CultNetPeer` remains as compatibility for older callers.
|
||||
|
||||
## Development
|
||||
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ export {
|
|||
type GhostlightWorldState,
|
||||
} from "./contracts";
|
||||
export { encodeFrame, LengthPrefixedMessageFramer } from "./framing";
|
||||
export { CultNetPeer, type CultNetPeerEvents } from "./peer";
|
||||
export { CultNetPeer, createTcpFramedCultNetPeer, type CultNetPeerEvents, type TcpFramedCultNetPeerOptions } from "./peer";
|
||||
export {
|
||||
CultNetReconnectController,
|
||||
TcpFramedTransportConnection,
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@ 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 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 CultNetSnapshotRequestMessage, type CultNetSnapshotResponseMessage, type CultNetTransportProfile, type CultNetVerifyMessage, type CultNetWireContract } from "./contracts";
|
||||
import { encodeFrame, LengthPrefixedMessageFramer } from "./framing";
|
||||
import type { CultNetTransportConnection } from "./transport";
|
||||
import { createTcpFramedTransportProfile, TcpFramedTransportConnection, type CultNetTransportConnection, type TcpFramedTransportProfileOptions } from "./transport";
|
||||
|
||||
export interface CultNetPeerEvents {
|
||||
message: (message: CultNetMessage) => void;
|
||||
|
|
@ -18,6 +18,36 @@ export interface CultNetPeerOptions {
|
|||
wireContract: CultNetWireContract;
|
||||
}
|
||||
|
||||
export interface TcpFramedCultNetPeerOptions extends CultNetPeerOptions, TcpFramedTransportProfileOptions {
|
||||
runtimeId: string;
|
||||
}
|
||||
|
||||
export function createTcpFramedCultNetPeer(
|
||||
stream: Duplex,
|
||||
options: TcpFramedCultNetPeerOptions,
|
||||
): CultNetPeer {
|
||||
const {
|
||||
runtimeId,
|
||||
wireContract,
|
||||
transportId,
|
||||
host,
|
||||
port,
|
||||
maxPayloadBytes,
|
||||
maxFragmentBytes,
|
||||
} = options;
|
||||
const transport = new TcpFramedTransportConnection(
|
||||
stream,
|
||||
createTcpFramedTransportProfile(runtimeId, {
|
||||
transportId,
|
||||
host,
|
||||
port,
|
||||
maxPayloadBytes,
|
||||
maxFragmentBytes,
|
||||
}),
|
||||
);
|
||||
return new CultNetPeer(transport, { wireContract });
|
||||
}
|
||||
|
||||
export class CultNetPeer extends EventEmitter {
|
||||
readonly #stream?: Duplex;
|
||||
readonly #transport?: CultNetTransportConnection;
|
||||
|
|
@ -49,6 +79,10 @@ export class CultNetPeer extends EventEmitter {
|
|||
}
|
||||
}
|
||||
|
||||
get transportProfile(): CultNetTransportProfile | undefined {
|
||||
return this.#transport?.profile;
|
||||
}
|
||||
|
||||
send(message: CultNetMessage): void {
|
||||
const wireValue = encodeCultNetMessageForWire(message, this.#wireContract);
|
||||
const payload = encode(wireValue);
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import {
|
|||
cultNetBuiltinSchemaRegistry,
|
||||
computeCultNetReconnectDelayMs,
|
||||
createCultNetReconnectPolicy,
|
||||
createTcpFramedCultNetPeer,
|
||||
createTcpFramedTransportProfile,
|
||||
createRudpTransportProfile,
|
||||
defineCultNetDocumentBinding,
|
||||
|
|
@ -734,6 +735,48 @@ test("CultNet peer can speak through a transport connection", async () => {
|
|||
receiver.close();
|
||||
});
|
||||
|
||||
test("CultNet TCP helper constructs a transport-backed peer with profile metadata", async () => {
|
||||
const { a, b } = createDuplexPair();
|
||||
const sender = createTcpFramedCultNetPeer(a, {
|
||||
runtimeId: "tcp-helper-left",
|
||||
wireContract: "cultnet.schema.v0",
|
||||
transportId: "helper-left",
|
||||
host: "127.0.0.1",
|
||||
port: 17777,
|
||||
maxPayloadBytes: 64 * 1024,
|
||||
});
|
||||
const receiver = createTcpFramedCultNetPeer(b, {
|
||||
runtimeId: "tcp-helper-right",
|
||||
wireContract: "cultnet.schema.v0",
|
||||
});
|
||||
|
||||
assert.equal(sender.transportProfile?.runtimeId, "tcp-helper-left");
|
||||
assert.equal(sender.transportProfile?.transports[0]?.transportId, "helper-left");
|
||||
assert.equal(sender.transportProfile?.transports[0]?.protocol, "tcp_framed");
|
||||
assert.equal(sender.transportProfile?.transports[0]?.host, "127.0.0.1");
|
||||
assert.equal(sender.transportProfile?.transports[0]?.port, 17777);
|
||||
assert.equal(sender.transportProfile?.transports[0]?.channels[0]?.channelId, "schema");
|
||||
assert.equal(sender.transportProfile?.transports[0]?.channels[0]?.maxPayloadBytes, 64 * 1024);
|
||||
|
||||
const message = await new Promise<ReturnType<typeof parseCultNetMessage>>((resolve, reject) => {
|
||||
receiver.once("message", resolve);
|
||||
receiver.once("invalidMessage", reject);
|
||||
sender.sendHello({
|
||||
schemaVersion: "cultnet.hello.v0",
|
||||
runtimeId: "tcp-helper-sender",
|
||||
runtimeKind: "node-worker",
|
||||
});
|
||||
});
|
||||
|
||||
assert.equal(message.schemaVersion, "cultnet.hello.v0");
|
||||
if (message.schemaVersion === "cultnet.hello.v0") {
|
||||
assert.equal(message.runtimeId, "tcp-helper-sender");
|
||||
}
|
||||
|
||||
sender.close();
|
||||
receiver.close();
|
||||
});
|
||||
|
||||
test("CultNet can round-trip gamecult.networking.v0 auth messages through the explicit legacy contract", () => {
|
||||
const message: CultNetLoginMessage = {
|
||||
schemaVersion: "cultnet.login.v0",
|
||||
|
|
|
|||
Loading…
Reference in a new issue