tool-db 2.6.2 → 2.6.6
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/bundle.js +1 -1
- package/dist/adapters-base/storageAdapter.js +1 -1
- package/dist/adapters-base/userAdapter.js +1 -1
- package/dist/crdt/counterCrdt.d.ts +2 -2
- package/dist/crdt/listCrdt.d.ts +2 -2
- package/dist/crdt/mapCrdt.d.ts +2 -2
- package/dist/messageHandlers/handlePut.js +101 -12
- package/dist/messageHandlers/handlePut.js.map +1 -1
- package/dist/toolDbAnonSignIn.js +1 -1
- package/dist/toolDbCheckSignupConflicts.d.ts +7 -0
- package/dist/toolDbCheckSignupConflicts.js +123 -0
- package/dist/toolDbCheckSignupConflicts.js.map +1 -0
- package/dist/toolDbKeysSignIn.js +1 -1
- package/dist/toolDbSignIn.js +1 -1
- package/dist/toolDbSignUp.d.ts +10 -0
- package/dist/toolDbSignUp.js +83 -54
- package/dist/toolDbSignUp.js.map +1 -1
- package/dist/toolDbVerificationWrapper.js +1 -1
- package/dist/tooldb.d.ts +18 -0
- package/dist/tooldb.js +120 -32
- package/dist/tooldb.js.map +1 -1
- package/dist/types/message.d.ts +2 -2
- package/dist/types/tooldb.d.ts +7 -7
- package/dist/utils/verifyMessage.js +11 -3
- package/dist/utils/verifyMessage.js.map +1 -1
- package/package.json +3 -3
- package/lib/adapters-base/networkAdapter.ts +0 -218
- package/lib/adapters-base/storageAdapter.ts +0 -57
- package/lib/adapters-base/userAdapter.ts +0 -49
- package/lib/crdt/baseCrdt.ts +0 -21
- package/lib/crdt/counterCrdt.ts +0 -112
- package/lib/crdt/listCrdt.ts +0 -190
- package/lib/crdt/mapCrdt.ts +0 -121
- package/lib/index.ts +0 -49
- package/lib/logger.ts +0 -30
- package/lib/messageHandlers/handleCrdtGet.ts +0 -34
- package/lib/messageHandlers/handleCrdtPut.ts +0 -133
- package/lib/messageHandlers/handleFunction.ts +0 -59
- package/lib/messageHandlers/handleGet.ts +0 -34
- package/lib/messageHandlers/handlePing.ts +0 -40
- package/lib/messageHandlers/handlePong.ts +0 -37
- package/lib/messageHandlers/handlePut.ts +0 -59
- package/lib/messageHandlers/handleQuery.ts +0 -30
- package/lib/messageHandlers/handleSubscribe.ts +0 -46
- package/lib/server.ts +0 -7
- package/lib/shared.ts +0 -5
- package/lib/toolDbAnonSignIn.ts +0 -5
- package/lib/toolDbClientOnMessage.ts +0 -79
- package/lib/toolDbCrdtGet.ts +0 -83
- package/lib/toolDbCrdtPut.ts +0 -78
- package/lib/toolDbFunction.ts +0 -49
- package/lib/toolDbGet.ts +0 -81
- package/lib/toolDbKeysSignIn.ts +0 -13
- package/lib/toolDbPut.ts +0 -84
- package/lib/toolDbQueryKeys.ts +0 -71
- package/lib/toolDbSignIn.ts +0 -37
- package/lib/toolDbSignUp.ts +0 -72
- package/lib/toolDbSubscribe.ts +0 -54
- package/lib/toolDbVerificationWrapper.ts +0 -55
- package/lib/tooldb.ts +0 -343
- package/lib/types/message.ts +0 -154
- package/lib/types/tooldb.ts +0 -140
- package/lib/utils/catchReturn.ts +0 -4
- package/lib/utils/encoding/arrayBufferToHex.ts +0 -18
- package/lib/utils/encoding/arrayBufferToString.ts +0 -4
- package/lib/utils/encoding/base64ToUint8.ts +0 -13
- package/lib/utils/encoding/fromBase64.ts +0 -4
- package/lib/utils/encoding/hexToArrayBuffer.ts +0 -13
- package/lib/utils/encoding/hexToString.ts +0 -8
- package/lib/utils/encoding/hexToUint8.ts +0 -5
- package/lib/utils/encoding/stringToArrayBuffer.ts +0 -4
- package/lib/utils/encoding/toBase64.ts +0 -4
- package/lib/utils/encoding/uint8ToBase64.ts +0 -11
- package/lib/utils/generateGroupKey.ts +0 -11
- package/lib/utils/getPeerSignature.ts +0 -12
- package/lib/utils/getTimestamp.ts +0 -3
- package/lib/utils/proofOfWork.ts +0 -31
- package/lib/utils/randomAnimal.ts +0 -77
- package/lib/utils/sha1.ts +0 -7
- package/lib/utils/sha256.ts +0 -7
- package/lib/utils/textRandom.ts +0 -11
- package/lib/utils/uniq.ts +0 -3
- package/lib/utils/verifyMessage.ts +0 -88
- package/lib/utils/verifyPeer.ts +0 -15
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { ToolDb, VerificationData } from "..";
|
|
2
|
-
|
|
3
|
-
export default class ToolDbUserAdapter {
|
|
4
|
-
private _tooldb: ToolDb;
|
|
5
|
-
|
|
6
|
-
get tooldb() {
|
|
7
|
-
return this._tooldb;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
constructor(db: ToolDb) {
|
|
11
|
-
this._tooldb = db;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public async anonUser(): Promise<void> {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public async setUser(account: unknown | undefined, name: string): Promise<void> { }
|
|
19
|
-
|
|
20
|
-
public signData(data: string): Promise<string> {
|
|
21
|
-
return Promise.resolve("");
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public verifySignature(
|
|
25
|
-
message: Partial<VerificationData<any>>
|
|
26
|
-
): Promise<boolean> {
|
|
27
|
-
return Promise.resolve(false);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public getAccountFromPrivate(privateKey: string): Promise<unknown> {
|
|
31
|
-
return Promise.resolve(undefined);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public encryptAccount(password: string): Promise<unknown> {
|
|
35
|
-
return Promise.resolve(undefined);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public decryptAccount(acc: unknown, password: string): Promise<any> {
|
|
39
|
-
return Promise.resolve(undefined);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public getAddress(): string | undefined {
|
|
43
|
-
return "";
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public getUsername(): string | undefined {
|
|
47
|
-
return "";
|
|
48
|
-
}
|
|
49
|
-
}
|
package/lib/crdt/baseCrdt.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
export const CRDT_MAP = "MAP";
|
|
2
|
-
|
|
3
|
-
export const CRDT_LIST = "LIST";
|
|
4
|
-
|
|
5
|
-
export const CRDT_COUNTER = "COUNTER";
|
|
6
|
-
|
|
7
|
-
export class BaseCrdt<T = any, Changes = any, Value = any> {
|
|
8
|
-
public type: string = "";
|
|
9
|
-
|
|
10
|
-
public mergeChanges(changes: Changes[]) {
|
|
11
|
-
//
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
public getChanges(): Changes[] {
|
|
15
|
-
return [];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
get value(): Value {
|
|
19
|
-
return "" as any;
|
|
20
|
-
}
|
|
21
|
-
}
|
package/lib/crdt/counterCrdt.ts
DELETED
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
import { BaseCrdt, CRDT_LIST } from "..";
|
|
2
|
-
|
|
3
|
-
export type CounterOperations = "ADD" | "SUB";
|
|
4
|
-
|
|
5
|
-
export interface ChangeCounterBase {
|
|
6
|
-
t: CounterOperations; // Operation type
|
|
7
|
-
v: number; // Value
|
|
8
|
-
a: string;
|
|
9
|
-
i: number;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface AddCounterChange extends ChangeCounterBase {
|
|
13
|
-
t: "ADD";
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface SubCounterChange extends ChangeCounterBase {
|
|
17
|
-
t: "SUB";
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export type CounterChanges = AddCounterChange | SubCounterChange;
|
|
21
|
-
|
|
22
|
-
export class CounterCrdt<T> extends BaseCrdt<number, CounterChanges, number> {
|
|
23
|
-
public type = CRDT_LIST;
|
|
24
|
-
|
|
25
|
-
public _changes: CounterChanges[] = [];
|
|
26
|
-
|
|
27
|
-
private _author = "";
|
|
28
|
-
|
|
29
|
-
private _value: number = 0;
|
|
30
|
-
|
|
31
|
-
private _lastUpdateSize: number = 0;
|
|
32
|
-
|
|
33
|
-
constructor(author: string, changes?: CounterChanges[]) {
|
|
34
|
-
super();
|
|
35
|
-
this._author = author;
|
|
36
|
-
if (changes) {
|
|
37
|
-
this.mergeChanges(changes);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
this.calculate();
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
changesSort(a: CounterChanges, b: CounterChanges) {
|
|
44
|
-
if (a.i > b.i) return 1;
|
|
45
|
-
if (a.i < b.i) return -1;
|
|
46
|
-
return 0; // Should never be equal!
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
calculate() {
|
|
50
|
-
let temp: number = 0;
|
|
51
|
-
// Only update if we have new changes
|
|
52
|
-
if (this._changes.length !== this._lastUpdateSize) {
|
|
53
|
-
this._changes.sort(this.changesSort).forEach((change) => {
|
|
54
|
-
if (change.t === "ADD") {
|
|
55
|
-
temp += change.v;
|
|
56
|
-
} else if (change.t === "SUB") {
|
|
57
|
-
temp -= change.v;
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
this._lastUpdateSize = this._changes.length;
|
|
62
|
-
this._value = temp;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
get value(): number {
|
|
67
|
-
this.calculate();
|
|
68
|
-
return this._value;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
public mergeChanges(newChanges: CounterChanges[]) {
|
|
72
|
-
newChanges.forEach((change) => {
|
|
73
|
-
// Filter by author and index
|
|
74
|
-
const filtered = this._changes.filter(
|
|
75
|
-
(c) => c.i === change.i && c.a === change.a && c.t === change.t
|
|
76
|
-
);
|
|
77
|
-
// Only add if there are not matches
|
|
78
|
-
if (filtered.length === 0) {
|
|
79
|
-
this._changes.push(change);
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
this.calculate();
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
public getChanges(): CounterChanges[] {
|
|
86
|
-
return this._changes;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
public ADD(value: number) {
|
|
90
|
-
const ourChanges = this._changes.filter((c) => c.a === this._author);
|
|
91
|
-
|
|
92
|
-
this._changes.push({
|
|
93
|
-
t: "ADD",
|
|
94
|
-
v: value,
|
|
95
|
-
a: this._author,
|
|
96
|
-
i: ourChanges.length,
|
|
97
|
-
});
|
|
98
|
-
this.calculate();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public SUB(value: number) {
|
|
102
|
-
const ourChanges = this._changes.filter((c) => c.a === this._author);
|
|
103
|
-
|
|
104
|
-
this._changes.push({
|
|
105
|
-
t: "SUB",
|
|
106
|
-
v: value,
|
|
107
|
-
a: this._author,
|
|
108
|
-
i: ourChanges.length,
|
|
109
|
-
});
|
|
110
|
-
this.calculate();
|
|
111
|
-
}
|
|
112
|
-
}
|
package/lib/crdt/listCrdt.ts
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
import { BaseCrdt, CRDT_LIST } from "..";
|
|
2
|
-
|
|
3
|
-
export type ListOperations = "INS" | "DEL";
|
|
4
|
-
|
|
5
|
-
export interface ChangeListBase<T> {
|
|
6
|
-
t: ListOperations; // Operation type
|
|
7
|
-
i: string; // "index", author + n
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export interface InsListChange<T> extends ChangeListBase<T> {
|
|
11
|
-
t: "INS";
|
|
12
|
-
v: T; // Value
|
|
13
|
-
p: string | undefined; // Previous index, if any
|
|
14
|
-
n: string | undefined; // Next index, if any
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface DelListChange<T> extends ChangeListBase<T> {
|
|
18
|
-
t: "DEL";
|
|
19
|
-
v: string; // target index to tombstone
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export type ListChanges<T> = InsListChange<T> | DelListChange<T>;
|
|
23
|
-
|
|
24
|
-
interface ListTempCursor<T> {
|
|
25
|
-
value: T;
|
|
26
|
-
index: string;
|
|
27
|
-
tomb: boolean;
|
|
28
|
-
prev: string | undefined;
|
|
29
|
-
next: string | undefined;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export class ListCrdt<T> extends BaseCrdt<T, ListChanges<T>, T[]> {
|
|
33
|
-
public type = CRDT_LIST;
|
|
34
|
-
|
|
35
|
-
public _changes: ListChanges<T>[] = [];
|
|
36
|
-
|
|
37
|
-
private _author = "";
|
|
38
|
-
|
|
39
|
-
private _value: T[] = [];
|
|
40
|
-
|
|
41
|
-
public _tempValues: ListTempCursor<T>[] = [];
|
|
42
|
-
|
|
43
|
-
private _lastUpdateSize: number = 0;
|
|
44
|
-
|
|
45
|
-
constructor(author: string, changes?: ListChanges<T>[]) {
|
|
46
|
-
super();
|
|
47
|
-
this._author = author;
|
|
48
|
-
if (changes) {
|
|
49
|
-
this.mergeChanges(changes);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
this.calculate();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
changesSort(a: ListChanges<T>, b: ListChanges<T>) {
|
|
56
|
-
if (a.i > b.i) return 1;
|
|
57
|
-
if (a.i < b.i) return -1;
|
|
58
|
-
return 0; // Should never be equal!
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
calculate() {
|
|
62
|
-
const temp: ListTempCursor<T>[] = [];
|
|
63
|
-
// Only update if we have new changes
|
|
64
|
-
if (this._changes.length !== this._lastUpdateSize) {
|
|
65
|
-
this._changes.sort(this.changesSort).forEach((change) => {
|
|
66
|
-
if (change.t === "INS") {
|
|
67
|
-
let poisitionToInsert = 0;
|
|
68
|
-
if (change.p) {
|
|
69
|
-
poisitionToInsert = temp.findIndex((v) => v.index === change.p) + 1;
|
|
70
|
-
} else if (change.n) {
|
|
71
|
-
poisitionToInsert = temp.findIndex((v) => v.index === change.n);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const cursorValue: ListTempCursor<T> = {
|
|
75
|
-
value: change.v,
|
|
76
|
-
index: change.i,
|
|
77
|
-
tomb: false,
|
|
78
|
-
prev: change.p,
|
|
79
|
-
next: change.n,
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
temp.splice(poisitionToInsert, 0, cursorValue);
|
|
83
|
-
} else if (change.t === "DEL") {
|
|
84
|
-
const poisitionToInsert = temp.findIndex((v) => v.index === change.v);
|
|
85
|
-
temp[poisitionToInsert].tomb = true;
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
this._lastUpdateSize = this._changes.length;
|
|
89
|
-
|
|
90
|
-
this._tempValues = temp;
|
|
91
|
-
|
|
92
|
-
this._value = temp.filter((v) => !v.tomb).map((v) => v.value);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
get value(): T[] {
|
|
97
|
-
this.calculate();
|
|
98
|
-
return this._value;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
public mergeChanges(newChanges: ListChanges<T>[]) {
|
|
102
|
-
newChanges.forEach((change) => {
|
|
103
|
-
// Filter by author and index
|
|
104
|
-
const filtered = this._changes.filter(
|
|
105
|
-
(c) => c.i === change.i && c.t === change.t && c.v === change.v
|
|
106
|
-
);
|
|
107
|
-
// Only add if there are not matches
|
|
108
|
-
if (filtered.length === 0) {
|
|
109
|
-
this._changes.push(change);
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
this.calculate();
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
public getChanges(): ListChanges<T>[] {
|
|
116
|
-
return this._changes;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
public INS(value: T, index: number) {
|
|
120
|
-
const filterTombs = this._tempValues.filter((v) => !v.tomb);
|
|
121
|
-
|
|
122
|
-
let currentPrev = undefined;
|
|
123
|
-
let currentNext = undefined;
|
|
124
|
-
|
|
125
|
-
if (filterTombs.length !== 0) {
|
|
126
|
-
const currentIndex = filterTombs[index].index;
|
|
127
|
-
const currentIndexPosition = this._tempValues.findIndex(
|
|
128
|
-
(v) => v.index === currentIndex
|
|
129
|
-
);
|
|
130
|
-
currentPrev = this._tempValues[currentIndexPosition - 1];
|
|
131
|
-
currentNext = this._tempValues[currentIndexPosition];
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
const ourChanges = this._changes.filter((c) =>
|
|
135
|
-
c.i.startsWith(this._author)
|
|
136
|
-
);
|
|
137
|
-
|
|
138
|
-
this._changes.push({
|
|
139
|
-
t: "INS",
|
|
140
|
-
p: currentPrev?.index,
|
|
141
|
-
n: currentNext?.index,
|
|
142
|
-
v: value,
|
|
143
|
-
i: this._author + "-" + `${ourChanges.length}`.padStart(8, "0"),
|
|
144
|
-
});
|
|
145
|
-
this.calculate();
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
public PUSH(value: T) {
|
|
149
|
-
const filterTombs = this._tempValues.filter((v) => !v.tomb);
|
|
150
|
-
const currentIndex = filterTombs[filterTombs.length - 1]?.index;
|
|
151
|
-
const currentIndexPosition = this._tempValues.findIndex(
|
|
152
|
-
(v) => v.index === currentIndex
|
|
153
|
-
);
|
|
154
|
-
|
|
155
|
-
const currentPrev = this._tempValues[currentIndexPosition];
|
|
156
|
-
|
|
157
|
-
const ourChanges = this._changes.filter((c) =>
|
|
158
|
-
c.i.startsWith(this._author)
|
|
159
|
-
);
|
|
160
|
-
|
|
161
|
-
this._changes.push({
|
|
162
|
-
t: "INS",
|
|
163
|
-
p: currentPrev?.index,
|
|
164
|
-
n: undefined,
|
|
165
|
-
v: value,
|
|
166
|
-
i: this._author + "-" + `${ourChanges.length}`.padStart(8, "0"),
|
|
167
|
-
});
|
|
168
|
-
this.calculate();
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
public DEL(index: number) {
|
|
172
|
-
const currentIndex = this._tempValues.filter((v) => !v.tomb)[index].index;
|
|
173
|
-
const currentIndexPosition = this._tempValues.findIndex(
|
|
174
|
-
(v) => v.index === currentIndex
|
|
175
|
-
);
|
|
176
|
-
|
|
177
|
-
const current = this._tempValues[currentIndexPosition];
|
|
178
|
-
|
|
179
|
-
const ourChanges = this._changes.filter((c) =>
|
|
180
|
-
c.i.startsWith(this._author)
|
|
181
|
-
);
|
|
182
|
-
|
|
183
|
-
this._changes.push({
|
|
184
|
-
t: "DEL",
|
|
185
|
-
v: current.index,
|
|
186
|
-
i: this._author + "-" + `${ourChanges.length}`.padStart(8, "0"),
|
|
187
|
-
});
|
|
188
|
-
this.calculate();
|
|
189
|
-
}
|
|
190
|
-
}
|
package/lib/crdt/mapCrdt.ts
DELETED
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import { BaseCrdt, CRDT_MAP } from "..";
|
|
2
|
-
|
|
3
|
-
export type MapOperations = "SET" | "DEL";
|
|
4
|
-
|
|
5
|
-
export interface ChangeMapBase<T> {
|
|
6
|
-
t: MapOperations;
|
|
7
|
-
a: string;
|
|
8
|
-
k: string;
|
|
9
|
-
i: number;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export interface SetMapChange<T> extends ChangeMapBase<T> {
|
|
13
|
-
t: "SET";
|
|
14
|
-
v: T;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface DelMapChange<T> extends ChangeMapBase<T> {
|
|
18
|
-
t: "DEL";
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export type MapChanges<T> = SetMapChange<T> | DelMapChange<T>;
|
|
22
|
-
|
|
23
|
-
export class MapCrdt<T> extends BaseCrdt<T, MapChanges<T>, Record<string, T>> {
|
|
24
|
-
public type = CRDT_MAP;
|
|
25
|
-
|
|
26
|
-
private _changes: MapChanges<T>[] = [];
|
|
27
|
-
|
|
28
|
-
private _author = "";
|
|
29
|
-
|
|
30
|
-
private _value: Record<string, T> = {};
|
|
31
|
-
|
|
32
|
-
private _keyIndex: Record<string, number> = {};
|
|
33
|
-
|
|
34
|
-
private _lastUpdateSize: number = 0;
|
|
35
|
-
|
|
36
|
-
constructor(author: string, changes?: MapChanges<T>[]) {
|
|
37
|
-
super();
|
|
38
|
-
this._author = author;
|
|
39
|
-
if (changes) {
|
|
40
|
-
this.mergeChanges(changes);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
this.calculate();
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
changesSort(a: MapChanges<T>, b: MapChanges<T>) {
|
|
47
|
-
if (a.i < b.i) return -1;
|
|
48
|
-
if (a.i > b.i) return 1;
|
|
49
|
-
if (a.t === "SET" && b.t === "DEL") return -1;
|
|
50
|
-
if (a.t === "DEL" && b.t === "SET") return 1;
|
|
51
|
-
if (a.a < b.a) return -1;
|
|
52
|
-
if (a.a > b.a) return 1;
|
|
53
|
-
return 0; // Should never be equal!
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
calculate() {
|
|
57
|
-
const temp: Record<string, T> = {};
|
|
58
|
-
// Only update if we have new changes
|
|
59
|
-
if (this._changes.length !== this._lastUpdateSize) {
|
|
60
|
-
this._changes.sort(this.changesSort).forEach((change) => {
|
|
61
|
-
// Here we apply the model properties
|
|
62
|
-
// Since this is a KV store we dont need much logic,
|
|
63
|
-
// except for the sorting of the changes. Then we just apply them.
|
|
64
|
-
if (change.t === "SET") {
|
|
65
|
-
temp[change.k] = change.v;
|
|
66
|
-
} else if (change.t === "DEL") {
|
|
67
|
-
delete temp[change.k];
|
|
68
|
-
}
|
|
69
|
-
this._keyIndex[change.k] = change.i;
|
|
70
|
-
});
|
|
71
|
-
this._lastUpdateSize = this._changes.length;
|
|
72
|
-
this._value = temp;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
get value(): Record<string, T> {
|
|
77
|
-
this.calculate();
|
|
78
|
-
return this._value;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
public mergeChanges(newChanges: MapChanges<T>[]) {
|
|
82
|
-
newChanges.forEach((change) => {
|
|
83
|
-
// Filter by key, author, index, and type
|
|
84
|
-
const filtered = this._changes.filter(
|
|
85
|
-
(c) => c.k === change.k && c.i === change.i && c.a === change.a && c.t === change.t
|
|
86
|
-
);
|
|
87
|
-
// Only add if there are not matches
|
|
88
|
-
if (filtered.length === 0) {
|
|
89
|
-
this._changes.push(change);
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
this.calculate();
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
public getChanges(): MapChanges<T>[] {
|
|
96
|
-
return this._changes;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
public SET(key: string, value: T) {
|
|
100
|
-
this._keyIndex[key] = (this._keyIndex[key] || 0) + 1;
|
|
101
|
-
this._changes.push({
|
|
102
|
-
t: "SET",
|
|
103
|
-
k: key,
|
|
104
|
-
a: this._author,
|
|
105
|
-
v: value,
|
|
106
|
-
i: this._keyIndex[key],
|
|
107
|
-
});
|
|
108
|
-
this.calculate();
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
public DEL(key: string) {
|
|
112
|
-
this._keyIndex[key] = (this._keyIndex[key] || 0) + 1;
|
|
113
|
-
this._changes.push({
|
|
114
|
-
t: "DEL",
|
|
115
|
-
k: key,
|
|
116
|
-
a: this._author,
|
|
117
|
-
i: this._keyIndex[key],
|
|
118
|
-
});
|
|
119
|
-
this.calculate();
|
|
120
|
-
}
|
|
121
|
-
}
|
package/lib/index.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
export { default as proofOfWork } from "./utils/proofOfWork";
|
|
2
|
-
export { default as sha1 } from "./utils/sha1";
|
|
3
|
-
export { default as sha256 } from "./utils/sha256";
|
|
4
|
-
export { default as uniq } from "./utils/uniq";
|
|
5
|
-
export { default as generateGroupKey } from "./utils/generateGroupKey";
|
|
6
|
-
|
|
7
|
-
export { default as randomAnimal } from "./utils/randomAnimal";
|
|
8
|
-
export { default as verifyMessage } from "./utils/verifyMessage";
|
|
9
|
-
export { default as getPeerSignature } from "./utils/getPeerSignature";
|
|
10
|
-
export { default as verifyPeer } from "./utils/verifyPeer";
|
|
11
|
-
export { default as textRandom } from "./utils/textRandom";
|
|
12
|
-
|
|
13
|
-
export { default as catchReturn } from "./utils/catchReturn";
|
|
14
|
-
|
|
15
|
-
export { default as stringToArrayBuffer } from "./utils/encoding/stringToArrayBuffer";
|
|
16
|
-
export { default as arrayBufferToString } from "./utils/encoding/arrayBufferToString";
|
|
17
|
-
|
|
18
|
-
export { default as arrayBufferToHex } from "./utils/encoding/arrayBufferToHex";
|
|
19
|
-
export { default as hexToArrayBuffer } from "./utils/encoding/hexToArrayBuffer";
|
|
20
|
-
export { default as hexToString } from "./utils/encoding/hexToString";
|
|
21
|
-
export { default as hexToUint8 } from "./utils/encoding/hexToUint8";
|
|
22
|
-
|
|
23
|
-
export { default as toBase64 } from "./utils/encoding/toBase64";
|
|
24
|
-
export { default as fromBase64 } from "./utils/encoding/fromBase64";
|
|
25
|
-
export { default as uint8ToBase64 } from "./utils/encoding/uint8ToBase64";
|
|
26
|
-
export { default as base64ToUint8 } from "./utils/encoding/base64ToUint8";
|
|
27
|
-
|
|
28
|
-
export { default as ToolDbNetworkAdapter } from "./adapters-base/networkAdapter";
|
|
29
|
-
export { default as ToolDbStorageAdapter } from "./adapters-base/storageAdapter";
|
|
30
|
-
export { default as ToolDbUserAdapter } from "./adapters-base/userAdapter";
|
|
31
|
-
|
|
32
|
-
export * from "./types/tooldb";
|
|
33
|
-
export * from "./types/message";
|
|
34
|
-
export * from "./crdt/baseCrdt";
|
|
35
|
-
export * from "./crdt/counterCrdt";
|
|
36
|
-
export * from "./crdt/listCrdt";
|
|
37
|
-
export * from "./crdt/mapCrdt";
|
|
38
|
-
|
|
39
|
-
export { default as handleSubscribe } from "./messageHandlers/handleSubscribe";
|
|
40
|
-
export { default as handleFunction } from "./messageHandlers/handleFunction";
|
|
41
|
-
export { default as handleCrdtGet } from "./messageHandlers/handleCrdtGet";
|
|
42
|
-
export { default as handleCrdtPut } from "./messageHandlers/handleCrdtPut";
|
|
43
|
-
export { default as handleQuery } from "./messageHandlers/handleQuery";
|
|
44
|
-
export { default as handlePing } from "./messageHandlers/handlePing";
|
|
45
|
-
export { default as handlePong } from "./messageHandlers/handlePong";
|
|
46
|
-
export { default as handleGet } from "./messageHandlers/handleGet";
|
|
47
|
-
export { default as handlePut } from "./messageHandlers/handlePut";
|
|
48
|
-
|
|
49
|
-
export { default as ToolDb } from "./tooldb";
|
package/lib/logger.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { ToolDb, sha1 } from ".";
|
|
2
|
-
|
|
3
|
-
export default function logger(
|
|
4
|
-
this: ToolDb,
|
|
5
|
-
arg0: any,
|
|
6
|
-
arg1?: any,
|
|
7
|
-
arg2?: any,
|
|
8
|
-
arg3?: any,
|
|
9
|
-
arg4?: any,
|
|
10
|
-
arg5?: any
|
|
11
|
-
) {
|
|
12
|
-
if (this.options.debug) {
|
|
13
|
-
const isNode = typeof window === "undefined";
|
|
14
|
-
const title = isNode
|
|
15
|
-
? this.options.storageName
|
|
16
|
-
: "%c" + this.options.storageName;
|
|
17
|
-
|
|
18
|
-
const style = isNode
|
|
19
|
-
? ""
|
|
20
|
-
: "background: #" +
|
|
21
|
-
sha1(this.options.storageName).slice(-6) +
|
|
22
|
-
"; padding: 2px; color: black";
|
|
23
|
-
|
|
24
|
-
console.log(
|
|
25
|
-
title,
|
|
26
|
-
style,
|
|
27
|
-
...[arg0, arg1, arg2, arg3, arg4, arg4, arg5].slice(0, arguments.length)
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { ToolDb, CrdtGetMessage, CrdtPutMessage } from "..";
|
|
2
|
-
|
|
3
|
-
export default function handleCrdtGet(
|
|
4
|
-
this: ToolDb,
|
|
5
|
-
message: CrdtGetMessage,
|
|
6
|
-
remotePeerId: string
|
|
7
|
-
) {
|
|
8
|
-
this.store
|
|
9
|
-
.get(message.key)
|
|
10
|
-
.then((data) => {
|
|
11
|
-
try {
|
|
12
|
-
// Use the id of the get so the other client knows we are replying
|
|
13
|
-
const oldData = {
|
|
14
|
-
type: "crdtPut",
|
|
15
|
-
data: JSON.parse(data),
|
|
16
|
-
to: [],
|
|
17
|
-
id: message.id,
|
|
18
|
-
} as CrdtPutMessage;
|
|
19
|
-
this.network.sendToClientId(remotePeerId, oldData);
|
|
20
|
-
} catch (e) {
|
|
21
|
-
// socket.send(data);
|
|
22
|
-
// do nothing
|
|
23
|
-
}
|
|
24
|
-
})
|
|
25
|
-
.catch((e) => {
|
|
26
|
-
const finalMessage: CrdtGetMessage = {
|
|
27
|
-
...message,
|
|
28
|
-
to: [...message.to, remotePeerId],
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
this.logger("Local key not found, relay", JSON.stringify(finalMessage));
|
|
32
|
-
this.network.sendToAll(finalMessage, false);
|
|
33
|
-
});
|
|
34
|
-
}
|