tool-db 2.1.2 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundle.js +1 -1
- package/dist/crdt/listCrdt.d.ts +2 -2
- package/dist/index.d.ts +14 -6
- package/dist/index.js +22 -12
- package/dist/index.js.map +1 -1
- package/dist/messageHandlers/handleCrdtPut.js +3 -3
- package/dist/messageHandlers/handleCrdtPut.js.map +1 -1
- package/dist/messageHandlers/handleJoin.js +24 -20
- package/dist/messageHandlers/handleJoin.js.map +1 -1
- package/dist/messageHandlers/handlePing.js +1 -1
- package/dist/messageHandlers/handlePing.js.map +1 -1
- package/dist/messageHandlers/handlePong.js +5 -5
- package/dist/messageHandlers/handlePong.js.map +1 -1
- package/dist/messageHandlers/handleServers.js +9 -8
- package/dist/messageHandlers/handleServers.js.map +1 -1
- package/dist/toolDbAnonSignIn.js +1 -6
- package/dist/toolDbAnonSignIn.js.map +1 -1
- package/dist/toolDbCrdtGet.js +4 -2
- package/dist/toolDbCrdtGet.js.map +1 -1
- package/dist/toolDbCrdtPut.js +9 -7
- package/dist/toolDbCrdtPut.js.map +1 -1
- package/dist/toolDbGet.js +4 -2
- package/dist/toolDbGet.js.map +1 -1
- package/dist/toolDbKeysSignIn.d.ts +1 -1
- package/dist/toolDbKeysSignIn.js +4 -2
- package/dist/toolDbKeysSignIn.js.map +1 -1
- package/dist/toolDbNetwork.d.ts +4 -16
- package/dist/toolDbNetwork.js +74 -179
- package/dist/toolDbNetwork.js.map +1 -1
- package/dist/toolDbNetworkAdapter.d.ts +57 -0
- package/dist/toolDbNetworkAdapter.js +192 -0
- package/dist/toolDbNetworkAdapter.js.map +1 -0
- package/dist/toolDbPut.js +7 -7
- package/dist/toolDbPut.js.map +1 -1
- package/dist/toolDbQueryKeys.js +3 -2
- package/dist/toolDbQueryKeys.js.map +1 -1
- package/dist/toolDbSignIn.d.ts +1 -2
- package/dist/toolDbSignIn.js +2 -2
- package/dist/toolDbSignIn.js.map +1 -1
- package/dist/toolDbSignUp.js +6 -6
- package/dist/toolDbSignUp.js.map +1 -1
- package/dist/toolDbSubscribe.js +4 -2
- package/dist/toolDbSubscribe.js.map +1 -1
- package/dist/toolDbWeb3User.d.ts +20 -0
- package/dist/toolDbWeb3User.js +75 -0
- package/dist/toolDbWeb3User.js.map +1 -0
- package/dist/toolDbWebrtc.d.ts +2 -12
- package/dist/toolDbWebrtc.js +69 -104
- package/dist/toolDbWebrtc.js.map +1 -1
- package/dist/toolDbWebsocket.d.ts +19 -0
- package/dist/toolDbWebsocket.js +193 -0
- package/dist/toolDbWebsocket.js.map +1 -0
- package/dist/tooldb.d.ts +6 -15
- package/dist/tooldb.js +24 -54
- package/dist/tooldb.js.map +1 -1
- package/dist/types/tooldb.d.ts +16 -10
- package/dist/types/tooldb.js +27 -13
- package/dist/types/tooldb.js.map +1 -1
- package/dist/utils/getPeerSignature.d.ts +2 -2
- package/dist/utils/getPeerSignature.js +1 -1
- package/dist/utils/getPeerSignature.js.map +1 -1
- package/dist/utils/randomAnimal.js +36 -1
- package/dist/utils/randomAnimal.js.map +1 -1
- package/dist/utils/verifyMessage.js +4 -3
- package/dist/utils/verifyMessage.js.map +1 -1
- package/dist/utils/verifyPeer.d.ts +1 -1
- package/dist/utils/verifyPeer.js +7 -3
- package/dist/utils/verifyPeer.js.map +1 -1
- package/package.json +1 -1
- package/test-alice/000663.ldb +0 -0
- package/test-alice/000665.ldb +0 -0
- package/test-alice/000668.ldb +0 -0
- package/test-alice/000669.log +0 -0
- package/test-alice/CURRENT +1 -1
- package/test-alice/LOG +5 -5
- package/test-alice/LOG.old +5 -5
- package/test-alice/MANIFEST-000667 +0 -0
- package/test-alice-speed/000035.log +0 -0
- package/test-alice-speed/000036.ldb +0 -0
- package/test-alice-speed/CURRENT +1 -1
- package/test-alice-speed/LOG +13 -14
- package/test-alice-speed/LOG.old +5 -5
- package/test-alice-speed/MANIFEST-000033 +0 -0
- package/test-bob/000665.ldb +0 -0
- package/test-bob/000667.ldb +0 -0
- package/test-bob/000668.log +0 -0
- package/test-bob/CURRENT +1 -1
- package/test-bob/LOG +5 -5
- package/test-bob/LOG.old +14 -5
- package/test-bob/MANIFEST-000666 +0 -0
- package/test-node-a/000663.ldb +0 -0
- package/test-node-a/000665.ldb +0 -0
- package/test-node-a/000668.ldb +0 -0
- package/test-node-a/000669.log +0 -0
- package/test-node-a/CURRENT +1 -1
- package/test-node-a/LOG +5 -5
- package/test-node-a/LOG.old +5 -5
- package/test-node-a/MANIFEST-000667 +0 -0
- package/test-node-b/000663.ldb +0 -0
- package/test-node-b/000665.ldb +0 -0
- package/test-node-b/000668.ldb +0 -0
- package/test-node-b/000669.log +0 -0
- package/test-node-b/CURRENT +1 -1
- package/test-node-b/LOG +5 -5
- package/test-node-b/LOG.old +5 -5
- package/test-node-b/MANIFEST-000667 +0 -0
- package/test-verify-a/{000212.log → 000250.log} +0 -0
- package/test-verify-a/CURRENT +1 -1
- package/test-verify-a/LOG +3 -3
- package/test-verify-a/LOG.old +3 -3
- package/test-verify-a/{MANIFEST-000211 → MANIFEST-000249} +0 -0
- package/test-alice/000425.ldb +0 -0
- package/test-alice/000427.ldb +0 -0
- package/test-alice/000430.ldb +0 -0
- package/test-alice/000431.log +0 -0
- package/test-alice/MANIFEST-000429 +0 -0
- package/test-alice-speed/000236.log +0 -0
- package/test-alice-speed/000237.ldb +0 -0
- package/test-alice-speed/MANIFEST-000234 +0 -0
- package/test-bob/000425.ldb +0 -0
- package/test-bob/000427.ldb +0 -0
- package/test-bob/000430.ldb +0 -0
- package/test-bob/000431.log +0 -0
- package/test-bob/MANIFEST-000429 +0 -0
- package/test-node-a/000425.ldb +0 -0
- package/test-node-a/000427.ldb +0 -0
- package/test-node-a/000430.ldb +0 -0
- package/test-node-a/000431.log +0 -0
- package/test-node-a/MANIFEST-000429 +0 -0
- package/test-node-b/000425.ldb +0 -0
- package/test-node-b/000427.ldb +0 -0
- package/test-node-b/000430.ldb +0 -0
- package/test-node-b/000431.log +0 -0
- package/test-node-b/MANIFEST-000429 +0 -0
package/dist/toolDbNetwork.js
CHANGED
|
@@ -1,59 +1,54 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
for (var p in
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
10
8
|
};
|
|
11
|
-
return
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
21
|
-
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
22
17
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
18
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
19
|
};
|
|
25
20
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
var lodash_1 = __importDefault(require("lodash"));
|
|
27
21
|
var ws_1 = __importDefault(require("ws"));
|
|
28
22
|
var _1 = require(".");
|
|
29
|
-
var
|
|
23
|
+
var toolDbNetworkAdapter_1 = __importDefault(require("./toolDbNetworkAdapter"));
|
|
24
|
+
var toolDbNetwork = /** @class */ (function (_super) {
|
|
25
|
+
__extends(toolDbNetwork, _super);
|
|
30
26
|
function toolDbNetwork(db) {
|
|
31
|
-
var _this = this;
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
? this.wnd.WebSocket || this.wnd.webkitWebSocket || this.wnd.mozWebSocket
|
|
27
|
+
var _this = _super.call(this, db) || this;
|
|
28
|
+
_this.wnd = typeof window === "undefined" ? undefined : window;
|
|
29
|
+
_this._wss = _this.wnd
|
|
30
|
+
? _this.wnd.WebSocket || _this.wnd.webkitWebSocket || _this.wnd.mozWebSocket
|
|
36
31
|
: ws_1.default;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
32
|
+
_this.server = null;
|
|
33
|
+
_this._connections = {};
|
|
34
|
+
_this._awaitingConnections = [];
|
|
35
|
+
_this.removeFromAwaiting = function (id) {
|
|
41
36
|
var index = _this._awaitingConnections.findIndex(function (c) { return c.id === id; });
|
|
42
37
|
if (index !== -1) {
|
|
43
38
|
_this._awaitingConnections.slice(index, 1);
|
|
44
39
|
}
|
|
45
40
|
};
|
|
46
|
-
this._clientSockets = {};
|
|
47
41
|
/**
|
|
48
|
-
* Open a connection to a
|
|
42
|
+
* Open a connection to a server
|
|
49
43
|
* @param url URL of the server (including port)
|
|
50
44
|
* @returns websocket
|
|
51
45
|
*/
|
|
52
|
-
|
|
46
|
+
_this.connectTo = function (host, port, connectionId) {
|
|
53
47
|
try {
|
|
54
48
|
var wsUrl = port === 443 ? "wss://" + host : "ws://" + host + ":" + port;
|
|
55
49
|
var wss_1 = new _this._wss(wsUrl);
|
|
56
50
|
var connId_1 = (0, _1.textRandom)(10);
|
|
51
|
+
var clientId_1 = "";
|
|
57
52
|
wss_1.connId = connectionId || connId_1;
|
|
58
53
|
var previousConnection = _this._awaitingConnections.filter(function (c) { return c.id === connectionId; })[0];
|
|
59
54
|
if (connectionId && previousConnection) {
|
|
@@ -70,14 +65,14 @@ var toolDbNetwork = /** @class */ (function () {
|
|
|
70
65
|
});
|
|
71
66
|
}
|
|
72
67
|
wss_1.onclose = function (_error) {
|
|
73
|
-
if (_this.options.debug) {
|
|
68
|
+
if (_this.tooldb.options.debug) {
|
|
74
69
|
console.log(_error.error);
|
|
75
70
|
}
|
|
76
71
|
_this.reconnect(connId_1);
|
|
77
72
|
};
|
|
78
73
|
wss_1.onerror = function (_error) {
|
|
79
74
|
var _a;
|
|
80
|
-
if (_this.options.debug) {
|
|
75
|
+
if (_this.tooldb.options.debug) {
|
|
81
76
|
console.log(_error.error);
|
|
82
77
|
}
|
|
83
78
|
if (((_a = _error === null || _error === void 0 ? void 0 : _error.error) === null || _a === void 0 ? void 0 : _a.code) !== "ETIMEDOUT") {
|
|
@@ -85,17 +80,16 @@ var toolDbNetwork = /** @class */ (function () {
|
|
|
85
80
|
}
|
|
86
81
|
};
|
|
87
82
|
wss_1.onopen = function () {
|
|
88
|
-
var _a, _b;
|
|
89
83
|
_this.removeFromAwaiting(connId_1);
|
|
90
|
-
if (_this.options.debug) {
|
|
84
|
+
if (_this.tooldb.options.debug) {
|
|
91
85
|
console.warn("Connected to " + host + ": " + port + " sucessfully.");
|
|
92
86
|
}
|
|
93
87
|
// hi peer
|
|
94
88
|
wss_1.send(JSON.stringify({
|
|
95
89
|
type: "ping",
|
|
96
|
-
clientId:
|
|
97
|
-
to: [
|
|
98
|
-
isServer: _this.options.server,
|
|
90
|
+
clientId: _this.tooldb.network.getClientAddress(),
|
|
91
|
+
to: [_this.tooldb.network.getClientAddress()],
|
|
92
|
+
isServer: _this.tooldb.options.server,
|
|
99
93
|
id: (0, _1.textRandom)(10),
|
|
100
94
|
}));
|
|
101
95
|
};
|
|
@@ -103,26 +97,15 @@ var toolDbNetwork = /** @class */ (function () {
|
|
|
103
97
|
if (!msg) {
|
|
104
98
|
return;
|
|
105
99
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
wss_1.
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
wss_1.toolDbId = parsedMessage.clientId;
|
|
116
|
-
wss_1.isServer = parsedMessage.isServer;
|
|
117
|
-
_this._clientSockets[parsedMessage.clientId] = wss_1;
|
|
118
|
-
_this.tooldb.processedOutHashes[parsedMessage.clientId] = [];
|
|
119
|
-
}
|
|
120
|
-
_this.tooldb.clientOnMessage(parsedMessage, wss_1.toolDbId);
|
|
121
|
-
}
|
|
122
|
-
catch (e) {
|
|
123
|
-
console.log("Got message ERR > ", msg);
|
|
124
|
-
console.log(e);
|
|
125
|
-
}
|
|
100
|
+
_this.onClientMessage(msg.data, clientId_1, function (id) {
|
|
101
|
+
clientId_1 = id;
|
|
102
|
+
_this.isClientConnected[id] = function () {
|
|
103
|
+
return wss_1.readyState === wss_1.OPEN;
|
|
104
|
+
};
|
|
105
|
+
_this.clientToSend[id] = function (_msg) {
|
|
106
|
+
wss_1.send(_msg);
|
|
107
|
+
};
|
|
108
|
+
});
|
|
126
109
|
};
|
|
127
110
|
return wss_1;
|
|
128
111
|
}
|
|
@@ -131,13 +114,13 @@ var toolDbNetwork = /** @class */ (function () {
|
|
|
131
114
|
}
|
|
132
115
|
return undefined;
|
|
133
116
|
};
|
|
134
|
-
|
|
117
|
+
_this.reconnect = function (connectionId) {
|
|
135
118
|
var connection = _this._awaitingConnections.filter(function (c) { return c.id === connectionId; })[0];
|
|
136
119
|
if (connection) {
|
|
137
120
|
if (connection.defer) {
|
|
138
121
|
clearTimeout(connection.defer);
|
|
139
122
|
}
|
|
140
|
-
if (connection.tries < _this.options.maxRetries) {
|
|
123
|
+
if (connection.tries < _this.tooldb.options.maxRetries) {
|
|
141
124
|
var defer = function () {
|
|
142
125
|
connection.tries += 1;
|
|
143
126
|
console.warn("Connection to " +
|
|
@@ -147,7 +130,7 @@ var toolDbNetwork = /** @class */ (function () {
|
|
|
147
130
|
" retry.");
|
|
148
131
|
_this.connectTo(connection.host, connection.port, connectionId);
|
|
149
132
|
};
|
|
150
|
-
connection.defer = setTimeout(defer, _this.options.wait);
|
|
133
|
+
connection.defer = setTimeout(defer, _this.tooldb.options.wait);
|
|
151
134
|
}
|
|
152
135
|
else {
|
|
153
136
|
console.warn("Connection attempts to " +
|
|
@@ -158,141 +141,53 @@ var toolDbNetwork = /** @class */ (function () {
|
|
|
158
141
|
_this.removeFromAwaiting(connectionId);
|
|
159
142
|
// There are no more peers to connect!
|
|
160
143
|
if (Object.keys(_this._connections).length === 0) {
|
|
161
|
-
_this.
|
|
162
|
-
_this.
|
|
144
|
+
_this.tooldb.onDisconnect();
|
|
145
|
+
_this.tooldb.isConnected = false;
|
|
163
146
|
}
|
|
164
147
|
}
|
|
165
148
|
}
|
|
166
149
|
// else , attempting to reconnect to a missing peer?
|
|
167
150
|
};
|
|
168
|
-
|
|
169
|
-
this.options = db.options;
|
|
170
|
-
this.options.peers.forEach(function (p) {
|
|
151
|
+
_this.tooldb.options.peers.forEach(function (p) {
|
|
171
152
|
_this.connectTo(p.host, p.port);
|
|
172
153
|
});
|
|
173
|
-
if (
|
|
174
|
-
|
|
175
|
-
port:
|
|
176
|
-
server:
|
|
154
|
+
if (_this.tooldb.options.server) {
|
|
155
|
+
_this.server = new ws_1.default.Server({
|
|
156
|
+
port: _this.tooldb.options.port,
|
|
157
|
+
server: _this.tooldb.options.httpServer,
|
|
177
158
|
});
|
|
178
|
-
|
|
179
|
-
|
|
159
|
+
_this.server.on("connection", function (socket) {
|
|
160
|
+
var clientId = null;
|
|
161
|
+
// console.log("new connection:", clientId);
|
|
180
162
|
socket.on("close", function () {
|
|
181
|
-
// console.log("closed connection:",
|
|
182
|
-
if (
|
|
183
|
-
|
|
163
|
+
// console.log("closed connection:", clientId);
|
|
164
|
+
if (clientId) {
|
|
165
|
+
_this.onClientDisconnect(clientId);
|
|
184
166
|
}
|
|
185
167
|
});
|
|
186
168
|
socket.on("error", function () {
|
|
187
|
-
// console.log("errored connection:",
|
|
188
|
-
if (
|
|
189
|
-
|
|
169
|
+
// console.log("errored connection:", clientId);
|
|
170
|
+
if (clientId) {
|
|
171
|
+
_this.onClientDisconnect(clientId);
|
|
190
172
|
}
|
|
191
173
|
});
|
|
192
174
|
socket.on("message", function (message) {
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
socket.
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
socket.isServer = parsedMessage.isServer;
|
|
204
|
-
_this._clientSockets[parsedMessage.clientId] = socket;
|
|
205
|
-
_this.tooldb.processedOutHashes[parsedMessage.clientId] = [];
|
|
206
|
-
}
|
|
207
|
-
_this.tooldb.clientOnMessage(parsedMessage, socket.toolDbId || "");
|
|
208
|
-
}
|
|
209
|
-
catch (e) {
|
|
210
|
-
console.log("Got message ERR > ", message);
|
|
211
|
-
console.log(e);
|
|
212
|
-
}
|
|
175
|
+
_this.onClientMessage(message, clientId || "", function (id) {
|
|
176
|
+
clientId = id;
|
|
177
|
+
// Set this socket's functions on the adapter
|
|
178
|
+
_this.isClientConnected[id] = function () {
|
|
179
|
+
return socket.readyState === socket.OPEN;
|
|
180
|
+
};
|
|
181
|
+
_this.clientToSend[id] = function (_msg) {
|
|
182
|
+
socket.send(_msg);
|
|
183
|
+
};
|
|
184
|
+
});
|
|
213
185
|
});
|
|
214
186
|
});
|
|
215
187
|
}
|
|
188
|
+
return _this;
|
|
216
189
|
}
|
|
217
|
-
Object.defineProperty(toolDbNetwork.prototype, "clientSockets", {
|
|
218
|
-
get: function () {
|
|
219
|
-
return this._clientSockets;
|
|
220
|
-
},
|
|
221
|
-
enumerable: false,
|
|
222
|
-
configurable: true
|
|
223
|
-
});
|
|
224
|
-
toolDbNetwork.prototype.close = function (clientId) {
|
|
225
|
-
var sock = this._clientSockets[clientId];
|
|
226
|
-
if (sock) {
|
|
227
|
-
if (sock.origUrl) {
|
|
228
|
-
var peer = this._connections[sock.origUrl];
|
|
229
|
-
peer.tries = this.options.maxRetries;
|
|
230
|
-
}
|
|
231
|
-
sock.close();
|
|
232
|
-
delete this._clientSockets[clientId];
|
|
233
|
-
}
|
|
234
|
-
};
|
|
235
|
-
toolDbNetwork.prototype.sendToAll = function (msg, crossServerOnly, isRelay) {
|
|
236
|
-
var _this = this;
|
|
237
|
-
if (crossServerOnly === void 0) { crossServerOnly = false; }
|
|
238
|
-
if (isRelay === void 0) { isRelay = false; }
|
|
239
|
-
var to = isRelay
|
|
240
|
-
? lodash_1.default.uniq(__spreadArray([], msg.to, true))
|
|
241
|
-
: lodash_1.default.uniq(__spreadArray(__spreadArray([], msg.to, true), [this.options.peerAccount.address], false));
|
|
242
|
-
var filteredConns = Object.keys(this.clientSockets)
|
|
243
|
-
.filter(function (id) { return !to.includes(id); })
|
|
244
|
-
.map(function (clientId) { return _this._clientSockets[clientId]; })
|
|
245
|
-
.filter(function (conn) { return conn && conn.readyState === conn.OPEN; });
|
|
246
|
-
// console.log(">> Send message: ", msg.type, "to: ", to);
|
|
247
|
-
// console.log(
|
|
248
|
-
// "filteredConns ",
|
|
249
|
-
// filteredConns.map((c) => c.toolDbId)
|
|
250
|
-
// );
|
|
251
|
-
filteredConns.forEach(function (conn) {
|
|
252
|
-
if ((crossServerOnly && conn.isServer) || !crossServerOnly) {
|
|
253
|
-
if (_this.options.debug) {
|
|
254
|
-
// console.log("Sent out to: ", conn.toolDbId, conn.origUrl);
|
|
255
|
-
}
|
|
256
|
-
if (msg.type === "put" || msg.type === "crdtPut") {
|
|
257
|
-
if (conn.toolDbId &&
|
|
258
|
-
!_this.tooldb.processedOutHashes[conn.toolDbId].includes(msg.data.h)) {
|
|
259
|
-
conn.send(JSON.stringify(__assign(__assign({}, msg), { to: to })));
|
|
260
|
-
_this.tooldb.processedOutHashes[conn.toolDbId].push(msg.data.h);
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
else {
|
|
264
|
-
conn.send(JSON.stringify(__assign(__assign({}, msg), { to: to })));
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
else {
|
|
268
|
-
// console.log("Fitlered out!", conn.toolDbId, conn.origUrl);
|
|
269
|
-
}
|
|
270
|
-
});
|
|
271
|
-
};
|
|
272
|
-
toolDbNetwork.prototype.sendToClientId = function (clientId, msg) {
|
|
273
|
-
var socket = this._clientSockets[clientId];
|
|
274
|
-
if (socket) {
|
|
275
|
-
var to = lodash_1.default.uniq(__spreadArray(__spreadArray([], msg.to, true), [this.options.peerAccount.address], false));
|
|
276
|
-
if (msg.type === "put" || msg.type === "crdtPut") {
|
|
277
|
-
if (clientId &&
|
|
278
|
-
!this.tooldb.processedOutHashes[clientId].includes(msg.data.h)) {
|
|
279
|
-
socket.send(JSON.stringify(__assign(__assign({}, msg), { to: to })));
|
|
280
|
-
this.tooldb.processedOutHashes[clientId].push(msg.data.h);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
else {
|
|
284
|
-
socket.send(JSON.stringify(__assign(__assign({}, msg), { to: to })));
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
};
|
|
288
|
-
Object.defineProperty(toolDbNetwork.prototype, "tooldb", {
|
|
289
|
-
get: function () {
|
|
290
|
-
return this._tooldb;
|
|
291
|
-
},
|
|
292
|
-
enumerable: false,
|
|
293
|
-
configurable: true
|
|
294
|
-
});
|
|
295
190
|
return toolDbNetwork;
|
|
296
|
-
}());
|
|
191
|
+
}(toolDbNetworkAdapter_1.default));
|
|
297
192
|
exports.default = toolDbNetwork;
|
|
298
193
|
//# sourceMappingURL=toolDbNetwork.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toolDbNetwork.js","sourceRoot":"","sources":["../src/toolDbNetwork.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"toolDbNetwork.js","sourceRoot":"","sources":["../src/toolDbNetwork.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AACA,0CAA2B;AAC3B,sBAA2D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AAY,CAAC,EAAE,CAAC,GAAG,UAAC,IAAY;4BACnC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;wBACpB,CAAC,CAAC;oBACJ,CAAC,CAAC,CAAC;gBACL,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;SACJ;;IACH,CAAC;IAwIH,oBAAC;AAAD,CAAC,AAlND,CAA2C,8BAAoB,GAkN9D"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import ToolDb from "./tooldb";
|
|
2
|
+
import { ToolDbMessage } from "./types/message";
|
|
3
|
+
export default class ToolDbNetworkAdapter {
|
|
4
|
+
private _clientToSend;
|
|
5
|
+
private _isClientConnected;
|
|
6
|
+
private _clientIsServer;
|
|
7
|
+
private _tooldb;
|
|
8
|
+
constructor(db: ToolDb);
|
|
9
|
+
get clientToSend(): Record<string, (message: string) => void>;
|
|
10
|
+
get isClientConnected(): Record<string, () => boolean>;
|
|
11
|
+
get tooldb(): ToolDb;
|
|
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
|
+
/**
|
|
25
|
+
* Execute the function to send a message to the specified client ID
|
|
26
|
+
* @param clientId Client ID
|
|
27
|
+
* @param message Message
|
|
28
|
+
*/
|
|
29
|
+
private executeSendToClient;
|
|
30
|
+
getClientAddress(): string;
|
|
31
|
+
onClientDisconnect(clientId: string): void;
|
|
32
|
+
/**
|
|
33
|
+
* Should be called as a message payload handler
|
|
34
|
+
* This function will take care of processing the messages and also making sure
|
|
35
|
+
* we use the correct response methods troughout the adapter.
|
|
36
|
+
* @param message message payload
|
|
37
|
+
* @param clientId Client ID (can be null for ping/pong)
|
|
38
|
+
* @param setClientId Callback to set the client id on the parent class
|
|
39
|
+
*/
|
|
40
|
+
onClientMessage(message: string, clientId: string | null, setClientId: (clientId: string) => void): void;
|
|
41
|
+
/**
|
|
42
|
+
* Sends a message to all peers connected to us
|
|
43
|
+
* This function takes care of the message deduplication, making sure we dont send
|
|
44
|
+
* the same message twice to the same peer.
|
|
45
|
+
* @param msg message data
|
|
46
|
+
* @param crossServerOnly If this message should be send to server peers only
|
|
47
|
+
* @param isRelay if we should relay this message
|
|
48
|
+
*/
|
|
49
|
+
sendToAll(msg: ToolDbMessage, crossServerOnly?: boolean, isRelay?: boolean): void;
|
|
50
|
+
/**
|
|
51
|
+
* Sends a message to a single peer.
|
|
52
|
+
* This function also takes care of the message deduplication.
|
|
53
|
+
* @param clientId Peer/Client id we want to send to.
|
|
54
|
+
* @param msg message data
|
|
55
|
+
*/
|
|
56
|
+
sendToClientId(clientId: string, msg: ToolDbMessage): void;
|
|
57
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
23
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
var lodash_1 = __importDefault(require("lodash"));
|
|
27
|
+
var ToolDbNetworkAdapter = /** @class */ (function () {
|
|
28
|
+
function ToolDbNetworkAdapter(db) {
|
|
29
|
+
this._clientToSend = {};
|
|
30
|
+
this._isClientConnected = {};
|
|
31
|
+
this._clientIsServer = {};
|
|
32
|
+
this._tooldb = db;
|
|
33
|
+
}
|
|
34
|
+
Object.defineProperty(ToolDbNetworkAdapter.prototype, "clientToSend", {
|
|
35
|
+
get: function () {
|
|
36
|
+
return this._clientToSend;
|
|
37
|
+
},
|
|
38
|
+
enumerable: false,
|
|
39
|
+
configurable: true
|
|
40
|
+
});
|
|
41
|
+
Object.defineProperty(ToolDbNetworkAdapter.prototype, "isClientConnected", {
|
|
42
|
+
get: function () {
|
|
43
|
+
return this._isClientConnected;
|
|
44
|
+
},
|
|
45
|
+
enumerable: false,
|
|
46
|
+
configurable: true
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(ToolDbNetworkAdapter.prototype, "tooldb", {
|
|
49
|
+
get: function () {
|
|
50
|
+
return this._tooldb;
|
|
51
|
+
},
|
|
52
|
+
enumerable: false,
|
|
53
|
+
configurable: true
|
|
54
|
+
});
|
|
55
|
+
/**
|
|
56
|
+
* Check if the specified client is connected or not
|
|
57
|
+
* @param clientId Client ID
|
|
58
|
+
* @returns boolean
|
|
59
|
+
*/
|
|
60
|
+
ToolDbNetworkAdapter.prototype.isConnected = function (clientId) {
|
|
61
|
+
return this._isClientConnected[clientId]
|
|
62
|
+
? this._isClientConnected[clientId]()
|
|
63
|
+
: false;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Check if the specified client is a server/relay
|
|
67
|
+
* @param clientId Client ID
|
|
68
|
+
* @returns boolean
|
|
69
|
+
*/
|
|
70
|
+
ToolDbNetworkAdapter.prototype.isServer = function (clientId) {
|
|
71
|
+
return this._clientIsServer[clientId] || false;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Execute the function to send a message to the specified client ID
|
|
75
|
+
* @param clientId Client ID
|
|
76
|
+
* @param message Message
|
|
77
|
+
*/
|
|
78
|
+
ToolDbNetworkAdapter.prototype.executeSendToClient = function (clientId, message) {
|
|
79
|
+
if (this._clientToSend[clientId]) {
|
|
80
|
+
this._clientToSend[clientId](message);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
ToolDbNetworkAdapter.prototype.getClientAddress = function () {
|
|
84
|
+
// This is not a good idea to use on all adapters, so it should be replaced
|
|
85
|
+
// if its causing issues. The only reason we use the last 20 chars is to
|
|
86
|
+
// muse the same peer address as the webrtc adapter.
|
|
87
|
+
return (this.tooldb.peerAccount.getAddress() || "").slice(-20);
|
|
88
|
+
};
|
|
89
|
+
ToolDbNetworkAdapter.prototype.onClientDisconnect = function (clientId) {
|
|
90
|
+
delete this._clientToSend[clientId];
|
|
91
|
+
delete this._clientIsServer[clientId];
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Should be called as a message payload handler
|
|
95
|
+
* This function will take care of processing the messages and also making sure
|
|
96
|
+
* we use the correct response methods troughout the adapter.
|
|
97
|
+
* @param message message payload
|
|
98
|
+
* @param clientId Client ID (can be null for ping/pong)
|
|
99
|
+
* @param setClientId Callback to set the client id on the parent class
|
|
100
|
+
*/
|
|
101
|
+
ToolDbNetworkAdapter.prototype.onClientMessage = function (message, clientId, setClientId) {
|
|
102
|
+
if (this.tooldb.options.debug) {
|
|
103
|
+
console.log(message, clientId);
|
|
104
|
+
}
|
|
105
|
+
if (clientId && !this.tooldb.processedOutHashes[clientId]) {
|
|
106
|
+
this.tooldb.processedOutHashes[clientId] = [];
|
|
107
|
+
}
|
|
108
|
+
try {
|
|
109
|
+
var parsedMessage = JSON.parse(message);
|
|
110
|
+
// We assume the first messages to arrive will always be ping or pong.
|
|
111
|
+
// Only after that we can get the client id for this socket.
|
|
112
|
+
if (parsedMessage.type === "ping" || parsedMessage.type === "pong") {
|
|
113
|
+
var cid = parsedMessage.clientId;
|
|
114
|
+
setClientId(cid);
|
|
115
|
+
this.tooldb.onPeerConnect(cid);
|
|
116
|
+
this._clientIsServer[cid] = parsedMessage.isServer;
|
|
117
|
+
this.tooldb.processedOutHashes[cid] = [];
|
|
118
|
+
this.tooldb.clientOnMessage(parsedMessage, cid);
|
|
119
|
+
}
|
|
120
|
+
else if (clientId) {
|
|
121
|
+
this.tooldb.clientOnMessage(parsedMessage, clientId);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
catch (e) {
|
|
125
|
+
console.log("Got message ERR > ", message);
|
|
126
|
+
console.log(e);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
/**
|
|
130
|
+
* Sends a message to all peers connected to us
|
|
131
|
+
* This function takes care of the message deduplication, making sure we dont send
|
|
132
|
+
* the same message twice to the same peer.
|
|
133
|
+
* @param msg message data
|
|
134
|
+
* @param crossServerOnly If this message should be send to server peers only
|
|
135
|
+
* @param isRelay if we should relay this message
|
|
136
|
+
*/
|
|
137
|
+
ToolDbNetworkAdapter.prototype.sendToAll = function (msg, crossServerOnly, isRelay) {
|
|
138
|
+
var _this = this;
|
|
139
|
+
if (crossServerOnly === void 0) { crossServerOnly = false; }
|
|
140
|
+
if (isRelay === void 0) { isRelay = false; }
|
|
141
|
+
// const to = isRelay
|
|
142
|
+
// ? _.uniq([...msg.to])
|
|
143
|
+
// : _.uniq([...msg.to, this.getClientAddress()]);
|
|
144
|
+
var to = lodash_1.default.uniq(__spreadArray(__spreadArray([], msg.to, true), [this.getClientAddress()], false));
|
|
145
|
+
var finalMessage = JSON.stringify(__assign(__assign({}, msg), { to: to }));
|
|
146
|
+
var filteredConns = Object.keys(this.clientToSend)
|
|
147
|
+
.filter(function (id) { return !to.includes(id); })
|
|
148
|
+
.filter(function (clientId) { return _this.isConnected(clientId); });
|
|
149
|
+
filteredConns.forEach(function (clientId) {
|
|
150
|
+
if ((crossServerOnly && _this.isServer(clientId)) || !crossServerOnly) {
|
|
151
|
+
if (_this.tooldb.options.debug) {
|
|
152
|
+
console.log(_this.tooldb.options.storageName, to, "Sent out to: ", clientId);
|
|
153
|
+
}
|
|
154
|
+
if (msg.type === "put" || msg.type === "crdtPut") {
|
|
155
|
+
if (!_this.tooldb.processedOutHashes[clientId].includes(msg.data.h)) {
|
|
156
|
+
_this.executeSendToClient(clientId, finalMessage);
|
|
157
|
+
_this.tooldb.processedOutHashes[clientId].push(msg.data.h);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
_this.executeSendToClient(clientId, finalMessage);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// } else {
|
|
165
|
+
// console.log("Fitlered out;", clientId);
|
|
166
|
+
// }
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
/**
|
|
170
|
+
* Sends a message to a single peer.
|
|
171
|
+
* This function also takes care of the message deduplication.
|
|
172
|
+
* @param clientId Peer/Client id we want to send to.
|
|
173
|
+
* @param msg message data
|
|
174
|
+
*/
|
|
175
|
+
ToolDbNetworkAdapter.prototype.sendToClientId = function (clientId, msg) {
|
|
176
|
+
var to = lodash_1.default.uniq(__spreadArray(__spreadArray([], msg.to, true), [this.getClientAddress()], false));
|
|
177
|
+
var finalMessage = JSON.stringify(__assign(__assign({}, msg), { to: to }));
|
|
178
|
+
if (msg.type === "put" || msg.type === "crdtPut") {
|
|
179
|
+
if (clientId &&
|
|
180
|
+
!this.tooldb.processedOutHashes[clientId].includes(msg.data.h)) {
|
|
181
|
+
this.executeSendToClient(clientId, finalMessage);
|
|
182
|
+
this.tooldb.processedOutHashes[clientId].push(msg.data.h);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
this.executeSendToClient(clientId, finalMessage);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
return ToolDbNetworkAdapter;
|
|
190
|
+
}());
|
|
191
|
+
exports.default = ToolDbNetworkAdapter;
|
|
192
|
+
//# sourceMappingURL=toolDbNetworkAdapter.js.map
|