@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.
- package/CHANGELOG.md +23 -0
- package/package.json +7 -7
- package/src/Message.res +3 -1
- package/src/Message.res.mjs +4 -1
- 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/Extension/Extension_Operations.res +8 -2
- package/src/components/Extension/Extension_Operations.res.mjs +9 -2
- package/src/components/ExtensionPoint/ExtensionPoint_Operations.res +3 -0
- package/src/components/ExtensionPoint/ExtensionPoint_Operations.res.mjs +4 -0
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res +170 -90
- package/src/components/StateChangeSlice/StateChangeSlice_Callback.res.mjs +153 -69
- package/src/components/StateViewSlice/StateViewSlice_Builder.res +6 -0
- package/src/components/StateViewSlice/StateViewSlice_Builder.res.mjs +8 -1
- package/src/components/StateViewSlice/StateViewSlice_Callback.res +5 -0
- package/src/components/StateViewSlice/StateViewSlice_Callback.res.mjs +5 -0
- 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 +142 -0
- package/tests/dcb/DcbPiiSliceTest.res.mjs +275 -0
- package/tests/eventlog/EventLogFixtures.res +14 -5
- package/tests/eventlog/EventLogFixtures.res.mjs +10 -4
- package/tests/eventlog/EventLogPiiTest.res +114 -0
- package/tests/eventlog/EventLogPiiTest.res.mjs +230 -0
- package/tests/pii/PiiPrefetchTest.res +116 -0
- package/tests/pii/PiiPrefetchTest.res.mjs +206 -0
- package/tests/util/LruTest.res +35 -0
- package/tests/util/LruTest.res.mjs +46 -8
package/src/util/Lru.res
CHANGED
|
@@ -5,22 +5,45 @@
|
|
|
5
5
|
// least-recently-used key as the first element of `Map.keys`. `put` evicts that
|
|
6
6
|
// first key once `size` exceeds `capacity`.
|
|
7
7
|
|
|
8
|
+
// An entry also carries when it stops being served: decoded state holds personal
|
|
9
|
+
// values in the clear, and an erased person's must leave a warm container within a
|
|
10
|
+
// bound the erasure promise can name, not whenever the entry happens to be evicted.
|
|
11
|
+
|
|
8
12
|
type t<'k, 'v> = {
|
|
9
13
|
capacity: int,
|
|
10
|
-
|
|
14
|
+
ttlMs: option<float>,
|
|
15
|
+
now: unit => float,
|
|
16
|
+
store: Map.t<'k, ('v, float)>,
|
|
11
17
|
}
|
|
12
18
|
|
|
13
|
-
|
|
19
|
+
@val external env: dict<string> = "process.env"
|
|
20
|
+
|
|
21
|
+
/** The time limit a cache of decoded state takes unless it says otherwise: five
|
|
22
|
+
minutes, or `REVENTLESS_DECODED_CACHE_TTL_SECONDS`. */
|
|
23
|
+
let decodedStateTtlMs = (): float =>
|
|
24
|
+
env
|
|
25
|
+
->Dict.get("REVENTLESS_DECODED_CACHE_TTL_SECONDS")
|
|
26
|
+
->Option.flatMap(Float.fromString)
|
|
27
|
+
->Option.filter(s => s > 0.)
|
|
28
|
+
->Option.getOr(300.) *. 1000.
|
|
29
|
+
|
|
30
|
+
let make = (~capacity: int, ~ttlMs: option<float>=?, ~now: unit => float=Date.now): t<'k, 'v> => {
|
|
14
31
|
capacity,
|
|
32
|
+
ttlMs,
|
|
33
|
+
now,
|
|
15
34
|
store: Map.make(),
|
|
16
35
|
}
|
|
17
36
|
|
|
18
37
|
let get = (cache: t<'k, 'v>, key: 'k): option<'v> =>
|
|
19
38
|
switch cache.store->Map.get(key) {
|
|
20
|
-
| Some(
|
|
21
|
-
// Touch: re-insert so this key becomes most-recently-used.
|
|
39
|
+
| Some((_, expiresAt)) if cache.now() >= expiresAt =>
|
|
22
40
|
let _ = cache.store->Map.delete(key)
|
|
23
|
-
|
|
41
|
+
None
|
|
42
|
+
| Some((value, expiresAt)) =>
|
|
43
|
+
// Touch: re-insert so this key becomes most-recently-used. The time limit runs
|
|
44
|
+
// from the put, not from the last read, so a hot key still expires.
|
|
45
|
+
let _ = cache.store->Map.delete(key)
|
|
46
|
+
cache.store->Map.set(key, (value, expiresAt))
|
|
24
47
|
Some(value)
|
|
25
48
|
| None => None
|
|
26
49
|
}
|
|
@@ -32,7 +55,11 @@ let put = (cache: t<'k, 'v>, key: 'k, value: 'v): unit =>
|
|
|
32
55
|
} else {
|
|
33
56
|
// Delete first so an update also moves the key to most-recently-used.
|
|
34
57
|
let _ = cache.store->Map.delete(key)
|
|
35
|
-
cache.
|
|
58
|
+
let expiresAt = switch cache.ttlMs {
|
|
59
|
+
| Some(ttl) => cache.now() +. ttl
|
|
60
|
+
| None => Float.Constants.positiveInfinity
|
|
61
|
+
}
|
|
62
|
+
cache.store->Map.set(key, (value, expiresAt))
|
|
36
63
|
if cache.store->Map.size > cache.capacity {
|
|
37
64
|
// Evict the least-recently-used entry = oldest insertion = first key.
|
|
38
65
|
switch cache.store->Map.keys->Array.fromIterator->Array.get(0) {
|
package/src/util/Lru.res.mjs
CHANGED
|
@@ -1,23 +1,41 @@
|
|
|
1
1
|
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
2
|
|
|
3
|
+
import * as Stdlib_Float from "@rescript/runtime/lib/es6/Stdlib_Float.js";
|
|
4
|
+
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
3
5
|
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
|
|
4
6
|
|
|
5
|
-
function
|
|
7
|
+
function decodedStateTtlMs() {
|
|
8
|
+
return Stdlib_Option.getOr(Stdlib_Option.filter(Stdlib_Option.flatMap(process.env["REVENTLESS_DECODED_CACHE_TTL_SECONDS"], Stdlib_Float.fromString), s => s > 0), 300) * 1000;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function make(capacity, ttlMs, nowOpt) {
|
|
12
|
+
let now = nowOpt !== undefined ? nowOpt : prim => Date.now();
|
|
6
13
|
return {
|
|
7
14
|
capacity: capacity,
|
|
15
|
+
ttlMs: ttlMs,
|
|
16
|
+
now: now,
|
|
8
17
|
store: new Map()
|
|
9
18
|
};
|
|
10
19
|
}
|
|
11
20
|
|
|
12
21
|
function get(cache, key) {
|
|
13
|
-
let
|
|
14
|
-
if (
|
|
22
|
+
let match = cache.store.get(key);
|
|
23
|
+
if (match === undefined) {
|
|
15
24
|
return;
|
|
16
25
|
}
|
|
17
|
-
let
|
|
18
|
-
|
|
19
|
-
cache.
|
|
20
|
-
|
|
26
|
+
let expiresAt = match[1];
|
|
27
|
+
let value = match[0];
|
|
28
|
+
if (cache.now() >= expiresAt) {
|
|
29
|
+
cache.store.delete(key);
|
|
30
|
+
return;
|
|
31
|
+
} else {
|
|
32
|
+
cache.store.delete(key);
|
|
33
|
+
cache.store.set(key, [
|
|
34
|
+
value,
|
|
35
|
+
expiresAt
|
|
36
|
+
]);
|
|
37
|
+
return Primitive_option.some(value);
|
|
38
|
+
}
|
|
21
39
|
}
|
|
22
40
|
|
|
23
41
|
function put(cache, key, value) {
|
|
@@ -25,7 +43,12 @@ function put(cache, key, value) {
|
|
|
25
43
|
return;
|
|
26
44
|
}
|
|
27
45
|
cache.store.delete(key);
|
|
28
|
-
cache.
|
|
46
|
+
let ttl = cache.ttlMs;
|
|
47
|
+
let expiresAt = ttl !== undefined ? cache.now() + ttl : Number.POSITIVE_INFINITY;
|
|
48
|
+
cache.store.set(key, [
|
|
49
|
+
value,
|
|
50
|
+
expiresAt
|
|
51
|
+
]);
|
|
29
52
|
if (cache.store.size <= cache.capacity) {
|
|
30
53
|
return;
|
|
31
54
|
}
|
|
@@ -49,6 +72,7 @@ function size(cache) {
|
|
|
49
72
|
}
|
|
50
73
|
|
|
51
74
|
export {
|
|
75
|
+
decodedStateTtlMs,
|
|
52
76
|
make,
|
|
53
77
|
get,
|
|
54
78
|
put,
|
package/src/util/Lru.resi
CHANGED
|
@@ -12,8 +12,14 @@ the cache entirely (`get` always misses, `put` is a no-op) so a slice can opt ou
|
|
|
12
12
|
*/
|
|
13
13
|
type t<'k, 'v>
|
|
14
14
|
|
|
15
|
-
/**
|
|
16
|
-
|
|
15
|
+
/** The time limit a cache of decoded state takes by default: five minutes, or
|
|
16
|
+
`REVENTLESS_DECODED_CACHE_TTL_SECONDS`. */
|
|
17
|
+
let decodedStateTtlMs: unit => float
|
|
18
|
+
|
|
19
|
+
/** Creates an empty cache holding at most `capacity` entries (`<= 0` disables it).
|
|
20
|
+
With `ttlMs`, an entry is served for that long after its `put` and then
|
|
21
|
+
missed; `now` is the clock, injectable for a test. */
|
|
22
|
+
let make: (~capacity: int, ~ttlMs: float=?, ~now: unit => float=?) => t<'k, 'v>
|
|
17
23
|
|
|
18
24
|
/** Looks up `key`, marking it most-recently-used on a hit. */
|
|
19
25
|
let get: (t<'k, 'v>, 'k) => option<'v>
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// A DCB slice seals a personal field once, stores and publishes that one form,
|
|
2
|
+
// opens it when it reads its history back, and refuses an append whose field comes
|
|
3
|
+
// out unsealed.
|
|
4
|
+
|
|
5
|
+
open JestGlobals
|
|
6
|
+
|
|
7
|
+
module MemberSpec = {
|
|
8
|
+
let name = "TestMemberNames"
|
|
9
|
+
module Id = Reventless.Id.String
|
|
10
|
+
let moduleUrl: string = %raw(`import.meta.url`)
|
|
11
|
+
|
|
12
|
+
@schema
|
|
13
|
+
type event =
|
|
14
|
+
| MemberNamed({
|
|
15
|
+
memberId: @s.matches(Reventless.DcbTag.string) string,
|
|
16
|
+
name: @s.matches(Reventless.Pii.marked(~subject="memberId", S.string))
|
|
17
|
+
Reventless.Pii.t<string>,
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
@schema
|
|
21
|
+
type consumedEvent =
|
|
22
|
+
| MemberNamed({
|
|
23
|
+
memberId: @s.matches(Reventless.DcbTag.string) string,
|
|
24
|
+
name: @s.matches(Reventless.Pii.marked(~subject="memberId", S.string))
|
|
25
|
+
Reventless.Pii.t<string>,
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
@schema
|
|
29
|
+
type command = Name({memberId: @s.matches(Reventless.DcbTag.string) string, name: string})
|
|
30
|
+
|
|
31
|
+
@schema
|
|
32
|
+
type error = Never
|
|
33
|
+
|
|
34
|
+
type state = option<Reventless.Pii.t<string>>
|
|
35
|
+
let initialState = None
|
|
36
|
+
let evolve = (_state, event) =>
|
|
37
|
+
switch event {
|
|
38
|
+
| MemberNamed({name}) => Some(name)
|
|
39
|
+
}
|
|
40
|
+
// Naming a member the same again is a no-op: only a history read back as the
|
|
41
|
+
// value, opened, can tell.
|
|
42
|
+
let decide = (state, command): result<array<event>, error> =>
|
|
43
|
+
switch (state, command) {
|
|
44
|
+
| (Some(Reventless.Pii.Value(held)), Name({name})) if held == name => Ok([])
|
|
45
|
+
| (_, Name({memberId, name})) => Ok([MemberNamed({memberId, name: Value(name)})])
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
module Decision = {
|
|
50
|
+
type state = MemberSpec.state
|
|
51
|
+
let initialState = MemberSpec.initialState
|
|
52
|
+
let evolve = MemberSpec.evolve
|
|
53
|
+
let decide = MemberSpec.decide
|
|
54
|
+
let moduleUrl = MemberSpec.moduleUrl
|
|
55
|
+
}
|
|
56
|
+
module Handler = StateChangeSlice_Callback.Make(MemberSpec, Decision)
|
|
57
|
+
|
|
58
|
+
let mock = DcbFixtures.makeMockStorage()
|
|
59
|
+
module Ops = DcbEventLog_Operations.Make({
|
|
60
|
+
let name = "TestDcbEventLog"
|
|
61
|
+
let serviceName = "TestDcbEventLog"
|
|
62
|
+
let storage = mock.operations
|
|
63
|
+
let publishJson = mock.mockPublishJson
|
|
64
|
+
})
|
|
65
|
+
let log: DcbEventLog.operations = {
|
|
66
|
+
read: Ops.read,
|
|
67
|
+
append: Ops.append,
|
|
68
|
+
readStream: Ops.readStream,
|
|
69
|
+
appendStream: Ops.appendStream,
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
let toBase64 = (s: string) =>
|
|
73
|
+
NodeCrypto.bufferFromString(s, "utf8")->NodeCrypto.bufferToString("base64")
|
|
74
|
+
let fromBase64 = (s: string) =>
|
|
75
|
+
NodeCrypto.bufferFromString(s, "base64")->NodeCrypto.bufferToString("utf8")
|
|
76
|
+
|
|
77
|
+
let fakeProvider = (~seals=true): Reventless.Pii.provider => {
|
|
78
|
+
name: "fake",
|
|
79
|
+
encrypts: true,
|
|
80
|
+
seal: async (~subject, ~field as _, ~forgettable as _, json) =>
|
|
81
|
+
seals
|
|
82
|
+
? Ok(Reventless.Pii.wrap(`enc:v1:${subject}:fake:${json->JSON.stringify->toBase64}`))
|
|
83
|
+
: Ok(json),
|
|
84
|
+
prefetch: async _ => Ok(),
|
|
85
|
+
openNow: (~field, json) =>
|
|
86
|
+
switch json->Reventless.Pii.unwrap->Option.flatMap(Reventless.Pii.parseStored) {
|
|
87
|
+
| Some(Cipher({bytes})) => Ok(bytes->fromBase64->JSON.parseOrThrow)
|
|
88
|
+
| _ => Error(Reventless.Pii.UnknownForm({field: field}))
|
|
89
|
+
},
|
|
90
|
+
release: async (~subject as _, ~ref as _) => Ok(),
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
let send = async (reference, command) =>
|
|
94
|
+
await Handler.handleCommands(
|
|
95
|
+
log,
|
|
96
|
+
Stream.fromIterable([
|
|
97
|
+
(
|
|
98
|
+
{
|
|
99
|
+
command: {
|
|
100
|
+
id: Reventless.Id.String.makeFromString("cmd-" ++ reference),
|
|
101
|
+
meta: DcbFixtures.testMeta,
|
|
102
|
+
command,
|
|
103
|
+
},
|
|
104
|
+
reference,
|
|
105
|
+
}: CommandTopic.topicItem<Message.command'<Reventless.Id.String.t, MemberSpec.command>>
|
|
106
|
+
),
|
|
107
|
+
]),
|
|
108
|
+
)->Effect.runPromise
|
|
109
|
+
|
|
110
|
+
let field = (json, name) => json->JSON.Decode.object->Option.flatMap(d => d->Dict.get(name))
|
|
111
|
+
|
|
112
|
+
describe("a DCB slice with a personal-data provider", () => {
|
|
113
|
+
beforeEach(() => {
|
|
114
|
+
mock.reset()
|
|
115
|
+
Handler.resetCache()
|
|
116
|
+
})
|
|
117
|
+
afterEach(() => Reventless.Pii.Provider.install(Reventless.Pii.plain))
|
|
118
|
+
|
|
119
|
+
testPromise("stores and publishes one sealed form, and decides on the opened value", async () => {
|
|
120
|
+
Reventless.Pii.Provider.install(fakeProvider())
|
|
121
|
+
let _ = await send("r1", Name({memberId: "m-1", name: "Ada"}))
|
|
122
|
+
let stored = (mock.getEvents()->Array.getUnsafe(0)).data->field("name")
|
|
123
|
+
expect(stored->Option.flatMap(Reventless.Pii.unwrap)->Option.isSome)->toBe(true)
|
|
124
|
+
let published =
|
|
125
|
+
(mock.publishedEvents.contents->Array.getUnsafe(0)).json
|
|
126
|
+
->field("event")
|
|
127
|
+
->Option.flatMap(e => e->field("name"))
|
|
128
|
+
expect(published)->toEqual(stored)
|
|
129
|
+
// Read back cold: only an opened history makes the same name a no-op.
|
|
130
|
+
Handler.resetCache()
|
|
131
|
+
let _ = await send("r2", Name({memberId: "m-1", name: "Ada"}))
|
|
132
|
+
expect(mock.getEvents()->Array.length)->toBe(1)
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
testPromise("refuses an append whose personal field comes out unsealed", async () => {
|
|
136
|
+
Reventless.Pii.Provider.install(fakeProvider(~seals=false))
|
|
137
|
+
let results = await send("r1", Name({memberId: "m-1", name: "Ada"}))
|
|
138
|
+
expect(results->Array.every(Result.isError))->toBe(true)
|
|
139
|
+
expect(mock.getEvents())->toEqual([])
|
|
140
|
+
expect(mock.publishedEvents.contents)->toEqual([])
|
|
141
|
+
})
|
|
142
|
+
})
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as Sury from "sury";
|
|
4
|
+
import * as Stdlib_JSON from "@rescript/runtime/lib/es6/Stdlib_JSON.js";
|
|
5
|
+
import * as Nodebuffer from "node:buffer";
|
|
6
|
+
import * as Id$Reventless from "@reventlessdev/reventless-spec/src/types/Id.res.mjs";
|
|
7
|
+
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
8
|
+
import * as Stdlib_Result from "@rescript/runtime/lib/es6/Stdlib_Result.js";
|
|
9
|
+
import * as Effect from "effect/Effect";
|
|
10
|
+
import * as Stream from "effect/Stream";
|
|
11
|
+
import * as Pii$Reventless from "@reventlessdev/reventless-spec/src/components/Pii.res.mjs";
|
|
12
|
+
import * as DcbTag$Reventless from "@reventlessdev/reventless-spec/src/components/DcbTag.res.mjs";
|
|
13
|
+
import * as DcbFixtures$ReventlessCore from "./DcbFixtures.res.mjs";
|
|
14
|
+
import * as DcbEventLog_Operations$ReventlessCore from "../../src/components/DcbEventLog/DcbEventLog_Operations.res.mjs";
|
|
15
|
+
import * as StateChangeSlice_Callback$ReventlessCore from "../../src/components/StateChangeSlice/StateChangeSlice_Callback.res.mjs";
|
|
16
|
+
|
|
17
|
+
let name = "TestMemberNames";
|
|
18
|
+
|
|
19
|
+
let moduleUrl = import.meta.url;
|
|
20
|
+
|
|
21
|
+
let eventSchema = Sury.$schema(s => ({
|
|
22
|
+
TAG: "MemberNamed",
|
|
23
|
+
memberId: s.m(DcbTag$Reventless.string),
|
|
24
|
+
name: s.m(Pii$Reventless.marked("memberId", undefined, Sury.string))
|
|
25
|
+
}));
|
|
26
|
+
|
|
27
|
+
let consumedEventSchema = Sury.$schema(s => ({
|
|
28
|
+
TAG: "MemberNamed",
|
|
29
|
+
memberId: s.m(DcbTag$Reventless.string),
|
|
30
|
+
name: s.m(Pii$Reventless.marked("memberId", undefined, Sury.string))
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
let commandSchema = Sury.$schema(s => ({
|
|
34
|
+
TAG: "Name",
|
|
35
|
+
memberId: s.m(DcbTag$Reventless.string),
|
|
36
|
+
name: s.m(Sury.string)
|
|
37
|
+
}));
|
|
38
|
+
|
|
39
|
+
let errorSchema = Sury.literal("Never");
|
|
40
|
+
|
|
41
|
+
function evolve(_state, event) {
|
|
42
|
+
return event.name;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function decide(state, command) {
|
|
46
|
+
if (state !== undefined) {
|
|
47
|
+
if (typeof state !== "object") {
|
|
48
|
+
state === "Erased";
|
|
49
|
+
} else if (state._0 === command.name) {
|
|
50
|
+
return {
|
|
51
|
+
TAG: "Ok",
|
|
52
|
+
_0: []
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
TAG: "Ok",
|
|
58
|
+
_0: [{
|
|
59
|
+
TAG: "MemberNamed",
|
|
60
|
+
memberId: command.memberId,
|
|
61
|
+
name: {
|
|
62
|
+
TAG: "Value",
|
|
63
|
+
_0: command.name
|
|
64
|
+
}
|
|
65
|
+
}]
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function authorizationOf(param) {
|
|
70
|
+
return "AllowAuthenticated";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function commandTransition(param) {
|
|
74
|
+
return "Undeclared";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let traits = [];
|
|
78
|
+
|
|
79
|
+
let MemberSpec = {
|
|
80
|
+
name: name,
|
|
81
|
+
Id: undefined,
|
|
82
|
+
moduleUrl: moduleUrl,
|
|
83
|
+
eventSchema: eventSchema,
|
|
84
|
+
consumedEventSchema: consumedEventSchema,
|
|
85
|
+
commandSchema: commandSchema,
|
|
86
|
+
errorSchema: errorSchema,
|
|
87
|
+
initialState: undefined,
|
|
88
|
+
evolve: evolve,
|
|
89
|
+
decide: decide,
|
|
90
|
+
authorizationOf: authorizationOf,
|
|
91
|
+
commandTransition: commandTransition,
|
|
92
|
+
traits: traits,
|
|
93
|
+
readConsistency: "EscalateOnRetry"
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
let Decision = {
|
|
97
|
+
initialState: undefined,
|
|
98
|
+
evolve: evolve,
|
|
99
|
+
decide: decide,
|
|
100
|
+
moduleUrl: moduleUrl
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
let Handler = StateChangeSlice_Callback$ReventlessCore.Make({
|
|
104
|
+
name: name,
|
|
105
|
+
moduleUrl: moduleUrl,
|
|
106
|
+
consumedEventSchema: consumedEventSchema,
|
|
107
|
+
errorSchema: errorSchema,
|
|
108
|
+
eventSchema: eventSchema,
|
|
109
|
+
commandSchema: commandSchema,
|
|
110
|
+
authorizationOf: authorizationOf,
|
|
111
|
+
commandTransition: commandTransition,
|
|
112
|
+
traits: traits,
|
|
113
|
+
readConsistency: "EscalateOnRetry"
|
|
114
|
+
})(Decision);
|
|
115
|
+
|
|
116
|
+
let mock = DcbFixtures$ReventlessCore.makeMockStorage();
|
|
117
|
+
|
|
118
|
+
let storage = mock.operations;
|
|
119
|
+
|
|
120
|
+
let publishJson = mock.mockPublishJson;
|
|
121
|
+
|
|
122
|
+
let Ops = DcbEventLog_Operations$ReventlessCore.Make({
|
|
123
|
+
name: "TestDcbEventLog",
|
|
124
|
+
serviceName: "TestDcbEventLog",
|
|
125
|
+
storage: storage,
|
|
126
|
+
publishJson: publishJson
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
let log_read = Ops.read;
|
|
130
|
+
|
|
131
|
+
let log_append = Ops.append;
|
|
132
|
+
|
|
133
|
+
let log_readStream = Ops.readStream;
|
|
134
|
+
|
|
135
|
+
let log_appendStream = Ops.appendStream;
|
|
136
|
+
|
|
137
|
+
let log = {
|
|
138
|
+
read: log_read,
|
|
139
|
+
append: log_append,
|
|
140
|
+
readStream: log_readStream,
|
|
141
|
+
appendStream: log_appendStream
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
function toBase64(s) {
|
|
145
|
+
return Nodebuffer.Buffer.from(s, "utf8").toString("base64");
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function fromBase64(s) {
|
|
149
|
+
return Nodebuffer.Buffer.from(s, "base64").toString("utf8");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function fakeProvider(sealsOpt) {
|
|
153
|
+
let seals = sealsOpt !== undefined ? sealsOpt : true;
|
|
154
|
+
return {
|
|
155
|
+
name: "fake",
|
|
156
|
+
encrypts: true,
|
|
157
|
+
seal: async (subject, param, param$1, json) => {
|
|
158
|
+
if (seals) {
|
|
159
|
+
return {
|
|
160
|
+
TAG: "Ok",
|
|
161
|
+
_0: Pii$Reventless.wrap(`enc:v1:` + subject + `:fake:` + toBase64(JSON.stringify(json)))
|
|
162
|
+
};
|
|
163
|
+
} else {
|
|
164
|
+
return {
|
|
165
|
+
TAG: "Ok",
|
|
166
|
+
_0: json
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
prefetch: async param => ({
|
|
171
|
+
TAG: "Ok",
|
|
172
|
+
_0: undefined
|
|
173
|
+
}),
|
|
174
|
+
openNow: (field, json) => {
|
|
175
|
+
let match = Stdlib_Option.flatMap(Pii$Reventless.unwrap(json), Pii$Reventless.parseStored);
|
|
176
|
+
if (match === undefined) {
|
|
177
|
+
return {
|
|
178
|
+
TAG: "Error",
|
|
179
|
+
_0: {
|
|
180
|
+
TAG: "UnknownForm",
|
|
181
|
+
field: field
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
switch (match.TAG) {
|
|
186
|
+
case "Cipher" :
|
|
187
|
+
return {
|
|
188
|
+
TAG: "Ok",
|
|
189
|
+
_0: JSON.parse(fromBase64(match.bytes))
|
|
190
|
+
};
|
|
191
|
+
case "ErasedForm" :
|
|
192
|
+
case "ForgottenForm" :
|
|
193
|
+
return {
|
|
194
|
+
TAG: "Error",
|
|
195
|
+
_0: {
|
|
196
|
+
TAG: "UnknownForm",
|
|
197
|
+
field: field
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
release: async (param, param$1) => ({
|
|
203
|
+
TAG: "Ok",
|
|
204
|
+
_0: undefined
|
|
205
|
+
})
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async function send(reference, command) {
|
|
210
|
+
return await Effect.runPromise(Handler.handleCommands(undefined, undefined, undefined, log, Stream.fromIterable([{
|
|
211
|
+
command: {
|
|
212
|
+
id: Id$Reventless.$$String.makeFromString("cmd-" + reference),
|
|
213
|
+
meta: DcbFixtures$ReventlessCore.testMeta,
|
|
214
|
+
command: command
|
|
215
|
+
},
|
|
216
|
+
reference: reference
|
|
217
|
+
}])));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function field(json, name) {
|
|
221
|
+
return Stdlib_Option.flatMap(Stdlib_JSON.Decode.object(json), d => d[name]);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
globalThis.describe("a DCB slice with a personal-data provider", () => {
|
|
225
|
+
globalThis.beforeEach(() => {
|
|
226
|
+
mock.reset();
|
|
227
|
+
Handler.resetCache();
|
|
228
|
+
});
|
|
229
|
+
globalThis.afterEach(() => Pii$Reventless.Provider.install(Pii$Reventless.plain));
|
|
230
|
+
globalThis.test("stores and publishes one sealed form, and decides on the opened value", async () => {
|
|
231
|
+
Pii$Reventless.Provider.install(fakeProvider(undefined));
|
|
232
|
+
await send("r1", {
|
|
233
|
+
TAG: "Name",
|
|
234
|
+
memberId: "m-1",
|
|
235
|
+
name: "Ada"
|
|
236
|
+
});
|
|
237
|
+
let stored = field(mock.getEvents()[0].data, "name");
|
|
238
|
+
globalThis.expect(Stdlib_Option.isSome(Stdlib_Option.flatMap(stored, Pii$Reventless.unwrap))).toBe(true);
|
|
239
|
+
let published = Stdlib_Option.flatMap(field(mock.publishedEvents.contents[0].json, "event"), e => field(e, "name"));
|
|
240
|
+
globalThis.expect(published).toEqual(stored);
|
|
241
|
+
Handler.resetCache();
|
|
242
|
+
await send("r2", {
|
|
243
|
+
TAG: "Name",
|
|
244
|
+
memberId: "m-1",
|
|
245
|
+
name: "Ada"
|
|
246
|
+
});
|
|
247
|
+
globalThis.expect(mock.getEvents().length).toBe(1);
|
|
248
|
+
});
|
|
249
|
+
globalThis.test("refuses an append whose personal field comes out unsealed", async () => {
|
|
250
|
+
Pii$Reventless.Provider.install(fakeProvider(false));
|
|
251
|
+
let results = await send("r1", {
|
|
252
|
+
TAG: "Name",
|
|
253
|
+
memberId: "m-1",
|
|
254
|
+
name: "Ada"
|
|
255
|
+
});
|
|
256
|
+
globalThis.expect(results.every(Stdlib_Result.isError)).toBe(true);
|
|
257
|
+
globalThis.expect(mock.getEvents()).toEqual([]);
|
|
258
|
+
globalThis.expect(mock.publishedEvents.contents).toEqual([]);
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
export {
|
|
263
|
+
MemberSpec,
|
|
264
|
+
Decision,
|
|
265
|
+
Handler,
|
|
266
|
+
mock,
|
|
267
|
+
Ops,
|
|
268
|
+
log,
|
|
269
|
+
toBase64,
|
|
270
|
+
fromBase64,
|
|
271
|
+
fakeProvider,
|
|
272
|
+
send,
|
|
273
|
+
field,
|
|
274
|
+
}
|
|
275
|
+
/* moduleUrl Not a pure module */
|
|
@@ -7,7 +7,14 @@ module ItemEventLogSpec = {
|
|
|
7
7
|
let name = "TestItemEventLog"
|
|
8
8
|
|
|
9
9
|
@schema
|
|
10
|
-
type event =
|
|
10
|
+
type event =
|
|
11
|
+
| ItemCreated({name: string})
|
|
12
|
+
| ItemDeleted({id: string})
|
|
13
|
+
| PersonNamed({
|
|
14
|
+
personId: string,
|
|
15
|
+
name: @s.matches(Reventless.Pii.marked(~subject="personId", S.string))
|
|
16
|
+
Reventless.Pii.t<string>,
|
|
17
|
+
})
|
|
11
18
|
}
|
|
12
19
|
|
|
13
20
|
// ─────────────────────────────────────────────────────────────
|
|
@@ -64,7 +71,9 @@ let mockStorage: EventLog_Adapter.operations = {
|
|
|
64
71
|
// Mock EventTopic module (stub make — never called in unit tests)
|
|
65
72
|
// ─────────────────────────────────────────────────────────────
|
|
66
73
|
|
|
67
|
-
|
|
74
|
+
// What the event log published, as the JSON the topic received: the event log
|
|
75
|
+
// publishes the form it stored, so a test can compare the two.
|
|
76
|
+
let capturedPublishes: ref<array<JSON.t>> = ref([])
|
|
68
77
|
|
|
69
78
|
module MockEventTopic: EventTopic.T
|
|
70
79
|
with module Spec.Id = ItemEventLogSpec.Id
|
|
@@ -87,10 +96,10 @@ module MockEventTopic: EventTopic.T
|
|
|
87
96
|
}
|
|
88
97
|
|
|
89
98
|
let mockEventTopicOps: MockEventTopic.operations = {
|
|
90
|
-
publish: async
|
|
91
|
-
|
|
99
|
+
publish: async _events => (),
|
|
100
|
+
publishJson: async (_service, _meta, json) => {
|
|
101
|
+
capturedPublishes := capturedPublishes.contents->Array.concat([json])
|
|
92
102
|
},
|
|
93
|
-
publishJson: async (_service, _meta, _json) => (),
|
|
94
103
|
publishJsonStream: _stream => Effect.succeed(),
|
|
95
104
|
}
|
|
96
105
|
|
|
@@ -5,6 +5,7 @@ import * as Id$Reventless from "@reventlessdev/reventless-spec/src/types/Id.res.
|
|
|
5
5
|
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
6
6
|
import * as Effect from "effect/Effect";
|
|
7
7
|
import * as Stream from "effect/Stream";
|
|
8
|
+
import * as Pii$Reventless from "@reventlessdev/reventless-spec/src/components/Pii.res.mjs";
|
|
8
9
|
|
|
9
10
|
let eventSchema = Sury.union([
|
|
10
11
|
Sury.$schema(s => ({
|
|
@@ -14,6 +15,11 @@ let eventSchema = Sury.union([
|
|
|
14
15
|
Sury.$schema(s => ({
|
|
15
16
|
TAG: "ItemDeleted",
|
|
16
17
|
id: s.m(Sury.string)
|
|
18
|
+
})),
|
|
19
|
+
Sury.$schema(s => ({
|
|
20
|
+
TAG: "PersonNamed",
|
|
21
|
+
personId: s.m(Sury.string),
|
|
22
|
+
name: s.m(Pii$Reventless.marked("personId", undefined, Sury.string))
|
|
17
23
|
}))
|
|
18
24
|
]);
|
|
19
25
|
|
|
@@ -132,12 +138,12 @@ let MockEventTopic = {
|
|
|
132
138
|
make: make
|
|
133
139
|
};
|
|
134
140
|
|
|
135
|
-
async function mockEventTopicOps_publish(
|
|
136
|
-
|
|
141
|
+
async function mockEventTopicOps_publish(_events) {
|
|
142
|
+
|
|
137
143
|
}
|
|
138
144
|
|
|
139
|
-
async function mockEventTopicOps_publishJson(_service, _meta,
|
|
140
|
-
|
|
145
|
+
async function mockEventTopicOps_publishJson(_service, _meta, json) {
|
|
146
|
+
capturedPublishes.contents = capturedPublishes.contents.concat([json]);
|
|
141
147
|
}
|
|
142
148
|
|
|
143
149
|
function mockEventTopicOps_publishJsonStream(_stream) {
|