@party-stack/ontology-server 0.11.1 → 0.11.3
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 +153 -71
- package/lib/definitionProjection.d.ts.map +1 -1
- package/lib/definitionProjection.js +3 -2
- package/lib/definitionProjection.js.map +1 -1
- package/lib/errorProjection.d.ts +1 -1
- package/lib/errorProjection.d.ts.map +1 -1
- package/lib/errorProjection.js +16 -1
- package/lib/errorProjection.js.map +1 -1
- package/lib/operations.d.ts.map +1 -1
- package/lib/operations.js +1 -4
- package/lib/operations.js.map +1 -1
- package/lib/routing.d.ts +5 -1
- package/lib/routing.d.ts.map +1 -1
- package/lib/routing.js +2 -2
- package/lib/routing.js.map +1 -1
- package/lib/rpc.d.ts +49 -1725
- package/lib/rpc.d.ts.map +1 -1
- package/lib/rpc.js +21 -15
- package/lib/rpc.js.map +1 -1
- package/lib/rpcLocal.d.ts.map +1 -1
- package/lib/rpcLocal.js +2 -2
- package/lib/rpcLocal.js.map +1 -1
- package/lib/rpcWorker.d.ts.map +1 -1
- package/lib/rpcWorker.js +1 -1
- package/lib/rpcWorker.js.map +1 -1
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -16,55 +16,79 @@ role by where writes commit:
|
|
|
16
16
|
|
|
17
17
|
```ts
|
|
18
18
|
import { makeOntologyServer } from "@party-stack/ontology-server";
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
import { Effect } from "effect";
|
|
20
|
+
import type { OntologyBackendService } from "@party-stack/ontology-backend";
|
|
21
|
+
import type { OwnedBackendService } from "@party-stack/ontology-backend/owned";
|
|
22
|
+
import type { ReplicaService } from "@party-stack/ontology-replica";
|
|
23
|
+
import type { OntologyAuthenticate } from "@party-stack/ontology-server";
|
|
24
|
+
import type { OntologyIR } from "@party-stack/ontology-spec";
|
|
25
|
+
|
|
26
|
+
export const openServers = Effect.fn("App.openServers")(function* (options: {
|
|
27
|
+
readonly owned: OwnedBackendService;
|
|
28
|
+
readonly upstream: OntologyBackendService;
|
|
29
|
+
readonly ir: OntologyIR;
|
|
30
|
+
readonly readCache: ReplicaService;
|
|
31
|
+
readonly replica: ReplicaService;
|
|
32
|
+
readonly authenticate: OntologyAuthenticate;
|
|
33
|
+
}) {
|
|
34
|
+
const { owned, upstream, ir, readCache, replica, authenticate } = options;
|
|
35
|
+
|
|
36
|
+
// Authoritative storage; commits actions, receipts and journal atomically.
|
|
37
|
+
const ownedServer = yield* makeOntologyServer({
|
|
24
38
|
kind: "owned",
|
|
25
39
|
backend: owned,
|
|
26
40
|
authenticate,
|
|
27
41
|
});
|
|
28
42
|
|
|
29
|
-
// Direct forwarding, with no local outbox or client-session registry.
|
|
30
|
-
const proxyServer =
|
|
31
|
-
yield *
|
|
32
|
-
makeOntologyServer({
|
|
43
|
+
// Direct forwarding, with no local outbox or client-session registry.
|
|
44
|
+
const proxyServer = yield* makeOntologyServer({
|
|
33
45
|
kind: "proxy",
|
|
34
46
|
backend: upstream,
|
|
35
47
|
ir,
|
|
36
48
|
authenticate,
|
|
37
49
|
});
|
|
38
50
|
|
|
39
|
-
// Confirmed read cache; writes still forward directly to upstream.
|
|
40
|
-
// Configure readCache with makeReplica({ ..., demand: { idleRetentionMs: 60_000, maxSubsets: 128 } }).
|
|
41
|
-
const cachedServer =
|
|
42
|
-
yield *
|
|
43
|
-
makeOntologyServer({
|
|
51
|
+
// Confirmed read cache; writes still forward directly to upstream.
|
|
52
|
+
// Configure readCache with makeReplica({ ..., demand: { idleRetentionMs: 60_000, maxSubsets: 128 } }).
|
|
53
|
+
const cachedServer = yield* makeOntologyServer({
|
|
44
54
|
kind: "cached-proxy",
|
|
45
55
|
backend: upstream,
|
|
46
56
|
cache: readCache,
|
|
47
57
|
authenticate,
|
|
48
58
|
});
|
|
49
59
|
|
|
50
|
-
// Intermediate replica: accepts durable local actions into its own outbox.
|
|
51
|
-
const replicaServer =
|
|
52
|
-
yield *
|
|
53
|
-
makeOntologyServer({
|
|
60
|
+
// Intermediate replica: accepts durable local actions into its own outbox.
|
|
61
|
+
const replicaServer = yield* makeOntologyServer({
|
|
54
62
|
enableRealtime: true,
|
|
55
63
|
kind: "replica",
|
|
56
64
|
backend: upstream,
|
|
57
65
|
replica,
|
|
58
66
|
dangerouslyAllowInternalAccess: true,
|
|
59
67
|
});
|
|
68
|
+
return { ownedServer, proxyServer, cachedServer, replicaServer };
|
|
69
|
+
});
|
|
60
70
|
```
|
|
61
71
|
|
|
62
72
|
Configure several targets with the same constructor:
|
|
63
73
|
|
|
64
74
|
```ts
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
75
|
+
import { makeOntologyServer } from "@party-stack/ontology-server";
|
|
76
|
+
import { makeOntologyHttpHandler } from "@party-stack/ontology-server/rpc/http";
|
|
77
|
+
import { Effect } from "effect";
|
|
78
|
+
import type { BackendError, OntologyBackendService } from "@party-stack/ontology-backend";
|
|
79
|
+
import type { OwnedBackendService } from "@party-stack/ontology-backend/owned";
|
|
80
|
+
import type { OntologyAuthenticate } from "@party-stack/ontology-server";
|
|
81
|
+
import type { OntologyIR } from "@party-stack/ontology-spec";
|
|
82
|
+
import type { Scope } from "effect";
|
|
83
|
+
|
|
84
|
+
export const openHandler = Effect.fn("App.openHandler")(function* (options: {
|
|
85
|
+
readonly owned: OwnedBackendService;
|
|
86
|
+
readonly ir: OntologyIR;
|
|
87
|
+
readonly authenticate: OntologyAuthenticate;
|
|
88
|
+
readonly openForm: (formId: string) => Effect.Effect<OntologyBackendService, BackendError, Scope.Scope>;
|
|
89
|
+
}) {
|
|
90
|
+
const { owned, ir, authenticate, openForm } = options;
|
|
91
|
+
const server = yield* makeOntologyServer({
|
|
68
92
|
routes: {
|
|
69
93
|
issues: { kind: "owned", backend: owned, authenticate },
|
|
70
94
|
"forms/:formId": Effect.fnUntraced(function* ({ formId }: Readonly<Record<string, string>>) {
|
|
@@ -73,7 +97,9 @@ const server =
|
|
|
73
97
|
},
|
|
74
98
|
idleRetentionMs: 60_000,
|
|
75
99
|
});
|
|
76
|
-
const handleRequest = yield
|
|
100
|
+
const handleRequest = yield* makeOntologyHttpHandler(server);
|
|
101
|
+
return handleRequest;
|
|
102
|
+
});
|
|
77
103
|
```
|
|
78
104
|
|
|
79
105
|
Exact names use a map; parameter patterns are compiled at construction. Acquired
|
|
@@ -137,10 +163,22 @@ change feeds.
|
|
|
137
163
|
```ts
|
|
138
164
|
import { objectSubset } from "@party-stack/ontology-backend/subset";
|
|
139
165
|
import { makeOntologyHttpBackend } from "@party-stack/ontology-server/rpc/http";
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
166
|
+
import { Effect } from "effect";
|
|
167
|
+
import { FetchHttpClient } from "effect/http";
|
|
168
|
+
import type { ActionRequest } from "@party-stack/ontology-backend";
|
|
169
|
+
import type { Query } from "@party-stack/ontology-storage";
|
|
170
|
+
|
|
171
|
+
export const readAndApply = Effect.fn("App.readAndApply")(function* (options: {
|
|
172
|
+
readonly url: string;
|
|
173
|
+
readonly query: Query;
|
|
174
|
+
readonly invocation: ActionRequest;
|
|
175
|
+
}) {
|
|
176
|
+
const { url, query, invocation } = options;
|
|
177
|
+
const backend = yield* makeOntologyHttpBackend({ url }).pipe(Effect.provide(FetchHttpClient.layer));
|
|
178
|
+
const snapshot = yield* backend.loadSubset(objectSubset("Issue", query));
|
|
179
|
+
const receipt = yield* backend.applyAction(invocation);
|
|
180
|
+
return { snapshot, receipt };
|
|
181
|
+
});
|
|
144
182
|
```
|
|
145
183
|
|
|
146
184
|
Mount the endpoint so it accepts the trailing slash appended by Effect's HTTP
|
|
@@ -186,15 +224,24 @@ active requests and streamed responses. `ir.policy` remains the domain permissio
|
|
|
186
224
|
contract for objects, properties, actions and query functions.
|
|
187
225
|
|
|
188
226
|
```ts
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
227
|
+
import { makeOntologyServer } from "@party-stack/ontology-server";
|
|
228
|
+
import { Effect } from "effect";
|
|
229
|
+
import type { OntologyBackendService } from "@party-stack/ontology-backend";
|
|
230
|
+
import type { OntologyAuthenticate } from "@party-stack/ontology-server";
|
|
231
|
+
import type { OntologyIR } from "@party-stack/ontology-spec";
|
|
232
|
+
|
|
233
|
+
export const openOwnedServer = Effect.fn("App.openOwnedServer")(function* (
|
|
234
|
+
backend: OntologyBackendService,
|
|
235
|
+
ir: OntologyIR,
|
|
236
|
+
authenticatedContext: OntologyAuthenticate
|
|
237
|
+
) {
|
|
238
|
+
return yield* makeOntologyServer({
|
|
192
239
|
kind: "owned",
|
|
193
240
|
backend,
|
|
194
241
|
ir,
|
|
195
242
|
authenticate: (request) => authenticatedContext(request),
|
|
196
|
-
})
|
|
197
|
-
);
|
|
243
|
+
});
|
|
244
|
+
});
|
|
198
245
|
```
|
|
199
246
|
|
|
200
247
|
`authenticatedContext` is the application's Effect returning values matching the
|
|
@@ -249,31 +296,45 @@ partition from those same values.
|
|
|
249
296
|
A gateway can replace the upstream context definition and policy with its own:
|
|
250
297
|
|
|
251
298
|
```ts
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
299
|
+
import { makeOntologyServer } from "@party-stack/ontology-server";
|
|
300
|
+
import { o } from "@party-stack/ontology-spec";
|
|
301
|
+
import { Effect } from "effect";
|
|
302
|
+
import type { OntologyBackendService } from "@party-stack/ontology-backend";
|
|
303
|
+
import type { ReplicaService } from "@party-stack/ontology-replica";
|
|
304
|
+
import type { OntologyIR } from "@party-stack/ontology-spec";
|
|
305
|
+
|
|
306
|
+
export const openApprovalGateway = Effect.fn("App.openApprovalGateway")(function* (options: {
|
|
307
|
+
readonly upstreamIR: OntologyIR;
|
|
308
|
+
readonly tokenPolicy: OntologyIR["policy"];
|
|
309
|
+
readonly upstream: OntologyBackendService;
|
|
310
|
+
readonly trustedServiceAccountCache: ReplicaService;
|
|
311
|
+
readonly verifiedToken: { readonly approvalId: string };
|
|
312
|
+
}) {
|
|
313
|
+
const { upstreamIR, tokenPolicy, upstream, trustedServiceAccountCache, verifiedToken } = options;
|
|
314
|
+
const tokenIR = {
|
|
315
|
+
...upstreamIR,
|
|
316
|
+
context: {
|
|
317
|
+
type: o.struct({
|
|
318
|
+
fields: [
|
|
319
|
+
{
|
|
320
|
+
name: "approval",
|
|
321
|
+
displayName: "Approval",
|
|
322
|
+
type: o.objectReference({ objectType: "PostApproval" }),
|
|
323
|
+
},
|
|
324
|
+
],
|
|
325
|
+
}),
|
|
326
|
+
partition: [["approval"]],
|
|
327
|
+
},
|
|
328
|
+
policy: tokenPolicy,
|
|
329
|
+
};
|
|
330
|
+
return yield* makeOntologyServer({
|
|
271
331
|
kind: "cached-proxy",
|
|
272
332
|
backend: upstream,
|
|
273
333
|
cache: trustedServiceAccountCache,
|
|
274
334
|
ir: tokenIR,
|
|
275
335
|
context: { approval: verifiedToken.approvalId },
|
|
276
336
|
});
|
|
337
|
+
});
|
|
277
338
|
```
|
|
278
339
|
|
|
279
340
|
Each policy `bind` hides its parameter from the public action schema and replaces
|
|
@@ -319,18 +380,30 @@ servers use the ordinary committed backend contract instead of speculative state
|
|
|
319
380
|
`./rpc/http` composes the same contract with Fetch handlers and streaming NDJSON:
|
|
320
381
|
|
|
321
382
|
```ts
|
|
383
|
+
import { replicaStateSource } from "@party-stack/ontology-replica/infrastructure";
|
|
322
384
|
import { makeOntologyHttpHandler } from "@party-stack/ontology-server/rpc/http";
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
385
|
+
import { Effect } from "effect";
|
|
386
|
+
import type { OntologyBackendService } from "@party-stack/ontology-backend";
|
|
387
|
+
import type { ReplicaService } from "@party-stack/ontology-replica";
|
|
388
|
+
import type { OntologyRpcServerOptions } from "@party-stack/ontology-server/rpc";
|
|
389
|
+
import type { OntologyIR } from "@party-stack/ontology-spec";
|
|
390
|
+
|
|
391
|
+
// Runs in the server's Scope; mount the returned handler at a dedicated HTTP route.
|
|
392
|
+
export const openReplicaHandler = Effect.fn("App.openReplicaHandler")(function* (options: {
|
|
393
|
+
readonly upstream: OntologyBackendService;
|
|
394
|
+
readonly replica: ReplicaService;
|
|
395
|
+
readonly ir: OntologyIR;
|
|
396
|
+
readonly authorizeRequest: OntologyRpcServerOptions["authorize"];
|
|
397
|
+
}) {
|
|
398
|
+
const { upstream, replica, ir, authorizeRequest } = options;
|
|
399
|
+
const handleRequest = yield* makeOntologyHttpHandler({
|
|
328
400
|
backend: upstream,
|
|
329
401
|
source: replicaStateSource(replica),
|
|
330
402
|
ir,
|
|
331
403
|
authorize: authorizeRequest,
|
|
332
404
|
});
|
|
333
|
-
|
|
405
|
+
return handleRequest;
|
|
406
|
+
});
|
|
334
407
|
```
|
|
335
408
|
|
|
336
409
|
```ts
|
|
@@ -338,14 +411,15 @@ import { makeOntologyHttpClient } from "@party-stack/ontology-server/rpc/http";
|
|
|
338
411
|
import { Effect } from "effect";
|
|
339
412
|
import { FetchHttpClient } from "effect/http";
|
|
340
413
|
|
|
341
|
-
//
|
|
342
|
-
const
|
|
343
|
-
yield
|
|
344
|
-
makeOntologyHttpClient({ url: "https://example.test/ontology/rpc" }).pipe(
|
|
414
|
+
// Runs in the client's Scope.
|
|
415
|
+
export const openClient = Effect.gen(function* () {
|
|
416
|
+
const client = yield* makeOntologyHttpClient({ url: "https://example.test/ontology/rpc" }).pipe(
|
|
345
417
|
Effect.provide(FetchHttpClient.layer)
|
|
346
418
|
);
|
|
347
|
-
// makeLiveOntology({ ir, client: Effect.succeed(client) }) composes
|
|
348
|
-
// the correct replica path with normal local row and blob storage.
|
|
419
|
+
// makeLiveOntology({ ir, client: Effect.succeed(client) }) composes
|
|
420
|
+
// the correct replica path with normal local row and blob storage.
|
|
421
|
+
return client;
|
|
422
|
+
});
|
|
349
423
|
```
|
|
350
424
|
|
|
351
425
|
The HttpClient service supplies authentication/fetch behavior; its Layer constructs
|
|
@@ -382,23 +456,31 @@ method envelope. Supply the browser platform Layer on each side:
|
|
|
382
456
|
```ts
|
|
383
457
|
import * as BrowserWorker from "@effect/platform-browser/BrowserWorker";
|
|
384
458
|
import { makeOntologyWorkerClient } from "@party-stack/ontology-server/rpc/worker";
|
|
459
|
+
import { Effect } from "effect";
|
|
385
460
|
|
|
386
|
-
//
|
|
387
|
-
const
|
|
461
|
+
// Runs in the tab's long-lived Scope.
|
|
462
|
+
export const connectToWorker = Effect.fn("App.connectToWorker")(function* (worker: Worker) {
|
|
463
|
+
return yield* makeOntologyWorkerClient().pipe(Effect.provide(BrowserWorker.layer(() => worker)));
|
|
464
|
+
});
|
|
388
465
|
```
|
|
389
466
|
|
|
390
467
|
```ts
|
|
391
468
|
import * as BrowserWorkerRunner from "@effect/platform-browser/BrowserWorkerRunner";
|
|
392
469
|
import { serveOntologyWorker } from "@party-stack/ontology-server/rpc/worker";
|
|
393
|
-
import { Fiber } from "effect";
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
470
|
+
import { Effect, Fiber } from "effect";
|
|
471
|
+
import type { OntologyRpcServerOptions } from "@party-stack/ontology-server/rpc";
|
|
472
|
+
|
|
473
|
+
// Runs in the owning worker's Scope.
|
|
474
|
+
export const serveWorker = Effect.fn("Worker.serve")(function* (options: {
|
|
475
|
+
readonly source: OntologyRpcServerOptions["source"];
|
|
476
|
+
readonly authorize: OntologyRpcServerOptions["authorize"];
|
|
477
|
+
}) {
|
|
478
|
+
const { source, authorize } = options;
|
|
479
|
+
const server = yield* serveOntologyWorker({ source, authorize, enableRealtime: true }).pipe(
|
|
399
480
|
Effect.provide(BrowserWorkerRunner.layer)
|
|
400
481
|
);
|
|
401
|
-
yield
|
|
482
|
+
yield* Fiber.join(server);
|
|
483
|
+
});
|
|
402
484
|
```
|
|
403
485
|
|
|
404
486
|
The server fiber is owned by the calling Scope. Separate port servers may also use
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"definitionProjection.d.ts","names":[],"sources":["../src/definitionProjection.ts"],"mappings":";;YAgBY,4BAA4B;YAE5B,2BAA2B,iBAAiB,qBAAqB,yBACxE,kBAAkB,cAAc,uCAEzB,iBAAiB,cAAc,wBAAwB,0CACvD;
|
|
1
|
+
{"version":3,"file":"definitionProjection.d.ts","names":[],"sources":["../src/definitionProjection.ts"],"mappings":";;YAgBY,4BAA4B;YAE5B,2BAA2B,iBAAiB,qBAAqB,yBACxE,kBAAkB,cAAc,uCAEzB,iBAAiB,cAAc,wBAAwB,0CACvD;wBAigBI,0BAA0B,iBAAiB,qBAAqB,oBAAoB;EAChG,IAAI;EAEJ,UAAU;EAEV,6BAA6B,2BAA2B;EACxD;EACA,6BAA6B;;;;;;EAM7B;EACA;EACA;IACA"}
|
|
@@ -167,13 +167,14 @@ function projectExpression(opts) {
|
|
|
167
167
|
}
|
|
168
168
|
} : void 0;
|
|
169
169
|
}
|
|
170
|
-
case "not":
|
|
170
|
+
case "not":
|
|
171
|
+
case "length": {
|
|
171
172
|
const expression = projectExpression({
|
|
172
173
|
...opts,
|
|
173
174
|
expression: opts.expression.value.expression
|
|
174
175
|
});
|
|
175
176
|
return expression ? {
|
|
176
|
-
kind:
|
|
177
|
+
kind: opts.expression.kind,
|
|
177
178
|
value: { expression }
|
|
178
179
|
} : void 0;
|
|
179
180
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"definitionProjection.js","names":["resolveType"],"sources":["../src/definitionProjection.ts"],"sourcesContent":["import {\n isRelatedFromForeignKeySource,\n resolveRelatedLink,\n resolveLookupReference,\n} from \"@party-stack/ontology-spec/utils\";\nimport type {\n ActionLogicStep,\n ActionTypeDef,\n Expression,\n OntologyDefinition,\n OntologyIR,\n PropertyAssignment,\n TypeDef,\n InputReferenceExpression,\n} from \"@party-stack/ontology-spec\";\n\nexport type FixedActionParameterValue = Expression;\n\nexport type FixedActionParameterValues<Ontology extends OntologyDefinition = OntologyDefinition> = {\n [ActionTypeName in Extract<keyof Ontology[\"actionTypes\"], string>]?: {\n [\n ParameterName in Extract<keyof Ontology[\"actionTypes\"][ActionTypeName][\"parameters\"], string>\n ]?: FixedActionParameterValue;\n };\n};\n\ntype RuntimeFixedActionParameterValues = Record<\n string,\n Record<string, FixedActionParameterValue> | undefined\n>;\n\nfunction hasPath(value: unknown, path: string[]): boolean {\n let current = value;\n for (const segment of path) {\n if (typeof current !== \"object\" || current === null || !(segment in current)) return false;\n current = (current as Record<string, unknown>)[segment];\n }\n return true;\n}\n\nfunction resolveType(ir: OntologyIR, type: TypeDef): TypeDef {\n if (type.kind !== \"ref\") return type;\n const namedType = ir.types.find((candidate) => candidate.name === type.value.name);\n if (!namedType) {\n throw new Error(`Unknown ontology type \"${type.value.name}\".`);\n }\n return resolveType(ir, namedType.type);\n}\n\nfunction getActionType(ir: OntologyIR, actionTypeName: string): ActionTypeDef {\n const actionType = ir.actionTypes.find((candidate) => candidate.name === actionTypeName);\n if (!actionType) {\n throw new Error(`Unknown action type \"${actionTypeName}\".`);\n }\n return actionType;\n}\n\nfunction getObjectReferenceObjectType(\n ir: OntologyIR,\n actionType: ActionTypeDef,\n reference: InputReferenceExpression\n): string {\n const parameter = actionType.parameters.find((candidate) => candidate.name === reference.name);\n if (!parameter) {\n throw new Error(`Unknown action parameter \"${reference.name}\".`);\n }\n const type = resolveType(ir, parameter.type);\n if (type.kind !== \"objectReference\") {\n throw new Error(`Action parameter \"${parameter.name}\" is not an object reference.`);\n }\n return type.value.objectType;\n}\n\nfunction getFixedActionParameterValues(\n fixedActionParameterValues: FixedActionParameterValues | undefined,\n actionName: string\n): Record<string, FixedActionParameterValue> | undefined {\n return (fixedActionParameterValues as RuntimeFixedActionParameterValues | undefined)?.[actionName];\n}\n\nfunction isActionParameterFixed(\n fixedActionParameterValues: FixedActionParameterValues | undefined,\n actionName: string,\n parameterName: string\n): boolean {\n return (\n getFixedActionParameterValues(fixedActionParameterValues, actionName)?.[parameterName] !== undefined\n );\n}\n\nfunction getVisibleActionParameterNames(\n actionTypeName: string,\n parameters: ReadonlyArray<{ name: string }>,\n fixedActionParameterValues?: FixedActionParameterValues\n): Set<string> {\n return new Set(\n parameters\n .filter(\n (parameter) =>\n !isActionParameterFixed(fixedActionParameterValues, actionTypeName, parameter.name)\n )\n .map((parameter) => parameter.name)\n );\n}\n\nfunction getExpressionObjectType(\n ir: OntologyIR,\n actionName: string,\n expression: Expression\n): string | undefined {\n if (expression.kind === \"lookup\") {\n return resolveLookupReference(\n ir,\n expression.value.reference,\n getActionType(ir, actionName).parameters\n )?.objectType;\n }\n if (expression.kind === \"related\") {\n const sourceObjectType = getExpressionObjectType(ir, actionName, expression.value.source);\n if (!sourceObjectType) return undefined;\n const link = resolveRelatedLink(ir, sourceObjectType, expression.value.link);\n if (!link) return undefined;\n const foreignKeyOnSource = isRelatedFromForeignKeySource(\n link,\n sourceObjectType,\n expression.value.link\n );\n if (!foreignKeyOnSource && link.cardinality === \"many\") {\n return undefined;\n }\n return foreignKeyOnSource ? link.target.objectType : link.source.objectType;\n }\n return undefined;\n}\n\nfunction projectExpression(opts: {\n expression: Expression;\n\n context: Record<string, unknown> | undefined;\n\n ir: OntologyIR;\n actionName: string;\n visibleParameters: Set<string>;\n fixedActionParameterValues: FixedActionParameterValues | undefined;\n allowedObjectTypeProperties: Record<string, readonly string[]>;\n projectFixedParameterValues: boolean;\n}): Expression | undefined {\n switch (opts.expression.kind) {\n case \"contextReference\": {\n const { name } = opts.expression.value;\n // Declared context is the public contract. An absent optional value\n // is still available as undefined (e.g. COALESCE's fallback).\n let type = opts.ir.context?.type;\n while (type) {\n type = resolveType(opts.ir, type);\n if (type.kind !== \"optional\") break;\n type = type.value.type;\n }\n const visible = type\n ? type.kind === \"struct\" && type.value.fields.some((field) => field.name === name)\n : hasPath(opts.context, [name]);\n return visible ? opts.expression : undefined;\n }\n case \"literal\":\n case \"uuid\":\n case \"now\":\n case \"localReference\":\n return opts.expression;\n case \"getAt\": {\n const objectType = getExpressionObjectType(\n opts.ir,\n opts.actionName,\n opts.expression.value.source\n );\n if (\n objectType &&\n !(opts.allowedObjectTypeProperties[objectType] ?? []).includes(opts.expression.value.path[0]!)\n ) {\n return undefined;\n }\n const source = projectExpression({\n ...opts,\n expression: opts.expression.value.source,\n });\n return source\n ? {\n kind: \"getAt\",\n value: {\n source,\n path: opts.expression.value.path,\n },\n }\n : undefined;\n }\n case \"lookup\": {\n const reference = projectExpression({\n ...opts,\n expression: opts.expression.value.reference,\n });\n return reference\n ? {\n kind: \"lookup\",\n value: { reference },\n }\n : undefined;\n }\n case \"related\": {\n const source = projectExpression({\n ...opts,\n expression: opts.expression.value.source,\n });\n return source && getExpressionObjectType(opts.ir, opts.actionName, opts.expression)\n ? {\n kind: \"related\",\n value: {\n source,\n link: opts.expression.value.link,\n },\n }\n : undefined;\n }\n case \"struct\": {\n const fields = opts.expression.value.fields.map((field) => ({\n ...field,\n value: projectExpression({\n ...opts,\n expression: field.value,\n }),\n }));\n return fields.some((field) => field.value === undefined)\n ? undefined\n : {\n kind: \"struct\",\n value: {\n fields: fields as Array<{\n name: string;\n value: Expression;\n }>,\n },\n };\n }\n case \"map\": {\n const source = projectExpression({\n ...opts,\n expression: opts.expression.value.source,\n });\n const body = projectExpression({\n ...opts,\n expression: opts.expression.value.body,\n });\n return source && body\n ? {\n kind: \"map\",\n value: {\n source,\n binding: opts.expression.value.binding,\n body,\n },\n }\n : undefined;\n }\n case \"inputReference\": {\n const parameterName = opts.expression.value.name;\n if (opts.visibleParameters.has(parameterName)) {\n return opts.expression;\n }\n if (!opts.projectFixedParameterValues) {\n return undefined;\n }\n const fixedValue = getFixedActionParameterValues(\n opts.fixedActionParameterValues,\n opts.actionName\n )?.[parameterName];\n if (fixedValue === undefined) return undefined;\n if (fixedValue.kind === \"inputReference\" && fixedValue.value.name === parameterName) {\n return undefined;\n }\n return projectExpression({\n ...opts,\n expression: fixedValue,\n });\n }\n case \"case\": {\n const branches = opts.expression.value.branches.map((branch) => ({\n condition: projectExpression({ ...opts, expression: branch.condition }),\n result: projectExpression({ ...opts, expression: branch.result }),\n }));\n const otherwise = projectExpression({ ...opts, expression: opts.expression.value.else });\n return otherwise && branches.every((b) => b.condition && b.result)\n ? {\n kind: \"case\",\n value: {\n branches: branches as { condition: Expression; result: Expression }[],\n else: otherwise,\n },\n }\n : undefined;\n }\n case \"not\": {\n const expression = projectExpression({ ...opts, expression: opts.expression.value.expression });\n return expression ? { kind: \"not\", value: { expression } } : undefined;\n }\n case \"and\":\n case \"or\":\n case \"concat\":\n case \"coalesce\": {\n const expressions = opts.expression.value.expressions.map((expression) =>\n projectExpression({ ...opts, expression })\n );\n return expressions.every((e) => e !== undefined)\n ? { kind: opts.expression.kind, value: { expressions: expressions as Expression[] } }\n : undefined;\n }\n case \"eq\":\n case \"ne\":\n case \"lt\":\n case \"lte\":\n case \"gt\":\n case \"gte\":\n case \"in\":\n case \"contains\":\n case \"like\":\n case \"ilike\":\n case \"add\":\n case \"subtract\":\n case \"multiply\":\n case \"divide\": {\n const left = projectExpression({ ...opts, expression: opts.expression.value.left });\n const right = projectExpression({ ...opts, expression: opts.expression.value.right });\n return left && right ? { kind: opts.expression.kind, value: { left, right } } : undefined;\n }\n case \"exists\":\n return undefined;\n }\n}\n\nfunction projectAssignments(opts: {\n assignments: PropertyAssignment[];\n\n context: Record<string, unknown> | undefined;\n\n ir: OntologyIR;\n actionName: string;\n objectTypeName: string;\n visibleParameters: Set<string>;\n fixedActionParameterValues: FixedActionParameterValues | undefined;\n allowedObjectTypeProperties: Record<string, readonly string[]>;\n}): PropertyAssignment[] {\n const allowedProperties = opts.allowedObjectTypeProperties[opts.objectTypeName] ?? [];\n return opts.assignments.flatMap((assignment) => {\n const propertyName = assignment.property[0];\n if (!propertyName || !allowedProperties.includes(propertyName)) return [];\n const projectedValue = projectExpression({\n expression: assignment.value,\n\n context: opts.context,\n\n ir: opts.ir,\n actionName: opts.actionName,\n visibleParameters: opts.visibleParameters,\n fixedActionParameterValues: opts.fixedActionParameterValues,\n allowedObjectTypeProperties: opts.allowedObjectTypeProperties,\n projectFixedParameterValues: true,\n });\n return projectedValue ? [{ ...assignment, value: projectedValue }] : [];\n });\n}\n\nfunction projectLogicStep(opts: {\n step: ActionLogicStep;\n\n context: Record<string, unknown> | undefined;\n\n ir: OntologyIR;\n actionType: ActionTypeDef;\n visibleParameters: Set<string>;\n fixedActionParameterValues: FixedActionParameterValues | undefined;\n allowedObjectTypeProperties: Record<string, readonly string[]>;\n}): ActionLogicStep | undefined {\n switch (opts.step.kind) {\n case \"createObject\": {\n const values = projectAssignments({\n assignments: opts.step.value.values,\n\n context: opts.context,\n\n ir: opts.ir,\n actionName: opts.actionType.name,\n objectTypeName: opts.step.value.objectType,\n visibleParameters: opts.visibleParameters,\n fixedActionParameterValues: opts.fixedActionParameterValues,\n allowedObjectTypeProperties: opts.allowedObjectTypeProperties,\n });\n return {\n ...opts.step,\n value: {\n ...opts.step.value,\n values,\n },\n };\n }\n case \"updateObject\": {\n const objectTypeName = getObjectReferenceObjectType(\n opts.ir,\n opts.actionType,\n opts.step.value.object\n );\n const values = projectAssignments({\n assignments: opts.step.value.values,\n\n context: opts.context,\n\n ir: opts.ir,\n actionName: opts.actionType.name,\n objectTypeName,\n visibleParameters: opts.visibleParameters,\n fixedActionParameterValues: opts.fixedActionParameterValues,\n allowedObjectTypeProperties: opts.allowedObjectTypeProperties,\n });\n return {\n ...opts.step,\n value: {\n ...opts.step.value,\n values,\n },\n };\n }\n case \"deleteObject\":\n return opts.step;\n }\n}\n\nfunction typeReferencesObjectTypes(type: TypeDef, ir: OntologyIR, seen = new Set<string>()): Set<string> {\n if (type.kind === \"ref\") {\n if (seen.has(type.value.name)) return new Set();\n seen.add(type.value.name);\n const named = ir.types.find((candidate) => candidate.name === type.value.name);\n return named ? typeReferencesObjectTypes(named.type, ir, seen) : new Set();\n }\n switch (type.kind) {\n case \"objectReference\":\n return new Set([type.value.objectType]);\n case \"optional\":\n return typeReferencesObjectTypes(type.value.type, ir, seen);\n case \"list\":\n return typeReferencesObjectTypes(type.value.elementType, ir, seen);\n case \"map\": {\n const names = typeReferencesObjectTypes(type.value.keyType, ir, seen);\n for (const name of typeReferencesObjectTypes(type.value.valueType, ir, seen)) names.add(name);\n return names;\n }\n case \"result\": {\n const names = typeReferencesObjectTypes(type.value.okType, ir, seen);\n for (const name of typeReferencesObjectTypes(type.value.errType, ir, seen)) names.add(name);\n return names;\n }\n case \"struct\": {\n const names = new Set<string>();\n for (const field of type.value.fields) {\n for (const name of typeReferencesObjectTypes(field.type, ir, seen)) names.add(name);\n }\n return names;\n }\n case \"union\": {\n const names = new Set<string>();\n for (const variant of type.value.variants) {\n for (const name of typeReferencesObjectTypes(variant.type, ir, seen)) names.add(name);\n }\n return names;\n }\n default:\n return new Set();\n }\n}\n\nfunction collectReferencedNamedTypes(\n type: TypeDef,\n ir: OntologyIR,\n into: Set<string>,\n seen = new Set<string>()\n): void {\n if (type.kind === \"ref\") {\n if (seen.has(type.value.name)) return;\n seen.add(type.value.name);\n into.add(type.value.name);\n const named = ir.types.find((candidate) => candidate.name === type.value.name);\n if (named) collectReferencedNamedTypes(named.type, ir, into, seen);\n return;\n }\n switch (type.kind) {\n case \"optional\":\n collectReferencedNamedTypes(type.value.type, ir, into, seen);\n break;\n case \"list\":\n collectReferencedNamedTypes(type.value.elementType, ir, into, seen);\n break;\n case \"map\":\n collectReferencedNamedTypes(type.value.keyType, ir, into, seen);\n collectReferencedNamedTypes(type.value.valueType, ir, into, seen);\n break;\n case \"result\":\n collectReferencedNamedTypes(type.value.okType, ir, into, seen);\n collectReferencedNamedTypes(type.value.errType, ir, into, seen);\n break;\n case \"struct\":\n for (const field of type.value.fields) {\n collectReferencedNamedTypes(field.type, ir, into, seen);\n }\n break;\n case \"union\":\n for (const variant of type.value.variants) {\n collectReferencedNamedTypes(variant.type, ir, into, seen);\n }\n break;\n default:\n break;\n }\n}\n\nfunction actionParametersReferenceHiddenObjectType(\n actionType: ActionTypeDef,\n ir: OntologyIR,\n visibleObjectTypes: Set<string>,\n visibleParameters?: Set<string>\n): boolean {\n for (const parameter of actionType.parameters) {\n if (visibleParameters && !visibleParameters.has(parameter.name)) continue;\n for (const objectType of typeReferencesObjectTypes(parameter.type, ir)) {\n if (!visibleObjectTypes.has(objectType)) return true;\n }\n }\n return false;\n}\n\nexport function projectOntologyDefinition<Ontology extends OntologyDefinition = OntologyDefinition>(opts: {\n ir: OntologyIR;\n\n context?: Record<string, unknown>;\n\n fixedActionParameterValues?: FixedActionParameterValues<Ontology>;\n projectFixedActionParameterValuesInDefaults?: boolean;\n allowedObjectTypeProperties: Record<string, readonly string[]>;\n /**\n * Projects object/property/link/action/query visibility from the resolved\n * authorization policy. Otherwise the full schema is retained while action\n * parameters and logic are still projected.\n */\n filterSchemaByAuthorization?: boolean;\n visibleActionTypes?: readonly string[] | \"all\";\n visibleQueryFunctionTypes?: readonly string[] | \"all\";\n}): OntologyIR {\n const filterSchemaByAuthorization = opts.filterSchemaByAuthorization ?? false;\n const visibleObjectTypes = new Set(\n filterSchemaByAuthorization\n ? Object.entries(opts.allowedObjectTypeProperties)\n .filter(([, properties]) => properties.length > 0)\n .map(([objectType]) => objectType)\n : opts.ir.objectTypes.map((objectType) => objectType.name)\n );\n\n const objectTypes = filterSchemaByAuthorization\n ? opts.ir.objectTypes\n .filter((objectType) => visibleObjectTypes.has(objectType.name))\n .map((objectType) => {\n const allowed = new Set(opts.allowedObjectTypeProperties[objectType.name] ?? []);\n return {\n ...objectType,\n title: objectType.title && allowed.has(objectType.title) ? objectType.title : undefined,\n properties: objectType.properties.filter((property) => allowed.has(property.name)),\n };\n })\n : opts.ir.objectTypes;\n\n const linkTypes = filterSchemaByAuthorization\n ? opts.ir.linkTypes.filter((link) => {\n if (\n !visibleObjectTypes.has(link.source.objectType) ||\n !visibleObjectTypes.has(link.target.objectType)\n ) {\n return false;\n }\n if (!link.foreignKey) return true;\n const sourceAllowed = new Set(opts.allowedObjectTypeProperties[link.source.objectType] ?? []);\n const targetAllowed = new Set(opts.allowedObjectTypeProperties[link.target.objectType] ?? []);\n return sourceAllowed.has(link.foreignKey) || targetAllowed.has(link.foreignKey);\n })\n : opts.ir.linkTypes;\n\n const visibleActions =\n opts.visibleActionTypes === undefined || opts.visibleActionTypes === \"all\"\n ? opts.ir.actionTypes\n : opts.ir.actionTypes.filter((actionType) =>\n (opts.visibleActionTypes as readonly string[]).includes(actionType.name)\n );\n\n const actionTypes = visibleActions\n .filter(\n (actionType) =>\n !filterSchemaByAuthorization ||\n !actionParametersReferenceHiddenObjectType(\n actionType,\n opts.ir,\n visibleObjectTypes,\n getVisibleActionParameterNames(\n actionType.name,\n actionType.parameters,\n opts.fixedActionParameterValues\n )\n )\n )\n .map((actionType) => {\n const visibleParameters = getVisibleActionParameterNames(\n actionType.name,\n actionType.parameters,\n opts.fixedActionParameterValues\n );\n return {\n ...actionType,\n parameters: actionType.parameters\n .filter((parameter) => visibleParameters.has(parameter.name))\n .map((parameter) => ({\n ...parameter,\n defaultValue: parameter.defaultValue\n ? projectExpression({\n expression: parameter.defaultValue,\n\n context: opts.context,\n\n ir: opts.ir,\n actionName: actionType.name,\n visibleParameters,\n fixedActionParameterValues: opts.fixedActionParameterValues,\n allowedObjectTypeProperties: opts.allowedObjectTypeProperties,\n projectFixedParameterValues:\n opts.projectFixedActionParameterValuesInDefaults ?? false,\n })\n : undefined,\n })),\n logic: actionType.logic.flatMap((step) => {\n if (\n filterSchemaByAuthorization &&\n step.kind === \"createObject\" &&\n !visibleObjectTypes.has(step.value.objectType)\n ) {\n return [];\n }\n const projectedStep = projectLogicStep({\n step,\n\n context: opts.context,\n\n ir: opts.ir,\n actionType,\n visibleParameters,\n fixedActionParameterValues: opts.fixedActionParameterValues,\n allowedObjectTypeProperties: opts.allowedObjectTypeProperties,\n });\n return projectedStep ? [projectedStep] : [];\n }),\n };\n });\n\n const queryFunctionTypes =\n opts.visibleQueryFunctionTypes === undefined || opts.visibleQueryFunctionTypes === \"all\"\n ? opts.ir.queryFunctionTypes.filter((queryFunction) => {\n if (!filterSchemaByAuthorization) return true;\n for (const parameter of queryFunction.parameters) {\n for (const objectType of typeReferencesObjectTypes(parameter.type, opts.ir)) {\n if (!visibleObjectTypes.has(objectType)) return false;\n }\n }\n for (const objectType of typeReferencesObjectTypes(queryFunction.returnType, opts.ir)) {\n if (!visibleObjectTypes.has(objectType)) return false;\n }\n return true;\n })\n : opts.ir.queryFunctionTypes.filter((queryFunction) =>\n (opts.visibleQueryFunctionTypes as readonly string[]).includes(queryFunction.name)\n );\n\n const referencedNamedTypes = new Set<string>();\n if (opts.ir.context?.type)\n collectReferencedNamedTypes(opts.ir.context.type, opts.ir, referencedNamedTypes);\n for (const objectType of objectTypes) {\n for (const property of objectType.properties) {\n collectReferencedNamedTypes(property.type, opts.ir, referencedNamedTypes);\n }\n }\n for (const actionType of actionTypes) {\n for (const parameter of actionType.parameters) {\n collectReferencedNamedTypes(parameter.type, opts.ir, referencedNamedTypes);\n }\n if (actionType.returnType)\n collectReferencedNamedTypes(actionType.returnType, opts.ir, referencedNamedTypes);\n }\n for (const queryFunction of queryFunctionTypes) {\n for (const parameter of queryFunction.parameters) {\n collectReferencedNamedTypes(parameter.type, opts.ir, referencedNamedTypes);\n }\n collectReferencedNamedTypes(queryFunction.returnType, opts.ir, referencedNamedTypes);\n }\n\n return {\n ...opts.ir,\n types: filterSchemaByAuthorization\n ? opts.ir.types.filter((type) => referencedNamedTypes.has(type.name))\n : opts.ir.types,\n objectTypes,\n linkTypes,\n actionTypes,\n queryFunctionTypes,\n };\n}\n"],"mappings":";;AA+BA,SAAS,QAAQ,OAAgB,MAAyB;CACtD,IAAI,UAAU;CACd,KAAK,MAAM,WAAW,MAAM;EACxB,IAAI,OAAO,YAAY,YAAY,YAAY,QA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AAM,QAAQ,SAAS,qBAAqB,IAAI,KAAK,IAAI,CAAC,IAClE,KAAK,GAAG;EACd;EACA;EACA;EACA;CACJ;AACJ"}
|
|
1
|
+
{"version":3,"file":"definitionProjection.js","names":["resolveType"],"sources":["../src/definitionProjection.ts"],"sourcesContent":["import {\n isRelatedFromForeignKeySource,\n resolveRelatedLink,\n resolveLookupReference,\n} from \"@party-stack/ontology-spec/utils\";\nimport type {\n ActionLogicStep,\n ActionTypeDef,\n Expression,\n OntologyDefinition,\n OntologyIR,\n PropertyAssignment,\n TypeDef,\n InputReferenceExpression,\n} from \"@party-stack/ontology-spec\";\n\nexport type FixedActionParameterValue = Expression;\n\nexport type FixedActionParameterValues<Ontology extends OntologyDefinition = OntologyDefinition> = {\n [ActionTypeName in Extract<keyof Ontology[\"actionTypes\"], string>]?: {\n [\n ParameterName in Extract<keyof Ontology[\"actionTypes\"][ActionTypeName][\"parameters\"], string>\n ]?: FixedActionParameterValue;\n };\n};\n\ntype RuntimeFixedActionParameterValues = Record<\n string,\n Record<string, FixedActionParameterValue> | undefined\n>;\n\nfunction hasPath(value: unknown, path: string[]): boolean {\n let current = value;\n for (const segment of path) {\n if (typeof current !== \"object\" || current === null || !(segment in current)) return false;\n current = (current as Record<string, unknown>)[segment];\n }\n return true;\n}\n\nfunction resolveType(ir: OntologyIR, type: TypeDef): TypeDef {\n if (type.kind !== \"ref\") return type;\n const namedType = ir.types.find((candidate) => candidate.name === type.value.name);\n if (!namedType) {\n throw new Error(`Unknown ontology type \"${type.value.name}\".`);\n }\n return resolveType(ir, namedType.type);\n}\n\nfunction getActionType(ir: OntologyIR, actionTypeName: string): ActionTypeDef {\n const actionType = ir.actionTypes.find((candidate) => candidate.name === actionTypeName);\n if (!actionType) {\n throw new Error(`Unknown action type \"${actionTypeName}\".`);\n }\n return actionType;\n}\n\nfunction getObjectReferenceObjectType(\n ir: OntologyIR,\n actionType: ActionTypeDef,\n reference: InputReferenceExpression\n): string {\n const parameter = actionType.parameters.find((candidate) => candidate.name === reference.name);\n if (!parameter) {\n throw new Error(`Unknown action parameter \"${reference.name}\".`);\n }\n const type = resolveType(ir, parameter.type);\n if (type.kind !== \"objectReference\") {\n throw new Error(`Action parameter \"${parameter.name}\" is not an object reference.`);\n }\n return type.value.objectType;\n}\n\nfunction getFixedActionParameterValues(\n fixedActionParameterValues: FixedActionParameterValues | undefined,\n actionName: string\n): Record<string, FixedActionParameterValue> | undefined {\n return (fixedActionParameterValues as RuntimeFixedActionParameterValues | undefined)?.[actionName];\n}\n\nfunction isActionParameterFixed(\n fixedActionParameterValues: FixedActionParameterValues | undefined,\n actionName: string,\n parameterName: string\n): boolean {\n return (\n getFixedActionParameterValues(fixedActionParameterValues, actionName)?.[parameterName] !== undefined\n );\n}\n\nfunction getVisibleActionParameterNames(\n actionTypeName: string,\n parameters: ReadonlyArray<{ name: string }>,\n fixedActionParameterValues?: FixedActionParameterValues\n): Set<string> {\n return new Set(\n parameters\n .filter(\n (parameter) =>\n !isActionParameterFixed(fixedActionParameterValues, actionTypeName, parameter.name)\n )\n .map((parameter) => parameter.name)\n );\n}\n\nfunction getExpressionObjectType(\n ir: OntologyIR,\n actionName: string,\n expression: Expression\n): string | undefined {\n if (expression.kind === \"lookup\") {\n return resolveLookupReference(\n ir,\n expression.value.reference,\n getActionType(ir, actionName).parameters\n )?.objectType;\n }\n if (expression.kind === \"related\") {\n const sourceObjectType = getExpressionObjectType(ir, actionName, expression.value.source);\n if (!sourceObjectType) return undefined;\n const link = resolveRelatedLink(ir, sourceObjectType, expression.value.link);\n if (!link) return undefined;\n const foreignKeyOnSource = isRelatedFromForeignKeySource(\n link,\n sourceObjectType,\n expression.value.link\n );\n if (!foreignKeyOnSource && link.cardinality === \"many\") {\n return undefined;\n }\n return foreignKeyOnSource ? link.target.objectType : link.source.objectType;\n }\n return undefined;\n}\n\nfunction projectExpression(opts: {\n expression: Expression;\n\n context: Record<string, unknown> | undefined;\n\n ir: OntologyIR;\n actionName: string;\n visibleParameters: Set<string>;\n fixedActionParameterValues: FixedActionParameterValues | undefined;\n allowedObjectTypeProperties: Record<string, readonly string[]>;\n projectFixedParameterValues: boolean;\n}): Expression | undefined {\n switch (opts.expression.kind) {\n case \"contextReference\": {\n const { name } = opts.expression.value;\n // Declared context is the public contract. An absent optional value\n // is still available as undefined (e.g. COALESCE's fallback).\n let type = opts.ir.context?.type;\n while (type) {\n type = resolveType(opts.ir, type);\n if (type.kind !== \"optional\") break;\n type = type.value.type;\n }\n const visible = type\n ? type.kind === \"struct\" && type.value.fields.some((field) => field.name === name)\n : hasPath(opts.context, [name]);\n return visible ? opts.expression : undefined;\n }\n case \"literal\":\n case \"uuid\":\n case \"now\":\n case \"localReference\":\n return opts.expression;\n case \"getAt\": {\n const objectType = getExpressionObjectType(\n opts.ir,\n opts.actionName,\n opts.expression.value.source\n );\n if (\n objectType &&\n !(opts.allowedObjectTypeProperties[objectType] ?? []).includes(opts.expression.value.path[0]!)\n ) {\n return undefined;\n }\n const source = projectExpression({\n ...opts,\n expression: opts.expression.value.source,\n });\n return source\n ? {\n kind: \"getAt\",\n value: {\n source,\n path: opts.expression.value.path,\n },\n }\n : undefined;\n }\n case \"lookup\": {\n const reference = projectExpression({\n ...opts,\n expression: opts.expression.value.reference,\n });\n return reference\n ? {\n kind: \"lookup\",\n value: { reference },\n }\n : undefined;\n }\n case \"related\": {\n const source = projectExpression({\n ...opts,\n expression: opts.expression.value.source,\n });\n return source && getExpressionObjectType(opts.ir, opts.actionName, opts.expression)\n ? {\n kind: \"related\",\n value: {\n source,\n link: opts.expression.value.link,\n },\n }\n : undefined;\n }\n case \"struct\": {\n const fields = opts.expression.value.fields.map((field) => ({\n ...field,\n value: projectExpression({\n ...opts,\n expression: field.value,\n }),\n }));\n return fields.some((field) => field.value === undefined)\n ? undefined\n : {\n kind: \"struct\",\n value: {\n fields: fields as Array<{\n name: string;\n value: Expression;\n }>,\n },\n };\n }\n case \"map\": {\n const source = projectExpression({\n ...opts,\n expression: opts.expression.value.source,\n });\n const body = projectExpression({\n ...opts,\n expression: opts.expression.value.body,\n });\n return source && body\n ? {\n kind: \"map\",\n value: {\n source,\n binding: opts.expression.value.binding,\n body,\n },\n }\n : undefined;\n }\n case \"inputReference\": {\n const parameterName = opts.expression.value.name;\n if (opts.visibleParameters.has(parameterName)) {\n return opts.expression;\n }\n if (!opts.projectFixedParameterValues) {\n return undefined;\n }\n const fixedValue = getFixedActionParameterValues(\n opts.fixedActionParameterValues,\n opts.actionName\n )?.[parameterName];\n if (fixedValue === undefined) return undefined;\n if (fixedValue.kind === \"inputReference\" && fixedValue.value.name === parameterName) {\n return undefined;\n }\n return projectExpression({\n ...opts,\n expression: fixedValue,\n });\n }\n case \"case\": {\n const branches = opts.expression.value.branches.map((branch) => ({\n condition: projectExpression({ ...opts, expression: branch.condition }),\n result: projectExpression({ ...opts, expression: branch.result }),\n }));\n const otherwise = projectExpression({ ...opts, expression: opts.expression.value.else });\n return otherwise && branches.every((b) => b.condition && b.result)\n ? {\n kind: \"case\",\n value: {\n branches: branches as { condition: Expression; result: Expression }[],\n else: otherwise,\n },\n }\n : undefined;\n }\n case \"not\":\n case \"length\": {\n const expression = projectExpression({ ...opts, expression: opts.expression.value.expression });\n return expression ? { kind: opts.expression.kind, value: { expression } } : undefined;\n }\n case \"and\":\n case \"or\":\n case \"concat\":\n case \"coalesce\": {\n const expressions = opts.expression.value.expressions.map((expression) =>\n projectExpression({ ...opts, expression })\n );\n return expressions.every((e) => e !== undefined)\n ? { kind: opts.expression.kind, value: { expressions: expressions as Expression[] } }\n : undefined;\n }\n case \"eq\":\n case \"ne\":\n case \"lt\":\n case \"lte\":\n case \"gt\":\n case \"gte\":\n case \"in\":\n case \"contains\":\n case \"like\":\n case \"ilike\":\n case \"add\":\n case \"subtract\":\n case \"multiply\":\n case \"divide\": {\n const left = projectExpression({ ...opts, expression: opts.expression.value.left });\n const right = projectExpression({ ...opts, expression: opts.expression.value.right });\n return left && right ? { kind: opts.expression.kind, value: { left, right } } : undefined;\n }\n case \"exists\":\n return undefined;\n }\n}\n\nfunction projectAssignments(opts: {\n assignments: PropertyAssignment[];\n\n context: Record<string, unknown> | undefined;\n\n ir: OntologyIR;\n actionName: string;\n objectTypeName: string;\n visibleParameters: Set<string>;\n fixedActionParameterValues: FixedActionParameterValues | undefined;\n allowedObjectTypeProperties: Record<string, readonly string[]>;\n}): PropertyAssignment[] {\n const allowedProperties = opts.allowedObjectTypeProperties[opts.objectTypeName] ?? [];\n return opts.assignments.flatMap((assignment) => {\n const propertyName = assignment.property[0];\n if (!propertyName || !allowedProperties.includes(propertyName)) return [];\n const projectedValue = projectExpression({\n expression: assignment.value,\n\n context: opts.context,\n\n ir: opts.ir,\n actionName: opts.actionName,\n visibleParameters: opts.visibleParameters,\n fixedActionParameterValues: opts.fixedActionParameterValues,\n allowedObjectTypeProperties: opts.allowedObjectTypeProperties,\n projectFixedParameterValues: true,\n });\n return projectedValue ? [{ ...assignment, value: projectedValue }] : [];\n });\n}\n\nfunction projectLogicStep(opts: {\n step: ActionLogicStep;\n\n context: Record<string, unknown> | undefined;\n\n ir: OntologyIR;\n actionType: ActionTypeDef;\n visibleParameters: Set<string>;\n fixedActionParameterValues: FixedActionParameterValues | undefined;\n allowedObjectTypeProperties: Record<string, readonly string[]>;\n}): ActionLogicStep | undefined {\n switch (opts.step.kind) {\n case \"createObject\": {\n const values = projectAssignments({\n assignments: opts.step.value.values,\n\n context: opts.context,\n\n ir: opts.ir,\n actionName: opts.actionType.name,\n objectTypeName: opts.step.value.objectType,\n visibleParameters: opts.visibleParameters,\n fixedActionParameterValues: opts.fixedActionParameterValues,\n allowedObjectTypeProperties: opts.allowedObjectTypeProperties,\n });\n return {\n ...opts.step,\n value: {\n ...opts.step.value,\n values,\n },\n };\n }\n case \"updateObject\": {\n const objectTypeName = getObjectReferenceObjectType(\n opts.ir,\n opts.actionType,\n opts.step.value.object\n );\n const values = projectAssignments({\n assignments: opts.step.value.values,\n\n context: opts.context,\n\n ir: opts.ir,\n actionName: opts.actionType.name,\n objectTypeName,\n visibleParameters: opts.visibleParameters,\n fixedActionParameterValues: opts.fixedActionParameterValues,\n allowedObjectTypeProperties: opts.allowedObjectTypeProperties,\n });\n return {\n ...opts.step,\n value: {\n ...opts.step.value,\n values,\n },\n };\n }\n case \"deleteObject\":\n return opts.step;\n }\n}\n\nfunction typeReferencesObjectTypes(type: TypeDef, ir: OntologyIR, seen = new Set<string>()): Set<string> {\n if (type.kind === \"ref\") {\n if (seen.has(type.value.name)) return new Set();\n seen.add(type.value.name);\n const named = ir.types.find((candidate) => candidate.name === type.value.name);\n return named ? typeReferencesObjectTypes(named.type, ir, seen) : new Set();\n }\n switch (type.kind) {\n case \"objectReference\":\n return new Set([type.value.objectType]);\n case \"optional\":\n return typeReferencesObjectTypes(type.value.type, ir, seen);\n case \"list\":\n return typeReferencesObjectTypes(type.value.elementType, ir, seen);\n case \"map\": {\n const names = typeReferencesObjectTypes(type.value.keyType, ir, seen);\n for (const name of typeReferencesObjectTypes(type.value.valueType, ir, seen)) names.add(name);\n return names;\n }\n case \"result\": {\n const names = typeReferencesObjectTypes(type.value.okType, ir, seen);\n for (const name of typeReferencesObjectTypes(type.value.errType, ir, seen)) names.add(name);\n return names;\n }\n case \"struct\": {\n const names = new Set<string>();\n for (const field of type.value.fields) {\n for (const name of typeReferencesObjectTypes(field.type, ir, seen)) names.add(name);\n }\n return names;\n }\n case \"union\": {\n const names = new Set<string>();\n for (const variant of type.value.variants) {\n for (const name of typeReferencesObjectTypes(variant.type, ir, seen)) names.add(name);\n }\n return names;\n }\n default:\n return new Set();\n }\n}\n\nfunction collectReferencedNamedTypes(\n type: TypeDef,\n ir: OntologyIR,\n into: Set<string>,\n seen = new Set<string>()\n): void {\n if (type.kind === \"ref\") {\n if (seen.has(type.value.name)) return;\n seen.add(type.value.name);\n into.add(type.value.name);\n const named = ir.types.find((candidate) => candidate.name === type.value.name);\n if (named) collectReferencedNamedTypes(named.type, ir, into, seen);\n return;\n }\n switch (type.kind) {\n case \"optional\":\n collectReferencedNamedTypes(type.value.type, ir, into, seen);\n break;\n case \"list\":\n collectReferencedNamedTypes(type.value.elementType, ir, into, seen);\n break;\n case \"map\":\n collectReferencedNamedTypes(type.value.keyType, ir, into, seen);\n collectReferencedNamedTypes(type.value.valueType, ir, into, seen);\n break;\n case \"result\":\n collectReferencedNamedTypes(type.value.okType, ir, into, seen);\n collectReferencedNamedTypes(type.value.errType, ir, into, seen);\n break;\n case \"struct\":\n for (const field of type.value.fields) {\n collectReferencedNamedTypes(field.type, ir, into, seen);\n }\n break;\n case \"union\":\n for (const variant of type.value.variants) {\n collectReferencedNamedTypes(variant.type, ir, into, seen);\n }\n break;\n default:\n break;\n }\n}\n\nfunction actionParametersReferenceHiddenObjectType(\n actionType: ActionTypeDef,\n ir: OntologyIR,\n visibleObjectTypes: Set<string>,\n visibleParameters?: Set<string>\n): boolean {\n for (const parameter of actionType.parameters) {\n if (visibleParameters && !visibleParameters.has(parameter.name)) continue;\n for (const objectType of typeReferencesObjectTypes(parameter.type, ir)) {\n if (!visibleObjectTypes.has(objectType)) return true;\n }\n }\n return false;\n}\n\nexport function projectOntologyDefinition<Ontology extends OntologyDefinition = OntologyDefinition>(opts: {\n ir: OntologyIR;\n\n context?: Record<string, unknown>;\n\n fixedActionParameterValues?: FixedActionParameterValues<Ontology>;\n projectFixedActionParameterValuesInDefaults?: boolean;\n allowedObjectTypeProperties: Record<string, readonly string[]>;\n /**\n * Projects object/property/link/action/query visibility from the resolved\n * authorization policy. Otherwise the full schema is retained while action\n * parameters and logic are still projected.\n */\n filterSchemaByAuthorization?: boolean;\n visibleActionTypes?: readonly string[] | \"all\";\n visibleQueryFunctionTypes?: readonly string[] | \"all\";\n}): OntologyIR {\n const filterSchemaByAuthorization = opts.filterSchemaByAuthorization ?? false;\n const visibleObjectTypes = new Set(\n filterSchemaByAuthorization\n ? Object.entries(opts.allowedObjectTypeProperties)\n .filter(([, properties]) => properties.length > 0)\n .map(([objectType]) => objectType)\n : opts.ir.objectTypes.map((objectType) => objectType.name)\n );\n\n const objectTypes = filterSchemaByAuthorization\n ? opts.ir.objectTypes\n .filter((objectType) => visibleObjectTypes.has(objectType.name))\n .map((objectType) => {\n const allowed = new Set(opts.allowedObjectTypeProperties[objectType.name] ?? []);\n return {\n ...objectType,\n title: objectType.title && allowed.has(objectType.title) ? objectType.title : undefined,\n properties: objectType.properties.filter((property) => allowed.has(property.name)),\n };\n })\n : opts.ir.objectTypes;\n\n const linkTypes = filterSchemaByAuthorization\n ? opts.ir.linkTypes.filter((link) => {\n if (\n !visibleObjectTypes.has(link.source.objectType) ||\n !visibleObjectTypes.has(link.target.objectType)\n ) {\n return false;\n }\n if (!link.foreignKey) return true;\n const sourceAllowed = new Set(opts.allowedObjectTypeProperties[link.source.objectType] ?? []);\n const targetAllowed = new Set(opts.allowedObjectTypeProperties[link.target.objectType] ?? []);\n return sourceAllowed.has(link.foreignKey) || targetAllowed.has(link.foreignKey);\n })\n : opts.ir.linkTypes;\n\n const visibleActions =\n opts.visibleActionTypes === undefined || opts.visibleActionTypes === \"all\"\n ? opts.ir.actionTypes\n : opts.ir.actionTypes.filter((actionType) =>\n (opts.visibleActionTypes as readonly string[]).includes(actionType.name)\n );\n\n const actionTypes = visibleActions\n .filter(\n (actionType) =>\n !filterSchemaByAuthorization ||\n !actionParametersReferenceHiddenObjectType(\n actionType,\n opts.ir,\n visibleObjectTypes,\n getVisibleActionParameterNames(\n actionType.name,\n actionType.parameters,\n opts.fixedActionParameterValues\n )\n )\n )\n .map((actionType) => {\n const visibleParameters = getVisibleActionParameterNames(\n actionType.name,\n actionType.parameters,\n opts.fixedActionParameterValues\n );\n return {\n ...actionType,\n parameters: actionType.parameters\n .filter((parameter) => visibleParameters.has(parameter.name))\n .map((parameter) => ({\n ...parameter,\n defaultValue: parameter.defaultValue\n ? projectExpression({\n expression: parameter.defaultValue,\n\n context: opts.context,\n\n ir: opts.ir,\n actionName: actionType.name,\n visibleParameters,\n fixedActionParameterValues: opts.fixedActionParameterValues,\n allowedObjectTypeProperties: opts.allowedObjectTypeProperties,\n projectFixedParameterValues:\n opts.projectFixedActionParameterValuesInDefaults ?? false,\n })\n : undefined,\n })),\n logic: actionType.logic.flatMap((step) => {\n if (\n filterSchemaByAuthorization &&\n step.kind === \"createObject\" &&\n !visibleObjectTypes.has(step.value.objectType)\n ) {\n return [];\n }\n const projectedStep = projectLogicStep({\n step,\n\n context: opts.context,\n\n ir: opts.ir,\n actionType,\n visibleParameters,\n fixedActionParameterValues: opts.fixedActionParameterValues,\n allowedObjectTypeProperties: opts.allowedObjectTypeProperties,\n });\n return projectedStep ? [projectedStep] : [];\n }),\n };\n });\n\n const queryFunctionTypes =\n opts.visibleQueryFunctionTypes === undefined || opts.visibleQueryFunctionTypes === \"all\"\n ? opts.ir.queryFunctionTypes.filter((queryFunction) => {\n if (!filterSchemaByAuthorization) return true;\n for (const parameter of queryFunction.parameters) {\n for (const objectType of typeReferencesObjectTypes(parameter.type, opts.ir)) {\n if (!visibleObjectTypes.has(objectType)) return false;\n }\n }\n for (const objectType of typeReferencesObjectTypes(queryFunction.returnType, opts.ir)) {\n if (!visibleObjectTypes.has(objectType)) return false;\n }\n return true;\n })\n : opts.ir.queryFunctionTypes.filter((queryFunction) =>\n (opts.visibleQueryFunctionTypes as readonly string[]).includes(queryFunction.name)\n );\n\n const referencedNamedTypes = new Set<string>();\n if (opts.ir.context?.type)\n collectReferencedNamedTypes(opts.ir.context.type, opts.ir, referencedNamedTypes);\n for (const objectType of objectTypes) {\n for (const property of objectType.properties) {\n collectReferencedNamedTypes(property.type, opts.ir, referencedNamedTypes);\n }\n }\n for (const actionType of actionTypes) {\n for (const parameter of actionType.parameters) {\n collectReferencedNamedTypes(parameter.type, opts.ir, referencedNamedTypes);\n }\n if (actionType.returnType)\n collectReferencedNamedTypes(actionType.returnType, opts.ir, referencedNamedTypes);\n }\n for (const queryFunction of queryFunctionTypes) {\n for (const parameter of queryFunction.parameters) {\n collectReferencedNamedTypes(parameter.type, opts.ir, referencedNamedTypes);\n }\n collectReferencedNamedTypes(queryFunction.returnType, opts.ir, referencedNamedTypes);\n }\n\n return {\n ...opts.ir,\n types: filterSchemaByAuthorization\n ? opts.ir.types.filter((type) => referencedNamedTypes.has(type.name))\n : opts.ir.types,\n objectTypes,\n linkTypes,\n actionTypes,\n queryFunctionTypes,\n };\n}\n"],"mappings":";;AA+BA,SAAS,QAAQ,OAAgB,MAAyB;CACtD,IAAI,UAAU;CACd,KAAK,MAAM,WAAW,MAAM;EACxB,IAAI,OAAO,YAAY,YAAY,YAAY,QAAQ,EAAE,WAAW,UAAU,OAAO;EACrF,UAAW,QAAoC;CACnD;CACA,OAAO;AACX;AAEA,SAASA,cAAY,IAAgB,MAAwB;CACzD,IAAI,KAAK,SAAS,OAAO,OAAO;CAChC,MAAM,YAAY,GAAG,MAAM,MAAM,cAAc,UAAU,SAAS,KAAK,MAAM,IAAI;CACjF,IAAI,CAAC,WACD,MAAM,IAAI,MAAM,0BAA0B,KAAK,MAAM,KAAK,GAAG;CAEjE,OAAOA,cAAY,IAAI,UAAU,IAAI;AACzC;AAEA,SAAS,cAAc,IAAgB,gBAAuC;CAC1E,MAAM,aAAa,GAAG,YAAY,MAAM,cAAc,UAAU,SAAS,cAAc;CACvF,IAAI,CAAC,YACD,MAAM,IAAI,MAAM,wBAAwB,eAAe,GAAG;CAE9D,OAAO;AACX;AAEA,SAAS,6BACL,IACA,YACA,WACM;CACN,MAAM,YAAY,WAAW,WAAW,MAAM,cAAc,UAAU,SAAS,UAAU,IAAI;CAC7F,IAAI,CAAC,WACD,MAAM,IAAI,MAAM,6BAA6B,UAAU,KAAK,GAAG;CAEnE,MAAM,OAAOA,cAAY,IAAI,UAAU,IAAI;CAC3C,IAAI,KAAK,SAAS,mBACd,MAAM,IAAI,MAAM,qBAAqB,UAAU,KAAK,8BAA8B;CAEtF,OAAO,KAAK,MAAM;AACtB;AAEA,SAAS,8BACL,4BACA,YACqD;CACrD,OAAQ,6BAA+E;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aAAa;EAClC,KAAK,MAAM,aAAa,WAAW,YAC/B,4BAA4B,UAAU,MAAM,KAAK,IAAI,oBAAoB;EAE7E,IAAI,WAAW,YACX,4BAA4B,WAAW,YAAY,KAAK,IAAI,oBAAoB;CACxF;CACA,KAAK,MAAM,iBAAiB,oBAAoB;EAC5C,KAAK,MAAM,aAAa,cAAc,YAClC,4BAA4B,UAAU,MAAM,KAAK,IAAI,oBAAoB;EAE7E,4BAA4B,cAAc,YAAY,KAAK,IAAI,oBAAoB;CACvF;CAEA,OAAO;EACH,GAAG,KAAK;EACR,OAAO,8BACD,KAAK,GAAG,MAAM,QAAQ,SAAS,qBAAqB,IAAI,KAAK,IAAI,CAAC,IAClE,KAAK,GAAG;EACd;EACA;EACA;EACA;CACJ;AACJ"}
|
package/lib/errorProjection.d.ts
CHANGED
|
@@ -13,7 +13,7 @@ export interface ErrorProjectionOptions {
|
|
|
13
13
|
export declare const errorContext: (options: ErrorProjectionOptions, name?: string) => ErrorProjectionContext;
|
|
14
14
|
export declare const projectValidationReport: (report: ValidationReport, options: ErrorProjectionOptions, name: string) => ValidationReport;
|
|
15
15
|
export declare function projectStateFailures(state: ReplicaStateSnapshot, options: ErrorProjectionOptions): ReplicaStateSnapshot;
|
|
16
|
-
export declare const projectActionFailure: (
|
|
16
|
+
export declare const projectActionFailure: (action: ReplicaStateSnapshot["pending"][number], options: ErrorProjectionOptions) => import("@party-stack/ontology-backend/protocol").PendingAction;
|
|
17
17
|
declare const OntologyErrorProjection_base: RpcMiddleware.ServiceClass<OntologyErrorProjection, "@party-stack/ontology-server/ErrorProjection", never, typeof BackendError, never, never, false>;
|
|
18
18
|
export declare class OntologyErrorProjection extends OntologyErrorProjection_base {}
|
|
19
19
|
export declare const errorProjectionLayer: (options: ErrorProjectionOptions) => Layer.Layer<OntologyErrorProjection, never, never>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorProjection.d.ts","names":[],"sources":["../src/errorProjection.ts"],"mappings":";;;;;;iBAWiB;WACJ,SAAS;;WAET,KAAK;WACL;;qBAEA,eAAY,SAAa,wBAAsB,kBAAkB;qBAUjE,0BAAuB,QACxB,kBAAgB,SACf,wBAAsB,iBAEhC;wBAea,qBACZ,OAAO,sBACP,SAAS,yBACV;qBAOU,uBAAoB,
|
|
1
|
+
{"version":3,"file":"errorProjection.d.ts","names":[],"sources":["../src/errorProjection.ts"],"mappings":";;;;;;iBAWiB;WACJ,SAAS;;WAET,KAAK;WACL;;qBAEA,eAAY,SAAa,wBAAsB,kBAAkB;qBAUjE,0BAAuB,QACxB,kBAAgB,SACf,wBAAsB,iBAEhC;wBAea,qBACZ,OAAO,sBACP,SAAS,yBACV;qBAOU,uBAAoB,QACrB,yCAAuC,SACtC,4EAAsB;;qBAmCtB,gCAAgC;qBAIhC,uBAAoB,SAAa,2BAAsB,MAAA,MAAA"}
|
package/lib/errorProjection.js
CHANGED
|
@@ -34,7 +34,8 @@ function projectStateFailures(state, options) {
|
|
|
34
34
|
completed: state.completed.map((entry) => projectActionFailure(entry, options))
|
|
35
35
|
};
|
|
36
36
|
}
|
|
37
|
-
const projectActionFailure = (
|
|
37
|
+
const projectActionFailure = (action, options) => {
|
|
38
|
+
const entry = projectPredictionFailure(action, options);
|
|
38
39
|
if (!entry.failure) return entry;
|
|
39
40
|
const error = projectBackendError(new BackendError({
|
|
40
41
|
operation: "applyAction",
|
|
@@ -51,6 +52,20 @@ const projectActionFailure = (entry, options) => {
|
|
|
51
52
|
failureIssues: error.issues
|
|
52
53
|
};
|
|
53
54
|
};
|
|
55
|
+
const projectPredictionFailure = (entry, options) => {
|
|
56
|
+
if (!entry.predictionFailure) return entry;
|
|
57
|
+
const error = projectBackendError(new BackendError({
|
|
58
|
+
operation: "replayAction",
|
|
59
|
+
kind: entry.predictionFailure,
|
|
60
|
+
message: entry.predictionMessage ?? "The local prediction failed.",
|
|
61
|
+
retryable: false
|
|
62
|
+
}), options.errors, errorContext(options, entry.invocation.name));
|
|
63
|
+
return {
|
|
64
|
+
...entry,
|
|
65
|
+
predictionFailure: error.kind,
|
|
66
|
+
predictionMessage: error.message
|
|
67
|
+
};
|
|
68
|
+
};
|
|
54
69
|
var OntologyErrorProjection = class extends RpcMiddleware.Service()("@party-stack/ontology-server/ErrorProjection", { error: BackendError }) {};
|
|
55
70
|
const errorProjectionLayer = (options) => Layer.succeed(OntologyErrorProjection, (effect, request) => effect.pipe(Effect.mapError((error) => {
|
|
56
71
|
if (!(error instanceof BackendError)) return error;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorProjection.js","names":[],"sources":["../src/errorProjection.ts"],"sourcesContent":["import { BackendError, projectBackendError } from \"@party-stack/ontology-backend\";\nimport { Effect, Layer } from \"effect\";\nimport { RpcMiddleware } from \"effect/rpc\";\nimport type {\n ErrorProjection,\n ErrorProjectionContext,\n ValidationReport,\n} from \"@party-stack/ontology-backend\";\nimport type { ReplicaStateSnapshot } from \"@party-stack/ontology-backend/protocol\";\nimport type { OntologyIR } from \"@party-stack/ontology-spec\";\n\nexport interface ErrorProjectionOptions {\n readonly errors?: ErrorProjection;\n /** The already projected schema, not the private authoritative definition. */\n readonly ir?: OntologyIR;\n readonly upstreamErrors?: boolean;\n}\nexport const errorContext = (options: ErrorProjectionOptions, name?: string): ErrorProjectionContext => {\n const action = options.ir?.actionTypes.find((action) => action.name === name);\n return {\n upstream: options.upstreamErrors,\n parameters: action?.parameters.map((parameter) => parameter.name),\n objectReferenceParameters: action?.parameters\n .filter((parameter) => parameter.type.kind === \"objectReference\")\n .map((parameter) => parameter.name),\n };\n};\nexport const projectValidationReport = (\n report: ValidationReport,\n options: ErrorProjectionOptions,\n name: string\n): ValidationReport => {\n if (options.errors === \"forward\" || !report.certain || report.value.kind === \"ok\") return report;\n const error = projectBackendError(\n new BackendError({\n operation: \"validate\",\n kind: \"ValidationFailed\",\n message: \"Submission failed validation.\",\n issues: report.value.value,\n retryable: false,\n }),\n options.errors,\n errorContext(options, name)\n );\n return { certain: true, value: { kind: \"err\", value: error.issues ?? [{ message: error.message }] } };\n};\nexport function projectStateFailures(\n state: ReplicaStateSnapshot,\n options: ErrorProjectionOptions\n): ReplicaStateSnapshot {\n return {\n ...state,\n pending: state.pending.map((entry) => projectActionFailure(entry, options)),\n completed: state.completed.map((entry) => projectActionFailure(entry, options)),\n };\n}\nexport const projectActionFailure = (\n
|
|
1
|
+
{"version":3,"file":"errorProjection.js","names":[],"sources":["../src/errorProjection.ts"],"sourcesContent":["import { BackendError, projectBackendError } from \"@party-stack/ontology-backend\";\nimport { Effect, Layer } from \"effect\";\nimport { RpcMiddleware } from \"effect/rpc\";\nimport type {\n ErrorProjection,\n ErrorProjectionContext,\n ValidationReport,\n} from \"@party-stack/ontology-backend\";\nimport type { ReplicaStateSnapshot } from \"@party-stack/ontology-backend/protocol\";\nimport type { OntologyIR } from \"@party-stack/ontology-spec\";\n\nexport interface ErrorProjectionOptions {\n readonly errors?: ErrorProjection;\n /** The already projected schema, not the private authoritative definition. */\n readonly ir?: OntologyIR;\n readonly upstreamErrors?: boolean;\n}\nexport const errorContext = (options: ErrorProjectionOptions, name?: string): ErrorProjectionContext => {\n const action = options.ir?.actionTypes.find((action) => action.name === name);\n return {\n upstream: options.upstreamErrors,\n parameters: action?.parameters.map((parameter) => parameter.name),\n objectReferenceParameters: action?.parameters\n .filter((parameter) => parameter.type.kind === \"objectReference\")\n .map((parameter) => parameter.name),\n };\n};\nexport const projectValidationReport = (\n report: ValidationReport,\n options: ErrorProjectionOptions,\n name: string\n): ValidationReport => {\n if (options.errors === \"forward\" || !report.certain || report.value.kind === \"ok\") return report;\n const error = projectBackendError(\n new BackendError({\n operation: \"validate\",\n kind: \"ValidationFailed\",\n message: \"Submission failed validation.\",\n issues: report.value.value,\n retryable: false,\n }),\n options.errors,\n errorContext(options, name)\n );\n return { certain: true, value: { kind: \"err\", value: error.issues ?? [{ message: error.message }] } };\n};\nexport function projectStateFailures(\n state: ReplicaStateSnapshot,\n options: ErrorProjectionOptions\n): ReplicaStateSnapshot {\n return {\n ...state,\n pending: state.pending.map((entry) => projectActionFailure(entry, options)),\n completed: state.completed.map((entry) => projectActionFailure(entry, options)),\n };\n}\nexport const projectActionFailure = (\n action: ReplicaStateSnapshot[\"pending\"][number],\n options: ErrorProjectionOptions\n) => {\n const entry = projectPredictionFailure(action, options);\n if (!entry.failure) return entry;\n const error = projectBackendError(\n new BackendError({\n operation: \"applyAction\",\n kind: entry.failure,\n message: entry.failureMessage ?? \"Action failed.\",\n issues: entry.failureIssues,\n retryable: entry.failureRetryable ?? false,\n actionOutcome: entry.failureOutcome,\n }),\n options.errors,\n errorContext(options, entry.invocation.name)\n );\n return { ...entry, failure: error.kind, failureMessage: error.message, failureIssues: error.issues };\n};\nconst projectPredictionFailure = (\n entry: ReplicaStateSnapshot[\"pending\"][number],\n options: ErrorProjectionOptions\n): ReplicaStateSnapshot[\"pending\"][number] => {\n if (!entry.predictionFailure) return entry;\n const error = projectBackendError(\n new BackendError({\n operation: \"replayAction\",\n kind: entry.predictionFailure,\n message: entry.predictionMessage ?? \"The local prediction failed.\",\n retryable: false,\n }),\n options.errors,\n errorContext(options, entry.invocation.name)\n );\n return { ...entry, predictionFailure: error.kind, predictionMessage: error.message };\n};\nexport class OntologyErrorProjection extends RpcMiddleware.Service<OntologyErrorProjection>()(\n \"@party-stack/ontology-server/ErrorProjection\",\n { error: BackendError }\n) {}\nexport const errorProjectionLayer = (options: ErrorProjectionOptions) =>\n Layer.succeed(OntologyErrorProjection, (effect, request) =>\n effect.pipe(\n Effect.mapError((error) => {\n if (!(error instanceof BackendError)) return error;\n const payload = request.payload as\n | { name?: string; invocation?: { name?: string } }\n | undefined;\n return projectBackendError(\n error,\n options.errors,\n errorContext(options, payload?.name ?? payload?.invocation?.name)\n );\n }),\n Effect.catchDefect((cause) =>\n Effect.logError(\"Ontology RPC defect\", { operation: request.rpc._tag, cause }).pipe(\n Effect.andThen(\n Effect.fail(\n new BackendError({\n operation: request.rpc._tag,\n kind: \"Internal\",\n message: \"The operation could not be completed.\",\n retryable: false,\n actionOutcome: \"unknown\",\n })\n )\n )\n )\n )\n )\n );\n"],"mappings":";;;;AAiBA,MAAa,gBAAgB,SAAiC,SAA0C;CACpG,MAAM,SAAS,QAAQ,IAAI,YAAY,MAAM,WAAW,OAAO,SAAS,IAAI;CAC5E,OAAO;EACH,UAAU,QAAQ;EAClB,YAAY,QAAQ,WAAW,KAAK,cAAc,UAAU,IAAI;EAChE,2BAA2B,QAAQ,WAC9B,QAAQ,cAAc,UAAU,KAAK,SAAS,iBAAiB,CAAC,CAChE,KAAK,cAAc,UAAU,IAAI;CAC1C;AACJ;AACA,MAAa,2BACT,QACA,SACA,SACmB;CACnB,IAAI,QAAQ,WAAW,aAAa,CAAC,OAAO,WAAW,OAAO,MAAM,SAAS,MAAM,OAAO;CAC1F,MAAM,QAAQ,oBACV,IAAI,aAAa;EACb,WAAW;EACX,MAAM;EACN,SAAS;EACT,QAAQ,OAAO,MAAM;EACrB,WAAW;CACf,CAAC,GACD,QAAQ,QACR,aAAa,SAAS,IAAI,CAC9B;CACA,OAAO;EAAE,SAAS;EAAM,OAAO;GAAE,MAAM;GAAO,OAAO,MAAM,UAAU,CAAC,EAAE,SAAS,MAAM,QAAQ,CAAC;EAAE;CAAE;AACxG;AACA,SAAgB,qBACZ,OACA,SACoB;CACpB,OAAO;EACH,GAAG;EACH,SAAS,MAAM,QAAQ,KAAK,UAAU,qBAAqB,OAAO,OAAO,CAAC;EAC1E,WAAW,MAAM,UAAU,KAAK,UAAU,qBAAqB,OAAO,OAAO,CAAC;CAClF;AACJ;AACA,MAAa,wBACT,QACA,YACC;CACD,MAAM,QAAQ,yBAAyB,QAAQ,OAAO;CACtD,IAAI,CAAC,MAAM,SAAS,OAAO;CAC3B,MAAM,QAAQ,oBACV,IAAI,aAAa;EACb,WAAW;EACX,MAAM,MAAM;EACZ,SAAS,MAAM,kBAAkB;EACjC,QAAQ,MAAM;EACd,WAAW,MAAM,oBAAoB;EACrC,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