Sync / foldkit-sync/journal

DocumentHostOptions

interface · packages/sync/src/journal.ts:755

interface DocumentHostOptions<Message, Shared, Principal, RP = never> {
  readonly sync: Sync<Message, Shared>
  readonly journal: () => Promise<
    Journal<Operation, Shared, Principal, WrittenOperation>
  >
  readonly resolvePrincipal: (
    request: Request,
  ) => Effect.Effect<Principal, RemoteUnauthenticated, RP>
  readonly revalidate?:
    | {
        readonly every: Duration.Input
        readonly key?: ((principal: Principal) => unknown) | undefined
      }
    | undefined
  readonly settle?:
    | ((
        journal: Journal<Operation, Shared, Principal, WrittenOperation>,
      ) => Effect.Effect<void, unknown>)
    | undefined
  readonly opens?:
    ((principal: Principal) => Effect.Effect<boolean>) | undefined
  readonly admit?:
    | ((
        principal: Principal,
        operation: Operation,
      ) => AuthorizeDecision | Effect.Effect<AuthorizeDecision, unknown>)
    | undefined
  readonly diagnostics?:
    | ((diagnostic: Diagnostic, request: Request) => Effect.Effect<void>)
    | undefined
}

What documentHost needs: one document, who may open it, and what they may do.

Members

DocumentHostOptions.sync

property · packages/sync/src/journal.ts:756

readonly sync: Sync<Message, Shared>

DocumentHostOptions.journal

property · packages/sync/src/journal.ts:761

readonly journal: () => Promise<
  Journal<Operation, Shared, Principal, WrittenOperation>
>

The document's journal, asked on each upgrade a principal passed; keep it open between calls. A rejection answers the upgrade with a 500.

DocumentHostOptions.resolvePrincipal

property · packages/sync/src/journal.ts:772

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

Who is opening the socket, from the upgrade request. Fails with a RemoteUnauthenticated (RemoteServer.unauthenticated(reason?)) to refuse it: a 401 that says "Unauthenticated", or its reason, the words written for the caller, when it has one. Its message is never sent. A defect, or a failure that is no RemoteUnauthenticated, is a 500 that says nothing of it: a session store that is down is no refusal, so let it die (Effect.orDie). A browser's WebSocket sends no header of the page's: Origin and the cookie are what it has.

DocumentHostOptions.revalidate

property · packages/sync/src/journal.ts:786

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 (cut to the 123 bytes a close carries), 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.

DocumentHostOptions.settle

property · packages/sync/src/journal.ts:793

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

After each exchange's appends, given the journal. See journalExchange's settle.

DocumentHostOptions.opens

property · packages/sync/src/journal.ts:799

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

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

DocumentHostOptions.admit

property · packages/sync/src/journal.ts:801

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

Whether an operation from principal may be appended. See journalExchange's admit.

DocumentHostOptions.diagnostics

property · packages/sync/src/journal.ts:814

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

Where this upgrade's diagnostics go, with its request: each HostDefect, a failure answered "Internal error" (or a socket closed with 1011, or a notice not sent) whose cause the replica is never told. The reporter in the Effect context (Diagnostics.Reporter) when not given, by default the log.