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
package/lib/tooldb.ts
DELETED
|
@@ -1,343 +0,0 @@
|
|
|
1
|
-
import EventEmitter from "events";
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
Peer,
|
|
5
|
-
randomAnimal,
|
|
6
|
-
ServerFunction,
|
|
7
|
-
ToolDbOptions,
|
|
8
|
-
ToolDbNetworkAdapter,
|
|
9
|
-
ToolDbStorageAdapter,
|
|
10
|
-
ToolDbUserAdapter,
|
|
11
|
-
ToolDbMessage,
|
|
12
|
-
verifyMessage,
|
|
13
|
-
VerificationData,
|
|
14
|
-
} from ".";
|
|
15
|
-
|
|
16
|
-
import toolDbGet from "./toolDbGet";
|
|
17
|
-
import toolDbPut from "./toolDbPut";
|
|
18
|
-
import toolDbSignIn from "./toolDbSignIn";
|
|
19
|
-
import toolDbSignUp from "./toolDbSignUp";
|
|
20
|
-
import toolDbCrdtGet from "./toolDbCrdtGet";
|
|
21
|
-
import toolDbCrdtPut from "./toolDbCrdtPut";
|
|
22
|
-
import toolDbFunction from "./toolDbFunction";
|
|
23
|
-
|
|
24
|
-
import toolDbAnonSignIn from "./toolDbAnonSignIn";
|
|
25
|
-
import toolDbClientOnMessage from "./toolDbClientOnMessage";
|
|
26
|
-
import toolDbVerificationWrapper from "./toolDbVerificationWrapper";
|
|
27
|
-
|
|
28
|
-
import toolDbQueryKeys from "./toolDbQueryKeys";
|
|
29
|
-
import toolDbKeysSignIn from "./toolDbKeysSignIn";
|
|
30
|
-
import toolDbSubscribe from "./toolDbSubscribe";
|
|
31
|
-
|
|
32
|
-
import handleGet from "./messageHandlers/handleGet";
|
|
33
|
-
import handlePut from "./messageHandlers/handlePut";
|
|
34
|
-
import handlePing from "./messageHandlers/handlePing";
|
|
35
|
-
import handlePong from "./messageHandlers/handlePong";
|
|
36
|
-
import handleQuery from "./messageHandlers/handleQuery";
|
|
37
|
-
import handleFunction from "./messageHandlers/handleFunction";
|
|
38
|
-
import handleCrdtGet from "./messageHandlers/handleCrdtGet";
|
|
39
|
-
import handleCrdtPut from "./messageHandlers/handleCrdtPut";
|
|
40
|
-
import handleSubscribe from "./messageHandlers/handleSubscribe";
|
|
41
|
-
|
|
42
|
-
import logger from "./logger";
|
|
43
|
-
|
|
44
|
-
export interface Listener<T = any> {
|
|
45
|
-
key: string;
|
|
46
|
-
timeout: number | null;
|
|
47
|
-
fn: (msg: VerificationData<T>) => void;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
interface Verificator<T> {
|
|
51
|
-
key: string;
|
|
52
|
-
fn: (
|
|
53
|
-
msg: VerificationData<T>,
|
|
54
|
-
previousData: T | undefined
|
|
55
|
-
) => Promise<boolean>;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export default class ToolDb extends EventEmitter {
|
|
59
|
-
private _network;
|
|
60
|
-
private _store: ToolDbStorageAdapter;
|
|
61
|
-
private _serverPeers: Peer[] = [];
|
|
62
|
-
private _peerAccount: ToolDbUserAdapter;
|
|
63
|
-
private _userAccount: ToolDbUserAdapter;
|
|
64
|
-
|
|
65
|
-
public logger = logger;
|
|
66
|
-
|
|
67
|
-
public clientOnMessage = toolDbClientOnMessage;
|
|
68
|
-
|
|
69
|
-
public verifyMessage = verifyMessage;
|
|
70
|
-
|
|
71
|
-
private _subscriptions: string[] = [];
|
|
72
|
-
|
|
73
|
-
get subscriptions() {
|
|
74
|
-
return this._subscriptions;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
private _processedIds: Record<string, string[]> = {};
|
|
78
|
-
|
|
79
|
-
get processedIds() {
|
|
80
|
-
return this._processedIds;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
private _processedOutHashes: Record<string, string[]> = {};
|
|
84
|
-
|
|
85
|
-
get processedOutHashes() {
|
|
86
|
-
return this._processedOutHashes;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
public subscribeData = toolDbSubscribe;
|
|
90
|
-
|
|
91
|
-
public isConnected = false;
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Emitted after a disconnection, when there are no more peers connected to.
|
|
95
|
-
*/
|
|
96
|
-
public onDisconnect = () => {
|
|
97
|
-
//
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Emitted the first time we are connected to a peer.
|
|
102
|
-
*/
|
|
103
|
-
public onConnect = () => {
|
|
104
|
-
//
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
public onPeerDisconnect = (peerId: string) => {
|
|
108
|
-
//
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
public onPeerConnect = (peerId: string) => {
|
|
112
|
-
//
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
public getData = toolDbGet;
|
|
116
|
-
|
|
117
|
-
public putData = toolDbPut;
|
|
118
|
-
|
|
119
|
-
public putCrdt = toolDbCrdtPut;
|
|
120
|
-
|
|
121
|
-
public getCrdt = toolDbCrdtGet;
|
|
122
|
-
|
|
123
|
-
public queryKeys = toolDbQueryKeys;
|
|
124
|
-
|
|
125
|
-
public doFunction = toolDbFunction;
|
|
126
|
-
|
|
127
|
-
public signIn = toolDbSignIn;
|
|
128
|
-
|
|
129
|
-
public anonSignIn = toolDbAnonSignIn;
|
|
130
|
-
|
|
131
|
-
public keysSignIn = toolDbKeysSignIn;
|
|
132
|
-
|
|
133
|
-
public signUp = toolDbSignUp;
|
|
134
|
-
|
|
135
|
-
public verify = toolDbVerificationWrapper;
|
|
136
|
-
|
|
137
|
-
// All message handlers go here
|
|
138
|
-
public handlePing = handlePing;
|
|
139
|
-
public handlePong = handlePong;
|
|
140
|
-
public handleCrdtGet = handleCrdtGet;
|
|
141
|
-
public handleCrdtPut = handleCrdtPut;
|
|
142
|
-
public handleGet = handleGet;
|
|
143
|
-
public handlePut = handlePut;
|
|
144
|
-
public handleQuery = handleQuery;
|
|
145
|
-
public handleSubscribe = handleSubscribe;
|
|
146
|
-
public handleFunction = handleFunction;
|
|
147
|
-
|
|
148
|
-
/**
|
|
149
|
-
* id listeners listen for a specific message ID just once
|
|
150
|
-
*/
|
|
151
|
-
public _idListeners: Record<string, (msg: ToolDbMessage) => void> = {};
|
|
152
|
-
|
|
153
|
-
public addIdListener = (id: string, fn: (msg: ToolDbMessage) => void) => {
|
|
154
|
-
this._idListeners[id] = fn;
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
public removeIdListener = (id: string) => {
|
|
158
|
-
delete this._idListeners[id];
|
|
159
|
-
};
|
|
160
|
-
|
|
161
|
-
public getUserNamespacedKey(key: string) {
|
|
162
|
-
return ":" + (this.userAccount?.getAddress() || "") + "." + key;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Server functions allow the server to define functions to be executed by the clients
|
|
167
|
-
* It is up to the function creator to specify proper security on these.
|
|
168
|
-
* Server functions are meant to execute on data stored on the server, in a way the clients
|
|
169
|
-
* dont have to overload the server, use with caution!
|
|
170
|
-
* Custom functions are expected to be a Promise that resolves to a string, and arguments are
|
|
171
|
-
* passed as an array of values. Type and sanity checking is up to the developer.
|
|
172
|
-
*/
|
|
173
|
-
private _functions: Record<string, ServerFunction<any, any>> = {};
|
|
174
|
-
|
|
175
|
-
get functions() {
|
|
176
|
-
return this._functions;
|
|
177
|
-
}
|
|
178
|
-
public addServerFunction<A, R>(
|
|
179
|
-
functionName: string,
|
|
180
|
-
fn: ServerFunction<A, R>
|
|
181
|
-
) {
|
|
182
|
-
this._functions[functionName] = fn;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Key listeners listen for a specific key, as long as the listener remains active
|
|
187
|
-
*/
|
|
188
|
-
public _keyListeners: (Listener | null)[] = [];
|
|
189
|
-
|
|
190
|
-
public addKeyListener = <T>(
|
|
191
|
-
key: string,
|
|
192
|
-
fn: (msg: VerificationData<T>) => void
|
|
193
|
-
) => {
|
|
194
|
-
this.logger(`Add key listener: ${key}`);
|
|
195
|
-
|
|
196
|
-
const newListener: Listener = {
|
|
197
|
-
key,
|
|
198
|
-
timeout: null,
|
|
199
|
-
fn,
|
|
200
|
-
};
|
|
201
|
-
this._keyListeners.push(newListener);
|
|
202
|
-
|
|
203
|
-
return this._keyListeners.length - 1;
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
public removeKeyListener = (id: number) => {
|
|
207
|
-
if (this._keyListeners[id]?.timeout) {
|
|
208
|
-
clearTimeout(this._keyListeners[id]?.timeout || undefined);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
this._keyListeners[id] = null;
|
|
212
|
-
};
|
|
213
|
-
|
|
214
|
-
public triggerKeyListener = <T = any>(
|
|
215
|
-
key: string,
|
|
216
|
-
message: VerificationData<T>
|
|
217
|
-
) => {
|
|
218
|
-
this._keyListeners.forEach((listener) => {
|
|
219
|
-
if (listener?.key === key) {
|
|
220
|
-
this.logger(`Trigger key listener: ${key}`);
|
|
221
|
-
if (listener.timeout) {
|
|
222
|
-
clearTimeout(listener.timeout);
|
|
223
|
-
}
|
|
224
|
-
listener.timeout = setTimeout(
|
|
225
|
-
() => listener.fn(message),
|
|
226
|
-
|
|
227
|
-
this.options.triggerDebouce
|
|
228
|
-
) as any;
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Custom verificators can enhance default verification on any key field
|
|
235
|
-
*/
|
|
236
|
-
public _customVerificator: (Verificator<any> | null)[] = [];
|
|
237
|
-
|
|
238
|
-
public addCustomVerification = <T = any>(
|
|
239
|
-
key: string,
|
|
240
|
-
fn: (msg: VerificationData<T>, previous: T | undefined) => Promise<boolean>
|
|
241
|
-
) => {
|
|
242
|
-
const newListener: Verificator<T> = {
|
|
243
|
-
key,
|
|
244
|
-
fn,
|
|
245
|
-
};
|
|
246
|
-
|
|
247
|
-
this._customVerificator.push(newListener);
|
|
248
|
-
return this._customVerificator.length;
|
|
249
|
-
};
|
|
250
|
-
|
|
251
|
-
public removeCustomVerification = (id: number) => {
|
|
252
|
-
this._customVerificator[id] = null;
|
|
253
|
-
};
|
|
254
|
-
|
|
255
|
-
private _options: ToolDbOptions = {
|
|
256
|
-
peers: [],
|
|
257
|
-
maxRetries: 5,
|
|
258
|
-
triggerDebouce: 100,
|
|
259
|
-
wait: 2000,
|
|
260
|
-
pow: 0,
|
|
261
|
-
server: false,
|
|
262
|
-
host: "127.0.0.1",
|
|
263
|
-
port: 8080,
|
|
264
|
-
debug: false,
|
|
265
|
-
httpServer: undefined,
|
|
266
|
-
networkAdapter: ToolDbNetworkAdapter,
|
|
267
|
-
storageAdapter: ToolDbStorageAdapter,
|
|
268
|
-
userAdapter: ToolDbUserAdapter,
|
|
269
|
-
storageName: "tooldb",
|
|
270
|
-
topic: "tool-db-default",
|
|
271
|
-
serverName: undefined,
|
|
272
|
-
ssl: false,
|
|
273
|
-
modules: {},
|
|
274
|
-
};
|
|
275
|
-
|
|
276
|
-
get options() {
|
|
277
|
-
return this._options;
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
get network() {
|
|
281
|
-
return this._network;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
get serverPeers() {
|
|
285
|
-
return this._serverPeers;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
get store() {
|
|
289
|
-
return this._store;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
get userAccount() {
|
|
293
|
-
return this._userAccount;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
get peerAccount() {
|
|
297
|
-
return this._peerAccount;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
constructor(options: Partial<ToolDbOptions> = {}) {
|
|
301
|
-
super();
|
|
302
|
-
this._options = { ...this.options, ...options };
|
|
303
|
-
|
|
304
|
-
this._store = new this.options.storageAdapter(this);
|
|
305
|
-
this._peerAccount = new this.options.userAdapter(this);
|
|
306
|
-
this._userAccount = new this.options.userAdapter(this);
|
|
307
|
-
|
|
308
|
-
this._network = new this.options.networkAdapter(this);
|
|
309
|
-
|
|
310
|
-
const DEFAULT_KEYS = "%default-peer%";
|
|
311
|
-
|
|
312
|
-
// DO NOT USE THE DEFAULT STORE FOR KEYS
|
|
313
|
-
const tempStore = new this.options.storageAdapter(
|
|
314
|
-
this,
|
|
315
|
-
"_____peer_" + this.options.storageName
|
|
316
|
-
);
|
|
317
|
-
|
|
318
|
-
tempStore
|
|
319
|
-
.get(DEFAULT_KEYS)
|
|
320
|
-
.then((val) => {
|
|
321
|
-
this.peerAccount
|
|
322
|
-
.decryptAccount(JSON.parse(val), DEFAULT_KEYS)
|
|
323
|
-
.then(async (a) => {
|
|
324
|
-
await this.peerAccount.setUser(a, randomAnimal());
|
|
325
|
-
})
|
|
326
|
-
.finally(() => {
|
|
327
|
-
this.emit("init", this.userAccount.getAddress());
|
|
328
|
-
});
|
|
329
|
-
})
|
|
330
|
-
.catch((_e) => {
|
|
331
|
-
this.peerAccount.encryptAccount(DEFAULT_KEYS).then((a) => {
|
|
332
|
-
tempStore
|
|
333
|
-
.put(DEFAULT_KEYS, JSON.stringify(a))
|
|
334
|
-
.catch(() => {
|
|
335
|
-
// nothing
|
|
336
|
-
})
|
|
337
|
-
.finally(() => {
|
|
338
|
-
this.emit("init", this.userAccount.getAddress());
|
|
339
|
-
});
|
|
340
|
-
});
|
|
341
|
-
});
|
|
342
|
-
}
|
|
343
|
-
}
|
package/lib/types/message.ts
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AllowedFunctionArguments,
|
|
3
|
-
AllowedFunctionReturn,
|
|
4
|
-
FunctionCodes,
|
|
5
|
-
Peer,
|
|
6
|
-
} from "..";
|
|
7
|
-
|
|
8
|
-
export enum VerifyResult {
|
|
9
|
-
CustomVerificationFailed = "CustomVerificationFailed",
|
|
10
|
-
InvalidData = "InvalidData",
|
|
11
|
-
InvalidVerification = "InvalidVerification",
|
|
12
|
-
CantOverwrite = "CantOverwrite",
|
|
13
|
-
InvalidTimestamp = "InvalidTimestamp",
|
|
14
|
-
AddressMismatch = "AddressMismatch",
|
|
15
|
-
NoProofOfWork = "NoProofOfWork",
|
|
16
|
-
InvalidHashNonce = "InvalidHashNonce",
|
|
17
|
-
InvalidSignature = "InvalidSignature",
|
|
18
|
-
Verified = "Verified",
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export interface VerificationData<T = any> {
|
|
22
|
-
/**
|
|
23
|
-
* Key/id
|
|
24
|
-
*/
|
|
25
|
-
k: string;
|
|
26
|
-
/**
|
|
27
|
-
* Address
|
|
28
|
-
*/
|
|
29
|
-
a: string;
|
|
30
|
-
/**
|
|
31
|
-
* Nonce
|
|
32
|
-
*/
|
|
33
|
-
n: number;
|
|
34
|
-
/**
|
|
35
|
-
* Hash of JSON.stringify(value) + nonce
|
|
36
|
-
*/
|
|
37
|
-
h: string;
|
|
38
|
-
/**
|
|
39
|
-
* Timestamp this was created
|
|
40
|
-
*/
|
|
41
|
-
t: number;
|
|
42
|
-
/**
|
|
43
|
-
* Signature
|
|
44
|
-
*/
|
|
45
|
-
s: string;
|
|
46
|
-
/**
|
|
47
|
-
* Value
|
|
48
|
-
*/
|
|
49
|
-
v: T;
|
|
50
|
-
/**
|
|
51
|
-
* CRDT type (null for regular values)
|
|
52
|
-
*/
|
|
53
|
-
c: string | null;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export type MessageType =
|
|
57
|
-
| "ping"
|
|
58
|
-
| "pong"
|
|
59
|
-
| "query"
|
|
60
|
-
| "queryAck"
|
|
61
|
-
| "function"
|
|
62
|
-
| "functionReturn"
|
|
63
|
-
| "subscribe"
|
|
64
|
-
| "get"
|
|
65
|
-
| "put"
|
|
66
|
-
| "crdtPut"
|
|
67
|
-
| "crdtGet"
|
|
68
|
-
| "crdt";
|
|
69
|
-
|
|
70
|
-
export interface BaseMessage {
|
|
71
|
-
type: MessageType;
|
|
72
|
-
/**
|
|
73
|
-
* Unique random id for the message, to ack back
|
|
74
|
-
*/
|
|
75
|
-
id: string;
|
|
76
|
-
/**
|
|
77
|
-
* Who was this message sent to already
|
|
78
|
-
*/
|
|
79
|
-
to: string[];
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export interface PingMessage extends BaseMessage {
|
|
83
|
-
type: "ping";
|
|
84
|
-
isServer: boolean;
|
|
85
|
-
clientId: string;
|
|
86
|
-
peer: Peer;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export interface PongMessage extends BaseMessage {
|
|
90
|
-
type: "pong";
|
|
91
|
-
isServer: boolean;
|
|
92
|
-
clientId: string;
|
|
93
|
-
servers: Peer[];
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export interface QueryMessage extends BaseMessage {
|
|
97
|
-
type: "query";
|
|
98
|
-
key: string; // key we want to get
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export interface QueryAckMessage extends BaseMessage {
|
|
102
|
-
type: "queryAck";
|
|
103
|
-
keys: string[];
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
export interface FunctionMessage extends BaseMessage {
|
|
107
|
-
type: "function";
|
|
108
|
-
function: string;
|
|
109
|
-
args: AllowedFunctionArguments<any>;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export interface FunctionReturnMessage extends BaseMessage {
|
|
113
|
-
type: "functionReturn";
|
|
114
|
-
code: FunctionCodes;
|
|
115
|
-
return: AllowedFunctionReturn<any>;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
export interface SubscribeMessage extends BaseMessage {
|
|
119
|
-
type: "subscribe";
|
|
120
|
-
key: string;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export interface GetMessage extends BaseMessage {
|
|
124
|
-
type: "get";
|
|
125
|
-
key: string; // key we want to get
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export interface PutMessage<T = any> extends BaseMessage {
|
|
129
|
-
type: "put";
|
|
130
|
-
data: VerificationData<T>;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
export interface CrdtPutMessage<T = any> extends BaseMessage {
|
|
134
|
-
type: "crdtPut";
|
|
135
|
-
data: VerificationData<T>;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export interface CrdtGetMessage<T = any> extends BaseMessage {
|
|
139
|
-
type: "crdtGet";
|
|
140
|
-
key: string;
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
export type ToolDbMessage =
|
|
144
|
-
| PingMessage
|
|
145
|
-
| PongMessage
|
|
146
|
-
| QueryMessage
|
|
147
|
-
| QueryAckMessage
|
|
148
|
-
| FunctionMessage
|
|
149
|
-
| FunctionReturnMessage
|
|
150
|
-
| SubscribeMessage
|
|
151
|
-
| GetMessage
|
|
152
|
-
| PutMessage
|
|
153
|
-
| CrdtPutMessage
|
|
154
|
-
| CrdtGetMessage;
|
package/lib/types/tooldb.ts
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { Server as HTTPServer } from "http";
|
|
2
|
-
import { Server as HTTPSServer } from "https";
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
ToolDb,
|
|
6
|
-
ToolDbMessage,
|
|
7
|
-
ToolDbNetworkAdapter,
|
|
8
|
-
ToolDbStorageAdapter,
|
|
9
|
-
ToolDbUserAdapter,
|
|
10
|
-
} from "..";
|
|
11
|
-
|
|
12
|
-
export interface Peer {
|
|
13
|
-
topic: string;
|
|
14
|
-
timestamp: number;
|
|
15
|
-
host: string;
|
|
16
|
-
port: number;
|
|
17
|
-
address: string;
|
|
18
|
-
sig: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export type ToolDbMessageHandler = (
|
|
22
|
-
this: ToolDb,
|
|
23
|
-
message: ToolDbMessage,
|
|
24
|
-
remotePeerId: string
|
|
25
|
-
) => void;
|
|
26
|
-
|
|
27
|
-
export interface ToolDbOptions {
|
|
28
|
-
/**
|
|
29
|
-
* Show debug console logs
|
|
30
|
-
*/
|
|
31
|
-
debug: boolean;
|
|
32
|
-
/**
|
|
33
|
-
* Array of peers to connect to
|
|
34
|
-
*/
|
|
35
|
-
peers: { host: string; port: number }[];
|
|
36
|
-
/**
|
|
37
|
-
* Max number of tries when a connection fails
|
|
38
|
-
*/
|
|
39
|
-
maxRetries: number;
|
|
40
|
-
/**
|
|
41
|
-
* How long to wait (max) for a debounced key listener recv
|
|
42
|
-
*/
|
|
43
|
-
triggerDebouce: number;
|
|
44
|
-
/**
|
|
45
|
-
* How long to wait between retries
|
|
46
|
-
*/
|
|
47
|
-
wait: number;
|
|
48
|
-
/**
|
|
49
|
-
* Proof of work difficulty (number of leading zeroes required).
|
|
50
|
-
* Set to null to bypass POW calculations entirely (useful for testing/CI).
|
|
51
|
-
*/
|
|
52
|
-
pow: number | null;
|
|
53
|
-
/**
|
|
54
|
-
* Whether we are a server or not
|
|
55
|
-
*/
|
|
56
|
-
server: boolean;
|
|
57
|
-
/**
|
|
58
|
-
* A server instance like Express (server only)
|
|
59
|
-
*/
|
|
60
|
-
httpServer: HTTPServer | HTTPSServer | undefined;
|
|
61
|
-
/**
|
|
62
|
-
* Our hostname (server only)
|
|
63
|
-
*/
|
|
64
|
-
host: string;
|
|
65
|
-
/**
|
|
66
|
-
* Port to listen incoming connections (server only, default is 8080)
|
|
67
|
-
*/
|
|
68
|
-
port: number;
|
|
69
|
-
/**
|
|
70
|
-
* If we are listening over SSL or not
|
|
71
|
-
*/
|
|
72
|
-
ssl: boolean;
|
|
73
|
-
/**
|
|
74
|
-
* Our storage namespace (default is "tooldb")
|
|
75
|
-
*/
|
|
76
|
-
storageName: string;
|
|
77
|
-
/**
|
|
78
|
-
* A custom network adapter class
|
|
79
|
-
*/
|
|
80
|
-
networkAdapter: typeof ToolDbNetworkAdapter;
|
|
81
|
-
/**
|
|
82
|
-
* A custom storage adapter function
|
|
83
|
-
*/
|
|
84
|
-
storageAdapter: typeof ToolDbStorageAdapter;
|
|
85
|
-
/**
|
|
86
|
-
* A custom user storage and validation adapter class
|
|
87
|
-
*/
|
|
88
|
-
userAdapter: typeof ToolDbUserAdapter;
|
|
89
|
-
/**
|
|
90
|
-
* The namespace/topic of our app (default is "tool-db-default")
|
|
91
|
-
*/
|
|
92
|
-
topic: string;
|
|
93
|
-
/*
|
|
94
|
-
* The name of the server (works like a domain)
|
|
95
|
-
*/
|
|
96
|
-
serverName: string | undefined;
|
|
97
|
-
/*
|
|
98
|
-
* Used only for modules extra options
|
|
99
|
-
* Ideally should be uesd as an internal key within
|
|
100
|
-
* like "db.options.hybrid.url" to avoid conflicts between modules using the same keys
|
|
101
|
-
*/
|
|
102
|
-
modules: {
|
|
103
|
-
[module: string]: any;
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export type GenericObject = { [key: string]: any };
|
|
108
|
-
|
|
109
|
-
export type AllowedFunctionArguments<A = GenericObject> = A;
|
|
110
|
-
|
|
111
|
-
export type AllowedFunctionReturn<R = unknown> = R;
|
|
112
|
-
|
|
113
|
-
export type FunctionCodes = "OK" | "ERR" | "NOT_FOUND";
|
|
114
|
-
|
|
115
|
-
export type ServerFunction<R, A = GenericObject> = (
|
|
116
|
-
args: AllowedFunctionArguments<A>
|
|
117
|
-
) => Promise<AllowedFunctionReturn<R>>;
|
|
118
|
-
|
|
119
|
-
export interface FunctionReturnBase {
|
|
120
|
-
code: FunctionCodes;
|
|
121
|
-
}
|
|
122
|
-
export interface FunctionReturnOk<R> extends FunctionReturnBase {
|
|
123
|
-
return: AllowedFunctionReturn<R>;
|
|
124
|
-
code: "OK";
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export interface FunctionReturnErr extends FunctionReturnBase {
|
|
128
|
-
return: string;
|
|
129
|
-
code: "ERR";
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export interface FunctionReturnNotFound extends FunctionReturnBase {
|
|
133
|
-
return: string;
|
|
134
|
-
code: "NOT_FOUND";
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
export type FunctionReturn<R> =
|
|
138
|
-
| FunctionReturnOk<R>
|
|
139
|
-
| FunctionReturnErr
|
|
140
|
-
| FunctionReturnNotFound;
|
package/lib/utils/catchReturn.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
export default function arrayBufferToHex(buffer: ArrayBuffer) {
|
|
2
|
-
const data_view = new DataView(buffer);
|
|
3
|
-
let iii;
|
|
4
|
-
let len;
|
|
5
|
-
let hex = "";
|
|
6
|
-
let c;
|
|
7
|
-
|
|
8
|
-
for (iii = 0, len = data_view.byteLength; iii < len; iii += 1) {
|
|
9
|
-
c = data_view.getUint8(iii).toString(16);
|
|
10
|
-
if (c.length < 2) {
|
|
11
|
-
c = "0" + c;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
hex += c;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return hex.toLowerCase();
|
|
18
|
-
}
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import fromBase64 from "./fromBase64";
|
|
2
|
-
|
|
3
|
-
export default function base64ToUint8(based: string): Uint8Array {
|
|
4
|
-
const str = fromBase64(based);
|
|
5
|
-
|
|
6
|
-
const buf = new ArrayBuffer(str.length);
|
|
7
|
-
const bufView = new Uint8Array(buf);
|
|
8
|
-
for (let i = 0, strLen = str.length; i < strLen; i += 1) {
|
|
9
|
-
bufView[i] = str.charCodeAt(i);
|
|
10
|
-
}
|
|
11
|
-
return bufView;
|
|
12
|
-
}
|
|
13
|
-
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export default function hexToArrayBuffer(hex: string): ArrayBuffer {
|
|
2
|
-
const pairs = hex.toUpperCase().match(/[\dA-F]{2}/gi);
|
|
3
|
-
|
|
4
|
-
if (!pairs) return new Uint8Array();
|
|
5
|
-
|
|
6
|
-
// convert the octets to integers
|
|
7
|
-
const integers = pairs.map((s) => {
|
|
8
|
-
return parseInt(s, 16);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
const array = new Uint8Array(integers);
|
|
12
|
-
return array.buffer;
|
|
13
|
-
}
|