foldkit-remote-server 0.19.0 → 0.20.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 +91 -8
- package/dist/fetch.d.mts +9 -1
- package/dist/fetch.d.mts.map +1 -1
- package/dist/fetch.mjs +20 -15
- package/dist/fetch.mjs.map +1 -1
- package/dist/{index-BnaRbvjr.d.mts → index-B85UhEfk.d.mts} +77 -16
- package/dist/index-B85UhEfk.d.mts.map +1 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/{src-ZV9pARkU.mjs → src-D8azLxgd.mjs} +298 -73
- package/dist/src-D8azLxgd.mjs.map +1 -0
- package/package.json +7 -7
- package/dist/index-BnaRbvjr.d.mts.map +0 -1
- package/dist/src-ZV9pARkU.mjs.map +0 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Cause, Clock, Context, Data, Duration, Effect, Match, Option, Queue, Schema, Stream } from "effect";
|
|
2
2
|
import { Derivation, Entity, Held, Operators, Query, QueryEvaluateError, SchemaShape, Write, assertSupported, assertSupported as assertSupported$1, evaluate, evaluate as evaluate$1, supported } from "foldkit-entity";
|
|
3
|
-
import { Diagnostics, MutationRequest, QueryRequest, RELATION_ALIAS, REMOTE_PROTOCOL_VERSION, ReadBatch, Refusal, Remote, RemoteForbidden, RemoteJsonRequest, RemoteLiveError, RemoteLiveRefused, RemoteMutationError, RemoteProtocolError, RemoteQueryError, RemoteReadError, RemoteUnauthenticated, Requirement, aliasedField, connectionIdentity, entityKey, queryIdentity, refParts, refsIn, relationShape, stableStringify } from "foldkit-remote";
|
|
3
|
+
import { DEFAULT_MAX_BODY_BYTES, DEFAULT_MAX_IDS_PER_ENTITY, Diagnostics, MutationRequest, QueryRequest, RELATION_ALIAS, REMOTE_PROTOCOL_VERSION, ReadBatch, Refusal, Remote, RemoteForbidden, RemoteJsonRequest, RemoteLiveError, RemoteLiveRefused, RemoteMutationError, RemotePrincipalChanged, RemoteProtocolError, RemoteQueryError, RemoteReadError, RemoteUnauthenticated, Requirement, aliasedField, connectionIdentity, entityKey, queryIdentity, refParts, refsIn, relationShape, stableStringify } from "foldkit-remote";
|
|
4
|
+
import { HttpServerRequest, HttpServerResponse } from "effect/http";
|
|
4
5
|
// src/diagnostics.ts
|
|
5
6
|
/**
|
|
6
7
|
* What a Remote server answers past without telling the caller why: a defect
|
|
@@ -21,7 +22,7 @@ const answerUnread = (mutation, reason) => ({
|
|
|
21
22
|
_tag: "AnswerUnread",
|
|
22
23
|
source,
|
|
23
24
|
level: "warning",
|
|
24
|
-
message: `${mutation} committed, but reading its answer failed (${reason}): the client reads
|
|
25
|
+
message: `${mutation} committed, but reading its answer failed (${reason}): the client reads what it lacks again.`,
|
|
25
26
|
mutation,
|
|
26
27
|
reason
|
|
27
28
|
});
|
|
@@ -49,6 +50,105 @@ const unauthenticatedAnswer = (refusal) => ({
|
|
|
49
50
|
unauthenticated: refusal.reason === void 0 ? {} : { reason: refusal.reason }
|
|
50
51
|
}
|
|
51
52
|
});
|
|
53
|
+
// src/body.ts
|
|
54
|
+
/**
|
|
55
|
+
* A request body read up to a limit, refused past it before anything decodes
|
|
56
|
+
* it: what `serveFetch` and `bodyLimit` share. The limit is checked against
|
|
57
|
+
* `content-length` before a byte is read, and against the bytes as they
|
|
58
|
+
* arrive, so a body without one, or with one that lies, is cancelled the
|
|
59
|
+
* moment it passes the limit rather than buffered whole.
|
|
60
|
+
*/
|
|
61
|
+
/** The body passed the limit; nothing past it was kept. */
|
|
62
|
+
var BodyTooLarge = class extends Error {};
|
|
63
|
+
/** Remote's answer to a body over `limit`, sent as a 413. */
|
|
64
|
+
const tooLargeAnswer = (limit) => ({
|
|
65
|
+
error: `The request body is larger than ${limit} bytes`,
|
|
66
|
+
tooLarge: { limit }
|
|
67
|
+
});
|
|
68
|
+
/** Whether `request` says it is larger than `limit`, before any of it is read. */
|
|
69
|
+
const declaresTooLarge = (request, limit) => {
|
|
70
|
+
const length = Number(request.headers.get("content-length") ?? NaN);
|
|
71
|
+
return Number.isFinite(length) && length > limit;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* `request`'s body, at most `limit` bytes of it. Rejects with `BodyTooLarge`
|
|
75
|
+
* once more arrive, having cancelled the rest, or with the stream's own error.
|
|
76
|
+
*/
|
|
77
|
+
const readBounded = async (request, limit) => {
|
|
78
|
+
if (declaresTooLarge(request, limit)) throw new BodyTooLarge();
|
|
79
|
+
if (request.body === null) return /* @__PURE__ */ new Uint8Array();
|
|
80
|
+
const reader = request.body.getReader();
|
|
81
|
+
const chunks = [];
|
|
82
|
+
let total = 0;
|
|
83
|
+
for (;;) {
|
|
84
|
+
const { done, value } = await reader.read();
|
|
85
|
+
if (done) break;
|
|
86
|
+
total += value.byteLength;
|
|
87
|
+
if (total > limit) {
|
|
88
|
+
await reader.cancel();
|
|
89
|
+
throw new BodyTooLarge();
|
|
90
|
+
}
|
|
91
|
+
chunks.push(value);
|
|
92
|
+
}
|
|
93
|
+
const body = new Uint8Array(total);
|
|
94
|
+
let offset = 0;
|
|
95
|
+
for (const chunk of chunks) {
|
|
96
|
+
body.set(chunk, offset);
|
|
97
|
+
offset += chunk.byteLength;
|
|
98
|
+
}
|
|
99
|
+
return body;
|
|
100
|
+
};
|
|
101
|
+
const BoundedBody = Data.taggedEnum();
|
|
102
|
+
/** `request`'s body read up to `limit` bytes; never fails. */
|
|
103
|
+
const readBody = (request, limit) => Effect.tryPromise(() => readBounded(request, limit)).pipe(Effect.match({
|
|
104
|
+
onSuccess: (bytes) => BoundedBody.Read({ bytes }),
|
|
105
|
+
onFailure: (error) => error.cause instanceof BodyTooLarge ? BoundedBody.TooLarge() : BoundedBody.Unreadable()
|
|
106
|
+
}));
|
|
107
|
+
/** A body refused before any handler: the status and Remote's answer. */
|
|
108
|
+
var BodyRefused = class extends Data.TaggedError("BodyRefused") {};
|
|
109
|
+
/** Decodes a body as UTF-8, refusing bytes that are not, as `JSON.parse` would refuse the text. */
|
|
110
|
+
const utf8 = new TextDecoder("utf-8", { fatal: true });
|
|
111
|
+
/** `request`'s body read up to `limit` bytes and parsed as JSON, or refused. */
|
|
112
|
+
const readJson = (request, limit) => {
|
|
113
|
+
const notJson = new BodyRefused({
|
|
114
|
+
status: 400,
|
|
115
|
+
answer: { error: "The body is not JSON" }
|
|
116
|
+
});
|
|
117
|
+
return Effect.flatMap(readBody(request, limit), (body) => BoundedBody.$match(body, {
|
|
118
|
+
Read: ({ bytes }) => Effect.try({
|
|
119
|
+
try: () => JSON.parse(utf8.decode(bytes)),
|
|
120
|
+
catch: () => notJson
|
|
121
|
+
}),
|
|
122
|
+
TooLarge: () => Effect.fail(new BodyRefused({
|
|
123
|
+
status: 413,
|
|
124
|
+
answer: tooLargeAnswer(limit)
|
|
125
|
+
})),
|
|
126
|
+
Unreadable: () => Effect.fail(notJson)
|
|
127
|
+
}));
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Effect HTTP middleware refusing a request body over `maxBytes` (default
|
|
131
|
+
* 1 MiB, `serveFetch`'s) with a 413 and Remote's `tooLarge` answer, before
|
|
132
|
+
* the endpoint reads it. For an endpoint that serves `RemoteRpc` through
|
|
133
|
+
* `RpcServer`, where Effect's own `MaxBodySize` fails the body read as a
|
|
134
|
+
* defect, a 500:
|
|
135
|
+
*
|
|
136
|
+
* ```ts
|
|
137
|
+
* HttpRouter.add('POST', '/rpc', bodyLimit(64 * 1024)(rpcHttpEffect))
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* A body within the limit reaches the endpoint whole, read once.
|
|
141
|
+
*/
|
|
142
|
+
const bodyLimit = (maxBytes = DEFAULT_MAX_BODY_BYTES) => (endpoint) => Effect.gen(function* () {
|
|
143
|
+
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
144
|
+
const web = yield* Effect.orDie(HttpServerRequest.toWeb(request));
|
|
145
|
+
const body = yield* readBody(web, maxBytes);
|
|
146
|
+
return yield* BoundedBody.$match(body, {
|
|
147
|
+
TooLarge: () => Effect.succeed(HttpServerResponse.jsonUnsafe(tooLargeAnswer(maxBytes), { status: 413 })),
|
|
148
|
+
Unreadable: () => Effect.succeed(HttpServerResponse.jsonUnsafe({ error: "The body could not be read" }, { status: 400 })),
|
|
149
|
+
Read: ({ bytes }) => Effect.provideService(endpoint, HttpServerRequest.HttpServerRequest, HttpServerRequest.fromWeb(new Request(web, { body: bytes })))
|
|
150
|
+
});
|
|
151
|
+
});
|
|
52
152
|
// src/index.ts
|
|
53
153
|
/**
|
|
54
154
|
* `foldkit-remote-server` — server-side Sources and handler compilation.
|
|
@@ -122,6 +222,7 @@ const readAs = (source, context) => {
|
|
|
122
222
|
}))));
|
|
123
223
|
};
|
|
124
224
|
const RowRequirement = Data.taggedEnum();
|
|
225
|
+
const DeleteOutcome = Data.taggedEnum();
|
|
125
226
|
const connectionChange = (connection, ref, position) => {
|
|
126
227
|
const edge = {
|
|
127
228
|
entity: ref.entity,
|
|
@@ -192,6 +293,30 @@ const exprContext = (body, viewer, now) => {
|
|
|
192
293
|
/** Who asks, as the server's `viewer` says: none for a visitor, or on a server with no viewer. */
|
|
193
294
|
const viewerOf = (server, principal) => server.viewer === void 0 ? Option.none() : server.viewer(principal);
|
|
194
295
|
/**
|
|
296
|
+
* Whom an answer is for, as the wire says it (#405): the key of who asks
|
|
297
|
+
* (their `viewer`), `null` for a visitor, and nothing from a server that
|
|
298
|
+
* names no `viewer`, which keeps no keys.
|
|
299
|
+
*/
|
|
300
|
+
const answeredFor = (server, principal) => server.viewer === void 0 ? {} : { principal: Option.match(server.viewer(principal), {
|
|
301
|
+
onNone: () => null,
|
|
302
|
+
onSome: (viewer) => entityKey(viewer.entity, viewer.id)
|
|
303
|
+
}) };
|
|
304
|
+
/**
|
|
305
|
+
* Refuses a write made for someone other than who asks, before anything of
|
|
306
|
+
* it runs (#405): one made for someone, asked by a visitor, is asked to sign
|
|
307
|
+
* in; any other told who is signed in changed, a visitor's (`null`) asked by
|
|
308
|
+
* someone signed in included. A server that keeps no keys cannot tell, and
|
|
309
|
+
* refuses any write that names one.
|
|
310
|
+
*/
|
|
311
|
+
const madeFor = (server, principal, expected) => Option.match(expected, {
|
|
312
|
+
onNone: () => Effect.void,
|
|
313
|
+
onSome: (key) => {
|
|
314
|
+
const asking = answeredFor(server, principal).principal;
|
|
315
|
+
if (asking === key) return Effect.void;
|
|
316
|
+
return asking === null ? Effect.fail(RemoteServer.unauthenticated()) : Effect.fail(new RemotePrincipalChanged({ message: "The write was made for someone other than who is signed in now" }));
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
/**
|
|
195
320
|
* One row as `principal` may see it, through the server's own source for its
|
|
196
321
|
* Entity: the fields its `authorize` allows, filtered as the source filters.
|
|
197
322
|
* None for a row hidden or not there.
|
|
@@ -209,8 +334,6 @@ const rowAs = (server, principal) => (ref, fields) => {
|
|
|
209
334
|
};
|
|
210
335
|
/** `read` with the services the request already runs with, so its caller needs none. */
|
|
211
336
|
const withServices = (read, services) => (...args) => Effect.provideContext(read(...args), services);
|
|
212
|
-
/** A read batch may not carry more than this many distinct ids per entity. */
|
|
213
|
-
const DEFAULT_MAX_IDS_PER_ENTITY = 1e3;
|
|
214
337
|
/** A nested selection may not reach further than this many relation levels. */
|
|
215
338
|
const DEFAULT_MAX_DEPTH = 8;
|
|
216
339
|
/**
|
|
@@ -814,6 +937,76 @@ const answerRead = (server, principal, options, shown, outcome) => Effect.gen(fu
|
|
|
814
937
|
});
|
|
815
938
|
});
|
|
816
939
|
/**
|
|
940
|
+
* A query's page as `FoldkitRemoteQuery` answers it: the source's edges, and
|
|
941
|
+
* with a select, what the page's items select, read through `readHelper`, so
|
|
942
|
+
* `authorize` and `filter` hold. A mutation's answer reads its lists with it.
|
|
943
|
+
*/
|
|
944
|
+
const answerQuery = (server, caller, options, payload) => Effect.gen(function* () {
|
|
945
|
+
const source = server.queries.get(payload.query);
|
|
946
|
+
if (source === void 0) return yield* new RemoteQueryError({ message: `Unknown query: ${payload.query}` });
|
|
947
|
+
const input = yield* Schema.decodeUnknownEffect(source.Input)(payload.input).pipe(Effect.catchTag("SchemaError", () => Effect.fail(new RemoteQueryError({ message: "Invalid query input" }))));
|
|
948
|
+
const select = payload.select;
|
|
949
|
+
if (select !== void 0) {
|
|
950
|
+
const paged = checkPagesPerRelation([select]);
|
|
951
|
+
if (paged !== void 0) return yield* new RemoteQueryError({ message: `Too many pages of "${paged}" in one query select` });
|
|
952
|
+
}
|
|
953
|
+
const page = yield* source.run({
|
|
954
|
+
input,
|
|
955
|
+
window: payload.window,
|
|
956
|
+
principal: caller,
|
|
957
|
+
viewer: viewerOf(server, caller),
|
|
958
|
+
...select === void 0 ? {} : { select }
|
|
959
|
+
}).pipe(Effect.catchTag("RemoteServerError", (error) => Effect.fail(new RemoteQueryError({ message: error.message }))));
|
|
960
|
+
if (select === void 0) return {
|
|
961
|
+
edges: page.edges,
|
|
962
|
+
start: page.start,
|
|
963
|
+
end: page.end
|
|
964
|
+
};
|
|
965
|
+
const requirements = page.edges.flatMap((edge) => edge.entity === select.entity ? [{
|
|
966
|
+
entity: select.entity,
|
|
967
|
+
id: edge.id,
|
|
968
|
+
fields: [...select.fields],
|
|
969
|
+
...select.windows === void 0 ? {} : { windows: select.windows },
|
|
970
|
+
...select.relations === void 0 ? {} : { relations: select.relations }
|
|
971
|
+
}] : []);
|
|
972
|
+
if (requirements.length === 0) return {
|
|
973
|
+
edges: page.edges,
|
|
974
|
+
start: page.start,
|
|
975
|
+
end: page.end,
|
|
976
|
+
entities: [],
|
|
977
|
+
settled: []
|
|
978
|
+
};
|
|
979
|
+
const read = yield* RemoteServer.readHelper(server, caller, options, requirements).pipe(Effect.catchTag("RemoteReadError", (error) => Effect.fail(new RemoteQueryError({ message: error.message }))));
|
|
980
|
+
return {
|
|
981
|
+
edges: page.edges,
|
|
982
|
+
start: page.start,
|
|
983
|
+
end: page.end,
|
|
984
|
+
...read
|
|
985
|
+
};
|
|
986
|
+
});
|
|
987
|
+
/**
|
|
988
|
+
* The lists of a mutation request the server answers after the write, judged
|
|
989
|
+
* before it runs: each a first page (`first`, no `after`) of a query it serves,
|
|
990
|
+
* selecting that query's entity, within the select's page bound, while the
|
|
991
|
+
* rows every admitted list may return stay within `maxIds` per entity. One
|
|
992
|
+
* past these is not answered, and the client asks for it again; the write
|
|
993
|
+
* runs either way.
|
|
994
|
+
*/
|
|
995
|
+
const admitLists = (server, lists, maxIds) => {
|
|
996
|
+
const rows = /* @__PURE__ */ new Map();
|
|
997
|
+
return lists.filter((request) => {
|
|
998
|
+
const source = server.queries.get(request.query);
|
|
999
|
+
const { select, window } = request;
|
|
1000
|
+
if (source === void 0 || select === void 0 || select.entity !== source.entity) return false;
|
|
1001
|
+
if (window.first === void 0 || window.after !== void 0) return false;
|
|
1002
|
+
if (checkPagesPerRelation([select]) !== void 0) return false;
|
|
1003
|
+
const listed = (rows.get(select.entity) ?? 0) + window.first;
|
|
1004
|
+
if (listed > maxIds) return false;
|
|
1005
|
+
rows.set(select.entity, listed);
|
|
1006
|
+
return true;
|
|
1007
|
+
});
|
|
1008
|
+
};
|
|
1009
|
+
/**
|
|
817
1010
|
* A Source's refusal as the client receives it: encoded by the mutation's own
|
|
818
1011
|
* `Refusal`. A refusal made for another mutation, or one the mutation does not
|
|
819
1012
|
* declare, is this side's bug, and says so without the value.
|
|
@@ -922,8 +1115,10 @@ const RemoteServer = {
|
|
|
922
1115
|
/**
|
|
923
1116
|
* A refusal of a caller who could not be told: a `serveFetch` or
|
|
924
1117
|
* `defineDocumentHost` resolver fails with it (`RemoteServer.unauthenticated()`)
|
|
925
|
-
* when the credential it was sent no longer holds
|
|
926
|
-
* `
|
|
1118
|
+
* when the credential it was sent no longer holds, and a write refused to a
|
|
1119
|
+
* visitor fails with it (`GuardServer` does), so a client can tell a session
|
|
1120
|
+
* that ended from a refusal. It answers 401, with `reason`, when given, for
|
|
1121
|
+
* the person who asked; a durable write so answered waits for a sign-in.
|
|
927
1122
|
*/
|
|
928
1123
|
unauthenticated: (reason) => new RemoteUnauthenticated({
|
|
929
1124
|
message: reason ?? "Unauthenticated",
|
|
@@ -957,30 +1152,67 @@ const RemoteServer = {
|
|
|
957
1152
|
}
|
|
958
1153
|
const refused = writer.refuses?.(write) ?? [];
|
|
959
1154
|
if (refused.length > 0) throw new Error(`[foldkit-remote-server] write "${mutation.name}" cannot land through its writer for ${writer.entity}: ${refused.join("; ")}`);
|
|
960
|
-
const
|
|
1155
|
+
const kind = Match.value(write).pipe(Match.tagsExhaustive({
|
|
1156
|
+
Insert: () => ({
|
|
1157
|
+
hasBefore: false,
|
|
1158
|
+
inserts: true,
|
|
1159
|
+
deletes: false
|
|
1160
|
+
}),
|
|
1161
|
+
Update: () => ({
|
|
1162
|
+
hasBefore: true,
|
|
1163
|
+
inserts: false,
|
|
1164
|
+
deletes: false
|
|
1165
|
+
}),
|
|
1166
|
+
Delete: () => ({
|
|
1167
|
+
hasBefore: false,
|
|
1168
|
+
inserts: false,
|
|
1169
|
+
deletes: true
|
|
1170
|
+
})
|
|
1171
|
+
}));
|
|
1172
|
+
const judged = Entity.invariants(write.input.entity).filter((held) => kind.hasBefore || !Entity.readsBefore(held));
|
|
961
1173
|
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)`);
|
|
962
|
-
|
|
963
|
-
if (write._tag === "Insert") {
|
|
1174
|
+
if (kind.inserts) {
|
|
964
1175
|
const sets = /* @__PURE__ */ new Set([
|
|
965
1176
|
"id",
|
|
966
1177
|
...write.sets.map((set) => set.field.key),
|
|
967
|
-
...
|
|
1178
|
+
...write.links.map((link) => link.relation.key),
|
|
1179
|
+
...write.context.map(({ member }) => member.key)
|
|
968
1180
|
]);
|
|
969
1181
|
const unset = judged.flatMap((held) => held.after.filter((key) => !sets.has(key)).map((key) => `${held.id} reads ${key}`));
|
|
970
1182
|
if (unset.length > 0) throw new Error(`[foldkit-remote-server] insert "${mutation.name}" leaves unset a field an invariant reads (${unset.join("; ")})`);
|
|
971
1183
|
}
|
|
972
1184
|
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)`);
|
|
973
1185
|
const encodeInput = Schema.encodeUnknownEffect(write.input.schema);
|
|
974
|
-
const
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
1186
|
+
const pointersOf = (entity) => {
|
|
1187
|
+
const relations = entity.relations;
|
|
1188
|
+
return Object.values(relations).flatMap((relation) => {
|
|
1189
|
+
if (relation._tag !== "Relation" || relation.cardinality !== "one") return [];
|
|
1190
|
+
const isThis = (read) => read.owner.token === relation.owner.token && read.key === relation.key;
|
|
1191
|
+
return Object.values(relation.target().derived).some((member) => member._tag === "Derived" && Derivation.$match(member.derives, {
|
|
1192
|
+
Supplied: () => true,
|
|
1193
|
+
Count: ({ relation: read }) => isThis(read),
|
|
1194
|
+
Exists: ({ relation: read }) => isThis(read)
|
|
1195
|
+
})) ? [relation.key] : [];
|
|
1196
|
+
});
|
|
1197
|
+
};
|
|
1198
|
+
const pointers = pointersOf(write.input.entity);
|
|
1199
|
+
const cascadePointers = /* @__PURE__ */ new Map();
|
|
1200
|
+
if (kind.deletes) {
|
|
1201
|
+
const reach = (entity) => {
|
|
1202
|
+
for (const { owner } of Entity.cascading(entity)) {
|
|
1203
|
+
if (cascadePointers.has(owner.name)) continue;
|
|
1204
|
+
cascadePointers.set(owner.name, pointersOf(owner));
|
|
1205
|
+
reach(owner);
|
|
1206
|
+
}
|
|
1207
|
+
};
|
|
1208
|
+
reach(write.input.entity);
|
|
1209
|
+
if (cascadePointers.size > 0 && writer.cascades !== true) throw new Error(`[foldkit-remote-server] delete "${mutation.name}" removes ${write.input.entity.name}, whose rows take ${[...cascadePointers.keys()].join(", ")} with them (onDelete: 'cascade'), but its writer does not remove them (cascades)`);
|
|
1210
|
+
}
|
|
1211
|
+
/** What a removed row pointed at that its removal moves, as refs. */
|
|
1212
|
+
const pointedBy = (row) => (cascadePointers.get(row.entity) ?? []).flatMap((key) => Option.match(Option.flatten(Option.fromUndefinedOr(row.links[key])), {
|
|
1213
|
+
onNone: () => [],
|
|
1214
|
+
onSome: (ref) => [ref]
|
|
1215
|
+
}));
|
|
984
1216
|
const seenLinks = (id, principal) => writer.read !== void 0 ? writer.read({
|
|
985
1217
|
id,
|
|
986
1218
|
fields: pointers,
|
|
@@ -1019,6 +1251,7 @@ const RemoteServer = {
|
|
|
1019
1251
|
onNone: () => null,
|
|
1020
1252
|
onSome: (values) => Object.hasOwn(values, key) ? values[key] : void 0
|
|
1021
1253
|
}))));
|
|
1254
|
+
const unfilled = Option.isNone(viewer) ? RemoteServer.unauthenticated() : RemoteServer.forbid();
|
|
1022
1255
|
const given = input;
|
|
1023
1256
|
const expect = Write.expected(write, given);
|
|
1024
1257
|
const conflict = (bound) => RemoteServer.refuse(mutation, { _tag: "Conflict" }, `${bound.entity} ${bound.id} changed since it was read`);
|
|
@@ -1075,7 +1308,7 @@ const RemoteServer = {
|
|
|
1075
1308
|
Update: () => Effect.gen(function* () {
|
|
1076
1309
|
const named = Write.bind(write, given, keys);
|
|
1077
1310
|
const set = yield* context;
|
|
1078
|
-
if (set.unfilled.length > 0) return yield* Effect.fail(
|
|
1311
|
+
if (set.unfilled.length > 0) return yield* Effect.fail(unfilled);
|
|
1079
1312
|
const bound = {
|
|
1080
1313
|
...named,
|
|
1081
1314
|
values: {
|
|
@@ -1107,7 +1340,7 @@ const RemoteServer = {
|
|
|
1107
1340
|
Insert: () => Effect.gen(function* () {
|
|
1108
1341
|
const bound = Write.bind(write, given);
|
|
1109
1342
|
const set = yield* context;
|
|
1110
|
-
if (set.unfilled.length > 0) return yield* Effect.fail(
|
|
1343
|
+
if (set.unfilled.length > 0) return yield* Effect.fail(unfilled);
|
|
1111
1344
|
const row = yield* invalid(writer.insert({
|
|
1112
1345
|
id: bound.id,
|
|
1113
1346
|
values: {
|
|
@@ -1137,17 +1370,23 @@ const RemoteServer = {
|
|
|
1137
1370
|
when: yield* when,
|
|
1138
1371
|
principal
|
|
1139
1372
|
})));
|
|
1140
|
-
|
|
1141
|
-
const gone = {
|
|
1373
|
+
const gone = (cascaded) => ({
|
|
1142
1374
|
output: {},
|
|
1143
1375
|
entities: [],
|
|
1144
1376
|
connections: [],
|
|
1145
1377
|
deleted: [{
|
|
1146
1378
|
entity: bound.entity,
|
|
1147
1379
|
id: bound.id
|
|
1148
|
-
}
|
|
1149
|
-
|
|
1150
|
-
|
|
1380
|
+
}, ...cascaded.map(({ entity, id }) => ({
|
|
1381
|
+
entity,
|
|
1382
|
+
id
|
|
1383
|
+
}))]
|
|
1384
|
+
});
|
|
1385
|
+
return yield* DeleteOutcome.$match(outcome, {
|
|
1386
|
+
Moved: () => Effect.fail(conflict(bound)),
|
|
1387
|
+
Absent: () => Effect.succeed(gone([])),
|
|
1388
|
+
Deleted: ({ cascaded }) => Effect.succeed(naming(gone(cascaded), [...before, ...cascaded.flatMap(pointedBy)]))
|
|
1389
|
+
});
|
|
1151
1390
|
})
|
|
1152
1391
|
}));
|
|
1153
1392
|
}
|
|
@@ -1197,7 +1436,12 @@ const RemoteServer = {
|
|
|
1197
1436
|
* The server: its sources by name. `viewer` says who asks, as a row
|
|
1198
1437
|
* (`principal => Option.some(Remote.ref(User, principal.id))`), or none for
|
|
1199
1438
|
* a visitor: what a declared write's `Expr.viewer` sets. A server
|
|
1200
|
-
* registering such an insert without one is refused here.
|
|
1439
|
+
* registering such an insert without one is refused here. It is also whom
|
|
1440
|
+
* each answer says it is for (`principal`, the row's key), which a durable
|
|
1441
|
+
* write carries back: one made for another viewer than who asks is refused
|
|
1442
|
+
* before it runs (`RemotePrincipalChanged`). A server without `viewer`
|
|
1443
|
+
* names no one in its answers, and refuses a write that names someone,
|
|
1444
|
+
* since it cannot check it; a write that names no one runs (#405).
|
|
1201
1445
|
*
|
|
1202
1446
|
* `clockSkew` is how far from this server's clock a client's time (the
|
|
1203
1447
|
* `now` a write is sent with, when the reader acted) may be and still be
|
|
@@ -1354,7 +1598,8 @@ const RemoteServer = {
|
|
|
1354
1598
|
* The request and its payload are decoded by the protocol's own schemas
|
|
1355
1599
|
* before a handler sees them, so an operation name is one of Remote's three
|
|
1356
1600
|
* and nothing else. A body that is not a request is a 400; a caller who
|
|
1357
|
-
* could not be told a 401; a refusal of who is asking a 403; a
|
|
1601
|
+
* could not be told a 401; a refusal of who is asking a 403; a write made
|
|
1602
|
+
* for someone else (`RemotePrincipalChanged`) a 409; a mutation's
|
|
1358
1603
|
* declared refusal a 422, or a 409 when it is `Refusal.taken`; a handler's
|
|
1359
1604
|
* failure a 500 with its message; a defect a 500 that says nothing of it,
|
|
1360
1605
|
* its cause reported as a `ServerDefect` (`Diagnostics`, by default the log).
|
|
@@ -1372,7 +1617,13 @@ const RemoteServer = {
|
|
|
1372
1617
|
})), Effect.catchTag("SchemaError", (error) => Effect.succeed({
|
|
1373
1618
|
status: 400,
|
|
1374
1619
|
body: { error: error.message }
|
|
1375
|
-
})), Effect.catchTag("Unauthenticated", (error) => Effect.succeed(unauthenticatedAnswer(error))), Effect.catchTag("
|
|
1620
|
+
})), Effect.catchTag("Unauthenticated", (error) => Effect.succeed(unauthenticatedAnswer(error))), Effect.catchTag("PrincipalChanged", (error) => Effect.succeed({
|
|
1621
|
+
status: 409,
|
|
1622
|
+
body: {
|
|
1623
|
+
error: error.message,
|
|
1624
|
+
principalChanged: {}
|
|
1625
|
+
}
|
|
1626
|
+
})), Effect.catchTag("Forbidden", (error) => Effect.succeed({
|
|
1376
1627
|
status: 403,
|
|
1377
1628
|
body: {
|
|
1378
1629
|
error: error.message,
|
|
@@ -1409,13 +1660,18 @@ const RemoteServer = {
|
|
|
1409
1660
|
const mismatch = protocolMismatch(payload.version);
|
|
1410
1661
|
if (mismatch !== void 0) return yield* mismatch;
|
|
1411
1662
|
const caller = yield* asking;
|
|
1412
|
-
return
|
|
1663
|
+
return {
|
|
1664
|
+
...yield* RemoteServer.readHelper(server, caller, options, payload.requests),
|
|
1665
|
+
...answeredFor(server, caller)
|
|
1666
|
+
};
|
|
1413
1667
|
}),
|
|
1414
1668
|
FoldkitRemoteMutate: Effect.fn("RemoteServer.FoldkitRemoteMutate")(function* (payload) {
|
|
1415
1669
|
const source = server.mutations.get(payload.mutation);
|
|
1416
1670
|
if (source === void 0) return yield* new RemoteMutationError({ message: `Unknown mutation: ${payload.mutation}` });
|
|
1417
1671
|
const input = yield* Schema.decodeUnknownEffect(source.Input)(payload.input).pipe(Effect.catchTag("SchemaError", () => Effect.fail(new RemoteMutationError({ message: "Invalid mutation input" }))));
|
|
1672
|
+
const lists = admitLists(server, payload.lists ?? [], Math.max(1, options.maxIdsPerEntity ?? DEFAULT_MAX_IDS_PER_ENTITY));
|
|
1418
1673
|
const caller = yield* asking;
|
|
1674
|
+
yield* madeFor(server, caller, Option.fromUndefinedOr(payload.principal));
|
|
1419
1675
|
const outcome = yield* source.run({
|
|
1420
1676
|
input,
|
|
1421
1677
|
principal: caller,
|
|
@@ -1428,6 +1684,10 @@ const RemoteServer = {
|
|
|
1428
1684
|
}).pipe(Effect.catchTag("RemoteServerError", (error) => Effect.fail(new RemoteMutationError({ message: error.message }))), Effect.catchTag("MutationRefused", (refused) => refusalError(source.Refusal, payload.mutation, refused)));
|
|
1429
1685
|
const output = yield* Schema.encodeUnknownEffect(source.Output)(outcome.output).pipe(Effect.catchTag("SchemaError", () => Effect.fail(new RemoteMutationError({ message: "Invalid mutation output" }))));
|
|
1430
1686
|
const read = yield* answerRead(server, caller, options, payload.shown ?? [], outcome).pipe(Effect.catchTag("RemoteReadError", (error) => Diagnostics.report(answerUnread(payload.mutation, error.message)).pipe(Effect.as(Option.none()))));
|
|
1687
|
+
const answered = yield* Effect.forEach(lists, (request) => answerQuery(server, caller, options, request).pipe(Effect.map((result) => [{
|
|
1688
|
+
request,
|
|
1689
|
+
result
|
|
1690
|
+
}]), Effect.catchTag("RemoteQueryError", (error) => Diagnostics.report(answerUnread(payload.mutation, `list "${request.query}": ${error.message}`)).pipe(Effect.as([]))))).pipe(Effect.map((pages) => pages.flat()));
|
|
1431
1691
|
const hub = options.live;
|
|
1432
1692
|
if (hub !== void 0) {
|
|
1433
1693
|
const stored = source.writes !== void 0;
|
|
@@ -1451,51 +1711,16 @@ const RemoteServer = {
|
|
|
1451
1711
|
...Option.match(read, {
|
|
1452
1712
|
onNone: () => ({}),
|
|
1453
1713
|
onSome: (value) => ({ read: value })
|
|
1454
|
-
})
|
|
1714
|
+
}),
|
|
1715
|
+
...answered.length === 0 ? {} : { lists: answered },
|
|
1716
|
+
...answeredFor(server, caller)
|
|
1455
1717
|
};
|
|
1456
1718
|
}),
|
|
1457
1719
|
FoldkitRemoteQuery: Effect.fn("RemoteServer.FoldkitRemoteQuery")(function* (payload) {
|
|
1458
|
-
const source = server.queries.get(payload.query);
|
|
1459
|
-
if (source === void 0) return yield* new RemoteQueryError({ message: `Unknown query: ${payload.query}` });
|
|
1460
|
-
const input = yield* Schema.decodeUnknownEffect(source.Input)(payload.input).pipe(Effect.catchTag("SchemaError", () => Effect.fail(new RemoteQueryError({ message: "Invalid query input" }))));
|
|
1461
|
-
const select = payload.select;
|
|
1462
|
-
if (select !== void 0) {
|
|
1463
|
-
const paged = checkPagesPerRelation([select]);
|
|
1464
|
-
if (paged !== void 0) return yield* new RemoteQueryError({ message: `Too many pages of "${paged}" in one query select` });
|
|
1465
|
-
}
|
|
1466
1720
|
const caller = yield* asking;
|
|
1467
|
-
const page = yield* source.run({
|
|
1468
|
-
input,
|
|
1469
|
-
window: payload.window,
|
|
1470
|
-
principal: caller,
|
|
1471
|
-
viewer: viewerOf(server, caller),
|
|
1472
|
-
...select === void 0 ? {} : { select }
|
|
1473
|
-
}).pipe(Effect.catchTag("RemoteServerError", (error) => Effect.fail(new RemoteQueryError({ message: error.message }))));
|
|
1474
|
-
if (select === void 0) return {
|
|
1475
|
-
edges: page.edges,
|
|
1476
|
-
start: page.start,
|
|
1477
|
-
end: page.end
|
|
1478
|
-
};
|
|
1479
|
-
const requirements = page.edges.flatMap((edge) => edge.entity === select.entity ? [{
|
|
1480
|
-
entity: select.entity,
|
|
1481
|
-
id: edge.id,
|
|
1482
|
-
fields: [...select.fields],
|
|
1483
|
-
...select.windows === void 0 ? {} : { windows: select.windows },
|
|
1484
|
-
...select.relations === void 0 ? {} : { relations: select.relations }
|
|
1485
|
-
}] : []);
|
|
1486
|
-
if (requirements.length === 0) return {
|
|
1487
|
-
edges: page.edges,
|
|
1488
|
-
start: page.start,
|
|
1489
|
-
end: page.end,
|
|
1490
|
-
entities: [],
|
|
1491
|
-
settled: []
|
|
1492
|
-
};
|
|
1493
|
-
const read = yield* RemoteServer.readHelper(server, caller, options, requirements).pipe(Effect.catchTag("RemoteReadError", (error) => Effect.fail(new RemoteQueryError({ message: error.message }))));
|
|
1494
1721
|
return {
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
end: page.end,
|
|
1498
|
-
...read
|
|
1722
|
+
...yield* answerQuery(server, caller, options, payload),
|
|
1723
|
+
...answeredFor(server, caller)
|
|
1499
1724
|
};
|
|
1500
1725
|
}),
|
|
1501
1726
|
FoldkitRemoteLive: (payload) => {
|
|
@@ -1545,6 +1770,6 @@ const RemoteServer = {
|
|
|
1545
1770
|
};
|
|
1546
1771
|
}
|
|
1547
1772
|
};
|
|
1548
|
-
export {
|
|
1773
|
+
export { supported as _, RemoteServerError as a, RowRequirement as c, declaresTooLarge as d, readJson as f, evaluate$1 as g, assertSupported$1 as h, RemoteServer as i, exprContext as l, QueryEvaluateError as m, MutationRefused as n, RemoteTaken as o, tooLargeAnswer as p, RemoteInvalid as r, RemoteUnmet as s, DeleteOutcome as t, bodyLimit as u, unauthenticatedAnswer as v, serverDefect as y };
|
|
1549
1774
|
|
|
1550
|
-
//# sourceMappingURL=src-
|
|
1775
|
+
//# sourceMappingURL=src-D8azLxgd.mjs.map
|