@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.
- package/README.md +1 -1
- package/dist/src/Config.d.ts +27 -30
- package/dist/src/Config.d.ts.map +1 -1
- package/dist/src/Config.js +32 -5
- package/dist/src/Crdt.d.ts +5 -4
- package/dist/src/Crdt.d.ts.map +1 -1
- package/dist/src/Crdt.js +27 -24
- package/dist/src/Crypto.d.ts +16 -10
- package/dist/src/Crypto.d.ts.map +1 -1
- package/dist/src/Crypto.js +24 -11
- package/dist/src/Db.d.ts +58 -78
- package/dist/src/Db.d.ts.map +1 -1
- package/dist/src/Db.js +368 -170
- package/dist/src/Diff.d.ts +9 -3
- package/dist/src/Diff.d.ts.map +1 -1
- package/dist/src/Diff.js +12 -11
- package/dist/src/Error.d.ts +15 -0
- package/dist/src/Error.d.ts.map +1 -0
- package/dist/src/Error.js +14 -0
- package/dist/src/Evolu.d.ts +139 -91
- package/dist/src/Evolu.d.ts.map +1 -1
- package/dist/src/Evolu.js +267 -239
- package/dist/src/Model.d.ts +52 -0
- package/dist/src/Model.d.ts.map +1 -1
- package/dist/src/Model.js +47 -5
- package/dist/src/Owner.d.ts.map +1 -1
- package/dist/src/Owner.js +14 -8
- package/dist/src/Platform.d.ts +24 -34
- package/dist/src/Platform.d.ts.map +1 -1
- package/dist/src/Platform.js +9 -31
- package/dist/src/Public.d.ts +3 -5
- package/dist/src/Public.d.ts.map +1 -1
- package/dist/src/Public.js +2 -3
- package/dist/src/Sqlite.d.ts +25 -16
- package/dist/src/Sqlite.d.ts.map +1 -1
- package/dist/src/Sqlite.js +28 -20
- package/dist/src/Store.d.ts.map +1 -1
- package/dist/src/Sync.d.ts +70 -0
- package/dist/src/Sync.d.ts.map +1 -0
- package/dist/src/Sync.js +126 -0
- package/dist/src/index.d.ts +2 -5
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -5
- package/package.json +12 -10
- package/src/Config.ts +77 -36
- package/src/Crdt.ts +61 -76
- package/src/Crypto.ts +83 -60
- package/src/Db.ts +782 -352
- package/src/Diff.ts +23 -21
- package/src/Error.ts +29 -0
- package/src/Evolu.ts +599 -499
- package/src/Model.ts +102 -6
- package/src/Owner.ts +24 -24
- package/src/Platform.ts +33 -81
- package/src/Public.ts +3 -5
- package/src/Sqlite.ts +81 -44
- package/src/Sync.ts +306 -0
- package/src/index.ts +2 -5
- package/dist/src/DbWorker.d.ts +0 -82
- package/dist/src/DbWorker.d.ts.map +0 -1
- package/dist/src/DbWorker.js +0 -335
- package/dist/src/ErrorStore.d.ts +0 -29
- package/dist/src/ErrorStore.d.ts.map +0 -1
- package/dist/src/ErrorStore.js +0 -13
- package/dist/src/OnCompletes.d.ts +0 -14
- package/dist/src/OnCompletes.d.ts.map +0 -1
- package/dist/src/OnCompletes.js +0 -25
- package/dist/src/SyncWorker.d.ts +0 -80
- package/dist/src/SyncWorker.d.ts.map +0 -1
- package/dist/src/SyncWorker.js +0 -150
- package/dist/src/Types.d.ts +0 -13
- package/dist/src/Types.d.ts.map +0 -1
- package/dist/src/Types.js +0 -1
- package/src/DbWorker.ts +0 -721
- package/src/ErrorStore.ts +0 -46
- package/src/OnCompletes.ts +0 -51
- package/src/SyncWorker.ts +0 -361
- 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
|
-
|
|
14
|
-
export interface Bip39 {
|
|
15
|
-
readonly make: Effect.Effect<Mnemonic>;
|
|
12
|
+
import { Id } from "./Model.js";
|
|
16
13
|
|
|
17
|
-
|
|
14
|
+
export class Bip39 extends Context.Tag("Bip39")<
|
|
15
|
+
Bip39,
|
|
16
|
+
{
|
|
17
|
+
readonly make: Effect.Effect<Mnemonic>;
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
mnemonic: string,
|
|
21
|
-
) => Effect.Effect<Mnemonic, InvalidMnemonicError>;
|
|
22
|
-
}
|
|
19
|
+
readonly toSeed: (mnemonic: Mnemonic) => Effect.Effect<Uint8Array>;
|
|
23
20
|
|
|
24
|
-
|
|
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
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
102
|
-
seal: (
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
+
}
|