@evolu/common 5.4.7 → 6.0.0

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 (242) hide show
  1. package/README.md +30 -34
  2. package/dist/src/Array.d.ts +17 -0
  3. package/dist/src/Array.d.ts.map +1 -0
  4. package/dist/src/Array.js +12 -0
  5. package/dist/src/Assert.d.ts +68 -0
  6. package/dist/src/Assert.d.ts.map +1 -0
  7. package/dist/src/Assert.js +77 -0
  8. package/dist/src/BigInt.d.ts +20 -0
  9. package/dist/src/BigInt.d.ts.map +1 -0
  10. package/dist/src/BigInt.js +18 -0
  11. package/dist/src/Buffer.d.ts +92 -0
  12. package/dist/src/Buffer.d.ts.map +1 -0
  13. package/dist/src/Buffer.js +62 -0
  14. package/dist/src/Callbacks.d.ts +20 -0
  15. package/dist/src/Callbacks.d.ts.map +1 -0
  16. package/dist/src/Callbacks.js +18 -0
  17. package/dist/src/Console.d.ts +78 -0
  18. package/dist/src/Console.d.ts.map +1 -0
  19. package/dist/src/Console.js +103 -0
  20. package/dist/src/Crypto.d.ts +72 -39
  21. package/dist/src/Crypto.d.ts.map +1 -1
  22. package/dist/src/Crypto.js +89 -54
  23. package/dist/src/Eq.d.ts +97 -0
  24. package/dist/src/Eq.d.ts.map +1 -0
  25. package/dist/src/Eq.js +167 -0
  26. package/dist/src/Error.d.ts +14 -10
  27. package/dist/src/Error.d.ts.map +1 -1
  28. package/dist/src/Error.js +43 -11
  29. package/dist/src/Evolu/Config.d.ts +69 -0
  30. package/dist/src/Evolu/Config.d.ts.map +1 -0
  31. package/dist/src/Evolu/Config.js +9 -0
  32. package/dist/src/Evolu/Db.d.ts +126 -0
  33. package/dist/src/Evolu/Db.d.ts.map +1 -0
  34. package/dist/src/Evolu/Db.js +774 -0
  35. package/dist/src/Evolu/Diff.d.ts +43 -0
  36. package/dist/src/Evolu/Diff.d.ts.map +1 -0
  37. package/dist/src/Evolu/Diff.js +95 -0
  38. package/dist/src/Evolu/Evolu.d.ts +334 -0
  39. package/dist/src/Evolu/Evolu.d.ts.map +1 -0
  40. package/dist/src/Evolu/Evolu.js +434 -0
  41. package/dist/src/Evolu/Internal.d.ts +26 -0
  42. package/dist/src/Evolu/Internal.d.ts.map +1 -0
  43. package/dist/src/Evolu/Internal.js +25 -0
  44. package/dist/src/Evolu/Kysely.d.ts +6 -0
  45. package/dist/src/Evolu/Kysely.d.ts.map +1 -0
  46. package/dist/src/Evolu/Kysely.js +21 -0
  47. package/dist/src/Evolu/Owner.d.ts +155 -0
  48. package/dist/src/Evolu/Owner.d.ts.map +1 -0
  49. package/dist/src/Evolu/Owner.js +126 -0
  50. package/dist/src/Evolu/Platform.d.ts +23 -0
  51. package/dist/src/Evolu/Platform.d.ts.map +1 -0
  52. package/dist/src/Evolu/Platform.js +1 -0
  53. package/dist/src/Evolu/Protocol.d.ts +401 -0
  54. package/dist/src/Evolu/Protocol.d.ts.map +1 -0
  55. package/dist/src/Evolu/Protocol.js +1151 -0
  56. package/dist/src/Evolu/Public.d.ts +18 -0
  57. package/dist/src/Evolu/Public.d.ts.map +1 -0
  58. package/dist/src/Evolu/Public.js +11 -0
  59. package/dist/src/Evolu/PublicKysely.d.ts +148 -0
  60. package/dist/src/Evolu/PublicKysely.d.ts.map +1 -0
  61. package/dist/src/Evolu/PublicKysely.js +185 -0
  62. package/dist/src/Evolu/Query.d.ts +63 -0
  63. package/dist/src/Evolu/Query.d.ts.map +1 -0
  64. package/dist/src/Evolu/Query.js +61 -0
  65. package/dist/src/Evolu/Relay.d.ts +13 -0
  66. package/dist/src/Evolu/Relay.d.ts.map +1 -0
  67. package/dist/src/Evolu/Relay.js +109 -0
  68. package/dist/src/Evolu/Schema.d.ts +201 -0
  69. package/dist/src/Evolu/Schema.d.ts.map +1 -0
  70. package/dist/src/Evolu/Schema.js +150 -0
  71. package/dist/src/Evolu/Storage.d.ts +49 -0
  72. package/dist/src/Evolu/Storage.d.ts.map +1 -0
  73. package/dist/src/Evolu/Storage.js +1111 -0
  74. package/dist/src/Evolu/Sync.d.ts +59 -0
  75. package/dist/src/Evolu/Sync.d.ts.map +1 -0
  76. package/dist/src/Evolu/Sync.js +29 -0
  77. package/dist/src/Evolu/Timestamp.d.ts +106 -0
  78. package/dist/src/Evolu/Timestamp.d.ts.map +1 -0
  79. package/dist/src/Evolu/Timestamp.js +179 -0
  80. package/dist/src/Function.d.ts +54 -0
  81. package/dist/src/Function.d.ts.map +1 -0
  82. package/dist/src/Function.js +38 -0
  83. package/dist/src/ManyToManyMap.d.ts +26 -0
  84. package/dist/src/ManyToManyMap.d.ts.map +1 -0
  85. package/dist/src/ManyToManyMap.js +92 -0
  86. package/dist/src/NanoId.d.ts +27 -0
  87. package/dist/src/NanoId.d.ts.map +1 -0
  88. package/dist/src/NanoId.js +6 -0
  89. package/dist/src/Number.d.ts +42 -0
  90. package/dist/src/Number.d.ts.map +1 -0
  91. package/dist/src/Number.js +55 -0
  92. package/dist/src/Object.d.ts +35 -0
  93. package/dist/src/Object.d.ts.map +1 -0
  94. package/dist/src/Object.js +36 -0
  95. package/dist/src/Order.d.ts +90 -0
  96. package/dist/src/Order.d.ts.map +1 -0
  97. package/dist/src/Order.js +85 -0
  98. package/dist/src/Promise.d.ts +180 -0
  99. package/dist/src/Promise.d.ts.map +1 -0
  100. package/dist/src/Promise.js +176 -0
  101. package/dist/src/Random.d.ts +52 -0
  102. package/dist/src/Random.d.ts.map +1 -0
  103. package/dist/src/Random.js +29 -0
  104. package/dist/src/Ref.d.ts +40 -0
  105. package/dist/src/Ref.d.ts.map +1 -0
  106. package/dist/src/Ref.js +13 -0
  107. package/dist/src/Result.d.ts +421 -0
  108. package/dist/src/Result.d.ts.map +1 -0
  109. package/dist/src/Result.js +357 -0
  110. package/dist/src/Skiplist.d.ts +23 -0
  111. package/dist/src/Skiplist.d.ts.map +1 -0
  112. package/dist/src/Skiplist.js +58 -0
  113. package/dist/src/Sqlite.d.ts +116 -52
  114. package/dist/src/Sqlite.d.ts.map +1 -1
  115. package/dist/src/Sqlite.js +183 -67
  116. package/dist/src/Store.d.ts +45 -8
  117. package/dist/src/Store.d.ts.map +1 -1
  118. package/dist/src/Store.js +33 -17
  119. package/dist/src/String.d.ts +2 -0
  120. package/dist/src/String.d.ts.map +1 -0
  121. package/dist/src/String.js +14 -0
  122. package/dist/src/Time.d.ts +20 -0
  123. package/dist/src/Time.d.ts.map +1 -0
  124. package/dist/src/Time.js +25 -0
  125. package/dist/src/Type.d.ts +1937 -0
  126. package/dist/src/Type.d.ts.map +1 -0
  127. package/dist/src/Type.js +2002 -0
  128. package/dist/src/Types.d.ts +188 -0
  129. package/dist/src/Types.d.ts.map +1 -0
  130. package/dist/src/Types.js +6 -0
  131. package/dist/src/WebSocket.d.ts +112 -0
  132. package/dist/src/WebSocket.d.ts.map +1 -0
  133. package/dist/src/WebSocket.js +139 -0
  134. package/dist/src/Worker.d.ts +44 -0
  135. package/dist/src/Worker.d.ts.map +1 -0
  136. package/dist/src/Worker.js +66 -0
  137. package/dist/src/index.d.ts +24 -11
  138. package/dist/src/index.d.ts.map +1 -1
  139. package/dist/src/index.js +24 -11
  140. package/package.json +29 -38
  141. package/src/Array.ts +39 -0
  142. package/src/Assert.ts +116 -0
  143. package/src/BigInt.ts +29 -0
  144. package/src/Buffer.ts +175 -0
  145. package/src/Callbacks.ts +43 -0
  146. package/src/Console.ts +159 -0
  147. package/src/Crypto.ts +169 -115
  148. package/src/Eq.ts +204 -0
  149. package/src/Error.ts +57 -20
  150. package/src/Evolu/Config.ts +83 -0
  151. package/src/Evolu/Db.ts +1275 -0
  152. package/src/Evolu/Diff.ts +142 -0
  153. package/src/Evolu/Evolu.ts +947 -0
  154. package/src/Evolu/Internal.ts +26 -0
  155. package/src/Evolu/Kysely.ts +38 -0
  156. package/src/Evolu/Owner.ts +296 -0
  157. package/src/Evolu/Platform.ts +27 -0
  158. package/src/Evolu/Protocol.ts +1857 -0
  159. package/src/Evolu/Public.ts +43 -0
  160. package/src/Evolu/PublicKysely.ts +240 -0
  161. package/src/Evolu/Query.ts +167 -0
  162. package/src/Evolu/Relay.ts +142 -0
  163. package/src/Evolu/Schema.ts +417 -0
  164. package/src/Evolu/Storage.ts +1281 -0
  165. package/src/Evolu/Sync.ts +105 -0
  166. package/src/Evolu/Timestamp.ts +311 -0
  167. package/src/Function.ts +58 -0
  168. package/src/ManyToManyMap.ts +140 -0
  169. package/src/NanoId.ts +39 -0
  170. package/src/Number.ts +90 -0
  171. package/src/Object.ts +64 -0
  172. package/src/Order.ts +113 -0
  173. package/src/Promise.ts +295 -0
  174. package/src/Random.ts +68 -0
  175. package/src/Ref.ts +63 -0
  176. package/src/Result.ts +453 -0
  177. package/src/Skiplist.ts +102 -0
  178. package/src/Sqlite.ts +366 -153
  179. package/src/Store.ts +79 -36
  180. package/src/String.ts +10 -0
  181. package/src/Time.ts +36 -0
  182. package/src/Type.ts +3978 -0
  183. package/src/Types.ts +209 -0
  184. package/src/WebSocket.ts +273 -0
  185. package/src/Worker.ts +129 -0
  186. package/src/index.ts +24 -11
  187. package/dist/src/Config.d.ts +0 -56
  188. package/dist/src/Config.d.ts.map +0 -1
  189. package/dist/src/Config.js +0 -39
  190. package/dist/src/Crdt.d.ts +0 -89
  191. package/dist/src/Crdt.d.ts.map +0 -1
  192. package/dist/src/Crdt.js +0 -181
  193. package/dist/src/Db.d.ts +0 -107
  194. package/dist/src/Db.d.ts.map +0 -1
  195. package/dist/src/Db.js +0 -443
  196. package/dist/src/Diff.d.ts +0 -27
  197. package/dist/src/Diff.d.ts.map +0 -1
  198. package/dist/src/Diff.js +0 -84
  199. package/dist/src/Evolu.d.ts +0 -426
  200. package/dist/src/Evolu.d.ts.map +0 -1
  201. package/dist/src/Evolu.js +0 -333
  202. package/dist/src/Model.d.ts +0 -141
  203. package/dist/src/Model.d.ts.map +0 -1
  204. package/dist/src/Model.js +0 -125
  205. package/dist/src/Murmurhash.d.ts +0 -2
  206. package/dist/src/Murmurhash.d.ts.map +0 -1
  207. package/dist/src/Murmurhash.js +0 -60
  208. package/dist/src/Owner.d.ts +0 -33
  209. package/dist/src/Owner.d.ts.map +0 -1
  210. package/dist/src/Owner.js +0 -26
  211. package/dist/src/Platform.d.ts +0 -37
  212. package/dist/src/Platform.d.ts.map +0 -1
  213. package/dist/src/Platform.js +0 -11
  214. package/dist/src/Protobuf.d.ts +0 -81
  215. package/dist/src/Protobuf.d.ts.map +0 -1
  216. package/dist/src/Protobuf.js +0 -92
  217. package/dist/src/Public.d.ts +0 -13
  218. package/dist/src/Public.d.ts.map +0 -1
  219. package/dist/src/Public.js +0 -6
  220. package/dist/src/Socket.d.ts +0 -8
  221. package/dist/src/Socket.d.ts.map +0 -1
  222. package/dist/src/Socket.js +0 -51
  223. package/dist/src/Sql.d.ts +0 -12
  224. package/dist/src/Sql.d.ts.map +0 -1
  225. package/dist/src/Sql.js +0 -30
  226. package/dist/src/Sync.d.ts +0 -70
  227. package/dist/src/Sync.d.ts.map +0 -1
  228. package/dist/src/Sync.js +0 -127
  229. package/src/Config.ts +0 -119
  230. package/src/Crdt.ts +0 -361
  231. package/src/Db.ts +0 -955
  232. package/src/Diff.ts +0 -114
  233. package/src/Evolu.ts +0 -1016
  234. package/src/Model.ts +0 -233
  235. package/src/Murmurhash.ts +0 -70
  236. package/src/Owner.ts +0 -69
  237. package/src/Platform.ts +0 -47
  238. package/src/Protobuf.ts +0 -155
  239. package/src/Public.ts +0 -12
  240. package/src/Socket.ts +0 -83
  241. package/src/Sql.ts +0 -41
  242. package/src/Sync.ts +0 -315
