Remote / foldkit-remote-server

RemoteServer

namespace · packages/remote-server/src/index.ts:2401

const RemoteServer = {
  memory,
  memoryWriter,
  prepend,
  append,
  remove,
  entity,
  mutation,
  refuse,
  tell,
  report,
  unmet,
  taken,
  invalid,
  forbid,
  unauthenticated,
  write,
  query,
  readAs,
  allowedFields,
  live,
  liveHub,
  make,
  validate,
  readHelper,
  answer,
  handlers,
}

In the guides

Members

RemoteServer.memory

function · packages/remote-server/src/index.ts:1833

memory<P = undefined>(
  config: MemoryConfig<P> & MemoryAnswering<P>,
): MemoryBackend<P>
memory<P = undefined>(config: MemoryConfig<P>): MemoryServed<P>

A backend held in memory, for a first run, a test or a demo. See memory.

RemoteServer.memoryWriter

function · packages/remote-server/src/memoryWriter.ts:118

memoryWriter<P, E extends AnyEntity = AnyEntity>(
  store: MemoryStore,
  entity: E,
  options?: MemoryWriterOptions<E>,
): EntityWriter<P>

A declared write landed over a memory backend's rows (MemoryStore), as foldkit-remote-drizzle's writer lands it over a table. See memoryWriter.

RemoteServer.prepend

function · packages/remote-server/src/index.ts:2412

prepend(
  connection: ConnectionIdentity,
  ref: {
    readonly entity: string
    readonly id: string
  },
): ConnectionChange

A mutation outcome's report that ref now heads the connection.

RemoteServer.append

function · packages/remote-server/src/index.ts:2418

append(
  connection: ConnectionIdentity,
  ref: {
    readonly entity: string
    readonly id: string
  },
): ConnectionChange

A mutation outcome's report that ref now ends the connection.

RemoteServer.remove

function · packages/remote-server/src/index.ts:2424

remove(
  connection: ConnectionIdentity,
  ref: {
    readonly entity: string
    readonly id: string
  },
): ConnectionChange

A mutation outcome's report that ref left the connection.

RemoteServer.entity

function · packages/remote-server/src/index.ts:2435

entity<P = unknown, R = never>(
  entity: EntityName,
  options: {
    readonly read: EntitySource<P, R>['read']
    readonly authorize?: EntitySource<P, R>['authorize']
    readonly filter?: EntitySource<P, R>['filter']
    readonly shared?: ReadonlyArray<string> | undefined
  },
): EntitySource<P, R>

An entity source. Given the Entity, every member it declares (fields, relations, derived members) may be requested; given anything with a name and fields, those fields. A request for anything else never reaches read or authorize. Given only a name, every field passes.

RemoteServer.mutation

function · packages/remote-server/src/index.ts:2467

mutation<
  P = unknown,
  R = never,
  Name extends string = string,
  Input = unknown,
  Output = unknown,
  Refused = unknown,
>(
  mutation: MutationDescriptor<Name, Input, Output, Refused>,
  run: (context: {
    readonly input: Input
    readonly principal: P
    readonly requestId: string
    readonly operation: Option.Option<string>
    readonly now: number
  }) => Effect.Effect<
    MutationOutcome<Output>,
    | RemoteServerError
    | MutationRefused
    | RemoteForbidden
    | RemoteUnauthenticated,
    R
  >,
): MutationSource<P, R>

RemoteServer.refuse

function · packages/remote-server/src/index.ts:2518

refuse<Refused>(
  mutation: MutationDescriptor<string, any, any, Refused>,
  refusal: NoInfer<Refused>,
  message?: string,
): MutationRefused

A refusal of mutation, as a value of its declared Refusal: fail a Source with it (yield* RemoteServer.refuse(SavePost, { _tag: 'Field', ... })) and the client reads it, decoded, with Data.refusal. message is what a person reads, beside it.

RemoteServer.tell

function · packages/remote-server/src/index.ts:155

tell(message: string): RemoteServerError

A failure whose message the caller reads as it is: fail a Source with it (yield* RemoteServer.tell('That report is still being built')) for words written for whoever asked. Every other RemoteServerError is this side's: reported, and answered "Internal error". It takes text, not what was caught: an error's own message may say anything.

RemoteServer.report

function · packages/remote-server/src/index.ts:2540

report(during: string, cause: unknown): Effect.Effect<InternalFailure>

Reports a failure on this side that no handler answers, as the server reports its own: a scheduled job's, or an adapter's on a transport of its own. cause goes to a ServerDefect under a new incident, with during (what was being done: publish due e3). It answers what a caller may be told of it, the fixed words and that id, and nothing of cause.

