When screens declare the exact fields they need, serving them usually forces you to choose between writing one custom endpoint per screen or exposing a raw query language that mixes row loading with field permissions.
This package lets you keep one server definition instead. Define a small
Source for each entity, query, mutation, and live stream. When a client
sends semantic requirements, the server validates the protocol and input,
strips any fields the caller may not read before touching your database,
de-duplicates shared IDs, and normalizes rows back into patches for
foldkit-remote.
foldkit-remote-server runs the Chirp demo
and examples/kitchen-sink. Your Sources decide
how data is loaded and written; your application authenticates the caller into a
principal and serves over Effect RPC, plain JSON over HTTP, Cloudflare
Workers, or a MessagePort.
pnpm add foldkit-remote-servereffect is a peer dependency; foldkit-remote comes with the package.
foldkit-remote-drizzle compiles Drizzle tables into
Sources when your data lives in SQLite or Postgres.
Define an entity, authorize fields per caller, and mount the Source into Effect RPC handlers:
import { Effect, Schema } from 'effect'
import { Entity, Remote, RemoteRpc } from 'foldkit-remote'
import { RemoteServer } from 'foldkit-remote-server'
const Project = Entity.make(
'Project',
Schema.Struct({ id: Schema.String, name: Schema.String, privateNotes: Schema.String }),
)
type Principal = { readonly isAdmin: boolean }
const rows = new Map([['p1', { id: 'p1', name: 'Apollo', privateNotes: 'Internal' }]])
const ProjectSource = RemoteServer.entity<Principal>(Project, {
authorize: (principal, fields) =>
fields.filter(field => field !== 'privateNotes' || principal.isAdmin),
read: ({ ids, fields }) =>
Effect.succeed(
ids.flatMap(id => {
const row = rows.get(id)
if (row === undefined) return []
const values = Object.fromEntries(Object.entries(row).filter(([k]) => fields.includes(k)))
return [{ id, values }]
}),
),
})
const Server = RemoteServer.make({ entities: [ProjectSource] })
const principal: Principal = { isAdmin: false }
const handlers = RemoteServer.handlers(Server, principal)
const layer = RemoteRpc.toLayer(handlers)When a client requests Project:p1 [id, name, privateNotes] for a non-admin
caller:
Projectdeclaresid,name, andprivateNotes, so all three pass schema validation.authorizeremovesprivateNotes, leaving['id', 'name'].readreceivesids: ['p1']andfields: ['id', 'name'], and returns{ id: 'p1', values: { id: 'p1', name: 'Apollo' } }.- The handler returns
{ id: 'p1', values: { id: 'p1', name: 'Apollo' } }and marksprivateNotesas settled (unavailable).
Because privateNotes is settled without a value, the client stops asking for
it on subsequent renders. A selection that omits privateNotes reads Ready;
one that includes it reads Failed with an Unavailable error until
Data.refresh asks again.
A client selection can request several IDs and nested relations at once. Before
calling your Sources, the handler groups requirements by entity, takes the union
of requested fields across screens, keeps only declared fields that pass
authorize, and batches de-duplicated IDs into one read({ ids, fields, windows?, principal }) call per entity.
If any selected field is a relation reference, the handler follows the returned IDs level by level into the target Entity's Source until every selected branch is resolved, then returns normalized patches.
Key rules enforced by every Entity Source:
- Undeclared fields are dropped before
authorizeorreadruns. Fields, relations, and derived members are all declared on the Entity, so a relation such asowneris authorized and followed like any field. - Requested fields are matched as own properties returned by your Source, so
constructoror__proto__are settled rather than read fromObject.prototype. authorizecan only narrow requested fields; it never adds fields the client did not ask for.- A Source may omit a requested field; any omitted field is settled as unavailable so the client stops re-requesting it.
- Shared IDs across multiple selections are fetched once, and relation targets are batched level by level without redundant fetches.
- A relation the principal may not read is skipped without following its target.
authorizeruns once per field across all rows;filterruns per record afterreadand may request extra fields (such as an owner ID) needed to decide visibility. Members removed byfilterare settled as unavailable.foldkit-guardbuildsfilterrules from member guards.- A Source or Query Source may declare
visible(from a Drizzle binding orRemoteServer.memory'sreadable) so startup checks likeGuardServer.validatecan verify row-level rules.
Keep your Source queries tight by projecting only fields in your database or
API call: the handler strips extra keys on return, and projecting at the data
boundary avoids wasted work.
When a client projects Project:p1 { id, name, owner { id, name } }, the
Project Source returns owner as a reference (User:u7) rather than joining
or inlining the user row.
RemoteServer collects all owner refs across the batch and requests their
selected fields from UserSource in one batched level. The Sources never call
each other recursively: RemoteServer handles traversal, depth limits,
batching, and de-duplication while each Source answers only for its own Entity.
A Query Source answers the ordered connection itself. The client's select
travels with the request, and the handlers return the selected fields of the
page's items with the edges, so one response carries both:
import { Query } from 'foldkit-remote'
const ProjectsByOwner = Query.make('ProjectsByOwner', {
Input: Schema.Struct({ ownerId: Schema.String }),
Result: Query.connection(Project),
})
// `projectsPage` is your own reader: one page of edges for the window.
const ProjectsByOwnerSource = RemoteServer.query(
ProjectsByOwner,
({ input, window, principal }) => projectsPage({ ownerId: input.ownerId, window, principal }),
)
const QueryServer = RemoteServer.make({
entities: [ProjectSource],
queries: [ProjectsByOwnerSource],
})A Query returns:
{
edges,
start,
end,
}A Source answers membership only; it may ignore select. The handlers fetch
the selected fields through the entity sources (authorization included) and
return them as entities/settled beside the page. Query membership and
entity data stay separate on the way in and travel together on the way out.
start and end are Remote Boundary values (Terminal, Cursor, or
Unknown). The client uses those explicit boundaries when merging pages instead
of guessing from row count.
Query input is decoded through the Query's own Schema before your callback runs.
The callback receives the decoded input, the requested QueryWindow, the
select slice when the client sent one, the principal, and the server's
viewer of it.
A query declared with Query.define carries its meaning as a value, so it
needs no hand-written Source: RemoteServer.memory runs its body with
evaluate, and foldkit-remote-drizzle compiles it to SQL (see Reference
semantics).
A Mutation Source changes server-owned state. It returns the protocol output plus any normalized entity/connection changes that should settle the client's cache immediately:
import { Effect } from 'effect'
import { Mutation } from 'foldkit-remote'
const RenameProject = Mutation.make('RenameProject', {
Input: Schema.Struct({ id: Schema.String, name: Schema.String }),
Output: Schema.Struct({ id: Schema.String }),
})
// `renameProject` is your own write, answering the Output.
const RenameProjectSource = RemoteServer.mutation(
RenameProject,
({ input, principal }) =>
renameProject(input, principal).pipe(
Effect.map(output => ({
output,
entities: [
Entity.patch(Project.ref(input.id), { name: input.name }),
],
})),
),
)The input is decoded before the Source runs and the output is encoded before it
crosses the wire. The entities are cache patches of rows the caller may read:
the answer carries your values, cut to what authorize and filter let the
caller read of each row, and none for a row they may not read at all (a source
with no authorize, filter or visible is answered as it is, with no read; if the read fails, none of
that entity's patches is sent and an AnswerUnread is reported, and the write
still succeeds). Live subscribers are told every patch and read each through
their own rules. What a handler must tell a caller who may not read the row goes
in output, which is its own Schema. A Source refuses with a value of the mutation's Refusal:
Effect.fail(RemoteServer.refuse(SetPrice, { _tag: 'Field', key: 'price', reason: 'too low' })).
The value is type-checked against that mutation, encoded by its schema, and
sent beside a message (a 422 over JSON); a refusal made for another mutation,
or not one the mutation declares, is sent as an invalid refusal instead.
A refusal of who is asking, rather than of what they asked, is
Effect.fail(RemoteServer.forbid('Only an admin may archive.')): sent as
Forbidden with the reason (a 403 over JSON), or with none when forbid() is
given none, so the refusal says nothing of why. It is the same for every
mutation, so it needs no Refusal declared. A refusal of a visitor, someone
not signed in, is RemoteServer.unauthenticated(reason) instead (a 401):
signing in is the remedy, and Remote's outbox keeps a durable write so
answered waiting for it, where it settles a Forbidden one as failed.
RemoteServer.write refuses a visitor a write whose Entity sets a member from
who asks that way, and GuardServer refuses a visitor every write it would
refuse.
A Source may additionally return connection changes:
RemoteServer.prepend(connection, Project.ref(id))
RemoteServer.append(connection, Project.ref(id))
RemoteServer.remove(connection, Project.ref(id))Those confirmed changes replace the corresponding optimistic connection layers on the client.
The Source is also given requestId, the client's id for the request, and
operation, the operation's id across clients, both the same on every retry of
it. Record a write under operation, never requestId: a request id is a
counter in one client's Model, so two clients' first requests share it, and a
record keyed by it would answer one client with another's write. operation
is none from a client that sends none, and bounded on the wire (128
characters of A-Z a-z 0-9 : . _ -).
Expect the same operation more than once. A write that no answer came back
for fails on the client with Unreachable (RemoteUnreachable): the request
did not go, or what came back was no Remote answer (not JSON, or under a 408,
429, 502, 503 or 504, which this package never answers with but a gateway or
a rate limiter may). The server may have landed it. Data.retry sends it
again under the same operation, and a durable write (Data.mutate(…, { durable: true })) is stored in the page's outbox and sent again on its own,
after an Unreachable and after a reload, with the same operation id and the
same now. What a retry does depends on the write: setting a value sets it
again, an expected revision answers Conflict, and only a ledger (below)
recognises the operation and answers it from its record. A durable Mutation
therefore needs a writer with a ledger, or a handler that records by
operation.
The outbox never sends one principal's waiting writes as another's: it holds its queue for one store key and scope, and sends none of it once either changes. The server still decides each send as whoever that request authenticates as.
A mutation declared as a Write (Mutation.write) needs no handler.
RemoteServer.write(mutation, writer) binds the input to the write, only the
keys the client named when it named some, and hands the row's id and new values
to an EntityWriter, which writes them and answers the row as written:
// `EditProject` is a Mutation.write; `writer` and the binding `Db` are foldkit-remote-drizzle's.
RemoteServer.write(EditProject, writer(Db.Project))The answer is the mutation's patch, or for a delete its deletion. The writer must be for the write's own Entity, which is checked where the mutation is served. The declared writes guide walks through the whole path; what this package does with each part of a write:
- What the server chooses. A write of an Entity with
Entity.setsis handed the members its context sets as well, never from the input (an insert all of them, an update those seton: 'write'):Expr.viewerfromRemoteServer.make({ viewer: principal => Option<{ entity, id }> }), andExpr.nowfrom the write's time. A value that comes to null where the member holds none (a visitor's) is refused as forbidden; a server that serves a write setting the viewer with novieweris refused where it is made. A path from the viewer is read through the server's own sources, as the asker may see each row (the run context'sread); a server that mounts no source for a row the path reaches is refused where it is made, and a path reaching no row the asker may see is null. - The write's time is the client's when it sent one (
Data.mutate'snow, the moment the reader acted, which the client showed the row at) and it lies withinRemoteServer.make({ clockSkew })of the server's EffectClock: 60 seconds unless given,0to never take a client's time. Otherwise it is the server's own, and a time past a bound above zero is reported as aClockSkeweddiagnostic: a durable write sent long after it was made takes the server's time. The bound is how far a client can backdate or postdate a row; nothing else of a write's context comes from the client. A hand-written mutation reads the same time asnowin its context. A retried insert, which only a writer's ledger recognises, keeps its first time. A retried update is a new write to a server without a ledger, and stores the time of the attempt that ran: the same sentnowwhile it lies withinclockSkew, else the server's clock at that attempt. A ledger answers the retry from its record, so the first time stays. expect: the writer is told the revision the row was read at; no row at it is refused asRefusal.conflict.when: the writer is handed the precondition and the input encoded, checks it in the statement that writes, and fails a row at the revision that does not hold it withRemoteUnmet(RemoteServer.unmet()), refused asRefusal.unmet: absent first, then a moved revision, then the precondition.- Invariants (
Entity.invariant) are the writer's to judge over the row the write would leave, refusing withRemoteServer.invalid(message, rule, path), sent asRefusal.invalid: the invariant's id and path, or none and empty when the caller may not be told them. - Increments and computed assignments reach the writer apart from its
values (
increments, each field's amount, encoded;assignments, each aComputation), to be applied in the statement that writes. One that expects no revision is served only over a writer with a ledger, and only for a request carrying an operation id; even so, a concurrent pair must land as one result: an increment only of an integer, and a computed assignment only where the writercommutesit, unless the writer pins the row it read (pins: true, a guard with a revision). - A rule over another row. A writer that holds an update's or a delete's
row to a
requires(RowRequirement: bodies over the row, all or any of them) in its own statement refuses a row that fails it withForbidden:foldkit-guardhands it a rule that reads another row that way. - Creates and deletes. An update of no row fails, and a delete of a row
already gone succeeds, so its retry is safe. An insert of an id already
taken is refused as
Refusal.taken(the writer failsinsertwithRemoteTaken,RemoteServer.taken()), with one answer whatever the row there holds and whether its asker may see it: comparing that row with the insert would tell the asker what it holds. Only a writer with a ledger tells a retry from another create of the id, by the operation the client sent, and answers it with the row; without one, a create whose answer was lost is refused when it is sent again. Give a writer a ledger where a client may resend a create, and make ids that a guess cannot find where whether a row exists is itself private.
A writer says what it can do, and RemoteServer.write refuses, where the
write is served, one that needs what its writer lacks rather than writing it
wrongly:
EntityWriter says |
It | Needed by |
|---|---|---|
preconditions: true |
checks when in the write statement |
a write with a when |
invariants: true |
judges the Entity's invariants | a write of an Entity with invariants |
postconditions: true |
judges ensures beside invariants on update and insert |
a write with ensures |
increments: true |
adds increments in the write statement |
a write with an increment |
assignments: true |
computes assignments in the write statement |
a write with a computed assignment |
ledger: true (with recorded) |
records each update and insert under its operation |
an increment or computed write without expect |
requires: true |
holds the row to requires |
GuardServer.writer over cross-row rules |
refuses(write) |
names store limits (such as fractional ints in Drizzle) | every write, checked before execution |
A writer with a read (the row as a principal may see it, under the read
rule its Source applies) answers through it: the patch is the written row as
its writer now sees it, and a row the write moved out of their sight answers
in deleted, so the client takes it out of every list rather than placing
it where the server would not list it. A writer may refuse with
RemoteServer.forbid, as foldkit-guard's GuardServer.writer does. Its
linked answers a row's links as stored, whatever a reader may see of the
rows they point at, so such a guard can tell a write that unlinks a hidden row
from one that changes nothing; foldkit-remote-drizzle's writer has both.
A Source answers the rows it changed; the client says what it shows of them.
Data.mutate sends, with each request, what its active Surfaces read of each
entity, every field its optimistic layer shows, and the supplied members a
created or edited row foresees (may) (shown). After the Source runs, the handler reads, of each row the
outcome names (a patch, or a list insert), the shown fields the patch left out
and every shown relation, and returns that read beside the patch (read).
So a declared write, which answers the columns it stored, also answers the
derived count, the may and the author's name the page reads of the row, and
the client releases its optimistic layer with nothing left to fetch. The read
is an ordinary one: the Sources' read, authorize and filter decide it, a
field the asker may not read comes back settled, and a row they cannot see is
left out of it, keeping what the patch gave.
A Source whose write the server cannot see (a row of a join table, a count it keeps itself) names the row with no values, and the read answers all of it:
// Sketch, inside a mutation Source over a `Post` Entity: a like in a join table
// changes its post, so name the post and read nothing yourself.
return { output: { liked: true }, entities: [Remote.patch(Post, input.postId, {})] }The read runs after the write commits, outside its transaction. If it fails,
it is reported as an AnswerUnread diagnostic and the
mutation is answered without it: the write has
landed, and the client fetches what it lacks as it would have. A deleted row is
not read.
A request also names the lists the client judges the write moves (lists):
each the first page of a query, as the client's read entry would ask for it.
The handler admits them before the write runs, then reads each after it with
the query's own handler, so the page is what FoldkitRemoteQuery would answer
(authorize and filter included), and returns them beside the rows in
answer.lists.
A list is admitted when its query is one the server serves, its window is a
first page (first, no after), its select names the query's Entity within
a read's page bound, and the rows of every list admitted so far stay within
maxIdsPerEntity per Entity. One that is not, or whose read fails (reported
as AnswerUnread), is left out; the write runs either way, and the client
fetches that list as it would have. The wire refuses more than
MAX_ANSWER_LISTS lists in one request.
A declared write (RemoteServer.write) also reads each row its row points at
through a one relation, before and after the write, through the writer's
linked, where that link can change one of the row's derived members: a
Derived.count or Derived.exists over that relation, or any supplied member
(Derived.make), which may read anything. Inserting a Like reads what the
page shows of its post, the new likeCount included, and not its user,
whose members no like changes. A member declared after Entity.relate counts:
the Entity has one set of members, whichever descriptor declared them. A row's derived
members change only with the rows pointing at it, so no app names them. These rows are read, never sent as patches of their own: one the
asker may not see is not answered at all. Live subscribers hear the outcome's
own patches, not the read, which is one asker's view.
A live subscription is one stream per Entity, from that Entity's live Source. There are three ways to produce one:
- write a live Source directly, below;
- over Drizzle,
foldkit-remote-drizzle'spollLive, a live Source that re-reads the subscribed rows and lists from inside the subscriber's own request, which is what Workers need; - in a long-lived process, a
liveHubthat re-reads what each write changed.
// `projectEvents` is your own stream of `LiveChange`s.
const ProjectLive = RemoteServer.live(Project, {
subscribe: ({ requirements, queries, after, principal }) =>
projectEvents({
requirements,
queries,
after,
principal,
}),
})after is the client's resume cursor. Events at or before it are duplicates,
and the stream's cursors count on from it: its first event is after + 1, or
the client takes the skipped cursors for a gap and restarts the stream.
The stream carries Remote LiveChanges: entity patches/deletes and connection
insert/remove/invalidate events.
queries are the lists of this Entity the subscriber reads live
(liveQuery(Data, …)), each a LiveQuery the handler has already
admitted and resolved: identity (the connection as the client keys it, for
the connection events), select (what the read draws of each row), and
edges, the registered query run as the subscriber with the page's window.
The handler refuses, with RemoteLiveRefused and before any live Source is
asked, a list of a query it does not register, an input that does not
decode, one with no select or no first, one whose select is of another
Entity than its query lists (a QuerySource names that Entity: RemoteServer.query
reads it from the descriptor's connection), and one whose first, added to
the ids of its Entity the subscription names, passes maxIdsPerEntity. A
subscription watches at most 32 lists (MAX_LIVE_QUERIES), refused at decode.
If an Entity has no live Source, it contributes no stream. That is not an error: the client planner can fall back to refetching it.
A live Source that reads rows to send them must read them as a read would, or
it sends what the read handlers withhold. RemoteServer.readAs is that read:
only the asked fields the Source declares and its authorize allows, each
record through its filter, and no other field.
const asReader = RemoteServer.readAs(ProjectSource, {
ids: ['p1'],
fields: ['name', 'privateNotes'],
principal: { isAdmin: false },
})
// An Effect that succeeds with Option.some([{ id: 'p1', values: { name: 'Apollo' } }]),
// or Option.none() when the principal may read none of the fields asked.RemoteServer.allowedFields(ProjectSource, principal, fields) says which of
fields that read would read, without reading: a Source reading several rows
together, each asked for fields of its own, reads them in one read and still
tells nothing of a row whose fields the principal may read none of.
A live Source that sends only what it reads through one Source names it as
reads, so a startup check can trust it as far as it trusts that Source:
GuardServer.validate accepts a live Source over a guarded Entity only when
it reads the guarded Source it checked. foldkit-remote-drizzle's pollLive
reads through readAs and sets reads.
A hub fans what changed out to live subscribers. Give it to the handlers, and every mutation's answer is published to it: each patch as a change of the fields it carries, each deletion as a deletion. A mutation Source names nothing twice.
// Building a hub is an Effect.
const program = Effect.gen(function* () {
const entitySources = [ProjectSource]
const hub = yield* RemoteServer.liveHub(entitySources)
const handlers = RemoteServer.handlers(Server, principal, {
live: hub,
})
// Only for a write that is not a mutation: a database trigger consumer, a job.
yield* hub.changed(Project.ref('p1'), ['name'])
yield* hub.deleted(Project.ref('p1'))
return handlers
})A mutation's publish runs after its Source returned, so after a write that
commits as it goes. A failed publish is reported as a PublishFailed
diagnostic and the mutation still answers: its write has
happened, and a failed answer would be retried.
The hub remembers each subscriber's requirements and principal. changed:
- intersects the changed fields with what each subscriber selected;
- re-reads only that intersection through the Entity Source;
- applies that subscriber's authorization again;
- emits the resulting patch with a continuing cursor, or a deletion where the re-read no longer finds the row: deleted, or moved out of that subscriber's sight.
deleted asks each subscriber again too, for every field it selects: a row
deleted for one writer may only be hidden from them, so a subscriber that
still reads it is sent what it now sees, and only one it is gone for a
deletion. One subscriber's failed read does not keep the others from theirs;
the call fails once they are sent.
Subscribers that selected none of the changed fields do no work, and
subscribers with one principal share one re-read. One principal means the same
value, unless the hub is given a key: then two requests whose principals
have one key, each with a principal object of its own, share it, read as the
first of them. A key must therefore name everything the Sources decide by,
the reader itself included when a Source answers a viewer-relative member (a
Derived.exists, whose value is each reader's own; foldkit-guard's
Policy.keyOf names every field of the principal). A key by role, as here,
fits Sources that answer only by role:
const keyed = Effect.gen(function* () {
return yield* RemoteServer.liveHub([ProjectSource], {
key: (user: Principal) => (user.isAdmin ? 'admin' : 'member'),
})
})A re-read per principal is how the hub stays exact when who is asking decides what a row shows. Where nothing does, a Source can say so, and the hub sends a written value as it is:
// Everyone reads a project's name alike; only an admin reads its private notes.
const SharedProjectSource = RemoteServer.entity<Principal>(Project, {
authorize: ProjectSource.authorize,
read: ProjectSource.read,
shared: ['name'],
})sharednames fields every principal reads alike, of every row: no rule about who is asking hides the row or the field, or changes its value. The server trusts it, so declare it only where that holds; a field the Source does not serve is refused when the Source is made.hub.changed(ref, fields, values)takes the row's stored values. Asharedfield among them goes to each subscriber that selected it with no read; the rest are re-read as above. A field a subscriber pages with a window is always re-read.- The handlers pass a declared write's answer (
RemoteServer.write), which is the row as stored. A handler's answer is its own claim, so its fields are re-read. foldkit-remote-drizzle'ssourceandRemoteServer.memorytake it too, andfoldkit-guard'sGuardServer.validaterefuses a shared field a Read rule could tell readers apart by.sourcerefuses aDerived.existsas shared: it is each reader's own.
A hub is an in-memory fan-out, so whatever calls changed must be able to wake
the subscriber's fiber. In a worker it cannot: one request cannot wake another's
stream, so a hub fed from a second request delivers nothing and tears the first
stream down: the open response belongs to the request that opened it, and the
Workers runtime cancels it when another request wakes it. On Workers, poll the
shared store from inside the subscriber's own request instead, as pollLive
does. A hub is right for a long-lived server process that owns both sides.
A hand-written live Source and a hub number their events independently. Use one or the other for a given subscription path rather than mixing cursor domains.
Keep authentication and authorization at separate boundaries:
- Authentication ("who is making this request?") runs in your Effect RPC
middleware or HTTP handler and resolves a
Principal. - Authorization ("which semantic fields may this principal read?") runs in
RemoteServer.entity(..., { authorize }).
RemoteServer.handlers(Server, principal) receives an already-resolved
principal rather than inspecting headers, sessions, cookies, or bearer tokens.
The principal is a value, or a Context key that each call reads, so one
handler set (one RPC layer) answers every request and authentication provides
the key per request:
import { Context } from 'effect'
class CurrentUser extends Context.Service<CurrentUser, Principal>()('app/CurrentUser') {}
const answering = RemoteServer.handlers(Server, CurrentUser)
// per request, once authentication has resolved `user`
RemoteServer.answer(answering, body).pipe(Effect.provideService(CurrentUser, user))Every handler then needs CurrentUser, which the types carry: a key holding
another type than the Server's principal is a type error.
For Entity reads:
- undeclared fields are dropped first;
authorizereceives only declared, requested fields;- omitting
authorizemeans the requested declared fields are readable; - a withheld field is answered as settled, without its reason;
- an id whose every requested field is withheld is not read from the Source: its fields are settled and no entity is contributed, so it looks the same as an id the Source does not know, and existence is not leaked.
Mutation and Query policy belongs in their Sources because those operations are application-specific rather than field-selection policy.
A complete server definition is just the collection of Sources:
const FullServer = RemoteServer.make({
entities: [ProjectSource],
queries: [ProjectsByOwnerSource],
mutations: [RenameProjectSource],
live: [ProjectLive],
})When the client and server share the Remote domain declaration, validate the Source names at startup:
const Data = Remote.define({
entities: [Project],
queries: [ProjectsByOwner],
mutations: [RenameProject],
})
RemoteServer.validate(Data, FullServer)That catches an Entity, Query, or Mutation Source whose name is not declared by the shared domain instead of letting it silently answer nothing at runtime.
viewer says who asks, as a row, and is the server's one answer to it:
RemoteServer.make({ ..., viewer: principal => Option<{ entity, id }> }). The
server hands it to every Source it mounts, with each read, query and live
subscription (EntitySourceContext.viewer), and to every write. A Source
answering a member per reader declares it (perViewer, as a Derived.exists
does), and a server with no viewer refuses that Source where it is made,
naming it. A query body's Expr.viewer and Expr.now read this viewer and the
server's Effect Clock (exprContext).
The viewer is also whom every answer says it is for. A read's, a list's and a
mutation's result carry principal: the viewer's key ('User:ada'), or
null for a visitor. The client treats it as opaque, stamps each durable
write it queues with it, and sends it back with every send of that write. The
handlers compare it with whom they answer as before the mutation's Source
runs: another person is refused with RemotePrincipalChanged (a 409 over
JSON), a visitor with Unauthenticated, and nothing runs, a ledger's replay
included, so a write resent after the cookie changed hands is never run, or
answered from its record, as someone else. A write that names null (made
with no one signed in) runs only for a visitor; asked by someone signed in it
is refused with RemotePrincipalChanged, unrun. A write that names no one
(made before any answer said who) runs as whoever asks.
A server with no viewer keeps no keys: its answers carry no principal, and
it refuses a write that names one, which it cannot check.
Then compile it into handlers, once per principal, or once for every request with a Context key as the principal (see Authentication vs authorization):
const bounded = RemoteServer.handlers(FullServer, principal, {
maxIdsPerEntity: 1000,
maxDepth: 8,
live: hub,
})handlers implements the RemoteRpc client shape directly. In-process — tests,
SSR, a worker, or a single service — it can become a RemoteClient with:
const clientLayer = Remote.clientLayer(handlers)Across a process boundary, use:
const rpcLayer = RemoteRpc.toLayer(handlers)The application supplies the Effect RPC server/transport layer. RemoteServer
does not choose HTTP, WebSocket, or another transport for you.
Sources are Effects, so dependencies stay explicit: a Project Source can
require Database and a Billing Query Source can require BillingApi, making
the composed RemoteServer require Database | BillingApi.
The server definition leaves both services uncaptured; provide their Layers at
the application boundary. This is also why foldkit-remote-drizzle provides
Drizzle-backed Sources rather than putting a database inside RemoteServer.
The handler applies bounded, protocol-level safeguards before or around Sources:
- a read batch may name at most
maxIdsPerEntitydistinct ids for one Entity (default1000); oversized client batches fail rather than loading unbounded rows. Remote's client cuts its batches to the same default, and to the number given inRemote.clientLayer(…, { maxIdsPerEntity }); - a request body may be at most
maxBodyBytes(default 1 MiB) where bytes are read:serveFetch's option, andbodyLimitin front of anRpcServerroute. A larger one is a 413,{ error, tooLarge: { limit } }, refused by itscontent-lengthbefore anything else, or cancelled the moment the bytes read pass the limit, and never decoded; - nested resolution is bounded by
maxDepth(default8); - the wire decoder also rejects a selection deeper than
MAX_RELATION_DEPTH(8) or more thanMAX_FIELDS_PER_REQUEST(256) fields for one Entity; - a collection relation read with no window is the Source's to bound:
foldkit-remote-drizzle'ssourcereads at mostmaxCollectionchildren of one row (default 100) and refuses a read that finds more, rather than answering a list cut short; - returned field names are copied with
Object.hasOwninto null-prototype objects, so names such as__proto__ortoStringcannot reach prototypes; - a Source's failure, a thrown error and a defect reach the caller as
Internal errorand an incident id, never in their own words, unless the Source wrote them for the caller withRemoteServer.tell(see When the server fails).
A live subscription is held to the same bounds, checked once by the handler
before any live Source or the hub sees it: more than maxIdsPerEntity ids of
one Entity (the first of each live list of it counted with them), or more
pages of one relation than a read may ask, fails the stream with
RemoteLiveRefused and nothing is read; more than MAX_LIVE_QUERIES (32) live
lists fails the wire decoder before that. That matters most for a polling
Source such as pollLive, which re-reads every subscribed row each tick. A
refusal is not truncation: the client does not reconnect it, and
liveStatus(Data, …) reads Refused. A hand-written live Source can rely on what
it is given having passed, and is responsible for any further bounds its
backend needs.
RemoteServer.memory serves rows you give it through the same handlers a
real server uses, so a client sees what production would send: only the
fields asked for, and nested relations resolved in one read. Use it for a
first run, a test, a story or a demo, when there is no database yet.
A body calling a primitive operator runs by the operators it is given
(Operators.kernel by default), and one calling an operator that table lacks
is refused where the server is built.
const backend = RemoteServer.memory({
domain: Data, // the definition from Remote.define, or a domain from Remote.make
rows: {
Project: [{ id: 'p1', name: 'Apollo', privateNotes: 'Internal' }],
},
// Optional: each mutation writes through the store, and the next read sees it.
mutations: store => [
RemoteServer.mutation(RenameProject, ({ input }) => {
store.write('Project', input.id, { name: input.name })
return Effect.succeed({
output: { id: input.id },
entities: [Remote.patch(Project, input.id, { name: input.name })],
})
}),
],
})
backend.layer // provide it where the real RemoteClient would gobackend.server is the server definition the layer answers through. Serve
it over a real transport to try a client against HTTP, or to test another
server against it:
const backendLayer = RemoteRpc.toLayer(RemoteServer.handlers(backend.server, undefined))- Rows are in their wire shape: a relation is its ref key (
'User:u1'), and a to-many relation is a list of them. - A query declared with
Query.defineis answered by running its body over the rows withevaluate, the reference interpreter, and paged with offset cursors. A query with no body has nothing to run and is refused when the backend is made, unless you pass a source for it inqueries. - Every field is readable unless
authorizenames the entity; see below. It pushes no live changes, and it is not a server to deploy.
To try authorization without a database, give authorize per entity name,
the function RemoteServer.entity takes, and the principal the layer
answers as. The principal's type is inferred from them, as RemoteServer.make
infers it from its sources:
const guarded = RemoteServer.memory({
domain: Data,
rows: { Project: [{ id: 'p1', name: 'Apollo', privateNotes: 'Internal' }] },
principal: { isAdmin: false },
authorize: {
Project: (principal: Principal, fields) =>
fields.filter(field => field !== 'privateNotes' || principal.isAdmin),
},
})
// The layer reads as a non-admin; the server answers as anyone.
const asAdmin = RemoteServer.handlers(guarded.server, { isAdmin: true })A withheld field is settled, as from any server: the client stops asking for
it, and a selection that needs it fails as Unavailable. A name in
authorize the domain does not declare is refused when the backend is made.
filter takes the same per-entity form. readable decides which rows a
principal may read, once per read, given each row's stored values; its second
argument looks another stored row up by id, for a rule that follows a
relation:
// Only an admin reads projects.
const adminsOnly = RemoteServer.memory({
domain: Data,
rows: { Project: [{ id: 'p1', name: 'Apollo', privateNotes: 'Internal' }] },
principal: { isAdmin: false },
readable: (principal: Principal) =>
Effect.succeed(entity => principal.isAdmin || entity !== 'Project'),
})A row it may not read is absent from a read by id (the client reads it as
not found) and from every query, and a ref to it is dropped from a relation:
a list leaves it out and a one ref reads null, as a database binding's
visible hides it.
An entity no row backs, answered from who is asking (foldkit-guard's
Me, say), is a Source of its own beside the rows, in entities. Leave it
out of the rows' domain: a Source for an entity the domain declares is
refused when the backend is made, as two Sources for one entity are.
const Whoami = Entity.make('Whoami', Schema.Struct({ id: Schema.String, admin: Schema.Boolean }))
const WhoamiSource = RemoteServer.entity<Principal>(Whoami, {
read: ({ ids, principal }) =>
Effect.succeed(ids.map(id => ({ id, values: { admin: principal.isAdmin } }))),
})
// No principal: served through `serveFetch` or `handlers`, as each request's.
const { server } = RemoteServer.memory({
domain: Data, // Project only; Whoami is the Source's
rows: { Project: [{ id: 'p1', name: 'Apollo', privateNotes: 'Internal' }] },
entities: [WhoamiSource],
})
const asVisitor = RemoteServer.handlers(server, { isAdmin: false })principal is only who layer answers as. Without it (and a principal type
that does not admit undefined), the backend has no layer, only its store
and server, so nothing can read the rows as nobody.
A declared write needs no handler over the
rows either. RemoteServer.memoryWriter(store, entity, { operators? }) is the
EntityWriter for one Entity's rows in a backend's store, and it decides what
foldkit-remote-drizzle's writer decides in its statement, so a write lands
here as it would over a database:
const Reserve = Mutation.write(
'Reserve',
Write.update(
Entity.input(
Stock,
Schema.Struct({ id: Schema.String, revision: Schema.Int, quantity: Schema.Int }),
{ quantity: Entity.unmapped },
),
{
id: 'id',
expect: 'revision',
when: () => Expr.eq(Stock.fields.status, 'published'),
increment: { reserved: 'quantity' },
},
),
)
const shop = RemoteServer.memory({
domain: Remote.define({ entities: [Stock], mutations: [Reserve] }),
rows: { Stock: [{ id: 's1', status: 'published', quantity: 10, reserved: 5, revision: 1 }] },
mutations: store => [RemoteServer.write(Reserve, RemoteServer.memoryWriter(store, Stock))],
})An update lands only while the row is at the revision it expects, holds its
when, and the row it would leave keeps every invariant; amounts and computed
fields read the row as it was before the write, and the revision moves on by
one. An insert lands while its id is free and its row keeps the invariants; a
revision and holds its when. Each refusal reaches the client as the
mutation's own: Conflict, Unmet, Invalid naming the invariant, Taken.
operators runs the when, computed fields and invariants, as a memory
backend's operators runs its query bodies.
The rows are one process's, read and written in one step, so a write cannot
interleave with another. What a database writer holds beyond that it does
not: no ledger of operations, no rule that reads another row, nothing a
delete takes, keeps or clears with it. RemoteServer.write refuses, by name,
a write that needs one (an increment with no expect needs a ledger), and so
does GuardServer.writer for a rule reading another row, where each is
served, rather than landing what the database would refuse.
servePort(handlers, port) from foldkit-remote-server/port answers
foldkit-remote/port's client over one MessagePort, for a server in a
worker or in the page. It speaks Effect's RPC worker protocol, so reads,
queries, mutations and live streams cross it with their typed errors, and an
interrupted call interrupts its handler. It returns once the client closes its
end; interrupt it to stop sooner:
import { servePort } from 'foldkit-remote-server/port'
// In the host, for each port a page hands it; `handlers` as above.
Effect.runFork(servePort(handlers, port))It binds no principal: pass the handlers for the conversation's own, as
everywhere else. It needs no browser package: the port is driven through
effect/workers directly.
RemoteServer.answer(handlers, body) answers one request as Remote.http
and Remote.json send it, whatever received it: an HTTP route, a worker, a
server running in the page. It decodes the request and its payload by the
protocol's own schemas before a handler sees them, so an operation is one of
Remote's three and nothing else, and says what to send:
// A fetch-style route (Bun, Deno, a Worker; Node through its own adapter).
const route = async (request: Request): Promise<Response> => {
const body: unknown = await request.json().catch(() => null)
const answered = await Effect.runPromise(RemoteServer.answer(handlers, body))
return Response.json(answered.body, { status: answered.status })
}- 200 with
{ result }: the handler answered. - 400 with
{ error }: the body is not a request, names an operation Remote does not have, or its payload fails the protocol's schema. No handler ran. - 401 with
{ error, unauthenticated: { reason? } }: a handler could not tell who is asking (RemoteServer.unauthenticated). The client fails the call, of whatever kind, with_tag: 'Unauthenticated'. - 403 with
{ error, forbidden: { reason? } }: who is asking may not do this (RemoteServer.forbid). In both,erroris thereason, orUnauthenticatedorForbiddenwhen the refusal gave none: the refusal's ownmessageis not sent, here or in a live stream's error frame. - 409 with
{ error, refusal }: a declared insert's id is already a row's, refused asRefusal.taken. - 409 with
{ error, principalChanged: {} }: the write names someone other than who asks (RemotePrincipalChanged). Nothing ran. The client fails it with_tag: 'PrincipalChanged', and the outbox keeps it waiting for them. - 422 with
{ error, refusal }: the mutation refused what was asked, in its ownRefusal. - 500 with
{ error, incident }: something on this side broke (a Source's failure, a thrown error, a defect). The message isInternal error, nothing more, andincidentis the id the cause was reported under, as aServerDefectdiagnostic, by default to the log, so a Worker's log (wrangler tail) shows what broke. - 500 with
{ error }and noincident: the server's own fixed sentence about the request (an unknown query or mutation, an input its Schema refuses, too many ids), or words a Source wrote for the caller withRemoteServer.tell. See When the server fails.
It never answers 408, 429, 502, 503 or 504. Remote's client reads a body
under one of those, or one that is not JSON, as no answer at all
(Unreachable): a proxy, gateway or rate limiter in front that answers one
leaves the client unsure whether the write ran, and a durable write is sent
again (see A write sent again).
It binds no principal: pass the handlers for the request's own,
established by your authentication, as everywhere else. It carries no live
data.
For an endpoint that serves RemoteRpc through Effect's own RpcServer,
bodyLimit(maxBytes) is the same limit as HTTP middleware (Effect's
MaxBodySize fails the RPC body read as a defect, a 500):
import { Layer } from 'effect'
import { HttpRouter } from 'effect/http'
import { RpcSerialization, RpcServer } from 'effect/rpc'
import { RemoteRpc } from 'foldkit-remote'
import { RemoteServer, bodyLimit } from 'foldkit-remote-server'
// `Server` is your RemoteServer.make; answered as a visitor here.
const RpcRoutes = RpcServer.layer(RemoteRpc).pipe(
Layer.provide(RemoteRpc.toLayer(RemoteServer.handlers(Server, { isAdmin: false }))),
Layer.provide(RpcServer.layerProtocolHttp({ path: '/rpc' })),
Layer.provide(RpcSerialization.layerJson),
Layer.provide(HttpRouter.middleware(bodyLimit(64 * 1024)).layer),
)The client reads that 413 as the call's own error, and a 401 or 403 from
session middleware in front of the route as Unauthenticated or Forbidden,
only over Remote.rpcProtocolHttp(url) in place of
RpcClient.layerProtocolHttp({ url }), which reads the refusal as RPC frames
and loses its status: a durable write would then read as unreachable and be
sent again for good.
Decide who is asking from the HTTP request, in front of the route, never
from the headers an RpcMiddleware or a handler is given. RpcServer
puts the HTTP request's headers before those the request body carries and
keeps the last of each name, so a body can claim another cookie, origin
or CSRF header; over a WebSocket, every frame can.
serveFetch from foldkit-remote-server/fetch is that route as a value: a
(request: Request, env: Env) => Promise<Response> for a Worker's fetch,
or any other runtime that speaks Request/Response. Each request is one
Effect: layer is built from the environment, then resolvePrincipal says
who is asking, then the request is answered as them. The resolver may need
what layer provides (a session store, an auth service):
import { serveFetch } from 'foldkit-remote-server/fetch'
// `server` is your RemoteServer.make; `databaseLayer` and `drizzle` provide what its Sources need.
export default {
fetch: serveFetch({
server,
resolvePrincipal: request =>
Effect.succeed({ isAdmin: request.headers.get('x-role') === 'admin' }),
layer: (env: Env) => databaseLayer(drizzle(env.DB)),
}),
}Only POSTs to path (default /remote) are answered; anything else is a
404. A body larger than maxBodyBytes (default 1 MiB) is a 413 with
{ error, tooLarge: { limit } }: one whose content-length says so before
the layer is built or the principal resolved, any other once the bytes read
pass the limit, its stream cancelled there. Give the client the same number
(Remote.clientLayer(Remote.http('/remote'), { maxBodyBytes })) and it cuts its
reads to fit; Remote.http reads a 413 as the call's own error, so a durable
write is not resent. A body that is not UTF-8 JSON, or not a request, is a 400
that reaches no handler; a failure on this side a 500 that says Internal error and the
incident its cause was reported under (When the server fails). Every response, refusals
and errors included, says cache-control: private, no-store unless headers gives a
cache-control of its own: each answer is one principal's rows (and may), so
no shared cache may keep one. The one exception is a read the server proves
public, with cache (below).
A resolver refuses the caller by failing with
RemoteServer.unauthenticated(reason?): a 401 whose body is
{ error, unauthenticated: { reason? } }, as answer's 403 carries
forbidden, and whose reason is the only thing the caller learns. Anything
else this side breaks, the resolver's defects and the layer included, is a
500 that says nothing of it but its incident. A visitor is not a refusal: resolve one to the
principal your Sources treat as nobody, and refuse only a credential that was
sent and no longer holds.
headers: principal => ({ … }) adds headers to every answer to that
principal, a live stream's included; a refusal answered no one and carries
none. foldkit-auth's AuthServer.answered uses it to say whom each answer
was for.
revalidate: { every, key? } asks resolvePrincipal again, of the same
request, every every while a live stream is open. A refusal ends the
stream with Unauthenticated; a principal whose key differs from the one
it opened with ends it as a live error, which the client opens again after,
as the new principal. key is who a principal is, as liveHub's key:
with guards, key: Policy.keyOf. Without revalidate a stream serves the
principal it opened with, a revoked session included, until it closes.
A body that opens live ({ operation: 'live', payload }) is answered with
the stream as text/event-stream: one data: frame per LiveChange, then
a terminal event: error frame carrying the failure's message when the
stream fails rather than ends. A payload no live stream speaks is a 400 that
subscribes to nothing. httpWithLive (foldkit-remote/live) is the other end; a client that
disconnects interrupts the subscription, as closing the RPC stream would.
The stream moves on its own even when nothing changes: a : ping comment opens
it, and another follows every liveHeartbeat (default 15 seconds), so proxies
and worker runtimes do not drop an idle connection. The heartbeat stops with
the events it serves, so a stream with nothing to serve still ends.
live takes a hub whose changed/deleted signals reach these streams. What
calls it must be able to wake the subscriber's fiber: in a worker, one request
cannot wake another's stream, so a hub fed from a second request delivers
nothing and ends the first stream. Poll the shared store from inside the
subscriber's own request instead: foldkit-remote-drizzle's pollLive does,
with a trigger-kept row log (rowChangeLog, rowLog) so a quiet tick reads
no rows. A hub is right for a long-lived process that owns both sides.
A Worker that renders pages reads what each page shows before rendering it
(Data.satisfy), as the reader who asked. pageReader is that reader, made
from the same config serveFetch takes: the request's RemoteClient as a
Layer, answered by the server's own handlers with no HTTP in between. The
server, its layer and its limits are the ones /remote answers with.
import { pageReader, serveFetch } from 'foldkit-remote-server/fetch'
const remote = { server, resolvePrincipal, layer: (env: Env) => databaseLayer(drizzle(env.DB)) }
const served = serveFetch(remote)
const reader = pageReader({ ...remote, resolvePrincipal: resolvePage })
export default {
fetch: (request: Request, env: Env) =>
new URL(request.url).pathname === '/remote'
? served(request, env)
: pages.fetch(PageContext.attach(request, { reader: reader(request, env) })),
}pages is the fetch handler Foldkit's build emits, and PageContext an
SSR.requestContext that carries the reader to renderPage
(foldkit-ssr).
The resolver is the page's own: a page GET is a navigation, which carries
no header a script adds, so a resolver that asks for one (Yielded's CSRF
header) refuses it. With foldkit-auth, give AuthServer.host's
navigation. pageReader hands its resolver a NavigationRequest and
serveFetch a ScriptedRequest (types from foldkit-remote, nothing at run
time): a resolver typed for one does not fit the other, while one taking any
Request, as here, fits both. When the reader is
provided, the principal is resolved and layer built, and layer closes
when that Effect ends, so provide it to the pre-read itself
(Data.satisfy(…).pipe(Effect.provide(reader))). A refusal fails it with
Unauthenticated, as a 401 would.
The reader only reads. A mutation fails with RemoteMutationError and a live
open with RemoteLiveError: another site can start a navigation, so the
principal it resolves must never write, and a render keeps no stream.
With cache, the reader answers each read it proves public as the visitor,
and notes every read for the render (PageReads): public with its
freshness and the rows it holds, or private, named by the rows or query it
asked for (Price a, Price b, query Search). foldkit-ssr's SSR.entry
keeps a page only when every read it made was public, and refuses a
prerendered page naming the first read that was not.
A read every visitor gets the same answer to costs the same work every
time it is asked. With cache, serveFetch keeps public reads in a store and
answers them without asking a Source:
- Public read (no per-caller rule and freshness declared): answered as the
visitor and kept in the store for
maxAge. - Private read: answered as the authenticated caller with
Cache-Control: private, no-store. - Write: purges every kept read holding a row it names and every list of that row's entity.
The domain declares how long a read stays fresh; it never says who may
share it. An Entity says it with EdgeCache.of, a Query with cache:
import { Effect, Layer, Schema } from 'effect'
import { Entity } from 'foldkit-entity'
import { CacheStore, EdgeCache, Query, REMOTE_PROTOCOL_VERSION } from 'foldkit-remote'
import { RemoteServer } from 'foldkit-remote-server'
import { checkCache, serveFetch } from 'foldkit-remote-server/fetch'
const Price = Entity.define(
'Price',
Schema.Struct({ id: Schema.String, amount: Schema.Number }),
).pipe(Entity.annotate(EdgeCache.of({ maxAge: '60 seconds' })))
const Cheap = Query.make('Cheap', { Input: {}, Result: Price, cache: { maxAge: '30 seconds' } })
const server = RemoteServer.make({
entities: [
RemoteServer.entity(Price, {
read: ({ ids }) => Effect.succeed(ids.map(id => ({ id, values: { id, amount: 12 } }))),
}),
],
})
const config = {
server,
resolvePrincipal: (request: Request) =>
Effect.succeed({ userId: request.headers.get('x-user') ?? undefined }),
layer: () => Layer.empty,
}
export const fetch = serveFetch({
...config,
cache: { store: CacheStore.memory(), visitor: { userId: undefined } },
})Whether a read is public is the server's to prove, from the Sources it
passes through: a read or query is public only when no Source on its way
(its Entity's, each relation's it reaches, a query's own and its select's)
has an authorize, a filter, a visible rule or a member answered per
viewer (Derived.exists), and each declares a freshness. Those rules are
functions of the principal the server cannot read, so a Source with one is
private even where its rule happens to read only the row: a guarded Entity
(foldkit-guard), a Drizzle binding with visible, a memory backend with
readable. Nothing is cached that the server could not prove.
A public read is answered as visitor, whoever asks: one answer, right for
everyone, and never someone's own, even from a Source whose read looks at
the principal. visitor must be no one: serveFetch refuses one the
server's viewer names a row for. The answer names nobody it was for (no
principal), says cache-control: public, max-age=… for the shortest
freshness it reaches, and is kept by the SHA-256 of its body under
/__foldkit/cache/remote/ on the request's origin. A refusal or a failure
is not kept. Everything else (a write, a live stream, a read that is not
public) says private, no-store.
A write answered here purges what its answer names: each row it wrote,
deleted, moved in a list or read back (shown, carried lists), and every
list of each of their entities, which a changed row can join or leave. A
write that is not answered by this serveFetch (another server, a job, a
database trigger) purges nothing: what it changed is served as it was kept
until maxAge.
CacheStore.memory({ limit }) keeps answers in the process, for Node, a dev
server and tests (1,000 by default, the oldest dropped first).
CacheStore.workers(caches.default) keeps them in the Workers Cache API;
which answers a tag names is known to the isolate that kept them, so a
write purges what this isolate kept, and another isolate's or data centre's
copy stays until its maxAge. Give pageReader the same config, and pages
read through it are judged the same way (foldkit-ssr's cache).
CacheStore.kv(env.CACHE) keeps them in a Workers KV namespace: one copy for
every isolate and data centre. KV's free plan allows 1,000 writes a day, so
an answer is written only when it is asked for a second time; this isolate's
memory counts the asking, since counting in KV would spend the write the
count saves. What KV refuses or fails never fails a request: a failed read
is a miss, a refused write (the day's writes spent, a key over 512 bytes)
keeps nothing, and each is logged as a warning. KV keeps an entry a minute
at least; the store serves it for its maxAge alone. Its purge, like the
Workers store's, reaches what this isolate kept. Its keep option is for
pages (foldkit-ssr): with it, kv returns a PageCacheStore, which
serveFetch's cache.store refuses as a type error, since a read's answer
carries no age and one served past its maxAge would read as fresh.
The checker answers each public read as two principals. cache.check
answers it as who asks too, and where the two differ reports
CachedReadDiffers and gives the asker their own: a development mode.
checkCache(config, requests, env) is the same, for a test: it serves each
request twice with check and a store in memory, and rejects a difference,
or a set of requests with no public read, which would check nothing:
const asking = (user?: string) => () =>
new Request('https://shop.test/remote', {
method: 'POST',
headers: user === undefined ? {} : { 'x-user': user },
body: JSON.stringify({
operation: 'read',
payload: {
version: REMOTE_PROTOCOL_VERSION,
requests: [{ entity: 'Price', id: 'a', fields: ['amount'] }],
},
}),
})
export const checked = checkCache(
{ ...config, cache: { visitor: { userId: undefined } } },
[asking(), asking('u1')],
undefined,
)Cheap is a Query a server registers with RemoteServer.query; its answer
is kept for 30 seconds, the shorter of its own and Price's.
A call that does not succeed tells its caller one of two kinds of thing, and which is decided by the type of the error where it is made, never by reading its message:
| What happened | What the caller reads |
|---|---|
Application refusal (refuse, forbid, unauthenticated, unmet, taken, invalid) |
the refusal's message and reason (or Forbidden / Unauthenticated) |
RemoteServer.tell(message) |
message, as written |
| Bad request (unknown query/mutation, Schema mismatch, too many IDs, wrong protocol) | a fixed sentence naming only what the request sent |
Unclassified failure or defect in a Source, authorize, filter, or operator |
Internal error, and an incident |
So a failure nobody classified is this side's: its message and cause are
reported as a ServerDefect diagnostic and never sent. A
Source says which it means:
import { Effect, Schema } from 'effect'
import { Entity } from 'foldkit-remote'
import { RemoteServer, RemoteServerError } from 'foldkit-remote-server'
const Report = Entity.make('Report', Schema.Struct({ id: Schema.String, total: Schema.Number }))
declare const building: (id: string) => boolean
declare const totals: (
ids: ReadonlyArray<string>,
) => Promise<ReadonlyArray<{ readonly id: string; readonly total: number }>>
const ReportSource = RemoteServer.entity(Report, {
read: ({ ids }) =>
ids.some(building)
? // Words for whoever asked: sent as written.
Effect.fail(RemoteServer.tell('That report is still being built'))
: Effect.tryPromise({
try: () => totals(ids),
// What the driver said stays on this side: reported, never sent.
catch: cause => new RemoteServerError({ message: 'The totals query failed', cause }),
}).pipe(Effect.map(rows => rows.map(row => ({ id: row.id, values: row })))),
})RemoteServer.tell takes text, not what was caught: an error's own message
may say anything, so tell(caught) does not compile. Write what it says from
what the caller may know.
The incident is 16 hex characters, made for each failure. The caller's
error carries it ({ error: 'Internal error', incident } over JSON, the
incident field of RemoteReadError, RemoteQueryError,
RemoteMutationError and RemoteLiveError over RPC, and of a live stream's
error frame), and the client reads it with RemoteData.incident(error). The
report carries the same id, what was asked (during), and the cause (its
stack lines left out here):
[foldkit-remote-server] incident 3f9c2a7be01d4c55: read Report failed on this side. No caller is told more of it than this id.
RemoteServerError: The totals query failed
[cause]: Error: SQLITE_BUSY: database is locked
What this does not cover: a refusal's message, a forbid reason, an
invariant's message and tell's text are sent as written, so nothing a
caller may not know belongs in them; and a 400's error is the protocol
Schema's own account of the request's payload. There is no switch that puts
the cause in a response: read the report.
A failure outside a handler has no caller's error to carry an incident: a job
a cron trigger runs, or an adapter answering on a transport of its own.
RemoteServer.report(during, cause) reports it as the server reports its own,
a ServerDefect under a new incident, and answers the only thing that may go
further: { message: 'Internal error', incident }.
import { Effect } from 'effect'
import { RemoteServer } from 'foldkit-remote-server'
declare const publishDue: Effect.Effect<void, unknown>
declare const remember: (shown: string) => Effect.Effect<void>
export const job = publishDue.pipe(
Effect.catchCause(cause =>
Effect.flatMap(RemoteServer.report('publish due', cause), ({ message, incident }) =>
// What a reader of the job's outcome may see: the words and the id.
remember(`${message} (incident ${incident})`),
),
),
)It takes what was caught, or a Cause, and returns nothing of either. It
reports through the Diagnostics.Reporter of the Effect it runs in.
A server answers some failures without telling the caller why. Each is a
typed diagnostic, reported through Diagnostics.Reporter (foldkit-surface's
channel, re-exported by foldkit-remote), which by default is Effect's log:
_tag |
level |
When |
|---|---|---|
ServerDefect |
error |
Unclassified Source failure, thrown error, broken layer, or handler defect |
AnswerUnread |
warning |
Mutation committed, but reading its answer failed; client re-reads |
PublishFailed |
error |
Mutation committed, but its live publish failed; subscribers miss it |
ClockSkewed |
warning |
Client write timestamp exceeded clockSkew; server used its own clock |
ServerDefect carries the cause, the incident returned to the caller, and
during (read Note, query Notes, mutate SaveNote, live Note, or
request). ClockSkewed carries sent, own, and bound.
Provide another reporter as any service: Layer.succeed(Diagnostics.Reporter, report) in the layer the handlers run in. serveFetch takes one with the
request beside it, used for everything that request runs, a broken layer
included:
import { serveFetch } from 'foldkit-remote-server/fetch'
serveFetch({
server,
resolvePrincipal,
layer: (env: Env) => databaseLayer(env),
diagnostics: (diagnostic, request) =>
Effect.logError(`${request.method} ${request.url}: ${diagnostic.message}`),
})In a test, Diagnostics.collect() gives a reporter and the array it fills.
A descriptor declared with
Query.define carries its meaning as a
value (body), and evaluate runs a body directly:
import { Schema } from 'effect'
import { Entity, Expr, Order, Query, evaluate } from 'foldkit-entity'
const Post = Entity.define('Post', Schema.Struct({ id: Schema.String, slug: Schema.String }))
const intro = Query.from(Post).pipe(
Query.where(Expr.eq(Post.fields.slug, 'intro')),
Query.orderBy(Order.asc(Post.fields.id)),
)
evaluate(intro, {}, [{ id: 'p1', slug: 'intro' }, { id: 'p2', slug: 'outro' }])
// [{ id: 'p1', slug: 'intro' }]: the matching rows, in the order the body asks forIt lives in foldkit-entity, beside the IR it interprets, because
it is that IR's reference semantics rather than anything this package does —
and because a client judging rows it already holds needs it without depending on
a server package. This package re-exports it, so importing it from either place
means the same thing.
It is pure — it reads the rows it is given and nothing else — and it is the
reference the compiled interpreters are checked against. foldkit-remote-drizzle
turns the same body into SQL; the test suite runs both over the same rows in a
real SQLite and Postgres and requires the same ids in the same order. That is
what makes a body source-neutral in fact rather than in principle.
It follows SQL, not JavaScript. The case that matters is null: null = null
is unknown in SQL and matches no row, where JavaScript would call the two
equal. A comparison keeps a row only when it is true, so a null on either side
drops it. Where an ordering puts null is the term's to say
(Order.asc(field, { nulls: 'first' })); unsaid, it is Postgres's (last
ascending, first descending), which the SQL compiler writes out for SQLite too.
What it refuses rather than guesses, each where the query runs:
- Ordering by values it has no order for, such as a number in one row and text in another, or by a backend's locale collation, which only that backend has. Every term is checked over every matched row before anything is sorted, so the refusal is the same on every engine, whichever pair a sort would have compared first. One matched row, or none, has nothing to order.
- NaN, which SQLite stores as null and Postgres orders after every number.
- Text holding a NUL character in a comparison or a search, which Postgres
text cannot hold and SQLite's
likestops at. - A term with no context. A body reading
Expr.viewerorExpr.nowis refused when run without them (ContextMissingError), never read as null.
evaluate is the reference, and cases (in foldkit-entity/conformance) is
what it is a reference to: the conformance suite, exported so an interpreter
written elsewhere can be held to the same semantics. Here run is your
interpreter:
import { expect } from 'vitest'
import { cases, rows } from 'foldkit-entity/conformance'
for (const { what, body, input, expected } of cases) {
expect(run(body, input, rows).map(row => row.id), what).toEqual(expected)
}Every case is chosen to make interpreters disagree, which is the only kind
that tests anything: mixed case, nulls on both sides of a comparison, % and
_ as literal text, the empty search, a predicate compared to a boolean either
way round. That is not caution for its own sake — contains reached a released
package meaning three different things because every value in the original
fixture happened to be lowercase.
Both interpreters here run it: this package over rows in memory, and
foldkit-remote-drizzle compiled to SQL against a real SQLite and Postgres.
- Server-derived state — the client/server mental model and ownership rules.
- Errors and failures — what a client is and is not told, beside the failures every other package owns.
foldkit-remote— requirements, normalized client Model, subscriptions, mutations, live reads, and retention.foldkit-remote-drizzle— compile Entity selections and Query windows into Drizzle reads.examples/kitchen-sink— Remote + RemoteServer + Drizzle +liveHubin one executable trace.examples/cloudflare— Remote + Sync + remote-drizzle on D1 in a real worker, with a polling live Source.