foldkit-remote-server 0.15.1 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -3
- package/dist/fetch.d.mts +0 -2
- package/dist/fetch.d.mts.map +1 -1
- package/dist/fetch.mjs +2 -3
- package/dist/index.d.mts +127 -17
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/port.d.mts +0 -2
- package/dist/port.d.mts.map +1 -1
- package/dist/port.mjs +1 -2
- package/dist/{src-DKYZjLc7.mjs → src-ErnDxcWe.mjs} +90 -19
- package/dist/src-ErnDxcWe.mjs.map +1 -0
- package/package.json +4 -4
- package/dist/src-DKYZjLc7.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -444,8 +444,12 @@ A refusal of who is asking, rather than of what they asked, is
|
|
|
444
444
|
`Forbidden` with the reason (a 403 over JSON), or with none when `forbid()` is
|
|
445
445
|
given none, so the refusal says nothing of why. It is the same for every
|
|
446
446
|
mutation, so it needs no `Refusal` declared. The Source is also given `requestId`, the client's id for the
|
|
447
|
-
request,
|
|
448
|
-
retry
|
|
447
|
+
request, and `operation`, the operation's id across clients, both the same on
|
|
448
|
+
every retry of it. Record a write under `operation`, never `requestId`: a
|
|
449
|
+
request id is a counter in one client's Model, so two clients' first requests
|
|
450
|
+
share it, and a record keyed by it would answer one client with another's write.
|
|
451
|
+
`operation` is none from a client that sends none, and bounded on the wire (128
|
|
452
|
+
characters of `A-Z a-z 0-9 : . _ -`). A Source
|
|
449
453
|
may additionally return connection changes:
|
|
450
454
|
|
|
451
455
|
```ts
|
|
@@ -468,7 +472,29 @@ RemoteServer.write(EditProject, writer(Db.Project)) // `writer` from foldkit-rem
|
|
|
468
472
|
|
|
469
473
|
The answer is the mutation's patch, or for a delete its deletion. With
|
|
470
474
|
`expect`, the writer is told the revision the row was read at; no row at it is
|
|
471
|
-
refused as `Refusal.conflict`.
|
|
475
|
+
refused as `Refusal.conflict`. With `when`, the writer is handed the
|
|
476
|
+
precondition and the input encoded, checks it in the statement that writes, and
|
|
477
|
+
fails a row at the revision that does not hold it with `RemoteUnmet`
|
|
478
|
+
(`RemoteServer.unmet()`), which is refused as `Refusal.unmet`: absent first, then
|
|
479
|
+
a moved revision, then the precondition. A row the write would leave that
|
|
480
|
+
breaks an invariant of its Entity (`Entity.invariant`) is the writer's to
|
|
481
|
+
refuse, with `RemoteServer.invalid(message, rule, path)`, which is refused as
|
|
482
|
+
`Refusal.invalid`: the invariant's id and path, or none and empty when the
|
|
483
|
+
caller may not be told them. A writer says it judges invariants
|
|
484
|
+
(`invariants: true`), and a write over an Entity that has some, through a writer
|
|
485
|
+
that does not, is refused where it is served. A writer says it checks one
|
|
486
|
+
(`preconditions: true` on `EntityWriter`), and a write with a `when` over a
|
|
487
|
+
writer that does not is refused where it is served, rather than written whatever
|
|
488
|
+
the row holds. A write's increments reach the writer apart from its values
|
|
489
|
+
(`increments`, each field's amount, encoded), to be added in the statement that
|
|
490
|
+
writes; a writer says it adds them (`increments: true`), and a write with one
|
|
491
|
+
over a writer that does not is refused where it is served. An increment that
|
|
492
|
+
expects no revision is served only over a writer with a ledger (`ledger: true`),
|
|
493
|
+
which records each update under its `operation` and answers a retry from the
|
|
494
|
+
record (`recorded`), and only for a request carrying an operation id. A writer
|
|
495
|
+
may refuse a write for what only its store knows (`refuses`, asked once where
|
|
496
|
+
the write is served): the Drizzle writer refuses an amount that may be a
|
|
497
|
+
fraction for a column that holds integers. An update of no row fails; an insert of an id
|
|
472
498
|
already holding what it writes answers that row, and a delete of a row already
|
|
473
499
|
gone succeeds, so a retry of either is safe. An insert of an id holding
|
|
474
500
|
anything else fails: that row is not its answer. The writer must be for the write's own Entity,
|
|
@@ -751,6 +777,9 @@ Source is responsible for any additional bounds appropriate to its backend.
|
|
|
751
777
|
real server uses, so a client sees what production would send: only the
|
|
752
778
|
fields asked for, and nested relations resolved in one read. Use it for a
|
|
753
779
|
first run, a test, a story or a demo, when there is no database yet.
|
|
780
|
+
A body calling a primitive operator runs by the `operators` it is given
|
|
781
|
+
(`Operators.kernel` by default), and one calling an operator that table lacks
|
|
782
|
+
is refused where the server is built.
|
|
754
783
|
|
|
755
784
|
```ts
|
|
756
785
|
const backend = RemoteServer.memory({
|
package/dist/fetch.d.mts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { LiveHub, ServerDefinition } from "./index.mjs";
|
|
2
2
|
import { Duration, Effect, Layer } from "effect";
|
|
3
3
|
import { RemoteUnauthenticated } from "foldkit-remote";
|
|
4
|
-
//#region src/fetch.d.ts
|
|
5
4
|
/** What `serveFetch` needs of the host runtime, per request. */
|
|
6
5
|
export interface FetchConfig<P, R, Env, RP = never> {
|
|
7
6
|
/** The server definition, as `RemoteServer.make` built it. */
|
|
@@ -73,5 +72,4 @@ export interface FetchConfig<P, R, Env, RP = never> {
|
|
|
73
72
|
* ```
|
|
74
73
|
*/
|
|
75
74
|
export declare const serveFetch: <P, R, Env, RP = never>(config: FetchConfig<P, R, Env, RP>) => ((request: Request, env: Env) => Promise<Response>);
|
|
76
|
-
//#endregion
|
|
77
75
|
//# sourceMappingURL=fetch.d.mts.map
|
package/dist/fetch.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetch.d.mts","names":[],"sources":["../src/fetch.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"fetch.d.mts","names":[],"sources":["../src/fetch.ts"],"mappings":";;;;iBAqCiB,YAAY,GAAG,GAAG,KAAK;;WAE7B,QAAQ,iBAAiB,GAAG;;;;;;WAM5B,mBACP,SAAS,SACT,KAAK,QACF,OAAO,OAAO,GAAG,uBAAuB;;;;;;WAMpC,QAAQ,KAAK,QAAQ,MAAM,MAAM,IAAI;;WAErC;;;;;;;WAOA,YAAY,WAAW,MAAM,SAAS;;WAEtC,OAAO,QAAQ,GAAG;;;;;;;;;;;;WAYlB;aAEM,OAAO,SAAS;aAChB,QAAQ,WAAW;;;WAIzB,gBAAgB,SAAS;;WAEzB;;WAEA;;;;;;;;;;;;;;;;;;;;;;;qBA+IE,aAAc,GAAG,GAAG,KAAK,YAAU,QACtC,YAAY,GAAG,GAAG,KAAK,UAC5B,SAAS,SAAS,KAAK,QAAQ,QAAQ"}
|
package/dist/fetch.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { r as RemoteServer, u as unauthenticatedAnswer } from "./src-ErnDxcWe.mjs";
|
|
2
2
|
import { Cause, Deferred, Duration, Effect, Exit, Layer, Option, Schedule, Schema, Scope, Stream } from "effect";
|
|
3
3
|
import { LiveRequirement, RemoteLiveError } from "foldkit-remote";
|
|
4
|
-
|
|
4
|
+
// src/fetch.ts
|
|
5
5
|
/**
|
|
6
6
|
* `foldkit-remote-server/fetch`: `RemoteServer` over the Fetch API, for
|
|
7
7
|
* Cloudflare Workers and any other runtime that speaks `Request`/`Response`.
|
|
@@ -160,7 +160,6 @@ const serveFetch = (config) => {
|
|
|
160
160
|
}
|
|
161
161
|
};
|
|
162
162
|
};
|
|
163
|
-
//#endregion
|
|
164
163
|
export { serveFetch };
|
|
165
164
|
|
|
166
165
|
//# sourceMappingURL=fetch.mjs.map
|
package/dist/index.d.mts
CHANGED
|
@@ -1,11 +1,36 @@
|
|
|
1
1
|
import { Context, Effect, Layer, Option, Schema, Stream } from "effect";
|
|
2
|
-
import { AnyWrite, EntityRef, QueryEvaluateError, Row, assertSupported, evaluate, supported } from "foldkit-entity";
|
|
2
|
+
import { AnyQuery, AnyWrite, EntityRef, MemoryOperators, QueryEvaluateError, Row, assertSupported, evaluate, supported } from "foldkit-entity";
|
|
3
3
|
import { Boundary, ConnectionChangeSchema, ConnectionIdentity, LiveChange, MutationDescriptor, NormalizedPatch, QueryDescriptor, QueryWindow, RelationRequirement, RemoteClient, RemoteDescriptor, RemoteForbidden, RemoteJsonAnswer, RemoteReadError, RemoteRpcClient, RemoteUnauthenticated, Requirement } from "foldkit-remote";
|
|
4
|
-
//#region src/index.d.ts
|
|
5
4
|
declare const RemoteServerError_base: Schema.Class<RemoteServerError, Schema.TaggedStruct<"RemoteServerError", {
|
|
6
5
|
readonly message: Schema.String;
|
|
7
6
|
}>, import("effect/Cause").YieldableError>;
|
|
8
7
|
export declare class RemoteServerError extends RemoteServerError_base {}
|
|
8
|
+
declare const RemoteUnmet_base: Schema.Class<RemoteUnmet, Schema.TaggedStruct<"RemoteUnmet", {
|
|
9
|
+
readonly message: Schema.String;
|
|
10
|
+
}>, import("effect/Cause").YieldableError>;
|
|
11
|
+
/**
|
|
12
|
+
* A writer's word that the row does not hold what the write's `when` asks of
|
|
13
|
+
* it, where the write would commit: `RemoteServer.write` answers it as the
|
|
14
|
+
* mutation's `Unmet` refusal.
|
|
15
|
+
*/
|
|
16
|
+
export declare class RemoteUnmet extends RemoteUnmet_base {}
|
|
17
|
+
declare const RemoteInvalid_base: Schema.Class<RemoteInvalid, Schema.TaggedStruct<"RemoteInvalid", {
|
|
18
|
+
readonly message: Schema.String;
|
|
19
|
+
readonly rule: Schema.Option<Schema.String>;
|
|
20
|
+
readonly path: Schema.$Array<Schema.String>;
|
|
21
|
+
}>, import("effect/Cause").YieldableError>;
|
|
22
|
+
/**
|
|
23
|
+
* A writer's word that the row a write would leave breaks one of its Entity's
|
|
24
|
+
* invariants: `RemoteServer.write` answers it as the mutation's `Invalid`
|
|
25
|
+
* refusal. `rule` and `path` are none and empty when saying them would tell
|
|
26
|
+
* the caller what a member hidden from them holds.
|
|
27
|
+
*/
|
|
28
|
+
export declare class RemoteInvalid extends RemoteInvalid_base {}
|
|
29
|
+
/** A write's precondition as a writer is handed it: the body, and the input encoded. */
|
|
30
|
+
export interface WriteWhen {
|
|
31
|
+
readonly body: AnyQuery;
|
|
32
|
+
readonly input: Readonly<Record<string, unknown>>;
|
|
33
|
+
}
|
|
9
34
|
declare const MutationRefused_base: Schema.Class<MutationRefused, Schema.TaggedStruct<"MutationRefused", {
|
|
10
35
|
/** The mutation the refusal was made for: a Source refusing as another is a bug. */
|
|
11
36
|
readonly mutation: Schema.String;
|
|
@@ -69,22 +94,75 @@ export interface ReadPlan {
|
|
|
69
94
|
/**
|
|
70
95
|
* Where a declared write lands: one row's new values, encoded as the store holds
|
|
71
96
|
* them. With `expect`, the row is written only while its revision is still that
|
|
72
|
-
* value, and the revision moves on with the write.
|
|
73
|
-
*
|
|
97
|
+
* value, and the revision moves on with the write. With `when`, only while the
|
|
98
|
+
* row holds its body, checked in the statement that writes it; a row there at
|
|
99
|
+
* the expected revision that does not hold it fails with `RemoteUnmet`. The row
|
|
100
|
+
* as written answers; none when no row matched.
|
|
74
101
|
*/
|
|
75
102
|
export interface EntityWriter<P, R = never> {
|
|
76
103
|
readonly entity: string;
|
|
104
|
+
/**
|
|
105
|
+
* Whether `update` and `delete` honour `when`. `RemoteServer.write` refuses
|
|
106
|
+
* a write with a precondition over a writer without it, which would write
|
|
107
|
+
* whatever the row held.
|
|
108
|
+
*/
|
|
109
|
+
readonly preconditions?: true;
|
|
110
|
+
/**
|
|
111
|
+
* Whether `update` adds `increments` to their fields where it writes.
|
|
112
|
+
* `RemoteServer.write` refuses a write with an increment over a writer
|
|
113
|
+
* without it, which would leave the fields where they were.
|
|
114
|
+
*/
|
|
115
|
+
readonly increments?: true;
|
|
116
|
+
/**
|
|
117
|
+
* Whether `update` records each write it lands under its `operation`, in
|
|
118
|
+
* the transaction that writes, and answers a retry of it from that record
|
|
119
|
+
* rather than writing again. An increment that expects no revision needs one.
|
|
120
|
+
*/
|
|
121
|
+
readonly ledger?: true;
|
|
122
|
+
/**
|
|
123
|
+
* Whether `update` and `insert` refuse a row, or a change, that would break
|
|
124
|
+
* one of its Entity's invariants (`Entity.invariant`), with `RemoteInvalid`,
|
|
125
|
+
* judged over the rows they write; an insert judges the state invariants
|
|
126
|
+
* alone. `RemoteServer.write` refuses a write the Entity holds to any through
|
|
127
|
+
* a writer without it.
|
|
128
|
+
*/
|
|
129
|
+
readonly invariants?: true;
|
|
130
|
+
/**
|
|
131
|
+
* Why this writer cannot land `write`, each a sentence; none when it can.
|
|
132
|
+
* `RemoteServer.write` asks once, where the write is registered, for what
|
|
133
|
+
* only the store knows: a column's type, say, which the Entity does not say.
|
|
134
|
+
*/
|
|
135
|
+
readonly refuses?: (write: AnyWrite) => ReadonlyArray<string>;
|
|
136
|
+
/**
|
|
137
|
+
* The answer recorded for `operation`, when this writer has a ledger and
|
|
138
|
+
* holds one: what a wrapping writer (a guard) answers a retry with, without
|
|
139
|
+
* deciding again over a row that has moved since.
|
|
140
|
+
*/
|
|
141
|
+
readonly recorded?: (context: {
|
|
142
|
+
readonly operation: string;
|
|
143
|
+
/** The row the operation writes: one operation may write several rows. */
|
|
144
|
+
readonly id: string;
|
|
145
|
+
readonly principal: P;
|
|
146
|
+
}) => Effect.Effect<Option.Option<Readonly<Record<string, unknown>>>, RemoteServerError, R>;
|
|
77
147
|
readonly update: (context: {
|
|
78
148
|
readonly id: string;
|
|
149
|
+
/** The operation's identity across clients, when the client sent one: a ledger's key. */
|
|
150
|
+
readonly operation: Option.Option<string>;
|
|
79
151
|
readonly values: Readonly<Record<string, unknown>>;
|
|
152
|
+
/**
|
|
153
|
+
* Each field to move, by the amount, encoded: added to the stored value in
|
|
154
|
+
* the statement that writes, never computed from a value read before it.
|
|
155
|
+
*/
|
|
156
|
+
readonly increments: Readonly<Record<string, number>>;
|
|
80
157
|
/** Each `one` relation it points, by the relation's key: the row, or none. */
|
|
81
158
|
readonly links: Readonly<Record<string, Option.Option<EntityRef>>>;
|
|
82
159
|
readonly expect: Option.Option<{
|
|
83
160
|
readonly field: string;
|
|
84
161
|
readonly revision: unknown;
|
|
85
162
|
}>;
|
|
163
|
+
readonly when: Option.Option<WriteWhen>;
|
|
86
164
|
readonly principal: P;
|
|
87
|
-
}) => Effect.Effect<Option.Option<Readonly<Record<string, unknown>>>, RemoteServerError | RemoteForbidden, R>;
|
|
165
|
+
}) => Effect.Effect<Option.Option<Readonly<Record<string, unknown>>>, RemoteServerError | RemoteForbidden | RemoteUnmet | RemoteInvalid, R>;
|
|
88
166
|
/**
|
|
89
167
|
* Inserts the row, or finds one already there under that id holding what
|
|
90
168
|
* it writes (a retry); answers the row as it is. Fails when the id holds
|
|
@@ -95,7 +173,7 @@ export interface EntityWriter<P, R = never> {
|
|
|
95
173
|
readonly values: Readonly<Record<string, unknown>>;
|
|
96
174
|
readonly links: Readonly<Record<string, Option.Option<EntityRef>>>;
|
|
97
175
|
readonly principal: P;
|
|
98
|
-
}) => Effect.Effect<Readonly<Record<string, unknown>>, RemoteServerError | RemoteForbidden, R>;
|
|
176
|
+
}) => Effect.Effect<Readonly<Record<string, unknown>>, RemoteServerError | RemoteForbidden | RemoteInvalid, R>;
|
|
99
177
|
/**
|
|
100
178
|
* Deletes the row, while it is still at the revision when one is expected:
|
|
101
179
|
* `deleted`, `absent` (no row, so nothing to delete; a retry's case), or
|
|
@@ -107,8 +185,9 @@ export interface EntityWriter<P, R = never> {
|
|
|
107
185
|
readonly field: string;
|
|
108
186
|
readonly revision: unknown;
|
|
109
187
|
}>;
|
|
188
|
+
readonly when: Option.Option<WriteWhen>;
|
|
110
189
|
readonly principal: P;
|
|
111
|
-
}) => Effect.Effect<'deleted' | 'absent' | 'moved', RemoteServerError | RemoteForbidden, R>;
|
|
190
|
+
}) => Effect.Effect<'deleted' | 'absent' | 'moved', RemoteServerError | RemoteForbidden | RemoteUnmet, R>;
|
|
112
191
|
/**
|
|
113
192
|
* The row as `principal` may see it, under the read rule its Source applies:
|
|
114
193
|
* none when there is no such row, or it is hidden. Given, a write's answer
|
|
@@ -150,13 +229,14 @@ export interface MutationOutcome<Output> {
|
|
|
150
229
|
export interface MutationSource<P, R = never> {
|
|
151
230
|
readonly mutation: string;
|
|
152
231
|
/**
|
|
153
|
-
* For a declared write (`RemoteServer.write`), the Entity it writes
|
|
154
|
-
* writer it lands through, as it was given one
|
|
155
|
-
* a startup check such as `GuardServer.validate` does.
|
|
232
|
+
* For a declared write (`RemoteServer.write`), the Entity it writes, the
|
|
233
|
+
* writer it lands through, as it was given one, and the write itself. The
|
|
234
|
+
* server never reads it; a startup check such as `GuardServer.validate` does.
|
|
156
235
|
*/
|
|
157
236
|
readonly writes?: {
|
|
158
237
|
readonly entity: string;
|
|
159
238
|
readonly writer: unknown;
|
|
239
|
+
readonly write: AnyWrite;
|
|
160
240
|
};
|
|
161
241
|
readonly Input: Schema.Codec<unknown>;
|
|
162
242
|
readonly Output: Schema.Codec<unknown>;
|
|
@@ -165,10 +245,16 @@ export interface MutationSource<P, R = never> {
|
|
|
165
245
|
readonly input: unknown;
|
|
166
246
|
readonly principal: P;
|
|
167
247
|
/**
|
|
168
|
-
* The client's id for this request, the same on every retry of it
|
|
169
|
-
*
|
|
248
|
+
* The client's id for this request, the same on every retry of it. It is
|
|
249
|
+
* unique within that client only: two clients' first requests can share
|
|
250
|
+
* it. Key a record of the write by `operation`, never by this.
|
|
170
251
|
*/
|
|
171
252
|
readonly requestId: string;
|
|
253
|
+
/**
|
|
254
|
+
* The operation's identity across clients, the same on every retry of it,
|
|
255
|
+
* when the client sent one (Remote's does); none from an older client.
|
|
256
|
+
*/
|
|
257
|
+
readonly operation: Option.Option<string>;
|
|
172
258
|
/** For a declared write: the input keys to write, when the client named only some. */
|
|
173
259
|
readonly keys?: ReadonlyArray<string> | undefined;
|
|
174
260
|
}) => Effect.Effect<{
|
|
@@ -324,6 +410,12 @@ export interface MemoryBackend<P = undefined> extends MemoryServed<P> {
|
|
|
324
410
|
export type MemoryConfig<P> = {
|
|
325
411
|
readonly domain: Pick<RemoteDescriptor, 'registry'>;
|
|
326
412
|
readonly rows: MemoryRows;
|
|
413
|
+
/**
|
|
414
|
+
* How the bodies' primitive operators are run: every query body is checked
|
|
415
|
+
* against it where the server is built, and one calling an operator it lacks
|
|
416
|
+
* is refused there. `Operators.kernel` when not given.
|
|
417
|
+
*/
|
|
418
|
+
readonly operators?: MemoryOperators | undefined;
|
|
327
419
|
/**
|
|
328
420
|
* Sources for entities the domain does not declare, which no row backs (a
|
|
329
421
|
* `Me` row answered from the principal, `GuardServer.me`), served beside
|
|
@@ -411,8 +503,10 @@ export declare const RemoteServer: {
|
|
|
411
503
|
mutation: <P = unknown, R = never, Name extends string = string, Input = unknown, Output = unknown, Refused = unknown>(mutation: MutationDescriptor<Name, Input, Output, Refused>, run: (context: {
|
|
412
504
|
readonly input: Input;
|
|
413
505
|
readonly principal: P;
|
|
414
|
-
/** The client's id for this request,
|
|
506
|
+
/** The client's id for this request, unique within that client only. */
|
|
415
507
|
readonly requestId: string;
|
|
508
|
+
/** The operation's identity across clients, for a record of the write; see `MutationSource`. */
|
|
509
|
+
readonly operation: Option.Option<string>;
|
|
416
510
|
}) => Effect.Effect<MutationOutcome<Output>, RemoteServerError | MutationRefused | RemoteForbidden, R>) => MutationSource<P, R>;
|
|
417
511
|
/**
|
|
418
512
|
* A refusal of `mutation`, as a value of its declared `Refusal`: fail a
|
|
@@ -421,6 +515,19 @@ export declare const RemoteServer: {
|
|
|
421
515
|
* person reads, beside it.
|
|
422
516
|
*/
|
|
423
517
|
refuse: <Refused>(mutation: MutationDescriptor<string, any, any, Refused>, refusal: NoInfer<Refused>, message?: string) => MutationRefused;
|
|
518
|
+
/**
|
|
519
|
+
* A writer's refusal of a row that does not hold the write's `when`: fail
|
|
520
|
+
* `update` or `delete` with it (`yield* RemoteServer.unmet()`), and
|
|
521
|
+
* `RemoteServer.write` answers the mutation's `Unmet` refusal.
|
|
522
|
+
*/
|
|
523
|
+
unmet: (message?: string) => RemoteUnmet;
|
|
524
|
+
/**
|
|
525
|
+
* A writer's refusal of a row that would break an invariant: fail `update`
|
|
526
|
+
* or `insert` with it, naming the invariant's id and path when the caller
|
|
527
|
+
* may be told them, and `RemoteServer.write` answers the mutation's
|
|
528
|
+
* `Invalid` refusal.
|
|
529
|
+
*/
|
|
530
|
+
invalid: (message: string, rule?: Option.Option<string>, path?: ReadonlyArray<string>) => RemoteInvalid;
|
|
424
531
|
/**
|
|
425
532
|
* A refusal of who is asking: fail a Source with it
|
|
426
533
|
* (`yield* RemoteServer.forbid()`) and the write never runs. `reason`, when
|
|
@@ -444,11 +551,15 @@ export declare const RemoteServer: {
|
|
|
444
551
|
* there; an insert of an id already holding what it writes (a retry)
|
|
445
552
|
* answers with that row, and of one holding anything else fails; a delete of
|
|
446
553
|
* a row already gone succeeds. A row that moved past the revision the write
|
|
447
|
-
* `expect`s is refused as a conflict
|
|
554
|
+
* `expect`s is refused as a conflict, and one that does not hold its `when`
|
|
555
|
+
* as unmet; a write with a `when` needs a writer that checks it
|
|
556
|
+
* (`preconditions`), and is refused here without one.
|
|
448
557
|
*/
|
|
449
|
-
write: <P = unknown, R = never, Name extends string = string, Input = unknown
|
|
558
|
+
write: <P = unknown, R = never, Name extends string = string, Input = unknown, Refused extends {
|
|
559
|
+
readonly _tag: 'Conflict' | 'Unmet' | 'Invalid';
|
|
560
|
+
} = {
|
|
450
561
|
readonly _tag: 'Conflict';
|
|
451
|
-
}> & {
|
|
562
|
+
}>(mutation: MutationDescriptor<Name, Input, {}, Refused> & {
|
|
452
563
|
readonly write: AnyWrite;
|
|
453
564
|
}, writer: EntityWriter<P, R>) => MutationSource<P, R>;
|
|
454
565
|
query: <P = unknown, R = never, Input = unknown>(query: QueryDescriptor<string, Input, unknown>, run: (context: {
|
|
@@ -523,6 +634,5 @@ export declare const RemoteServer: {
|
|
|
523
634
|
*/
|
|
524
635
|
handlers: <P, R, I = never>(server: ServerDefinition<P, R>, principal: P | Context.Key<I, P>, options?: HandlerOptions<P, R>) => RemoteRpcClient<R | I>;
|
|
525
636
|
};
|
|
526
|
-
//#endregion
|
|
527
637
|
export { QueryEvaluateError, type Row, assertSupported, evaluate, supported };
|
|
528
638
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":"
|
|
1
|
+
{"version":3,"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AG;QAC5C,WAAW,GAAG;;;;;;;;;;EAeV,OAAA,aAAa,WAAS;aAClB,mBAAmB,aAAa,GAAG;aACnC,qBAAqB,eAAe,GAAG;aACvC,mBAAmB,YAAY,GAAG;aAClC,gBAAgB,WAAW,GAAG;QACrC,iBAAiB,GAAG;;;;;;EAYL,WAAA,QAAA,kBAAgB,QAAU;;;;;;;EAsBhC,aAAA,GAAG,GAAC,QACP,iBAAiB,GAAG,IAAE,WACnB,GAAC,SACH,eAAe,GAAG,IAAE,UACnB,cAAc,iBACvB,OAAO;aAEG,UAAU;eACR;eACA;eACA,QAAQ;;aAEV,SAAS;eACP;eACA;eACA,QAAQ;;KAGrB,iBACA;;;;;;;;;;;EAgJO,SAAA,GAAC,UACE,gBAAgB,IAAE,kBAE3B,OAAO;aACG;aAAoD,MAAM;YAErE;;;;;;;;EA2DS,WAAA,GAAG,GAAG,WAAS,QAChB,iBAAiB,GAAG,IAAE,WACnB,IAAI,QAAQ,IAAI,GAAG,IAAE,UACvB,eAAe,GAAG,OAC1B,gBAAgB,IAAI"}
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { t as MutationRefused,
|
|
1
|
+
export { t as MutationRefused, o as QueryEvaluateError, n as RemoteInvalid, r as RemoteServer, i as RemoteServerError, a as RemoteUnmet, s as assertSupported, c as evaluate, l as supported } from "./src-ErnDxcWe.mjs";
|
package/dist/port.d.mts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Effect, Scope } from "effect";
|
|
2
2
|
import { RemoteRpcClient } from "foldkit-remote";
|
|
3
3
|
import { WorkerError } from "effect/workers/WorkerError";
|
|
4
|
-
//#region src/port.d.ts
|
|
5
4
|
/**
|
|
6
5
|
* Serves `handlers` (`RemoteServer.handlers(...)`) over `port`, returning once
|
|
7
6
|
* the client closes its end; interrupt it to stop sooner. Provide what the
|
|
@@ -9,5 +8,4 @@ import { WorkerError } from "effect/workers/WorkerError";
|
|
|
9
8
|
* breaks.
|
|
10
9
|
*/
|
|
11
10
|
export declare const servePort: <R>(handlers: RemoteRpcClient<R>, port: MessagePort) => Effect.Effect<void, WorkerError, Exclude<R, Scope.Scope>>;
|
|
12
|
-
//#endregion
|
|
13
11
|
//# sourceMappingURL=port.d.mts.map
|
package/dist/port.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"port.d.mts","names":[],"sources":["../src/port.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"port.d.mts","names":[],"sources":["../src/port.ts"],"mappings":";;;;;;;;;qBAkBa,YAAa,GAAC,UACf,gBAAgB,IAAE,MACtB,gBACL,OAAO,aAAa,aAAa,QAAQ,GAAG,MAAM"}
|
package/dist/port.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { RemoteRpc } from "foldkit-remote";
|
|
|
3
3
|
import { RpcServer } from "effect/rpc";
|
|
4
4
|
import * as WorkerRunner from "effect/workers/WorkerRunner";
|
|
5
5
|
import { portRunner } from "foldkit-remote/port";
|
|
6
|
-
|
|
6
|
+
// src/port.ts
|
|
7
7
|
/**
|
|
8
8
|
* `foldkit-remote-server/port`: `RemoteServer`'s handlers answering over a
|
|
9
9
|
* `MessagePort`, the other end of `foldkit-remote/port`'s `port`. A host in a
|
|
@@ -19,7 +19,6 @@ const servePort = (handlers, port) => {
|
|
|
19
19
|
const { platform, closed } = portRunner(port);
|
|
20
20
|
return Effect.scoped(Layer.build(RpcServer.layer(RemoteRpc).pipe(Layer.provide(RemoteRpc.toLayer(handlers)), Layer.provide(RpcServer.layerProtocolWorkerRunner), Layer.provide(Layer.succeed(WorkerRunner.WorkerRunnerPlatform)(platform)))).pipe(Effect.andThen(closed)));
|
|
21
21
|
};
|
|
22
|
-
//#endregion
|
|
23
22
|
export { servePort };
|
|
24
23
|
|
|
25
24
|
//# sourceMappingURL=port.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Context, Effect, Match, Option, Queue, Schema, Stream } from "effect";
|
|
2
|
-
import { QueryEvaluateError, Write, assertSupported, evaluate, evaluate as evaluate$1, supported } from "foldkit-entity";
|
|
2
|
+
import { Entity, Operators, QueryEvaluateError, Write, assertSupported, assertSupported as assertSupported$1, evaluate, evaluate as evaluate$1, supported } from "foldkit-entity";
|
|
3
3
|
import { MutationRequest, QueryRequest, RELATION_ALIAS, REMOTE_PROTOCOL_VERSION, ReadBatch, Remote, RemoteForbidden, RemoteJsonRequest, RemoteLiveError, RemoteMutationError, RemoteProtocolError, RemoteQueryError, RemoteReadError, RemoteUnauthenticated, Requirement, aliasedField, connectionIdentity, entityKey, refParts, refsIn, relationShape, stableStringify } from "foldkit-remote";
|
|
4
|
-
|
|
4
|
+
// src/refusal.ts
|
|
5
5
|
/** A caller who could not be told, as a 401 body: the twin of `answer`'s 403. */
|
|
6
6
|
const unauthenticatedAnswer = (refusal) => ({
|
|
7
7
|
status: 401,
|
|
@@ -10,8 +10,7 @@ const unauthenticatedAnswer = (refusal) => ({
|
|
|
10
10
|
unauthenticated: refusal.reason === void 0 ? {} : { reason: refusal.reason }
|
|
11
11
|
}
|
|
12
12
|
});
|
|
13
|
-
|
|
14
|
-
//#region src/index.ts
|
|
13
|
+
// src/index.ts
|
|
15
14
|
/**
|
|
16
15
|
* `foldkit-remote-server` — server-side Sources and handler compilation.
|
|
17
16
|
*
|
|
@@ -21,6 +20,23 @@ const unauthenticatedAnswer = (refusal) => ({
|
|
|
21
20
|
*/
|
|
22
21
|
var RemoteServerError = class extends Schema.TaggedError()("RemoteServerError", { message: Schema.String }) {};
|
|
23
22
|
/**
|
|
23
|
+
* A writer's word that the row does not hold what the write's `when` asks of
|
|
24
|
+
* it, where the write would commit: `RemoteServer.write` answers it as the
|
|
25
|
+
* mutation's `Unmet` refusal.
|
|
26
|
+
*/
|
|
27
|
+
var RemoteUnmet = class extends Schema.TaggedError()("RemoteUnmet", { message: Schema.String }) {};
|
|
28
|
+
/**
|
|
29
|
+
* A writer's word that the row a write would leave breaks one of its Entity's
|
|
30
|
+
* invariants: `RemoteServer.write` answers it as the mutation's `Invalid`
|
|
31
|
+
* refusal. `rule` and `path` are none and empty when saying them would tell
|
|
32
|
+
* the caller what a member hidden from them holds.
|
|
33
|
+
*/
|
|
34
|
+
var RemoteInvalid = class extends Schema.TaggedError()("RemoteInvalid", {
|
|
35
|
+
message: Schema.String,
|
|
36
|
+
rule: Schema.Option(Schema.String),
|
|
37
|
+
path: Schema.Array(Schema.String)
|
|
38
|
+
}) {};
|
|
39
|
+
/**
|
|
24
40
|
* A mutation Source's refusal, as the value its descriptor's `Refusal`
|
|
25
41
|
* declares: made with `RemoteServer.refuse`, which checks the value's type
|
|
26
42
|
* against the mutation, and sent to the client encoded, beside a message.
|
|
@@ -395,6 +411,8 @@ const pageOf = (items, window, idOf, locate) => {
|
|
|
395
411
|
};
|
|
396
412
|
};
|
|
397
413
|
function memory(config) {
|
|
414
|
+
const operators = config.operators ?? Operators.kernel;
|
|
415
|
+
for (const query of config.domain.registry.queries.values()) if (query.body !== void 0) assertSupported(query.body, operators);
|
|
398
416
|
const declared = config.domain.registry.entities;
|
|
399
417
|
const extra = /* @__PURE__ */ new Set();
|
|
400
418
|
for (const source of config.entities ?? []) {
|
|
@@ -511,7 +529,7 @@ function memory(config) {
|
|
|
511
529
|
const encoded = Schema.encodeSync(query.Input)(input);
|
|
512
530
|
const entity = body.entity.name;
|
|
513
531
|
const rows = store.rows(entity).filter((row) => readable(entity, row));
|
|
514
|
-
const matched = evaluate(body, encoded, rows);
|
|
532
|
+
const matched = evaluate(body, encoded, rows, { operators });
|
|
515
533
|
const locate = (cursor) => {
|
|
516
534
|
const row = rows.find((candidate) => String(candidate.id) === cursor);
|
|
517
535
|
if (row === void 0) throw new Error(`Cursor "${cursor}" names a row that no longer exists`);
|
|
@@ -519,7 +537,7 @@ function memory(config) {
|
|
|
519
537
|
index: evaluate({
|
|
520
538
|
...body,
|
|
521
539
|
where: []
|
|
522
|
-
}, encoded, [...matched, row]).indexOf(row),
|
|
540
|
+
}, encoded, [...matched, row], { operators }).indexOf(row),
|
|
523
541
|
exact: false
|
|
524
542
|
};
|
|
525
543
|
};
|
|
@@ -602,7 +620,8 @@ const RemoteServer = {
|
|
|
602
620
|
run: (context) => run({
|
|
603
621
|
input: context.input,
|
|
604
622
|
principal: context.principal,
|
|
605
|
-
requestId: context.requestId
|
|
623
|
+
requestId: context.requestId,
|
|
624
|
+
operation: context.operation
|
|
606
625
|
}).pipe(Effect.map((outcome) => ({
|
|
607
626
|
output: outcome.output,
|
|
608
627
|
entities: outcome.entities ?? [],
|
|
@@ -622,6 +641,23 @@ const RemoteServer = {
|
|
|
622
641
|
message
|
|
623
642
|
}),
|
|
624
643
|
/**
|
|
644
|
+
* A writer's refusal of a row that does not hold the write's `when`: fail
|
|
645
|
+
* `update` or `delete` with it (`yield* RemoteServer.unmet()`), and
|
|
646
|
+
* `RemoteServer.write` answers the mutation's `Unmet` refusal.
|
|
647
|
+
*/
|
|
648
|
+
unmet: (message = "The row does not hold what the write asks of it") => new RemoteUnmet({ message }),
|
|
649
|
+
/**
|
|
650
|
+
* A writer's refusal of a row that would break an invariant: fail `update`
|
|
651
|
+
* or `insert` with it, naming the invariant's id and path when the caller
|
|
652
|
+
* may be told them, and `RemoteServer.write` answers the mutation's
|
|
653
|
+
* `Invalid` refusal.
|
|
654
|
+
*/
|
|
655
|
+
invalid: (message, rule = Option.none(), path = []) => new RemoteInvalid({
|
|
656
|
+
message,
|
|
657
|
+
rule,
|
|
658
|
+
path
|
|
659
|
+
}),
|
|
660
|
+
/**
|
|
625
661
|
* A refusal of who is asking: fail a Source with it
|
|
626
662
|
* (`yield* RemoteServer.forbid()`) and the write never runs. `reason`, when
|
|
627
663
|
* given, is what the person who asked reads; without it the refusal says
|
|
@@ -650,24 +686,54 @@ const RemoteServer = {
|
|
|
650
686
|
* there; an insert of an id already holding what it writes (a retry)
|
|
651
687
|
* answers with that row, and of one holding anything else fails; a delete of
|
|
652
688
|
* a row already gone succeeds. A row that moved past the revision the write
|
|
653
|
-
* `expect`s is refused as a conflict
|
|
689
|
+
* `expect`s is refused as a conflict, and one that does not hold its `when`
|
|
690
|
+
* as unmet; a write with a `when` needs a writer that checks it
|
|
691
|
+
* (`preconditions`), and is refused here without one.
|
|
654
692
|
*/
|
|
655
693
|
write: (mutation, writer) => {
|
|
656
694
|
const { write } = mutation;
|
|
657
695
|
if (write.input.entity.name !== writer.entity) throw new Error(`[foldkit-remote-server] write "${mutation.name}" writes ${write.input.entity.name}, but its writer is for ${writer.entity}`);
|
|
696
|
+
if (write.increments.length > 0 && write.expect === void 0 && writer.ledger !== true) throw new Error(`[foldkit-remote-server] write "${mutation.name}" increments a field and expects no revision, so a retry could add twice; give it \`expect\`, or a writer with a ledger`);
|
|
697
|
+
if (write.increments.length > 0 && writer.increments !== true) throw new Error(`[foldkit-remote-server] write "${mutation.name}" increments a field, but its writer for ${writer.entity} does not add one (increments)`);
|
|
698
|
+
const refused = writer.refuses?.(write) ?? [];
|
|
699
|
+
if (refused.length > 0) throw new Error(`[foldkit-remote-server] write "${mutation.name}" cannot land through its writer for ${writer.entity}: ${refused.join("; ")}`);
|
|
700
|
+
const judged = Entity.invariants(write.input.entity).filter((held) => write._tag === "Update" || !Entity.readsBefore(held));
|
|
701
|
+
if (judged.length > 0 && writer.invariants !== true) throw new Error(`[foldkit-remote-server] write "${mutation.name}" writes ${writer.entity}, which has invariants, but its writer does not judge them (invariants)`);
|
|
702
|
+
if (write._tag === "Insert") {
|
|
703
|
+
const sets = /* @__PURE__ */ new Set(["id", ...write.sets.map((set) => set.field.key)]);
|
|
704
|
+
const unset = judged.flatMap((held) => held.after.filter((key) => !sets.has(key)).map((key) => `${held.id} reads ${key}`));
|
|
705
|
+
if (unset.length > 0) throw new Error(`[foldkit-remote-server] insert "${mutation.name}" leaves unset a field an invariant reads (${unset.join("; ")})`);
|
|
706
|
+
}
|
|
707
|
+
if (Option.isSome(write.when) && writer.preconditions !== true) throw new Error(`[foldkit-remote-server] write "${mutation.name}" has a precondition, but its writer for ${writer.entity} does not check one (preconditions)`);
|
|
708
|
+
const encodeInput = Schema.encodeUnknownEffect(write.input.schema);
|
|
658
709
|
return {
|
|
659
710
|
mutation: mutation.name,
|
|
660
711
|
writes: {
|
|
661
712
|
entity: writer.entity,
|
|
662
|
-
writer
|
|
713
|
+
writer,
|
|
714
|
+
write
|
|
663
715
|
},
|
|
664
716
|
Input: mutation.Input,
|
|
665
717
|
Output: mutation.Output,
|
|
666
718
|
Refusal: mutation.Refusal,
|
|
667
|
-
run: ({ input, principal, keys }) => {
|
|
719
|
+
run: ({ input, principal, keys, operation }) => {
|
|
668
720
|
const given = input;
|
|
669
721
|
const expect = Write.expected(write, given);
|
|
670
722
|
const conflict = (bound) => RemoteServer.refuse(mutation, { _tag: "Conflict" }, `${bound.entity} ${bound.id} changed since it was read`);
|
|
723
|
+
const when = Option.match(write.when, {
|
|
724
|
+
onNone: () => Effect.succeed(Option.none()),
|
|
725
|
+
onSome: (body) => encodeInput(given).pipe(Effect.map((encoded) => Option.some({
|
|
726
|
+
body,
|
|
727
|
+
input: encoded
|
|
728
|
+
})), Effect.mapError(() => new RemoteServerError({ message: `${mutation.name}'s input did not encode` })))
|
|
729
|
+
});
|
|
730
|
+
const unmet = (bound, writing) => Effect.catchTag(writing, "RemoteUnmet", () => Option.isNone(write.when) ? Effect.fail(new RemoteServerError({ message: `${writer.entity}'s writer refused ${mutation.name}, which has no precondition, as unmet` })) : Effect.fail(RemoteServer.refuse(mutation, { _tag: "Unmet" }, `${bound.entity} ${bound.id} does not hold what ${mutation.name} asks of it`)));
|
|
731
|
+
const invalid = (writing) => Effect.catchTag(writing, "RemoteInvalid", (refused) => Effect.fail(RemoteServer.refuse(mutation, {
|
|
732
|
+
_tag: "Invalid",
|
|
733
|
+
rule: refused.rule,
|
|
734
|
+
message: refused.message,
|
|
735
|
+
path: refused.path
|
|
736
|
+
}, refused.message)));
|
|
671
737
|
const answered = (bound, values) => {
|
|
672
738
|
const read = writer.read;
|
|
673
739
|
const shown = read === void 0 ? Effect.succeed(Option.some(values)) : read({
|
|
@@ -700,33 +766,38 @@ const RemoteServer = {
|
|
|
700
766
|
return Match.value(write).pipe(Match.tagsExhaustive({
|
|
701
767
|
Update: () => Effect.gen(function* () {
|
|
702
768
|
const bound = Write.bind(write, given, keys);
|
|
703
|
-
|
|
769
|
+
if (write.increments.length > 0 && write.expect === void 0 && Option.isNone(operation)) return yield* new RemoteServerError({ message: `${mutation.name} increments a field with no revision, so it needs an operation id` });
|
|
770
|
+
const written = yield* invalid(unmet(bound, writer.update({
|
|
704
771
|
id: bound.id,
|
|
772
|
+
operation,
|
|
705
773
|
values: bound.values,
|
|
774
|
+
increments: bound.increments,
|
|
706
775
|
links: bound.links,
|
|
707
776
|
expect,
|
|
777
|
+
when: yield* when,
|
|
708
778
|
principal
|
|
709
|
-
});
|
|
779
|
+
})));
|
|
710
780
|
if (Option.isSome(written)) return yield* answered(bound, written.value);
|
|
711
781
|
return yield* Option.isSome(expect) ? Effect.fail(conflict(bound)) : Effect.fail(new RemoteServerError({ message: `No ${bound.entity} ${bound.id} to update` }));
|
|
712
782
|
}),
|
|
713
783
|
Insert: () => Effect.gen(function* () {
|
|
714
784
|
const bound = Write.bind(write, given);
|
|
715
|
-
const row = yield* writer.insert({
|
|
785
|
+
const row = yield* invalid(writer.insert({
|
|
716
786
|
id: bound.id,
|
|
717
787
|
values: bound.values,
|
|
718
788
|
links: bound.links,
|
|
719
789
|
principal
|
|
720
|
-
});
|
|
790
|
+
}));
|
|
721
791
|
return yield* answered(bound, row);
|
|
722
792
|
}),
|
|
723
793
|
Delete: () => Effect.gen(function* () {
|
|
724
794
|
const bound = Write.bind(write, given);
|
|
725
|
-
if ((yield* writer.delete({
|
|
795
|
+
if ((yield* invalid(unmet(bound, writer.delete({
|
|
726
796
|
id: bound.id,
|
|
727
797
|
expect,
|
|
798
|
+
when: yield* when,
|
|
728
799
|
principal
|
|
729
|
-
})) === "moved") return yield* Effect.fail(conflict(bound));
|
|
800
|
+
})))) === "moved") return yield* Effect.fail(conflict(bound));
|
|
730
801
|
return {
|
|
731
802
|
output: {},
|
|
732
803
|
entities: [],
|
|
@@ -961,6 +1032,7 @@ const RemoteServer = {
|
|
|
961
1032
|
input,
|
|
962
1033
|
principal: caller,
|
|
963
1034
|
requestId: payload.requestId,
|
|
1035
|
+
operation: Option.fromUndefinedOr(payload.operation),
|
|
964
1036
|
keys: payload.keys
|
|
965
1037
|
}).pipe(Effect.catchTag("RemoteServerError", (error) => Effect.fail(new RemoteMutationError({ message: error.message }))), Effect.catchTag("MutationRefused", (refused) => refusalError(source.Refusal, payload.mutation, refused)));
|
|
966
1038
|
const output = yield* Schema.encodeUnknownEffect(source.Output)(outcome.output).pipe(Effect.catchTag("SchemaError", () => Effect.fail(new RemoteMutationError({ message: "Invalid mutation output" }))));
|
|
@@ -1054,7 +1126,6 @@ const RemoteServer = {
|
|
|
1054
1126
|
};
|
|
1055
1127
|
}
|
|
1056
1128
|
};
|
|
1057
|
-
|
|
1058
|
-
export { assertSupported as a, unauthenticatedAnswer as c, QueryEvaluateError as i, RemoteServer as n, evaluate$1 as o, RemoteServerError as r, supported as s, MutationRefused as t };
|
|
1129
|
+
export { RemoteUnmet as a, evaluate$1 as c, RemoteServerError as i, supported as l, RemoteInvalid as n, QueryEvaluateError as o, RemoteServer as r, assertSupported$1 as s, MutationRefused as t, unauthenticatedAnswer as u };
|
|
1059
1130
|
|
|
1060
|
-
//# sourceMappingURL=src-
|
|
1131
|
+
//# sourceMappingURL=src-ErnDxcWe.mjs.map
|