Sync / API

foldkit-sync/do

foldkit-sync/do: serve one document's Sync exchange from a Cloudflare Durable Object, the single writer its journal wants. Each socket the object accepts exchanges against the same journal through serveJournal; the replica side already exists as Sync.transport.socket over the object's wss://… URL, which hears other writers' commits as notices.

The class takes its DurableObject base as a parameter, so this module imports no worker runtime and loads anywhere:

ts
import { DurableObject } from 'cloudflare:workers'

export class SyncHost extends defineDocumentHost(DurableObject, {
  sync: Todos,
  openJournal: (_ctx, env) => openTodosJournal(env.DB),
  resolvePrincipal: request =>
    Effect.succeed({ actorId: request.headers.get('x-actor') ?? 'anon' }),
}) {}

Open the journal once per object: on the first upgrade, so probes never provision storage. Route one object per document (idFromName(sync .documentId)); its in-memory cache and change stream are then exact because nothing else writes.

Functions

armAlarmBrings a Durable Object's alarm to at (ms since the Unix epoch) unless it is already sooner: what an application's own alarm() uses beside the host's, which…
defineDocumentHostDefines the Durable Object class serving one document's Sync exchange. Non-upgrades are 404s that open nothing; a refused principal is a 401; a resolver…

Interfaces

AcceptingWebSocketA server WebSocket: accepted once it serves. The accept member is a worker-runtime extension the DOM type does not declare.
AlarmStorageThe alarm a Durable Object's storage keeps: one per object.
DocumentHostConfigWhat defineDocumentHost needs of the application, per document.
DocumentHostContextThe sliver of a Durable Object the host uses: state for the journal to open with, and, with server, the storage's alarm (getAlarm, setAlarm). Left unknown…
DocumentHostServerThe server's own side of the document, for defineDocumentHost's server.

Types

DocumentHostLayerWhat the resolver needs, from the environment: required exactly when it needs something. Built per upgrade and kept until the socket closes, since…