Skip to content

@ogygia/federation

Setup

Each app calls federate() once to say who it is, which apps it talks to and what it serves. That one call is the whole federation config, and it returns a typed handle for each peer.

On this page

Every app that federates calls federate() once, in a server module that runs when the server starts. The call says who the app is (name and its private key), which apps it talks to (peers), and, when other apps call it, what it serves (expose and widgets). It returns one handle per peer, keyed by the names you gave them.

Read keys with $env/dynamic/private, as above. Keep the module on the server: a $lib/server/ path or a .server.ts name makes Kit refuse it in browser code, and it has to stay there because a handle signs with the private key.

import { federate } from '@ogygia/federation';
import { env } from '$env/dynamic/private';

export const { cms, dash } = federate({
  name: 'shell',
  key: env.SHELL_SIGNING_KEY,
  peers: {
    cms: {
      origin: 'https://cms.internal',
      key: env.CMS_PUBLIC_KEY,
      timeout: 800
    },
    dash: {
      origin: 'https://dash.internal',
      key: env.DASH_PUBLIC_KEY
    }
  }
});

Registration

federate() registers the app with ogygia’s handle, which serves the federation endpoints from then on. So the module that calls it has to run before the first federation request arrives. Import it from hooks.server.ts, directly or through a module that imports it, such as your route table:

A federation request that comes in before any federate() ran isn’t served: it goes on to your app’s routes, usually a 404, and the server says federation_unregistered once. That happens when federate() lives in a module that only some routes import, such as a +page.server.ts: a fresh server hasn’t loaded it yet.

There’s one registration per server. A second federate() call replaces the first, which is what happens in dev when the module is edited and runs again.

import * as ogygia from 'ogygia/server';
import '$lib/server/peers';

export const handle = ogygia.handle();

Roles

The same call describes every kind of app. What you pass decides its role:

  • A shell consumes fragments. It has peers and a key, and no expose or widgets.
  • A producer serves fragments to other apps, with expose (a whole route table) or widgets (a catalog) or both. Its peers list the apps that call it, with their public keys. Visitors’ browsers load its island code, CSS and images straight from it, so the origin it answers on must be one the browser can reach: a public ORIGIN, or a public address its callers use (Asset origin).
  • A chain does both: a producer that places another team’s fragments in its own pages, and is itself placed in a shell.

A producer exposes its table like this. Its peers name the shell that calls it, and audience names the host the shell’s calls are signed for (Audience):

import { federate } from '@ogygia/federation';
import { env } from '$env/dynamic/private';
import { cmsRouter } from '$lib/server/router';

export const { shell } = federate({
  name: 'cms',
  key: env.CMS_SIGNING_KEY,
  audience: 'cms.internal',
  peers: {
    shell: {
      origin: 'https://www.example.com',
      key: env.SHELL_PUBLIC_KEY
    }
  },
  expose: cmsRouter,
  base: '/cms'
});

Peers

peers is symmetric: every app lists every app it talks to, in either direction, under a name of your choosing. That one map is the whole trust configuration.

  • peers.X.key is X’s public key, or a list of them. It checks calls and thaw notices that come in from X. Every listed key is trusted for both (a key is trusted both ways).
  • The top-level key is this app’s private key. It signs every call and notice this app sends.
  • peers.X.origin is where X is reached, and where its islands’ code may load from on your pages (Code origins).

A peer you only call, and never hear from, can leave out key. Only an app that never calls a peer and never sends a thaw notice can leave out its own key: one that makes no postrender clears, and, if it’s a producer with postrender, sets deploy: 'manual'. A producer whose fragments shells store needs a key, since every postrender clear and every deploy sends notices, and without a key they go out unsigned and every peer refuses them. The name you give a peer is also the prefix your stored pages file its fragments under, so pick one and keep it.

Endpoints

ogygia.handle() serves these paths once federate() ran. Kit shells and router shells get the same ones, since every ogygia app has the handle:

PathWhat it servesSigned
/og/fragment/pageA page of the exposed route table, as a fragment document.yes
/og/fragment/<name>One widget of the catalog.yes
/og/fragment/__catalogThe catalog’s list of widgets and their props.no, on purpose
/og/thawThaw notices from peers.yes, always
/og/fragThe browser’s fetch of a deferred remote region, checked against this app’s own signed URL.this app’s own

Non-ogygia hosts lists each endpoint’s requests and answers in full.

Fail closed

A producer that exposes a table or widgets but lists no peer with a key can’t check anyone, so federate() warns with federation_open_without_keys. Every call to its page and widget endpoints then gets a 401: with no keys, no signature can pass. The catalog stays public, as always. The warning’s own text says the calls would be unauthenticated, which isn’t what happens (#2311, item 1).

When the endpoint is meant to take unsigned calls, because something in front of it already checks the caller (mTLS, a service mesh), pass open: true (Open endpoints).

Peer options

Each entry of peers takes these options:

OptionDefaultWhat it does
originrequiredThe peer’s origin, or a list. Extra entries are replicas of the same build, asked in order for reads; a POST goes only to the first (Replicas).
keynoneThe peer’s public key, base64 SPKI DER, or a list while rotating. Checks calls and notices from it. Leave it out for a peer you only call.
timeout5000Milliseconds for each attempt of an uncached call. Each replica is a new attempt with its own timeout.
cacheoff{ ttl, max }: a stale-while-revalidate cache of the peer’s page documents, ttl in milliseconds, max entries (default 500). See Document cache.
audiencethe origin’s hostThe audience signed into calls to this peer. It must equal what the peer checks as its own. Set it when you reach the peer under another host than it answers on, and on a peer with replicas.
labelthe peer’s nameThe name in a mount’s Server-Timing entries (Tracing).

Federate options

federate() itself takes these options:

OptionDefaultWhat it does
namerequiredThis app’s name. It’s sent in this app’s thaw notices. Peers file your fragments under the name they gave you in their own peers.
keynoneThis app’s private key, base64 PKCS#8 DER. Signs every outbound call and thaw notice. Without it, calls and notices go out unsigned, and peers refuse them.
peersnoneEvery app this one talks to, by name (above).
visitornoneWorks out the visitor’s claims for a deferred remote region, from the hole’s request. See Visitor identity.
exposenoneA route table served as fragments at /og/fragment/page (Exposing routes).
base''The base that exposed table renders under, the same as its routes(table, { base }).
widgetsnoneThe widget catalog served at /og/fragment/<name> (Widgets and catalog).
audiencethe host of ORIGINThe host, or list of hosts, inbound calls must be signed for. With neither, every signed call is refused (Audience).
skewMs120000How far a caller’s clock may be from this one, in milliseconds, which is also how long a signature counts as fresh (Replay and clocks).
openfalseServe expose and widgets to unsigned callers, for an endpoint behind mTLS or a mesh (Fail closed).
maxBodyBytes1048576 (1 MiB)The largest request body accepted on /og/fragment/page and /og/thaw. A larger one gets a 413, before any signature check.
deploy'auto''auto' tells every peer to clear what it stored from this app after a deploy; 'manual' leaves that to you (Deploys).