Sync / foldkit-sync/do

DocumentHostConfig

interface · packages/sync/src/do.ts:96

interface DocumentHostConfig<Message, Shared, Principal, Env, RP = never> {
  readonly sync: Sync<Message, Shared>
  readonly openJournal: (
    ctx: DocumentHostContext,
    env: Env,
  ) => Promise<Journal<Operation, Shared, Principal, WrittenOperation>>
  readonly resolvePrincipal: (
    request: Request,
    env: Env,
  ) => Effect.Effect<Principal, RemoteUnauthenticated, RP>
  readonly settle?:
    | ((
        env: Env,
        journal: Journal<Operation, Shared, Principal, WrittenOperation>,
      ) => Effect.Effect<void, unknown>)
    | undefined
  readonly revalidate?:
    | {
        readonly every: Duration.Input
        readonly key?: ((principal: Principal) => unknown) | undefined
      }
    | undefined
  readonly opens?:
    ((principal: Principal) => Effect.Effect<boolean>) | undefined
  readonly admit?:
    | ((
        principal: Principal,
        operation: Operation,
      ) => AuthorizeDecision | Effect.Effect<AuthorizeDecision, unknown>)
    | undefined
  readonly server?: DocumentHostServer<Principal, Env> | undefined
  readonly pair?:
    (() => readonly [client: WebSocket, server: AcceptingWebSocket]) | undefined
  readonly diagnostics?:
    | ((diagnostic: Diagnostic, request: Request) => Effect.Effect<void>)
    | undefined
}

What defineDocumentHost needs of the application, per document.

Members

DocumentHostConfig.sync

property · packages/sync/src/do.ts:98

readonly sync: Sync<Message, Shared>

The document's contract: its codecs and journal shape.

DocumentHostConfig.openJournal

property · packages/sync/src/do.ts:106

readonly openJournal: (
  ctx: DocumentHostContext,
  env: Env,
) => Promise<Journal<Operation, Shared, Principal, WrittenOperation>>

Opens the document's journal, once: on the first upgrade, not on construction, so probes never provision storage. Over D1 (d1: true), or the object's SQLite storage through ctx. May build with Effect and unwrap; a rejection answers the upgrade with a 500, and the next upgrade tries again.

DocumentHostConfig.resolvePrincipal

property · packages/sync/src/do.ts:118

readonly resolvePrincipal: (
  request: Request,
  env: Env,
) => Effect.Effect<Principal, RemoteUnauthenticated, RP>

Who is opening this socket, from the upgrade request. Fails with a RemoteUnauthenticated (foldkit-remote; RemoteServer.unauthenticated(reason?) makes one) to refuse it: a 401 that says "Unauthenticated", or its reason when it has one, and never its message. A defect, or any other failure, is a 500 that says nothing of it. What it needs (a session store) comes from layer.

DocumentHostConfig.settle

property · packages/sync/src/do.ts:128

readonly settle?:
  | ((
      env: Env,
      journal: Journal<Operation, Shared, Principal, WrittenOperation>,
    ) => Effect.Effect<void, unknown>)
  | undefined

Runs after each exchange's appends and before the reply is read: apply what committed to another store, through journal.recover. Takes the environment — a D1 binding, say — and the journal, because both live outside the exchange. See serveJournal.

DocumentHostConfig.revalidate

property · packages/sync/src/do.ts:145

readonly revalidate?:
  | {
      readonly every: Duration.Input
      readonly key?: ((principal: Principal) => unknown) | undefined
    }
  | undefined

Asks resolvePrincipal again, of the upgrade request, every every while the socket is open: a refusal closes it with 4401 and the reason "Unauthenticated", or the refusal's reason when it has one, and a principal whose key differs from the one it opened with (Policy.keyOf) closes it with 1012, which the replica reconnects after, as the new principal; without key only a refusal closes it. A resolver that breaks closes it with 1011. Without revalidate a socket serves the principal it opened with until it closes, a revoked session included.

DocumentHostConfig.opens

property · packages/sync/src/do.ts:152

readonly opens?:
  ((principal: Principal) => Effect.Effect<boolean>) | undefined

Whether principal may open the document. See journalExchange's opens.

DocumentHostConfig.admit

property · packages/sync/src/do.ts:157

readonly admit?:
  | ((
      principal: Principal,
      operation: Operation,
    ) => AuthorizeDecision | Effect.Effect<AuthorizeDecision, unknown>)
  | undefined

Whether an operation from principal may be appended, before the journal sees it. See journalExchange's admit.

DocumentHostConfig.server

property · packages/sync/src/do.ts:174

readonly server?: DocumentHostServer<Principal, Env> | undefined

The server's own side: who it commits as, and the document's reactions (foldkit-sync/journal's reaction), for a contract from Sync.forApplication(App).make. The host registers them when it opens the journal, before the first socket is served. After an exchange that committed, it sets the object's alarm and then runs them without holding the reply; alarm() runs them and sets the alarm again from what they answer. A run eviction cut off is resumed by the alarm, never by the runtime's retries of a throwing alarm(). The object has one alarm: the host only ever brings it earlier.

DocumentHostConfig.pair

property · packages/sync/src/do.ts:180

readonly pair?:
  (() => readonly [client: WebSocket, server: AcceptingWebSocket]) | undefined

Makes the socket pair for an upgrade: the server end is accepted before serving. Defaults to the WebSocketPair global where one exists (a worker, miniflare); pass one in tests.

DocumentHostConfig.diagnostics

property · packages/sync/src/do.ts:189

readonly diagnostics?:
  | ((diagnostic: Diagnostic, request: Request) => Effect.Effect<void>)
  | undefined

Where each upgrade's diagnostics go, with its request: a HostDefect for every failure answered "Internal error" (making the socket pair, the resolver, the journal, the exchange) and the rest documentHost reports. A pass of reactions that broke has no request of its own: it is reported with one for https://durable-object/alarm. The reporter in the Effect context when not given, by default the log.