tool-db 2.5.3 → 2.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. package/README.md +82 -82
  2. package/bundle.js +1 -1
  3. package/dist/adapters-base/networkAdapter.d.ts +58 -58
  4. package/dist/adapters-base/networkAdapter.js +215 -215
  5. package/dist/adapters-base/storageAdapter.d.ts +11 -11
  6. package/dist/adapters-base/storageAdapter.js +35 -35
  7. package/dist/adapters-base/userAdapter.d.ts +15 -15
  8. package/dist/adapters-base/userAdapter.js +41 -41
  9. package/dist/crdt/baseCrdt.d.ts +9 -9
  10. package/dist/crdt/baseCrdt.js +26 -26
  11. package/dist/crdt/counterCrdt.d.ts +30 -30
  12. package/dist/crdt/counterCrdt.js +105 -105
  13. package/dist/crdt/listCrdt.d.ts +42 -42
  14. package/dist/crdt/listCrdt.js +158 -158
  15. package/dist/crdt/mapCrdt.d.ts +32 -32
  16. package/dist/crdt/mapCrdt.js +117 -117
  17. package/dist/index.d.ts +34 -34
  18. package/dist/index.js +82 -82
  19. package/dist/logger.d.ts +2 -2
  20. package/dist/logger.js +28 -28
  21. package/dist/messageHandlers/handleCrdtGet.d.ts +2 -2
  22. package/dist/messageHandlers/handleCrdtGet.js +49 -28
  23. package/dist/messageHandlers/handleCrdtGet.js.map +1 -1
  24. package/dist/messageHandlers/handleCrdtPut.d.ts +2 -2
  25. package/dist/messageHandlers/handleCrdtPut.js +102 -92
  26. package/dist/messageHandlers/handleCrdtPut.js.map +1 -1
  27. package/dist/messageHandlers/handleGet.d.ts +2 -2
  28. package/dist/messageHandlers/handleGet.js +49 -28
  29. package/dist/messageHandlers/handleGet.js.map +1 -1
  30. package/dist/messageHandlers/handlePing.d.ts +2 -2
  31. package/dist/messageHandlers/handlePing.js +35 -35
  32. package/dist/messageHandlers/handlePing.js.map +1 -1
  33. package/dist/messageHandlers/handlePong.d.ts +2 -2
  34. package/dist/messageHandlers/handlePong.js +25 -25
  35. package/dist/messageHandlers/handlePut.d.ts +2 -2
  36. package/dist/messageHandlers/handlePut.js +77 -56
  37. package/dist/messageHandlers/handlePut.js.map +1 -1
  38. package/dist/messageHandlers/handleQuery.d.ts +2 -2
  39. package/dist/messageHandlers/handleQuery.js +43 -22
  40. package/dist/messageHandlers/handleQuery.js.map +1 -1
  41. package/dist/messageHandlers/handleSubscribe.d.ts +2 -2
  42. package/dist/messageHandlers/handleSubscribe.js +43 -43
  43. package/dist/server.d.ts +1 -1
  44. package/dist/server.js +8 -8
  45. package/dist/shared.d.ts +2 -2
  46. package/dist/shared.js +6 -6
  47. package/dist/toolDbAnonSignIn.d.ts +2 -2
  48. package/dist/toolDbAnonSignIn.js +6 -6
  49. package/dist/toolDbClientOnMessage.d.ts +2 -2
  50. package/dist/toolDbClientOnMessage.js +58 -58
  51. package/dist/toolDbCrdtGet.d.ts +9 -9
  52. package/dist/toolDbCrdtGet.js +76 -76
  53. package/dist/toolDbCrdtGet.js.map +1 -1
  54. package/dist/toolDbCrdtPut.d.ts +9 -9
  55. package/dist/toolDbCrdtPut.js +62 -62
  56. package/dist/toolDbCrdtPut.js.map +1 -1
  57. package/dist/toolDbGet.d.ts +9 -9
  58. package/dist/toolDbGet.js +76 -76
  59. package/dist/toolDbGet.js.map +1 -1
  60. package/dist/toolDbKeysSignIn.d.ts +2 -2
  61. package/dist/toolDbKeysSignIn.js +15 -15
  62. package/dist/toolDbPut.d.ts +9 -9
  63. package/dist/toolDbPut.js +68 -68
  64. package/dist/toolDbPut.js.map +1 -1
  65. package/dist/toolDbQueryKeys.d.ts +8 -8
  66. package/dist/toolDbQueryKeys.js +65 -65
  67. package/dist/toolDbQueryKeys.js.map +1 -1
  68. package/dist/toolDbSignIn.d.ts +2 -2
  69. package/dist/toolDbSignIn.js +26 -26
  70. package/dist/toolDbSignIn.js.map +1 -1
  71. package/dist/toolDbSignUp.d.ts +2 -2
  72. package/dist/toolDbSignUp.js +100 -100
  73. package/dist/toolDbSignUp.js.map +1 -1
  74. package/dist/toolDbSubscribe.d.ts +9 -9
  75. package/dist/toolDbSubscribe.js +49 -49
  76. package/dist/toolDbSubscribe.js.map +1 -1
  77. package/dist/toolDbVerificationWrapper.d.ts +2 -2
  78. package/dist/toolDbVerificationWrapper.js +97 -97
  79. package/dist/tooldb.d.ts +108 -108
  80. package/dist/tooldb.js +285 -285
  81. package/dist/types/message.d.ts +100 -100
  82. package/dist/types/message.js +16 -16
  83. package/dist/types/tooldb.d.ts +84 -84
  84. package/dist/types/tooldb.js +2 -2
  85. package/dist/utils/catchReturn.d.ts +1 -1
  86. package/dist/utils/catchReturn.js +7 -7
  87. package/dist/utils/encoding/arrayBufferToHex.d.ts +1 -1
  88. package/dist/utils/encoding/arrayBufferToHex.js +18 -18
  89. package/dist/utils/encoding/arrayBufferToString.d.ts +1 -1
  90. package/dist/utils/encoding/arrayBufferToString.js +11 -11
  91. package/dist/utils/encoding/hexToArrayBuffer.d.ts +1 -1
  92. package/dist/utils/encoding/hexToArrayBuffer.js +14 -14
  93. package/dist/utils/encoding/hexToString.d.ts +1 -1
  94. package/dist/utils/encoding/hexToString.js +11 -11
  95. package/dist/utils/encoding/hexToUint8.d.ts +1 -1
  96. package/dist/utils/encoding/hexToUint8.js +7 -7
  97. package/dist/utils/encoding/stringToArrayBuffer.d.ts +1 -1
  98. package/dist/utils/encoding/stringToArrayBuffer.js +11 -11
  99. package/dist/utils/getPeerSignature.d.ts +2 -2
  100. package/dist/utils/getPeerSignature.js +8 -8
  101. package/dist/utils/getTimestamp.d.ts +1 -1
  102. package/dist/utils/getTimestamp.js +6 -6
  103. package/dist/utils/proofOfWork.d.ts +4 -4
  104. package/dist/utils/proofOfWork.js +15 -15
  105. package/dist/utils/randomAnimal.d.ts +1 -1
  106. package/dist/utils/randomAnimal.js +76 -76
  107. package/dist/utils/sha1.d.ts +1 -1
  108. package/dist/utils/sha1.js +12 -12
  109. package/dist/utils/sha256.d.ts +3 -3
  110. package/dist/utils/sha256.js +12 -12
  111. package/dist/utils/textRandom.d.ts +1 -1
  112. package/dist/utils/textRandom.js +14 -14
  113. package/dist/utils/uniq.d.ts +1 -1
  114. package/dist/utils/uniq.js +6 -6
  115. package/dist/utils/verifyMessage.d.ts +8 -8
  116. package/dist/utils/verifyMessage.js +128 -128
  117. package/dist/utils/verifyPeer.d.ts +2 -2
  118. package/dist/utils/verifyPeer.js +14 -14
  119. package/lib/adapters-base/networkAdapter.ts +217 -217
  120. package/lib/adapters-base/storageAdapter.ts +35 -35
  121. package/lib/adapters-base/userAdapter.ts +49 -49
  122. package/lib/crdt/baseCrdt.ts +21 -21
  123. package/lib/crdt/counterCrdt.ts +111 -111
  124. package/lib/crdt/listCrdt.ts +190 -190
  125. package/lib/crdt/mapCrdt.ts +119 -119
  126. package/lib/index.ts +42 -42
  127. package/lib/logger.ts +30 -30
  128. package/lib/messageHandlers/handleCrdtGet.ts +34 -29
  129. package/lib/messageHandlers/handleCrdtPut.ts +123 -118
  130. package/lib/messageHandlers/handleGet.ts +34 -29
  131. package/lib/messageHandlers/handlePing.ts +40 -40
  132. package/lib/messageHandlers/handlePong.ts +30 -30
  133. package/lib/messageHandlers/handlePut.ts +59 -54
  134. package/lib/messageHandlers/handleQuery.ts +30 -25
  135. package/lib/messageHandlers/handleSubscribe.ts +46 -46
  136. package/lib/server.ts +7 -7
  137. package/lib/shared.ts +5 -5
  138. package/lib/toolDbAnonSignIn.ts +5 -5
  139. package/lib/toolDbClientOnMessage.ts +75 -75
  140. package/lib/toolDbCrdtGet.ts +83 -82
  141. package/lib/toolDbCrdtPut.ts +78 -77
  142. package/lib/toolDbGet.ts +81 -80
  143. package/lib/toolDbKeysSignIn.ts +16 -16
  144. package/lib/toolDbPut.ts +84 -83
  145. package/lib/toolDbQueryKeys.ts +65 -64
  146. package/lib/toolDbSignIn.ts +32 -31
  147. package/lib/toolDbSignUp.ts +72 -71
  148. package/lib/toolDbSubscribe.ts +54 -53
  149. package/lib/toolDbVerificationWrapper.ts +55 -55
  150. package/lib/tooldb.ts +316 -316
  151. package/lib/types/message.ts +133 -133
  152. package/lib/types/tooldb.ts +97 -97
  153. package/lib/utils/catchReturn.ts +4 -4
  154. package/lib/utils/encoding/arrayBufferToHex.ts +18 -18
  155. package/lib/utils/encoding/arrayBufferToString.ts +8 -8
  156. package/lib/utils/encoding/hexToArrayBuffer.ts +13 -13
  157. package/lib/utils/encoding/hexToString.ts +8 -8
  158. package/lib/utils/encoding/hexToUint8.ts +5 -5
  159. package/lib/utils/encoding/stringToArrayBuffer.ts +8 -8
  160. package/lib/utils/getPeerSignature.ts +12 -12
  161. package/lib/utils/getTimestamp.ts +3 -3
  162. package/lib/utils/proofOfWork.ts +16 -16
  163. package/lib/utils/randomAnimal.ts +77 -77
  164. package/lib/utils/sha1.ts +7 -7
  165. package/lib/utils/sha256.ts +7 -7
  166. package/lib/utils/textRandom.ts +11 -11
  167. package/lib/utils/uniq.ts +3 -3
  168. package/lib/utils/verifyMessage.ts +88 -88
  169. package/lib/utils/verifyPeer.ts +15 -15
  170. package/package.json +2 -2
  171. package/tsconfig.json +14 -14
