@thoughts-on-things/beacon-protocol 0.0.0-stage → 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,115 @@
1
+ // The Atlas public directory API contract.
2
+ //
3
+ // The Atlas exposes a small, friendly read-only API so a beacon's own
4
+ // onboarding flow can render the live globe and resolve where its draft
5
+ // listing will land. These are the response shapes for those endpoints —
6
+ // the single source of truth both the Atlas (which serves them) and a beacon
7
+ // (which fetches them) type against, so the two can never silently disagree.
8
+ //
9
+ // Everything here is *public*: it is derived from already-public listing
10
+ // facts. No credential, owner id, or secret has a field to ride on.
11
+
12
+ import type { CommunityLocation, JoinPolicy, Region, ServerLocation } from "./index.js";
13
+
14
+ /** One beacon as the public globe renders it: a pin with a derived place. */
15
+ export interface PublicGlobeBeacon {
16
+ /** Public slug: the `/b/[slug]` id and the globe pin key. */
17
+ id: string;
18
+ name: string;
19
+ serverLocation: ServerLocation;
20
+ communityLocation: CommunityLocation;
21
+ /** Short human label derived from tags / modpack. */
22
+ vibe: string;
23
+ /** Whether joining is invite-gated or open to any verified Atlas player. */
24
+ joinPolicy?: JoinPolicy;
25
+ /** Server-reported Minecraft version, when the latest heartbeat provided it. */
26
+ mcVersion?: string;
27
+ /** Public Minecraft connection address, when the operator published one. */
28
+ mcAddress?: string;
29
+ /** Players connected right now. */
30
+ online: number;
31
+ /** Player slot capacity. */
32
+ cap: number;
33
+ /** Whether the server itself is reachable: a beacon with zero players
34
+ * online may still be `isOnline: true`. Optional for forward-compat with
35
+ * older Atlas deployments that didn't carry it. */
36
+ isOnline?: boolean;
37
+ /** Map placement, derived deterministically from the slug + community location. */
38
+ lat: number;
39
+ lon: number;
40
+ /** Stable accent colour, keyed off the slug. */
41
+ color: string;
42
+ /** The beacon's public web-app origin (`https://play.example`), when the
43
+ * operator set one. The desktop launcher uses it to resolve the beacon's
44
+ * active play target (`GET <publicUrl>/api/play`) and to geo-match an
45
+ * installed server back to its pin. Absent when the operator left it blank. */
46
+ publicUrl?: string;
47
+ /** The `code` of the {@link PublicAtlasNode} that last ingested this
48
+ * beacon's heartbeat. Drives the transmission arc on selection.
49
+ * Optional for forward-compat with older Atlas deployments and beacons
50
+ * whose last heartbeat predates the introduction of node tracking. */
51
+ lastIngestNode?: string;
52
+ }
53
+
54
+ /**
55
+ * One Atlas node — a running directory process at a real location.
56
+ *
57
+ * Identity is derived from the location, so the `code` is the lower-cased
58
+ * country code plus a slug of the locality (`us-chicago`, `de-frankfurt`).
59
+ * Multiple machines in the same locality collapse to one node by design.
60
+ */
61
+ export interface PublicAtlasNode {
62
+ /** Derived from location; never operator-typed. */
63
+ code: string;
64
+ /** Coarse social region for filtering (mirrors `Region` from the manifest). */
65
+ region: Region;
66
+ /** ISO 3166-1 α-2. */
67
+ countryCode: string;
68
+ /** ISO 3166-2 subdivision (optional). */
69
+ subdivisionCode?: string;
70
+ /** City / metro, the human-readable label. */
71
+ locality: string;
72
+ /** Approximate geographic point — the satellite's orbital anchor. */
73
+ lat: number;
74
+ lon: number;
75
+ }
76
+
77
+ /** Directory-wide counters for the globe HUD. */
78
+ export interface PublicDirectoryStats {
79
+ beacons: number;
80
+ online: number;
81
+ regions: number;
82
+ versions: number;
83
+ }
84
+
85
+ /** `GET /api/directory`: every publicly listed beacon, plus roll-up counters
86
+ * and the set of currently-live Atlas nodes. `nodes` is optional for
87
+ * forward-compat with older Atlas deployments. */
88
+ export interface DirectoryResponse {
89
+ beacons: PublicGlobeBeacon[];
90
+ stats: PublicDirectoryStats;
91
+ nodes?: PublicAtlasNode[];
92
+ }
93
+
94
+ /**
95
+ * `GET /api/directory/[beaconId]`: one listing resolved by its opaque
96
+ * `beaconId`. Operator-typed identity (name, locations) is stored on the
97
+ * Atlas listing row, so every field is present from the moment the
98
+ * operator claims the listing — no heartbeat required.
99
+ */
100
+ export interface BeaconLookupResponse {
101
+ /** The opaque `beaconId` that was looked up. */
102
+ id: string;
103
+ /** Canonical public slug minted by the Atlas. */
104
+ slug: string;
105
+ name: string;
106
+ /** Listing lifecycle status, e.g. `pending_verification` / `active`. */
107
+ status: string;
108
+ serverLocation: ServerLocation;
109
+ communityLocation: CommunityLocation;
110
+ /** Published globe placement, derived from the community location. */
111
+ lat: number;
112
+ lon: number;
113
+ /** Absolute URL of the public profile page on the Atlas. */
114
+ profileUrl: string;
115
+ }
@@ -0,0 +1,169 @@
1
+ // Conformance fixtures: canonical heartbeats shared by both sides of the
2
+ // wire. The beacon-side publisher's projector test asserts it *produces* one
3
+ // of these; the Atlas-side ingest test asserts it *accepts* and stores one of
4
+ // these. Because both tests pin the same object, the publisher and the ingest
5
+ // can never silently drift apart on the wire shape.
6
+ //
7
+ // `validHeartbeats` are well-formed and must round-trip through
8
+ // `parseHeartbeat`. `invalidBodies` are deliberately malformed and must be
9
+ // rejected: each names the rule it violates.
10
+ //
11
+ // Fixtures are deep-frozen. A consumer that needs to mutate one (e.g. to
12
+ // build a near-miss variant) must `cloneFixture` it first.
13
+
14
+ import {
15
+ buildHeartbeat,
16
+ type BeaconManifest,
17
+ type BeaconProfile,
18
+ type BeaconStatusReport,
19
+ type Heartbeat,
20
+ } from "./index.js";
21
+
22
+ const BEACON_ID = "atlas_3kf9Qz1aBcDeF2gh";
23
+
24
+ // ── Building blocks ────────────────────────────────────────────────────────
25
+
26
+ /** The leanest valid manifest: every required field, no optionals. */
27
+ const minimalManifest: BeaconManifest = {
28
+ mcVersion: "1.21.4",
29
+ loader: "vanilla",
30
+ mods: [],
31
+ };
32
+
33
+ /** A manifest exercising every optional field. */
34
+ const fullManifest: BeaconManifest = {
35
+ mcVersion: "1.21.4",
36
+ loader: "paper",
37
+ mods: ["Create", "Carpet", "FastAsyncWorldEdit"],
38
+ renderDistance: 12,
39
+ worldAgeDays: 420,
40
+ worldSizeMib: 8602,
41
+ vantage: { protocolVersion: 1, mode: "live" },
42
+ };
43
+
44
+ /** The leanest valid status. */
45
+ const minimalStatus: BeaconStatusReport = {
46
+ online: true,
47
+ playersOnline: 3,
48
+ playersMax: 20,
49
+ sampledAt: "2026-05-17T12:00:00.000Z",
50
+ };
51
+
52
+ /** A status exercising every optional perf field. */
53
+ const fullStatus: BeaconStatusReport = {
54
+ online: true,
55
+ playersOnline: 14,
56
+ playersMax: 24,
57
+ tps: 19.8,
58
+ uptimeSeconds: 86_400,
59
+ uptimePct: 99.7,
60
+ playersPeak7d: [9, 12, 18, 14, 22, 16, 14],
61
+ sampledAt: "2026-05-17T12:00:00.000Z",
62
+ };
63
+
64
+ /** A profile exercising both opt-in fields. */
65
+ const fullProfile: BeaconProfile = {
66
+ roster: [
67
+ { name: "mira", role: "host", online: true },
68
+ { name: "kal", role: "admin", online: true },
69
+ { name: "juniper", role: "builder", online: false },
70
+ ],
71
+ activity: [
72
+ {
73
+ kind: "advancement",
74
+ actor: "kal",
75
+ detail: "earned The End",
76
+ at: "2026-05-17T11:00:00.000Z",
77
+ },
78
+ {
79
+ kind: "join",
80
+ actor: "mira",
81
+ detail: "joined the game",
82
+ at: "2026-05-17T11:42:00.000Z",
83
+ },
84
+ ],
85
+ };
86
+
87
+ // ── Valid fixtures ─────────────────────────────────────────────────────────
88
+
89
+ /** Required fields only, no `profile`: the floor of a valid heartbeat. */
90
+ export const minimalHeartbeat: Heartbeat = deepFreeze(
91
+ buildHeartbeat(BEACON_ID, minimalManifest, minimalStatus),
92
+ );
93
+
94
+ /** Every field populated, including the opt-in roster + activity. */
95
+ export const fullHeartbeat: Heartbeat = deepFreeze(
96
+ buildHeartbeat(BEACON_ID, fullManifest, fullStatus, fullProfile),
97
+ );
98
+
99
+ /**
100
+ * A rich manifest + status but no `profile` block: a beacon that lists in the
101
+ * directory without opting into the consent-gated player data.
102
+ */
103
+ export const profileLessHeartbeat: Heartbeat = deepFreeze(
104
+ buildHeartbeat(BEACON_ID, fullManifest, fullStatus),
105
+ );
106
+
107
+ /** Every valid fixture, keyed by name. */
108
+ export const validHeartbeats = {
109
+ minimal: minimalHeartbeat,
110
+ full: fullHeartbeat,
111
+ profileLess: profileLessHeartbeat,
112
+ } as const;
113
+
114
+ // ── Invalid fixtures ───────────────────────────────────────────────────────
115
+
116
+ /**
117
+ * Deliberately malformed bodies, each violating exactly one rule. Both sides
118
+ * must reject every one. Typed `unknown`: they are not valid `Heartbeat`s.
119
+ */
120
+ export const invalidBodies: Record<string, unknown> = {
121
+ // `sampledAt` must be a strict ISO 8601 datetime.
122
+ badDatetime: withStatus({ sampledAt: "last tuesday" }),
123
+ // `loader` must be a known enum member.
124
+ unknownLoader: withManifest({ loader: "bukkit" }),
125
+ // `beaconId` must be opaque and url-safe.
126
+ unsafeBeaconId: { ...cloneFixture(minimalHeartbeat), beaconId: "../../etc" },
127
+ // The whole `manifest` block is required.
128
+ missingManifest: {
129
+ schemaVersion: 1,
130
+ beaconId: BEACON_ID,
131
+ status: cloneFixture(minimalStatus),
132
+ },
133
+ // `mcVersion` is required on the manifest.
134
+ missingMcVersion: withManifest({ mcVersion: undefined }),
135
+ };
136
+
137
+ // ── Helpers ────────────────────────────────────────────────────────────────
138
+
139
+ /** A mutable deep copy of a (possibly frozen) fixture. */
140
+ export function cloneFixture<T>(value: T): T {
141
+ return structuredClone(value);
142
+ }
143
+
144
+ /** A minimal heartbeat with `manifest` fields overridden: for near-misses. */
145
+ function withManifest(patch: Record<string, unknown>): unknown {
146
+ const body = cloneFixture(minimalHeartbeat) as Record<string, unknown>;
147
+ const next: Record<string, unknown> = { ...(body.manifest as object) };
148
+ for (const [k, v] of Object.entries(patch)) {
149
+ if (v === undefined) delete next[k];
150
+ else next[k] = v;
151
+ }
152
+ body.manifest = next;
153
+ return body;
154
+ }
155
+
156
+ /** A minimal heartbeat with `status` fields overridden: for near-misses. */
157
+ function withStatus(patch: Record<string, unknown>): unknown {
158
+ const body = cloneFixture(minimalHeartbeat) as Record<string, unknown>;
159
+ body.status = { ...(body.status as object), ...patch };
160
+ return body;
161
+ }
162
+
163
+ function deepFreeze<T>(value: T): T {
164
+ if (value && typeof value === "object") {
165
+ for (const v of Object.values(value)) deepFreeze(v);
166
+ Object.freeze(value);
167
+ }
168
+ return value;
169
+ }