Remote / foldkit-remote-server

Compose and validate the server

A complete server definition is just the collection of Sources:

ts
const FullServer = RemoteServer.make({
  entities: [ProjectSource],
  queries: [ProjectsByOwnerSource],
  mutations: [RenameProjectSource],
  live: [ProjectLive],
})

When the client and server share the Remote domain declaration, validate the Source names at startup:

ts
const Data = Remote.define({
  entities: [Project],
  queries: [ProjectsByOwner],
  mutations: [RenameProject],
})

RemoteServer.validate(Data, FullServer)

That catches an Entity, Query, or Mutation Source whose name is not declared by the shared domain instead of letting it silently answer nothing at runtime.

viewer says who asks, as a row, and is the server's one answer to it: RemoteServer.make({ ..., viewer: principal => Option<{ entity, id }> }). The server hands it to every Source it mounts, with each read, query and live subscription (EntitySourceContext.viewer), and to every write. A Source answering a member per reader declares it (perViewer, as a Derived.exists does), and a server with no viewer refuses that Source where it is made, naming it. A query body's Expr.viewer and Expr.now read this viewer and the server's Effect Clock (exprContext).

The viewer is also whom every answer says it is for. A read's, a list's and a mutation's result carry principal: the viewer's key ('User:ada'), or null for a visitor. The client treats it as opaque, stamps each durable write it queues with it, and sends it back with every send of that write. The handlers compare it with whom they answer as before the mutation's Source runs: another person is refused with RemotePrincipalChanged (a 409 over JSON), a visitor with Unauthenticated, and nothing runs, a ledger's replay included, so a write resent after the cookie changed hands is never run, or answered from its record, as someone else. A write that names null (made with no one signed in) runs only for a visitor; asked by someone signed in it is refused with RemotePrincipalChanged, unrun. A write that names no one (made before any answer said who) runs as whoever asks. A server with no viewer keeps no keys: its answers carry no principal, and it refuses a write that names one, which it cannot check.

Then compile it into handlers, once per principal, or once for every request with a Context key as the principal (see Authentication vs authorization):

ts
const bounded = RemoteServer.handlers(FullServer, principal, {
  maxIdsPerEntity: 1000,
  maxDepth: 8,
  live: hub,
})

handlers implements the RemoteRpc client shape directly. In-process — tests, SSR, a worker, or a single service — it can become a RemoteClient with:

ts
const clientLayer = Remote.clientLayer(handlers)

Across a process boundary, use:

ts
const rpcLayer = RemoteRpc.toLayer(handlers)

The application supplies the Effect RPC server/transport layer. RemoteServer does not choose HTTP, WebSocket, or another transport for you.

Runtime dependencies belong to Sources

Sources are Effects, so dependencies stay explicit: a Project Source can require Database and a Billing Query Source can require BillingApi, making the composed RemoteServer require Database | BillingApi.

The server definition leaves both services uncaptured; provide their Layers at the application boundary. This is also why foldkit-remote-drizzle provides Drizzle-backed Sources rather than putting a database inside RemoteServer.