tool-db 2.6.1 → 2.6.3

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.
Files changed (83) hide show
  1. package/bundle.js +1 -1
  2. package/dist/adapters-base/storageAdapter.js +1 -1
  3. package/dist/adapters-base/userAdapter.js +1 -1
  4. package/dist/crdt/counterCrdt.d.ts +2 -2
  5. package/dist/crdt/listCrdt.d.ts +2 -2
  6. package/dist/crdt/mapCrdt.d.ts +2 -2
  7. package/dist/messageHandlers/handlePut.js +101 -12
  8. package/dist/messageHandlers/handlePut.js.map +1 -1
  9. package/dist/toolDbAnonSignIn.js +1 -1
  10. package/dist/toolDbCheckSignupConflicts.d.ts +7 -0
  11. package/dist/toolDbCheckSignupConflicts.js +123 -0
  12. package/dist/toolDbCheckSignupConflicts.js.map +1 -0
  13. package/dist/toolDbKeysSignIn.js +1 -1
  14. package/dist/toolDbSignIn.js +1 -1
  15. package/dist/toolDbSignUp.d.ts +10 -0
  16. package/dist/toolDbSignUp.js +83 -54
  17. package/dist/toolDbSignUp.js.map +1 -1
  18. package/dist/toolDbVerificationWrapper.js +1 -1
  19. package/dist/tooldb.d.ts +18 -0
  20. package/dist/tooldb.js +120 -32
  21. package/dist/tooldb.js.map +1 -1
  22. package/dist/types/message.d.ts +2 -2
  23. package/dist/types/tooldb.d.ts +7 -7
  24. package/dist/utils/verifyMessage.js +1 -1
  25. package/package.json +3 -3
  26. package/lib/adapters-base/networkAdapter.ts +0 -218
  27. package/lib/adapters-base/storageAdapter.ts +0 -57
  28. package/lib/adapters-base/userAdapter.ts +0 -49
  29. package/lib/crdt/baseCrdt.ts +0 -21
  30. package/lib/crdt/counterCrdt.ts +0 -112
  31. package/lib/crdt/listCrdt.ts +0 -190
  32. package/lib/crdt/mapCrdt.ts +0 -121
  33. package/lib/index.ts +0 -49
  34. package/lib/logger.ts +0 -30
  35. package/lib/messageHandlers/handleCrdtGet.ts +0 -34
  36. package/lib/messageHandlers/handleCrdtPut.ts +0 -133
  37. package/lib/messageHandlers/handleFunction.ts +0 -59
  38. package/lib/messageHandlers/handleGet.ts +0 -34
  39. package/lib/messageHandlers/handlePing.ts +0 -40
  40. package/lib/messageHandlers/handlePong.ts +0 -37
  41. package/lib/messageHandlers/handlePut.ts +0 -59
  42. package/lib/messageHandlers/handleQuery.ts +0 -30
  43. package/lib/messageHandlers/handleSubscribe.ts +0 -46
  44. package/lib/server.ts +0 -7
  45. package/lib/shared.ts +0 -5
  46. package/lib/toolDbAnonSignIn.ts +0 -5
  47. package/lib/toolDbClientOnMessage.ts +0 -79
  48. package/lib/toolDbCrdtGet.ts +0 -83
  49. package/lib/toolDbCrdtPut.ts +0 -78
  50. package/lib/toolDbFunction.ts +0 -49
  51. package/lib/toolDbGet.ts +0 -81
  52. package/lib/toolDbKeysSignIn.ts +0 -13
  53. package/lib/toolDbPut.ts +0 -84
  54. package/lib/toolDbQueryKeys.ts +0 -71
  55. package/lib/toolDbSignIn.ts +0 -37
  56. package/lib/toolDbSignUp.ts +0 -72
  57. package/lib/toolDbSubscribe.ts +0 -54
  58. package/lib/toolDbVerificationWrapper.ts +0 -55
  59. package/lib/tooldb.ts +0 -343
  60. package/lib/types/message.ts +0 -154
  61. package/lib/types/tooldb.ts +0 -140
  62. package/lib/utils/catchReturn.ts +0 -4
  63. package/lib/utils/encoding/arrayBufferToHex.ts +0 -18
  64. package/lib/utils/encoding/arrayBufferToString.ts +0 -4
  65. package/lib/utils/encoding/base64ToUint8.ts +0 -13
  66. package/lib/utils/encoding/fromBase64.ts +0 -4
  67. package/lib/utils/encoding/hexToArrayBuffer.ts +0 -13
  68. package/lib/utils/encoding/hexToString.ts +0 -8
  69. package/lib/utils/encoding/hexToUint8.ts +0 -5
  70. package/lib/utils/encoding/stringToArrayBuffer.ts +0 -4
  71. package/lib/utils/encoding/toBase64.ts +0 -4
  72. package/lib/utils/encoding/uint8ToBase64.ts +0 -11
  73. package/lib/utils/generateGroupKey.ts +0 -11
  74. package/lib/utils/getPeerSignature.ts +0 -12
  75. package/lib/utils/getTimestamp.ts +0 -3
  76. package/lib/utils/proofOfWork.ts +0 -31
  77. package/lib/utils/randomAnimal.ts +0 -77
  78. package/lib/utils/sha1.ts +0 -7
  79. package/lib/utils/sha256.ts +0 -7
  80. package/lib/utils/textRandom.ts +0 -11
  81. package/lib/utils/uniq.ts +0 -3
  82. package/lib/utils/verifyMessage.ts +0 -88
  83. package/lib/utils/verifyPeer.ts +0 -15
