@evolu/common 4.1.1 → 5.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 (78) hide show
  1. package/README.md +1 -1
  2. package/dist/src/Config.d.ts +27 -30
  3. package/dist/src/Config.d.ts.map +1 -1
  4. package/dist/src/Config.js +32 -5
  5. package/dist/src/Crdt.d.ts +5 -4
  6. package/dist/src/Crdt.d.ts.map +1 -1
  7. package/dist/src/Crdt.js +27 -24
  8. package/dist/src/Crypto.d.ts +16 -10
  9. package/dist/src/Crypto.d.ts.map +1 -1
  10. package/dist/src/Crypto.js +24 -11
  11. package/dist/src/Db.d.ts +58 -78
  12. package/dist/src/Db.d.ts.map +1 -1
  13. package/dist/src/Db.js +368 -170
  14. package/dist/src/Diff.d.ts +9 -3
  15. package/dist/src/Diff.d.ts.map +1 -1
  16. package/dist/src/Diff.js +12 -11
  17. package/dist/src/Error.d.ts +15 -0
  18. package/dist/src/Error.d.ts.map +1 -0
  19. package/dist/src/Error.js +14 -0
  20. package/dist/src/Evolu.d.ts +139 -91
  21. package/dist/src/Evolu.d.ts.map +1 -1
  22. package/dist/src/Evolu.js +267 -239
  23. package/dist/src/Model.d.ts +52 -0
  24. package/dist/src/Model.d.ts.map +1 -1
  25. package/dist/src/Model.js +47 -5
  26. package/dist/src/Owner.d.ts.map +1 -1
  27. package/dist/src/Owner.js +14 -8
  28. package/dist/src/Platform.d.ts +24 -34
  29. package/dist/src/Platform.d.ts.map +1 -1
  30. package/dist/src/Platform.js +9 -31
  31. package/dist/src/Public.d.ts +3 -5
  32. package/dist/src/Public.d.ts.map +1 -1
  33. package/dist/src/Public.js +2 -3
  34. package/dist/src/Sqlite.d.ts +25 -16
  35. package/dist/src/Sqlite.d.ts.map +1 -1
  36. package/dist/src/Sqlite.js +28 -20
  37. package/dist/src/Store.d.ts.map +1 -1
  38. package/dist/src/Sync.d.ts +70 -0
  39. package/dist/src/Sync.d.ts.map +1 -0
  40. package/dist/src/Sync.js +126 -0
  41. package/dist/src/index.d.ts +2 -5
  42. package/dist/src/index.d.ts.map +1 -1
  43. package/dist/src/index.js +2 -5
  44. package/package.json +12 -10
  45. package/src/Config.ts +77 -36
  46. package/src/Crdt.ts +61 -76
  47. package/src/Crypto.ts +83 -60
  48. package/src/Db.ts +782 -352
  49. package/src/Diff.ts +23 -21
  50. package/src/Error.ts +29 -0
  51. package/src/Evolu.ts +599 -499
  52. package/src/Model.ts +102 -6
  53. package/src/Owner.ts +24 -24
  54. package/src/Platform.ts +33 -81
  55. package/src/Public.ts +3 -5
  56. package/src/Sqlite.ts +81 -44
  57. package/src/Sync.ts +306 -0
  58. package/src/index.ts +2 -5
  59. package/dist/src/DbWorker.d.ts +0 -82
  60. package/dist/src/DbWorker.d.ts.map +0 -1
  61. package/dist/src/DbWorker.js +0 -335
  62. package/dist/src/ErrorStore.d.ts +0 -29
  63. package/dist/src/ErrorStore.d.ts.map +0 -1
  64. package/dist/src/ErrorStore.js +0 -13
  65. package/dist/src/OnCompletes.d.ts +0 -14
  66. package/dist/src/OnCompletes.d.ts.map +0 -1
  67. package/dist/src/OnCompletes.js +0 -25
  68. package/dist/src/SyncWorker.d.ts +0 -80
  69. package/dist/src/SyncWorker.d.ts.map +0 -1
  70. package/dist/src/SyncWorker.js +0 -150
  71. package/dist/src/Types.d.ts +0 -13
  72. package/dist/src/Types.d.ts.map +0 -1
  73. package/dist/src/Types.js +0 -1
  74. package/src/DbWorker.ts +0 -721
  75. package/src/ErrorStore.ts +0 -46
  76. package/src/OnCompletes.ts +0 -51
  77. package/src/SyncWorker.ts +0 -361
  78. package/src/Types.ts +0 -11
