@evolu/common 1.0.9 → 1.0.10

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/src/Crdt.ts ADDED
@@ -0,0 +1,376 @@
1
+ import * as Schema from "@effect/schema/Schema";
2
+ import {
3
+ Brand,
4
+ Context,
5
+ Effect,
6
+ Either,
7
+ Layer,
8
+ Number,
9
+ Option,
10
+ ReadonlyArray,
11
+ String,
12
+ pipe,
13
+ } from "effect";
14
+ import { Config } from "./Config.js";
15
+ import { NanoId, NodeId } from "./Crypto.js";
16
+ import { murmurhash } from "./Murmurhash.js";
17
+
18
+ // https://muratbuffalo.blogspot.com/2014/07/hybrid-logical-clocks.html
19
+ // https://jaredforsyth.com/posts/hybrid-logical-clocks/
20
+ // https://github.com/clintharris/crdt-example-app_annotated/blob/master/shared/timestamp.js
21
+ // https://github.com/actualbudget/actual/tree/master/packages/crdt
22
+
23
+ export interface Timestamp {
24
+ readonly node: NodeId;
25
+ readonly millis: Millis;
26
+ readonly counter: Counter;
27
+ }
28
+
29
+ export const AllowedTimeRange = {
30
+ greaterThan: 860934419999,
31
+ lessThan: 2582803260000,
32
+ };
33
+
34
+ /**
35
+ * Millis represents a time that is valid for usage with the Merkle tree.
36
+ * It must be between Apr 13, 1997, and Nov 05, 2051, to ensure MinutesBase3
37
+ * length equals 16. We can find diff for two Merkle trees only within this range.
38
+ * If the device clock is out of range, Evolu will not store data until it's fixed.
39
+ */
40
+ export const Millis = Schema.number.pipe(
41
+ Schema.greaterThan(AllowedTimeRange.greaterThan),
42
+ Schema.lessThan(AllowedTimeRange.lessThan),
43
+ Schema.brand("Millis"),
44
+ );
45
+
46
+ export type Millis = Schema.Schema.To<typeof Millis>;
47
+
48
+ export const initialMillis = Schema.parseSync(Millis)(
49
+ AllowedTimeRange.greaterThan + 1,
50
+ );
51
+
52
+ export const Counter = Schema.number.pipe(
53
+ Schema.between(0, 65535),
54
+ Schema.brand("Counter"),
55
+ );
56
+ export type Counter = Schema.Schema.To<typeof Counter>;
57
+
58
+ const initialCounter = Schema.parseSync(Counter)(0);
59
+
60
+ export type TimestampHash = number & Brand.Brand<"TimestampHash">;
61
+
62
+ export type TimestampString = string & Brand.Brand<"TimestampString">;
63
+
64
+ export const timestampToString = (t: Timestamp): TimestampString =>
65
+ [
66
+ new Date(t.millis).toISOString(),
67
+ t.counter.toString(16).toUpperCase().padStart(4, "0"),
68
+ t.node,
69
+ ].join("-") as TimestampString;
70
+
71
+ // TODO: Replace with safe.
72
+ export const unsafeTimestampFromString = (s: TimestampString): Timestamp => {
73
+ const a = s.split("-");
74
+ return {
75
+ millis: Date.parse(a.slice(0, 3).join("-")).valueOf() as Millis,
76
+ counter: parseInt(a[3], 16) as Counter,
77
+ node: a[4] as NodeId,
78
+ };
79
+ };
80
+
81
+ export const timestampToHash = (t: Timestamp): TimestampHash =>
82
+ murmurhash(timestampToString(t)) as TimestampHash;
83
+
84
+ const syncNodeId = Schema.parseSync(NodeId)("0000000000000000");
85
+
86
+ export const makeSyncTimestamp = (
87
+ millis: Millis = initialMillis,
88
+ ): Timestamp => ({
89
+ millis,
90
+ counter: initialCounter,
91
+ node: syncNodeId,
92
+ });
93
+
94
+ export const makeInitialTimestamp = NanoId.pipe(
95
+ Effect.flatMap(({ nanoidAsNodeId }) => nanoidAsNodeId),
96
+ Effect.map(
97
+ (node): Timestamp => ({
98
+ millis: initialMillis,
99
+ counter: initialCounter,
100
+ node,
101
+ }),
102
+ ),
103
+ );
104
+
105
+ export interface Time {
106
+ readonly now: Effect.Effect<never, TimestampTimeOutOfRangeError, Millis>;
107
+ }
108
+
109
+ export const Time = Context.Tag<Time>("evolu/Time");
110
+
111
+ export const TimeLive = Layer.succeed(
112
+ Time,
113
+ Time.of({
114
+ now: Effect.suspend(() => Schema.parse(Millis)(Date.now())).pipe(
115
+ Effect.catchTag("ParseError", () =>
116
+ Effect.fail<TimestampTimeOutOfRangeError>({
117
+ _tag: "TimestampTimeOutOfRangeError",
118
+ }),
119
+ ),
120
+ ),
121
+ }),
122
+ );
123
+
124
+ export type TimestampError =
125
+ | TimestampDriftError
126
+ | TimestampCounterOverflowError
127
+ | TimestampDuplicateNodeError
128
+ | TimestampTimeOutOfRangeError;
129
+
130
+ export interface TimestampDriftError {
131
+ readonly _tag: "TimestampDriftError";
132
+ readonly next: Millis;
133
+ readonly now: Millis;
134
+ }
135
+
136
+ export interface TimestampCounterOverflowError {
137
+ readonly _tag: "TimestampCounterOverflowError";
138
+ }
139
+
140
+ export interface TimestampDuplicateNodeError {
141
+ readonly _tag: "TimestampDuplicateNodeError";
142
+ readonly node: NodeId;
143
+ }
144
+
145
+ export interface TimestampTimeOutOfRangeError {
146
+ readonly _tag: "TimestampTimeOutOfRangeError";
147
+ }
148
+
149
+ const getNextMillis = (
150
+ millis: ReadonlyArray<Millis>,
151
+ ): Effect.Effect<
152
+ Time | Config,
153
+ TimestampDriftError | TimestampTimeOutOfRangeError,
154
+ Millis
155
+ > =>
156
+ Effect.gen(function* (_) {
157
+ const time = yield* _(Time);
158
+ const config = yield* _(Config);
159
+
160
+ const now = yield* _(time.now);
161
+ const next = Math.max(now, ...millis) as Millis;
162
+
163
+ if (next - now > config.maxDrift)
164
+ yield* _(
165
+ Effect.fail<TimestampDriftError>({
166
+ _tag: "TimestampDriftError",
167
+ now,
168
+ next,
169
+ }),
170
+ );
171
+
172
+ return next;
173
+ });
174
+
175
+ const incrementCounter = (
176
+ counter: Counter,
177
+ ): Either.Either<TimestampCounterOverflowError, Counter> =>
178
+ pipe(
179
+ Number.increment(counter),
180
+ Schema.parseEither(Counter),
181
+ Either.mapLeft(() => ({ _tag: "TimestampCounterOverflowError" })),
182
+ );
183
+
184
+ const counterMin = Schema.parseSync(Counter)(0);
185
+
186
+ export const sendTimestamp = (
187
+ timestamp: Timestamp,
188
+ ): Effect.Effect<
189
+ Time | Config,
190
+ | TimestampDriftError
191
+ | TimestampCounterOverflowError
192
+ | TimestampTimeOutOfRangeError,
193
+ Timestamp
194
+ > =>
195
+ Effect.gen(function* (_) {
196
+ const millis = yield* _(getNextMillis([timestamp.millis]));
197
+ const counter =
198
+ millis === timestamp.millis
199
+ ? yield* _(incrementCounter(timestamp.counter))
200
+ : counterMin;
201
+ return { ...timestamp, millis, counter };
202
+ });
203
+
204
+ export const receiveTimestamp = ({
205
+ local,
206
+ remote,
207
+ }: {
208
+ readonly local: Timestamp;
209
+ readonly remote: Timestamp;
210
+ }): Effect.Effect<
211
+ Time | Config,
212
+ | TimestampDriftError
213
+ | TimestampCounterOverflowError
214
+ | TimestampDuplicateNodeError
215
+ | TimestampTimeOutOfRangeError,
216
+ Timestamp
217
+ > =>
218
+ Effect.gen(function* (_) {
219
+ if (local.node === remote.node)
220
+ yield* _(
221
+ Effect.fail<TimestampDuplicateNodeError>({
222
+ _tag: "TimestampDuplicateNodeError",
223
+ node: local.node,
224
+ }),
225
+ );
226
+
227
+ const millis = yield* _(getNextMillis([local.millis, remote.millis]));
228
+ const counter = yield* _(
229
+ millis === local.millis && millis === remote.millis
230
+ ? incrementCounter(Math.max(local.counter, remote.counter) as Counter)
231
+ : millis === local.millis
232
+ ? incrementCounter(local.counter)
233
+ : millis === remote.millis
234
+ ? incrementCounter(remote.counter)
235
+ : Either.right(counterMin),
236
+ );
237
+
238
+ return { ...local, millis, counter };
239
+ });
240
+
241
+ /**
242
+ * It's actually not Merkle Tree but a Merkleized prefix tree, aka Merkle Trie.
243
+ * https://decomposition.al/blog/2019/05/31/how-i-learned-about-merklix-trees-without-having-to-become-a-cryptocurrency-enthusiast
244
+ */
245
+ export interface MerkleTree {
246
+ readonly hash?: TimestampHash;
247
+ readonly "0"?: MerkleTree;
248
+ readonly "1"?: MerkleTree;
249
+ readonly "2"?: MerkleTree;
250
+ }
251
+
252
+ export type MerkleTreeString = string & Brand.Brand<"MerkleTreeString">;
253
+
254
+ export const initialMerkleTree = Object.create(null) as MerkleTree;
255
+
256
+ type MerkleTreeKey = keyof Omit<MerkleTree, "hash">;
257
+
258
+ type MerkleTreePath = ReadonlyArray<MerkleTreeKey>;
259
+
260
+ export const millisToMerkleTreePath = (millis: Millis): MerkleTreePath =>
261
+ Math.floor(millis / 1000 / 60)
262
+ .toString(3)
263
+ .split("") as MerkleTreePath;
264
+
265
+ const merkleTreePathToMillis = (path: MerkleTreePath): Millis =>
266
+ path.length === 0
267
+ ? initialMillis
268
+ : // 16 is the length of the base 3 value of the current time in minutes.
269
+ // Ensure it's padded to create the full value.
270
+ ((parseInt(path.join("").padEnd(16, "0"), 3) * 1000 * 60) as Millis);
271
+
272
+ const xorTimestampHashes = (
273
+ a: TimestampHash | undefined,
274
+ b: TimestampHash,
275
+ ): TimestampHash => ((a || 0) ^ b) as TimestampHash;
276
+
277
+ const insertKey = (
278
+ tree: MerkleTree,
279
+ path: MerkleTreePath,
280
+ hash: TimestampHash,
281
+ ): MerkleTree => {
282
+ if (path.length === 0) return tree;
283
+ const key = path[0];
284
+ const child = tree[key] || {};
285
+ return {
286
+ ...tree,
287
+ [key]: {
288
+ ...child,
289
+ ...insertKey(child, path.slice(1), hash),
290
+ hash: xorTimestampHashes(child.hash, hash),
291
+ },
292
+ };
293
+ };
294
+
295
+ export const insertIntoMerkleTree =
296
+ (timestamp: Timestamp) =>
297
+ (tree: MerkleTree): MerkleTree => {
298
+ const path = millisToMerkleTreePath(timestamp.millis);
299
+ const hash = timestampToHash(timestamp);
300
+ return insertKey(
301
+ { ...tree, hash: xorTimestampHashes(tree.hash, hash) },
302
+ path,
303
+ hash,
304
+ );
305
+ };
306
+
307
+ const sortedMerkleTreeKeys: ReadonlyArray<MerkleTreeKey> = ["0", "1", "2"];
308
+
309
+ const getSortedMerkleTreeKeys = (
310
+ tree: MerkleTree,
311
+ ): ReadonlyArray<MerkleTreeKey> =>
312
+ sortedMerkleTreeKeys.filter((key) => key in tree);
313
+
314
+ export const diffMerkleTrees = (
315
+ tree1: MerkleTree,
316
+ tree2: MerkleTree,
317
+ ): Option.Option<Millis> => {
318
+ if (tree1.hash === tree2.hash) return Option.none();
319
+ let node1 = tree1;
320
+ let node2 = tree2;
321
+ let diffPath: MerkleTreePath = [];
322
+
323
+ // This loop will eventually stop when it traverses down to find
324
+ // where the hashes differ, or otherwise when there are no leaves
325
+ // left (this shouldn't happen, if that's the case the hash check at
326
+ // the top of this function should pass)
327
+ // eslint-disable-next-line no-constant-condition
328
+ while (1) {
329
+ const keys = ReadonlyArray.dedupeWith(
330
+ getSortedMerkleTreeKeys(node1).concat(getSortedMerkleTreeKeys(node2)),
331
+ String.Equivalence,
332
+ );
333
+ let diffKey: MerkleTreeKey | null = null;
334
+
335
+ // Traverse down the trie through keys that are different. We
336
+ // traverse down the keys in order. Stop in two cases: either one
337
+ // of the nodes doesn't have the key or a different key isn't
338
+ // found. For the former case, we have to do that because pruning is
339
+ // lossy. We don't know if we've pruned off a changed key, so we
340
+ // can't traverse down anymore. For the latter case, it means two
341
+ // things: either we've hit the bottom of the tree, or the changed
342
+ // key has been pruned off. In the latter case, we have a "partial"
343
+ // key and will fill the rest with 0s. If multiple older
344
+ // messages were added into one trie, we might likely
345
+ // generate a time that only encompasses *some* of those
346
+ // messages. Pruning is lossy, and we traverse down the left-most
347
+ // changed time that we know of, because of pruning, it might take
348
+ // multiple passes to sync up a trie.
349
+ for (let i = 0; i < keys.length; i++) {
350
+ const key = keys[i];
351
+ const next1 = node1[key];
352
+ const next2 = node2[key];
353
+ if (!next1 || !next2) break;
354
+ if (next1.hash !== next2.hash) {
355
+ diffKey = key;
356
+ break;
357
+ }
358
+ }
359
+
360
+ if (!diffKey) {
361
+ return Option.some(merkleTreePathToMillis(diffPath));
362
+ }
363
+
364
+ diffPath = [...diffPath, diffKey];
365
+ node1 = node1[diffKey] || initialMerkleTree;
366
+ node2 = node2[diffKey] || initialMerkleTree;
367
+ }
368
+
369
+ return Option.none();
370
+ };
371
+
372
+ export const merkleTreeToString = (m: MerkleTree): MerkleTreeString =>
373
+ JSON.stringify(m) as MerkleTreeString;
374
+
375
+ export const unsafeMerkleTreeFromString = (m: MerkleTreeString): MerkleTree =>
376
+ JSON.parse(m) as MerkleTree;
package/src/Crypto.ts CHANGED
@@ -63,7 +63,7 @@ export const NanoIdLive = Layer.succeed(
63
63
  */
64
64
  export const slip21Derive = (
65
65
  seed: Uint8Array,
66
- path: string[],
66
+ path: ReadonlyArray<string>,
67
67
  ): Effect.Effect<never, never, Uint8Array> =>
68
68
  Effect.sync(() => {
69
69
  let m = hmac(sha512, "Symmetric key seed", seed);
package/src/Db.ts CHANGED
@@ -16,8 +16,13 @@ import {
16
16
  } from "effect";
17
17
  import * as Kysely from "kysely";
18
18
  import { urlAlphabet } from "nanoid";
19
+ import {
20
+ initialMerkleTree,
21
+ makeInitialTimestamp,
22
+ merkleTreeToString,
23
+ timestampToString,
24
+ } from "./Crdt.js";
19
25
  import { Bip39, Mnemonic, NanoId, slip21Derive } from "./Crypto.js";
20
- import { initialMerkleTree, merkleTreeToString } from "./MerkleTree.js";
21
26
  import { Id, SqliteBoolean, SqliteDate } from "./Model.js";
22
27
  import {
23
28
  createMessageTable,
@@ -33,7 +38,6 @@ import {
33
38
  Value,
34
39
  queryObjectToQuery,
35
40
  } from "./Sqlite.js";
36
- import { makeInitialTimestamp, timestampToString } from "./Timestamp.js";
37
41
 
38
42
  export type TableSchema = ReadonlyRecord.ReadonlyRecord<Value> & {
39
43
  readonly id: Id;
package/src/DbWorker.ts CHANGED
@@ -12,6 +12,25 @@ import {
12
12
  pipe,
13
13
  } from "effect";
14
14
  import { Config, ConfigLive } from "./Config.js";
15
+ import {
16
+ MerkleTree,
17
+ Time,
18
+ TimeLive,
19
+ Timestamp,
20
+ TimestampCounterOverflowError,
21
+ TimestampDriftError,
22
+ TimestampError,
23
+ TimestampString,
24
+ TimestampTimeOutOfRangeError,
25
+ diffMerkleTrees,
26
+ insertIntoMerkleTree,
27
+ makeSyncTimestamp,
28
+ merkleTreeToString,
29
+ receiveTimestamp,
30
+ sendTimestamp,
31
+ timestampToString,
32
+ unsafeTimestampFromString,
33
+ } from "./Crdt.js";
15
34
  import { Bip39, Mnemonic, NanoId } from "./Crypto.js";
16
35
  import {
17
36
  Owner,
@@ -25,12 +44,6 @@ import {
25
44
  } from "./Db.js";
26
45
  import { QueryPatches, makePatches } from "./Diff.js";
27
46
  import { EvoluError, UnexpectedError, makeUnexpectedError } from "./Errors.js";
28
- import {
29
- MerkleTree,
30
- diffMerkleTrees,
31
- insertIntoMerkleTree,
32
- merkleTreeToString,
33
- } from "./MerkleTree.js";
34
47
  import { Id, SqliteDate, cast } from "./Model.js";
35
48
  import {
36
49
  insertIntoMessagesIfNew,
@@ -50,20 +63,6 @@ import {
50
63
  SyncWorkerOutputSyncResponse,
51
64
  SyncWorkerPostMessage,
52
65
  } from "./SyncWorker.js";
53
- import {
54
- Time,
55
- TimeLive,
56
- Timestamp,
57
- TimestampCounterOverflowError,
58
- TimestampDriftError,
59
- TimestampError,
60
- TimestampString,
61
- makeSyncTimestamp,
62
- receiveTimestamp,
63
- sendTimestamp,
64
- timestampToString,
65
- unsafeTimestampFromString,
66
- } from "./Timestamp.js";
67
66
 
68
67
  // TODO: Refactor to Effect.
69
68
  export interface DbWorker {
@@ -395,7 +394,9 @@ const mutate = ({
395
394
  | RowsCacheRef
396
395
  | DbWorkerOnMessage
397
396
  | SyncWorkerPostMessage,
398
- TimestampDriftError | TimestampCounterOverflowError,
397
+ | TimestampDriftError
398
+ | TimestampCounterOverflowError
399
+ | TimestampTimeOutOfRangeError,
399
400
  void
400
401
  > =>
401
402
  Effect.gen(function* (_) {
package/src/Errors.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Effect } from "effect";
2
- import { TimestampError } from "./Timestamp.js";
2
+ import { TimestampError } from "./Crdt.js";
3
3
 
4
4
  export type EvoluError = UnexpectedError | TimestampError;
5
5
 
package/src/Evolu.ts CHANGED
@@ -12,6 +12,7 @@ import {
12
12
  } from "effect";
13
13
  import { Simplify } from "kysely";
14
14
  import { Config, ConfigLive } from "./Config.js";
15
+ import { Time, TimeLive } from "./Crdt.js";
15
16
  import { Bip39, NanoId } from "./Crypto.js";
16
17
  import {
17
18
  CommonColumns,
@@ -36,7 +37,6 @@ import { AppState, FlushSync } from "./Platform.js";
36
37
  import { Query, Row } from "./Sqlite.js";
37
38
  import { Store, StoreListener, StoreUnsubscribe, makeStore } from "./Store.js";
38
39
  import { SyncState } from "./SyncWorker.js";
39
- import { Time, TimeLive } from "./Timestamp.js";
40
40
 
41
41
  export interface Evolu<S extends Schema> {
42
42
  readonly subscribeError: ErrorStore["subscribe"];
package/src/Protobuf.ts CHANGED
@@ -3,10 +3,9 @@
3
3
  // @generated from protobuf file "Protobuf.proto" (syntax proto3)
4
4
  // tslint:disable
5
5
  import { MessageType } from "@protobuf-ts/runtime";
6
- import { MerkleTreeString } from "./MerkleTree.js";
7
- import { OwnerId } from "./Db.js";
6
+ import { MerkleTreeString, TimestampString } from "./Crdt.js";
8
7
  import { NodeId } from "./Crypto.js";
9
- import { TimestampString } from "./Timestamp.js";
8
+ import { OwnerId } from "./Db.js";
10
9
  import { Id } from "./Model.js";
11
10
  /**
12
11
  * @generated from protobuf message SyncRequest
package/src/SyncWorker.ts CHANGED
@@ -11,14 +11,17 @@ import {
11
11
  absurd,
12
12
  identity,
13
13
  } from "effect";
14
- import { SecretBox } from "./Crypto.js";
15
- import { Owner } from "./Db.js";
16
- import { UnexpectedError, makeUnexpectedError } from "./Errors.js";
17
14
  import {
18
15
  MerkleTree,
16
+ Millis,
17
+ Timestamp,
18
+ TimestampString,
19
19
  merkleTreeToString,
20
20
  unsafeMerkleTreeFromString,
21
- } from "./MerkleTree.js";
21
+ } from "./Crdt.js";
22
+ import { SecretBox } from "./Crypto.js";
23
+ import { Owner } from "./Db.js";
24
+ import { UnexpectedError, makeUnexpectedError } from "./Errors.js";
22
25
  import { Id } from "./Model.js";
23
26
  import { Fetch, SyncLock } from "./Platform.js";
24
27
  import {
@@ -28,7 +31,6 @@ import {
28
31
  SyncResponse,
29
32
  } from "./Protobuf.js";
30
33
  import { JsonObjectOrArray, Value } from "./Sqlite.js";
31
- import { Millis, Timestamp, TimestampString } from "./Timestamp.js";
32
34
 
33
35
  export interface SyncWorker {
34
36
  readonly postMessage: (input: SyncWorkerInput) => void;
package/src/index.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  export { jsonArrayFrom, jsonObjectFrom } from "kysely/helpers/sqlite";
2
2
  export * from "./Config.js";
3
+ export * from "./Crdt.js";
3
4
  export * from "./Crypto.js";
4
5
  export * from "./Db.js";
5
6
  export * from "./DbWorker.js";
6
7
  export * from "./Diff.js";
7
8
  export * from "./Errors.js";
8
9
  export * from "./Evolu.js";
9
- export * from "./MerkleTree.js";
10
10
  export * from "./Model.js";
11
11
  export * from "./Murmurhash.js";
12
12
  export * from "./Platform.js";
@@ -15,4 +15,3 @@ export * from "./Sql.js";
15
15
  export * from "./Sqlite.js";
16
16
  export * from "./Store.js";
17
17
  export * from "./SyncWorker.js";
18
- export * from "./Timestamp.js";
@@ -1,15 +0,0 @@
1
- import { Brand, Option } from "effect";
2
- import { Millis, Timestamp, TimestampHash } from "./Timestamp.js";
3
- export interface MerkleTree {
4
- readonly hash?: TimestampHash;
5
- readonly "0"?: MerkleTree;
6
- readonly "1"?: MerkleTree;
7
- readonly "2"?: MerkleTree;
8
- }
9
- export type MerkleTreeString = string & Brand.Brand<"MerkleTreeString">;
10
- export declare const initialMerkleTree: MerkleTree;
11
- export declare const insertIntoMerkleTree: (timestamp: Timestamp) => (tree: MerkleTree) => MerkleTree;
12
- export declare const diffMerkleTrees: (tree1: MerkleTree, tree2: MerkleTree) => Option.Option<Millis>;
13
- export declare const merkleTreeToString: (m: MerkleTree) => MerkleTreeString;
14
- export declare const unsafeMerkleTreeFromString: (m: MerkleTreeString) => MerkleTree;
15
- //# sourceMappingURL=MerkleTree.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"MerkleTree.d.ts","sourceRoot":"","sources":["../../src/MerkleTree.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACvC,OAAO,EACL,MAAM,EACN,SAAS,EACT,aAAa,EAEd,MAAM,gBAAgB,CAAC;AAKxB,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,IAAI,CAAC,EAAE,aAAa,CAAC;IAC9B,QAAQ,CAAC,GAAG,CAAC,EAAE,UAAU,CAAC;IAC1B,QAAQ,CAAC,GAAG,CAAC,EAAE,UAAU,CAAC;IAC1B,QAAQ,CAAC,GAAG,CAAC,EAAE,UAAU,CAAC;CAC3B;AAED,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,KAAK,CAAC,KAAK,CAAC,kBAAkB,CAAC,CAAC;AAExE,eAAO,MAAM,iBAAiB,YAAoC,CAAC;AAwBnE,eAAO,MAAM,oBAAoB,cACnB,SAAS,YACd,UAAU,KAAG,UAKnB,CAAC;AAKJ,eAAO,MAAM,eAAe,UACnB,UAAU,SACV,UAAU,KAChB,aAAa,CAAC,MAAM,CAiBtB,CAAC;AAEF,eAAO,MAAM,kBAAkB,MAAO,UAAU,KAAG,gBACZ,CAAC;AAExC,eAAO,MAAM,0BAA0B,MAAO,gBAAgB,KAAG,UACpC,CAAC"}
@@ -1,49 +0,0 @@
1
- import { Option } from "effect";
2
- import { timestampToHash, } from "./Timestamp.js";
3
- export const initialMerkleTree = Object.create(null);
4
- const timestampToKey = (timestamp) => Math.floor(timestamp.millis / 1000 / 60).toString(3);
5
- const insertKey = (tree, key, hash) => {
6
- if (key.length === 0)
7
- return tree;
8
- const childKey = key[0];
9
- const child = tree[childKey] || {};
10
- return {
11
- ...tree,
12
- [childKey]: {
13
- ...child,
14
- ...insertKey(child, key.slice(1), hash),
15
- // @ts-expect-error undefined is OK
16
- hash: child.hash ^ hash,
17
- },
18
- };
19
- };
20
- export const insertIntoMerkleTree = (timestamp) => (tree) => {
21
- const key = timestampToKey(timestamp);
22
- const hash = timestampToHash(timestamp);
23
- // @ts-expect-error undefined is OK
24
- return insertKey({ ...tree, hash: tree.hash ^ hash }, key, hash);
25
- };
26
- const keyToTimestamp = (key) => (parseInt(key.length > 0 ? key : "0", 3) * 1000 * 60);
27
- export const diffMerkleTrees = (tree1, tree2) => {
28
- if (tree1.hash === tree2.hash)
29
- return Option.none();
30
- for1: for (let node1 = tree1, node2 = tree2, key = "";;) {
31
- for (const k of ["0", "1", "2"]) {
32
- const next1 = node1[k];
33
- const next2 = node2[k];
34
- if (!next1 && !next2)
35
- continue;
36
- if (!next1 || !next2)
37
- break;
38
- if (next1.hash !== next2.hash) {
39
- key += k;
40
- node1 = next1;
41
- node2 = next2;
42
- continue for1;
43
- }
44
- }
45
- return Option.some(keyToTimestamp(key));
46
- }
47
- };
48
- export const merkleTreeToString = (m) => JSON.stringify(m);
49
- export const unsafeMerkleTreeFromString = (m) => JSON.parse(m);
@@ -1,44 +0,0 @@
1
- import * as Schema from "@effect/schema/Schema";
2
- import { Brand, Context, Effect, Layer } from "effect";
3
- import { Config } from "./Config.js";
4
- import { NanoId, NodeId } from "./Crypto.js";
5
- export interface Timestamp {
6
- readonly node: NodeId;
7
- readonly millis: Millis;
8
- readonly counter: Counter;
9
- }
10
- export declare const Millis: Schema.BrandSchema<number, number & Brand.Brand<"Millis">>;
11
- export type Millis = Schema.Schema.To<typeof Millis>;
12
- export declare const Counter: Schema.BrandSchema<number, number & Brand.Brand<"Counter">>;
13
- export type Counter = Schema.Schema.To<typeof Counter>;
14
- export type TimestampHash = number & Brand.Brand<"TimestampHash">;
15
- export type TimestampString = string & Brand.Brand<"TimestampString">;
16
- export declare const timestampToString: (t: Timestamp) => TimestampString;
17
- export declare const unsafeTimestampFromString: (s: TimestampString) => Timestamp;
18
- export declare const timestampToHash: (t: Timestamp) => TimestampHash;
19
- export declare const makeSyncTimestamp: (millis?: Millis) => Timestamp;
20
- export declare const makeInitialTimestamp: Effect.Effect<NanoId, never, Timestamp>;
21
- export interface Time {
22
- readonly now: Effect.Effect<never, never, Millis>;
23
- }
24
- export declare const Time: Context.Tag<Time, Time>;
25
- export declare const TimeLive: Layer.Layer<never, never, Time>;
26
- export type TimestampError = TimestampDriftError | TimestampCounterOverflowError | TimestampDuplicateNodeError;
27
- export interface TimestampDriftError {
28
- readonly _tag: "TimestampDriftError";
29
- readonly next: Millis;
30
- readonly now: Millis;
31
- }
32
- export interface TimestampCounterOverflowError {
33
- readonly _tag: "TimestampCounterOverflowError";
34
- }
35
- export declare const sendTimestamp: (timestamp: Timestamp) => Effect.Effect<Time | Config, TimestampDriftError | TimestampCounterOverflowError, Timestamp>;
36
- export interface TimestampDuplicateNodeError {
37
- readonly _tag: "TimestampDuplicateNodeError";
38
- readonly node: NodeId;
39
- }
40
- export declare const receiveTimestamp: ({ local, remote, }: {
41
- readonly local: Timestamp;
42
- readonly remote: Timestamp;
43
- }) => Effect.Effect<Time | Config, TimestampDriftError | TimestampCounterOverflowError | TimestampDuplicateNodeError, Timestamp>;
44
- //# sourceMappingURL=Timestamp.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Timestamp.d.ts","sourceRoot":"","sources":["../../src/Timestamp.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAU,KAAK,EAAgB,MAAM,QAAQ,CAAC;AAC7E,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAM7C,MAAM,WAAW,SAAS;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B;AAED,eAAO,MAAM,MAAM,4DAGlB,CAAC;AACF,MAAM,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,MAAM,CAAC,CAAC;AAIrD,eAAO,MAAM,OAAO,6DAGnB,CAAC;AACF,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,OAAO,OAAO,CAAC,CAAC;AAIvD,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,KAAK,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC;AAElE,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,KAAK,CAAC,KAAK,CAAC,iBAAiB,CAAC,CAAC;AAEtE,eAAO,MAAM,iBAAiB,MAAO,SAAS,KAAG,eAKjB,CAAC;AAEjC,eAAO,MAAM,yBAAyB,MAAO,eAAe,KAAG,SAO9D,CAAC;AAEF,eAAO,MAAM,eAAe,MAAO,SAAS,KAAG,aACI,CAAC;AAIpD,eAAO,MAAM,iBAAiB,YACpB,MAAM,KACb,SAID,CAAC;AAEH,eAAO,MAAM,oBAAoB,yCAShC,CAAC;AAEF,MAAM,WAAW,IAAI;IACnB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;CACnD;AAED,eAAO,MAAM,IAAI,yBAAkC,CAAC;AAEpD,eAAO,MAAM,QAAQ,iCAKpB,CAAC;AAEF,MAAM,MAAM,cAAc,GACtB,mBAAmB,GACnB,6BAA6B,GAC7B,2BAA2B,CAAC;AAEhC,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,IAAI,EAAE,qBAAqB,CAAC;IACrC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,6BAA6B;IAC5C,QAAQ,CAAC,IAAI,EAAE,+BAA+B,CAAC;CAChD;AAmCD,eAAO,MAAM,aAAa,cACb,SAAS,KACnB,aAAa,CACd,IAAI,GAAG,MAAM,EACb,mBAAmB,GAAG,6BAA6B,EACnD,SAAS,CASP,CAAC;AAEL,MAAM,WAAW,2BAA2B;IAC1C,QAAQ,CAAC,IAAI,EAAE,6BAA6B,CAAC;IAC7C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,eAAO,MAAM,gBAAgB;oBAIX,SAAS;qBACR,SAAS;MACxB,aAAa,CACf,IAAI,GAAG,MAAM,EACX,mBAAmB,GACnB,6BAA6B,GAC7B,2BAA2B,EAC7B,SAAS,CAuBP,CAAC"}