Remote / foldkit-remote-server

Over plain JSON

RemoteServer.answer(handlers, body) answers one request as Remote.http and Remote.json send it, whatever received it: an HTTP route, a worker, a server running in the page. It decodes the request and its payload by the protocol's own schemas before a handler sees them, so an operation is one of Remote's three and nothing else, and says what to send:

ts
// A fetch-style route (Bun, Deno, a Worker; Node through its own adapter).
const route = async (request: Request): Promise<Response> => {
  const body: unknown = await request.json().catch(() => null)
  const answered = await Effect.runPromise(RemoteServer.answer(handlers, body))
  return Response.json(answered.body, { status: answered.status })
}
  • 200 with { result }: the handler answered.
  • 400 with { error }: the body is not a request, names an operation Remote does not have, or its payload fails the protocol's schema. No handler ran.
  • 401 with { error, unauthenticated: { reason? } }: a handler could not tell who is asking (RemoteServer.unauthenticated). The client fails the call, of whatever kind, with _tag: 'Unauthenticated'.
  • 403 with { error, forbidden: { reason? } }: who is asking may not do this (RemoteServer.forbid). In both, error is the reason, or Unauthenticated or Forbidden when the refusal gave none: the refusal's own message is not sent, here or in a live stream's error frame.
  • 409 with { error, refusal }: a declared insert's id is already a row's, refused as Refusal.taken.
  • 409 with { error, principalChanged: {} }: the write names someone other than who asks (RemotePrincipalChanged). Nothing ran. The client fails it with _tag: 'PrincipalChanged', and the outbox keeps it waiting for them.
  • 422 with { error, refusal }: the mutation refused what was asked, in its own Refusal.
  • 500 with { error, incident }: something on this side broke (a Source's failure, a thrown error, a defect). The message is Internal error, nothing more, and incident is the id the cause was reported under, as a ServerDefect diagnostic, by default to the log, so a Worker's log (wrangler tail) shows what broke.
  • 500 with { error } and no incident: the server's own fixed sentence about the request (an unknown query or mutation, an input its Schema refuses, too many ids), or words a Source wrote for the caller with RemoteServer.tell. See When the server fails.

It never answers 408, 429, 502, 503 or 504. Remote's client reads a body under one of those, or one that is not JSON, as no answer at all (Unreachable): a proxy, gateway or rate limiter in front that answers one leaves the client unsure whether the write ran, and a durable write is sent again (see A write sent again).

It binds no principal: pass the handlers for the request's own, established by your authentication, as everywhere else. It carries no live data.

For an endpoint that serves RemoteRpc through Effect's own RpcServer, bodyLimit(maxBytes) is the same limit as HTTP middleware (Effect's MaxBodySize fails the RPC body read as a defect, a 500):

ts
import { Layer } from 'effect'
import { HttpRouter } from 'effect/http'
import { RpcSerialization, RpcServer } from 'effect/rpc'
import { RemoteRpc } from 'foldkit-remote'
import { RemoteServer, bodyLimit } from 'foldkit-remote-server'

// `Server` is your RemoteServer.make; answered as a visitor here.
const RpcRoutes = RpcServer.layer(RemoteRpc).pipe(
  Layer.provide(RemoteRpc.toLayer(RemoteServer.handlers(Server, { isAdmin: false }))),
  Layer.provide(RpcServer.layerProtocolHttp({ path: '/rpc' })),
  Layer.provide(RpcSerialization.layerJson),
  Layer.provide(HttpRouter.middleware(bodyLimit(64 * 1024)).layer),
)

The client reads that 413 as the call's own error, and a 401 or 403 from session middleware in front of the route as Unauthenticated or Forbidden, only over Remote.rpcProtocolHttp(url) in place of RpcClient.layerProtocolHttp({ url }), which reads the refusal as RPC frames and loses its status: a durable write would then read as unreachable and be sent again for good.

Decide who is asking from the HTTP request, in front of the route, never from the headers an RpcMiddleware or a handler is given. RpcServer puts the HTTP request's headers before those the request body carries and keeps the last of each name, so a body can claim another cookie, origin or CSRF header; over a WebSocket, every frame can.