Skip to content

@ogygia/core

Contexts and facts

app is the server process as one plugin sees it, and ctx is one request. Through them a plugin reads its options, calls shared hooks, signs messages and capability URLs, marks a render as per-visitor, and reads and writes the request facts every plugin shares.

On this page

Every server hook gets one of two contexts. app is the server process as one plugin sees it: setup receives it, and every ctx.app is the same object. ctx is one request as one plugin sees it, made fresh for each request. Both are scoped to the plugin, so app.options and ctx.state are this plugin’s own, and what app.sign signs only this plugin can check.

import { defineServer } from '@ogygia/core/plugin';

export default defineServer<{ header: string }>({
  page(ctx) {
    if (ctx.routeConfig('stamp') === false) return;
    if (ctx.event.cookies.get('session'))
      ctx.perVisitor('cookie:session');
  }
});

app

MemberWhat it is
name, optionsThe plugin’s package name, and the options from its definePlugin, as JSON.
notify, askCall a shared hook on every plugin, or ask for the first answer.
facts(request)A request’s facts, made on first touch. For your plugin’s own code outside a hook that holds the Request.
current()The facts of the request the running code belongs to (a load, a render), found through Kit’s request store, or null outside a request.
perVisitor(request, reason)Say the running render read something per visitor, such as a flag decided for its visitor. The read set of the running render and the request get reason.
observeReads(event)From now on, count the personal things event’s code reads as per-visitor reads (Per-visitor reads). Once per request.
sign(message), verify(message, signature)Sign a string with the app’s region key as an HMAC, in this plugin’s own domain, and check one in constant time (Signing).
deny(why)The refusal ogygia’s own endpoints answer a bad signature with: a 403 with cache-control: no-store, x-ogygia-deny: <why> and x-ogygia-key, the fingerprint of the key it checked with. why is expired, renew-window, foreign-key or sig.
capabilitymint and open a signed URL for an endpoint the plugin serves (Capabilities).
devtools(event)Record a server event into the current request’s devtools stream. Nothing happens unless devtools is on for that browser.
keepAlive(work)Keep a promise alive past the response of the running request, through the platform’s waitUntil when it has one. Outside a request, or without one, it runs on in the process. Never throws.
warn(message)console.warn the message once, prefixed with [<plugin name>]. It remembers the last 1,024 messages.

ctx

MemberWhat it is
eventKit’s own RequestEvent, untouched.
pathThe decoded pathname. A path that doesn’t decode is answered 400 before request runs.
forPagetrue when the request renders on behalf of a page it isn’t, such as a remote function call or the region endpoint: its URL may be the page’s, but it isn’t a request for the page.
state, options, appThis plugin’s per-request state from state(), its options, and its app.
factsThe request’s shared facts.
routeConfig(key)This request’s value for a key this plugin claimed: a route table that claims the path answers first, then the Kit page route’s merged config. undefined when nothing sets it, or for an endpoint.
renderHole(endpoint)Render one signed server island in place, with this request’s event, so its cookies ride along. Its HTML, or null when it can’t render now (the fallback stays, and the browser fetches it).
perVisitor(reason)Say this render belongs to one visitor. The first reason is kept.

handle(ctx, next) gets a smaller context with only event, options and app, since nothing about the request is decided when it runs.

Request facts

Facts are what a request has learned so far, shared by every plugin and by core. The code that learns a fact writes it, and the code that needs it reads it, without either one importing the other. They’re kept per Request object, made on first touch and gone with the request, and they live in one place for the whole process, so a plugin and core still share them when the app ends up with two copies of the module.

FactTypeMeaning
storablebooleanThis render may be stored and served to others later. A plugin sets it before the render, in page. The holes it mints then get long-lived URLs, and data-source receipts are kept.
readsReadsWhat the render read that differs per visitor. reads.perVisitor names the first such read, or is null; reads.pageData says it read its page.
streamedbooleanThe page streams load promises after the document.
privatebooleanThe response belongs to one browser, which turned devtools on. Never store or share it.
answerFormbooleanAn app’s own HTML pass renders this router-fetched page as an answer, a form a store keyed by path doesn’t hold.
sourcesstring[] \| nullData-source receipts (s:<id>:<fp>) the render filed. Collected only once a plugin sets a list here.
assignmentsRecord<string, string>Every flag decided for this request, name to variant. Federation carries them to other apps as claims.
codeOriginsstring[], optionalOrigins island code on this page may load from, learned during the render, such as a peer fragment’s runtime origin.