package/src/Crypto.ts CHANGED
@@ -8,25 +8,40 @@ import * as Brand from "effect/Brand";
8
8
  import * as Context from "effect/Context";
9
9
  import * as Effect from "effect/Effect";
10
10
  import * as Layer from "effect/Layer";
11
- import { customAlphabet, nanoid } from "nanoid";
12
11
 
13
- // TODO: Add dynamic import error.
14
- export interface Bip39 {
15
- readonly make: Effect.Effect<Mnemonic>;
12
+ import { Id } from "./Model.js";
16
13
 
17
- readonly toSeed: (mnemonic: Mnemonic) => Effect.Effect<Uint8Array>;
14
+ export class Bip39 extends Context.Tag("Bip39")<
15
+ Bip39,
16
+ {
17
+ readonly make: Effect.Effect<Mnemonic>;
18
18
 
19
- readonly parse: (
20
- mnemonic: string,
21
- ) => Effect.Effect<Mnemonic, InvalidMnemonicError>;
22
- }
19
+ readonly toSeed: (mnemonic: Mnemonic) => Effect.Effect<Uint8Array>;
23
20
 
24
- export const Bip39 = Context.GenericTag<Bip39>("@services/Bip39");
21
+ readonly parse: (
22
+ mnemonic: string,
23
+ ) => Effect.Effect<Mnemonic, InvalidMnemonicError>;
24
+ }
25
+ >() {}
25
26
 
26
27
  export interface InvalidMnemonicError {
27
28
  readonly _tag: "InvalidMnemonicError";
28
29
  }
29
30
 
31
+ export const validateMnemonicToEffect =
32
+ (validateMnemonic: (mnemonic: string, wordlist: string[]) => boolean) =>
33
+ (
34
+ mnemonic: string,
35
+ wordlist: string[],
36
+ ): Effect.Effect<Mnemonic, InvalidMnemonicError, never> => {
37
+ const mnemonicTrimmed = mnemonic.trim();
38
+ return validateMnemonic(mnemonicTrimmed, wordlist)
39
+ ? Effect.succeed(mnemonicTrimmed as Mnemonic)
40
+ : Effect.fail<InvalidMnemonicError>({
41
+ _tag: "InvalidMnemonicError",
42
+ });
43
+ };
44
+
30
45
  /**
31
46
  * Mnemonic is a password generated by Evolu in BIP39 format.
32
47
  *
@@ -36,33 +51,41 @@ export interface InvalidMnemonicError {
36
51
  */
37
52
  export type Mnemonic = string & Brand.Brand<"Mnemonic">;
38
53
 
39
- export interface NanoIdGenerator {
40
- readonly nanoid: Effect.Effect<NanoId>;
41
- readonly nanoidAsNodeId: Effect.Effect<NodeId>;
42
- }
43
-
44
- export const NanoIdGenerator = Context.GenericTag<NanoIdGenerator>(
45
- "@services/NanoIdGenerator",
46
- );
54
+ export class NanoIdGenerator extends Context.Tag("NanoIdGenerator")<
55
+ NanoIdGenerator,
56
+ {
57
+ readonly nanoid: Effect.Effect<NanoId>;
58
+ readonly nodeId: Effect.Effect<NodeId>;
59
+ readonly rowId: Effect.Effect<Id>;
60
+ }
61
+ >() {}
62
+
63
+ export const createNanoIdGeneratorLive = (
64
+ customAlphabet: (
65
+ alphabet: string,
66
+ defaultSize?: number | undefined,
67
+ ) => (size?: number | undefined) => string,
68
+ nanoid: (size?: number) => string,
69
+ ): Layer.Layer<NanoIdGenerator, never, never> => {
70
+ const nanoidForNodeId = customAlphabet("0123456789abcdef", 16);
71
+ return Layer.succeed(
72
+ NanoIdGenerator,
73
+ NanoIdGenerator.of({
74
+ nanoid: Effect.sync(() => nanoid() as NanoId),
75
+ nodeId: Effect.sync(() => nanoidForNodeId() as NodeId),
76
+ rowId: Effect.sync(() => nanoid() as Id),
77
+ }),
78
+ );
79
+ };
47
80
 
