@reventlessdev/reventless-core 3.0.0-alpha.315 → 3.0.0-alpha.316
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/CHANGELOG.md +7 -0
- package/package.json +6 -6
- package/src/components/Aggregate/Aggregate_Callback.res +3 -0
- package/src/components/Aggregate/Aggregate_Callback.res.mjs +1 -1
- package/src/components/EventLog/EventLog_Operations.res +174 -53
- package/src/components/EventLog/EventLog_Operations.res.mjs +148 -38
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res +170 -90
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res.mjs +153 -69
- package/src/util/Lru.res +33 -6
- package/src/util/Lru.res.mjs +32 -8
- package/src/util/Lru.resi +8 -2
- package/tests/dcb/DcbPiiSliceTest.res +141 -0
- package/tests/dcb/DcbPiiSliceTest.res.mjs +271 -0
- package/tests/eventlog/EventLogFixtures.res +14 -5
- package/tests/eventlog/EventLogFixtures.res.mjs +10 -4
- package/tests/eventlog/EventLogPiiTest.res +113 -0
- package/tests/eventlog/EventLogPiiTest.res.mjs +226 -0
- package/tests/util/LruTest.res +35 -0
- package/tests/util/LruTest.res.mjs +46 -8
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,13 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# 3.0.0-alpha.316 (2026-10-29)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* **core:** an event's personal fields are sealed once at append and opened on read ([ef3daae](https://github.com/ReventlessDev/reventless-core/commit/ef3daae9ad02090b9ec6de4decbe41c44b5dd554))
|
|
11
|
+
|
|
12
|
+
|
|
6
13
|
# 3.0.0-alpha.315 (2026-10-29)
|
|
7
14
|
|
|
8
15
|
* feat(ppx)!: `@pii` marks a value about a person, and an event reads it as `Pii.t` ([37174c1](https://github.com/ReventlessDev/reventless-core/commit/37174c10eb80e9e3adbb93a52ad7a9ef5def7100))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reventlessdev/reventless-core",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.316",
|
|
4
4
|
"description": "Core package for Reventless framework",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"jest": {
|
|
@@ -30,15 +30,15 @@
|
|
|
30
30
|
"uuid": "^13.0.0",
|
|
31
31
|
"@reventlessdev/rescript-effect": "1.0.0-alpha.1",
|
|
32
32
|
"@reventlessdev/rescript-fast-csv": "2.0.0-alpha.24",
|
|
33
|
+
"@reventlessdev/rescript-hash-object": "1.2.0-alpha.15",
|
|
33
34
|
"@reventlessdev/rescript-jest": "1.0.0-alpha.13",
|
|
34
35
|
"@reventlessdev/rescript-node": "2.0.0-alpha.24",
|
|
35
|
-
"@reventlessdev/rescript-hash-object": "1.2.0-alpha.15",
|
|
36
36
|
"@reventlessdev/rescript-pulumi-pulumi": "3.0.0-alpha.0",
|
|
37
|
-
"@reventlessdev/reventless-infra": "3.0.0-alpha.212",
|
|
38
|
-
"@reventlessdev/reventless-interop": "3.0.0-alpha.42",
|
|
39
|
-
"@reventlessdev/reventless-spec": "3.0.0-alpha.183",
|
|
40
37
|
"@reventlessdev/rescript-ssh2": "2.0.0-alpha.25",
|
|
41
|
-
"@reventlessdev/rescript-uuid": "2.0.0-alpha.1"
|
|
38
|
+
"@reventlessdev/rescript-uuid": "2.0.0-alpha.1",
|
|
39
|
+
"@reventlessdev/reventless-infra": "3.0.0-alpha.213",
|
|
40
|
+
"@reventlessdev/reventless-interop": "3.0.0-alpha.42",
|
|
41
|
+
"@reventlessdev/reventless-spec": "3.0.0-alpha.184"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"rescript": "12.3.1",
|
|
@@ -201,8 +201,11 @@ module Make = (
|
|
|
201
201
|
// the fixed capacity (a per-aggregate knob is a future refinement — see
|
|
202
202
|
// docs/plans/done/aggregate-snapshotting.md).
|
|
203
203
|
let replayCacheCapacity = 100
|
|
204
|
+
// Decoded state holds personal values in the clear: it is served for minutes at
|
|
205
|
+
// most, so an erased person's leaves a warm container within a named bound.
|
|
204
206
|
let replayCache: Lru.t<string, (Decision.state, int, array<string>)> = Lru.make(
|
|
205
207
|
~capacity=replayCacheCapacity,
|
|
208
|
+
~ttlMs=Lru.decodedStateTtlMs(),
|
|
206
209
|
)
|
|
207
210
|
|
|
208
211
|
let resetCache = () => replayCache->Lru.clear
|
|
@@ -129,7 +129,7 @@ function Make(Spec) {
|
|
|
129
129
|
]])
|
|
130
130
|
};
|
|
131
131
|
});
|
|
132
|
-
let replayCache = Lru$ReventlessCore.make(100);
|
|
132
|
+
let replayCache = Lru$ReventlessCore.make(100, Lru$ReventlessCore.decodedStateTtlMs(), undefined);
|
|
133
133
|
let resetCache = () => Lru$ReventlessCore.clear(replayCache);
|
|
134
134
|
let snapshotConfig = actsOnOwned ? undefined : Decision.snapshot;
|
|
135
135
|
if (actsOnOwned && Stdlib_Option.isSome(Decision.snapshot)) {
|
|
@@ -44,22 +44,63 @@ module Make = (Spec: ReventlessInfra.EventLog.T, Ops: Ops with module Spec = Spe
|
|
|
44
44
|
) => {
|
|
45
45
|
module Spec = Spec
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
let (eventType, data) = json->Message.splitMessage
|
|
47
|
+
// The stored item for an event already encoded and sealed: its type and fields.
|
|
48
|
+
let storedEventOf = (id, sequenceNr, meta, (eventType, data)) => {
|
|
50
49
|
let stored: Reventless.StoredEvent.storedEvent<Spec.Id.t> = {
|
|
51
50
|
id,
|
|
52
51
|
position: sequenceNr->Int.toString->String.padStart(9, "0"),
|
|
53
52
|
event: eventType,
|
|
54
53
|
data: JSON.Object(data),
|
|
55
|
-
meta
|
|
54
|
+
meta,
|
|
56
55
|
recordedAt: Message.nowAsISOString(),
|
|
57
56
|
}
|
|
58
57
|
stored->Message.storedEventToFlatJson(Spec.Id.schema)
|
|
59
58
|
}
|
|
60
59
|
|
|
61
|
-
|
|
62
|
-
|
|
60
|
+
/**
|
|
61
|
+
Every event encoded once and sealed once: the form both the stored item and the
|
|
62
|
+
published message are built from. An encrypting provider draws a fresh nonce on
|
|
63
|
+
every seal, so a second encode would publish a ciphertext the log does not hold.
|
|
64
|
+
Refused, never degraded, when a personal field cannot be sealed or comes out
|
|
65
|
+
unsealed; the message names the event type and the field, never the value.
|
|
66
|
+
*/
|
|
67
|
+
let sealEvents = async (events': array<Message.event'<Spec.Id.t, Spec.event>>): result<
|
|
68
|
+
array<(string, dict<JSON.t>)>,
|
|
69
|
+
string,
|
|
70
|
+
> => {
|
|
71
|
+
let out = []
|
|
72
|
+
let failure = ref(None)
|
|
73
|
+
for i in 0 to events'->Array.length - 1 {
|
|
74
|
+
if failure.contents->Option.isNone {
|
|
75
|
+
let (eventType, data) =
|
|
76
|
+
(events'->Array.getUnsafe(i)).event
|
|
77
|
+
->Message.encode(Spec.eventSchema)
|
|
78
|
+
->Message.splitMessage
|
|
79
|
+
switch await Reventless.Pii.sealData(Spec.eventSchema, ~eventType, data) {
|
|
80
|
+
| Error(e) => failure := Some(`cannot seal ${eventType}: ${e->Reventless.Pii.errorMessage}`)
|
|
81
|
+
| Ok(sealed) =>
|
|
82
|
+
switch Reventless.Pii.unsealedField(Spec.eventSchema, ~eventType, sealed) {
|
|
83
|
+
| Some(field) =>
|
|
84
|
+
failure := Some(`refused ${eventType}: its personal field ${field} is not sealed`)
|
|
85
|
+
| None => out->Array.push((eventType, sealed))
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
switch failure.contents {
|
|
91
|
+
| Some(msg) => Error(msg)
|
|
92
|
+
| None => Ok(out)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** What the topic publishes for one sealed event: the envelope `encodeEvent'`
|
|
97
|
+
writes, with the sealed fields rather than a second encode. */
|
|
98
|
+
let publishedJsonOf = (id, meta, (eventType, data)) =>
|
|
99
|
+
Dict.fromArray([
|
|
100
|
+
("id", id->Reventless.Util_Sury.toJson(Spec.Id.schema)),
|
|
101
|
+
("meta", meta->Message.encode(Message.metaSchema)),
|
|
102
|
+
("event", Message.combineMessage(eventType, data)),
|
|
103
|
+
])->JSON.Encode.object
|
|
63
104
|
|
|
64
105
|
let makePublishedEvent = (idStr, eventsJson, meta): EventPublish_Callback.publishedEvent => {
|
|
65
106
|
componentName: Spec.name,
|
|
@@ -104,13 +145,30 @@ module Make = (Spec: ReventlessInfra.EventLog.T, Ops: Ops with module Spec = Spe
|
|
|
104
145
|
}
|
|
105
146
|
|
|
106
147
|
// Returns result<unit, string> — never throws. Publish failures are surfaced as Error.
|
|
107
|
-
|
|
148
|
+
// Publishes the sealed form the log stored, never a fresh encode.
|
|
149
|
+
let publishToEventTopic = async (
|
|
150
|
+
id,
|
|
151
|
+
events': array<Message.event'<_, _>>,
|
|
152
|
+
sealed: array<(string, dict<JSON.t>)>,
|
|
153
|
+
eventsJson,
|
|
154
|
+
) => {
|
|
108
155
|
let idStr = id->Spec.Id.toString
|
|
109
156
|
let firstEvent = events'->Array.getUnsafe(0)
|
|
110
157
|
let meta = firstEvent.meta
|
|
111
158
|
await runBeforePublishHook(idStr, eventsJson, meta)
|
|
112
159
|
try {
|
|
113
|
-
let
|
|
160
|
+
let count = events'->Array.length->Int.toString
|
|
161
|
+
let _ = await events'
|
|
162
|
+
->Array.mapWithIndex(async (event', i) => {
|
|
163
|
+
let json = publishedJsonOf(event'.id, event'.meta, sealed->Array.getUnsafe(i))
|
|
164
|
+
let _ = await Ops.eventTopic.publishJson(idStr, event'.meta, json)
|
|
165
|
+
EffectLogger.logInfo(
|
|
166
|
+
~comp=`EventTopic(${Spec.name})`,
|
|
167
|
+
~detail=json,
|
|
168
|
+
`published event ${(i + 1)->Int.toString}/${count}: ${LogFormat.eventDetail(json)}`,
|
|
169
|
+
)->Effect.runSync
|
|
170
|
+
})
|
|
171
|
+
->Promise.all
|
|
114
172
|
await runAfterPublishHook(idStr, eventsJson, meta)
|
|
115
173
|
Ok()
|
|
116
174
|
} catch {
|
|
@@ -126,43 +184,54 @@ module Make = (Spec: ReventlessInfra.EventLog.T, Ops: Ops with module Spec = Spe
|
|
|
126
184
|
// Transient storage failures are retried with exponential backoff (up to 5
|
|
127
185
|
// times); a Conflict is returned immediately (retried a layer up), and other
|
|
128
186
|
// failures propagate after retries are exhausted.
|
|
129
|
-
let append = async (sequenceNr, id, events') =>
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
// A thrown storage error becomes a StorageFailure; a returned Error keeps its
|
|
133
|
-
// typed variant. The Effect fails with the typed appendError so the schedule
|
|
134
|
-
// can retry only transient failures, then we collapse the outcome back into a
|
|
135
|
-
// result (no Cause stringification / substring matching).
|
|
136
|
-
let storageEffect =
|
|
137
|
-
Effect.tryPromise(
|
|
138
|
-
~catch=(err: unknown) => EventLog.StorageFailure(
|
|
139
|
-
Util.Error.messageFromUnknown(err, "storage error"),
|
|
140
|
-
),
|
|
141
|
-
() => Ops.storage.append(sequenceNr, idStr, eventsJson),
|
|
142
|
-
)
|
|
143
|
-
->Effect.flatMap(result =>
|
|
144
|
-
switch result {
|
|
145
|
-
| Ok(_) => Effect.succeed()
|
|
146
|
-
| Error(e) => Effect.fail(e)
|
|
147
|
-
}
|
|
148
|
-
)
|
|
149
|
-
->Effect.retry(storageRetrySchedule)
|
|
150
|
-
->Effect.map(_ => Ok())
|
|
151
|
-
->Effect.catchAll(e => Effect.succeed(Error(e)))
|
|
152
|
-
switch await storageEffect->Effect.runPromise {
|
|
153
|
-
| Ok() =>
|
|
154
|
-
(
|
|
155
|
-
await publishToEventTopic(id, events', eventsJson)
|
|
156
|
-
)->Result.mapError(msg => EventLog.StorageFailure(msg))
|
|
157
|
-
| Error(EventLog.Conflict) => Error(EventLog.Conflict)
|
|
158
|
-
| Error(StorageFailure(msg)) =>
|
|
187
|
+
let append = async (sequenceNr, id, events') =>
|
|
188
|
+
switch await sealEvents(events') {
|
|
189
|
+
| Error(msg) =>
|
|
159
190
|
Error(
|
|
160
191
|
EventLog.StorageFailure(
|
|
161
|
-
`EventLog: Error: Couldn't append for ${Spec.name}(${
|
|
192
|
+
`EventLog: Error: Couldn't append for ${Spec.name}(${id->Spec.Id.toString}): ${msg}`,
|
|
162
193
|
),
|
|
163
194
|
)
|
|
195
|
+
| Ok(sealed) =>
|
|
196
|
+
let eventsJson =
|
|
197
|
+
sealed->Array.mapWithIndex((part, i) =>
|
|
198
|
+
storedEventOf(id, sequenceNr + i, (events'->Array.getUnsafe(i)).meta, part)
|
|
199
|
+
)
|
|
200
|
+
let idStr = id->Spec.Id.toString
|
|
201
|
+
// A thrown storage error becomes a StorageFailure; a returned Error keeps its
|
|
202
|
+
// typed variant. The Effect fails with the typed appendError so the schedule
|
|
203
|
+
// can retry only transient failures, then we collapse the outcome back into a
|
|
204
|
+
// result (no Cause stringification / substring matching).
|
|
205
|
+
let storageEffect =
|
|
206
|
+
Effect.tryPromise(
|
|
207
|
+
~catch=(err: unknown) => EventLog.StorageFailure(
|
|
208
|
+
Util.Error.messageFromUnknown(err, "storage error"),
|
|
209
|
+
),
|
|
210
|
+
() => Ops.storage.append(sequenceNr, idStr, eventsJson),
|
|
211
|
+
)
|
|
212
|
+
->Effect.flatMap(result =>
|
|
213
|
+
switch result {
|
|
214
|
+
| Ok(_) => Effect.succeed()
|
|
215
|
+
| Error(e) => Effect.fail(e)
|
|
216
|
+
}
|
|
217
|
+
)
|
|
218
|
+
->Effect.retry(storageRetrySchedule)
|
|
219
|
+
->Effect.map(_ => Ok())
|
|
220
|
+
->Effect.catchAll(e => Effect.succeed(Error(e)))
|
|
221
|
+
switch await storageEffect->Effect.runPromise {
|
|
222
|
+
| Ok() =>
|
|
223
|
+
(
|
|
224
|
+
await publishToEventTopic(id, events', sealed, eventsJson)
|
|
225
|
+
)->Result.mapError(msg => EventLog.StorageFailure(msg))
|
|
226
|
+
| Error(EventLog.Conflict) => Error(EventLog.Conflict)
|
|
227
|
+
| Error(StorageFailure(msg)) =>
|
|
228
|
+
Error(
|
|
229
|
+
EventLog.StorageFailure(
|
|
230
|
+
`EventLog: Error: Couldn't append for ${Spec.name}(${idStr}): ${msg}`,
|
|
231
|
+
),
|
|
232
|
+
)
|
|
233
|
+
}
|
|
164
234
|
}
|
|
165
|
-
}
|
|
166
235
|
|
|
167
236
|
let decodeEvent = (id, json) =>
|
|
168
237
|
try {
|
|
@@ -188,16 +257,58 @@ module Make = (Spec: ReventlessInfra.EventLog.T, Ops: Ops with module Spec = Spe
|
|
|
188
257
|
|
|
189
258
|
let decodeEvents = (jsons, id) => jsons->Array.map(json => decodeEvent(id, json))
|
|
190
259
|
|
|
260
|
+
// The stored item with its sealed personal fields opened by the installed
|
|
261
|
+
// provider, so the decoder reads values and the markers of values that are gone.
|
|
262
|
+
// Most items hold nothing sealed and come back as they are, without a promise.
|
|
263
|
+
let sealedParts = json =>
|
|
264
|
+
switch json->JSON.Decode.object {
|
|
265
|
+
| Some(dict) =>
|
|
266
|
+
switch (dict->Dict.get("event"), dict->Dict.get("data")) {
|
|
267
|
+
| (Some(JSON.String(eventType)), Some(JSON.Object(data)))
|
|
268
|
+
if Reventless.Pii.needsOpen(Spec.eventSchema, ~eventType, data) =>
|
|
269
|
+
Some((dict, eventType, data))
|
|
270
|
+
| _ => None
|
|
271
|
+
}
|
|
272
|
+
| None => None
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
let openStored = async (id, json) =>
|
|
276
|
+
switch sealedParts(json) {
|
|
277
|
+
| None => json
|
|
278
|
+
| Some((dict, eventType, data)) =>
|
|
279
|
+
switch await Reventless.Pii.openData(Spec.eventSchema, ~eventType, data) {
|
|
280
|
+
| Ok(opened) =>
|
|
281
|
+
let out = dict->Dict.copy
|
|
282
|
+
out->Dict.set("data", JSON.Object(opened))
|
|
283
|
+
JSON.Object(out)
|
|
284
|
+
| Error(e) =>
|
|
285
|
+
JsError.throwWithMessage(
|
|
286
|
+
`EventLog.replay: Error: id:${id}: cannot open ${eventType}: ${e->Reventless.Pii.errorMessage}`,
|
|
287
|
+
)
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
let openAndDecode = (id, json) =>
|
|
292
|
+
switch sealedParts(json) {
|
|
293
|
+
| None => Effect.sync(() => decodeEvent(id, json))
|
|
294
|
+
| Some(_) => Effect.promise(() => openStored(id, json))->Effect.map(j => decodeEvent(id, j))
|
|
295
|
+
}
|
|
296
|
+
|
|
191
297
|
let replay = async id => {
|
|
192
|
-
let
|
|
193
|
-
eventsJson
|
|
298
|
+
let idStr = id->Spec.Id.toString
|
|
299
|
+
let eventsJson = await Ops.storage.replay(idStr)
|
|
300
|
+
let opened = []
|
|
301
|
+
for i in 0 to eventsJson->Array.length - 1 {
|
|
302
|
+
opened->Array.push(await openStored(idStr, eventsJson->Array.getUnsafe(i)))
|
|
303
|
+
}
|
|
304
|
+
opened->decodeEvents(idStr)
|
|
194
305
|
}
|
|
195
306
|
|
|
196
|
-
// Lazy streaming replay —
|
|
307
|
+
// Lazy streaming replay — decodes inside the effect so thrown exceptions
|
|
197
308
|
// surface through the stream's error channel rather than as unhandled exceptions.
|
|
198
309
|
let replayStream = (id, ~fromSeq=?) =>
|
|
199
310
|
Ops.storage.replayStream(id->Spec.Id.toString, ~fromSeq?)->Stream.mapEffect(json =>
|
|
200
|
-
|
|
311
|
+
openAndDecode(id->Spec.Id.toString, json)
|
|
201
312
|
)
|
|
202
313
|
|
|
203
314
|
// The stored item is the flat form `storedEventToFlatJson` writes: meta.* at the
|
|
@@ -221,10 +332,10 @@ module Make = (Spec: ReventlessInfra.EventLog.T, Ops: Ops with module Spec = Spe
|
|
|
221
332
|
let replayStream' = (id, ~fromSeq=?) => {
|
|
222
333
|
let idStr = id->Spec.Id.toString
|
|
223
334
|
Ops.storage.replayStream(idStr, ~fromSeq?)->Stream.mapEffect(json =>
|
|
224
|
-
Effect.
|
|
335
|
+
openAndDecode(idStr, json)->Effect.map(event => {
|
|
225
336
|
Reventless.Message.id,
|
|
226
337
|
meta: decodeMeta(idStr, json),
|
|
227
|
-
event
|
|
338
|
+
event,
|
|
228
339
|
})
|
|
229
340
|
)
|
|
230
341
|
}
|
|
@@ -244,12 +355,22 @@ module Make = (Spec: ReventlessInfra.EventLog.T, Ops: Ops with module Spec = Spe
|
|
|
244
355
|
Ops.storage.appendStream(
|
|
245
356
|
startingSeqNr,
|
|
246
357
|
id->Spec.Id.toString,
|
|
247
|
-
stream
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
358
|
+
// Sealed as `append` seals, so a copied stream stores no personal value in
|
|
359
|
+
// the clear; a field that cannot be sealed fails the stream.
|
|
360
|
+
stream->Stream.mapEffect(event =>
|
|
361
|
+
Effect.promise(async () => {
|
|
362
|
+
let (eventType, data) = event->Message.encode(Spec.eventSchema)->Message.splitMessage
|
|
363
|
+
let sealed = switch await Reventless.Pii.sealData(Spec.eventSchema, ~eventType, data) {
|
|
364
|
+
| Ok(sealed) => sealed
|
|
365
|
+
| Error(e) =>
|
|
366
|
+
JsError.throwWithMessage(
|
|
367
|
+
`EventLog.appendStream: cannot seal ${eventType}: ${e->Reventless.Pii.errorMessage}`,
|
|
368
|
+
)
|
|
369
|
+
}
|
|
370
|
+
[("event", JSON.String(eventType)), ("data", JSON.Object(sealed))]
|
|
371
|
+
->Dict.fromArray
|
|
372
|
+
->JSON.Encode.object
|
|
373
|
+
})
|
|
374
|
+
),
|
|
254
375
|
)
|
|
255
376
|
}
|
|
@@ -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
|
|
37
|
-
let
|
|
38
|
-
let
|
|
39
|
-
let
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
187
|
-
let
|
|
188
|
-
|
|
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 =>
|
|
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.
|
|
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:
|
|
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.
|
|
210
|
-
let
|
|
211
|
-
let
|
|
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
|
-
|
|
325
|
+
eventType
|
|
216
326
|
],
|
|
217
327
|
[
|
|
218
328
|
"data",
|
|
219
|
-
|
|
329
|
+
sealed$1
|
|
220
330
|
]
|
|
221
331
|
]);
|
|
222
|
-
}));
|
|
332
|
+
})));
|
|
223
333
|
return {
|
|
224
334
|
Spec: Spec,
|
|
225
335
|
append: append,
|