@reventlessdev/reventless-core 3.0.0-alpha.315 → 3.0.0-alpha.317

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.
Files changed (31) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/package.json +7 -7
  3. package/src/Message.res +3 -1
  4. package/src/Message.res.mjs +4 -1
  5. package/src/components/Aggregate/Aggregate_Callback.res +3 -0
  6. package/src/components/Aggregate/Aggregate_Callback.res.mjs +1 -1
  7. package/src/components/EventLog/EventLog_Operations.res +174 -53
  8. package/src/components/EventLog/EventLog_Operations.res.mjs +148 -38
  9. package/src/components/Extension/Extension_Operations.res +8 -2
  10. package/src/components/Extension/Extension_Operations.res.mjs +9 -2
  11. package/src/components/ExtensionPoint/ExtensionPoint_Operations.res +3 -0
  12. package/src/components/ExtensionPoint/ExtensionPoint_Operations.res.mjs +4 -0
  13. package/src/components/StateChangeSlice/StateChangeSlice_Callback.res +170 -90
  14. package/src/components/StateChangeSlice/StateChangeSlice_Callback.res.mjs +153 -69
  15. package/src/components/StateViewSlice/StateViewSlice_Builder.res +6 -0
  16. package/src/components/StateViewSlice/StateViewSlice_Builder.res.mjs +8 -1
  17. package/src/components/StateViewSlice/StateViewSlice_Callback.res +5 -0
  18. package/src/components/StateViewSlice/StateViewSlice_Callback.res.mjs +5 -0
  19. package/src/util/Lru.res +33 -6
  20. package/src/util/Lru.res.mjs +32 -8
  21. package/src/util/Lru.resi +8 -2
  22. package/tests/dcb/DcbPiiSliceTest.res +142 -0
  23. package/tests/dcb/DcbPiiSliceTest.res.mjs +275 -0
  24. package/tests/eventlog/EventLogFixtures.res +14 -5
  25. package/tests/eventlog/EventLogFixtures.res.mjs +10 -4
  26. package/tests/eventlog/EventLogPiiTest.res +114 -0
  27. package/tests/eventlog/EventLogPiiTest.res.mjs +230 -0
  28. package/tests/pii/PiiPrefetchTest.res +116 -0
  29. package/tests/pii/PiiPrefetchTest.res.mjs +206 -0
  30. package/tests/util/LruTest.res +35 -0
  31. package/tests/util/LruTest.res.mjs +46 -8
@@ -7,12 +7,14 @@ import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
7
7
  import * as Stdlib_Result from "@rescript/runtime/lib/es6/Stdlib_Result.js";
8
8
  import * as Effect$1 from "effect/Effect";
9
9
  import * as Stream from "effect/Stream";
10
+ import * as Pii$Reventless from "@reventlessdev/reventless-spec/src/components/Pii.res.mjs";
10
11
  import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
11
12
  import * as Duration from "effect/Duration";
12
13
  import * as Schedule from "effect/Schedule";
13
14
  import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
14
15
  import * as Util_Sury$Reventless from "@reventlessdev/reventless-spec/src/util/Util_Sury.res.mjs";
15
16
  import * as Message$ReventlessCore from "../../Message.res.mjs";
17
+ import * as LogFormat$ReventlessCore from "../../util/LogFormat.res.mjs";
16
18
  import * as Util_Error$ReventlessCore from "../../util/Util_Error.res.mjs";
17
19
  import * as EffectLogger$ReventlessCore from "../../util/EffectLogger.res.mjs";
18
20
  import * as EventPublish_Callback$ReventlessCore from "./EventPublish_Callback.res.mjs";
