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

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.
DocumentHostConfigWhat defineDocumentHost needs of the application, per document.
DocumentHostContextThe sliver of a Durable Object the host uses: state for the journal to open with.

Types

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