RemoteServer.unmet

function · packages/remote-server/src/index.ts:2548

unmet(message?: string): RemoteUnmet

A writer's refusal of a row that does not hold the write's when: fail update or delete with it (yield* RemoteServer.unmet()), and RemoteServer.write answers the mutation's Unmet refusal.

RemoteServer.taken

function · packages/remote-server/src/index.ts:2556

taken(message?: string): RemoteTaken

A writer's refusal of an insert whose id a row already holds: fail insert with it (yield* RemoteServer.taken()), and RemoteServer.write answers the mutation's Taken refusal, naming the Entity and the id.

RemoteServer.invalid

function · packages/remote-server/src/index.ts:2564

invalid(
  message: string,
  rule?: Option.Option<string>,
  path?: ReadonlyArray<string>,
): RemoteInvalid

A writer's refusal of a row that would break an invariant: fail update or insert with it, naming the invariant's id and path when the caller may be told them, and RemoteServer.write answers the mutation's Invalid refusal.

RemoteServer.forbid

function · packages/remote-server/src/index.ts:2577

forbid(reason?: string): RemoteForbidden

A refusal of who is asking: fail a Source with it (yield* RemoteServer.forbid()) and the write never runs. reason, when given, is what the person who asked reads; without it the refusal says nothing of why. answer sends it as a 403, and the client's mutation fails with _tag: 'Forbidden'.

RemoteServer.unauthenticated

function · packages/remote-server/src/index.ts:2591

unauthenticated(reason?: string): RemoteUnauthenticated

A refusal of a caller who could not be told: a serveFetch or defineDocumentHost resolver fails with it (RemoteServer.unauthenticated()) when the credential it was sent no longer holds, and a write refused to a visitor fails with it (GuardServer does), so a client can tell a session that ended from a refusal. It answers 401, with reason, when given, for the person who asked; a durable write so answered waits for a sign-in.

RemoteServer.write

function · packages/remote-server/src/index.ts:2613

write<P = unknown, R = never, Name extends string = string, Input = unknown>(
  mutation: MutationDescriptor<Name, Input, {}, any> & {
    readonly write: AnyWrite
  },
  writer: EntityWriter<P, R>,
): MutationSource<P, R>

The Source of a mutation that is a declared Write, with no handler: the input bound to the write lands through writer, and the row as written answers as a patch (a deletion as deleted). An update writes only the keys the client named, when it named some, and fails for a row that is not there; an insert of an id already taken is refused as taken, alike whatever the row holds, unless the writer's ledger records this operation as the insert that made it (a retry), answered with that row; a delete of a row already gone succeeds. A row that moved past the revision the write expects is refused as a conflict, and one that does not hold its when as unmet; a write with a when needs a writer that checks it (preconditions), and is refused here without one. A row the write would leave outside its postcondition (ensures) is refused as invalid, as one breaking an invariant is; a write with one needs a writer that judges it (postconditions).

RemoteServer.query

function · packages/remote-server/src/index.ts:3236

query<P = unknown, R = never, Input = unknown>(
  query: QueryDescriptor<string, Input, ConnectionSpec>,
  run: (context: {
    readonly input: Input
    readonly window: QueryWindow
    readonly principal: P
    readonly viewer?: Option.Option<EntityRef> | undefined
    readonly select?: RelationRequirement | undefined
  }) => Effect.Effect<QueryPage, RemoteServerError, R>,
): QuerySource<P, R>

RemoteServer.readAs

function · packages/remote-server/src/index.ts:336

readAs<P, R>(
  source: EntitySource<P, R>,
  context: EntitySourceContext<P>,
): Effect.Effect<
  Option.Option<ReadonlyArray<EntityRecord>>,
  RemoteServerError,
  R
>

Reads through source as the read handlers and the live hub do: only the asked fields the source declares and its authorize allows, each record through its filter and holding no other field. None when the principal may read none of the fields asked. What a live source of its own reads with, so it sends no more than a read would.

RemoteServer.allowedFields

function · packages/remote-server/src/index.ts:748

allowedFields<P, R>(
  source: EntitySource<P, R>,
  principal: P,
  requested: ReadonlyArray<string>,
): ReadonlyArray<string>

The fields of fields that readAs reads for principal: those the source declares and its authorize allows, in the order asked. Asks nothing of the store.

RemoteServer.live

function · packages/remote-server/src/index.ts:3278

live<P = unknown, R = never>(
  entity: EntityName,
  options: {
    readonly subscribe: LiveSource<P, R>['subscribe']
  },
): LiveSource<P, R>