48
81
  export type NanoId = string & Brand.Brand<"NanoId">;
49
82
 
50
- export const NodeId = S.string.pipe(
83
+ export const NodeId = S.String.pipe(
51
84
  S.pattern(/^[\w-]{16}$/),
52
85
  S.brand("NodeId"),
53
86
  );
54
87
  export type NodeId = S.Schema.Type<typeof NodeId>;
55
88
 
56
- const nanoidForNodeId = customAlphabet("0123456789abcdef", 16);
57
-
58
- export const NanoIdGeneratorLive = Layer.succeed(
59
- NanoIdGenerator,
60
- NanoIdGenerator.of({
61
- nanoid: Effect.sync(() => nanoid() as NanoId),
62
- nanoidAsNodeId: Effect.sync(() => nanoidForNodeId() as NodeId),
63
- }),
64
- );
65
-
66
89
  // SLIP-21 implementation
67
90
  // https://github.com/satoshilabs/slips/blob/master/slip-0021.md
68
91
  export const slip21Derive = (
@@ -81,35 +104,35 @@ export const slip21Derive = (
81
104
  return m.slice(32, 64);
82
105
  });
83
106
 
84
- /** Alias to xsalsa20poly1305, for compatibility with libsodium/nacl */
85
- export interface SecretBox {
86
- readonly seal: (
87
- key: Uint8Array,
88
- plaintext: Uint8Array,
89
- ) => Effect.Effect<Uint8Array>;
90
-
91
- readonly open: (
92
- key: Uint8Array,
93
- ciphertext: Uint8Array,
94
- ) => Effect.Effect<Uint8Array>;
95
- }
96
-
97
- export const SecretBox = Context.GenericTag<SecretBox>("@services/SecretBox");
98
-
99
- export const SecretBoxLive = Layer.succeed(
107
+ export class SecretBox extends Context.Tag("SecretBox")<
100
108
  SecretBox,
101
- SecretBox.of({
102
- seal: (key, plaintext) =>
103
- Effect.sync(() => {
104
- const nonce = randomBytes(24);
105
- const ciphertext = secretbox(key, nonce).seal(plaintext);
106
- return concatBytes(nonce, ciphertext);
107
- }),
108
- open: (key, ciphertext) =>
109
- Effect.sync(() => {
110
- const nonce = ciphertext.subarray(0, 24);
111
- const ciphertextWithoutNonce = ciphertext.subarray(24);
112
- return secretbox(key, nonce).open(ciphertextWithoutNonce);
113
- }),
114
- }),
115
- );
109
+ {
110
+ readonly seal: (
111
+ key: Uint8Array,
112
+ plaintext: Uint8Array,
113
+ ) => Effect.Effect<Uint8Array>;
114
+
115
+ readonly open: (
116
+ key: Uint8Array,
117
+ ciphertext: Uint8Array,
118
+ ) => Effect.Effect<Uint8Array>;
119
+ }
120
+ >() {
121
+ static Live = Layer.succeed(
122
+ SecretBox,
123
+ SecretBox.of({
124
+ seal: (key, plaintext) =>
125
+ Effect.sync(() => {
126
+ const nonce = randomBytes(24);
127
+ const ciphertext = secretbox(key, nonce).seal(plaintext);
128
+ return concatBytes(nonce, ciphertext);
129
+ }),
130
+ open: (key, ciphertext) =>
131
+ Effect.sync(() => {
132
+ const nonce = ciphertext.subarray(0, 24);
133
+ const ciphertextWithoutNonce = ciphertext.subarray(24);
134
+ return secretbox(key, nonce).open(ciphertextWithoutNonce);
135
+ }),
136
+ }),
137
+ );
138
+ }