tool-db 2.5.3 → 2.5.4
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 +82 -82
- package/bundle.js +1 -1
- package/dist/adapters-base/networkAdapter.d.ts +58 -58
- package/dist/adapters-base/networkAdapter.js +215 -215
- package/dist/adapters-base/storageAdapter.d.ts +11 -11
- package/dist/adapters-base/storageAdapter.js +35 -35
- package/dist/adapters-base/userAdapter.d.ts +15 -15
- package/dist/adapters-base/userAdapter.js +41 -41
- package/dist/crdt/baseCrdt.d.ts +9 -9
- package/dist/crdt/baseCrdt.js +26 -26
- package/dist/crdt/counterCrdt.d.ts +30 -30
- package/dist/crdt/counterCrdt.js +105 -105
- package/dist/crdt/listCrdt.d.ts +42 -42
- package/dist/crdt/listCrdt.js +158 -158
- package/dist/crdt/mapCrdt.d.ts +32 -32
- package/dist/crdt/mapCrdt.js +117 -117
- package/dist/index.d.ts +34 -34
- package/dist/index.js +82 -82
- package/dist/logger.d.ts +2 -2
- package/dist/logger.js +28 -28
- package/dist/messageHandlers/handleCrdtGet.d.ts +2 -2
- package/dist/messageHandlers/handleCrdtGet.js +49 -28
- package/dist/messageHandlers/handleCrdtGet.js.map +1 -1
- package/dist/messageHandlers/handleCrdtPut.d.ts +2 -2
- package/dist/messageHandlers/handleCrdtPut.js +102 -92
- package/dist/messageHandlers/handleCrdtPut.js.map +1 -1
- package/dist/messageHandlers/handleGet.d.ts +2 -2
- package/dist/messageHandlers/handleGet.js +49 -28
- package/dist/messageHandlers/handleGet.js.map +1 -1
- package/dist/messageHandlers/handlePing.d.ts +2 -2
- package/dist/messageHandlers/handlePing.js +35 -35
- package/dist/messageHandlers/handlePing.js.map +1 -1
- package/dist/messageHandlers/handlePong.d.ts +2 -2
- package/dist/messageHandlers/handlePong.js +25 -25
- package/dist/messageHandlers/handlePut.d.ts +2 -2
- package/dist/messageHandlers/handlePut.js +77 -56
- package/dist/messageHandlers/handlePut.js.map +1 -1
- package/dist/messageHandlers/handleQuery.d.ts +2 -2
- package/dist/messageHandlers/handleQuery.js +43 -22
- package/dist/messageHandlers/handleQuery.js.map +1 -1
- package/dist/messageHandlers/handleSubscribe.d.ts +2 -2
- package/dist/messageHandlers/handleSubscribe.js +43 -43
- package/dist/server.d.ts +1 -1
- package/dist/server.js +8 -8
- package/dist/shared.d.ts +2 -2
- package/dist/shared.js +6 -6
- package/dist/toolDbAnonSignIn.d.ts +2 -2
- package/dist/toolDbAnonSignIn.js +6 -6
- package/dist/toolDbClientOnMessage.d.ts +2 -2
- package/dist/toolDbClientOnMessage.js +58 -58
- package/dist/toolDbCrdtGet.d.ts +9 -9
- package/dist/toolDbCrdtGet.js +76 -76
- package/dist/toolDbCrdtGet.js.map +1 -1
- package/dist/toolDbCrdtPut.d.ts +9 -9
- package/dist/toolDbCrdtPut.js +62 -62
- package/dist/toolDbCrdtPut.js.map +1 -1
- package/dist/toolDbGet.d.ts +9 -9
- package/dist/toolDbGet.js +76 -76
- package/dist/toolDbGet.js.map +1 -1
- package/dist/toolDbKeysSignIn.d.ts +2 -2
- package/dist/toolDbKeysSignIn.js +15 -15
- package/dist/toolDbPut.d.ts +9 -9
- package/dist/toolDbPut.js +68 -68
- package/dist/toolDbPut.js.map +1 -1
- package/dist/toolDbQueryKeys.d.ts +8 -8
- package/dist/toolDbQueryKeys.js +65 -65
- package/dist/toolDbQueryKeys.js.map +1 -1
- package/dist/toolDbSignIn.d.ts +2 -2
- package/dist/toolDbSignIn.js +26 -26
- package/dist/toolDbSignIn.js.map +1 -1
- package/dist/toolDbSignUp.d.ts +2 -2
- package/dist/toolDbSignUp.js +100 -100
- package/dist/toolDbSignUp.js.map +1 -1
- package/dist/toolDbSubscribe.d.ts +9 -9
- package/dist/toolDbSubscribe.js +49 -49
- package/dist/toolDbSubscribe.js.map +1 -1
- package/dist/toolDbVerificationWrapper.d.ts +2 -2
- package/dist/toolDbVerificationWrapper.js +97 -97
- package/dist/tooldb.d.ts +108 -108
- package/dist/tooldb.js +285 -285
- package/dist/types/message.d.ts +100 -100
- package/dist/types/message.js +16 -16
- package/dist/types/tooldb.d.ts +84 -84
- package/dist/types/tooldb.js +2 -2
- package/dist/utils/catchReturn.d.ts +1 -1
- package/dist/utils/catchReturn.js +7 -7
- package/dist/utils/encoding/arrayBufferToHex.d.ts +1 -1
- package/dist/utils/encoding/arrayBufferToHex.js +18 -18
- package/dist/utils/encoding/arrayBufferToString.d.ts +1 -1
- package/dist/utils/encoding/arrayBufferToString.js +11 -11
- package/dist/utils/encoding/hexToArrayBuffer.d.ts +1 -1
- package/dist/utils/encoding/hexToArrayBuffer.js +14 -14
- package/dist/utils/encoding/hexToString.d.ts +1 -1
- package/dist/utils/encoding/hexToString.js +11 -11
- package/dist/utils/encoding/hexToUint8.d.ts +1 -1
- package/dist/utils/encoding/hexToUint8.js +7 -7
- package/dist/utils/encoding/stringToArrayBuffer.d.ts +1 -1
- package/dist/utils/encoding/stringToArrayBuffer.js +11 -11
- package/dist/utils/getPeerSignature.d.ts +2 -2
- package/dist/utils/getPeerSignature.js +8 -8
- package/dist/utils/getTimestamp.d.ts +1 -1
- package/dist/utils/getTimestamp.js +6 -6
- package/dist/utils/proofOfWork.d.ts +4 -4
- package/dist/utils/proofOfWork.js +15 -15
- package/dist/utils/randomAnimal.d.ts +1 -1
- package/dist/utils/randomAnimal.js +76 -76
- package/dist/utils/sha1.d.ts +1 -1
- package/dist/utils/sha1.js +12 -12
- package/dist/utils/sha256.d.ts +3 -3
- package/dist/utils/sha256.js +12 -12
- package/dist/utils/textRandom.d.ts +1 -1
- package/dist/utils/textRandom.js +14 -14
- package/dist/utils/uniq.d.ts +1 -1
- package/dist/utils/uniq.js +6 -6
- package/dist/utils/verifyMessage.d.ts +8 -8
- package/dist/utils/verifyMessage.js +128 -128
- package/dist/utils/verifyPeer.d.ts +2 -2
- package/dist/utils/verifyPeer.js +14 -14
- package/lib/adapters-base/networkAdapter.ts +217 -217
- package/lib/adapters-base/storageAdapter.ts +35 -35
- package/lib/adapters-base/userAdapter.ts +49 -49
- package/lib/crdt/baseCrdt.ts +21 -21
- package/lib/crdt/counterCrdt.ts +111 -111
- package/lib/crdt/listCrdt.ts +190 -190
- package/lib/crdt/mapCrdt.ts +119 -119
- package/lib/index.ts +42 -42
- package/lib/logger.ts +30 -30
- package/lib/messageHandlers/handleCrdtGet.ts +34 -29
- package/lib/messageHandlers/handleCrdtPut.ts +123 -118
- package/lib/messageHandlers/handleGet.ts +34 -29
- package/lib/messageHandlers/handlePing.ts +40 -40
- package/lib/messageHandlers/handlePong.ts +30 -30
- package/lib/messageHandlers/handlePut.ts +59 -54
- package/lib/messageHandlers/handleQuery.ts +30 -25
- package/lib/messageHandlers/handleSubscribe.ts +46 -46
- package/lib/server.ts +7 -7
- package/lib/shared.ts +5 -5
- package/lib/toolDbAnonSignIn.ts +5 -5
- package/lib/toolDbClientOnMessage.ts +75 -75
- package/lib/toolDbCrdtGet.ts +83 -82
- package/lib/toolDbCrdtPut.ts +78 -77
- package/lib/toolDbGet.ts +81 -80
- package/lib/toolDbKeysSignIn.ts +16 -16
- package/lib/toolDbPut.ts +84 -83
- package/lib/toolDbQueryKeys.ts +65 -64
- package/lib/toolDbSignIn.ts +32 -31
- package/lib/toolDbSignUp.ts +72 -71
- package/lib/toolDbSubscribe.ts +54 -53
- package/lib/toolDbVerificationWrapper.ts +55 -55
- package/lib/tooldb.ts +316 -316
- package/lib/types/message.ts +133 -133
- package/lib/types/tooldb.ts +97 -97
- package/lib/utils/catchReturn.ts +4 -4
- package/lib/utils/encoding/arrayBufferToHex.ts +18 -18
- package/lib/utils/encoding/arrayBufferToString.ts +8 -8
- package/lib/utils/encoding/hexToArrayBuffer.ts +13 -13
- package/lib/utils/encoding/hexToString.ts +8 -8
- package/lib/utils/encoding/hexToUint8.ts +5 -5
- package/lib/utils/encoding/stringToArrayBuffer.ts +8 -8
- package/lib/utils/getPeerSignature.ts +12 -12
- package/lib/utils/getTimestamp.ts +3 -3
- package/lib/utils/proofOfWork.ts +16 -16
- package/lib/utils/randomAnimal.ts +77 -77
- package/lib/utils/sha1.ts +7 -7
- package/lib/utils/sha256.ts +7 -7
- package/lib/utils/textRandom.ts +11 -11
- package/lib/utils/uniq.ts +3 -3
- package/lib/utils/verifyMessage.ts +88 -88
- package/lib/utils/verifyPeer.ts +15 -15
- package/package.json +2 -2
- package/tsconfig.json +14 -14
|
@@ -1,58 +1,58 @@
|
|
|
1
|
-
import { ToolDb, ToolDbMessage, Peer } from "..";
|
|
2
|
-
export default class ToolDbNetworkAdapter {
|
|
3
|
-
private _clientToSend;
|
|
4
|
-
private _isClientConnected;
|
|
5
|
-
private _clientIsServer;
|
|
6
|
-
private _tooldb;
|
|
7
|
-
constructor(db: ToolDb);
|
|
8
|
-
get clientToSend(): Record<string, (message: string) => void>;
|
|
9
|
-
get isClientConnected(): Record<string, () => boolean>;
|
|
10
|
-
get tooldb(): ToolDb;
|
|
11
|
-
getMeAsPeer(): Promise<Peer>;
|
|
12
|
-
/**
|
|
13
|
-
* Check if the specified client is connected or not
|
|
14
|
-
* @param clientId Client ID
|
|
15
|
-
* @returns boolean
|
|
16
|
-
*/
|
|
17
|
-
isConnected(clientId: string): boolean;
|
|
18
|
-
/**
|
|
19
|
-
* Check if the specified client is a server/relay
|
|
20
|
-
* @param clientId Client ID
|
|
21
|
-
* @returns boolean
|
|
22
|
-
*/
|
|
23
|
-
isServer(clientId: string): boolean;
|
|
24
|
-
craftPingMessage(): Promise<string>;
|
|
25
|
-
/**
|
|
26
|
-
* Execute the function to send a message to the specified client ID
|
|
27
|
-
* @param clientId Client ID
|
|
28
|
-
* @param message Message
|
|
29
|
-
*/
|
|
30
|
-
private executeSendToClient;
|
|
31
|
-
getClientAddress(): string;
|
|
32
|
-
onClientDisconnect(clientId: string): void;
|
|
33
|
-
/**
|
|
34
|
-
* Should be called as a message payload handler
|
|
35
|
-
* This function will take care of processing the messages and also making sure
|
|
36
|
-
* we use the correct response methods troughout the adapter.
|
|
37
|
-
* @param message message payload
|
|
38
|
-
* @param clientId Client ID (can be null for ping/pong)
|
|
39
|
-
* @param setClientId Callback to set the client id on the parent class
|
|
40
|
-
*/
|
|
41
|
-
onClientMessage(message: string, clientId: string | null, setClientId: (clientId: string) => void): void;
|
|
42
|
-
/**
|
|
43
|
-
* Sends a message to all peers connected to us
|
|
44
|
-
* This function takes care of the message deduplication, making sure we dont send
|
|
45
|
-
* the same message twice to the same peer.
|
|
46
|
-
* @param msg message data
|
|
47
|
-
* @param crossServerOnly If this message should be send to server peers only
|
|
48
|
-
* @param isRelay if we should relay this message
|
|
49
|
-
*/
|
|
50
|
-
sendToAll(msg: ToolDbMessage, crossServerOnly?: boolean): void;
|
|
51
|
-
/**
|
|
52
|
-
* Sends a message to a single peer.
|
|
53
|
-
* This function also takes care of the message deduplication.
|
|
54
|
-
* @param clientId Peer/Client id we want to send to.
|
|
55
|
-
* @param msg message data
|
|
56
|
-
*/
|
|
57
|
-
sendToClientId(clientId: string, msg: ToolDbMessage): void;
|
|
58
|
-
}
|
|
1
|
+
import { ToolDb, ToolDbMessage, Peer } from "..";
|
|
2
|
+
export default class ToolDbNetworkAdapter {
|
|
3
|
+
private _clientToSend;
|
|
4
|
+
private _isClientConnected;
|
|
5
|
+
private _clientIsServer;
|
|
6
|
+
private _tooldb;
|
|
7
|
+
constructor(db: ToolDb);
|
|
8
|
+
get clientToSend(): Record<string, (message: string) => void>;
|
|
9
|
+
get isClientConnected(): Record<string, () => boolean>;
|
|
10
|
+
get tooldb(): ToolDb;
|
|
11
|
+
getMeAsPeer(): Promise<Peer>;
|
|
12
|
+
/**
|
|
13
|
+
* Check if the specified client is connected or not
|
|
14
|
+
* @param clientId Client ID
|
|
15
|
+
* @returns boolean
|
|
16
|
+
*/
|
|
17
|
+
isConnected(clientId: string): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Check if the specified client is a server/relay
|
|
20
|
+
* @param clientId Client ID
|
|
21
|
+
* @returns boolean
|
|
22
|
+
*/
|
|
23
|
+
isServer(clientId: string): boolean;
|
|
24
|
+
craftPingMessage(): Promise<string>;
|
|
25
|
+
/**
|
|
26
|
+
* Execute the function to send a message to the specified client ID
|
|
27
|
+
* @param clientId Client ID
|
|
28
|
+
* @param message Message
|
|
29
|
+
*/
|
|
30
|
+
private executeSendToClient;
|
|
31
|
+
getClientAddress(): string;
|
|
32
|
+
onClientDisconnect(clientId: string): void;
|
|
33
|
+
/**
|
|
34
|
+
* Should be called as a message payload handler
|
|
35
|
+
* This function will take care of processing the messages and also making sure
|
|
36
|
+
* we use the correct response methods troughout the adapter.
|
|
37
|
+
* @param message message payload
|
|
38
|
+
* @param clientId Client ID (can be null for ping/pong)
|
|
39
|
+
* @param setClientId Callback to set the client id on the parent class
|
|
40
|
+
*/
|
|
41
|
+
onClientMessage(message: string, clientId: string | null, setClientId: (clientId: string) => void): void;
|
|
42
|
+
/**
|
|
43
|
+
* Sends a message to all peers connected to us
|
|
44
|
+
* This function takes care of the message deduplication, making sure we dont send
|
|
45
|
+
* the same message twice to the same peer.
|
|
46
|
+
* @param msg message data
|
|
47
|
+
* @param crossServerOnly If this message should be send to server peers only
|
|
48
|
+
* @param isRelay if we should relay this message
|
|
49
|
+
*/
|
|
50
|
+
sendToAll(msg: ToolDbMessage, crossServerOnly?: boolean): void;
|
|
51
|
+
/**
|
|
52
|
+
* Sends a message to a single peer.
|
|
53
|
+
* This function also takes care of the message deduplication.
|
|
54
|
+
* @param clientId Peer/Client id we want to send to.
|
|
55
|
+
* @param msg message data
|
|
56
|
+
*/
|
|
57
|
+
sendToClientId(clientId: string, msg: ToolDbMessage): void;
|
|
58
|
+
}
|
|
@@ -1,216 +1,216 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
14
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
15
|
-
if (ar || !(i in from)) {
|
|
16
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
17
|
-
ar[i] = from[i];
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
21
|
-
};
|
|
22
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
var __1 = require("..");
|
|
24
|
-
var ToolDbNetworkAdapter = /** @class */ (function () {
|
|
25
|
-
function ToolDbNetworkAdapter(db) {
|
|
26
|
-
var _this = this;
|
|
27
|
-
this._clientToSend = {};
|
|
28
|
-
this._isClientConnected = {};
|
|
29
|
-
this._clientIsServer = {};
|
|
30
|
-
this._tooldb = db;
|
|
31
|
-
setTimeout(function () {
|
|
32
|
-
if (_this.tooldb.options.server) {
|
|
33
|
-
_this.getMeAsPeer().then(function (meAsPeer) {
|
|
34
|
-
_this.tooldb.serverPeers.push(meAsPeer);
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
}, 100);
|
|
38
|
-
}
|
|
39
|
-
Object.defineProperty(ToolDbNetworkAdapter.prototype, "clientToSend", {
|
|
40
|
-
get: function () {
|
|
41
|
-
return this._clientToSend;
|
|
42
|
-
},
|
|
43
|
-
enumerable: false,
|
|
44
|
-
configurable: true
|
|
45
|
-
});
|
|
46
|
-
Object.defineProperty(ToolDbNetworkAdapter.prototype, "isClientConnected", {
|
|
47
|
-
get: function () {
|
|
48
|
-
return this._isClientConnected;
|
|
49
|
-
},
|
|
50
|
-
enumerable: false,
|
|
51
|
-
configurable: true
|
|
52
|
-
});
|
|
53
|
-
Object.defineProperty(ToolDbNetworkAdapter.prototype, "tooldb", {
|
|
54
|
-
get: function () {
|
|
55
|
-
return this._tooldb;
|
|
56
|
-
},
|
|
57
|
-
enumerable: false,
|
|
58
|
-
configurable: true
|
|
59
|
-
});
|
|
60
|
-
ToolDbNetworkAdapter.prototype.getMeAsPeer = function () {
|
|
61
|
-
var _this = this;
|
|
62
|
-
var timestamp = new Date().getTime();
|
|
63
|
-
return (0, __1.getPeerSignature)(this.tooldb.peerAccount, this.tooldb.options.topic, timestamp, this.tooldb.options.host, this.tooldb.options.port).then(function (signature) {
|
|
64
|
-
return {
|
|
65
|
-
topic: _this.tooldb.options.topic,
|
|
66
|
-
timestamp: timestamp,
|
|
67
|
-
host: _this.tooldb.options.host,
|
|
68
|
-
port: _this.tooldb.options.port,
|
|
69
|
-
address: _this.tooldb.peerAccount.getAddress() || "",
|
|
70
|
-
sig: signature,
|
|
71
|
-
};
|
|
72
|
-
});
|
|
73
|
-
};
|
|
74
|
-
/**
|
|
75
|
-
* Check if the specified client is connected or not
|
|
76
|
-
* @param clientId Client ID
|
|
77
|
-
* @returns boolean
|
|
78
|
-
*/
|
|
79
|
-
ToolDbNetworkAdapter.prototype.isConnected = function (clientId) {
|
|
80
|
-
return this._isClientConnected[clientId]
|
|
81
|
-
? this._isClientConnected[clientId]()
|
|
82
|
-
: false;
|
|
83
|
-
};
|
|
84
|
-
/**
|
|
85
|
-
* Check if the specified client is a server/relay
|
|
86
|
-
* @param clientId Client ID
|
|
87
|
-
* @returns boolean
|
|
88
|
-
*/
|
|
89
|
-
ToolDbNetworkAdapter.prototype.isServer = function (clientId) {
|
|
90
|
-
return this._clientIsServer[clientId] || false;
|
|
91
|
-
};
|
|
92
|
-
ToolDbNetworkAdapter.prototype.craftPingMessage = function () {
|
|
93
|
-
var _this = this;
|
|
94
|
-
return this.getMeAsPeer().then(function (meAsPeer) {
|
|
95
|
-
return JSON.stringify({
|
|
96
|
-
type: "ping",
|
|
97
|
-
clientId: _this.getClientAddress(),
|
|
98
|
-
to: [_this.getClientAddress()],
|
|
99
|
-
isServer: _this.tooldb.options.server,
|
|
100
|
-
id: (0, __1.textRandom)(10),
|
|
101
|
-
peer: meAsPeer,
|
|
102
|
-
});
|
|
103
|
-
});
|
|
104
|
-
};
|
|
105
|
-
/**
|
|
106
|
-
* Execute the function to send a message to the specified client ID
|
|
107
|
-
* @param clientId Client ID
|
|
108
|
-
* @param message Message
|
|
109
|
-
*/
|
|
110
|
-
ToolDbNetworkAdapter.prototype.executeSendToClient = function (clientId, message) {
|
|
111
|
-
if (this._clientToSend[clientId]) {
|
|
112
|
-
this._clientToSend[clientId](message);
|
|
113
|
-
}
|
|
114
|
-
};
|
|
115
|
-
ToolDbNetworkAdapter.prototype.getClientAddress = function () {
|
|
116
|
-
// This is not a good idea to use on all adapters, so it should be replaced
|
|
117
|
-
// if its causing issues. The only reason we use the last 20 chars is to
|
|
118
|
-
// muse the same peer address as the webrtc adapter.
|
|
119
|
-
return (this.tooldb.peerAccount.getAddress() || "").slice(-20);
|
|
120
|
-
};
|
|
121
|
-
ToolDbNetworkAdapter.prototype.onClientDisconnect = function (clientId) {
|
|
122
|
-
delete this._clientToSend[clientId];
|
|
123
|
-
delete this._clientIsServer[clientId];
|
|
124
|
-
};
|
|
125
|
-
/**
|
|
126
|
-
* Should be called as a message payload handler
|
|
127
|
-
* This function will take care of processing the messages and also making sure
|
|
128
|
-
* we use the correct response methods troughout the adapter.
|
|
129
|
-
* @param message message payload
|
|
130
|
-
* @param clientId Client ID (can be null for ping/pong)
|
|
131
|
-
* @param setClientId Callback to set the client id on the parent class
|
|
132
|
-
*/
|
|
133
|
-
ToolDbNetworkAdapter.prototype.onClientMessage = function (message, clientId, setClientId) {
|
|
134
|
-
// this.tooldb.logger("onClientMessage", clientId);
|
|
135
|
-
if (clientId && !this.tooldb.processedOutHashes[clientId]) {
|
|
136
|
-
this.tooldb.processedOutHashes[clientId] = [];
|
|
137
|
-
}
|
|
138
|
-
try {
|
|
139
|
-
var parsedMessage = JSON.parse(message);
|
|
140
|
-
// We assume the first messages to arrive will always be ping or pong.
|
|
141
|
-
// Only after that we can get the client id for this socket.
|
|
142
|
-
if (parsedMessage.type === "ping" || parsedMessage.type === "pong") {
|
|
143
|
-
var cid = parsedMessage.clientId;
|
|
144
|
-
setClientId(cid);
|
|
145
|
-
this.tooldb.onPeerConnect(cid);
|
|
146
|
-
this._clientIsServer[cid] = parsedMessage.isServer;
|
|
147
|
-
this.tooldb.processedOutHashes[cid] = [];
|
|
148
|
-
this.tooldb.clientOnMessage(parsedMessage, cid);
|
|
149
|
-
}
|
|
150
|
-
else if (clientId) {
|
|
151
|
-
this.tooldb.clientOnMessage(parsedMessage, clientId);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
catch (e) {
|
|
155
|
-
this.tooldb.logger("Got message ERR", message);
|
|
156
|
-
this.tooldb.logger(e);
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
/**
|
|
160
|
-
* Sends a message to all peers connected to us
|
|
161
|
-
* This function takes care of the message deduplication, making sure we dont send
|
|
162
|
-
* the same message twice to the same peer.
|
|
163
|
-
* @param msg message data
|
|
164
|
-
* @param crossServerOnly If this message should be send to server peers only
|
|
165
|
-
* @param isRelay if we should relay this message
|
|
166
|
-
*/
|
|
167
|
-
ToolDbNetworkAdapter.prototype.sendToAll = function (msg, crossServerOnly) {
|
|
168
|
-
var _this = this;
|
|
169
|
-
if (crossServerOnly === void 0) { crossServerOnly = false; }
|
|
170
|
-
var to = (0, __1.uniq)(__spreadArray(__spreadArray([], msg.to, true), [this.getClientAddress()], false));
|
|
171
|
-
var finalMessage = JSON.stringify(__assign(__assign({}, msg), { to: to }));
|
|
172
|
-
var filteredConns = Object.keys(this.clientToSend)
|
|
173
|
-
.filter(function (id) { return !to.includes(id); })
|
|
174
|
-
.filter(function (clientId) { return _this.isConnected(clientId); });
|
|
175
|
-
filteredConns.forEach(function (clientId) {
|
|
176
|
-
if ((crossServerOnly && _this.isServer(clientId)) || !crossServerOnly) {
|
|
177
|
-
_this.tooldb.logger(to, "Sent out to:", clientId);
|
|
178
|
-
if (msg.type === "put" || msg.type === "crdtPut") {
|
|
179
|
-
if (!_this.tooldb.processedOutHashes[clientId].includes(msg.data.h)) {
|
|
180
|
-
_this.executeSendToClient(clientId, finalMessage);
|
|
181
|
-
_this.tooldb.processedOutHashes[clientId].push(msg.data.h);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
else {
|
|
185
|
-
_this.executeSendToClient(clientId, finalMessage);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
// } else {
|
|
189
|
-
// this.tooldb.logger("Fitlered out;", clientId);
|
|
190
|
-
// }
|
|
191
|
-
});
|
|
192
|
-
};
|
|
193
|
-
/**
|
|
194
|
-
* Sends a message to a single peer.
|
|
195
|
-
* This function also takes care of the message deduplication.
|
|
196
|
-
* @param clientId Peer/Client id we want to send to.
|
|
197
|
-
* @param msg message data
|
|
198
|
-
*/
|
|
199
|
-
ToolDbNetworkAdapter.prototype.sendToClientId = function (clientId, msg) {
|
|
200
|
-
var to = (0, __1.uniq)(__spreadArray(__spreadArray([], msg.to, true), [this.getClientAddress()], false));
|
|
201
|
-
var finalMessage = JSON.stringify(__assign(__assign({}, msg), { to: to }));
|
|
202
|
-
if (msg.type === "put" || msg.type === "crdtPut") {
|
|
203
|
-
if (clientId &&
|
|
204
|
-
!this.tooldb.processedOutHashes[clientId].includes(msg.data.h)) {
|
|
205
|
-
this.executeSendToClient(clientId, finalMessage);
|
|
206
|
-
this.tooldb.processedOutHashes[clientId].push(msg.data.h);
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
else {
|
|
210
|
-
this.executeSendToClient(clientId, finalMessage);
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
return ToolDbNetworkAdapter;
|
|
214
|
-
}());
|
|
215
|
-
exports.default = ToolDbNetworkAdapter;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
14
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
15
|
+
if (ar || !(i in from)) {
|
|
16
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
17
|
+
ar[i] = from[i];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
21
|
+
};
|
|
22
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
+
var __1 = require("..");
|
|
24
|
+
var ToolDbNetworkAdapter = /** @class */ (function () {
|
|
25
|
+
function ToolDbNetworkAdapter(db) {
|
|
26
|
+
var _this = this;
|
|
27
|
+
this._clientToSend = {};
|
|
28
|
+
this._isClientConnected = {};
|
|
29
|
+
this._clientIsServer = {};
|
|
30
|
+
this._tooldb = db;
|
|
31
|
+
setTimeout(function () {
|
|
32
|
+
if (_this.tooldb.options.server) {
|
|
33
|
+
_this.getMeAsPeer().then(function (meAsPeer) {
|
|
34
|
+
_this.tooldb.serverPeers.push(meAsPeer);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}, 100);
|
|
38
|
+
}
|
|
39
|
+
Object.defineProperty(ToolDbNetworkAdapter.prototype, "clientToSend", {
|
|
40
|
+
get: function () {
|
|
41
|
+
return this._clientToSend;
|
|
42
|
+
},
|
|
43
|
+
enumerable: false,
|
|
44
|
+
configurable: true
|
|
45
|
+
});
|
|
46
|
+
Object.defineProperty(ToolDbNetworkAdapter.prototype, "isClientConnected", {
|
|
47
|
+
get: function () {
|
|
48
|
+
return this._isClientConnected;
|
|
49
|
+
},
|
|
50
|
+
enumerable: false,
|
|
51
|
+
configurable: true
|
|
52
|
+
});
|
|
53
|
+
Object.defineProperty(ToolDbNetworkAdapter.prototype, "tooldb", {
|
|
54
|
+
get: function () {
|
|
55
|
+
return this._tooldb;
|
|
56
|
+
},
|
|
57
|
+
enumerable: false,
|
|
58
|
+
configurable: true
|
|
59
|
+
});
|
|
60
|
+
ToolDbNetworkAdapter.prototype.getMeAsPeer = function () {
|
|
61
|
+
var _this = this;
|
|
62
|
+
var timestamp = new Date().getTime();
|
|
63
|
+
return (0, __1.getPeerSignature)(this.tooldb.peerAccount, this.tooldb.options.topic, timestamp, this.tooldb.options.host, this.tooldb.options.port).then(function (signature) {
|
|
64
|
+
return {
|
|
65
|
+
topic: _this.tooldb.options.topic,
|
|
66
|
+
timestamp: timestamp,
|
|
67
|
+
host: _this.tooldb.options.host,
|
|
68
|
+
port: _this.tooldb.options.port,
|
|
69
|
+
address: _this.tooldb.peerAccount.getAddress() || "",
|
|
70
|
+
sig: signature,
|
|
71
|
+
};
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* Check if the specified client is connected or not
|
|
76
|
+
* @param clientId Client ID
|
|
77
|
+
* @returns boolean
|
|
78
|
+
*/
|
|
79
|
+
ToolDbNetworkAdapter.prototype.isConnected = function (clientId) {
|
|
80
|
+
return this._isClientConnected[clientId]
|
|
81
|
+
? this._isClientConnected[clientId]()
|
|
82
|
+
: false;
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Check if the specified client is a server/relay
|
|
86
|
+
* @param clientId Client ID
|
|
87
|
+
* @returns boolean
|
|
88
|
+
*/
|
|
89
|
+
ToolDbNetworkAdapter.prototype.isServer = function (clientId) {
|
|
90
|
+
return this._clientIsServer[clientId] || false;
|
|
91
|
+
};
|
|
92
|
+
ToolDbNetworkAdapter.prototype.craftPingMessage = function () {
|
|
93
|
+
var _this = this;
|
|
94
|
+
return this.getMeAsPeer().then(function (meAsPeer) {
|
|
95
|
+
return JSON.stringify({
|
|
96
|
+
type: "ping",
|
|
97
|
+
clientId: _this.getClientAddress(),
|
|
98
|
+
to: [_this.getClientAddress()],
|
|
99
|
+
isServer: _this.tooldb.options.server,
|
|
100
|
+
id: (0, __1.textRandom)(10),
|
|
101
|
+
peer: meAsPeer,
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
/**
|
|
106
|
+
* Execute the function to send a message to the specified client ID
|
|
107
|
+
* @param clientId Client ID
|
|
108
|
+
* @param message Message
|
|
109
|
+
*/
|
|
110
|
+
ToolDbNetworkAdapter.prototype.executeSendToClient = function (clientId, message) {
|
|
111
|
+
if (this._clientToSend[clientId]) {
|
|
112
|
+
this._clientToSend[clientId](message);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
ToolDbNetworkAdapter.prototype.getClientAddress = function () {
|
|
116
|
+
// This is not a good idea to use on all adapters, so it should be replaced
|
|
117
|
+
// if its causing issues. The only reason we use the last 20 chars is to
|
|
118
|
+
// muse the same peer address as the webrtc adapter.
|
|
119
|
+
return (this.tooldb.peerAccount.getAddress() || "").slice(-20);
|
|
120
|
+
};
|
|
121
|
+
ToolDbNetworkAdapter.prototype.onClientDisconnect = function (clientId) {
|
|
122
|
+
delete this._clientToSend[clientId];
|
|
123
|
+
delete this._clientIsServer[clientId];
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* Should be called as a message payload handler
|
|
127
|
+
* This function will take care of processing the messages and also making sure
|
|
128
|
+
* we use the correct response methods troughout the adapter.
|
|
129
|
+
* @param message message payload
|
|
130
|
+
* @param clientId Client ID (can be null for ping/pong)
|
|
131
|
+
* @param setClientId Callback to set the client id on the parent class
|
|
132
|
+
*/
|
|
133
|
+
ToolDbNetworkAdapter.prototype.onClientMessage = function (message, clientId, setClientId) {
|
|
134
|
+
// this.tooldb.logger("onClientMessage", clientId);
|
|
135
|
+
if (clientId && !this.tooldb.processedOutHashes[clientId]) {
|
|
136
|
+
this.tooldb.processedOutHashes[clientId] = [];
|
|
137
|
+
}
|
|
138
|
+
try {
|
|
139
|
+
var parsedMessage = JSON.parse(message);
|
|
140
|
+
// We assume the first messages to arrive will always be ping or pong.
|
|
141
|
+
// Only after that we can get the client id for this socket.
|
|
142
|
+
if (parsedMessage.type === "ping" || parsedMessage.type === "pong") {
|
|
143
|
+
var cid = parsedMessage.clientId;
|
|
144
|
+
setClientId(cid);
|
|
145
|
+
this.tooldb.onPeerConnect(cid);
|
|
146
|
+
this._clientIsServer[cid] = parsedMessage.isServer;
|
|
147
|
+
this.tooldb.processedOutHashes[cid] = [];
|
|
148
|
+
this.tooldb.clientOnMessage(parsedMessage, cid);
|
|
149
|
+
}
|
|
150
|
+
else if (clientId) {
|
|
151
|
+
this.tooldb.clientOnMessage(parsedMessage, clientId);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
catch (e) {
|
|
155
|
+
this.tooldb.logger("Got message ERR", message);
|
|
156
|
+
this.tooldb.logger(e);
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
/**
|
|
160
|
+
* Sends a message to all peers connected to us
|
|
161
|
+
* This function takes care of the message deduplication, making sure we dont send
|
|
162
|
+
* the same message twice to the same peer.
|
|
163
|
+
* @param msg message data
|
|
164
|
+
* @param crossServerOnly If this message should be send to server peers only
|
|
165
|
+
* @param isRelay if we should relay this message
|
|
166
|
+
*/
|
|
167
|
+
ToolDbNetworkAdapter.prototype.sendToAll = function (msg, crossServerOnly) {
|
|
168
|
+
var _this = this;
|
|
169
|
+
if (crossServerOnly === void 0) { crossServerOnly = false; }
|
|
170
|
+
var to = (0, __1.uniq)(__spreadArray(__spreadArray([], msg.to, true), [this.getClientAddress()], false));
|
|
171
|
+
var finalMessage = JSON.stringify(__assign(__assign({}, msg), { to: to }));
|
|
172
|
+
var filteredConns = Object.keys(this.clientToSend)
|
|
173
|
+
.filter(function (id) { return !to.includes(id); })
|
|
174
|
+
.filter(function (clientId) { return _this.isConnected(clientId); });
|
|
175
|
+
filteredConns.forEach(function (clientId) {
|
|
176
|
+
if ((crossServerOnly && _this.isServer(clientId)) || !crossServerOnly) {
|
|
177
|
+
_this.tooldb.logger(to, "Sent out to:", clientId);
|
|
178
|
+
if (msg.type === "put" || msg.type === "crdtPut") {
|
|
179
|
+
if (!_this.tooldb.processedOutHashes[clientId].includes(msg.data.h)) {
|
|
180
|
+
_this.executeSendToClient(clientId, finalMessage);
|
|
181
|
+
_this.tooldb.processedOutHashes[clientId].push(msg.data.h);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
_this.executeSendToClient(clientId, finalMessage);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// } else {
|
|
189
|
+
// this.tooldb.logger("Fitlered out;", clientId);
|
|
190
|
+
// }
|
|
191
|
+
});
|
|
192
|
+
};
|
|
193
|
+
/**
|
|
194
|
+
* Sends a message to a single peer.
|
|
195
|
+
* This function also takes care of the message deduplication.
|
|
196
|
+
* @param clientId Peer/Client id we want to send to.
|
|
197
|
+
* @param msg message data
|
|
198
|
+
*/
|
|
199
|
+
ToolDbNetworkAdapter.prototype.sendToClientId = function (clientId, msg) {
|
|
200
|
+
var to = (0, __1.uniq)(__spreadArray(__spreadArray([], msg.to, true), [this.getClientAddress()], false));
|
|
201
|
+
var finalMessage = JSON.stringify(__assign(__assign({}, msg), { to: to }));
|
|
202
|
+
if (msg.type === "put" || msg.type === "crdtPut") {
|
|
203
|
+
if (clientId &&
|
|
204
|
+
!this.tooldb.processedOutHashes[clientId].includes(msg.data.h)) {
|
|
205
|
+
this.executeSendToClient(clientId, finalMessage);
|
|
206
|
+
this.tooldb.processedOutHashes[clientId].push(msg.data.h);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
this.executeSendToClient(clientId, finalMessage);
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
return ToolDbNetworkAdapter;
|
|
214
|
+
}());
|
|
215
|
+
exports.default = ToolDbNetworkAdapter;
|
|
216
216
|
//# sourceMappingURL=networkAdapter.js.map
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { ToolDb } from "..";
|
|
2
|
-
export default class ToolDbStorageAdapter {
|
|
3
|
-
private _tooldb;
|
|
4
|
-
private _forceStorageName;
|
|
5
|
-
get tooldb(): ToolDb;
|
|
6
|
-
get storageName(): string;
|
|
7
|
-
constructor(db: ToolDb, forceStorageName?: string);
|
|
8
|
-
put(key: string, data: string): Promise<unknown>;
|
|
9
|
-
get(key: string): Promise<string>;
|
|
10
|
-
query(key: string): Promise<string[]>;
|
|
11
|
-
}
|
|
1
|
+
import { ToolDb } from "..";
|
|
2
|
+
export default class ToolDbStorageAdapter {
|
|
3
|
+
private _tooldb;
|
|
4
|
+
private _forceStorageName;
|
|
5
|
+
get tooldb(): ToolDb;
|
|
6
|
+
get storageName(): string;
|
|
7
|
+
constructor(db: ToolDb, forceStorageName?: string);
|
|
8
|
+
put(key: string, data: string): Promise<unknown>;
|
|
9
|
+
get(key: string): Promise<string>;
|
|
10
|
+
query(key: string): Promise<string[]>;
|
|
11
|
+
}
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
var ToolDbStorageAdapter = /** @class */ (function () {
|
|
4
|
-
function ToolDbStorageAdapter(db, forceStorageName) {
|
|
5
|
-
this._tooldb = db;
|
|
6
|
-
if (forceStorageName) {
|
|
7
|
-
this._forceStorageName = forceStorageName;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
Object.defineProperty(ToolDbStorageAdapter.prototype, "tooldb", {
|
|
11
|
-
get: function () {
|
|
12
|
-
return this._tooldb;
|
|
13
|
-
},
|
|
14
|
-
enumerable: false,
|
|
15
|
-
configurable: true
|
|
16
|
-
});
|
|
17
|
-
Object.defineProperty(ToolDbStorageAdapter.prototype, "storageName", {
|
|
18
|
-
get: function () {
|
|
19
|
-
return this._forceStorageName || this._tooldb.options.storageName;
|
|
20
|
-
},
|
|
21
|
-
enumerable: false,
|
|
22
|
-
configurable: true
|
|
23
|
-
});
|
|
24
|
-
ToolDbStorageAdapter.prototype.put = function (key, data) {
|
|
25
|
-
return Promise.resolve();
|
|
26
|
-
};
|
|
27
|
-
ToolDbStorageAdapter.prototype.get = function (key) {
|
|
28
|
-
return Promise.resolve("");
|
|
29
|
-
};
|
|
30
|
-
ToolDbStorageAdapter.prototype.query = function (key) {
|
|
31
|
-
return Promise.resolve([]);
|
|
32
|
-
};
|
|
33
|
-
return ToolDbStorageAdapter;
|
|
34
|
-
}());
|
|
35
|
-
exports.default = ToolDbStorageAdapter;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var ToolDbStorageAdapter = /** @class */ (function () {
|
|
4
|
+
function ToolDbStorageAdapter(db, forceStorageName) {
|
|
5
|
+
this._tooldb = db;
|
|
6
|
+
if (forceStorageName) {
|
|
7
|
+
this._forceStorageName = forceStorageName;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
Object.defineProperty(ToolDbStorageAdapter.prototype, "tooldb", {
|
|
11
|
+
get: function () {
|
|
12
|
+
return this._tooldb;
|
|
13
|
+
},
|
|
14
|
+
enumerable: false,
|
|
15
|
+
configurable: true
|
|
16
|
+
});
|
|
17
|
+
Object.defineProperty(ToolDbStorageAdapter.prototype, "storageName", {
|
|
18
|
+
get: function () {
|
|
19
|
+
return this._forceStorageName || this._tooldb.options.storageName;
|
|
20
|
+
},
|
|
21
|
+
enumerable: false,
|
|
22
|
+
configurable: true
|
|
23
|
+
});
|
|
24
|
+
ToolDbStorageAdapter.prototype.put = function (key, data) {
|
|
25
|
+
return Promise.resolve();
|
|
26
|
+
};
|
|
27
|
+
ToolDbStorageAdapter.prototype.get = function (key) {
|
|
28
|
+
return Promise.resolve("");
|
|
29
|
+
};
|
|
30
|
+
ToolDbStorageAdapter.prototype.query = function (key) {
|
|
31
|
+
return Promise.resolve([]);
|
|
32
|
+
};
|
|
33
|
+
return ToolDbStorageAdapter;
|
|
34
|
+
}());
|
|
35
|
+
exports.default = ToolDbStorageAdapter;
|
|
36
36
|
//# sourceMappingURL=storageAdapter.js.map
|