package/lib/toolDbGet.ts CHANGED
@@ -1,80 +1,81 @@
1
- import { ToolDb, textRandom } 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 toolDbGet<T = any>(
11
- this: ToolDb,
12
- key: string,
13
- userNamespaced = false,
14
- timeoutMs = 1000
15
- ): Promise<T | null> {
16
- return new Promise((resolve, reject) => {
17
- if (userNamespaced && this.userAccount.getAddress() === undefined) {
18
- reject(new Error("You are not authorized yet!"));
19
- return;
20
- }
21
- const finalKey = userNamespaced
22
- ? `:${this.userAccount.getAddress()}.${key}`
23
- : key;
24
- this.logger("GET", finalKey);
25
-
26
- const msgId = textRandom(10);
27
-
28
- const cancelTimeout = setTimeout(() => {
29
- this.store
30
- .get(finalKey)
31
- .then((data) => {
32
- try {
33
- const message = JSON.parse(data);
34
- this.emit("data", message);
35
- resolve(message.v);
36
- } catch (e) {
37
- resolve(null);
38
- }
39
- })
40
- .catch((e) => {
41
- resolve(null);
42
- });
43
- }, timeoutMs);
44
-
45
- this.addIdListener(msgId, (msg) => {
46
- this.logger("GET RECV", finalKey);
47
-
48
- clearTimeout(cancelTimeout);
49
- if (msg.type === "put") {
50
- resolve(msg.data.v);
51
- }
52
- });
53
-
54
- this.store
55
- .get(finalKey)
56
- .then((data) => {
57
- try {
58
- const parsed = JSON.parse(data);
59
- const val = parsed.v;
60
- clearTimeout(cancelTimeout);
61
- this.removeIdListener(msgId);
62
- this.emit("data", parsed);
63
- resolve(val);
64
- } catch (e) {
65
- // do nothing
66
- }
67
- })
68
- .catch((e) => {
69
- // do nothing
70
- });
71
-
72
- // Do get
73
- this.network.sendToAll({
74
- type: "get",
75
- to: [],
76
- key: finalKey,
77
- id: msgId,
78
- });
79
- });
80
- }
1
+ import { ToolDb, textRandom } 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 toolDbGet<T = any>(
11
+ this: ToolDb,
12
+ key: string,
13
+ userNamespaced = false,
14
+ timeoutMs = 1000,
15
+ to?: string[]
16
+ ): Promise<T | null> {
17
+ return new Promise((resolve, reject) => {
18
+ if (userNamespaced && this.userAccount.getAddress() === undefined) {
19
+ reject(new Error("You are not authorized yet!"));
20
+ return;
21
+ }
22
+ const finalKey = userNamespaced
23
+ ? `:${this.userAccount.getAddress()}.${key}`
24
+ : key;
25
+ this.logger("GET", finalKey);
26
+
27
+ const msgId = textRandom(10);
28
+
29
+ const cancelTimeout = setTimeout(() => {
30
+ this.store
31
+ .get(finalKey)
32
+ .then((data) => {
33
+ try {
34
+ const message = JSON.parse(data);
35
+ this.emit("data", message);
36
+ resolve(message.v);
37
+ } catch (e) {
38
+ resolve(null);
39
+ }
40
+ })
41
+ .catch((e) => {
42
+ resolve(null);
43
+ });
44
+ }, timeoutMs);
45
+
46
+ this.addIdListener(msgId, (msg) => {
47
+ this.logger("GET RECV", finalKey);
48
+
49
+ clearTimeout(cancelTimeout);
50
+ if (msg.type === "put") {
51
+ resolve(msg.data.v);
52
+ }
53
+ });
54
+
55
+ this.store
56
+ .get(finalKey)
57
+ .then((data) => {
58
+ try {
59
+ const parsed = JSON.parse(data);
60
+ const val = parsed.v;
61
+ clearTimeout(cancelTimeout);
62
+ this.removeIdListener(msgId);
63
+ this.emit("data", parsed);
64
+ resolve(val);
65
+ } catch (e) {
66
+ // do nothing
67
+ }
68
+ })
69
+ .catch((e) => {
70
+ // do nothing
71
+ });
72
+
73
+ // Do get
74
+ this.network.sendToAll({
75
+ type: "get",
76
+ to: to || [],
77
+ key: finalKey,
78
+ id: msgId,
79
+ });
80
+ });
81
+ }
@@ -1,16 +1,16 @@
1
- import { ToolDb, randomAnimal } from ".";
2
-
3
- export default function toolDbKeysSignIn(
4
- this: ToolDb,
5
- privateKey: string,
6
- username?: string
7
- ): Promise<unknown> {
8
- if (!this.userAccount) return Promise.resolve(undefined);
9
-
10
- return this.userAccount
11
- .getAccountFromPrivate(privateKey)
12
- .then((newAccount) => {
13
- this.userAccount.setUser(newAccount, username || randomAnimal());
14
- return newAccount;
15
- });
16
- }
1
+ import { ToolDb, randomAnimal } from ".";
2
+
3
+ export default function toolDbKeysSignIn(
4
+ this: ToolDb,
5
+ privateKey: string,
6
+ username?: string
7
+ ): Promise<unknown> {
8
+ if (!this.userAccount) return Promise.resolve(undefined);
9
+
10
+ return this.userAccount
11
+ .getAccountFromPrivate(privateKey)
12
+ .then((newAccount) => {
13
+ this.userAccount.setUser(newAccount, username || randomAnimal());
14
+ return newAccount;
15
+ });
16
+ }
package/lib/toolDbPut.ts CHANGED
@@ -1,83 +1,84 @@
1
- import {
2
- ToolDb,
3
- PutMessage,
4
- textRandom,
5
- VerificationData,
6
- proofOfWork,
7
- } from ".";
8
-
9
- /**
10
- * Triggers a PUT request to other peers.
11
- * @param key key where we want to put the data at.
12
- * @param value Data we want to any (any type)
13
- * @param userNamespaced If this key bolongs to a user or its public. Making it private will enforce validation for our address and signatures.
14
- * @returns Promise<Data | null>
15
- */
16
- export default function toolDbPut<T = any>(
17
- this: ToolDb,
18
- key: string,
19
- value: T,
20
- userNamespaced = false
21
- ): Promise<PutMessage<T> | null> {
22
- return new Promise((resolve, reject) => {
23
- if (key.includes(".")) {
24
- // Dots are used as a delimitator character between bublic keys and the key of the user's data
25
- reject(new Error(`Key cannot include dots!; ${key}`));
26
- return;
27
- }
28
-
29
- if (!this.userAccount || !this.userAccount.getAddress()) {
30
- reject(new Error("You need to log in before you can PUT."));
31
- return;
32
- }
33
-
34
- const timestamp = new Date().getTime();
35
- const dataString = `${JSON.stringify(
36
- value
37
- )}${this.userAccount.getAddress()}${timestamp}`;
38
-
39
- // WORK
40
- proofOfWork(dataString, this.options.pow)
41
- .then(({ hash, nonce }) => {
42
- this.userAccount.signData(hash).then((signature) => {
43
- if (signature && this.userAccount.getAddress()) {
44
- const finalKey = userNamespaced
45
- ? `:${this.userAccount.getAddress()}.${key}`
46
- : key;
47
-
48
- // Compose the message
49
- const data: VerificationData = {
50
- k: finalKey,
51
- a: this.userAccount.getAddress() || "",
52
- n: nonce,
53
- t: timestamp,
54
- h: hash,
55
- s: signature,
56
- v: value,
57
- c: null,
58
- };
59
-
60
- this.logger("PUT", key, data);
61
-
62
- const finalMessage: PutMessage = {
63
- type: "put",
64
- id: textRandom(10),
65
- to: [],
66
- data,
67
- };
68
-
69
- this.network.sendToAll(finalMessage);
70
- this.store
71
- .put(finalKey, JSON.stringify(data))
72
- .catch((e) => {
73
- // do nothing
74
- })
75
- .finally(() => {
76
- resolve(finalMessage);
77
- });
78
- }
79
- });
80
- })
81
- .catch(reject);
82
- });
83
- }
1
+ import {
2
+ ToolDb,
3
+ PutMessage,
4
+ textRandom,
5
+ VerificationData,
6
+ proofOfWork,
7
+ } from ".";
8
+
9
+ /**
10
+ * Triggers a PUT request to other peers.
11
+ * @param key key where we want to put the data at.
12
+ * @param value Data we want to any (any type)
13
+ * @param userNamespaced If this key bolongs to a user or its public. Making it private will enforce validation for our address and signatures.
14
+ * @returns Promise<Data | null>
15
+ */
16
+ export default function toolDbPut<T = any>(
17
+ this: ToolDb,
18
+ key: string,
19
+ value: T,
20
+ userNamespaced = false,
21
+ to?: string[]
22
+ ): Promise<PutMessage<T> | null> {
23
+ return new Promise((resolve, reject) => {
24
+ if (key.includes(".")) {
25
+ // Dots are used as a delimitator character between bublic keys and the key of the user's data
26
+ reject(new Error(`Key cannot include dots!; ${key}`));
27
+ return;
28
+ }
29
+
30
+ if (!this.userAccount || !this.userAccount.getAddress()) {
31
+ reject(new Error("You need to log in before you can PUT."));
32
+ return;
33
+ }
34
+
35
+ const timestamp = new Date().getTime();
36
+ const dataString = `${JSON.stringify(
37
+ value
38
+ )}${this.userAccount.getAddress()}${timestamp}`;
39
+
40
+ // WORK
41
+ proofOfWork(dataString, this.options.pow)
42
+ .then(({ hash, nonce }) => {
43
+ this.userAccount.signData(hash).then((signature) => {
44
+ if (signature && this.userAccount.getAddress()) {
45
+ const finalKey = userNamespaced
46
+ ? `:${this.userAccount.getAddress()}.${key}`
47
+ : key;
48
+
49
+ // Compose the message
50
+ const data: VerificationData = {
51
+ k: finalKey,
52
+ a: this.userAccount.getAddress() || "",
53
+ n: nonce,
54
+ t: timestamp,
55
+ h: hash,
56
+ s: signature,
57
+ v: value,
58
+ c: null,
59
+ };
60
+
61
+ this.logger("PUT", key, data);
62
+
63
+ const finalMessage: PutMessage = {
64
+ type: "put",
65
+ id: textRandom(10),
66
+ to: to || [],
67
+ data,
68
+ };
69
+
70
+ this.network.sendToAll(finalMessage);
71
+ this.store
72
+ .put(finalKey, JSON.stringify(data))
73
+ .catch((e) => {
74
+ // do nothing
75
+ })
76
+ .finally(() => {
77
+ resolve(finalMessage);
78
+ });
79
+ }
80
+ });
81
+ })
82
+ .catch(reject);
83
+ });
84
+ }
@@ -1,64 +1,65 @@
1
- import { ToolDb, textRandom, uniq } from ".";
2
-
3
- /**
4
- * Triggers a QUERY request to other peers.
5
- * @param key start of the key
6
- * @param userNamespaced If this key bolongs to a user or its public.
7
- * @returns Promise<Data>
8
- */
9
- export default function toolDbQueryKeys(
10
- this: ToolDb,
11
- key: string,
12
- userNamespaced = false,
13
- timeoutMs = 1000
14
- ): Promise<string[] | null> {
15
- const user = this.userAccount;
16
-
17
- return new Promise((resolve, reject) => {
18
- if (userNamespaced && user.getAddress() === undefined) {
19
- reject(new Error("You are not authorized yet!"));
20
- return;
21
- }
22
- const finalKey = userNamespaced ? `:${user.getAddress()}.${key}` : key;
23
- this.logger("QUERY", finalKey);
24
-
25
- const msgId = textRandom(10);
26
- let foundKeys: string[] = [];
27
- let timeout: NodeJS.Timeout | undefined;
28
-
29
- this.store
30
- .query(finalKey)
31
- .then((localKeys) => {
32
- foundKeys = [...foundKeys, ...localKeys];
33
- timeout = setTimeout(finishListening, timeoutMs);
34
- })
35
- .catch((e) => {
36
- // do nothing
37
- });
38
-
39
- const finishListening = () => {
40
- resolve(uniq(foundKeys));
41
- };
42
-
43
- this.addIdListener(msgId, (msg) => {
44
- this.logger("QUERY RECV", finalKey);
45
-
46
- if (msg.type === "queryAck") {
47
- foundKeys = [...foundKeys, ...msg.keys];
48
-
49
- if (timeout) {
50
- clearTimeout(timeout);
51
- }
52
- timeout = setTimeout(finishListening, timeoutMs);
53
- }
54
- });
55
-
56
- // Do get
57
- this.network.sendToAll({
58
- type: "query",
59
- to: [],
60
- key: finalKey,
61
- id: msgId,
62
- });
63
- });
64
- }
1
+ import { ToolDb, textRandom, uniq } from ".";
2
+
3
+ /**
4
+ * Triggers a QUERY request to other peers.
5
+ * @param key start of the key
6
+ * @param userNamespaced If this key bolongs to a user or its public.
7
+ * @returns Promise<Data>
8
+ */
9
+ export default function toolDbQueryKeys(
10
+ this: ToolDb,
11
+ key: string,
12
+ userNamespaced = false,
13
+ timeoutMs = 1000,
14
+ to?: string[]
15
+ ): Promise<string[] | null> {
16
+ const user = this.userAccount;
17
+
18
+ return new Promise((resolve, reject) => {
19
+ if (userNamespaced && user.getAddress() === undefined) {
20
+ reject(new Error("You are not authorized yet!"));
21
+ return;
22
+ }
23
+ const finalKey = userNamespaced ? `:${user.getAddress()}.${key}` : key;
24
+ this.logger("QUERY", finalKey);
25
+
26
+ const msgId = textRandom(10);
27
+ let foundKeys: string[] = [];
28
+ let timeout: NodeJS.Timeout | undefined;
29
+
30
+ this.store
31
+ .query(finalKey)
32
+ .then((localKeys) => {
33
+ foundKeys = [...foundKeys, ...localKeys];
34
+ timeout = setTimeout(finishListening, timeoutMs);
35
+ })
36
+ .catch((e) => {
37
+ // do nothing
38
+ });
39
+
40
+ const finishListening = () => {
41
+ resolve(uniq(foundKeys));
42
+ };
43
+
44
+ this.addIdListener(msgId, (msg) => {
45
+ this.logger("QUERY RECV", finalKey);
46
+
47
+ if (msg.type === "queryAck") {
48
+ foundKeys = [...foundKeys, ...msg.keys];
49
+
50
+ if (timeout) {
51
+ clearTimeout(timeout);
52
+ }
53
+ timeout = setTimeout(finishListening, timeoutMs);
54
+ }
55
+ });
56
+
57
+ // Do get
58
+ this.network.sendToAll({
59
+ type: "query",
60
+ to: to || [],
61
+ key: finalKey,
62
+ id: msgId,
63
+ });
64
+ });
65
+ }
@@ -1,31 +1,32 @@
1
- import { ToolDb, sha256, randomAnimal } from ".";
2
-
3
- export default function toolDbSignIn(
4
- this: ToolDb,
5
- user: string,
6
- password: string
7
- ): Promise<unknown | undefined> {
8
- return new Promise((resolve, reject) => {
9
- this.getData<unknown>(`==${user}`, false, 5000).then((_user) => {
10
- if (!_user) {
11
- reject("Could not find user");
12
- return;
13
- }
14
-
15
- try {
16
- this.userAccount
17
- .decryptAccount(_user, sha256(password))
18
- .then((newAccount) => {
19
- this.userAccount.setUser(
20
- newAccount,
21
- user || `Anonymous ${randomAnimal()}`
22
- );
23
-
24
- resolve(_user);
25
- });
26
- } catch (e) {
27
- reject(e);
28
- }
29
- });
30
- });
31
- }
1
+ import { ToolDb, sha256, randomAnimal } from ".";
2
+
3
+ export default function toolDbSignIn(
4
+ this: ToolDb,
5
+ user: string,
6
+ password: string,
7
+ to?: string[]
8
+ ): Promise<unknown | undefined> {
9
+ return new Promise((resolve, reject) => {
10
+ this.getData<unknown>(`==${user}`, false, 5000, to).then((_user) => {
11
+ if (!_user) {
12
+ reject("Could not find user");
13
+ return;
14
+ }
15
+
16
+ try {
17
+ this.userAccount
18
+ .decryptAccount(_user, sha256(password))
19
+ .then((newAccount) => {
20
+ this.userAccount.setUser(
21
+ newAccount,
22
+ user || `Anonymous ${randomAnimal()}`
23
+ );
24
+
25
+ resolve(_user);
26
+ });
27
+ } catch (e) {
28
+ reject(e);
29
+ }
30
+ });
31
+ });
32
+ }