@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
|
@@ -0,0 +1,114 @@
|
|
|
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
|
+
prefetch: async _ => Ok(),
|
|
31
|
+
openNow: (~field, json) =>
|
|
32
|
+
switch json->Reventless.Pii.unwrap->Option.flatMap(Reventless.Pii.parseStored) {
|
|
33
|
+
| Some(Cipher({bytes})) => Ok(bytes->fromBase64->JSON.parseOrThrow)
|
|
34
|
+
| _ => Error(Reventless.Pii.UnknownForm({field: field}))
|
|
35
|
+
},
|
|
36
|
+
release: async (~subject as _, ~ref as _) => Ok(),
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let named: ItemEventLogSpec.event = PersonNamed({personId: "p-1", name: Value("Ada Lovelace")})
|
|
40
|
+
|
|
41
|
+
let storedName = id =>
|
|
42
|
+
storedEvents.contents
|
|
43
|
+
->Dict.get(id)
|
|
44
|
+
->Option.flatMap(a => a->Array.get(0))
|
|
45
|
+
->Option.flatMap(JSON.Decode.object)
|
|
46
|
+
->Option.flatMap(d => d->Dict.get("data"))
|
|
47
|
+
->Option.flatMap(JSON.Decode.object)
|
|
48
|
+
->Option.flatMap(d => d->Dict.get("name"))
|
|
49
|
+
|
|
50
|
+
let publishedName = () =>
|
|
51
|
+
capturedPublishes.contents
|
|
52
|
+
->Array.get(0)
|
|
53
|
+
->Option.flatMap(JSON.Decode.object)
|
|
54
|
+
->Option.flatMap(d => d->Dict.get("event"))
|
|
55
|
+
->Option.flatMap(JSON.Decode.object)
|
|
56
|
+
->Option.flatMap(d => d->Dict.get("name"))
|
|
57
|
+
|
|
58
|
+
describe("EventLog with a personal-data provider", () => {
|
|
59
|
+
beforeEach(() => reset())
|
|
60
|
+
afterEach(() => Reventless.Pii.Provider.install(Reventless.Pii.plain))
|
|
61
|
+
|
|
62
|
+
testPromise("stores and publishes one sealed form, and replays the value", async () => {
|
|
63
|
+
Reventless.Pii.Provider.install(fakeProvider())
|
|
64
|
+
let result = await Log.append(0, "p-1", [makeEvent'("p-1", named)])
|
|
65
|
+
expect(result)->toEqual(Ok())
|
|
66
|
+
let stored = storedName("p-1")
|
|
67
|
+
expect(stored->Option.flatMap(Reventless.Pii.unwrap)->Option.isSome)->toBe(true)
|
|
68
|
+
expect(
|
|
69
|
+
stored->Option.map(j => j->JSON.stringify)->Option.getOr("")->String.includes("Ada"),
|
|
70
|
+
)->toBe(false)
|
|
71
|
+
expect(publishedName())->toEqual(stored)
|
|
72
|
+
let replayed = await Log.replay("p-1")
|
|
73
|
+
expect(replayed)->toEqual([named])
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
testPromise("refuses an append whose personal field comes out unsealed", async () => {
|
|
77
|
+
Reventless.Pii.Provider.install(fakeProvider(~seals=false))
|
|
78
|
+
let result = await Log.append(0, "p-1", [makeEvent'("p-1", named)])
|
|
79
|
+
switch result {
|
|
80
|
+
| Error(EventLog.StorageFailure(msg)) =>
|
|
81
|
+
expect(msg->String.includes("name is not sealed"))->toBe(true)
|
|
82
|
+
expect(msg->String.includes("Ada"))->toBe(false)
|
|
83
|
+
| _ => fail("expected a refusal")
|
|
84
|
+
}
|
|
85
|
+
expect(storedEvents.contents->Dict.get("p-1"))->toEqual(None)
|
|
86
|
+
expect(capturedPublishes.contents)->toEqual([])
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
testPromise("with the plain provider the value is stored as it is", async () => {
|
|
90
|
+
let _ = await Log.append(0, "p-1", [makeEvent'("p-1", named)])
|
|
91
|
+
expect(storedName("p-1"))->toEqual(Some(JSON.Encode.string("Ada Lovelace")))
|
|
92
|
+
expect(await Log.replay("p-1"))->toEqual([named])
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
testPromise("a value whose key is gone replays as Erased", async () => {
|
|
96
|
+
let _ = await Log.append(0, "p-1", [makeEvent'("p-1", named)])
|
|
97
|
+
// A masking sweep or a provider left the erased form in place of the value.
|
|
98
|
+
let item = storedEvents.contents->Dict.get("p-1")->Option.getOrThrow->Array.getUnsafe(0)
|
|
99
|
+
switch item {
|
|
100
|
+
| Object(d) =>
|
|
101
|
+
d->Dict.set(
|
|
102
|
+
"data",
|
|
103
|
+
Dict.fromArray([
|
|
104
|
+
("personId", JSON.Encode.string("p-1")),
|
|
105
|
+
("name", Reventless.Pii.wrap("erased:v1")),
|
|
106
|
+
])->JSON.Encode.object,
|
|
107
|
+
)
|
|
108
|
+
| _ => ()
|
|
109
|
+
}
|
|
110
|
+
expect(await Log.replay("p-1"))->toEqual([
|
|
111
|
+
ItemEventLogSpec.PersonNamed({personId: "p-1", name: Erased}),
|
|
112
|
+
])
|
|
113
|
+
})
|
|
114
|
+
})
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as JestGlobals from "@reventlessdev/rescript-jest/src/JestGlobals.res.mjs";
|
|
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 Pii$Reventless from "@reventlessdev/reventless-spec/src/components/Pii.res.mjs";
|
|
9
|
+
import * as EventLogFixtures$ReventlessCore from "./EventLogFixtures.res.mjs";
|
|
10
|
+
import * as EventLog_Operations$ReventlessCore from "../../src/components/EventLog/EventLog_Operations.res.mjs";
|
|
11
|
+
|
|
12
|
+
let $$let = EventLogFixtures$ReventlessCore.MockEventTopic.Spec;
|
|
13
|
+
|
|
14
|
+
let $$let$1 = $$let.Id;
|
|
15
|
+
|
|
16
|
+
let TestOps_Spec = {
|
|
17
|
+
Id: Id$Reventless.StringPure,
|
|
18
|
+
name: EventLogFixtures$ReventlessCore.ItemEventLogSpec.name,
|
|
19
|
+
eventSchema: EventLogFixtures$ReventlessCore.ItemEventLogSpec.eventSchema
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
let TestOps_EventTopic = {
|
|
23
|
+
Spec: {
|
|
24
|
+
Id: {
|
|
25
|
+
schema: $$let$1.schema,
|
|
26
|
+
make: prim => prim,
|
|
27
|
+
makeFromString: prim => prim,
|
|
28
|
+
toString: prim => prim,
|
|
29
|
+
cmp: $$let$1.cmp
|
|
30
|
+
},
|
|
31
|
+
name: $$let.name,
|
|
32
|
+
eventSchema: $$let.eventSchema
|
|
33
|
+
},
|
|
34
|
+
make: EventLogFixtures$ReventlessCore.MockEventTopic.make
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
let TestOps = {
|
|
38
|
+
Spec: TestOps_Spec,
|
|
39
|
+
EventTopic: TestOps_EventTopic,
|
|
40
|
+
eventTopic: EventLogFixtures$ReventlessCore.mockEventTopicOps,
|
|
41
|
+
storage: EventLogFixtures$ReventlessCore.mockStorage
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
let $$let$2 = TestOps_Spec;
|
|
45
|
+
|
|
46
|
+
let $$let$3 = $$let$2.Id;
|
|
47
|
+
|
|
48
|
+
let Log = EventLog_Operations$ReventlessCore.Make({
|
|
49
|
+
Id: {
|
|
50
|
+
schema: Id$Reventless.StringPure.schema,
|
|
51
|
+
make: prim => prim,
|
|
52
|
+
makeFromString: prim => prim,
|
|
53
|
+
toString: prim => prim,
|
|
54
|
+
cmp: Id$Reventless.StringPure.cmp
|
|
55
|
+
},
|
|
56
|
+
name: EventLogFixtures$ReventlessCore.ItemEventLogSpec.name,
|
|
57
|
+
eventSchema: EventLogFixtures$ReventlessCore.ItemEventLogSpec.eventSchema
|
|
58
|
+
})({
|
|
59
|
+
Spec: {
|
|
60
|
+
Id: {
|
|
61
|
+
schema: $$let$3.schema,
|
|
62
|
+
make: prim => prim,
|
|
63
|
+
makeFromString: prim => prim,
|
|
64
|
+
toString: prim => prim,
|
|
65
|
+
cmp: $$let$3.cmp
|
|
66
|
+
},
|
|
67
|
+
name: $$let$2.name,
|
|
68
|
+
eventSchema: $$let$2.eventSchema
|
|
69
|
+
},
|
|
70
|
+
EventTopic: TestOps_EventTopic,
|
|
71
|
+
eventTopic: EventLogFixtures$ReventlessCore.mockEventTopicOps,
|
|
72
|
+
storage: EventLogFixtures$ReventlessCore.mockStorage
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
function toBase64(s) {
|
|
76
|
+
return Nodebuffer.Buffer.from(s, "utf8").toString("base64");
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function fromBase64(s) {
|
|
80
|
+
return Nodebuffer.Buffer.from(s, "base64").toString("utf8");
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function fakeProvider(sealsOpt) {
|
|
84
|
+
let seals = sealsOpt !== undefined ? sealsOpt : true;
|
|
85
|
+
return {
|
|
86
|
+
name: "fake",
|
|
87
|
+
encrypts: true,
|
|
88
|
+
seal: async (subject, param, param$1, json) => {
|
|
89
|
+
if (seals) {
|
|
90
|
+
return {
|
|
91
|
+
TAG: "Ok",
|
|
92
|
+
_0: Pii$Reventless.wrap(`enc:v1:` + subject + `:fake:` + toBase64(JSON.stringify(json)))
|
|
93
|
+
};
|
|
94
|
+
} else {
|
|
95
|
+
return {
|
|
96
|
+
TAG: "Ok",
|
|
97
|
+
_0: json
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
prefetch: async param => ({
|
|
102
|
+
TAG: "Ok",
|
|
103
|
+
_0: undefined
|
|
104
|
+
}),
|
|
105
|
+
openNow: (field, json) => {
|
|
106
|
+
let match = Stdlib_Option.flatMap(Pii$Reventless.unwrap(json), Pii$Reventless.parseStored);
|
|
107
|
+
if (match === undefined) {
|
|
108
|
+
return {
|
|
109
|
+
TAG: "Error",
|
|
110
|
+
_0: {
|
|
111
|
+
TAG: "UnknownForm",
|
|
112
|
+
field: field
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
switch (match.TAG) {
|
|
117
|
+
case "Cipher" :
|
|
118
|
+
return {
|
|
119
|
+
TAG: "Ok",
|
|
120
|
+
_0: JSON.parse(fromBase64(match.bytes))
|
|
121
|
+
};
|
|
122
|
+
case "ErasedForm" :
|
|
123
|
+
case "ForgottenForm" :
|
|
124
|
+
return {
|
|
125
|
+
TAG: "Error",
|
|
126
|
+
_0: {
|
|
127
|
+
TAG: "UnknownForm",
|
|
128
|
+
field: field
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
release: async (param, param$1) => ({
|
|
134
|
+
TAG: "Ok",
|
|
135
|
+
_0: undefined
|
|
136
|
+
})
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
let named = {
|
|
141
|
+
TAG: "PersonNamed",
|
|
142
|
+
personId: "p-1",
|
|
143
|
+
name: {
|
|
144
|
+
TAG: "Value",
|
|
145
|
+
_0: "Ada Lovelace"
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
function storedName(id) {
|
|
150
|
+
return Stdlib_Option.flatMap(Stdlib_Option.flatMap(Stdlib_Option.flatMap(Stdlib_Option.flatMap(Stdlib_Option.flatMap(EventLogFixtures$ReventlessCore.storedEvents.contents[id], a => a[0]), Stdlib_JSON.Decode.object), d => d["data"]), Stdlib_JSON.Decode.object), d => d["name"]);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function publishedName() {
|
|
154
|
+
return Stdlib_Option.flatMap(Stdlib_Option.flatMap(Stdlib_Option.flatMap(Stdlib_Option.flatMap(EventLogFixtures$ReventlessCore.capturedPublishes.contents[0], Stdlib_JSON.Decode.object), d => d["event"]), Stdlib_JSON.Decode.object), d => d["name"]);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
globalThis.describe("EventLog with a personal-data provider", () => {
|
|
158
|
+
globalThis.beforeEach(() => EventLogFixtures$ReventlessCore.reset());
|
|
159
|
+
globalThis.afterEach(() => Pii$Reventless.Provider.install(Pii$Reventless.plain));
|
|
160
|
+
globalThis.test("stores and publishes one sealed form, and replays the value", async () => {
|
|
161
|
+
Pii$Reventless.Provider.install(fakeProvider(undefined));
|
|
162
|
+
let result = await Log.append(0, "p-1", [EventLogFixtures$ReventlessCore.makeEvent$p("p-1", named)]);
|
|
163
|
+
globalThis.expect(result).toEqual({
|
|
164
|
+
TAG: "Ok",
|
|
165
|
+
_0: undefined
|
|
166
|
+
});
|
|
167
|
+
let stored = storedName("p-1");
|
|
168
|
+
globalThis.expect(Stdlib_Option.isSome(Stdlib_Option.flatMap(stored, Pii$Reventless.unwrap))).toBe(true);
|
|
169
|
+
globalThis.expect(Stdlib_Option.getOr(Stdlib_Option.map(stored, j => JSON.stringify(j)), "").includes("Ada")).toBe(false);
|
|
170
|
+
globalThis.expect(publishedName()).toEqual(stored);
|
|
171
|
+
let replayed = await Log.replay("p-1");
|
|
172
|
+
globalThis.expect(replayed).toEqual([named]);
|
|
173
|
+
});
|
|
174
|
+
globalThis.test("refuses an append whose personal field comes out unsealed", async () => {
|
|
175
|
+
Pii$Reventless.Provider.install(fakeProvider(false));
|
|
176
|
+
let result = await Log.append(0, "p-1", [EventLogFixtures$ReventlessCore.makeEvent$p("p-1", named)]);
|
|
177
|
+
if (result.TAG === "Ok") {
|
|
178
|
+
JestGlobals.fail("expected a refusal");
|
|
179
|
+
} else {
|
|
180
|
+
let msg = result._0;
|
|
181
|
+
if (typeof msg !== "object") {
|
|
182
|
+
JestGlobals.fail("expected a refusal");
|
|
183
|
+
} else {
|
|
184
|
+
let msg$1 = msg._0;
|
|
185
|
+
globalThis.expect(msg$1.includes("name is not sealed")).toBe(true);
|
|
186
|
+
globalThis.expect(msg$1.includes("Ada")).toBe(false);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
globalThis.expect(EventLogFixtures$ReventlessCore.storedEvents.contents["p-1"]).toEqual(undefined);
|
|
190
|
+
globalThis.expect(EventLogFixtures$ReventlessCore.capturedPublishes.contents).toEqual([]);
|
|
191
|
+
});
|
|
192
|
+
globalThis.test("with the plain provider the value is stored as it is", async () => {
|
|
193
|
+
await Log.append(0, "p-1", [EventLogFixtures$ReventlessCore.makeEvent$p("p-1", named)]);
|
|
194
|
+
globalThis.expect(storedName("p-1")).toEqual("Ada Lovelace");
|
|
195
|
+
globalThis.expect(await Log.replay("p-1")).toEqual([named]);
|
|
196
|
+
});
|
|
197
|
+
globalThis.test("a value whose key is gone replays as Erased", async () => {
|
|
198
|
+
await Log.append(0, "p-1", [EventLogFixtures$ReventlessCore.makeEvent$p("p-1", named)]);
|
|
199
|
+
let item = Stdlib_Option.getOrThrow(EventLogFixtures$ReventlessCore.storedEvents.contents["p-1"], undefined)[0];
|
|
200
|
+
if (typeof item === "object" && item !== null && !Array.isArray(item)) {
|
|
201
|
+
item["data"] = Object.fromEntries([
|
|
202
|
+
[
|
|
203
|
+
"personId",
|
|
204
|
+
"p-1"
|
|
205
|
+
],
|
|
206
|
+
[
|
|
207
|
+
"name",
|
|
208
|
+
Pii$Reventless.wrap("erased:v1")
|
|
209
|
+
]
|
|
210
|
+
]);
|
|
211
|
+
}
|
|
212
|
+
globalThis.expect(await Log.replay("p-1")).toEqual([{
|
|
213
|
+
TAG: "PersonNamed",
|
|
214
|
+
personId: "p-1",
|
|
215
|
+
name: "Erased"
|
|
216
|
+
}]);
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
export {
|
|
221
|
+
TestOps,
|
|
222
|
+
Log,
|
|
223
|
+
toBase64,
|
|
224
|
+
fromBase64,
|
|
225
|
+
fakeProvider,
|
|
226
|
+
named,
|
|
227
|
+
storedName,
|
|
228
|
+
publishedName,
|
|
229
|
+
}
|
|
230
|
+
/* Log Not a pure module */
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// The decoders open sealed fields with the keys a batch prepared, and fail rather
|
|
2
|
+
// than drop an event whose key is not at hand.
|
|
3
|
+
|
|
4
|
+
open JestGlobals
|
|
5
|
+
|
|
6
|
+
module Spec = {
|
|
7
|
+
@schema
|
|
8
|
+
type event =
|
|
9
|
+
| MemberNamed({
|
|
10
|
+
memberId: string,
|
|
11
|
+
name: @s.matches(Reventless.Pii.marked(~subject="memberId", S.string))
|
|
12
|
+
Reventless.Pii.t<string>,
|
|
13
|
+
})
|
|
14
|
+
}
|
|
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
|
+
// Opens only what a prefetch made available, as a key store with a cache would.
|
|
22
|
+
let fetched: ref<array<string>> = ref([])
|
|
23
|
+
let asked: ref<array<array<string>>> = ref([])
|
|
24
|
+
let provider: Reventless.Pii.provider = {
|
|
25
|
+
name: "prefetching",
|
|
26
|
+
encrypts: true,
|
|
27
|
+
seal: async (~subject, ~field as _, ~forgettable as _, json) => Ok(
|
|
28
|
+
Reventless.Pii.wrap(`enc:v1:${subject}:fake:${json->JSON.stringify->toBase64}`),
|
|
29
|
+
),
|
|
30
|
+
prefetch: async keyIds => {
|
|
31
|
+
asked := asked.contents->Array.concat([keyIds])
|
|
32
|
+
fetched := fetched.contents->Array.concat(keyIds)
|
|
33
|
+
Ok()
|
|
34
|
+
},
|
|
35
|
+
openNow: (~field, json) =>
|
|
36
|
+
switch json->Reventless.Pii.unwrap->Option.flatMap(Reventless.Pii.parseStored) {
|
|
37
|
+
| Some(Cipher({keyId, bytes})) =>
|
|
38
|
+
fetched.contents->Array.includes(keyId)
|
|
39
|
+
? Ok(bytes->fromBase64->JSON.parseOrThrow)
|
|
40
|
+
: Error(Reventless.Pii.KeyUnavailable({subject: keyId}))
|
|
41
|
+
| _ => Error(Reventless.Pii.UnknownForm({field: field}))
|
|
42
|
+
},
|
|
43
|
+
release: async (~subject as _, ~ref as _) => Ok(),
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let sealedData = () =>
|
|
47
|
+
Dict.fromArray([
|
|
48
|
+
("memberId", JSON.Encode.string("m-1")),
|
|
49
|
+
("name", Reventless.Pii.wrap(`enc:v1:m-1:fake:${"\"Ada\""->toBase64}`)),
|
|
50
|
+
])
|
|
51
|
+
|
|
52
|
+
let envelope = () =>
|
|
53
|
+
Dict.fromArray([
|
|
54
|
+
("id", JSON.Encode.string("m-1")),
|
|
55
|
+
(
|
|
56
|
+
"meta",
|
|
57
|
+
Message.generateMeta(~service="Members")->Reventless.Util_Sury.toJson(Message.metaSchema),
|
|
58
|
+
),
|
|
59
|
+
(
|
|
60
|
+
"event",
|
|
61
|
+
{
|
|
62
|
+
let d = sealedData()
|
|
63
|
+
d->Dict.set("TAG", JSON.Encode.string("MemberNamed"))
|
|
64
|
+
JSON.Object(d)
|
|
65
|
+
},
|
|
66
|
+
),
|
|
67
|
+
])->JSON.Encode.object
|
|
68
|
+
|
|
69
|
+
let threw = f =>
|
|
70
|
+
switch f() {
|
|
71
|
+
| _ => false
|
|
72
|
+
| exception _ => true
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
describe("decoding sealed fields with prepared keys", () => {
|
|
76
|
+
beforeEach(() => {
|
|
77
|
+
fetched := []
|
|
78
|
+
asked := []
|
|
79
|
+
Reventless.Pii.Provider.install(provider)
|
|
80
|
+
})
|
|
81
|
+
afterEach(() => Reventless.Pii.Provider.install(Reventless.Pii.plain))
|
|
82
|
+
|
|
83
|
+
let decoder = Reventless.DcbDecode.makeDecoder(Spec.eventSchema)
|
|
84
|
+
|
|
85
|
+
testSync("an unprepared key fails the read, it does not drop the event", () =>
|
|
86
|
+
expect(threw(() => decoder.decode(~eventType="MemberNamed", ~data=sealedData())))->toBe(true)
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
testPromise("after prepare the DCB decoder reads the value", async () => {
|
|
90
|
+
await Reventless.Pii.prepareOrThrow([JSON.Object(sealedData())])
|
|
91
|
+
expect(decoder.decode(~eventType="MemberNamed", ~data=sealedData()))->toEqual(
|
|
92
|
+
Some(Spec.MemberNamed({memberId: "m-1", name: Value("Ada")})),
|
|
93
|
+
)
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
testPromise("after prepare an envelope decodes through decodeEvent'", async () => {
|
|
97
|
+
expect(
|
|
98
|
+
threw(
|
|
99
|
+
() => envelope()->Message.decodeEvent'(Reventless.Id.StringPure.schema, Spec.eventSchema),
|
|
100
|
+
),
|
|
101
|
+
)->toBe(true)
|
|
102
|
+
await Reventless.Pii.prepareOrThrow([envelope()])
|
|
103
|
+
let decoded =
|
|
104
|
+
envelope()->Message.decodeEvent'(Reventless.Id.StringPure.schema, Spec.eventSchema)
|
|
105
|
+
expect(decoded.event)->toEqual(Spec.MemberNamed({memberId: "m-1", name: Value("Ada")}))
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
testPromise(
|
|
109
|
+
"prepare asks for the keys a batch names, once each, and nothing for none",
|
|
110
|
+
async () => {
|
|
111
|
+
await Reventless.Pii.prepareOrThrow([envelope(), envelope()])
|
|
112
|
+
await Reventless.Pii.prepareOrThrow([JSON.Encode.string("nothing sealed")])
|
|
113
|
+
expect(asked.contents)->toEqual([["m-1"]])
|
|
114
|
+
},
|
|
115
|
+
)
|
|
116
|
+
})
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as Sury from "sury";
|
|
4
|
+
import * as Nodebuffer from "node:buffer";
|
|
5
|
+
import * as Id$Reventless from "@reventlessdev/reventless-spec/src/types/Id.res.mjs";
|
|
6
|
+
import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
|
|
7
|
+
import * as Pii$Reventless from "@reventlessdev/reventless-spec/src/components/Pii.res.mjs";
|
|
8
|
+
import * as DcbDecode$Reventless from "@reventlessdev/reventless-spec/src/components/DcbDecode.res.mjs";
|
|
9
|
+
import * as Util_Sury$Reventless from "@reventlessdev/reventless-spec/src/util/Util_Sury.res.mjs";
|
|
10
|
+
import * as Message$ReventlessCore from "../../src/Message.res.mjs";
|
|
11
|
+
|
|
12
|
+
let eventSchema = Sury.$schema(s => ({
|
|
13
|
+
TAG: "MemberNamed",
|
|
14
|
+
memberId: s.m(Sury.string),
|
|
15
|
+
name: s.m(Pii$Reventless.marked("memberId", undefined, Sury.string))
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
let Spec = {
|
|
19
|
+
eventSchema: eventSchema
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function toBase64(s) {
|
|
23
|
+
return Nodebuffer.Buffer.from(s, "utf8").toString("base64");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function fromBase64(s) {
|
|
27
|
+
return Nodebuffer.Buffer.from(s, "base64").toString("utf8");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
let fetched = {
|
|
31
|
+
contents: []
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
let asked = {
|
|
35
|
+
contents: []
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
async function provider_seal(subject, param, param$1, json) {
|
|
39
|
+
return {
|
|
40
|
+
TAG: "Ok",
|
|
41
|
+
_0: Pii$Reventless.wrap(`enc:v1:` + subject + `:fake:` + toBase64(JSON.stringify(json)))
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async function provider_prefetch(keyIds) {
|
|
46
|
+
asked.contents = asked.contents.concat([keyIds]);
|
|
47
|
+
fetched.contents = fetched.contents.concat(keyIds);
|
|
48
|
+
return {
|
|
49
|
+
TAG: "Ok",
|
|
50
|
+
_0: undefined
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function provider_openNow(field, json) {
|
|
55
|
+
let match = Stdlib_Option.flatMap(Pii$Reventless.unwrap(json), Pii$Reventless.parseStored);
|
|
56
|
+
if (match === undefined) {
|
|
57
|
+
return {
|
|
58
|
+
TAG: "Error",
|
|
59
|
+
_0: {
|
|
60
|
+
TAG: "UnknownForm",
|
|
61
|
+
field: field
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
switch (match.TAG) {
|
|
66
|
+
case "Cipher" :
|
|
67
|
+
let keyId = match.keyId;
|
|
68
|
+
if (fetched.contents.includes(keyId)) {
|
|
69
|
+
return {
|
|
70
|
+
TAG: "Ok",
|
|
71
|
+
_0: JSON.parse(fromBase64(match.bytes))
|
|
72
|
+
};
|
|
73
|
+
} else {
|
|
74
|
+
return {
|
|
75
|
+
TAG: "Error",
|
|
76
|
+
_0: {
|
|
77
|
+
TAG: "KeyUnavailable",
|
|
78
|
+
subject: keyId
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
case "ErasedForm" :
|
|
83
|
+
case "ForgottenForm" :
|
|
84
|
+
return {
|
|
85
|
+
TAG: "Error",
|
|
86
|
+
_0: {
|
|
87
|
+
TAG: "UnknownForm",
|
|
88
|
+
field: field
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function provider_release(param, param$1) {
|
|
95
|
+
return {
|
|
96
|
+
TAG: "Ok",
|
|
97
|
+
_0: undefined
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
let provider = {
|
|
102
|
+
name: "prefetching",
|
|
103
|
+
encrypts: true,
|
|
104
|
+
seal: provider_seal,
|
|
105
|
+
prefetch: provider_prefetch,
|
|
106
|
+
openNow: provider_openNow,
|
|
107
|
+
release: provider_release
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
function sealedData() {
|
|
111
|
+
return Object.fromEntries([
|
|
112
|
+
[
|
|
113
|
+
"memberId",
|
|
114
|
+
"m-1"
|
|
115
|
+
],
|
|
116
|
+
[
|
|
117
|
+
"name",
|
|
118
|
+
Pii$Reventless.wrap(`enc:v1:m-1:fake:` + toBase64("\"Ada\""))
|
|
119
|
+
]
|
|
120
|
+
]);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function envelope() {
|
|
124
|
+
let d = sealedData();
|
|
125
|
+
return Object.fromEntries([
|
|
126
|
+
[
|
|
127
|
+
"id",
|
|
128
|
+
"m-1"
|
|
129
|
+
],
|
|
130
|
+
[
|
|
131
|
+
"meta",
|
|
132
|
+
Util_Sury$Reventless.toJson(Message$ReventlessCore.generateMeta("Members", undefined, undefined, undefined, undefined, undefined, undefined), Message$ReventlessCore.metaSchema)
|
|
133
|
+
],
|
|
134
|
+
[
|
|
135
|
+
"event",
|
|
136
|
+
(d["TAG"] = "MemberNamed", d)
|
|
137
|
+
]
|
|
138
|
+
]);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function threw(f) {
|
|
142
|
+
try {
|
|
143
|
+
f();
|
|
144
|
+
return false;
|
|
145
|
+
} catch (exn) {
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
globalThis.describe("decoding sealed fields with prepared keys", () => {
|
|
151
|
+
globalThis.beforeEach(() => {
|
|
152
|
+
fetched.contents = [];
|
|
153
|
+
asked.contents = [];
|
|
154
|
+
Pii$Reventless.Provider.install(provider);
|
|
155
|
+
});
|
|
156
|
+
globalThis.afterEach(() => Pii$Reventless.Provider.install(Pii$Reventless.plain));
|
|
157
|
+
let decoder = DcbDecode$Reventless.makeDecoder(eventSchema);
|
|
158
|
+
globalThis.test("an unprepared key fails the read, it does not drop the event", () => {
|
|
159
|
+
globalThis.expect(threw(() => decoder.decode("MemberNamed", sealedData()))).toBe(true);
|
|
160
|
+
});
|
|
161
|
+
globalThis.test("after prepare the DCB decoder reads the value", async () => {
|
|
162
|
+
await Pii$Reventless.prepareOrThrow([sealedData()]);
|
|
163
|
+
globalThis.expect(decoder.decode("MemberNamed", sealedData())).toEqual({
|
|
164
|
+
TAG: "MemberNamed",
|
|
165
|
+
memberId: "m-1",
|
|
166
|
+
name: {
|
|
167
|
+
TAG: "Value",
|
|
168
|
+
_0: "Ada"
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
globalThis.test("after prepare an envelope decodes through decodeEvent'", async () => {
|
|
173
|
+
globalThis.expect(threw(() => Message$ReventlessCore.decodeEvent$p(envelope(), Id$Reventless.StringPure.schema, eventSchema))).toBe(true);
|
|
174
|
+
await Pii$Reventless.prepareOrThrow([envelope()]);
|
|
175
|
+
let decoded = Message$ReventlessCore.decodeEvent$p(envelope(), Id$Reventless.StringPure.schema, eventSchema);
|
|
176
|
+
globalThis.expect(decoded.event).toEqual({
|
|
177
|
+
TAG: "MemberNamed",
|
|
178
|
+
memberId: "m-1",
|
|
179
|
+
name: {
|
|
180
|
+
TAG: "Value",
|
|
181
|
+
_0: "Ada"
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
globalThis.test("prepare asks for the keys a batch names, once each, and nothing for none", async () => {
|
|
186
|
+
await Pii$Reventless.prepareOrThrow([
|
|
187
|
+
envelope(),
|
|
188
|
+
envelope()
|
|
189
|
+
]);
|
|
190
|
+
await Pii$Reventless.prepareOrThrow(["nothing sealed"]);
|
|
191
|
+
globalThis.expect(asked.contents).toEqual([["m-1"]]);
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
export {
|
|
196
|
+
Spec,
|
|
197
|
+
toBase64,
|
|
198
|
+
fromBase64,
|
|
199
|
+
fetched,
|
|
200
|
+
asked,
|
|
201
|
+
provider,
|
|
202
|
+
sealedData,
|
|
203
|
+
envelope,
|
|
204
|
+
threw,
|
|
205
|
+
}
|
|
206
|
+
/* eventSchema Not a pure module */
|
package/tests/util/LruTest.res
CHANGED
|
@@ -99,3 +99,38 @@ describe("Lru:", () => {
|
|
|
99
99
|
)
|
|
100
100
|
})
|
|
101
101
|
})
|
|
102
|
+
|
|
103
|
+
describe("Lru time limit", () => {
|
|
104
|
+
testSync("an entry is served until its time limit and missed after, however hot", () => {
|
|
105
|
+
let clock = ref(0.)
|
|
106
|
+
let cache: Lru.t<string, int> = Lru.make(~capacity=10, ~ttlMs=1000., ~now=() => clock.contents)
|
|
107
|
+
cache->Lru.put("a", 1)
|
|
108
|
+
clock := 999.
|
|
109
|
+
expect(cache->Lru.get("a"))->toEqual(Some(1))
|
|
110
|
+
clock := 1000.
|
|
111
|
+
expect(cache->Lru.get("a"))->toEqual(None)
|
|
112
|
+
expect(cache->Lru.size)->toBe(0)
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
testSync("a put starts the limit again", () => {
|
|
116
|
+
let clock = ref(0.)
|
|
117
|
+
let cache: Lru.t<string, int> = Lru.make(~capacity=10, ~ttlMs=1000., ~now=() => clock.contents)
|
|
118
|
+
cache->Lru.put("a", 1)
|
|
119
|
+
clock := 900.
|
|
120
|
+
cache->Lru.put("a", 2)
|
|
121
|
+
clock := 1500.
|
|
122
|
+
expect(cache->Lru.get("a"))->toEqual(Some(2))
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
testSync("without a limit an entry stays until evicted", () => {
|
|
126
|
+
let clock = ref(0.)
|
|
127
|
+
let cache: Lru.t<string, int> = Lru.make(~capacity=10, ~now=() => clock.contents)
|
|
128
|
+
cache->Lru.put("a", 1)
|
|
129
|
+
clock := 1e12
|
|
130
|
+
expect(cache->Lru.get("a"))->toEqual(Some(1))
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
testSync("decoded state is held for minutes by default", () =>
|
|
134
|
+
expect(Lru.decodedStateTtlMs() <= 600_000.)->toBe(true)
|
|
135
|
+
)
|
|
136
|
+
})
|