@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/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,141 @@
|
|
|
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
|
+
open_: async (~field, json) =>
|
|
85
|
+
switch json->Reventless.Pii.unwrap->Option.flatMap(Reventless.Pii.parseStored) {
|
|
86
|
+
| Some(Cipher({bytes})) => Ok(bytes->fromBase64->JSON.parseOrThrow)
|
|
87
|
+
| _ => Error(Reventless.Pii.UnknownForm({field: field}))
|
|
88
|
+
},
|
|
89
|
+
release: async (~subject as _, ~ref as _) => Ok(),
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
let send = async (reference, command) =>
|
|
93
|
+
await Handler.handleCommands(
|
|
94
|
+
log,
|
|
95
|
+
Stream.fromIterable([
|
|
96
|
+
(
|
|
97
|
+
{
|
|
98
|
+
command: {
|
|
99
|
+
id: Reventless.Id.String.makeFromString("cmd-" ++ reference),
|
|
100
|
+
meta: DcbFixtures.testMeta,
|
|
101
|
+
command,
|
|
102
|
+
},
|
|
103
|
+
reference,
|
|
104
|
+
}: CommandTopic.topicItem<Message.command'<Reventless.Id.String.t, MemberSpec.command>>
|
|
105
|
+
),
|
|
106
|
+
]),
|
|
107
|
+
)->Effect.runPromise
|
|
108
|
+
|
|
109
|
+
let field = (json, name) => json->JSON.Decode.object->Option.flatMap(d => d->Dict.get(name))
|
|
110
|
+
|
|
111
|
+
describe("a DCB slice with a personal-data provider", () => {
|
|
112
|
+
beforeEach(() => {
|
|
113
|
+
mock.reset()
|
|
114
|
+
Handler.resetCache()
|
|
115
|
+
})
|
|
116
|
+
afterEach(() => Reventless.Pii.Provider.install(Reventless.Pii.plain))
|
|
117
|
+
|
|
118
|
+
testPromise("stores and publishes one sealed form, and decides on the opened value", async () => {
|
|
119
|
+
Reventless.Pii.Provider.install(fakeProvider())
|
|
120
|
+
let _ = await send("r1", Name({memberId: "m-1", name: "Ada"}))
|
|
121
|
+
let stored = (mock.getEvents()->Array.getUnsafe(0)).data->field("name")
|
|
122
|
+
expect(stored->Option.flatMap(Reventless.Pii.unwrap)->Option.isSome)->toBe(true)
|
|
123
|
+
let published =
|
|
124
|
+
(mock.publishedEvents.contents->Array.getUnsafe(0)).json
|
|
125
|
+
->field("event")
|
|
126
|
+
->Option.flatMap(e => e->field("name"))
|
|
127
|
+
expect(published)->toEqual(stored)
|
|
128
|
+
// Read back cold: only an opened history makes the same name a no-op.
|
|
129
|
+
Handler.resetCache()
|
|
130
|
+
let _ = await send("r2", Name({memberId: "m-1", name: "Ada"}))
|
|
131
|
+
expect(mock.getEvents()->Array.length)->toBe(1)
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
testPromise("refuses an append whose personal field comes out unsealed", async () => {
|
|
135
|
+
Reventless.Pii.Provider.install(fakeProvider(~seals=false))
|
|
136
|
+
let results = await send("r1", Name({memberId: "m-1", name: "Ada"}))
|
|
137
|
+
expect(results->Array.every(Result.isError))->toBe(true)
|
|
138
|
+
expect(mock.getEvents())->toEqual([])
|
|
139
|
+
expect(mock.publishedEvents.contents)->toEqual([])
|
|
140
|
+
})
|
|
141
|
+
})
|
|
@@ -0,0 +1,271 @@
|
|
|
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
|
+
open: async (field, json) => {
|
|
171
|
+
let match = Stdlib_Option.flatMap(Pii$Reventless.unwrap(json), Pii$Reventless.parseStored);
|
|
172
|
+
if (match === undefined) {
|
|
173
|
+
return {
|
|
174
|
+
TAG: "Error",
|
|
175
|
+
_0: {
|
|
176
|
+
TAG: "UnknownForm",
|
|
177
|
+
field: field
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
switch (match.TAG) {
|
|
182
|
+
case "Cipher" :
|
|
183
|
+
return {
|
|
184
|
+
TAG: "Ok",
|
|
185
|
+
_0: JSON.parse(fromBase64(match.bytes))
|
|
186
|
+
};
|
|
187
|
+
case "ErasedForm" :
|
|
188
|
+
case "ForgottenForm" :
|
|
189
|
+
return {
|
|
190
|
+
TAG: "Error",
|
|
191
|
+
_0: {
|
|
192
|
+
TAG: "UnknownForm",
|
|
193
|
+
field: field
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
release: async (param, param$1) => ({
|
|
199
|
+
TAG: "Ok",
|
|
200
|
+
_0: undefined
|
|
201
|
+
})
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async function send(reference, command) {
|
|
206
|
+
return await Effect.runPromise(Handler.handleCommands(undefined, undefined, undefined, log, Stream.fromIterable([{
|
|
207
|
+
command: {
|
|
208
|
+
id: Id$Reventless.$$String.makeFromString("cmd-" + reference),
|
|
209
|
+
meta: DcbFixtures$ReventlessCore.testMeta,
|
|
210
|
+
command: command
|
|
211
|
+
},
|
|
212
|
+
reference: reference
|
|
213
|
+
}])));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function field(json, name) {
|
|
217
|
+
return Stdlib_Option.flatMap(Stdlib_JSON.Decode.object(json), d => d[name]);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
globalThis.describe("a DCB slice with a personal-data provider", () => {
|
|
221
|
+
globalThis.beforeEach(() => {
|
|
222
|
+
mock.reset();
|
|
223
|
+
Handler.resetCache();
|
|
224
|
+
});
|
|
225
|
+
globalThis.afterEach(() => Pii$Reventless.Provider.install(Pii$Reventless.plain));
|
|
226
|
+
globalThis.test("stores and publishes one sealed form, and decides on the opened value", async () => {
|
|
227
|
+
Pii$Reventless.Provider.install(fakeProvider(undefined));
|
|
228
|
+
await send("r1", {
|
|
229
|
+
TAG: "Name",
|
|
230
|
+
memberId: "m-1",
|
|
231
|
+
name: "Ada"
|
|
232
|
+
});
|
|
233
|
+
let stored = field(mock.getEvents()[0].data, "name");
|
|
234
|
+
globalThis.expect(Stdlib_Option.isSome(Stdlib_Option.flatMap(stored, Pii$Reventless.unwrap))).toBe(true);
|
|
235
|
+
let published = Stdlib_Option.flatMap(field(mock.publishedEvents.contents[0].json, "event"), e => field(e, "name"));
|
|
236
|
+
globalThis.expect(published).toEqual(stored);
|
|
237
|
+
Handler.resetCache();
|
|
238
|
+
await send("r2", {
|
|
239
|
+
TAG: "Name",
|
|
240
|
+
memberId: "m-1",
|
|
241
|
+
name: "Ada"
|
|
242
|
+
});
|
|
243
|
+
globalThis.expect(mock.getEvents().length).toBe(1);
|
|
244
|
+
});
|
|
245
|
+
globalThis.test("refuses an append whose personal field comes out unsealed", async () => {
|
|
246
|
+
Pii$Reventless.Provider.install(fakeProvider(false));
|
|
247
|
+
let results = await send("r1", {
|
|
248
|
+
TAG: "Name",
|
|
249
|
+
memberId: "m-1",
|
|
250
|
+
name: "Ada"
|
|
251
|
+
});
|
|
252
|
+
globalThis.expect(results.every(Stdlib_Result.isError)).toBe(true);
|
|
253
|
+
globalThis.expect(mock.getEvents()).toEqual([]);
|
|
254
|
+
globalThis.expect(mock.publishedEvents.contents).toEqual([]);
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
export {
|
|
259
|
+
MemberSpec,
|
|
260
|
+
Decision,
|
|
261
|
+
Handler,
|
|
262
|
+
mock,
|
|
263
|
+
Ops,
|
|
264
|
+
log,
|
|
265
|
+
toBase64,
|
|
266
|
+
fromBase64,
|
|
267
|
+
fakeProvider,
|
|
268
|
+
send,
|
|
269
|
+
field,
|
|
270
|
+
}
|
|
271
|
+
/* 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) {
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// The event log seals a personal field once, stores and publishes that one form,
|
|
2
|
+
// opens it on replay, and refuses an append whose field comes out unsealed.
|
|
3
|
+
|
|
4
|
+
open JestGlobals
|
|
5
|
+
open EventLogFixtures
|
|
6
|
+
|
|
7
|
+
module TestOps: EventLog_Operations.Ops with module Spec = ItemEventLogSpec = {
|
|
8
|
+
module Spec = ItemEventLogSpec
|
|
9
|
+
module EventTopic = MockEventTopic
|
|
10
|
+
let eventTopic = mockEventTopicOps
|
|
11
|
+
let storage = mockStorage
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
module Log = EventLog_Operations.Make(ItemEventLogSpec, TestOps)
|
|
15
|
+
|
|
16
|
+
let toBase64 = (s: string) =>
|
|
17
|
+
NodeCrypto.bufferFromString(s, "utf8")->NodeCrypto.bufferToString("base64")
|
|
18
|
+
let fromBase64 = (s: string) =>
|
|
19
|
+
NodeCrypto.bufferFromString(s, "base64")->NodeCrypto.bufferToString("utf8")
|
|
20
|
+
|
|
21
|
+
/** A provider that "encrypts" by base64: enough to see the form, the subject and
|
|
22
|
+
the round trip, with nothing to fetch. */
|
|
23
|
+
let fakeProvider = (~seals=true): Reventless.Pii.provider => {
|
|
24
|
+
name: "fake",
|
|
25
|
+
encrypts: true,
|
|
26
|
+
seal: async (~subject, ~field as _, ~forgettable as _, json) =>
|
|
27
|
+
seals
|
|
28
|
+
? Ok(Reventless.Pii.wrap(`enc:v1:${subject}:fake:${json->JSON.stringify->toBase64}`))
|
|
29
|
+
: Ok(json),
|
|
30
|
+
open_: async (~field, json) =>
|
|
31
|
+
switch json->Reventless.Pii.unwrap->Option.flatMap(Reventless.Pii.parseStored) {
|
|
32
|
+
| Some(Cipher({bytes})) => Ok(bytes->fromBase64->JSON.parseOrThrow)
|
|
33
|
+
| _ => Error(Reventless.Pii.UnknownForm({field: field}))
|
|
34
|
+
},
|
|
35
|
+
release: async (~subject as _, ~ref as _) => Ok(),
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
let named: ItemEventLogSpec.event = PersonNamed({personId: "p-1", name: Value("Ada Lovelace")})
|
|
39
|
+
|
|
40
|
+
let storedName = id =>
|
|
41
|
+
storedEvents.contents
|
|
42
|
+
->Dict.get(id)
|
|
43
|
+
->Option.flatMap(a => a->Array.get(0))
|
|
44
|
+
->Option.flatMap(JSON.Decode.object)
|
|
45
|
+
->Option.flatMap(d => d->Dict.get("data"))
|
|
46
|
+
->Option.flatMap(JSON.Decode.object)
|
|
47
|
+
->Option.flatMap(d => d->Dict.get("name"))
|
|
48
|
+
|
|
49
|
+
let publishedName = () =>
|
|
50
|
+
capturedPublishes.contents
|
|
51
|
+
->Array.get(0)
|
|
52
|
+
->Option.flatMap(JSON.Decode.object)
|
|
53
|
+
->Option.flatMap(d => d->Dict.get("event"))
|
|
54
|
+
->Option.flatMap(JSON.Decode.object)
|
|
55
|
+
->Option.flatMap(d => d->Dict.get("name"))
|
|
56
|
+
|
|
57
|
+
describe("EventLog with a personal-data provider", () => {
|
|
58
|
+
beforeEach(() => reset())
|
|
59
|
+
afterEach(() => Reventless.Pii.Provider.install(Reventless.Pii.plain))
|
|
60
|
+
|
|
61
|
+
testPromise("stores and publishes one sealed form, and replays the value", async () => {
|
|
62
|
+
Reventless.Pii.Provider.install(fakeProvider())
|
|
63
|
+
let result = await Log.append(0, "p-1", [makeEvent'("p-1", named)])
|
|
64
|
+
expect(result)->toEqual(Ok())
|
|
65
|
+
let stored = storedName("p-1")
|
|
66
|
+
expect(stored->Option.flatMap(Reventless.Pii.unwrap)->Option.isSome)->toBe(true)
|
|
67
|
+
expect(
|
|
68
|
+
stored->Option.map(j => j->JSON.stringify)->Option.getOr("")->String.includes("Ada"),
|
|
69
|
+
)->toBe(false)
|
|
70
|
+
expect(publishedName())->toEqual(stored)
|
|
71
|
+
let replayed = await Log.replay("p-1")
|
|
72
|
+
expect(replayed)->toEqual([named])
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
testPromise("refuses an append whose personal field comes out unsealed", async () => {
|
|
76
|
+
Reventless.Pii.Provider.install(fakeProvider(~seals=false))
|
|
77
|
+
let result = await Log.append(0, "p-1", [makeEvent'("p-1", named)])
|
|
78
|
+
switch result {
|
|
79
|
+
| Error(EventLog.StorageFailure(msg)) =>
|
|
80
|
+
expect(msg->String.includes("name is not sealed"))->toBe(true)
|
|
81
|
+
expect(msg->String.includes("Ada"))->toBe(false)
|
|
82
|
+
| _ => fail("expected a refusal")
|
|
83
|
+
}
|
|
84
|
+
expect(storedEvents.contents->Dict.get("p-1"))->toEqual(None)
|
|
85
|
+
expect(capturedPublishes.contents)->toEqual([])
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
testPromise("with the plain provider the value is stored as it is", async () => {
|
|
89
|
+
let _ = await Log.append(0, "p-1", [makeEvent'("p-1", named)])
|
|
90
|
+
expect(storedName("p-1"))->toEqual(Some(JSON.Encode.string("Ada Lovelace")))
|
|
91
|
+
expect(await Log.replay("p-1"))->toEqual([named])
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
testPromise("a value whose key is gone replays as Erased", async () => {
|
|
95
|
+
let _ = await Log.append(0, "p-1", [makeEvent'("p-1", named)])
|
|
96
|
+
// A masking sweep or a provider left the erased form in place of the value.
|
|
97
|
+
let item = storedEvents.contents->Dict.get("p-1")->Option.getOrThrow->Array.getUnsafe(0)
|
|
98
|
+
switch item {
|
|
99
|
+
| Object(d) =>
|
|
100
|
+
d->Dict.set(
|
|
101
|
+
"data",
|
|
102
|
+
Dict.fromArray([
|
|
103
|
+
("personId", JSON.Encode.string("p-1")),
|
|
104
|
+
("name", Reventless.Pii.wrap("erased:v1")),
|
|
105
|
+
])->JSON.Encode.object,
|
|
106
|
+
)
|
|
107
|
+
| _ => ()
|
|
108
|
+
}
|
|
109
|
+
expect(await Log.replay("p-1"))->toEqual([
|
|
110
|
+
ItemEventLogSpec.PersonNamed({personId: "p-1", name: Erased}),
|
|
111
|
+
])
|
|
112
|
+
})
|
|
113
|
+
})
|