tool-db 2.6.2 → 2.6.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundle.js +1 -1
- package/dist/adapters-base/storageAdapter.js +1 -1
- package/dist/adapters-base/userAdapter.js +1 -1
- package/dist/crdt/counterCrdt.d.ts +2 -2
- package/dist/crdt/listCrdt.d.ts +2 -2
- package/dist/crdt/mapCrdt.d.ts +2 -2
- package/dist/messageHandlers/handlePut.js +101 -12
- package/dist/messageHandlers/handlePut.js.map +1 -1
- package/dist/toolDbAnonSignIn.js +1 -1
- package/dist/toolDbCheckSignupConflicts.d.ts +7 -0
- package/dist/toolDbCheckSignupConflicts.js +123 -0
- package/dist/toolDbCheckSignupConflicts.js.map +1 -0
- package/dist/toolDbKeysSignIn.js +1 -1
- package/dist/toolDbSignIn.js +1 -1
- package/dist/toolDbSignUp.d.ts +10 -0
- package/dist/toolDbSignUp.js +83 -54
- package/dist/toolDbSignUp.js.map +1 -1
- package/dist/toolDbVerificationWrapper.js +1 -1
- package/dist/tooldb.d.ts +18 -0
- package/dist/tooldb.js +120 -32
- package/dist/tooldb.js.map +1 -1
- package/dist/types/message.d.ts +2 -2
- package/dist/types/tooldb.d.ts +7 -7
- package/dist/utils/verifyMessage.js +11 -3
- package/dist/utils/verifyMessage.js.map +1 -1
- package/package.json +3 -3
- package/lib/adapters-base/networkAdapter.ts +0 -218
- package/lib/adapters-base/storageAdapter.ts +0 -57
- package/lib/adapters-base/userAdapter.ts +0 -49
- package/lib/crdt/baseCrdt.ts +0 -21
- package/lib/crdt/counterCrdt.ts +0 -112
- package/lib/crdt/listCrdt.ts +0 -190
- package/lib/crdt/mapCrdt.ts +0 -121
- package/lib/index.ts +0 -49
- package/lib/logger.ts +0 -30
- package/lib/messageHandlers/handleCrdtGet.ts +0 -34
- package/lib/messageHandlers/handleCrdtPut.ts +0 -133
- package/lib/messageHandlers/handleFunction.ts +0 -59
- package/lib/messageHandlers/handleGet.ts +0 -34
- package/lib/messageHandlers/handlePing.ts +0 -40
- package/lib/messageHandlers/handlePong.ts +0 -37
- package/lib/messageHandlers/handlePut.ts +0 -59
- package/lib/messageHandlers/handleQuery.ts +0 -30
- package/lib/messageHandlers/handleSubscribe.ts +0 -46
- package/lib/server.ts +0 -7
- package/lib/shared.ts +0 -5
- package/lib/toolDbAnonSignIn.ts +0 -5
- package/lib/toolDbClientOnMessage.ts +0 -79
- package/lib/toolDbCrdtGet.ts +0 -83
- package/lib/toolDbCrdtPut.ts +0 -78
- package/lib/toolDbFunction.ts +0 -49
- package/lib/toolDbGet.ts +0 -81
- package/lib/toolDbKeysSignIn.ts +0 -13
- package/lib/toolDbPut.ts +0 -84
- package/lib/toolDbQueryKeys.ts +0 -71
- package/lib/toolDbSignIn.ts +0 -37
- package/lib/toolDbSignUp.ts +0 -72
- package/lib/toolDbSubscribe.ts +0 -54
- package/lib/toolDbVerificationWrapper.ts +0 -55
- package/lib/tooldb.ts +0 -343
- package/lib/types/message.ts +0 -154
- package/lib/types/tooldb.ts +0 -140
- package/lib/utils/catchReturn.ts +0 -4
- package/lib/utils/encoding/arrayBufferToHex.ts +0 -18
- package/lib/utils/encoding/arrayBufferToString.ts +0 -4
- package/lib/utils/encoding/base64ToUint8.ts +0 -13
- package/lib/utils/encoding/fromBase64.ts +0 -4
- package/lib/utils/encoding/hexToArrayBuffer.ts +0 -13
- package/lib/utils/encoding/hexToString.ts +0 -8
- package/lib/utils/encoding/hexToUint8.ts +0 -5
- package/lib/utils/encoding/stringToArrayBuffer.ts +0 -4
- package/lib/utils/encoding/toBase64.ts +0 -4
- package/lib/utils/encoding/uint8ToBase64.ts +0 -11
- package/lib/utils/generateGroupKey.ts +0 -11
- package/lib/utils/getPeerSignature.ts +0 -12
- package/lib/utils/getTimestamp.ts +0 -3
- package/lib/utils/proofOfWork.ts +0 -31
- package/lib/utils/randomAnimal.ts +0 -77
- package/lib/utils/sha1.ts +0 -7
- package/lib/utils/sha256.ts +0 -7
- package/lib/utils/textRandom.ts +0 -11
- package/lib/utils/uniq.ts +0 -3
- package/lib/utils/verifyMessage.ts +0 -88
- package/lib/utils/verifyPeer.ts +0 -15
|
@@ -1,133 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ToolDb,
|
|
3
|
-
VerifyResult,
|
|
4
|
-
CrdtPutMessage,
|
|
5
|
-
VerificationData,
|
|
6
|
-
MapCrdt,
|
|
7
|
-
MapChanges,
|
|
8
|
-
CRDT_COUNTER,
|
|
9
|
-
CRDT_LIST,
|
|
10
|
-
CRDT_MAP,
|
|
11
|
-
ListCrdt,
|
|
12
|
-
ListChanges,
|
|
13
|
-
CounterCrdt,
|
|
14
|
-
CounterChanges,
|
|
15
|
-
} from "..";
|
|
16
|
-
|
|
17
|
-
import toolDbVerificationWrapper from "../toolDbVerificationWrapper";
|
|
18
|
-
|
|
19
|
-
export default function handleCrdtPut(
|
|
20
|
-
this: ToolDb,
|
|
21
|
-
message: CrdtPutMessage,
|
|
22
|
-
remotePeerId: string
|
|
23
|
-
) {
|
|
24
|
-
toolDbVerificationWrapper.call(this, message.data).then((value) => {
|
|
25
|
-
// this.logger("Verification wrapper result: ", value, message.k);
|
|
26
|
-
if (value === VerifyResult.Verified) {
|
|
27
|
-
const finalMessage: CrdtPutMessage = {
|
|
28
|
-
...message,
|
|
29
|
-
to: [...message.to, remotePeerId],
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
this.emit("crdtput", finalMessage);
|
|
33
|
-
this.emit("data", finalMessage.data);
|
|
34
|
-
this.emit("verified", finalMessage);
|
|
35
|
-
// relay to other servers !!!
|
|
36
|
-
this.network.sendToAll(finalMessage, true);
|
|
37
|
-
|
|
38
|
-
this.store
|
|
39
|
-
.get(finalMessage.data.k)
|
|
40
|
-
.then((oldData) => {
|
|
41
|
-
if (oldData) {
|
|
42
|
-
try {
|
|
43
|
-
const parsedOldData: VerificationData<any> = JSON.parse(oldData);
|
|
44
|
-
|
|
45
|
-
let newMessage = finalMessage;
|
|
46
|
-
|
|
47
|
-
// Merge old document with new data incoming and save it
|
|
48
|
-
// Add handles for all kinds of CRDT we add
|
|
49
|
-
let oldDoc:
|
|
50
|
-
| MapCrdt<any>
|
|
51
|
-
| ListCrdt<any>
|
|
52
|
-
| CounterCrdt<any>
|
|
53
|
-
| undefined;
|
|
54
|
-
|
|
55
|
-
if (parsedOldData.c === CRDT_MAP) {
|
|
56
|
-
oldDoc = new MapCrdt(
|
|
57
|
-
this.userAccount.getAddress() || "",
|
|
58
|
-
parsedOldData.v
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (parsedOldData.c === CRDT_LIST) {
|
|
63
|
-
oldDoc = new ListCrdt(
|
|
64
|
-
this.userAccount.getAddress() || "",
|
|
65
|
-
parsedOldData.v
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (parsedOldData.c === CRDT_COUNTER) {
|
|
70
|
-
oldDoc = new CounterCrdt(
|
|
71
|
-
this.userAccount.getAddress() || "",
|
|
72
|
-
parsedOldData.v
|
|
73
|
-
);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
let changesMerged:
|
|
77
|
-
| MapChanges<any>[]
|
|
78
|
-
| ListChanges<any>[]
|
|
79
|
-
| CounterChanges[] = [];
|
|
80
|
-
|
|
81
|
-
if (oldDoc) {
|
|
82
|
-
oldDoc.mergeChanges(finalMessage.data.v);
|
|
83
|
-
changesMerged = oldDoc.getChanges();
|
|
84
|
-
}
|
|
85
|
-
newMessage = {
|
|
86
|
-
...finalMessage,
|
|
87
|
-
};
|
|
88
|
-
newMessage.data.v = changesMerged;
|
|
89
|
-
|
|
90
|
-
if (parsedOldData.t < finalMessage.data.t) {
|
|
91
|
-
const key = newMessage.data.k;
|
|
92
|
-
this.triggerKeyListener(key, newMessage.data);
|
|
93
|
-
this.store
|
|
94
|
-
.put(newMessage.data.k, JSON.stringify(newMessage.data))
|
|
95
|
-
.catch((e) => {
|
|
96
|
-
// do nothing
|
|
97
|
-
});
|
|
98
|
-
} else {
|
|
99
|
-
const key = message.data.k;
|
|
100
|
-
this.triggerKeyListener(key, parsedOldData);
|
|
101
|
-
}
|
|
102
|
-
// } else {
|
|
103
|
-
// this.logger(
|
|
104
|
-
// `${message.k} has old data, but its newer. old ${parsedOldData.t} < new ${message.t}`
|
|
105
|
-
// );
|
|
106
|
-
// }
|
|
107
|
-
} catch (e) {
|
|
108
|
-
this.logger("Couldnt parse crdt data", oldData, e);
|
|
109
|
-
}
|
|
110
|
-
} else {
|
|
111
|
-
const key = finalMessage.data.k;
|
|
112
|
-
this.triggerKeyListener(key, finalMessage.data);
|
|
113
|
-
this.store
|
|
114
|
-
.put(finalMessage.data.k, JSON.stringify(finalMessage.data))
|
|
115
|
-
.catch((e) => {
|
|
116
|
-
this.logger("Couldnt insert crdt data", e);
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
})
|
|
120
|
-
.catch((e) => {
|
|
121
|
-
const key = finalMessage.data.k;
|
|
122
|
-
this.triggerKeyListener(key, finalMessage.data);
|
|
123
|
-
this.store
|
|
124
|
-
.put(finalMessage.data.k, JSON.stringify(finalMessage.data))
|
|
125
|
-
.catch((e) => {
|
|
126
|
-
// do nothing
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
} else {
|
|
130
|
-
this.logger("unverified message: ", value, message);
|
|
131
|
-
}
|
|
132
|
-
});
|
|
133
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { FunctionMessage, FunctionReturnMessage, ToolDb } from "..";
|
|
2
|
-
|
|
3
|
-
export default function handleFunction(
|
|
4
|
-
this: ToolDb,
|
|
5
|
-
message: FunctionMessage,
|
|
6
|
-
remotePeerId: string
|
|
7
|
-
) {
|
|
8
|
-
// executes the given function by message on the server if it exists
|
|
9
|
-
if (this.functions[message.function]) {
|
|
10
|
-
try {
|
|
11
|
-
this.functions[message.function](message.args)
|
|
12
|
-
.then((ret) => {
|
|
13
|
-
const messageReturn: FunctionReturnMessage = {
|
|
14
|
-
return: ret,
|
|
15
|
-
type: "functionReturn",
|
|
16
|
-
id: message.id,
|
|
17
|
-
code: "OK",
|
|
18
|
-
to: [],
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
this.network.sendToClientId(remotePeerId, messageReturn);
|
|
22
|
-
})
|
|
23
|
-
.catch((e) => {
|
|
24
|
-
const messageReturn: FunctionReturnMessage = {
|
|
25
|
-
return: e.toString(),
|
|
26
|
-
type: "functionReturn",
|
|
27
|
-
id: message.id,
|
|
28
|
-
code: "ERR",
|
|
29
|
-
to: [],
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
this.network.sendToClientId(remotePeerId, messageReturn);
|
|
33
|
-
});
|
|
34
|
-
} catch (e: any) {
|
|
35
|
-
// something went wrong, nothing to do here
|
|
36
|
-
// We might want to return the exception to the client?
|
|
37
|
-
const messageReturn: FunctionReturnMessage = {
|
|
38
|
-
return: e.toString(),
|
|
39
|
-
type: "functionReturn",
|
|
40
|
-
id: message.id,
|
|
41
|
-
code: "ERR",
|
|
42
|
-
to: [],
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
this.network.sendToClientId(remotePeerId, messageReturn);
|
|
46
|
-
}
|
|
47
|
-
} else {
|
|
48
|
-
// function not found
|
|
49
|
-
const messageReturn: FunctionReturnMessage = {
|
|
50
|
-
return: "Function not found",
|
|
51
|
-
type: "functionReturn",
|
|
52
|
-
id: message.id,
|
|
53
|
-
code: "NOT_FOUND",
|
|
54
|
-
to: [],
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
this.network.sendToClientId(remotePeerId, messageReturn);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { ToolDb, GetMessage, PutMessage } from "..";
|
|
2
|
-
|
|
3
|
-
export default function handleGet(
|
|
4
|
-
this: ToolDb,
|
|
5
|
-
message: GetMessage,
|
|
6
|
-
remotePeerId: string
|
|
7
|
-
) {
|
|
8
|
-
this.store
|
|
9
|
-
.get(message.key)
|
|
10
|
-
.then((data) => {
|
|
11
|
-
try {
|
|
12
|
-
// Use the id of the get so the other client knows we are replying
|
|
13
|
-
const oldData = {
|
|
14
|
-
type: "put",
|
|
15
|
-
data: JSON.parse(data),
|
|
16
|
-
to: [],
|
|
17
|
-
id: message.id,
|
|
18
|
-
} as PutMessage;
|
|
19
|
-
this.network.sendToClientId(remotePeerId, oldData);
|
|
20
|
-
} catch (e) {
|
|
21
|
-
// socket.send(data);
|
|
22
|
-
// do nothing
|
|
23
|
-
}
|
|
24
|
-
})
|
|
25
|
-
.catch((e) => {
|
|
26
|
-
const finalMessage: GetMessage = {
|
|
27
|
-
...message,
|
|
28
|
-
to: [...message.to, remotePeerId],
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
this.logger("Local key not found, relay", JSON.stringify(finalMessage));
|
|
32
|
-
this.network.sendToAll(finalMessage, false);
|
|
33
|
-
});
|
|
34
|
-
}
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { ToolDb, verifyPeer, PingMessage, PongMessage } from "..";
|
|
2
|
-
|
|
3
|
-
export default function handlePing(
|
|
4
|
-
this: ToolDb,
|
|
5
|
-
message: PingMessage,
|
|
6
|
-
remotePeerId: string
|
|
7
|
-
) {
|
|
8
|
-
if (!this.isConnected) {
|
|
9
|
-
this.isConnected = true;
|
|
10
|
-
this.onConnect();
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
verifyPeer(this, message.peer).then((verified) => {
|
|
14
|
-
// Verify integrity and topic
|
|
15
|
-
if (verified && message.peer.topic === this.options.topic) {
|
|
16
|
-
// Add this peer to our list of peers
|
|
17
|
-
const filteredPeers = this.serverPeers.filter(
|
|
18
|
-
(p) => p.address === message.peer.address
|
|
19
|
-
);
|
|
20
|
-
if (filteredPeers.length === 0 && message.isServer) {
|
|
21
|
-
// Add this peer to the list
|
|
22
|
-
this.serverPeers.push(message.peer);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
this.network.sendToClientId(remotePeerId, {
|
|
26
|
-
type: "pong",
|
|
27
|
-
isServer: this.options.server,
|
|
28
|
-
clientId: this.network.getClientAddress(),
|
|
29
|
-
to: [],
|
|
30
|
-
servers: this.serverPeers,
|
|
31
|
-
id: message.id,
|
|
32
|
-
} as PongMessage);
|
|
33
|
-
|
|
34
|
-
this.onPeerConnect(this.peerAccount.getAddress() || "");
|
|
35
|
-
} else {
|
|
36
|
-
this.logger("Blocked a remote peer from joining; ", verified, message);
|
|
37
|
-
// Drop connection here!
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { ToolDb, verifyPeer, PongMessage } from "..";
|
|
2
|
-
|
|
3
|
-
export default function handlePong(
|
|
4
|
-
this: ToolDb,
|
|
5
|
-
message: PongMessage,
|
|
6
|
-
remotePeerId: string
|
|
7
|
-
) {
|
|
8
|
-
if (!this.isConnected) {
|
|
9
|
-
this.isConnected = true;
|
|
10
|
-
this.onConnect();
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
message.servers.forEach((peer) => {
|
|
14
|
-
// Check for duplicates first (synchronously) to avoid race conditions
|
|
15
|
-
const filteredPeers = this.serverPeers.filter(
|
|
16
|
-
(p) => p.address === peer.address
|
|
17
|
-
);
|
|
18
|
-
|
|
19
|
-
if (filteredPeers.length === 0 && peer.host && peer.port) {
|
|
20
|
-
verifyPeer(this, peer).then((verified) => {
|
|
21
|
-
// Verify integrity and topic
|
|
22
|
-
if (verified && peer.topic === this.options.topic) {
|
|
23
|
-
// Double-check for duplicates after async verification
|
|
24
|
-
const recheck = this.serverPeers.filter(
|
|
25
|
-
(p) => p.address === peer.address
|
|
26
|
-
);
|
|
27
|
-
if (recheck.length === 0) {
|
|
28
|
-
// Add this peer to the list
|
|
29
|
-
this.serverPeers.push(peer);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
this.onPeerConnect(this.peerAccount.getAddress() || "");
|
|
37
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { ToolDb, PutMessage, VerifyResult } from "..";
|
|
2
|
-
|
|
3
|
-
import toolDbVerificationWrapper from "../toolDbVerificationWrapper";
|
|
4
|
-
|
|
5
|
-
export default function handlePut(
|
|
6
|
-
this: ToolDb,
|
|
7
|
-
message: PutMessage,
|
|
8
|
-
remotePeerId: string
|
|
9
|
-
) {
|
|
10
|
-
toolDbVerificationWrapper.call(this, message.data).then((value) => {
|
|
11
|
-
// this.logger("Verification wrapper result: ", value, message.k);
|
|
12
|
-
if (value === VerifyResult.Verified) {
|
|
13
|
-
this.emit("put", message);
|
|
14
|
-
this.emit("data", message.data);
|
|
15
|
-
this.emit("verified", message);
|
|
16
|
-
// relay to other servers !!!
|
|
17
|
-
const finalMessage: PutMessage = {
|
|
18
|
-
...message,
|
|
19
|
-
to: [...message.to, remotePeerId],
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
this.network.sendToAll(finalMessage, true);
|
|
23
|
-
|
|
24
|
-
this.store
|
|
25
|
-
.get(finalMessage.data.k)
|
|
26
|
-
.then((oldData) => {
|
|
27
|
-
const parsedOldData = JSON.parse(oldData);
|
|
28
|
-
if (parsedOldData.t < finalMessage.data.t) {
|
|
29
|
-
const key = finalMessage.data.k;
|
|
30
|
-
this.triggerKeyListener(key, finalMessage.data);
|
|
31
|
-
this.store
|
|
32
|
-
.put(finalMessage.data.k, JSON.stringify(finalMessage.data))
|
|
33
|
-
.catch((e) => {
|
|
34
|
-
// do nothing
|
|
35
|
-
});
|
|
36
|
-
} else {
|
|
37
|
-
const key = finalMessage.data.k;
|
|
38
|
-
this.triggerKeyListener(key, parsedOldData);
|
|
39
|
-
}
|
|
40
|
-
// } else {
|
|
41
|
-
// this.logger(
|
|
42
|
-
// `${message.k} has old data, but its newer. old ${parsedOldData.t} < new ${message.t}`
|
|
43
|
-
// );
|
|
44
|
-
// }
|
|
45
|
-
})
|
|
46
|
-
.catch((e) => {
|
|
47
|
-
const key = message.data.k;
|
|
48
|
-
this.triggerKeyListener(key, message.data);
|
|
49
|
-
this.store
|
|
50
|
-
.put(message.data.k, JSON.stringify(message.data))
|
|
51
|
-
.catch((e) => {
|
|
52
|
-
//
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
} else {
|
|
56
|
-
this.logger("unverified message: ", value, message);
|
|
57
|
-
}
|
|
58
|
-
});
|
|
59
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
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
DELETED
package/lib/shared.ts
DELETED
package/lib/toolDbAnonSignIn.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
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) => resolve(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
|
-
}
|