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.
Files changed (183) 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 +35 -34
  18. package/dist/index.js +84 -82
  19. package/dist/index.js.map +1 -1
  20. package/dist/logger.d.ts +2 -2
  21. package/dist/logger.js +28 -28
  22. package/dist/messageHandlers/handleCrdtGet.d.ts +2 -2
  23. package/dist/messageHandlers/handleCrdtGet.js +49 -28
  24. package/dist/messageHandlers/handleCrdtGet.js.map +1 -1
  25. package/dist/messageHandlers/handleCrdtPut.d.ts +2 -2
  26. package/dist/messageHandlers/handleCrdtPut.js +102 -92
  27. package/dist/messageHandlers/handleCrdtPut.js.map +1 -1
  28. package/dist/messageHandlers/handleFunction.d.ts +2 -0
  29. package/dist/messageHandlers/handleFunction.js +56 -0
  30. package/dist/messageHandlers/handleFunction.js.map +1 -0
  31. package/dist/messageHandlers/handleGet.d.ts +2 -2
  32. package/dist/messageHandlers/handleGet.js +49 -28
  33. package/dist/messageHandlers/handleGet.js.map +1 -1
  34. package/dist/messageHandlers/handlePing.d.ts +2 -2
  35. package/dist/messageHandlers/handlePing.js +35 -35
  36. package/dist/messageHandlers/handlePing.js.map +1 -1
  37. package/dist/messageHandlers/handlePong.d.ts +2 -2
  38. package/dist/messageHandlers/handlePong.js +25 -25
  39. package/dist/messageHandlers/handlePut.d.ts +2 -2
  40. package/dist/messageHandlers/handlePut.js +77 -56
  41. package/dist/messageHandlers/handlePut.js.map +1 -1
  42. package/dist/messageHandlers/handleQuery.d.ts +2 -2
  43. package/dist/messageHandlers/handleQuery.js +43 -22
  44. package/dist/messageHandlers/handleQuery.js.map +1 -1
  45. package/dist/messageHandlers/handleSubscribe.d.ts +2 -2
  46. package/dist/messageHandlers/handleSubscribe.js +43 -43
  47. package/dist/server.d.ts +1 -1
  48. package/dist/server.js +8 -8
  49. package/dist/shared.d.ts +2 -2
  50. package/dist/shared.js +6 -6
  51. package/dist/toolDbAnonSignIn.d.ts +2 -2
  52. package/dist/toolDbAnonSignIn.js +6 -6
  53. package/dist/toolDbClientOnMessage.d.ts +2 -2
  54. package/dist/toolDbClientOnMessage.js +61 -58
  55. package/dist/toolDbClientOnMessage.js.map +1 -1
  56. package/dist/toolDbCrdtGet.d.ts +9 -9
  57. package/dist/toolDbCrdtGet.js +76 -76
  58. package/dist/toolDbCrdtGet.js.map +1 -1
  59. package/dist/toolDbCrdtPut.d.ts +9 -9
  60. package/dist/toolDbCrdtPut.js +62 -62
  61. package/dist/toolDbCrdtPut.js.map +1 -1
  62. package/dist/toolDbFunction.d.ts +9 -0
  63. package/dist/toolDbFunction.js +38 -0
  64. package/dist/toolDbFunction.js.map +1 -0
  65. package/dist/toolDbGet.d.ts +9 -9
  66. package/dist/toolDbGet.js +76 -76
  67. package/dist/toolDbGet.js.map +1 -1
  68. package/dist/toolDbKeysSignIn.d.ts +2 -2
  69. package/dist/toolDbKeysSignIn.js +15 -15
  70. package/dist/toolDbPut.d.ts +9 -9
  71. package/dist/toolDbPut.js +68 -68
  72. package/dist/toolDbPut.js.map +1 -1
  73. package/dist/toolDbQueryKeys.d.ts +8 -8
  74. package/dist/toolDbQueryKeys.js +65 -65
  75. package/dist/toolDbQueryKeys.js.map +1 -1
  76. package/dist/toolDbSignIn.d.ts +2 -2
  77. package/dist/toolDbSignIn.js +26 -26
  78. package/dist/toolDbSignIn.js.map +1 -1
  79. package/dist/toolDbSignUp.d.ts +2 -2
  80. package/dist/toolDbSignUp.js +100 -100
  81. package/dist/toolDbSignUp.js.map +1 -1
  82. package/dist/toolDbSubscribe.d.ts +9 -9
  83. package/dist/toolDbSubscribe.js +49 -49
  84. package/dist/toolDbSubscribe.js.map +1 -1
  85. package/dist/toolDbVerificationWrapper.d.ts +2 -2
  86. package/dist/toolDbVerificationWrapper.js +97 -97
  87. package/dist/tooldb.d.ts +123 -108
  88. package/dist/tooldb.js +309 -285
  89. package/dist/tooldb.js.map +1 -1
  90. package/dist/types/message.d.ts +110 -100
  91. package/dist/types/message.js +16 -16
  92. package/dist/types/message.js.map +1 -1
  93. package/dist/types/tooldb.d.ts +110 -84
  94. package/dist/types/tooldb.js +2 -2
  95. package/dist/utils/catchReturn.d.ts +1 -1
  96. package/dist/utils/catchReturn.js +7 -7
  97. package/dist/utils/encoding/arrayBufferToHex.d.ts +1 -1
  98. package/dist/utils/encoding/arrayBufferToHex.js +18 -18
  99. package/dist/utils/encoding/arrayBufferToString.d.ts +1 -1
  100. package/dist/utils/encoding/arrayBufferToString.js +11 -11
  101. package/dist/utils/encoding/hexToArrayBuffer.d.ts +1 -1
  102. package/dist/utils/encoding/hexToArrayBuffer.js +14 -14
  103. package/dist/utils/encoding/hexToString.d.ts +1 -1
  104. package/dist/utils/encoding/hexToString.js +11 -11
  105. package/dist/utils/encoding/hexToUint8.d.ts +1 -1
  106. package/dist/utils/encoding/hexToUint8.js +7 -7
  107. package/dist/utils/encoding/stringToArrayBuffer.d.ts +1 -1
  108. package/dist/utils/encoding/stringToArrayBuffer.js +11 -11
  109. package/dist/utils/getPeerSignature.d.ts +2 -2
  110. package/dist/utils/getPeerSignature.js +8 -8
  111. package/dist/utils/getTimestamp.d.ts +1 -1
  112. package/dist/utils/getTimestamp.js +6 -6
  113. package/dist/utils/proofOfWork.d.ts +4 -4
  114. package/dist/utils/proofOfWork.js +15 -15
  115. package/dist/utils/randomAnimal.d.ts +1 -1
  116. package/dist/utils/randomAnimal.js +76 -76
  117. package/dist/utils/sha1.d.ts +1 -1
  118. package/dist/utils/sha1.js +12 -12
  119. package/dist/utils/sha256.d.ts +3 -3
  120. package/dist/utils/sha256.js +12 -12
  121. package/dist/utils/textRandom.d.ts +1 -1
  122. package/dist/utils/textRandom.js +14 -14
  123. package/dist/utils/uniq.d.ts +1 -1
  124. package/dist/utils/uniq.js +6 -6
  125. package/dist/utils/verifyMessage.d.ts +8 -8
  126. package/dist/utils/verifyMessage.js +128 -128
  127. package/dist/utils/verifyPeer.d.ts +2 -2
  128. package/dist/utils/verifyPeer.js +14 -14
  129. package/lib/adapters-base/networkAdapter.ts +217 -217
  130. package/lib/adapters-base/storageAdapter.ts +35 -35
  131. package/lib/adapters-base/userAdapter.ts +49 -49
  132. package/lib/crdt/baseCrdt.ts +21 -21
  133. package/lib/crdt/counterCrdt.ts +111 -111
  134. package/lib/crdt/listCrdt.ts +190 -190
  135. package/lib/crdt/mapCrdt.ts +119 -119
  136. package/lib/index.ts +43 -42
  137. package/lib/logger.ts +30 -30
  138. package/lib/messageHandlers/handleCrdtGet.ts +34 -29
  139. package/lib/messageHandlers/handleCrdtPut.ts +123 -118
  140. package/lib/messageHandlers/handleFunction.ts +59 -0
  141. package/lib/messageHandlers/handleGet.ts +34 -29
  142. package/lib/messageHandlers/handlePing.ts +40 -40
  143. package/lib/messageHandlers/handlePong.ts +30 -30
  144. package/lib/messageHandlers/handlePut.ts +59 -54
  145. package/lib/messageHandlers/handleQuery.ts +30 -25
  146. package/lib/messageHandlers/handleSubscribe.ts +46 -46
  147. package/lib/server.ts +7 -7
  148. package/lib/shared.ts +5 -5
  149. package/lib/toolDbAnonSignIn.ts +5 -5
  150. package/lib/toolDbClientOnMessage.ts +79 -75
  151. package/lib/toolDbCrdtGet.ts +83 -82
  152. package/lib/toolDbCrdtPut.ts +78 -77
  153. package/lib/toolDbFunction.ts +49 -0
  154. package/lib/toolDbGet.ts +81 -80
  155. package/lib/toolDbKeysSignIn.ts +16 -16
  156. package/lib/toolDbPut.ts +84 -83
  157. package/lib/toolDbQueryKeys.ts +65 -64
  158. package/lib/toolDbSignIn.ts +32 -31
  159. package/lib/toolDbSignUp.ts +72 -71
  160. package/lib/toolDbSubscribe.ts +54 -53
  161. package/lib/toolDbVerificationWrapper.ts +55 -55
  162. package/lib/tooldb.ts +343 -316
  163. package/lib/types/message.ts +154 -133
  164. package/lib/types/tooldb.ts +139 -97
  165. package/lib/utils/catchReturn.ts +4 -4
  166. package/lib/utils/encoding/arrayBufferToHex.ts +18 -18
  167. package/lib/utils/encoding/arrayBufferToString.ts +8 -8
  168. package/lib/utils/encoding/hexToArrayBuffer.ts +13 -13
  169. package/lib/utils/encoding/hexToString.ts +8 -8
  170. package/lib/utils/encoding/hexToUint8.ts +5 -5
  171. package/lib/utils/encoding/stringToArrayBuffer.ts +8 -8
  172. package/lib/utils/getPeerSignature.ts +12 -12
  173. package/lib/utils/getTimestamp.ts +3 -3
  174. package/lib/utils/proofOfWork.ts +16 -16
  175. package/lib/utils/randomAnimal.ts +77 -77
  176. package/lib/utils/sha1.ts +7 -7
  177. package/lib/utils/sha256.ts +7 -7
  178. package/lib/utils/textRandom.ts +11 -11
  179. package/lib/utils/uniq.ts +3 -3
  180. package/lib/utils/verifyMessage.ts +88 -88
  181. package/lib/utils/verifyPeer.ts +15 -15
  182. package/package.json +2 -2
  183. package/tsconfig.json +14 -14
@@ -0,0 +1,49 @@
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
+ }
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
+ }