session-relay
Consumer-authenticated session access through a same-origin backend.
Relay Aperture session access through your application's backend. Your application authenticates the browser and decides which session it may access; Aperture credentials stay on the backend.
npm install @aperture-browser/session-relay effect @effect/platform-nodeMount the relay
sessionRelay registers seven GET routes on an Effect HttpRouter below <prefix>/sessions/:sessionId: /browser/status, /session, /webrtc/signal, /recordings/:recordingId/content, /thumbnail, /targets/:targetId/thumbnail, and /targets/:targetId/favicon. /browser/status, thumbnails, and favicons are passive; /session and /webrtc/signal upgrade to WebSockets and may wake a suspended session. The Node adapter supplies upstream HTTP and WebSocket clients; it does not start a server.
import * as Layer from "effect/Layer";
import { sessionRelay, type SessionRelayOptions } from "@aperture-browser/session-relay";
import { nodeRelayLayer } from "@aperture-browser/session-relay/node";
export function relayRoutes(authorize: SessionRelayOptions["authorize"]) {
return sessionRelay({
prefix: "/aperture",
publicOrigin: "https://app.example.com",
apertureBaseUrl: "https://aperture.example.com",
authorize,
}).pipe(Layer.provide(nodeRelayLayer));
}Merge the returned layer with your app's other routes and serve it with HttpRouter.serve and NodeHttpServer. Existing Node HTTP servers can use NodeHttpServer.makeHandler and makeUpgradeHandler for the corresponding request and upgrade events. Configure the server's WebSocket maxPayload for your expected raster frames, for example 16 MiB. Your ingress must support persistent WebSocket upgrades.
Authorize every request
The callback receives the original HttpServerRequest and the requested session identifier. Read your app's login cookie, verify the user's session access, and return an Effect containing:
import * as Effect from "effect/Effect";
import * as Redacted from "effect/Redacted";
import { RelayDenied, type RelayGrant } from "@aperture-browser/session-relay";
// After your application's authentication and authorization have succeeded:
export function grantSession(
sessionId: string,
capability: Redacted.Redacted<string>,
signal: AbortSignal,
): Effect.Effect<RelayGrant> {
return Effect.succeed({ sessionId, capability, signal });
}
// Return these from the authorization callback for login/access failures:
export const signInRequired = Effect.fail(new RelayDenied({ status: 401 }));
export const accessDenied = Effect.fail(new RelayDenied({ status: 403 }));Select the session's collaboration.editorToken or collaboration.viewerToken from your server-side ApertureClient.sessions.getSession result according to the app user's permissions. An aps_ owner token or account API token is rejected as a relay grant. The browser cannot select its role. Aperture enforces that role on both WebSockets and direct WebRTC data channels.
The requested identifier is one path segment containing letters, digits, underscores, or hyphens. It may be an app-owned alias; the callback maps it to the actual Aperture sessionId. Every status request, fallback connection, and reconnect calls the callback again. Authentication callbacks may depend on Effect services; supply those services to the served router.
Own an AbortController for each access lifetime. Return its signal for all connections belonging to that access. Abort it when login expires, the user logs out, or their access is revoked or downgraded. This closes active signaling/session connections and their upstream peers. Reject later callback invocations until the user has access again. Use a fresh controller for a newly granted lifetime. Merely checking login when a socket opens does not revoke an already connected browser.
Connect the browser
import { ApertureSession } from "@aperture-browser/session-react";
import "@aperture-browser/session-react/styles.css";
const access = { kind: "relay", baseUrl: "/aperture", sessionId: "your-session-id" } as const;
export function Browser() {
return <ApertureSession access={access} className="h-full" />;
}For custom elements, assign the same object to element.access. Headless React uses useSession({ access }). Keep the access object stable across renders. No token or role is supplied to the browser; same-origin cookies authenticate it to the consumer app.
Connection behavior
- Status and both WebSockets use the relay mount. Status authorization is forwarded directly without a live-session preflight and does not wake or touch retention. WebSocket connection remains the action that may wake a suspended session. WebRTC video and data channels connect directly to Aperture, subject to its ICE/TURN connectivity. Raster fallback travels through the relay.
- Status uses
source: "live"for a running snapshot,source: "persisted"for the page and thumbnail generation saved at suspension, andsource: "unavailable"when no historical page generation exists. Persisted target IDs may change after resume. - Editors download stopped recordings through the relay. Aperture rejects viewer grants, and aborting the access signal ends a download in progress. The file is streamed, not buffered.
- Session and tab thumbnails pass through the relay for both roles. They never wake a suspended session, which serves the thumbnails saved when it suspended.
If-Modified-Sinceis forwarded, so unchanged thumbnails answer 304. - The relay checks browser Origin against the configured public origin before authentication. It constructs upstream requests afresh, sending only the selected capability and session subprotocol. Browser cookies, Origin, authorization, and tenant headers are not forwarded.
embed_allowed_originsneeds no entry for the app. - Upstream status authorization is checked before each WebSocket upgrade. Denials become close codes 4401/4403; missing or expired sessions use 4404/4410. All four stop client retries. Restore login/access before calling
reconnect()explicitly. Transient transport failures retain normal fallback and reconnect behavior. - Message boundaries and text/binary framing are preserved. Effect sockets apply backpressure; either connection closing disposes the other. No session protocol interpretation or input filtering happens in the relay.
- HTTP responses are not cacheable. The upstream HTTP client must not follow redirects or inject unrelated credentials;
nodeRelayLayersupplies an undecorated client.
Only viewer/editor live-session access, recording downloads, and thumbnails are exposed. Session management, raw CDP, and files are separate backend integrations.