diff --git a/web/src/main.ts b/web/src/main.ts index 821a48e..a843246 100644 --- a/web/src/main.ts +++ b/web/src/main.ts @@ -13,6 +13,7 @@ import "./style.css"; class GhostlightEveTransport implements EveBrowserProviderTransport { private provider: EveProviderAdvertisement | undefined; private sourceVersion = 0; + private authenticatedSurface = false; async providerAdvertisement(): Promise { this.provider = await fetchJson("api/eve/provider"); @@ -25,9 +26,14 @@ class GhostlightEveTransport implements EveBrowserProviderTransport { const query = pendingInvite ? `?invite=${encodeURIComponent(pendingInvite)}` : ""; const document = await fetchJson(`api/eve/surfaces/${encodeURIComponent(surfaceId)}${query}`); this.sourceVersion = Math.max(0, Math.trunc(document.version || 0)); + this.authenticatedSurface = !containsAnonymousAccessGate(document); return document; } + hasAuthenticatedSurface(): boolean { + return this.authenticatedSurface; + } + async submitCommand(intent: EveCommandIntent): Promise { const result = await fetchJson("api/eve/commands", { method: "POST", @@ -79,6 +85,19 @@ async function fetchJson(input: RequestInfo | URL, init?: RequestInit): return body as T; } +function containsAnonymousAccessGate(value: unknown): boolean { + if (!value || typeof value !== "object") return false; + if (Array.isArray(value)) return value.some(containsAnonymousAccessGate); + const node = value as Record; + if (node.kind === "heimdall.access_gate") { + const props = node.props && typeof node.props === "object" && !Array.isArray(node.props) + ? node.props as Record + : {}; + return props.state === "anonymous"; + } + return Object.values(node).some(containsAnonymousAccessGate); +} + function requiredElement(id: string): T { const element = document.getElementById(id); if (!(element instanceof HTMLElement)) throw new Error(`Missing browser host #${id}.`); @@ -114,8 +133,10 @@ await resumeHeimdallAccess({ }, }); -const events = new EventSource("api/eve/events"); -events.addEventListener("revision", () => void host.refresh()); -events.addEventListener("error", () => { - status.textContent = "Live revision notices are unavailable; your authoritative surface remains safe to refresh."; -}); +if (api.hasAuthenticatedSurface()) { + const events = new EventSource("api/eve/events"); + events.addEventListener("revision", () => void host.refresh()); + events.addEventListener("error", () => { + status.textContent = "Live revision notices are unavailable; your authoritative surface remains safe to refresh."; + }); +}