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/dist/tooldb.d.ts
CHANGED
|
@@ -11,6 +11,7 @@ import toolDbFunction from "./toolDbFunction";
|
|
|
11
11
|
import toolDbAnonSignIn from "./toolDbAnonSignIn";
|
|
12
12
|
import toolDbClientOnMessage from "./toolDbClientOnMessage";
|
|
13
13
|
import toolDbVerificationWrapper from "./toolDbVerificationWrapper";
|
|
14
|
+
import toolDbCheckSignupConflicts from "./toolDbCheckSignupConflicts";
|
|
14
15
|
import toolDbQueryKeys from "./toolDbQueryKeys";
|
|
15
16
|
import toolDbKeysSignIn from "./toolDbKeysSignIn";
|
|
16
17
|
import toolDbSubscribe from "./toolDbSubscribe";
|
|
@@ -36,9 +37,11 @@ interface Verificator<T> {
|
|
|
36
37
|
export default class ToolDb extends EventEmitter {
|
|
37
38
|
private _network;
|
|
38
39
|
private _store;
|
|
40
|
+
private _peerStore;
|
|
39
41
|
private _serverPeers;
|
|
40
42
|
private _peerAccount;
|
|
41
43
|
private _userAccount;
|
|
44
|
+
private _initPromise;
|
|
42
45
|
logger: typeof logger;
|
|
43
46
|
clientOnMessage: typeof toolDbClientOnMessage;
|
|
44
47
|
verifyMessage: typeof verifyMessage;
|
|
@@ -71,6 +74,7 @@ export default class ToolDb extends EventEmitter {
|
|
|
71
74
|
keysSignIn: typeof toolDbKeysSignIn;
|
|
72
75
|
signUp: typeof toolDbSignUp;
|
|
73
76
|
verify: typeof toolDbVerificationWrapper;
|
|
77
|
+
_checkSignupConflicts: typeof toolDbCheckSignupConflicts;
|
|
74
78
|
handlePing: typeof handlePing;
|
|
75
79
|
handlePong: typeof handlePong;
|
|
76
80
|
handleCrdtGet: typeof handleCrdtGet;
|
|
@@ -118,6 +122,20 @@ export default class ToolDb extends EventEmitter {
|
|
|
118
122
|
get store(): ToolDbStorageAdapter;
|
|
119
123
|
get userAccount(): ToolDbUserAdapter;
|
|
120
124
|
get peerAccount(): ToolDbUserAdapter;
|
|
125
|
+
/**
|
|
126
|
+
* Promise that resolves when the database is fully initialized.
|
|
127
|
+
* Use this to wait for initialization before using the database.
|
|
128
|
+
*/
|
|
129
|
+
get ready(): Promise<void>;
|
|
121
130
|
constructor(options?: Partial<ToolDbOptions>);
|
|
131
|
+
/**
|
|
132
|
+
* Initialize peer and user accounts from storage or create new ones.
|
|
133
|
+
*/
|
|
134
|
+
private _initializeAccounts;
|
|
135
|
+
/**
|
|
136
|
+
* Properly closes all stores to prevent resource leaks.
|
|
137
|
+
* Call this when you're done with the database instance.
|
|
138
|
+
*/
|
|
139
|
+
close(): Promise<void>;
|
|
122
140
|
}
|
|
123
141
|
export {};
|
package/dist/tooldb.js
CHANGED
|
@@ -40,7 +40,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
40
40
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
41
41
|
function step(op) {
|
|
42
42
|
if (f) throw new TypeError("Generator is already executing.");
|
|
43
|
-
while (_) try {
|
|
43
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
44
44
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
45
45
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
46
46
|
switch (op[0]) {
|
|
@@ -77,6 +77,7 @@ var toolDbFunction_1 = __importDefault(require("./toolDbFunction"));
|
|
|
77
77
|
var toolDbAnonSignIn_1 = __importDefault(require("./toolDbAnonSignIn"));
|
|
78
78
|
var toolDbClientOnMessage_1 = __importDefault(require("./toolDbClientOnMessage"));
|
|
79
79
|
var toolDbVerificationWrapper_1 = __importDefault(require("./toolDbVerificationWrapper"));
|
|
80
|
+
var toolDbCheckSignupConflicts_1 = __importDefault(require("./toolDbCheckSignupConflicts"));
|
|
80
81
|
var toolDbQueryKeys_1 = __importDefault(require("./toolDbQueryKeys"));
|
|
81
82
|
var toolDbKeysSignIn_1 = __importDefault(require("./toolDbKeysSignIn"));
|
|
82
83
|
var toolDbSubscribe_1 = __importDefault(require("./toolDbSubscribe"));
|
|
@@ -133,6 +134,7 @@ var ToolDb = /** @class */ (function (_super) {
|
|
|
133
134
|
_this.keysSignIn = toolDbKeysSignIn_1.default;
|
|
134
135
|
_this.signUp = toolDbSignUp_1.default;
|
|
135
136
|
_this.verify = toolDbVerificationWrapper_1.default;
|
|
137
|
+
_this._checkSignupConflicts = toolDbCheckSignupConflicts_1.default;
|
|
136
138
|
// All message handlers go here
|
|
137
139
|
_this.handlePing = handlePing_1.default;
|
|
138
140
|
_this.handlePong = handlePong_1.default;
|
|
@@ -233,40 +235,17 @@ var ToolDb = /** @class */ (function (_super) {
|
|
|
233
235
|
_this._store = new _this.options.storageAdapter(_this);
|
|
234
236
|
_this._peerAccount = new _this.options.userAdapter(_this);
|
|
235
237
|
_this._userAccount = new _this.options.userAdapter(_this);
|
|
238
|
+
// Create network adapter synchronously so it can listen to "init" event
|
|
239
|
+
// The network adapter will wait for the "init" event before doing anything critical
|
|
236
240
|
_this._network = new _this.options.networkAdapter(_this);
|
|
237
241
|
var DEFAULT_KEYS = "%default-peer%";
|
|
242
|
+
var DEFAULT_USER_KEYS = "%default-user%";
|
|
238
243
|
// DO NOT USE THE DEFAULT STORE FOR KEYS
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
.
|
|
243
|
-
|
|
244
|
-
.decryptAccount(JSON.parse(val), DEFAULT_KEYS)
|
|
245
|
-
.then(function (a) { return __awaiter(_this, void 0, void 0, function () {
|
|
246
|
-
return __generator(this, function (_a) {
|
|
247
|
-
switch (_a.label) {
|
|
248
|
-
case 0: return [4 /*yield*/, this.peerAccount.setUser(a, (0, _1.randomAnimal)())];
|
|
249
|
-
case 1:
|
|
250
|
-
_a.sent();
|
|
251
|
-
return [2 /*return*/];
|
|
252
|
-
}
|
|
253
|
-
});
|
|
254
|
-
}); })
|
|
255
|
-
.finally(function () {
|
|
256
|
-
_this.emit("init", _this.userAccount.getAddress());
|
|
257
|
-
});
|
|
258
|
-
})
|
|
259
|
-
.catch(function (_e) {
|
|
260
|
-
_this.peerAccount.encryptAccount(DEFAULT_KEYS).then(function (a) {
|
|
261
|
-
tempStore
|
|
262
|
-
.put(DEFAULT_KEYS, JSON.stringify(a))
|
|
263
|
-
.catch(function () {
|
|
264
|
-
// nothing
|
|
265
|
-
})
|
|
266
|
-
.finally(function () {
|
|
267
|
-
_this.emit("init", _this.userAccount.getAddress());
|
|
268
|
-
});
|
|
269
|
-
});
|
|
244
|
+
_this._peerStore = new _this.options.storageAdapter(_this, "_____peer_" + _this.options.storageName);
|
|
245
|
+
// Initialize accounts - this will emit "init" event when done
|
|
246
|
+
_this._initPromise = _this._initializeAccounts(_this._peerStore, DEFAULT_KEYS, DEFAULT_USER_KEYS).catch(function (err) {
|
|
247
|
+
_this.logger("Failed to initialize ToolDb", err);
|
|
248
|
+
throw err;
|
|
270
249
|
});
|
|
271
250
|
return _this;
|
|
272
251
|
}
|
|
@@ -347,6 +326,115 @@ var ToolDb = /** @class */ (function (_super) {
|
|
|
347
326
|
enumerable: false,
|
|
348
327
|
configurable: true
|
|
349
328
|
});
|
|
329
|
+
Object.defineProperty(ToolDb.prototype, "ready", {
|
|
330
|
+
/**
|
|
331
|
+
* Promise that resolves when the database is fully initialized.
|
|
332
|
+
* Use this to wait for initialization before using the database.
|
|
333
|
+
*/
|
|
334
|
+
get: function () {
|
|
335
|
+
return this._initPromise;
|
|
336
|
+
},
|
|
337
|
+
enumerable: false,
|
|
338
|
+
configurable: true
|
|
339
|
+
});
|
|
340
|
+
/**
|
|
341
|
+
* Initialize peer and user accounts from storage or create new ones.
|
|
342
|
+
*/
|
|
343
|
+
ToolDb.prototype._initializeAccounts = function (tempStore, peerKey, userKey) {
|
|
344
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
345
|
+
var val, account, _e_1, encrypted, _putError_1, userVal, userAccount, userName, _e_2;
|
|
346
|
+
return __generator(this, function (_a) {
|
|
347
|
+
switch (_a.label) {
|
|
348
|
+
case 0:
|
|
349
|
+
_a.trys.push([0, 4, , 11]);
|
|
350
|
+
return [4 /*yield*/, tempStore.get(peerKey)];
|
|
351
|
+
case 1:
|
|
352
|
+
val = _a.sent();
|
|
353
|
+
return [4 /*yield*/, this.peerAccount.decryptAccount(JSON.parse(val), peerKey)];
|
|
354
|
+
case 2:
|
|
355
|
+
account = _a.sent();
|
|
356
|
+
return [4 /*yield*/, this.peerAccount.setUser(account, (0, _1.randomAnimal)())];
|
|
357
|
+
case 3:
|
|
358
|
+
_a.sent();
|
|
359
|
+
return [3 /*break*/, 11];
|
|
360
|
+
case 4:
|
|
361
|
+
_e_1 = _a.sent();
|
|
362
|
+
// No peer account found, create anonymous peer account
|
|
363
|
+
return [4 /*yield*/, this.peerAccount.anonUser()];
|
|
364
|
+
case 5:
|
|
365
|
+
// No peer account found, create anonymous peer account
|
|
366
|
+
_a.sent();
|
|
367
|
+
return [4 /*yield*/, this.peerAccount.encryptAccount(peerKey)];
|
|
368
|
+
case 6:
|
|
369
|
+
encrypted = _a.sent();
|
|
370
|
+
_a.label = 7;
|
|
371
|
+
case 7:
|
|
372
|
+
_a.trys.push([7, 9, , 10]);
|
|
373
|
+
return [4 /*yield*/, tempStore.put(peerKey, JSON.stringify(encrypted))];
|
|
374
|
+
case 8:
|
|
375
|
+
_a.sent();
|
|
376
|
+
return [3 /*break*/, 10];
|
|
377
|
+
case 9:
|
|
378
|
+
_putError_1 = _a.sent();
|
|
379
|
+
return [3 /*break*/, 10];
|
|
380
|
+
case 10: return [3 /*break*/, 11];
|
|
381
|
+
case 11:
|
|
382
|
+
_a.trys.push([11, 15, , 17]);
|
|
383
|
+
return [4 /*yield*/, tempStore.get(userKey)];
|
|
384
|
+
case 12:
|
|
385
|
+
userVal = _a.sent();
|
|
386
|
+
return [4 /*yield*/, this.userAccount.decryptAccount(JSON.parse(userVal), userKey)];
|
|
387
|
+
case 13:
|
|
388
|
+
userAccount = _a.sent();
|
|
389
|
+
userName = (userAccount === null || userAccount === void 0 ? void 0 : userAccount.name) ||
|
|
390
|
+
this.userAccount.getUsername() ||
|
|
391
|
+
(0, _1.randomAnimal)();
|
|
392
|
+
return [4 /*yield*/, this.userAccount.setUser(userAccount, userName)];
|
|
393
|
+
case 14:
|
|
394
|
+
_a.sent();
|
|
395
|
+
return [3 /*break*/, 17];
|
|
396
|
+
case 15:
|
|
397
|
+
_e_2 = _a.sent();
|
|
398
|
+
// No saved user, generate anonymous user
|
|
399
|
+
return [4 /*yield*/, this.userAccount.anonUser()];
|
|
400
|
+
case 16:
|
|
401
|
+
// No saved user, generate anonymous user
|
|
402
|
+
_a.sent();
|
|
403
|
+
return [3 /*break*/, 17];
|
|
404
|
+
case 17:
|
|
405
|
+
// Emit init event after both accounts are ready
|
|
406
|
+
this.emit("init", this.userAccount.getAddress());
|
|
407
|
+
return [2 /*return*/];
|
|
408
|
+
}
|
|
409
|
+
});
|
|
410
|
+
});
|
|
411
|
+
};
|
|
412
|
+
/**
|
|
413
|
+
* Properly closes all stores to prevent resource leaks.
|
|
414
|
+
* Call this when you're done with the database instance.
|
|
415
|
+
*/
|
|
416
|
+
ToolDb.prototype.close = function () {
|
|
417
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
418
|
+
return __generator(this, function (_a) {
|
|
419
|
+
switch (_a.label) {
|
|
420
|
+
case 0:
|
|
421
|
+
if (!(this._store && typeof this._store.close === "function")) return [3 /*break*/, 2];
|
|
422
|
+
return [4 /*yield*/, this._store.close()];
|
|
423
|
+
case 1:
|
|
424
|
+
_a.sent();
|
|
425
|
+
_a.label = 2;
|
|
426
|
+
case 2:
|
|
427
|
+
if (!(this._peerStore &&
|
|
428
|
+
typeof this._peerStore.close === "function")) return [3 /*break*/, 4];
|
|
429
|
+
return [4 /*yield*/, this._peerStore.close()];
|
|
430
|
+
case 3:
|
|
431
|
+
_a.sent();
|
|
432
|
+
_a.label = 4;
|
|
433
|
+
case 4: return [2 /*return*/];
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
});
|
|
437
|
+
};
|
|
350
438
|
return ToolDb;
|
|
351
439
|
}(events_1.default));
|
|
352
440
|
exports.default = ToolDb;
|
package/dist/tooldb.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tooldb.js","sourceRoot":"","sources":["../lib/tooldb.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAAkC;AAElC,sBAWW;AAEX,0DAAoC;AACpC,0DAAoC;AACpC,gEAA0C;AAC1C,gEAA0C;AAC1C,kEAA4C;AAC5C,kEAA4C;AAC5C,oEAA8C;AAE9C,wEAAkD;AAClD,kFAA4D;AAC5D,0FAAoE;
|
|
1
|
+
{"version":3,"file":"tooldb.js","sourceRoot":"","sources":["../lib/tooldb.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAAkC;AAElC,sBAWW;AAEX,0DAAoC;AACpC,0DAAoC;AACpC,gEAA0C;AAC1C,gEAA0C;AAC1C,kEAA4C;AAC5C,kEAA4C;AAC5C,oEAA8C;AAE9C,wEAAkD;AAClD,kFAA4D;AAC5D,0FAAoE;AACpE,4FAAsE;AAEtE,sEAAgD;AAChD,wEAAkD;AAClD,sEAAgD;AAEhD,0EAAoD;AACpD,0EAAoD;AACpD,4EAAsD;AACtD,4EAAsD;AACtD,8EAAwD;AACxD,oFAA8D;AAC9D,kFAA4D;AAC5D,kFAA4D;AAC5D,sFAAgE;AAEhE,oDAA8B;AAgB9B;IAAoC,0BAAY;IA8P9C,gBAAY,OAAoC;QAApC,wBAAA,EAAA,YAAoC;QAAhD,YACE,iBAAO,SA6BR;QAxRO,kBAAY,G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EM,CAAA,IAAI,CAAC,MAAM,IAAI,OAAQ,IAAI,CAAC,MAAc,CAAC,KAAK,KAAK,UAAU,CAAA,EAA/D,wBAA+D;wBACjE,qBAAO,IAAI,CAAC,MAAc,CAAC,KAAK,EAAE,EAAA;;wBAAlC,SAAkC,CAAC;;;6BAInC,CAAA,IAAI,CAAC,UAAU;4BACf,OAAQ,IAAI,CAAC,UAAkB,CAAC,KAAK,KAAK,UAAU,CAAA,EADpD,wBACoD;wBAEpD,qBAAO,IAAI,CAAC,UAAkB,CAAC,KAAK,EAAE,EAAA;;wBAAtC,SAAsC,CAAC;;;;;;KAE1C;IACH,aAAC;AAAD,CAAC,AAhWD,CAAoC,gBAAY,GAgW/C"}
|
package/dist/types/message.d.ts
CHANGED
|
@@ -45,7 +45,7 @@ export interface VerificationData<T = any> {
|
|
|
45
45
|
*/
|
|
46
46
|
c: string | null;
|
|
47
47
|
}
|
|
48
|
-
export
|
|
48
|
+
export type MessageType = "ping" | "pong" | "query" | "queryAck" | "function" | "functionReturn" | "subscribe" | "get" | "put" | "crdtPut" | "crdtGet" | "crdt";
|
|
49
49
|
export interface BaseMessage {
|
|
50
50
|
type: MessageType;
|
|
51
51
|
/**
|
|
@@ -107,4 +107,4 @@ export interface CrdtGetMessage<T = any> extends BaseMessage {
|
|
|
107
107
|
type: "crdtGet";
|
|
108
108
|
key: string;
|
|
109
109
|
}
|
|
110
|
-
export
|
|
110
|
+
export type ToolDbMessage = PingMessage | PongMessage | QueryMessage | QueryAckMessage | FunctionMessage | FunctionReturnMessage | SubscribeMessage | GetMessage | PutMessage | CrdtPutMessage | CrdtGetMessage;
|
package/dist/types/tooldb.d.ts
CHANGED
|
@@ -11,7 +11,7 @@ export interface Peer {
|
|
|
11
11
|
address: string;
|
|
12
12
|
sig: string;
|
|
13
13
|
}
|
|
14
|
-
export
|
|
14
|
+
export type ToolDbMessageHandler = (this: ToolDb, message: ToolDbMessage, remotePeerId: string) => void;
|
|
15
15
|
export interface ToolDbOptions {
|
|
16
16
|
/**
|
|
17
17
|
* Show debug console logs
|
|
@@ -86,13 +86,13 @@ export interface ToolDbOptions {
|
|
|
86
86
|
[module: string]: any;
|
|
87
87
|
};
|
|
88
88
|
}
|
|
89
|
-
export
|
|
89
|
+
export type GenericObject = {
|
|
90
90
|
[key: string]: any;
|
|
91
91
|
};
|
|
92
|
-
export
|
|
93
|
-
export
|
|
94
|
-
export
|
|
95
|
-
export
|
|
92
|
+
export type AllowedFunctionArguments<A = GenericObject> = A;
|
|
93
|
+
export type AllowedFunctionReturn<R = unknown> = R;
|
|
94
|
+
export type FunctionCodes = "OK" | "ERR" | "NOT_FOUND";
|
|
95
|
+
export type ServerFunction<R, A = GenericObject> = (args: AllowedFunctionArguments<A>) => Promise<AllowedFunctionReturn<R>>;
|
|
96
96
|
export interface FunctionReturnBase {
|
|
97
97
|
code: FunctionCodes;
|
|
98
98
|
}
|
|
@@ -108,4 +108,4 @@ export interface FunctionReturnNotFound extends FunctionReturnBase {
|
|
|
108
108
|
return: string;
|
|
109
109
|
code: "NOT_FOUND";
|
|
110
110
|
}
|
|
111
|
-
export
|
|
111
|
+
export type FunctionReturn<R> = FunctionReturnOk<R> | FunctionReturnErr | FunctionReturnNotFound;
|
|
@@ -14,7 +14,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
14
14
|
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
15
|
function step(op) {
|
|
16
16
|
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
18
|
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
19
|
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
20
|
switch (op[0]) {
|
|
@@ -87,8 +87,16 @@ function verifyMessage(msg, pow) {
|
|
|
87
87
|
})];
|
|
88
88
|
case 1:
|
|
89
89
|
data = _b.sent();
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
// If data exists with a different author, compare timestamps
|
|
91
|
+
// The OLDER timestamp wins (first writer owns forever)
|
|
92
|
+
if (data && data.a !== msg.a) {
|
|
93
|
+
if (data.t <= msg.t) {
|
|
94
|
+
// Local data is older or same age - reject incoming
|
|
95
|
+
return [2 /*return*/, __1.VerifyResult.CantOverwrite];
|
|
96
|
+
}
|
|
97
|
+
// Incoming data is older - let it through to handlePut
|
|
98
|
+
// where it will replace our local data
|
|
99
|
+
}
|
|
92
100
|
_b.label = 2;
|
|
93
101
|
case 2:
|
|
94
102
|
if (addressNamespace && addressNamespace !== msg.a) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verifyMessage.js","sourceRoot":"","sources":["../../lib/utils/verifyMessage.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,wBAAoE;AAEpE;;;;;GAKG;AACH,SAA8B,aAAa,CAEzC,GAAiC,EACjC,GAAsB;IAAtB,oBAAA,EAAA,OAAsB;;;;;;oBAGhB,OAAO,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;oBAEtC,IACE,GAAG,CAAC,CAAC,KAAK,SAAS;wBACnB,GAAG,CAAC,CAAC,KAAK,SAAS;wBACnB,GAAG,CAAC,CAAC,KAAK,SAAS;wBACnB,GAAG,CAAC,CAAC,KAAK,SAAS;wBACnB,GAAG,CAAC,CAAC,KAAK,SAAS;wBACnB,GAAG,CAAC,CAAC,KAAK,SAAS,EACnB;wBACA,sBAAO,gBAAY,CAAC,WAAW,EAAC;qBACjC;oBAED,yCAAyC;oBACzC,kEAAkE;oBAClE,IAAI,GAAG,CAAC,CAAC,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,GAAG,KAAK,EAAE;wBACxC,6CAA6C;wBAC7C,sBAAO,gBAAY,CAAC,gBAA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AyFC"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tool-db",
|
|
3
|
-
"version": "2.6.
|
|
3
|
+
"version": "2.6.6",
|
|
4
4
|
"description": "A decentralized database model for federated p2p networks.n",
|
|
5
5
|
"author": "Manwe <manuel.etchegaray7@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/Manwe-777/tool-db#readme",
|
|
7
|
-
"license": "
|
|
7
|
+
"license": "Apache-2.0",
|
|
8
8
|
"main": "dist/index.js",
|
|
9
9
|
"types": "dist/index.d.ts",
|
|
10
10
|
"engines": {
|
|
@@ -28,5 +28,5 @@
|
|
|
28
28
|
"typescript": "^4.7.4",
|
|
29
29
|
"uglify-js": "^3.16.2"
|
|
30
30
|
},
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "4f705c44c8e4e48e43724774f9fee6250587a9b2"
|
|
32
32
|
}
|
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ToolDb,
|
|
3
|
-
PingMessage,
|
|
4
|
-
ToolDbMessage,
|
|
5
|
-
Peer,
|
|
6
|
-
getPeerSignature,
|
|
7
|
-
textRandom,
|
|
8
|
-
uniq,
|
|
9
|
-
} from "..";
|
|
10
|
-
|
|
11
|
-
export default class ToolDbNetworkAdapter {
|
|
12
|
-
private _clientToSend: Record<string, (message: string) => void> = {};
|
|
13
|
-
|
|
14
|
-
private _isClientConnected: Record<string, () => boolean> = {};
|
|
15
|
-
|
|
16
|
-
private _clientIsServer: Record<string, boolean> = {};
|
|
17
|
-
|
|
18
|
-
private _tooldb: ToolDb;
|
|
19
|
-
|
|
20
|
-
constructor(db: ToolDb) {
|
|
21
|
-
this._tooldb = db;
|
|
22
|
-
|
|
23
|
-
// Wait for peerAccount to be initialized before adding ourselves to serverPeers
|
|
24
|
-
this.tooldb.once("init", () => {
|
|
25
|
-
if (this.tooldb.options.server) {
|
|
26
|
-
this.getMeAsPeer().then((meAsPeer) => {
|
|
27
|
-
this.tooldb.serverPeers.push(meAsPeer);
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
get clientToSend() {
|
|
34
|
-
return this._clientToSend;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
get isClientConnected() {
|
|
38
|
-
return this._isClientConnected;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
get tooldb() {
|
|
42
|
-
return this._tooldb;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public getMeAsPeer() {
|
|
46
|
-
const timestamp = new Date().getTime();
|
|
47
|
-
return getPeerSignature(
|
|
48
|
-
this.tooldb.peerAccount,
|
|
49
|
-
this.tooldb.options.topic,
|
|
50
|
-
timestamp,
|
|
51
|
-
this.tooldb.options.host,
|
|
52
|
-
this.tooldb.options.port
|
|
53
|
-
).then((signature) => {
|
|
54
|
-
return {
|
|
55
|
-
topic: this.tooldb.options.topic,
|
|
56
|
-
timestamp: timestamp,
|
|
57
|
-
host: this.tooldb.options.host,
|
|
58
|
-
port: this.tooldb.options.port,
|
|
59
|
-
address: this.tooldb.peerAccount.getAddress() || "",
|
|
60
|
-
sig: signature,
|
|
61
|
-
} as Peer;
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Check if the specified client is connected or not
|
|
67
|
-
* @param clientId Client ID
|
|
68
|
-
* @returns boolean
|
|
69
|
-
*/
|
|
70
|
-
public isConnected(clientId: string) {
|
|
71
|
-
return this._isClientConnected[clientId]
|
|
72
|
-
? this._isClientConnected[clientId]()
|
|
73
|
-
: false;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Check if the specified client is a server/relay
|
|
78
|
-
* @param clientId Client ID
|
|
79
|
-
* @returns boolean
|
|
80
|
-
*/
|
|
81
|
-
public isServer(clientId: string) {
|
|
82
|
-
return this._clientIsServer[clientId] || false;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
public craftPingMessage() {
|
|
86
|
-
return this.getMeAsPeer().then((meAsPeer) => {
|
|
87
|
-
return JSON.stringify({
|
|
88
|
-
type: "ping",
|
|
89
|
-
clientId: this.getClientAddress(),
|
|
90
|
-
to: [this.getClientAddress()],
|
|
91
|
-
isServer: this.tooldb.options.server,
|
|
92
|
-
id: textRandom(10),
|
|
93
|
-
peer: meAsPeer,
|
|
94
|
-
} as PingMessage);
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Execute the function to send a message to the specified client ID
|
|
100
|
-
* @param clientId Client ID
|
|
101
|
-
* @param message Message
|
|
102
|
-
*/
|
|
103
|
-
private executeSendToClient(clientId: string, message: string) {
|
|
104
|
-
if (this._clientToSend[clientId]) {
|
|
105
|
-
this._clientToSend[clientId](message);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
public getClientAddress() {
|
|
110
|
-
// This is not a good idea to use on all adapters, so it should be replaced
|
|
111
|
-
// if its causing issues. The only reason we use the last 20 chars is to
|
|
112
|
-
// muse the same peer address as the webrtc adapter.
|
|
113
|
-
return (this.tooldb.peerAccount.getAddress() || "").slice(-20);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
public onClientDisconnect(clientId: string) {
|
|
117
|
-
delete this._clientToSend[clientId];
|
|
118
|
-
delete this._clientIsServer[clientId];
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Should be called as a message payload handler
|
|
123
|
-
* This function will take care of processing the messages and also making sure
|
|
124
|
-
* we use the correct response methods troughout the adapter.
|
|
125
|
-
* @param message message payload
|
|
126
|
-
* @param clientId Client ID (can be null for ping/pong)
|
|
127
|
-
* @param setClientId Callback to set the client id on the parent class
|
|
128
|
-
*/
|
|
129
|
-
public onClientMessage(
|
|
130
|
-
message: string,
|
|
131
|
-
clientId: string | null,
|
|
132
|
-
setClientId: (clientId: string) => void
|
|
133
|
-
) {
|
|
134
|
-
// this.tooldb.logger("onClientMessage", clientId);
|
|
135
|
-
|
|
136
|
-
if (clientId && !this.tooldb.processedOutHashes[clientId]) {
|
|
137
|
-
this.tooldb.processedOutHashes[clientId] = [];
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
try {
|
|
141
|
-
const parsedMessage = JSON.parse(message) as ToolDbMessage;
|
|
142
|
-
// We assume the first messages to arrive will always be ping or pong.
|
|
143
|
-
// Only after that we can get the client id for this socket.
|
|
144
|
-
if (parsedMessage.type === "ping" || parsedMessage.type === "pong") {
|
|
145
|
-
const cid = parsedMessage.clientId;
|
|
146
|
-
setClientId(cid);
|
|
147
|
-
this.tooldb.onPeerConnect(cid);
|
|
148
|
-
this._clientIsServer[cid] = parsedMessage.isServer;
|
|
149
|
-
this.tooldb.processedOutHashes[cid] = [];
|
|
150
|
-
this.tooldb.clientOnMessage(parsedMessage, cid);
|
|
151
|
-
} else if (clientId) {
|
|
152
|
-
this.tooldb.clientOnMessage(parsedMessage, clientId);
|
|
153
|
-
}
|
|
154
|
-
} catch (e) {
|
|
155
|
-
this.tooldb.logger("Got message ERR", message);
|
|
156
|
-
this.tooldb.logger(e);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Sends a message to all peers connected to us
|
|
162
|
-
* This function takes care of the message deduplication, making sure we dont send
|
|
163
|
-
* the same message twice to the same peer.
|
|
164
|
-
* @param msg message data
|
|
165
|
-
* @param crossServerOnly If this message should be send to server peers only
|
|
166
|
-
* @param isRelay if we should relay this message
|
|
167
|
-
*/
|
|
168
|
-
public sendToAll(msg: ToolDbMessage, crossServerOnly = false) {
|
|
169
|
-
const to = uniq([...msg.to, this.getClientAddress()]);
|
|
170
|
-
|
|
171
|
-
const finalMessage = JSON.stringify({ ...msg, to });
|
|
172
|
-
|
|
173
|
-
const filteredConns = Object.keys(this.clientToSend)
|
|
174
|
-
.filter((id) => !to.includes(id))
|
|
175
|
-
.filter((clientId) => this.isConnected(clientId));
|
|
176
|
-
|
|
177
|
-
filteredConns.forEach((clientId) => {
|
|
178
|
-
if ((crossServerOnly && this.isServer(clientId)) || !crossServerOnly) {
|
|
179
|
-
this.tooldb.logger(to, "Sent out to:", clientId);
|
|
180
|
-
|
|
181
|
-
if (msg.type === "put" || msg.type === "crdtPut") {
|
|
182
|
-
if (!this.tooldb.processedOutHashes[clientId].includes(msg.data.h)) {
|
|
183
|
-
this.executeSendToClient(clientId, finalMessage);
|
|
184
|
-
this.tooldb.processedOutHashes[clientId].push(msg.data.h);
|
|
185
|
-
}
|
|
186
|
-
} else {
|
|
187
|
-
this.executeSendToClient(clientId, finalMessage);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
// } else {
|
|
191
|
-
// this.tooldb.logger("Fitlered out;", clientId);
|
|
192
|
-
// }
|
|
193
|
-
});
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Sends a message to a single peer.
|
|
198
|
-
* This function also takes care of the message deduplication.
|
|
199
|
-
* @param clientId Peer/Client id we want to send to.
|
|
200
|
-
* @param msg message data
|
|
201
|
-
*/
|
|
202
|
-
public sendToClientId(clientId: string, msg: ToolDbMessage) {
|
|
203
|
-
const to = uniq([...msg.to, this.getClientAddress()]);
|
|
204
|
-
const finalMessage = JSON.stringify({ ...msg, to });
|
|
205
|
-
|
|
206
|
-
if (msg.type === "put" || msg.type === "crdtPut") {
|
|
207
|
-
if (
|
|
208
|
-
clientId &&
|
|
209
|
-
!this.tooldb.processedOutHashes[clientId].includes(msg.data.h)
|
|
210
|
-
) {
|
|
211
|
-
this.executeSendToClient(clientId, finalMessage);
|
|
212
|
-
this.tooldb.processedOutHashes[clientId].push(msg.data.h);
|
|
213
|
-
}
|
|
214
|
-
} else {
|
|
215
|
-
this.executeSendToClient(clientId, finalMessage);
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { ToolDb } from "..";
|
|
2
|
-
|
|
3
|
-
export default class ToolDbStorageAdapter {
|
|
4
|
-
private _tooldb: ToolDb;
|
|
5
|
-
|
|
6
|
-
private _forceStorageName: string | undefined;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Promise that resolves when the storage adapter is ready for operations.
|
|
10
|
-
* Subclasses should set this in their constructor.
|
|
11
|
-
*/
|
|
12
|
-
protected _readyPromise: Promise<void> = Promise.resolve();
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Public getter for the ready promise.
|
|
16
|
-
* Await this before performing operations if you need to ensure the store is ready.
|
|
17
|
-
*/
|
|
18
|
-
get ready(): Promise<void> {
|
|
19
|
-
return this._readyPromise;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
get tooldb() {
|
|
23
|
-
return this._tooldb;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
get storageName() {
|
|
27
|
-
return this._forceStorageName || this._tooldb.options.storageName;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
constructor(db: ToolDb, forceStorageName?: string) {
|
|
31
|
-
this._tooldb = db;
|
|
32
|
-
|
|
33
|
-
if (forceStorageName) {
|
|
34
|
-
this._forceStorageName = forceStorageName;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Wait for the storage adapter to be ready.
|
|
40
|
-
* Called internally by put/get/query operations.
|
|
41
|
-
*/
|
|
42
|
-
protected async waitForReady(): Promise<void> {
|
|
43
|
-
await this._readyPromise;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
put(key: string, data: string): Promise<unknown> {
|
|
47
|
-
return Promise.resolve();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
get(key: string): Promise<string> {
|
|
51
|
-
return Promise.resolve("");
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
query(key: string): Promise<string[]> {
|
|
55
|
-
return Promise.resolve([]);
|
|
56
|
-
}
|
|
57
|
-
}
|