@@ -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
@@ -1,7 +0,0 @@
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 DELETED
@@ -1,5 +0,0 @@
1
- const shared = {
2
- toolDb: undefined,
3
- } as any;
4
-
5
- export default shared;
@@ -1,5 +0,0 @@
1
- import { ToolDb } from ".";
2
-
3
- export default async function toolDbAnonSignIn(this: ToolDb): Promise<void> {
4
- await this.userAccount.anonUser();
5
- }
@@ -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
- }
@@ -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
- }
@@ -1,78 +0,0 @@
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
- }
@@ -1,49 +0,0 @@
1
- import {
2
- AllowedFunctionArguments,
3
- FunctionReturn,
4
- GenericObject,
5
- textRandom,
6
- ToolDb,
7
- } from ".";
8
-
9
- /**
10
- * Triggers a FUNCTION request to other peers. If the function executes sucessfully it will return code "OK"
11
- * @param function function name
12
- * @param args arguments for the function
13
- * @param timeout Max time to wait for remote.
14
- * @returns Promise<FunctionReturn>
15
- */
16
- export default function toolDbFunction<R, A = GenericObject>(
17
- this: ToolDb,
18
- fName: string,
19
- args: AllowedFunctionArguments<A>,
20
- timeoutMs = 10000
21
- ): Promise<FunctionReturn<R>> {
22
- return new Promise((resolve, reject) => {
23
- this.logger("FUNCTION > " + fName);
24
-
25
- const msgId = textRandom(10);
26
-
27
- const cancelTimeout = setTimeout(() => {
28
- resolve({ return: "Timed out", code: "ERR" });
29
- }, timeoutMs);
30
-
31
- this.addIdListener(msgId, (msg) => {
32
- this.logger("FUNCTION RECV > " + fName, msg);
33
-
34
- clearTimeout(cancelTimeout);
35
- if (msg.type === "functionReturn") {
36
- resolve({ return: msg.return, code: msg.code });
37
- }
38
- });
39
-
40
- // Do get
41
- this.network.sendToAll({
42
- type: "function",
43
- to: [],
44
- function: fName,
45
- args: args,
46
- id: msgId,
47
- });
48
- });
49
- }