Streams live entity patches for a client's live requirements.

RemoteServer.liveHub

function · packages/remote-server/src/index.ts:1309

liveHub<P, R>(
  entities: ReadonlyArray<EntitySource<P, R>>,
  options?: {
    readonly key?: ((principal: P) => unknown) | undefined
  },
): Effect.Effect<LiveHub<P, R>>

A LiveHub over entity sources. Pass it to handlers as live, and every mutation's answered patches and deletions are published to it; call hub.changed(ref, fields) yourself only for a write that is not a mutation (a database trigger, a job). Each subscriber that selects any of those fields receives them, re-read through the entity source under its own principal.

RemoteServer.make

function · packages/remote-server/src/index.ts:3313

make<P = unknown, R = never>(config: {
  readonly entities: readonly EntitySource<P, R>[]
  readonly mutations?: readonly MutationSource<P, R>[]
  readonly queries?: readonly QuerySource<P, R>[]
  readonly live?: readonly LiveSource<P, R>[]
  readonly viewer?: ((principal: P) => Option.Option<EntityRef>) | undefined
  readonly clockSkew?: Duration.Input | undefined
}): ServerDefinition<P, R>

The server: its sources by name. viewer says who asks, as a row (principal => Option.some(Remote.ref(User, principal.id))), or none for a visitor: what a declared write's Expr.viewer sets. A server registering such an insert without one is refused here. It is also whom each answer says it is for (principal, the row's key), which a durable write carries back: one made for another viewer than who asks is refused before it runs (RemotePrincipalChanged). A server without viewer names no one in its answers, and refuses a write that names someone, since it cannot check it; a write that names no one runs (#405).

clockSkew is how far from this server's clock a client's time (the now a write is sent with, when the reader acted) may be and still be the write's time: 60 seconds unless given, and 0 to never take it. A time beyond it is replaced by the server's own, and reported as ClockSkewed. It bounds how far a client can backdate or postdate a row.

RemoteServer.validate

function · packages/remote-server/src/index.ts:3366

validate(domain: RemoteDescriptor, server: ServerDefinition<any, any>): void

Checks every source name against the declared domain, so an undeclared entity/query/mutation fails at startup rather than returning nothing at call time.

RemoteServer.readHelper

function · packages/remote-server/src/index.ts:3388

readHelper<P, R>(
  server: ServerDefinition<P, R>,
  principal: P,
  options: HandlerOptions<P, R>,
  requests: ReadonlyArray<Requirement>,
): Effect.Effect<
  {
    readonly entities: Array<{
      readonly entity: string
      readonly id: string
      readonly values: Record<string, unknown>
    }>
    readonly settled: Array<{
      readonly entity: string
      readonly id: string
      readonly fields: Array<string>
    }>
  },
  RemoteReadError,
  R
>

Reads entity requirements through the entity sources, level by level, following relations as FoldkitRemoteRead does. Shared by the read handler and the query handler's payload: a query page's items are just requirements the page names, so one response carries both.

RemoteServer.answer

function · packages/remote-server/src/index.ts:3562

answer<R>(
  handlers: RemoteRpcClient<R>,
  body: unknown,
): Effect.Effect<
  {
    readonly status: 200 | 400 | 401 | 403 | 409 | 422 | 500
    readonly body: RemoteJsonAnswer
  },
  never,
  R
>

Answers one JSON request through handlers: what an HTTP endpoint, a worker or a server in the page calls with the body it received, parsed. The request and its payload are decoded by the protocol's own schemas before a handler sees them, so an operation name is one of Remote's three and nothing else. A body that is not a request is a 400; a caller who could not be told a 401; a refusal of who is asking a 403; a write made for someone else (RemotePrincipalChanged) a 409; a mutation's declared refusal a 422, or a 409 when it is Refusal.taken; a handler's failure a 500 with its message, and the incident it carries; a defect a 500 that says nothing of it but the incident its cause was reported under, as a ServerDefect (Diagnostics, by default the log). Remote.json and Remote.http are the other end.

RemoteServer.handlers

function · packages/remote-server/src/index.ts:3646

handlers<P, R, I = never>(
  server: ServerDefinition<P, R>,
  principal: P | Context.Key<I, P>,
  options?: HandlerOptions<P, R>,
): RemoteRpcClient<R | I>

Compiles the server into Remote's RPC handlers, answering as principal: a value, or a Context key (G.Principal) read on each call, which authentication provides per request. Unknown entities, entities with no allowed fields, and unknown mutations return an error or nothing rather than leaking existence.