commit
Add Hub RPC entrypoint for the system site
The system site calls broadcast() to fan fronting and mental-state events out over /v2/ws, and exportLegacy() once to copy the stored plural data. Co-Authored-By: Sandrone <[email protected]>
diff --git a/src/index.ts b/src/index.ts
index 190f9e1..2dfafbb 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -53,10 +53,32 @@ import { DOCS_HTML } from "./docs";
import { ABUSE_HTML, ABUSE_CONTACT, securityTxt } from "./abuse";
import { LANDING_HTML, TERMS_HTML, PRIVACY_HTML, NOT_FOUND_HTML } from "./pages";
import { OPENAPI_JSON } from "./openapi";
-import { SystemState } from "./system/do";
+import { SystemState, BROADCAST_PATH, EXPORT_PATH } from "./system/do";
+import { WorkerEntrypoint } from "cloudflare:workers";
export { GatewayManager, SystemState };
+/**
+ * RPC entrypoint for the system site (doughmination.co.uk), bound there as
+ * HUB with entrypoint "Hub". Service-binding only: it has no public route.
+ */
+export class Hub extends WorkerEntrypoint<Env> {
+ /** Fan a { type, data } event out to every /v2/ws client. */
+ async broadcast(data: unknown): Promise<void> {
+ await systemStub(this.env).fetch(`https://do${BROADCAST_PATH}`, {
+ method: "POST",
+ body: JSON.stringify(data),
+ });
+ }
+
+ /** The old SystemState store, every key, for the site's one-time import. */
+ async exportLegacy(): Promise<Record<string, unknown>> {
+ const res = await systemStub(this.env).fetch(`https://do${EXPORT_PATH}`);
+ if (!res.ok) throw new Error(`Legacy export failed: ${res.status}`);
+ return (await res.json()) as Record<string, unknown>;
+ }
+}
+
const CORS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, OPTIONS",
diff --git a/src/system/do.ts b/src/system/do.ts
index 2fdc1ae..1078611 100644
--- a/src/system/do.ts
+++ b/src/system/do.ts
@@ -53,6 +53,9 @@ class DoStore implements Store {
const WS_PATH = "/v2/ws";
/** Internal DO-to-DO path GatewayManager relays presence updates to. */
const PRESENCE_RELAY_PATH = "/internal/presence";
+/** Internal paths the Hub entrypoint (index.ts) calls for the system site. */
+export const BROADCAST_PATH = "/internal/broadcast";
+export const EXPORT_PATH = "/internal/export";
/** Per-socket presence subscription, persisted on the hibernatable socket via
* serializeAttachment so it survives eviction. Presence events are filtered
@@ -102,6 +105,18 @@ export class SystemState implements DurableObject {
return new Response(null, { status: 204 });
}
+ // Fronting / mental-state events from the system site, via the Hub entrypoint
+ if (url.pathname === BROADCAST_PATH && req.method === "POST") {
+ this.broadcast(await req.json());
+ return new Response(null, { status: 204 });
+ }
+
+ // Every stored key, for the system site's one-time import
+ if (url.pathname === EXPORT_PATH && req.method === "GET") {
+ const entries = await this.state.storage.list();
+ return Response.json(Object.fromEntries(entries));
+ }
+
return systemApp.fetch(req, {} as never);
}