@evolu/common 6.0.1-preview.2 → 6.0.1-preview.20

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.
Files changed (154) hide show
  1. package/dist/src/Assert.d.ts +6 -3
  2. package/dist/src/Assert.d.ts.map +1 -1
  3. package/dist/src/Assert.js +7 -4
  4. package/dist/src/Brand.d.ts +75 -0
  5. package/dist/src/Brand.d.ts.map +1 -0
  6. package/dist/src/Brand.js +1 -0
  7. package/dist/src/Buffer.d.ts +1 -1
  8. package/dist/src/Buffer.d.ts.map +1 -1
  9. package/dist/src/Buffer.js +1 -1
  10. package/dist/src/CallbackRegistry.d.ts +53 -0
  11. package/dist/src/CallbackRegistry.d.ts.map +1 -0
  12. package/dist/src/CallbackRegistry.js +25 -0
  13. package/dist/src/Console.d.ts +31 -6
  14. package/dist/src/Console.d.ts.map +1 -1
  15. package/dist/src/Console.js +72 -9
  16. package/dist/src/Crypto.d.ts +61 -34
  17. package/dist/src/Crypto.d.ts.map +1 -1
  18. package/dist/src/Crypto.js +29 -42
  19. package/dist/src/Evolu/Db.d.ts +137 -66
  20. package/dist/src/Evolu/Db.d.ts.map +1 -1
  21. package/dist/src/Evolu/Db.js +300 -705
  22. package/dist/src/Evolu/Diff.d.ts +3 -3
  23. package/dist/src/Evolu/Diff.d.ts.map +1 -1
  24. package/dist/src/Evolu/Diff.js +7 -5
  25. package/dist/src/Evolu/Evolu.d.ts +200 -115
  26. package/dist/src/Evolu/Evolu.d.ts.map +1 -1
  27. package/dist/src/Evolu/Evolu.js +287 -157
  28. package/dist/src/Evolu/Internal.d.ts +0 -2
  29. package/dist/src/Evolu/Internal.d.ts.map +1 -1
  30. package/dist/src/Evolu/Internal.js +0 -2
  31. package/dist/src/Evolu/LocalAuth.d.ts +144 -0
  32. package/dist/src/Evolu/LocalAuth.d.ts.map +1 -0
  33. package/dist/src/Evolu/LocalAuth.js +171 -0
  34. package/dist/src/Evolu/Owner.d.ts +152 -127
  35. package/dist/src/Evolu/Owner.d.ts.map +1 -1
  36. package/dist/src/Evolu/Owner.js +88 -107
  37. package/dist/src/Evolu/Platform.d.ts +9 -7
  38. package/dist/src/Evolu/Platform.d.ts.map +1 -1
  39. package/dist/src/Evolu/Protocol.d.ts +170 -186
  40. package/dist/src/Evolu/Protocol.d.ts.map +1 -1
  41. package/dist/src/Evolu/Protocol.js +487 -343
  42. package/dist/src/Evolu/Public.d.ts +6 -8
  43. package/dist/src/Evolu/Public.d.ts.map +1 -1
  44. package/dist/src/Evolu/Public.js +2 -3
  45. package/dist/src/Evolu/PublicKysely.js +3 -3
  46. package/dist/src/Evolu/Query.d.ts +2 -1
  47. package/dist/src/Evolu/Query.d.ts.map +1 -1
  48. package/dist/src/Evolu/Relay.d.ts +4 -3
  49. package/dist/src/Evolu/Relay.d.ts.map +1 -1
  50. package/dist/src/Evolu/Relay.js +50 -12
  51. package/dist/src/Evolu/Schema.d.ts +129 -73
  52. package/dist/src/Evolu/Schema.d.ts.map +1 -1
  53. package/dist/src/Evolu/Schema.js +169 -89
  54. package/dist/src/Evolu/Storage.d.ts +159 -14
  55. package/dist/src/Evolu/Storage.d.ts.map +1 -1
  56. package/dist/src/Evolu/Storage.js +42 -32
  57. package/dist/src/Evolu/Sync.d.ts +77 -13
  58. package/dist/src/Evolu/Sync.d.ts.map +1 -1
  59. package/dist/src/Evolu/Sync.js +453 -20
  60. package/dist/src/Evolu/Timestamp.d.ts +35 -27
  61. package/dist/src/Evolu/Timestamp.d.ts.map +1 -1
  62. package/dist/src/Evolu/Timestamp.js +27 -18
  63. package/dist/src/ManyToManyMap.d.ts +74 -10
  64. package/dist/src/ManyToManyMap.d.ts.map +1 -1
  65. package/dist/src/ManyToManyMap.js +41 -6
  66. package/dist/src/Number.d.ts +2 -1
  67. package/dist/src/Number.d.ts.map +1 -1
  68. package/dist/src/Random.d.ts +3 -2
  69. package/dist/src/Random.d.ts.map +1 -1
  70. package/dist/src/RefCountedResourceManager.d.ts +119 -0
  71. package/dist/src/RefCountedResourceManager.d.ts.map +1 -0
  72. package/dist/src/RefCountedResourceManager.js +197 -0
  73. package/dist/src/Result.d.ts +147 -51
  74. package/dist/src/Result.d.ts.map +1 -1
  75. package/dist/src/Result.js +7 -241
  76. package/dist/src/Sqlite.d.ts +21 -4
  77. package/dist/src/Sqlite.d.ts.map +1 -1
  78. package/dist/src/Sqlite.js +50 -8
  79. package/dist/src/Task.d.ts +511 -0
  80. package/dist/src/Task.d.ts.map +1 -0
  81. package/dist/src/Task.js +410 -0
  82. package/dist/src/Time.d.ts +59 -0
  83. package/dist/src/Time.d.ts.map +1 -1
  84. package/dist/src/Time.js +87 -4
  85. package/dist/src/Type.d.ts +462 -327
  86. package/dist/src/Type.d.ts.map +1 -1
  87. package/dist/src/Type.js +490 -436
  88. package/dist/src/Types.d.ts +0 -74
  89. package/dist/src/Types.d.ts.map +1 -1
  90. package/dist/src/WebSocket.d.ts +5 -2
  91. package/dist/src/WebSocket.d.ts.map +1 -1
  92. package/dist/src/WebSocket.js +12 -13
  93. package/dist/src/Worker.d.ts +39 -11
  94. package/dist/src/Worker.d.ts.map +1 -1
  95. package/dist/src/Worker.js +22 -4
  96. package/dist/src/index.d.ts +3 -3
  97. package/dist/src/index.d.ts.map +1 -1
  98. package/dist/src/index.js +3 -3
  99. package/package.json +14 -13
  100. package/src/Assert.ts +8 -7
  101. package/src/Brand.ts +75 -0
  102. package/src/Buffer.ts +1 -1
  103. package/src/CallbackRegistry.ts +84 -0
  104. package/src/Console.ts +91 -11
  105. package/src/Crypto.ts +94 -79
  106. package/src/Evolu/Db.ts +505 -1027
  107. package/src/Evolu/Diff.ts +7 -5
  108. package/src/Evolu/Evolu.ts +682 -329
  109. package/src/Evolu/Internal.ts +0 -2
  110. package/src/Evolu/LocalAuth.ts +422 -0
  111. package/src/Evolu/Owner.ts +221 -235
  112. package/src/Evolu/Platform.ts +9 -9
  113. package/src/Evolu/Protocol.ts +681 -614
  114. package/src/Evolu/Public.ts +7 -14
  115. package/src/Evolu/PublicKysely.ts +3 -3
  116. package/src/Evolu/Query.ts +2 -1
  117. package/src/Evolu/Relay.ts +62 -16
  118. package/src/Evolu/Schema.ts +391 -191
  119. package/src/Evolu/Storage.ts +275 -55
  120. package/src/Evolu/Sync.ts +758 -37
  121. package/src/Evolu/Timestamp.ts +38 -35
  122. package/src/ManyToManyMap.ts +127 -24
  123. package/src/Number.ts +2 -6
  124. package/src/Random.ts +3 -2
  125. package/src/RefCountedResourceManager.ts +368 -0
  126. package/src/Result.ts +152 -53
  127. package/src/Sqlite.ts +59 -23
  128. package/src/Task.ts +779 -0
  129. package/src/Time.ts +168 -4
  130. package/src/Type.ts +696 -660
  131. package/src/Types.ts +0 -76
  132. package/src/WebSocket.ts +23 -17
  133. package/src/Worker.ts +72 -23
  134. package/src/index.ts +3 -3
  135. package/dist/src/Callbacks.d.ts +0 -20
  136. package/dist/src/Callbacks.d.ts.map +0 -1
  137. package/dist/src/Callbacks.js +0 -18
  138. package/dist/src/Evolu/Config.d.ts +0 -69
  139. package/dist/src/Evolu/Config.d.ts.map +0 -1
  140. package/dist/src/Evolu/Config.js +0 -9
  141. package/dist/src/Evolu/Kysely.d.ts +0 -6
  142. package/dist/src/Evolu/Kysely.d.ts.map +0 -1
  143. package/dist/src/Evolu/Kysely.js +0 -21
  144. package/dist/src/NanoId.d.ts +0 -27
  145. package/dist/src/NanoId.d.ts.map +0 -1
  146. package/dist/src/NanoId.js +0 -6
  147. package/dist/src/Promise.d.ts +0 -180
  148. package/dist/src/Promise.d.ts.map +0 -1
  149. package/dist/src/Promise.js +0 -176
  150. package/src/Callbacks.ts +0 -43
  151. package/src/Evolu/Config.ts +0 -83
  152. package/src/Evolu/Kysely.ts +0 -38
  153. package/src/NanoId.ts +0 -39
  154. package/src/Promise.ts +0 -295
