tool-db 2.5.3 → 2.5.5
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 +35 -34
- package/dist/index.js +84 -82
- package/dist/index.js.map +1 -1
- 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/handleFunction.d.ts +2 -0
- package/dist/messageHandlers/handleFunction.js +56 -0
- package/dist/messageHandlers/handleFunction.js.map +1 -0
- 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 +61 -58
- package/dist/toolDbClientOnMessage.js.map +1 -1
- 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/toolDbFunction.d.ts +9 -0
- package/dist/toolDbFunction.js +38 -0
- package/dist/toolDbFunction.js.map +1 -0
- 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 +123 -108
- package/dist/tooldb.js +309 -285
- package/dist/tooldb.js.map +1 -1
- package/dist/types/message.d.ts +110 -100
- package/dist/types/message.js +16 -16
- package/dist/types/message.js.map +1 -1
- package/dist/types/tooldb.d.ts +110 -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 +43 -42
- package/lib/logger.ts +30 -30
- package/lib/messageHandlers/handleCrdtGet.ts +34 -29
- package/lib/messageHandlers/handleCrdtPut.ts +123 -118
- package/lib/messageHandlers/handleFunction.ts +59 -0
- 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 +79 -75
- package/lib/toolDbCrdtGet.ts +83 -82
- package/lib/toolDbCrdtPut.ts +78 -77
- package/lib/toolDbFunction.ts +49 -0
- 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 +343 -316
- package/lib/types/message.ts +154 -133
- package/lib/types/tooldb.ts +139 -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,25 +1,30 @@
|
|
|
1
|
-
import { ToolDb, QueryAckMessage, QueryMessage } from "..";
|
|
2
|
-
|
|
3
|
-
export default function handleQuery(
|
|
4
|
-
this: ToolDb,
|
|
5
|
-
message: QueryMessage,
|
|
6
|
-
remotePeerId: string
|
|
7
|
-
) {
|
|
8
|
-
this.store
|
|
9
|
-
.query(message.key)
|
|
10
|
-
.then((keys) => {
|
|
11
|
-
this.network.sendToClientId(remotePeerId, {
|
|
12
|
-
type: "queryAck",
|
|
13
|
-
id: message.id,
|
|
14
|
-
to: [],
|
|
15
|
-
keys,
|
|
16
|
-
} as QueryAckMessage);
|
|
17
|
-
})
|
|
18
|
-
.catch((e) => {
|
|
19
|
-
// do nothing
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
if (this.options.server) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
1
|
+
import { ToolDb, QueryAckMessage, QueryMessage } from "..";
|
|
2
|
+
|
|
3
|
+
export default function handleQuery(
|
|
4
|
+
this: ToolDb,
|
|
5
|
+
message: QueryMessage,
|
|
6
|
+
remotePeerId: string
|
|
7
|
+
) {
|
|
8
|
+
this.store
|
|
9
|
+
.query(message.key)
|
|
10
|
+
.then((keys) => {
|
|
11
|
+
this.network.sendToClientId(remotePeerId, {
|
|
12
|
+
type: "queryAck",
|
|
13
|
+
id: message.id,
|
|
14
|
+
to: [],
|
|
15
|
+
keys,
|
|
16
|
+
} as QueryAckMessage);
|
|
17
|
+
})
|
|
18
|
+
.catch((e) => {
|
|
19
|
+
// do nothing
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
if (this.options.server) {
|
|
23
|
+
const finalMessage: QueryMessage = {
|
|
24
|
+
...message,
|
|
25
|
+
to: [...message.to, remotePeerId],
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
this.network.sendToAll(finalMessage, true);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
import { ToolDb, PutMessage, SubscribeMessage } from "..";
|
|
2
|
-
|
|
3
|
-
export default function handleSubscribe(
|
|
4
|
-
this: ToolDb,
|
|
5
|
-
message: SubscribeMessage,
|
|
6
|
-
remotePeerId: string
|
|
7
|
-
) {
|
|
8
|
-
const subId = remotePeerId + "-" + message.key;
|
|
9
|
-
if (!this.subscriptions.includes(subId)) {
|
|
10
|
-
this.subscriptions.push(subId);
|
|
11
|
-
|
|
12
|
-
this.addKeyListener(message.key, (msg) => {
|
|
13
|
-
if (remotePeerId) {
|
|
14
|
-
// We do not reply to the socket directly
|
|
15
|
-
// instead we use the client id, in case the socket reconnects
|
|
16
|
-
const newMsg: PutMessage = {
|
|
17
|
-
data: msg,
|
|
18
|
-
id: message.id,
|
|
19
|
-
type: "put",
|
|
20
|
-
to: [],
|
|
21
|
-
};
|
|
22
|
-
this.network.sendToClientId(remotePeerId, newMsg);
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// basically the exact same as GET, below
|
|
28
|
-
this.store
|
|
29
|
-
.get(message.key)
|
|
30
|
-
.then((data) => {
|
|
31
|
-
try {
|
|
32
|
-
const oldData: PutMessage = {
|
|
33
|
-
data: JSON.parse(data),
|
|
34
|
-
id: message.id,
|
|
35
|
-
type: "put",
|
|
36
|
-
to: [],
|
|
37
|
-
};
|
|
38
|
-
this.network.sendToClientId(remotePeerId, oldData);
|
|
39
|
-
} catch (e) {
|
|
40
|
-
// do nothing
|
|
41
|
-
}
|
|
42
|
-
})
|
|
43
|
-
.catch((e) => {
|
|
44
|
-
// do nothing
|
|
45
|
-
});
|
|
46
|
-
}
|
|
1
|
+
import { ToolDb, PutMessage, SubscribeMessage } from "..";
|
|
2
|
+
|
|
3
|
+
export default function handleSubscribe(
|
|
4
|
+
this: ToolDb,
|
|
5
|
+
message: SubscribeMessage,
|
|
6
|
+
remotePeerId: string
|
|
7
|
+
) {
|
|
8
|
+
const subId = remotePeerId + "-" + message.key;
|
|
9
|
+
if (!this.subscriptions.includes(subId)) {
|
|
10
|
+
this.subscriptions.push(subId);
|
|
11
|
+
|
|
12
|
+
this.addKeyListener(message.key, (msg) => {
|
|
13
|
+
if (remotePeerId) {
|
|
14
|
+
// We do not reply to the socket directly
|
|
15
|
+
// instead we use the client id, in case the socket reconnects
|
|
16
|
+
const newMsg: PutMessage = {
|
|
17
|
+
data: msg,
|
|
18
|
+
id: message.id,
|
|
19
|
+
type: "put",
|
|
20
|
+
to: [],
|
|
21
|
+
};
|
|
22
|
+
this.network.sendToClientId(remotePeerId, newMsg);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// basically the exact same as GET, below
|
|
28
|
+
this.store
|
|
29
|
+
.get(message.key)
|
|
30
|
+
.then((data) => {
|
|
31
|
+
try {
|
|
32
|
+
const oldData: PutMessage = {
|
|
33
|
+
data: JSON.parse(data),
|
|
34
|
+
id: message.id,
|
|
35
|
+
type: "put",
|
|
36
|
+
to: [],
|
|
37
|
+
};
|
|
38
|
+
this.network.sendToClientId(remotePeerId, oldData);
|
|
39
|
+
} catch (e) {
|
|
40
|
+
// do nothing
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
.catch((e) => {
|
|
44
|
+
// do nothing
|
|
45
|
+
});
|
|
46
|
+
}
|
package/lib/server.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ToolDb } from ".";
|
|
2
|
-
|
|
3
|
-
console.log("Starting server..");
|
|
4
|
-
const server = new ToolDb({
|
|
5
|
-
port: 8080,
|
|
6
|
-
server: true,
|
|
7
|
-
});
|
|
1
|
+
import { ToolDb } from ".";
|
|
2
|
+
|
|
3
|
+
console.log("Starting server..");
|
|
4
|
+
const server = new ToolDb({
|
|
5
|
+
port: 8080,
|
|
6
|
+
server: true,
|
|
7
|
+
});
|
package/lib/shared.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const shared = {
|
|
2
|
-
toolDb: undefined,
|
|
3
|
-
} as any;
|
|
4
|
-
|
|
5
|
-
export default shared;
|
|
1
|
+
const shared = {
|
|
2
|
+
toolDb: undefined,
|
|
3
|
+
} as any;
|
|
4
|
+
|
|
5
|
+
export default shared;
|
package/lib/toolDbAnonSignIn.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { ToolDb } from ".";
|
|
2
|
-
|
|
3
|
-
export default function toolDbAnonSignIn(this: ToolDb): void {
|
|
4
|
-
this.userAccount.anonUser();
|
|
5
|
-
}
|
|
1
|
+
import { ToolDb } from ".";
|
|
2
|
+
|
|
3
|
+
export default function toolDbAnonSignIn(this: ToolDb): void {
|
|
4
|
+
this.userAccount.anonUser();
|
|
5
|
+
}
|
|
@@ -1,75 +1,79 @@
|
|
|
1
|
-
import { ToolDb, ToolDbMessage } from ".";
|
|
2
|
-
|
|
3
|
-
export default function toolDbClientOnMessage(
|
|
4
|
-
this: ToolDb,
|
|
5
|
-
message: ToolDbMessage,
|
|
6
|
-
remotePeerId: string
|
|
7
|
-
) {
|
|
8
|
-
if (!this.processedIds[message.type]) {
|
|
9
|
-
this.processedIds[message.type] = [];
|
|
10
|
-
}
|
|
11
|
-
if (this.processedIds[message.type].includes(message.id)) {
|
|
12
|
-
// this.logger(
|
|
13
|
-
// `Already processed this message > ${message.type} from ${remotePeerId}`
|
|
14
|
-
// );
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
this.emit("message", message, remotePeerId);
|
|
19
|
-
|
|
20
|
-
this.processedIds[message.type].push(message.id);
|
|
21
|
-
|
|
22
|
-
this.logger(`Got message ${message.type} from ${remotePeerId}`);
|
|
23
|
-
this.logger(message);
|
|
24
|
-
|
|
25
|
-
// Check if we are listening for this ID
|
|
26
|
-
if (message.id) {
|
|
27
|
-
const msgId = message.id;
|
|
28
|
-
if (this._idListeners[msgId]) {
|
|
29
|
-
this._idListeners[msgId](message);
|
|
30
|
-
this.removeIdListener(msgId);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
if (message === undefined || message.type === undefined) {
|
|
35
|
-
this.logger("Message is invalid!", message, typeof message);
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
switch (message.type) {
|
|
40
|
-
case "ping":
|
|
41
|
-
this.handlePing(message, remotePeerId);
|
|
42
|
-
break;
|
|
43
|
-
|
|
44
|
-
case "pong":
|
|
45
|
-
this.handlePong(message, remotePeerId);
|
|
46
|
-
break;
|
|
47
|
-
|
|
48
|
-
case "subscribe":
|
|
49
|
-
this.handleSubscribe(message, remotePeerId);
|
|
50
|
-
break;
|
|
51
|
-
|
|
52
|
-
case "get":
|
|
53
|
-
this.handleGet(message, remotePeerId);
|
|
54
|
-
break;
|
|
55
|
-
|
|
56
|
-
case "put":
|
|
57
|
-
this.handlePut(message, remotePeerId);
|
|
58
|
-
break;
|
|
59
|
-
|
|
60
|
-
case "crdtPut":
|
|
61
|
-
this.handleCrdtPut(message, remotePeerId);
|
|
62
|
-
break;
|
|
63
|
-
|
|
64
|
-
case "crdtGet":
|
|
65
|
-
this.handleCrdtGet(message, remotePeerId);
|
|
66
|
-
break;
|
|
67
|
-
|
|
68
|
-
case "query":
|
|
69
|
-
this.handleQuery(message, remotePeerId);
|
|
70
|
-
break;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
1
|
+
import { ToolDb, ToolDbMessage } from ".";
|
|
2
|
+
|
|
3
|
+
export default function toolDbClientOnMessage(
|
|
4
|
+
this: ToolDb,
|
|
5
|
+
message: ToolDbMessage,
|
|
6
|
+
remotePeerId: string
|
|
7
|
+
) {
|
|
8
|
+
if (!this.processedIds[message.type]) {
|
|
9
|
+
this.processedIds[message.type] = [];
|
|
10
|
+
}
|
|
11
|
+
if (this.processedIds[message.type].includes(message.id)) {
|
|
12
|
+
// this.logger(
|
|
13
|
+
// `Already processed this message > ${message.type} from ${remotePeerId}`
|
|
14
|
+
// );
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
this.emit("message", message, remotePeerId);
|
|
19
|
+
|
|
20
|
+
this.processedIds[message.type].push(message.id);
|
|
21
|
+
|
|
22
|
+
this.logger(`Got message ${message.type} from ${remotePeerId}`);
|
|
23
|
+
this.logger(message);
|
|
24
|
+
|
|
25
|
+
// Check if we are listening for this ID
|
|
26
|
+
if (message.id) {
|
|
27
|
+
const msgId = message.id;
|
|
28
|
+
if (this._idListeners[msgId]) {
|
|
29
|
+
this._idListeners[msgId](message);
|
|
30
|
+
this.removeIdListener(msgId);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (message === undefined || message.type === undefined) {
|
|
35
|
+
this.logger("Message is invalid!", message, typeof message);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
switch (message.type) {
|
|
40
|
+
case "ping":
|
|
41
|
+
this.handlePing(message, remotePeerId);
|
|
42
|
+
break;
|
|
43
|
+
|
|
44
|
+
case "pong":
|
|
45
|
+
this.handlePong(message, remotePeerId);
|
|
46
|
+
break;
|
|
47
|
+
|
|
48
|
+
case "subscribe":
|
|
49
|
+
this.handleSubscribe(message, remotePeerId);
|
|
50
|
+
break;
|
|
51
|
+
|
|
52
|
+
case "get":
|
|
53
|
+
this.handleGet(message, remotePeerId);
|
|
54
|
+
break;
|
|
55
|
+
|
|
56
|
+
case "put":
|
|
57
|
+
this.handlePut(message, remotePeerId);
|
|
58
|
+
break;
|
|
59
|
+
|
|
60
|
+
case "crdtPut":
|
|
61
|
+
this.handleCrdtPut(message, remotePeerId);
|
|
62
|
+
break;
|
|
63
|
+
|
|
64
|
+
case "crdtGet":
|
|
65
|
+
this.handleCrdtGet(message, remotePeerId);
|
|
66
|
+
break;
|
|
67
|
+
|
|
68
|
+
case "query":
|
|
69
|
+
this.handleQuery(message, remotePeerId);
|
|
70
|
+
break;
|
|
71
|
+
|
|
72
|
+
case "function":
|
|
73
|
+
this.handleFunction(message, remotePeerId);
|
|
74
|
+
break;
|
|
75
|
+
|
|
76
|
+
default:
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
package/lib/toolDbCrdtGet.ts
CHANGED
|
@@ -1,82 +1,83 @@
|
|
|
1
|
-
import { textRandom, BaseCrdt, ToolDb, CrdtPutMessage } from ".";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Triggers a GET request to other peers. If the data is available locally it will return that instead.
|
|
5
|
-
* @param key key of the data
|
|
6
|
-
* @param userNamespaced If this key bolongs to a user or its public. Making it private will enforce validation for our address and signatures.
|
|
7
|
-
* @param timeout Max time to wait for remote.
|
|
8
|
-
* @returns Promise<Data>
|
|
9
|
-
*/
|
|
10
|
-
export default function toolDbCrdtGet<T = any>(
|
|
11
|
-
this: ToolDb,
|
|
12
|
-
key: string,
|
|
13
|
-
crdt: BaseCrdt<T, any, any>,
|
|
14
|
-
userNamespaced = false,
|
|
15
|
-
timeoutMs = 1000
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
.
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
.
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
1
|
+
import { textRandom, BaseCrdt, ToolDb, CrdtPutMessage } from ".";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Triggers a GET request to other peers. If the data is available locally it will return that instead.
|
|
5
|
+
* @param key key of the data
|
|
6
|
+
* @param userNamespaced If this key bolongs to a user or its public. Making it private will enforce validation for our address and signatures.
|
|
7
|
+
* @param timeout Max time to wait for remote.
|
|
8
|
+
* @returns Promise<Data>
|
|
9
|
+
*/
|
|
10
|
+
export default function toolDbCrdtGet<T = any>(
|
|
11
|
+
this: ToolDb,
|
|
12
|
+
key: string,
|
|
13
|
+
crdt: BaseCrdt<T, any, any>,
|
|
14
|
+
userNamespaced = false,
|
|
15
|
+
timeoutMs = 1000,
|
|
16
|
+
to?: string[]
|
|
17
|
+
): Promise<CrdtPutMessage<T> | null> {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
if (userNamespaced && this.userAccount.getAddress() === undefined) {
|
|
20
|
+
reject(new Error("You are not authorized yet!"));
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const finalKey = userNamespaced
|
|
24
|
+
? `:${this.userAccount.getAddress()}.${key}`
|
|
25
|
+
: key;
|
|
26
|
+
this.logger("CRDT GET", finalKey);
|
|
27
|
+
|
|
28
|
+
const msgId = textRandom(10);
|
|
29
|
+
|
|
30
|
+
const cancelTimeout = setTimeout(() => {
|
|
31
|
+
this.store
|
|
32
|
+
.get(finalKey)
|
|
33
|
+
.then((data) => {
|
|
34
|
+
try {
|
|
35
|
+
const message = JSON.parse(data);
|
|
36
|
+
crdt.mergeChanges(message.v);
|
|
37
|
+
|
|
38
|
+
this.emit("data", message);
|
|
39
|
+
resolve(message);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
resolve(null);
|
|
42
|
+
}
|
|
43
|
+
})
|
|
44
|
+
.catch((e) => reject(null));
|
|
45
|
+
}, timeoutMs);
|
|
46
|
+
|
|
47
|
+
this.addIdListener(msgId, (msg) => {
|
|
48
|
+
this.logger("GET RECV", finalKey);
|
|
49
|
+
|
|
50
|
+
clearTimeout(cancelTimeout);
|
|
51
|
+
if (msg.type === "crdtPut") {
|
|
52
|
+
crdt.mergeChanges(msg.data.v);
|
|
53
|
+
resolve(msg);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
this.store
|
|
58
|
+
.get(finalKey)
|
|
59
|
+
.then((data) => {
|
|
60
|
+
try {
|
|
61
|
+
const msg = JSON.parse(data);
|
|
62
|
+
clearTimeout(cancelTimeout);
|
|
63
|
+
this.removeIdListener(msgId);
|
|
64
|
+
crdt.mergeChanges(msg.v);
|
|
65
|
+
this.emit("data", msg);
|
|
66
|
+
resolve(msg);
|
|
67
|
+
} catch (e) {
|
|
68
|
+
// do nothing
|
|
69
|
+
}
|
|
70
|
+
})
|
|
71
|
+
.catch(() => {
|
|
72
|
+
// do nothing
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
// Do get
|
|
76
|
+
this.network.sendToAll({
|
|
77
|
+
type: "crdtGet",
|
|
78
|
+
to: to || [],
|
|
79
|
+
key: finalKey,
|
|
80
|
+
id: msgId,
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
}
|
package/lib/toolDbCrdtPut.ts
CHANGED
|
@@ -1,77 +1,78 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ToolDb,
|
|
3
|
-
CrdtPutMessage,
|
|
4
|
-
textRandom,
|
|
5
|
-
VerificationData,
|
|
6
|
-
proofOfWork,
|
|
7
|
-
BaseCrdt,
|
|
8
|
-
} from ".";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Triggers a PUT request to other peers.
|
|
12
|
-
* @param key key where we want to put the data at.
|
|
13
|
-
* @param value Data we want to any (any type)
|
|
14
|
-
* @param userNamespaced If this key bolongs to a user or its public. Making it private will enforce validation for our address and signatures.
|
|
15
|
-
* @returns Promise<Data | null>
|
|
16
|
-
*/
|
|
17
|
-
export default function toolDbCrdtPut<T = any>(
|
|
18
|
-
this: ToolDb,
|
|
19
|
-
key: string,
|
|
20
|
-
crdt: BaseCrdt<T, any, any>,
|
|
21
|
-
userNamespaced = false
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
ToolDb,
|
|
3
|
+
CrdtPutMessage,
|
|
4
|
+
textRandom,
|
|
5
|
+
VerificationData,
|
|
6
|
+
proofOfWork,
|
|
7
|
+
BaseCrdt,
|
|
8
|
+
} from ".";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Triggers a PUT request to other peers.
|
|
12
|
+
* @param key key where we want to put the data at.
|
|
13
|
+
* @param value Data we want to any (any type)
|
|
14
|
+
* @param userNamespaced If this key bolongs to a user or its public. Making it private will enforce validation for our address and signatures.
|
|
15
|
+
* @returns Promise<Data | null>
|
|
16
|
+
*/
|
|
17
|
+
export default function toolDbCrdtPut<T = any>(
|
|
18
|
+
this: ToolDb,
|
|
19
|
+
key: string,
|
|
20
|
+
crdt: BaseCrdt<T, any, any>,
|
|
21
|
+
userNamespaced = false,
|
|
22
|
+
to?: string[]
|
|
23
|
+
): Promise<CrdtPutMessage | null> {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
if (key.includes(".")) {
|
|
26
|
+
// Dots are used as a delimitator character between bublic keys and the key of the user's data
|
|
27
|
+
reject(new Error(`Key cannot include dots!; ${key}`));
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (!this.userAccount.getAddress()) {
|
|
32
|
+
reject(new Error("You need to log in before you can PUT."));
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const timestamp = new Date().getTime();
|
|
37
|
+
|
|
38
|
+
const crdtChanges = crdt.getChanges();
|
|
39
|
+
|
|
40
|
+
const encodedData = JSON.stringify(crdtChanges);
|
|
41
|
+
|
|
42
|
+
const dataString = `${encodedData}${this.userAccount.getAddress()}${timestamp}`;
|
|
43
|
+
|
|
44
|
+
// WORK
|
|
45
|
+
proofOfWork(dataString, this.options.pow)
|
|
46
|
+
.then(({ hash, nonce }) => {
|
|
47
|
+
this.userAccount.signData(hash).then((signature) => {
|
|
48
|
+
if (signature && this.userAccount.getAddress()) {
|
|
49
|
+
// Compose the message
|
|
50
|
+
const data: VerificationData<any> = {
|
|
51
|
+
k: userNamespaced
|
|
52
|
+
? `:${this.userAccount.getAddress()}.${key}`
|
|
53
|
+
: key,
|
|
54
|
+
a: this.userAccount.getAddress() || "",
|
|
55
|
+
n: nonce,
|
|
56
|
+
t: timestamp,
|
|
57
|
+
h: hash,
|
|
58
|
+
s: signature,
|
|
59
|
+
v: crdtChanges,
|
|
60
|
+
c: crdt.type,
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
this.logger("PUT CRDT", key, data);
|
|
64
|
+
|
|
65
|
+
const finalMessage: CrdtPutMessage<any> = {
|
|
66
|
+
type: "crdtPut",
|
|
67
|
+
id: textRandom(10),
|
|
68
|
+
to: to || [],
|
|
69
|
+
data,
|
|
70
|
+
};
|
|
71
|
+
this.network.sendToAll(finalMessage);
|
|
72
|
+
resolve(finalMessage);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
})
|
|
76
|
+
.catch(reject);
|
|
77
|
+
});
|
|
78
|
+
}
|