@@ -1,33 +0,0 @@
1
- import * as Brand from "effect/Brand";
2
- import * as Context from "effect/Context";
3
- import * as Effect from "effect/Effect";
4
- import { Mnemonic } from "./Crypto.js";
5
- import { Id } from "./Model.js";
6
- /**
7
- * The Owner represents the Evolu database owner, a user. Instead of traditional
8
- * email with a password, the Owner uses a mnemonic, also known as a "seed
9
- * phrase," which is a set of 12 words in a specific order chosen from a
10
- * predefined list.
11
- *
12
- * The purpose of the BIP39 mnemonic is to provide a human-readable way of
13
- * storing a private key.
14
- *
15
- * Mnemonic is generated safely in the user's device and must not be shared with
16
- * anyone.
17
- */
18
- export interface Owner {
19
- /** The {@link Mnemonic} associated with {@link Owner}. */
20
- readonly mnemonic: Mnemonic;
21
- /** The unique identifier safely derived from {@link Mnemonic}. */
22
- readonly id: OwnerId;
23
- /** The encryption key safely derived from {@link Mnemonic}. */
24
- readonly encryptionKey: Uint8Array;
25
- }
26
- export declare const Owner: Context.Tag<Owner, Owner>;
27
- /**
28
- * The unique identifier of {@link Owner} safely derived from its
29
- * {@link Mnemonic}.
30
- */
31
- export type OwnerId = Id & Brand.Brand<"Owner">;
32
- export declare const makeOwner: (mnemonic?: Mnemonic) => Effect.Effect<Owner>;
33
- //# sourceMappingURL=Owner.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Owner.d.ts","sourceRoot":"","sources":["../../src/Owner.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,EACL,QAAQ,EAIT,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,EAAE,EAAE,MAAM,YAAY,CAAC;AAEhC;;;;;;;;;;;GAWG;AACH,MAAM,WAAW,KAAK;IACpB,0DAA0D;IAC1D,QAAQ,CAAC,QAAQ,EAAE,QAAQ,CAAC;IAE5B,kEAAkE;IAClE,QAAQ,CAAC,EAAE,EAAE,OAAO,CAAC;IAErB,+DAA+D;IAC/D,QAAQ,CAAC,aAAa,EAAE,UAAU,CAAC;CACpC;AAED,eAAO,MAAM,KAAK,2BAAqC,CAAC;AAExD;;;GAGG;AACH,MAAM,MAAM,OAAO,GAAG,EAAE,GAAG,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;AAEhD,eAAO,MAAM,SAAS,cAAe,QAAQ,KAAG,MAAM,CAAC,MAAM,CAAC,KAAK,CAoB/D,CAAC"}
package/dist/src/Owner.js DELETED
@@ -1,26 +0,0 @@
1
- import * as Context from "effect/Context";
2
- import * as Effect from "effect/Effect";
3
- import { createMnemonic, mnemonicToSeed, slip21Derive, } from "./Crypto.js";
4
- export const Owner = Context.GenericTag("Owner");
5
- export const makeOwner = (mnemonic) => Effect.gen(function* (_) {
6
- if (mnemonic == null)
7
- mnemonic = createMnemonic();
8
- const seed = mnemonicToSeed(mnemonic);
9
- const id = yield* Effect.map(slip21Derive(seed, ["Evolu", "Owner Id"]), (key) => {
10
- // convert key to nanoid
11
- let id = "";
12
- for (let i = 0; i < 21; i++) {
13
- id += urlAlphabet[key[i] & 63];
14
- }
15
- return id;
16
- });
17
- const encryptionKey = yield* slip21Derive(seed, [
18
- "Evolu",
19
- "Encryption Key",
20
- ]);
21
- return { mnemonic, id, encryptionKey };
22
- });
23
- // From https://github.com/ai/nanoid/blob/main/url-alphabet/index.js
24
- // It's copy-pasted for now because NanoId import breaks Metro bundler.
25
- // https://github.com/ai/nanoid/issues/468
26
- const urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
@@ -1,37 +0,0 @@
1
- import * as Context from "effect/Context";
2
- import * as Effect from "effect/Effect";
3
- import * as Scope from "effect/Scope";
4
- import { Config } from "./Config.js";
5
- /**
6
- * FlushSync is a service for libraries like React to synchronously flush
7
- * updates inside the provided callback to ensure the DOM is updated
8
- * immediately.
9
- *
10
- * https://react.dev/reference/react-dom/flushSync
11
- */
12
- export type FlushSync = (callback: () => void) => void;
13
- export declare const FlushSync: Context.Tag<FlushSync, FlushSync>;
14
- declare const AppState_base: Context.TagClass<AppState, "AppState", {
15
- readonly init: (options: {
16
- readonly reloadUrl: string;
17
- readonly onRequestSync: () => void;
18
- }) => Effect.Effect<AppStateReset>;
19
- }>;
20
- export declare class AppState extends AppState_base {
21
- }
22
- interface AppStateReset {
23
- readonly reset: Effect.Effect<void>;
24
- }
25
- declare const SyncLock_base: Context.TagClass<SyncLock, "SyncLock", {
26
- readonly tryAcquire: Effect.Effect<SyncLockRelease, SyncLockAlreadySyncingError, Config | Scope.Scope>;
27
- }>;
28
- export declare class SyncLock extends SyncLock_base {
29
- }
30
- export interface SyncLockRelease {
31
- readonly release: Effect.Effect<void>;
32
- }
33
- export declare class SyncLockAlreadySyncingError {
34
- readonly _tag = "SyncLockAlreadySyncingError";
35
- }
36
- export {};
37
- //# sourceMappingURL=Platform.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Platform.d.ts","sourceRoot":"","sources":["../../src/Platform.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,gBAAgB,CAAC;AAC1C,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC;;;;;;GAMG;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAI,CAAC;AACvD,eAAO,MAAM,SAAS,mCAA6C,CAAC;;mBAKjD,CAAC,OAAO,EAAE;QACvB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;QAC3B,QAAQ,CAAC,aAAa,EAAE,MAAM,IAAI,CAAC;KACpC,KAAK,MAAM,CAAC,MAAM,CAAC,aAAa,CAAC;;AANtC,qBAAa,QAAS,SAAQ,aAQ3B;CAAG;AAEN,UAAU,aAAa;IACrB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;CACrC;;yBAKwB,MAAM,CAAC,MAAM,CAChC,eAAe,EACf,2BAA2B,EAC3B,MAAM,GAAG,KAAK,CAAC,KAAK,CACrB;;AAPL,qBAAa,QAAS,SAAQ,aAS3B;CAAG;AAEN,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;CACvC;AAED,qBAAa,2BAA2B;IACtC,QAAQ,CAAC,IAAI,iCAAiC;CAC/C"}
@@ -1,11 +0,0 @@
1
- import * as Context from "effect/Context";
2
- export const FlushSync = Context.GenericTag("FlushSync");
3
- export class AppState extends Context.Tag("AppState")() {
4
- }
5
- export class SyncLock extends Context.Tag("SyncLock")() {
6
- }
7
- export class SyncLockAlreadySyncingError {
8
- constructor() {
9
- this._tag = "SyncLockAlreadySyncingError";
10
- }
11
- }
@@ -1,81 +0,0 @@
1
- import { MessageType } from "@protobuf-ts/runtime";
2
- import { MerkleTreeString, TimestampString } from "./Crdt.js";
3
- import { NodeId } from "./Crypto.js";
4
- import { OwnerId } from "./Owner.js";
5
- import { Id } from "./Model.js";
6
- /** @generated from protobuf message SyncRequest */
7
- export interface SyncRequest {
8
- /** @generated from protobuf field: repeated EncryptedMessage messages = 1; */
9
- messages: ReadonlyArray<EncryptedMessage>;
10
- /** @generated from protobuf field: string userId = 2; */
11
- userId: OwnerId;
12
- /** @generated from protobuf field: string nodeId = 3; */
13
- nodeId: NodeId;
14
- /** @generated from protobuf field: string merkleTree = 4; */
15
- merkleTree: MerkleTreeString;
16
- }
17
- /** @generated from protobuf message SyncResponse */
18
- export interface SyncResponse {
19
- /** @generated from protobuf field: repeated EncryptedMessage messages = 1; */
20
- messages: ReadonlyArray<EncryptedMessage>;
21
- /** @generated from protobuf field: string merkleTree = 2; */
22
- merkleTree: MerkleTreeString;
23
- }
24
- /** @generated from protobuf message EncryptedMessage */
25
- export interface EncryptedMessage {
26
- /** @generated from protobuf field: string timestamp = 1; */
27
- timestamp: TimestampString;
28
- /** @generated from protobuf field: bytes content = 2; */
29
- content: Uint8Array;
30
- }
31
- /** @generated from protobuf message MessageContent */
32
- export interface MessageContent {
33
- /** @generated from protobuf field: string table = 1; */
34
- table: string;
35
- /** @generated from protobuf field: string row = 2; */
36
- row: Id;
37
- /** @generated from protobuf field: string column = 3; */
38
- column: string;
39
- /** @generated from protobuf oneof: value */
40
- value: {
41
- oneofKind: "stringValue";
42
- /** @generated from protobuf field: string stringValue = 4; */
43
- stringValue: string;
44
- } | {
45
- oneofKind: "numberValue";
46
- /** @generated from protobuf field: string numberValue = 5; */
47
- numberValue: string;
48
- } | {
49
- oneofKind: "bytesValue";
50
- /** @generated from protobuf field: bytes bytesValue = 6; */
51
- bytesValue: Uint8Array;
52
- } | {
53
- oneofKind: "jsonValue";
54
- /** @generated from protobuf field: string jsonValue = 7; */
55
- jsonValue: string;
56
- } | {
57
- oneofKind: undefined;
58
- };
59
- }
60
- declare class SyncRequest$Type extends MessageType<SyncRequest> {
61
- constructor();
62
- }
63
- /** @generated MessageType for protobuf message SyncRequest */
64
- export declare const SyncRequest: SyncRequest$Type;
65
- declare class SyncResponse$Type extends MessageType<SyncResponse> {
66
- constructor();
67
- }
68
- /** @generated MessageType for protobuf message SyncResponse */
69
- export declare const SyncResponse: SyncResponse$Type;
70
- declare class EncryptedMessage$Type extends MessageType<EncryptedMessage> {
71
- constructor();
72
- }
73
- /** @generated MessageType for protobuf message EncryptedMessage */
74
- export declare const EncryptedMessage: EncryptedMessage$Type;
75
- declare class MessageContent$Type extends MessageType<MessageContent> {
76
- constructor();
77
- }
78
- /** @generated MessageType for protobuf message MessageContent */
79
- export declare const MessageContent: MessageContent$Type;
80
- export {};
81
- //# sourceMappingURL=Protobuf.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Protobuf.d.ts","sourceRoot":"","sources":["../../src/Protobuf.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,WAAW,CAAC;AAC9D,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACrC,OAAO,EAAE,EAAE,EAAE,MAAM,YAAY,CAAC;AAChC,mDAAmD;AACnD,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AElD,cAAM,iBAAkB,SAAQ,WAAW,CAAC,YAAY,CAAC;;CAaxD;AACD,+DAA+D;AAC/D,eAAO,MAAM,YAAY,mBAA0B,CAAC;AAEpD,cAAM,qBAAsB,SAAQ,WAAW,CAAC,gBAAgB,CAAC;;CAOhE;AACD,mEAAmE;AACnE,eAAO,MAAM,gBAAgB,uBAA8B,CAAC;AAE5D,cAAM,mBAAoB,SAAQ,WAAW,CAAC,cAAc,CAAC;;CAoC5D;AACD,iEAAiE;AACjE,eAAO,MAAM,cAAc,qBAA4B,CAAC"}
@@ -1,92 +0,0 @@
1
- /* eslint-disable */
2
- // @generated by protobuf-ts 2.9.3 with parameter eslint_disable,optimize_code_size
3
- // @generated from protobuf file "Protobuf.proto" (syntax proto3)
4
- // tslint:disable
5
- import { MessageType } from "@protobuf-ts/runtime";
6
- // @generated message type with reflection information, may provide speed optimized methods
7
- class SyncRequest$Type extends MessageType {
8
- constructor() {
9
- super("SyncRequest", [
10
- {
11
- no: 1,
12
- name: "messages",
13
- kind: "message",
14
- repeat: 1 /*RepeatType.PACKED*/,
15
- T: () => EncryptedMessage,
16
- },
17
- { no: 2, name: "userId", kind: "scalar", T: 9 /*ScalarType.STRING*/ },
18
- { no: 3, name: "nodeId", kind: "scalar", T: 9 /*ScalarType.STRING*/ },
19
- { no: 4, name: "merkleTree", kind: "scalar", T: 9 /*ScalarType.STRING*/ },
20
- ]);
21
- }
22
- }
23
- /** @generated MessageType for protobuf message SyncRequest */
24
- export const SyncRequest = new SyncRequest$Type();
25
- // @generated message type with reflection information, may provide speed optimized methods
26
- class SyncResponse$Type extends MessageType {
27
- constructor() {
28
- super("SyncResponse", [
29
- {
30
- no: 1,
31
- name: "messages",
32
- kind: "message",
33
- repeat: 1 /*RepeatType.PACKED*/,
34
- T: () => EncryptedMessage,
35
- },
36
- { no: 2, name: "merkleTree", kind: "scalar", T: 9 /*ScalarType.STRING*/ },
37
- ]);
38
- }
39
- }
40
- /** @generated MessageType for protobuf message SyncResponse */
41
- export const SyncResponse = new SyncResponse$Type();
42
- // @generated message type with reflection information, may provide speed optimized methods
43
- class EncryptedMessage$Type extends MessageType {
44
- constructor() {
45
- super("EncryptedMessage", [
46
- { no: 1, name: "timestamp", kind: "scalar", T: 9 /*ScalarType.STRING*/ },
47
- { no: 2, name: "content", kind: "scalar", T: 12 /*ScalarType.BYTES*/ },
48
- ]);
49
- }
50
- }
51
- /** @generated MessageType for protobuf message EncryptedMessage */
52
- export const EncryptedMessage = new EncryptedMessage$Type();
53
- // @generated message type with reflection information, may provide speed optimized methods
54
- class MessageContent$Type extends MessageType {
55
- constructor() {
56
- super("MessageContent", [
57
- { no: 1, name: "table", kind: "scalar", T: 9 /*ScalarType.STRING*/ },
58
- { no: 2, name: "row", kind: "scalar", T: 9 /*ScalarType.STRING*/ },
59
- { no: 3, name: "column", kind: "scalar", T: 9 /*ScalarType.STRING*/ },
60
- {
61
- no: 4,
62
- name: "stringValue",
63
- kind: "scalar",
64
- oneof: "value",
65
- T: 9 /*ScalarType.STRING*/,
66
- },
67
- {
68
- no: 5,
69
- name: "numberValue",
70
- kind: "scalar",
71
- oneof: "value",
72
- T: 9 /*ScalarType.STRING*/,
73
- },
74
- {
75
- no: 6,
76
- name: "bytesValue",
77
- kind: "scalar",
78
- oneof: "value",
79
- T: 12 /*ScalarType.BYTES*/,
80
- },
81
- {
82
- no: 7,
83
- name: "jsonValue",
84
- kind: "scalar",
85
- oneof: "value",
86
- T: 9 /*ScalarType.STRING*/,
87
- },
88
- ]);
89
- }
90
- }
91
- /** @generated MessageType for protobuf message MessageContent */
92
- export const MessageContent = new MessageContent$Type();
@@ -1,13 +0,0 @@
1
- export { sql } from "kysely";
2
- export type { NotNull } from "kysely";
3
- export { jsonArrayFrom, jsonObjectFrom } from "kysely/helpers/sqlite";
4
- export type { Timestamp, TimestampError } from "./Crdt.js";
5
- export { createMnemonic, parseMnemonic } from "./Crypto.js";
6
- export type { InvalidMnemonicError, Mnemonic } from "./Crypto.js";
7
- export type { ExtractRow, QueryResult } from "./Db.js";
8
- export * from "./Error.js";
9
- export { createIndexes } from "./Evolu.js";
10
- export * from "./Model.js";
11
- export type { Owner, OwnerId } from "./Owner.js";
12
- export type { SyncState } from "./Sync.js";
13
- //# sourceMappingURL=Public.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Public.d.ts","sourceRoot":"","sources":["../../src/Public.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,QAAQ,CAAC;AAC7B,YAAY,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACtE,YAAY,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5D,YAAY,EAAE,oBAAoB,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAClE,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACvD,cAAc,YAAY,CAAC;AAC3B,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC3C,cAAc,YAAY,CAAC;AAC3B,YAAY,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AACjD,YAAY,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC"}
@@ -1,6 +0,0 @@
1
- export { sql } from "kysely";
2
- export { jsonArrayFrom, jsonObjectFrom } from "kysely/helpers/sqlite";
3
- export { createMnemonic, parseMnemonic } from "./Crypto.js";
4
- export * from "./Error.js";
5
- export { createIndexes } from "./Evolu.js";
6
- export * from "./Model.js";
@@ -1,8 +0,0 @@
1
- import { Config } from "./Config.js";
2
- import { Owner } from "./Owner.js";
3
- type ForceSyncMethod = ({ refreshQueries, }: {
4
- refreshQueries: boolean;
5
- }) => () => void;
6
- export declare const createSocket: (sync: ForceSyncMethod, config: Config, owner: Owner) => WebSocket | undefined;
7
- export {};
8
- //# sourceMappingURL=Socket.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Socket.d.ts","sourceRoot":"","sources":["../../src/Socket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAWnC,KAAK,eAAe,GAAG,CAAC,EACtB,cAAc,GACf,EAAE;IACD,cAAc,EAAE,OAAO,CAAC;CACzB,KAAK,MAAM,IAAI,CAAC;AAoBjB,eAAO,MAAM,YAAY,SACjB,eAAe,UACb,MAAM,SACP,KAAK,KACX,SAAS,GAAG,SA0Cd,CAAC"}
@@ -1,51 +0,0 @@
1
- let syncTimeout;
2
- let lockTimeout;
3
- let socketTimeout;
4
- let isLocked = false;
5
- const SocketTimeout = 3000;
6
- const SyncTimeout = 500;
7
- const SyncLockTimeout = 500;
8
- let socket;
9
- const processWebsocketEvent = (sync, event) => {
10
- if (typeof event.data === "string" || isLocked === true)
11
- return;
12
- clearTimeout(syncTimeout);
13
- syncTimeout = undefined;
14
- syncTimeout = setTimeout(() => {
15
- isLocked = true;
16
- sync({ refreshQueries: true })();
17
- clearTimeout(syncTimeout);
18
- clearTimeout(lockTimeout);
19
- lockTimeout = setTimeout(() => {
20
- isLocked = false;
21
- }, SyncLockTimeout);
22
- }, SyncTimeout);
23
- };
24
- export const createSocket = (sync, config, owner) => {
25
- if (config.externalWebsocketConnection) {
26
- // Hook up to an external WebSocket connection
27
- config.externalWebsocketConnection.send(JSON.stringify({ message: "subscribe", channelId: owner.id }));
28
- config.externalWebsocketConnection.addEventListener("message", (event) => processWebsocketEvent(sync, event));
29
- return config.externalWebsocketConnection;
30
- }
31
- if (config.enableWebsocketConnection) {
32
- // Create a WebSocket connection
33
- socket = new WebSocket(config.syncUrl?.replace("http", "ws"));
34
- // Handle incoming WebSocket messages
35
- socket.addEventListener("message", (event) => processWebsocketEvent(sync, event));
36
- // Handle WebSocket errors
37
- socket.onerror = (error) => {
38
- // eslint-disable-next-line no-console
39
- console.error("WebSocket error:", error);
40
- };
41
- // Handle WebSocket closure
42
- socket.onclose = () => {
43
- socketTimeout = setTimeout(() => createSocket(sync, config, owner), SocketTimeout);
44
- };
45
- socket.onopen = () => {
46
- socket.send(JSON.stringify({ message: "subscribe", channelId: owner.id }));
47
- clearTimeout(socketTimeout);
48
- };
49
- return socket;
50
- }
51
- };
package/dist/src/Sql.d.ts DELETED
@@ -1,12 +0,0 @@
1
- import { SqliteQuery } from "./Sqlite.js";
2
- export declare const selectOwner: SqliteQuery;
3
- export declare const createMessageTable: SqliteQuery;
4
- export declare const createMessageTableIndex: SqliteQuery;
5
- export declare const createOwnerTable: SqliteQuery;
6
- export declare const insertOwner: SqliteQuery;
7
- export declare const selectOwnerTimestampAndMerkleTree: SqliteQuery;
8
- export declare const selectLastTimestampForTableRowColumn: SqliteQuery;
9
- export declare const insertIntoMessagesIfNew: SqliteQuery;
10
- export declare const updateOwnerTimestampAndMerkleTree: SqliteQuery;
11
- export declare const selectMessagesToSync: SqliteQuery;
12
- //# sourceMappingURL=Sql.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Sql.d.ts","sourceRoot":"","sources":["../../src/Sql.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,eAAO,MAAM,WAAW,EAAE,WAEzB,CAAC;AAEF,eAAO,MAAM,kBAAkB,EAAE,WAEhC,CAAC;AAEF,eAAO,MAAM,uBAAuB,EAAE,WAErC,CAAC;AAEF,eAAO,MAAM,gBAAgB,EAAE,WAE9B,CAAC;AAEF,eAAO,MAAM,WAAW,EAAE,WAEzB,CAAC;AAEF,eAAO,MAAM,iCAAiC,EAAE,WAE/C,CAAC;AAEF,eAAO,MAAM,oCAAoC,EAAE,WAElD,CAAC;AAEF,eAAO,MAAM,uBAAuB,EAAE,WAErC,CAAC;AAEF,eAAO,MAAM,iCAAiC,EAAE,WAE/C,CAAC;AAEF,eAAO,MAAM,oBAAoB,EAAE,WAElC,CAAC"}
package/dist/src/Sql.js DELETED
@@ -1,30 +0,0 @@
1
- export const selectOwner = {
2
- sql: `select "id", "mnemonic", "encryptionKey" from "evolu_owner"`,
3
- };
4
- export const createMessageTable = {
5
- sql: `create table "evolu_message" ("timestamp" blob primary key, "table" blob, "row" blob, "column" blob, "value" blob)`,
6
- };
7
- export const createMessageTableIndex = {
8
- sql: `create index "index_evolu_message" on "evolu_message" ("table", "row", "column", "timestamp" desc)`,
9
- };
10
- export const createOwnerTable = {
11
- sql: `create table "evolu_owner" ("id" blob, "mnemonic" blob, "encryptionKey" blob, "timestamp" blob, "merkleTree" blob)`,
12
- };
13
- export const insertOwner = {
14
- sql: `insert into "evolu_owner" ("id", "mnemonic", "encryptionKey", "timestamp", "merkleTree") values (?, ?, ?, ?, ?)`,
15
- };
16
- export const selectOwnerTimestampAndMerkleTree = {
17
- sql: `select "timestamp", "merkleTree" from "evolu_owner"`,
18
- };
19
- export const selectLastTimestampForTableRowColumn = {
20
- sql: `select "timestamp" from "evolu_message" where "table" = ? and "row" = ? and "column" = ? order by "timestamp" desc limit ?`,
21
- };
22
- export const insertIntoMessagesIfNew = {
23
- sql: `insert into "evolu_message" ("timestamp", "table", "row", "column", "value") values (?, ?, ?, ?, ?) on conflict do nothing`,
24
- };
25
- export const updateOwnerTimestampAndMerkleTree = {
26
- sql: `update "evolu_owner" set "merkleTree" = ?, "timestamp" = ?`,
27
- };
28
- export const selectMessagesToSync = {
29
- sql: `select * from "evolu_message" where "timestamp" >= ? order by "timestamp"`,
30
- };
@@ -1,70 +0,0 @@
1
- import * as Context from "effect/Context";
2
- import * as Effect from "effect/Effect";
3
- import { Config } from "./Config.js";
4
- import { MerkleTree, Millis, Timestamp, TimestampString } from "./Crdt.js";
5
- import { SecretBox } from "./Crypto.js";
6
- import { Id } from "./Model.js";
7
- import { Owner } from "./Owner.js";
8
- import { Value } from "./Sqlite.js";
9
- export interface Sync {
10
- readonly init: (owner: Owner) => Effect.Effect<void>;
11
- readonly sync: (syncData: SyncData) => Effect.Effect<SyncResult, SyncStateIsNotSynced, Config>;
12
- }
13
- export declare const Sync: Context.Tag<Sync, Sync>;
14
- export interface SyncData {
15
- merkleTree: MerkleTree;
16
- timestamp: Timestamp;
17
- messages?: ReadonlyArray<Message>;
18
- }
19
- export interface Message extends NewMessage {
20
- readonly timestamp: TimestampString;
21
- }
22
- export interface NewMessage {
23
- readonly table: string;
24
- readonly row: Id;
25
- readonly column: string;
26
- readonly value: Value;
27
- }
28
- export interface SyncResult {
29
- readonly messages: ReadonlyArray<Message>;
30
- readonly merkleTree: MerkleTree;
31
- }
32
- /**
33
- * The SyncState type represents the various states that a synchronization
34
- * process can be in.
35
- */
36
- export type SyncState = SyncStateInitial | SyncStateIsSyncing | SyncStateIsSynced | SyncStateIsNotSynced;
37
- /** On app start, we need to find out whether the state is synced. */
38
- export interface SyncStateInitial {
39
- readonly _tag: "SyncStateInitial";
40
- }
41
- export declare const initialSyncState: SyncStateInitial;
42
- export interface SyncStateIsSyncing {
43
- readonly _tag: "SyncStateIsSyncing";
44
- }
45
- export interface SyncStateIsSynced {
46
- readonly _tag: "SyncStateIsSynced";
47
- readonly time: Millis;
48
- }
49
- export interface SyncStateIsNotSynced {
50
- readonly _tag: "SyncStateIsNotSynced";
51
- readonly error: NetworkError | ServerError | PaymentRequiredError;
52
- }
53
- export interface NetworkError {
54
- readonly _tag: "NetworkError";
55
- }
56
- export interface ServerError {
57
- readonly _tag: "ServerError";
58
- readonly status: number;
59
- }
60
- export interface PaymentRequiredError {
61
- readonly _tag: "PaymentRequiredError";
62
- }
63
- declare const SyncFactory_base: Context.TagClass<SyncFactory, "SyncFactory", {
64
- readonly createSync: Effect.Effect<Sync, never, never>;
65
- }>;
66
- export declare class SyncFactory extends SyncFactory_base {
67
- }
68
- export declare const createSync: Effect.Effect<Sync, never, SecretBox>;
69
- export {};
70
- //# sourceMappingURL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2
- import * as S from "@effect/schema/Schema";
3
- import { concatBytes } from "@noble/ciphers/utils";
4
- import { BinaryReader, BinaryWriter } from "@protobuf-ts/runtime";
5
- import * as Arr from "effect/Array";
6
- import * as Context from "effect/Context";
7
- import * as Deferred from "effect/Deferred";
8
- import * as Effect from "effect/Effect";
9
- import { absurd } from "effect/Function";
10
- import * as Option from "effect/Option";
11
- import * as Predicate from "effect/Predicate";
12
- import { Config } from "./Config.js";
13
- import { merkleTreeToString, unsafeMerkleTreeFromString, } from "./Crdt.js";
14
- import { SecretBox } from "./Crypto.js";
15
- import { Owner } from "./Owner.js";
16
- import * as Protobuf from "./Protobuf.js";
17
- export const Sync = Context.GenericTag("Sync");
18
- // To preserve identity.
19
- export const initialSyncState = { _tag: "SyncStateInitial" };
20
- export class SyncFactory extends Context.Tag("SyncFactory")() {
21
- }
22
- export const createSync = Effect.gen(function* () {
23
- const initContext = Context.empty().pipe(Context.add(SecretBox, yield* SecretBox));
24
- const afterInitContext = yield* Deferred.make();
25
- return Sync.of({
26
- init: (owner) => Effect.logDebug(["Sync init", { owner }]).pipe(Effect.tap(Deferred.succeed(afterInitContext, initContext.pipe(Context.add(Owner, owner))))),
27
- sync: ({ merkleTree, timestamp, messages }) => Effect.gen(function* () {
28
- yield* Effect.logDebug([
29
- "Sync request",
30
- { merkleTree, timestamp, messages },
31
- ]);
32
- const secretBox = yield* SecretBox;
33
- const owner = yield* Owner;
34
- const config = yield* Config;
35
- return yield* Effect.forEach(messages || [], ({ timestamp, ...newMessage }) => Effect.map(secretBox.seal(owner.encryptionKey, newMessageToBinary(newMessage)), (content) => ({ timestamp, content }))).pipe(Effect.map((encrypedMessages) => Protobuf.SyncRequest.toBinary({
36
- messages: encrypedMessages,
37
- userId: owner.id,
38
- nodeId: timestamp.node,
39
- merkleTree: merkleTreeToString(merkleTree),
40
- }, binaryWriteOptions)), Effect.flatMap((body) => Effect.gen(function* () {
41
- const client = (yield* Platform.HttpClient.HttpClient).pipe(Platform.HttpClient.filterStatusOk);
42
- return yield* Platform.HttpClientRequest.post(config.syncUrl).pipe(Platform.HttpClientRequest.bodyUint8Array(body, "application/x-protobuf"), client.execute, Effect.flatMap((response) => response.arrayBuffer), Effect.scoped);
43
- }).pipe(Effect.provide(Platform.FetchHttpClient.layer))), Effect.map((buffer) => Protobuf.SyncResponse.fromBinary(new Uint8Array(buffer), binaryReadOptions)), Effect.flatMap((syncResponse) => Effect.forEach(syncResponse.messages, (encrypedMessage) => Effect.map(secretBox.open(owner.encryptionKey, encrypedMessage.content), (binary) => [binary, encrypedMessage.timestamp])).pipe(Effect.map(Arr.filterMap(([binary, timestamp]) => Option.map(newMessageFromBinary(binary), (newMessage) => ({ ...newMessage, timestamp })))), Effect.map((messages) => ({
44
- messages,
45
- merkleTree: unsafeMerkleTreeFromString(syncResponse.merkleTree),
46
- })), Effect.tap((response) => Effect.logDebug(["Sync response", response])))), Effect.catchTag("RequestError", () => Effect.fail({
47
- _tag: "SyncStateIsNotSynced",
48
- error: { _tag: "NetworkError" },
49
- })), Effect.catchTag("ResponseError", ({ response: { status } }) => {
50
- switch (status) {
51
- case 402:
52
- return Effect.fail({
53
- _tag: "SyncStateIsNotSynced",
54
- error: { _tag: "PaymentRequiredError" },
55
- });
56
- default:
57
- return Effect.fail({
58
- _tag: "SyncStateIsNotSynced",
59
- error: { _tag: "ServerError", status },
60
- });
61
- }
62
- }));
63
- }).pipe((effect) => Effect.flatMap(Deferred.await(afterInitContext), (context) => Effect.provide(effect, context))),
64
- });
65
- });
66
- const newMessageToBinary = ({ value, ...rest }) => concatBytes(version1, Protobuf.MessageContent.toBinary({ value: valueToProtobuf(value), ...rest }, binaryWriteOptions));
67
- const version1 = new Uint8Array([0, 1]);
68
- const valueToProtobuf = (value) => {
69
- switch (typeof value) {
70
- case "string":
71
- return { oneofKind: "stringValue", stringValue: value };
72
- case "number":
73
- return {
74
- oneofKind: "numberValue",
75
- numberValue: S.encodeSync(S.NumberFromString)(value),
76
- };
77
- }
78
- if (value == null)
79
- return { oneofKind: undefined };
80
- if (Predicate.isUint8Array(value))
81
- return { oneofKind: "bytesValue", bytesValue: value };
82
- return { oneofKind: "jsonValue", jsonValue: JSON.stringify(value) };
83
- };
84
- // The 'protobuf-ts' uses TextEncoder, but polyfill fast-text-encoding
85
- // doesn't support the fatal option.
86
- // https://github.com/timostamm/protobuf-ts/issues/184#issuecomment-1658443836
87
- const binaryWriteOptions = {
88
- writerFactory: () => new BinaryWriter({
89
- encode: (input) => new TextEncoder().encode(input),
90
- }),
91
- };
92
- const binaryReadOptions = {
93
- readerFactory: (bytes) => new BinaryReader(bytes, {
94
- decode: (input) => new TextDecoder().decode(input),
95
- }),
96
- };
97
- const newMessageFromBinary = (binary) => {
98
- if (!startsWithArray(binary, version1))
99
- return Option.none();
100
- const { value, ...content } = Protobuf.MessageContent.fromBinary(binary.slice(version1.length), binaryReadOptions);
101
- return Option.some({ value: valueFromProtobuf(value), ...content });
102
- };
103
- const startsWithArray = (array, prefix) => {
104
- if (prefix.length > array.length)
105
- return false;
106
- for (let i = 0; i < prefix.length; i++) {
107
- if (array[i] !== prefix[i])
108
- return false;
109
- }
110
- return true;
111
- };
112
- const valueFromProtobuf = (value) => {
113
- switch (value.oneofKind) {
114
- case "numberValue":
115
- return S.decodeSync(S.NumberFromString)(value.numberValue);
116
- case "stringValue":
117
- return value.stringValue;
118
- case "bytesValue":
119
- return value.bytesValue;
120
- case "jsonValue":
121
- return JSON.parse(value.jsonValue);
122
- case undefined:
123
- return null;
124
- default:
125
- return absurd(value);
126
- }
127
- };