A plugin adds facts of its own the same way it adds shared hooks, by declaration merging. Make them optional, since every other request has none:

import { defineServer } from '@ogygia/core/plugin';

declare module '@ogygia/core/plugin' {
  interface RequestFacts {
    stamped?: boolean;
  }
}

export default defineServer({
  page(ctx) {
    ctx.facts.stamped = true;
  }
});

Per-visitor reads

A render that read a cookie, a flag or a visitor’s header can’t be stored for everyone, and the plugins that store renders (postrender, the server island cache) need to know. Every render has a read set, and so does the request. reads.perVisitor holds the first per-visitor input, such as flag:<name>, cookie:<name>, header:<name>, locals.<key> or a plugin’s own reason, or null when there was none.

  • ctx.perVisitor(reason) and app.perVisitor(request, reason) add one by hand. Use them when your plugin decides something for the visitor that the render then reads.
  • app.observeReads(event) makes core watch event for the rest of the request. A cookie, a personal header such as authorization, user-agent, accept-language or a forwarded address (or reading the headers whole), a locals value that isn’t its default, the client address, per-request platform data, and a fetch that carries the visitor’s cookies each count as a per-visitor read.

What gets stored shows how postrender uses them.

Signing

app.sign(message) signs a string with the app’s region key, the same key that signs server island URLs, and app.verify(message, signature) checks one in constant time. The key never leaves core. Each plugin signs in its own domain: what one plugin signs never checks for another plugin, and never passes as a region signature.

A message that starts with cap| belongs to capabilities, so sign and verify refuse it with plugin_capability_message.

Capabilities

A capability is a signed URL for an endpoint your plugin serves, such as a fragment the browser fetches later. It follows the rules of the app’s own server island URLs: the same lifetime, an expiry in unix seconds, the same refusal once it expired, and the same renewal the runtime already does for a server island.

  • app.capability.mint(message) signs a capability minted now. message is a function of the expiry, so the expiry is part of what’s signed. It returns { exp, sig, k }, with k the signing key’s fingerprint. Put all three in the URL as query parameters of those names, after your own.
  • app.capability.open(event, message) checks a request for one, by the URL’s exp, sig and k. It charges the visitor’s region budgets the way a server island request does: the probe budget before the signature is checked, the render budget after. It returns { ok: true, headers } to answer with, or { ok: false, response }, the 403 or 429 to send back.

An expired capability is refused with x-ogygia-deny: expired, and the runtime retries it once with &renew=1. Within the renewal window open accepts it and returns an x-ogygia-capability header with the same URL, freshly signed, which the runtime uses from then on. A capability minted on a page that may be stored lives as long as a stored page can. One minted by another deployment, with another key, is refused as foreign-key rather than as a bad signature.

import {
  defineServer,
  type ServerApp
} from '@ogygia/core/plugin';

const message = (id: string) => (exp: string) =>
  `quote|${id}|${exp}`;

export default defineServer({
  request(ctx) {
    const url = ctx.event.url;
    if (url.pathname !== '/__quote') return;
    const id = url.searchParams.get('id') ?? '';
    const opened = ctx.app.capability.open(
      ctx.event,
      message(id)
    );
    if (!opened.ok) return opened.response;
    return new Response(`<p>Quote ${id}</p>`, {
      headers: {
        'content-type': 'text/html',
        ...opened.headers
      }
    });
  }
});

/** The URL a page renders for the browser to fetch. */
export function quoteUrl(app: ServerApp, id: string): string {
  const { exp, sig, k } = app.capability.mint(message(id));
  return `/__quote?${new URLSearchParams({ id, exp, sig, k })}`;
}