@@ -1 +1 @@
1
- {"version":3,"file":"Owner.d.ts","sourceRoot":"","sources":["../../../src/Evolu/Owner.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAGH,O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,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,UAAU,CAAC,EAAE,aAAa,CAAC,eAAe,CAAC,CAAC;CACtD;AAED,gEAAgE;AAChE,eAAO,MAAM,gBAAgB,GAC3B,QAAQ,WAAW,EACnB,aAAa,aAAa,CAAC,eAAe,CAAC,KAC1C,UAMF,CAAC;AAEF;;;;;;;;;;;;;;;;;GAiBG;AACH,eAAO,MAAM,gBAAgB,GAC3B,OAAO,QAAQ,EACf,MAAM,qBAAqB,CAAC,MAAM,GAAG,MAAM,CAAC,EAC5C,aAAa,aAAa,CAAC,eAAe,CAAC,KAC1C,UAQF,CAAC;AAEF,iEAAiE;AACjE,MAAM,WAAW,WAAY,SAAQ,KAAK;IACxC,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC;IAC7B,QAAQ,CAAC,UAAU,CAAC,EAAE,aAAa,CAAC,eAAe,CAAC,CAAC;CACtD;AAED;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,GAC5B,QAAQ,WAAW,EACnB,aAAa,aAAa,CAAC,eAAe,CAAC,KAC1C,WAMF,CAAC;AAEF;;;;GAIG;AACH,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,IAAI,EAAE,qBAAqB,CAAC;IACrC,QAAQ,CAAC,EAAE,EAAE,OAAO,CAAC;IACrB,QAAQ,CAAC,aAAa,EAAE,aAAa,CAAC;IACtC,QAAQ,CAAC,UAAU,CAAC,EAAE,aAAa,CAAC,eAAe,CAAC,CAAC;CACtD;AAED,wEAAwE;AACxE,eAAO,MAAM,yBAAyB,GACpC,aAAa,WAAW,KACvB,mBAKD,CAAC"}
@@ -1,131 +1,112 @@
1
+ import * as bip39 from "@scure/bip39";
2
+ import { wordlist } from "@scure/bip39/wordlists/english.js";
3
+ import { createSlip21, EncryptionKey, Entropy16, Entropy32, } from "../Crypto.js";
4
+ import { brand, Id, IdBytes, idBytesToId, idToIdBytes, } from "../Type.js";
1
5
  /**
2
- * TODO:
6
+ * 32 bytes of cryptographic entropy used to derive {@link Owner} keys.
3
7
  *
4
- * @module
8
+ * Can be created using {@link createOwnerSecret} or converted from a
9
+ * {@link Mnemonic} using {@link mnemonicToOwnerSecret}.
5
10
  */
6
- import { assert } from "../Assert.js";
7
- import { createEncryptionKey, createSlip21, createSlip21Id, mnemonicToMnemonicSeed, } from "../Crypto.js";
8
- import { brand, DateIso, Id, length, Uint8Array, } from "../Type.js";
9
- import { createInitialTimestamp, timestampToTimestampString, } from "./Timestamp.js";
11
+ export const OwnerSecret = brand("OwnerSecret", Entropy32);
12
+ /** Creates a {@link OwnerSecret}. */
13
+ export const createOwnerSecret = (deps) => deps.randomBytes.create(32);
14
+ /** Converts an {@link OwnerSecret} to a {@link Mnemonic}. */
15
+ export const ownerSecretToMnemonic = (secret) => bip39.entropyToMnemonic(secret, wordlist);
16
+ /** Converts a {@link Mnemonic} to an {@link OwnerSecret}. */
17
+ export const mnemonicToOwnerSecret = (mnemonic) => bip39.mnemonicToEntropy(mnemonic, wordlist);
10
18
  /**
11
- * The unique identifier of {@link Owner} derived from the {@link Mnemonic}.
12
- *
13
- * This branded {@link Id} type, generated by {@link createSlip21Id}, is a
14
- * 21-character {@link Base64Url} string (126 bits of entropy), providing a
15
- * compact, shareable, and secure identifier for UI use, tied to the owner's
16
- * mnemonic and derivation path.
19
+ * OwnerId is a branded {@link Id} that uniquely identifies an {@link Owner}.
20
+ * Branded from {@link Id} to leverage existing helpers like {@link idToIdBytes}.
17
21
  */
18
22
  export const OwnerId = brand("OwnerId", Id);
19
- export const writeKeyLength = 16;
20
- /**
21
- * A secure token proving the initiator can write changes. Derived from a
22
- * mnemonic or randomly generated. It's rotatable.
23
- */
24
- export const WriteKey = brand("WriteKey", length(writeKeyLength)(Uint8Array));
23
+ /** Bytes representation of {@link OwnerId}. */
24
+ export const OwnerIdBytes = brand("OwnerIdBytes", IdBytes);
25
+ export const ownerIdToOwnerIdBytes = (ownerId) => idToIdBytes(ownerId);
26
+ export const ownerIdBytesToOwnerId = (ownerIdBytes) => idBytesToId(ownerIdBytes);
27
+ export const ownerWriteKeyLength = 16;
28
+ export const OwnerEncryptionKey = brand("OwnerEncryptionKey", EncryptionKey);
25
29
  /**
26
- * Creates an {@link AppOwner}, optionally from an existing mnemonic to restore
27
- * it on another device; otherwise, generates a new mnemonic.
30
+ * A secure token for write operations. It's derived from {@link OwnerSecret} by
31
+ * default and can be rotated via {@link createOwnerWriteKey}.
28
32
  */
29
- export const createAppOwner = (deps) => (mnemonic = deps.createMnemonic()) => {
30
- const owner = createOwner(deps)(mnemonic);
31
- return { type: "AppOwner", ...owner };
32
- };
33
+ export const OwnerWriteKey = brand("OwnerWriteKey", Entropy16);
34
+ /** Creates a randomly generated {@link OwnerWriteKey}. */
35
+ export const createOwnerWriteKey = (deps) => deps.randomBytes.create(16);
33
36
  /**
34
- * Creates a {@link ShardOwner} for sharding app data with a freshly generated
35
- * mnemonic. Unlike {@link createAppOwner}, it doesn’t accept an existing
36
- * mnemonic because ShardOwner mnemonics are always generated and restored
37
- * automatically via database sync.
37
+ * Creates an {@link Owner} from a {@link OwnerSecret} using SLIP-21 key
38
+ * derivation.
39
+ *
40
+ * This is an internal helper function, use:
41
+ *
42
+ * - {@link createAppOwner}
43
+ * - {@link createShardOwner}
44
+ * - {@link createSharedOwner}
45
+ * - {@link createSharedReadonlyOwner}
38
46
  */
39
- export const createShardOwner = (deps) => {
40
- const owner = createOwner(deps)();
47
+ export const createOwner = (secret) => ({
48
+ id: ownerIdBytesToOwnerId(OwnerIdBytes.orThrow(createSlip21(secret, ["Evolu", "OwnerIdBytes"]).slice(0, 16))),
49
+ encryptionKey: OwnerEncryptionKey.orThrow(createSlip21(secret, ["Evolu", "OwnerEncryptionKey"])),
50
+ writeKey: OwnerWriteKey.orThrow(createSlip21(secret, ["Evolu", "OwnerWriteKey"]).slice(0, 16)),
51
+ });
52
+ /** Creates an {@link AppOwner} from an {@link OwnerSecret}. */
53
+ export const createAppOwner = (secret) => ({
54
+ type: "AppOwner",
55
+ mnemonic: ownerSecretToMnemonic(secret),
56
+ ...createOwner(secret),
57
+ });
58
+ /** Creates a {@link ShardOwner} from an {@link OwnerSecret}. */
59
+ export const createShardOwner = (secret, transports) => {
41
60
  return {
42
61
  type: "ShardOwner",
43
- mnemonic: owner.mnemonic,
44
- writeKey: owner.writeKey,
62
+ ...createOwner(secret),
63
+ ...(transports && { transports }),
45
64
  };
46
65
  };
47
66
  /**
48
- * Creates a fresh {@link SharedOwner} for sharing data with write access. Takes
49
- * no arguments as both `mnemonic` and rotatable `writeKey` are newly generated;
50
- * when shared, recipients use the provided `mnemonic` and `writeKey` directly
51
- * as a {@link SharedOwner} without needing to recreate it.
52
- */
53
- export const createSharedOwner = (deps) => {
54
- const mnemonic = deps.createMnemonic();
55
- const writeKey = createWriteKey(deps)(); // Random, no seed
56
- return {
57
- type: "SharedOwner",
58
- mnemonic,
59
- writeKey,
60
- };
61
- };
62
- /**
63
- * Creates a {@link SharedReadonlyOwner} from a {@link SharedOwner} for read-only
64
- * data sharing. Extracts the `mnemonic` from the provided {@link SharedOwner},
65
- * omitting its `writeKey` to ensure read-only access.
67
+ * Derives a {@link ShardOwner} from an {@link AppOwner} using the specified path.
68
+ *
69
+ * **Advantages of derived owners:**
70
+ *
71
+ * - **Deterministic**: Same path always produces the same ShardOwner across all
72
+ * devices
73
+ * - **Immediate availability**: Can be hardcoded and used before sync occurs
74
+ * - **Consistent setup**: All devices start with identical data structure
75
+ * - **Lifecycle management**: Can implement epoch patterns for clean data
76
+ * deletion and recreation
77
+ *
78
+ * **Common patterns:**
79
+ *
80
+ * - Use paths like `["shard", 1]` for versioned data lifecycle
81
+ * - Use paths like `["project", "MyApp", 1]` for named partitions with versions
82
+ * - Each device can derive the same owners and set up initial structure
66
83
  */
67
- export const createSharedReadonlyOwner = (sharedOwner) => {
84
+ export const deriveShardOwner = (owner, path, transports) => {
85
+ const secret = createSlip21(owner.encryptionKey, path);
68
86
  return {
69
- type: "SharedReadonlyOwner",
70
- mnemonic: sharedOwner.mnemonic,
71
- };
72
- };
73
- /** Creates an {@link Owner} with optional `mnemonic` and `writeKey`. */
74
- export const createOwner = (deps) => (mnemonic = deps.createMnemonic(), writeKey) => {
75
- const seed = mnemonicToMnemonicSeed(mnemonic);
76
- const id = createSlip21Id(seed, ["Evolu", "Owner Id"]);
77
- const encryptionKey = createEncryptionKey(seed);
78
- const createdAt = DateIso.fromParent(new Date(deps.time.now()));
79
- assert(createdAt.ok, "Invalid DateIso: bad system clock");
80
- return {
81
- mnemonic,
82
- createdAt: createdAt.value,
83
- id,
84
- encryptionKey,
85
- writeKey: writeKey ?? createWriteKey(deps)(seed),
87
+ type: "ShardOwner",
88
+ ...createOwner(secret),
89
+ ...(transports && { transports }),
86
90
  };
87
91
  };
88
- export const createWriteKey = (deps) => (seed) => {
89
- const key = seed
90
- ? createSlip21(seed, ["Evolu", "Write Key"]).slice(0, 16)
91
- : deps.createRandomBytes(16);
92
- const writeKey = WriteKey.from(key);
93
- assert(writeKey.ok, "Ensure valid WriteKey");
94
- return writeKey.value;
95
- };
96
- /**
97
- * Creates an {@link OwnerRow} from any {@link Owner} variant for the
98
- * `evolu_owner` table, adding a `timestamp` ({@link TimestampString}) for CRDT
99
- * sync.
100
- */
101
- export const createOwnerRow = (deps) => (owner) => {
102
- const timestamp = timestampToTimestampString(createInitialTimestamp(deps));
103
- switch (owner.type) {
104
- case "AppOwner": {
105
- const { type, ...rest } = owner;
106
- return { ...rest, timestamp };
107
- }
108
- case "ShardOwner":
109
- case "SharedOwner":
110
- return {
111
- ...createOwner(deps)(owner.mnemonic, owner.writeKey),
112
- timestamp,
113
- };
114
- case "SharedReadonlyOwner":
115
- return {
116
- ...createOwner(deps)(owner.mnemonic),
117
- writeKey: null,
118
- timestamp,
119
- };
120
- }
121
- };
122
92
  /**
123
- * Rotates the {@link WriteKey} for an {@link AppOwner}, {@link ShardOwner}, or
124
- * {@link SharedOwner}, returning a new instance with the updated key.
93
+ * Creates a {@link SharedOwner} from an {@link OwnerSecret} for collaborative
94
+ * write access.
95
+ *
96
+ * Use {@link createSharedReadonlyOwner} to create a read-only version for
97
+ * sharing.
125
98
  */
126
- export const rotateWriteKey = (owner, newWriteKey) => {
99
+ export const createSharedOwner = (secret, transports) => {
127
100
  return {
128
- ...owner,
129
- writeKey: newWriteKey,
101
+ type: "SharedOwner",
102
+ ...createOwner(secret),
103
+ ...(transports && { transports }),
130
104
  };
131
105
  };
106
+ /** Creates a {@link SharedReadonlyOwner} from a {@link SharedOwner}. */
107
+ export const createSharedReadonlyOwner = (sharedOwner) => ({
108
+ type: "SharedReadonlyOwner",
109
+ id: sharedOwner.id,
110
+ encryptionKey: sharedOwner.encryptionKey,
111
+ ...(sharedOwner.transports && { transports: sharedOwner.transports }),
112
+ });
@@ -1,4 +1,3 @@
1
- import { Config } from "./Config.js";
2
1
  /**
3
2
  * FlushSync is for libraries like React to flush updates synchronously inside
4
3
  * the provided callback to ensure the DOM is updated immediately.
@@ -13,11 +12,14 @@ export type FlushSync = (callback: () => void) => void;
13
12
  export interface FlushSyncDep {
14
13
  readonly flushSync: FlushSync;
15
14
  }
16
- export interface AppState {
17
- readonly reset: () => void;
18
- }
19
- export type CreateAppState = (config: Config) => AppState;
20
- export interface CreateAppStateDep {
21
- readonly createAppState: CreateAppState;
15
+ /**
16
+ * Reload the app in a platform-specific way.
17
+ *
18
+ * - **Web**: Redirects to the specified URL
19
+ * - **React Native**: Restarts the app (URL parameter ignored)
20
+ */
21
+ export type ReloadApp = (url: string) => void;
22
+ export interface ReloadAppDep {
23
+ readonly reloadApp: ReloadApp;
22
24
  }
23
25
  //# sourceMappingURL=Platform.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"Platform.d.ts","sourceRoot":"","sources":["../../../src/Evolu/Platform.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC;;;;;;;;;GASG;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;AAEvD,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;CAC/B;AAED,MAAM,WAAW,QAAQ;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,IAAI,CAAC;CAC5B;AAED,MAAM,MAAM,cAAc,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,QAAQ,CAAC;AAE1D,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,cAAc,EAAE,cAAc,CAAC;CACzC"}
1
+ {"version":3,"file":"Platform.d.ts","sourceRoot":"","sources":["../../../src/Evolu/Platform.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;AAEvD,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;CAC/B;AAED;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,CAAC;AAE9C,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;CAC/B"}