@@ -33,25 +35,57 @@ let storageRetrySchedule = Schedule.whileInput(Schedule.intersect(Schedule.jitte
33
35
 
34
36
  function Make(Spec) {
35
37
  return Ops => {
36
- let encodeEvents$p = (events$p, id, startingSeqNr) => events$p.map((event, i) => {
37
- let sequenceNr = startingSeqNr + i | 0;
38
- let json = Message$ReventlessCore.encode(event.event, Spec.eventSchema);
39
- let match = Message$ReventlessCore.splitMessage(json);
40
- let stored_position = sequenceNr.toString().padStart(9, "0");
41
- let stored_event = match[0];
42
- let stored_data = match[1];
43
- let stored_meta = event.meta;
44
- let stored_recordedAt = Message$ReventlessCore.nowAsISOString();
45
- let stored = {
46
- id: id,
47
- position: stored_position,
48
- event: stored_event,
49
- data: stored_data,
50
- meta: stored_meta,
51
- recordedAt: stored_recordedAt
52
- };
53
- return Message$ReventlessCore.storedEventToFlatJson(stored, Spec.Id.schema);
54
- });
38
+ let sealEvents = async events$p => {
39
+ let out = [];
40
+ let failure;
41
+ for (let i = 0, i_finish = events$p.length; i < i_finish; ++i) {
42
+ if (Stdlib_Option.isNone(failure)) {
43
+ let match = Message$ReventlessCore.splitMessage(Message$ReventlessCore.encode(events$p[i].event, Spec.eventSchema));
44
+ let eventType = match[0];
45
+ let e = await Pii$Reventless.sealData(Spec.eventSchema, eventType, match[1]);
46
+ if (e.TAG === "Ok") {
47
+ let sealed = e._0;
48
+ let field = Pii$Reventless.unsealedField(Spec.eventSchema, eventType, sealed);
49
+ if (field !== undefined) {
50
+ failure = `refused ` + eventType + `: its personal field ` + field + ` is not sealed`;
51
+ } else {
52
+ out.push([
53
+ eventType,
54
+ sealed
55
+ ]);
56
+ }
57
+ } else {
58
+ failure = `cannot seal ` + eventType + `: ` + Pii$Reventless.errorMessage(e._0);
59
+ }
60
+ }
61
+ }
62
+ let msg = failure;
63
+ if (msg !== undefined) {
64
+ return {
65
+ TAG: "Error",
66
+ _0: msg
67
+ };
68
+ } else {
69
+ return {
70
+ TAG: "Ok",
71
+ _0: out
72
+ };
73
+ }
74
+ };
75
+ let publishedJsonOf = (id, meta, param) => Object.fromEntries([
76
+ [
77
+ "id",
78
+ Util_Sury$Reventless.toJson(id, Spec.Id.schema)
79
+ ],
80
+ [
81
+ "meta",
82
+ Message$ReventlessCore.encode(meta, Message$ReventlessCore.metaSchema)
83
+ ],
84
+ [
85
+ "event",
86
+ Message$ReventlessCore.combineMessage(param[0], param[1])
87
+ ]
88
+ ]);
55
89
  let makePublishedEvent = (idStr, eventsJson, meta) => ({
56
90
  componentName: Spec.name,
57
91
  entityId: idStr,
@@ -87,13 +121,18 @@ function Make(Spec) {
87
121
  return Effect$1.runSync(EffectLogger$ReventlessCore.logError(`EventLog(` + Spec.name + `)`, undefined, `afterPublishHook error: ` + errMsg));
88
122
  }
89
123
  };
90
- let publishToEventTopic = async (id, events$p, eventsJson) => {
124
+ let publishToEventTopic = async (id, events$p, sealed, eventsJson) => {
91
125
  let idStr = Spec.Id.toString(id);
92
126
  let firstEvent = events$p[0];
93
127
  let meta = firstEvent.meta;
94
128
  await runBeforePublishHook(idStr, eventsJson, meta);
95
129
  try {
96
- await Ops.eventTopic.publish(events$p);
130
+ let count = events$p.length.toString();
131
+ await Promise.all(events$p.map(async (event$p, i) => {
132
+ let json = publishedJsonOf(event$p.id, event$p.meta, sealed[i]);
133
+ await Ops.eventTopic.publishJson(idStr, event$p.meta, json);
134
+ return Effect$1.runSync(EffectLogger$ReventlessCore.logInfo(`EventTopic(` + Spec.name + `)`, json, `published event ` + (i + 1 | 0).toString() + `/` + count + `: ` + LogFormat$ReventlessCore.eventDetail(json)));
135
+ }));
97
136
  await runAfterPublishHook(idStr, eventsJson, meta);
98
137
  return {
99
138
  TAG: "Ok",
@@ -112,7 +151,34 @@ function Make(Spec) {
112
151
  }
113
152
  };
114
153
  let append = async (sequenceNr, id, events$p) => {
115
- let eventsJson = encodeEvents$p(events$p, id, sequenceNr);
154
+ let msg = await sealEvents(events$p);
155
+ if (msg.TAG !== "Ok") {
156
+ return {
157
+ TAG: "Error",
158
+ _0: {
159
+ TAG: "StorageFailure",
160
+ _0: `EventLog: Error: Couldn't append for ` + Spec.name + `(` + Spec.Id.toString(id) + `): ` + msg._0
161
+ }
162
+ };
163
+ }
164
+ let sealed = msg._0;
165
+ let eventsJson = sealed.map((part, i) => {
166
+ let sequenceNr$1 = sequenceNr + i | 0;
167
+ let meta = events$p[i].meta;
168
+ let stored_position = sequenceNr$1.toString().padStart(9, "0");
169
+ let stored_event = part[0];
170
+ let stored_data = part[1];
171
+ let stored_recordedAt = Message$ReventlessCore.nowAsISOString();
172
+ let stored = {
173
+ id: id,
174
+ position: stored_position,
175
+ event: stored_event,
176
+ data: stored_data,
177
+ meta: meta,
178
+ recordedAt: stored_recordedAt
179
+ };
180
+ return Message$ReventlessCore.storedEventToFlatJson(stored, Spec.Id.schema);
181
+ });
116
182
  let idStr = Spec.Id.toString(id);
117
183
  let storageEffect = Effect$1.catchAll(Effect$1.map(Effect$1.retry(Effect$1.flatMap(Effect.tryPromise(err => ({
118
184
  TAG: "StorageFailure",
@@ -132,13 +198,13 @@ function Make(Spec) {
132
198
  }));
133
199
  let match = await Effect$1.runPromise(storageEffect);
134
200
  if (match.TAG === "Ok") {
135
- return Stdlib_Result.mapError(await publishToEventTopic(id, events$p, eventsJson), msg => ({
201
+ return Stdlib_Result.mapError(await publishToEventTopic(id, events$p, sealed, eventsJson), msg => ({
136
202
  TAG: "StorageFailure",
137
203
  _0: msg
138
204
  }));
139
205
  }
140
- let msg = match._0;
141
- if (typeof msg !== "object") {
206
+ let msg$1 = match._0;
207
+ if (typeof msg$1 !== "object") {
142
208
  return {
143
209
  TAG: "Error",
144
210
  _0: "Conflict"
@@ -148,7 +214,7 @@ function Make(Spec) {
148
214
  TAG: "Error",
149
215
  _0: {
150
216
  TAG: "StorageFailure",
151
- _0: `EventLog: Error: Couldn't append for ` + Spec.name + `(` + idStr + `): ` + msg._0
217
+ _0: `EventLog: Error: Couldn't append for ` + Spec.name + `(` + idStr + `): ` + msg$1._0
152
218
  }
153
219
  };
154
220
  }
@@ -182,12 +248,53 @@ function Make(Spec) {
182
248
  throw e;
183
249
  }
184
250
  };
251
+ let sealedParts = json => {
252
+ let dict = Stdlib_JSON.Decode.object(json);
253
+ if (dict === undefined) {
254
+ return;
255
+ }
256
+ let match = dict["event"];
257
+ let match$1 = dict["data"];
258
+ if (match !== undefined && typeof match === "string" && match$1 !== undefined && typeof match$1 === "object" && match$1 !== null && !Array.isArray(match$1) && Pii$Reventless.needsOpen(Spec.eventSchema, match, match$1)) {
259
+ return [
260
+ dict,
261
+ match,
262
+ match$1
263
+ ];
264
+ }
265
+ };
266
+ let openStored = async (id, json) => {
267
+ let match = sealedParts(json);
268
+ if (match === undefined) {
269
+ return json;
270
+ }
271
+ let eventType = match[1];
272
+ let opened = await Pii$Reventless.openData(Spec.eventSchema, eventType, match[2]);
273
+ if (opened.TAG !== "Ok") {
274
+ return Stdlib_JsError.throwWithMessage(`EventLog.replay: Error: id:` + id + `: cannot open ` + eventType + `: ` + Pii$Reventless.errorMessage(opened._0));
275
+ }
276
+ let out = Object.assign({}, match[0]);
277
+ out["data"] = opened._0;
278
+ return out;
279
+ };
280
+ let openAndDecode = (id, json) => {
281
+ let match = sealedParts(json);
282
+ if (match !== undefined) {
283
+ return Effect$1.map(Effect$1.promise(() => openStored(id, json)), j => decodeEvent(id, j));
284
+ } else {
285
+ return Effect$1.sync(() => decodeEvent(id, json));
286
+ }
287
+ };
185
288
  let replay = async id => {
186
- let eventsJson = await Ops.storage.replay(Spec.Id.toString(id));
187
- let id$1 = Spec.Id.toString(id);
188
- return eventsJson.map(json => decodeEvent(id$1, json));
289
+ let idStr = Spec.Id.toString(id);
290
+ let eventsJson = await Ops.storage.replay(idStr);
291
+ let opened = [];
292
+ for (let i = 0, i_finish = eventsJson.length; i < i_finish; ++i) {
293
+ opened.push(await openStored(idStr, eventsJson[i]));
294
+ }
295
+ return opened.map(json => decodeEvent(idStr, json));
189
296
  };
190
- let replayStream = (id, fromSeq) => Stream.mapEffect(Ops.storage.replayStream(Spec.Id.toString(id), fromSeq), json => Effect$1.sync(() => decodeEvent(Spec.Id.toString(id), json)));
297
+ let replayStream = (id, fromSeq) => Stream.mapEffect(Ops.storage.replayStream(Spec.Id.toString(id), fromSeq), json => openAndDecode(Spec.Id.toString(id), json));
191
298
  let decodeMeta = (id, json) => {
192
299
  try {
193
300
  return Util_Sury$Reventless.fromJson(Message$ReventlessCore.composeMeta(Stdlib_Option.getOrThrow(Stdlib_JSON.Decode.object(json), undefined)), Message$ReventlessCore.metaSchema);
@@ -198,28 +305,31 @@ function Make(Spec) {
198
305
  };
199
306
  let replayStream$p = (id, fromSeq) => {
200
307
  let idStr = Spec.Id.toString(id);
201
- return Stream.mapEffect(Ops.storage.replayStream(idStr, fromSeq), json => Effect$1.sync(() => ({
308
+ return Stream.mapEffect(Ops.storage.replayStream(idStr, fromSeq), json => Effect$1.map(openAndDecode(idStr, json), event => ({
202
309
  id: id,
203
310
  meta: decodeMeta(idStr, json),
204
- event: decodeEvent(idStr, json)
311
+ event: event
205
312
  })));
206
313
  };
207
314
  let latestSnapshot = id => Ops.storage.latestSnapshot(Spec.Id.toString(id));
208
315
  let writeSnapshot = (id, snap) => Ops.storage.writeSnapshot(Spec.Id.toString(id), snap);
209
- let appendStream = (startingSeqNr, id, stream) => Ops.storage.appendStream(startingSeqNr, Spec.Id.toString(id), Stream.map(stream, event => {
210
- let json = Message$ReventlessCore.encode(event, Spec.eventSchema);
211
- let match = Message$ReventlessCore.splitMessage(json);
316
+ let appendStream = (startingSeqNr, id, stream) => Ops.storage.appendStream(startingSeqNr, Spec.Id.toString(id), Stream.mapEffect(stream, event => Effect$1.promise(async () => {
317
+ let match = Message$ReventlessCore.splitMessage(Message$ReventlessCore.encode(event, Spec.eventSchema));
318
+ let eventType = match[0];
319
+ let sealed = await Pii$Reventless.sealData(Spec.eventSchema, eventType, match[1]);
320
+ let sealed$1;
321
+ sealed$1 = sealed.TAG === "Ok" ? sealed._0 : Stdlib_JsError.throwWithMessage(`EventLog.appendStream: cannot seal ` + eventType + `: ` + Pii$Reventless.errorMessage(sealed._0));
212
322
  return Object.fromEntries([
213
323
  [
214
324
  "event",
215
- match[0]
325
+ eventType
216
326
  ],
217
327
  [
218
328
  "data",
219
- match[1]
329
+ sealed$1
220
330
  ]
221
331
  ]);
222
- }));
332
+ })));
223
333
  return {
224
334
  Spec: Spec,
225
335
  append: append,
@@ -152,6 +152,9 @@ module Make = (
152
152
  }
153
153
 
154
154
  let incomingJsonEventsHandler = async (eventJson', pluginDef) => {
155
+ if Reventless.Pii.needsPrepare([eventJson']) {
156
+ await Reventless.Pii.prepareOrThrow([eventJson'])
157
+ }
155
158
  EffectLogger.logInfo(
156
159
  ~comp,
157
160
  `incoming EP event: ${LogFormat.eventSummary(eventJson')}`,
@@ -178,13 +181,16 @@ module Make = (
178
181
  }
179
182
  }
180
183
 
181
- let outgoingJsonEventsHandler = (eventJson', pluginDef) => {
184
+ let outgoingJsonEventsHandler = async (eventJson', pluginDef) => {
185
+ if Reventless.Pii.needsPrepare([eventJson']) {
186
+ await Reventless.Pii.prepareOrThrow([eventJson'])
187
+ }
182
188
  EffectLogger.logInfo(
183
189
  ~comp,
184
190
  `outgoing delegate event: ${LogFormat.eventSummary(eventJson')}`,
185
191
  )->Effect.runSync
186
192
  let commandActions = mapOutgoingEvent(eventJson', pluginDef)
187
- commandActions
193
+ await commandActions
188
194
  ->Array.map(applyOutgoingCommandAction)
189
195
  ->Promise.all
190
196
  ->Util.Promise.toUnit
@@ -4,6 +4,7 @@ import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
4
4
  import * as Id$Reventless from "@reventlessdev/reventless-spec/src/types/Id.res.mjs";
5
5
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
6
6
  import * as Effect from "effect/Effect";
7
+ import * as Pii$Reventless from "@reventlessdev/reventless-spec/src/components/Pii.res.mjs";
7
8
  import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
8
9
  import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
9
10
  import * as Message$ReventlessCore from "../../Message.res.mjs";
@@ -111,6 +112,9 @@ function Make(MappingSpec) {
111
112
  }
112
113
  };
113
114
  let incomingJsonEventsHandler = async (eventJson$p, pluginDef) => {
115
+ if (Pii$Reventless.needsPrepare([eventJson$p])) {
116
+ await Pii$Reventless.prepareOrThrow([eventJson$p]);
117
+ }
114
118
  Effect.runSync(EffectLogger$ReventlessCore.logInfo(comp, undefined, `incoming EP event: ` + LogFormat$ReventlessCore.eventSummary(eventJson$p)));
115
119
  let event$p;
116
120
  try {
@@ -124,10 +128,13 @@ function Make(MappingSpec) {
124
128
  let apply = async commandActions => await Util_Promise$ReventlessCore.toUnit(Promise.all(commandActions.map(applyIncomingCommandAction)));
125
129
  return await apply(commandActions);
126
130
  };
127
- let outgoingJsonEventsHandler = (eventJson$p, pluginDef) => {
131
+ let outgoingJsonEventsHandler = async (eventJson$p, pluginDef) => {
132
+ if (Pii$Reventless.needsPrepare([eventJson$p])) {
133
+ await Pii$Reventless.prepareOrThrow([eventJson$p]);
134
+ }
128
135
  Effect.runSync(EffectLogger$ReventlessCore.logInfo(comp, undefined, `outgoing delegate event: ` + LogFormat$ReventlessCore.eventSummary(eventJson$p)));
129
136
  let commandActions = mapOutgoingEvent(eventJson$p, pluginDef);
130
- return Util_Promise$ReventlessCore.toUnit(Promise.all(commandActions.map(applyOutgoingCommandAction)));
137
+ return await Util_Promise$ReventlessCore.toUnit(Promise.all(commandActions.map(applyOutgoingCommandAction)));
131
138
  };
132
139
  return {
133
140
  incomingJsonEventsHandler: incomingJsonEventsHandler,
@@ -133,6 +133,9 @@ module Make = (
133
133
  }
134
134
 
135
135
  let outgoingJsonEventsHandler = async (eventJson', _pluginDef) => {
136
+ if Reventless.Pii.needsPrepare([eventJson']) {
137
+ await Reventless.Pii.prepareOrThrow([eventJson'])
138
+ }
136
139
  EffectLogger.logInfo(
137
140
  ~comp=`ExtensionPoint(${MappingSpec.name})`,
138
141
  `outgoing event: ${LogFormat.eventSummary(eventJson')}`,
@@ -3,6 +3,7 @@
3
3
  import * as Stdlib_JsExn from "@rescript/runtime/lib/es6/Stdlib_JsExn.js";
4
4
  import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
5
5
  import * as Effect from "effect/Effect";
6
+ import * as Pii$Reventless from "@reventlessdev/reventless-spec/src/components/Pii.res.mjs";
6
7
  import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
7
8
  import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
8
9
  import * as Message$ReventlessCore from "../../Message.res.mjs";
@@ -102,6 +103,9 @@ function Make(MappingSpec) {
102
103
  }
103
104
  };
104
105
  let outgoingJsonEventsHandler = async (eventJson$p, _pluginDef) => {
106
+ if (Pii$Reventless.needsPrepare([eventJson$p])) {
107
+ await Pii$Reventless.prepareOrThrow([eventJson$p]);
108
+ }
105
109
  Effect.runSync(EffectLogger$ReventlessCore.logInfo(`ExtensionPoint(` + MappingSpec.name + `)`, undefined, `outgoing event: ` + LogFormat$ReventlessCore.eventSummary(eventJson$p)));
106
110
  let eventActions = mapOutgoingEvent(eventJson$p, Mappings.mappings, Ops.scheduler, Ops.commandTopicResources, Ops.queryEngine, Ops.resourceNaming);
107
111
  return await Util_Promise$ReventlessCore.toUnit(Promise.all(eventActions.map(applyEventAction)));