Archived
97 lines
3.1 KiB
Markdown
97 lines
3.1 KiB
Markdown
# rcon-data-bridge-client
|
|
|
|
Small, typed TypeScript/Node client for RconDataBridge's v2 RCON protocol:
|
|
batched/queued requests, typed per-op args/data, and runtime response
|
|
validation, all over a plain RCON connection (no extra port, no extra auth).
|
|
|
|
Talks raw Source RCON over TCP (`node:net`), including a workaround for
|
|
large responses arriving split across multiple RCON packets; no external
|
|
RCON library dependency.
|
|
|
|
## Usage
|
|
|
|
```ts
|
|
import { RconDataBridgeClient, RconDataBridgeError } from "rcon-data-bridge-client";
|
|
|
|
const client = await RconDataBridgeClient.connect({
|
|
host: "127.0.0.1",
|
|
port: 27015,
|
|
password: "password",
|
|
});
|
|
|
|
try
|
|
{
|
|
// Fully typed against the op's schema.
|
|
const status = await client.call("get_bridge_status");
|
|
console.log(status.uptimeSeconds);
|
|
|
|
// Batch several requests into one RCON round trip; results come back as
|
|
// a tuple, one entry per request, in order.
|
|
const [worldStats, players] = await client.batch([
|
|
{ op: "get_world_stats" },
|
|
{ op: "list_players", args: { limit: 20 } },
|
|
]);
|
|
|
|
try
|
|
{
|
|
await client.call("get_player", { id: "no-such-player" });
|
|
}
|
|
catch (err)
|
|
{
|
|
if (err instanceof RconDataBridgeError) {
|
|
console.log(err.code, err.message); // "PLAYER_NOT_FOUND", ...
|
|
}
|
|
}
|
|
}
|
|
finally { client.close(); }
|
|
```
|
|
|
|
An op this client has no built-in schema for (e.g. a server-side plugin
|
|
extension) can still be validated. Bind a Zod schema with `with()` and call
|
|
through that instead of `client` directly. `.call` on the returned object
|
|
has the same `(op, args?, options?)` shape as `client.call` itself, but it
|
|
validates against the bound schema and infers its return type from it
|
|
instead of taking a type argument:
|
|
|
|
```ts
|
|
import { z } from "zod";
|
|
|
|
const MyOpResultSchema = z.object({ status: z.string() });
|
|
|
|
const data = await client.with(MyOpResultSchema).call("some_custom_op", { targetId: "x" });
|
|
// data: { status: string }, already validated
|
|
```
|
|
|
|
Or skip validation entirely and just trust a type you supply yourself:
|
|
|
|
```ts
|
|
interface MyOpResult {
|
|
status: string;
|
|
}
|
|
|
|
const data = await client.call<MyOpResult>("some_custom_op", { targetId: "x" });
|
|
```
|
|
|
|
## Features
|
|
|
|
- **Responses are validated, not just typed.** Every op's response is
|
|
checked against a [Zod](https://zod.dev) schema, built-in for known ops,
|
|
or your own via `with()`, so a bad response throws a clear
|
|
`RconDataBridgeSchemaMismatchError` instead of handing you the wrong shape.
|
|
- **Polling speeds up while you're active, then eases off.** It polls fast
|
|
right after you submit something and slows down once things go quiet. Tune
|
|
both speeds, and the window between them, via
|
|
`RconDataBridgeClient.connect`'s options.
|
|
- **One client, one connection.** Don't overlap `call`/`batch` calls on the
|
|
same client. Await one before starting the next, or just open a second
|
|
client.
|
|
|
|
## Live-testing against a running server
|
|
|
|
```bash
|
|
RDB_HOST=127.0.0.1 RDB_PORT=27015 RDB_PASSWORD=password npm run smoke
|
|
```
|
|
|
|
`tests/smoke.mjs` is not a unit test suite. It exercises real ops over a real
|
|
RCON connection against a running server and fails loudly on any mismatch.
|