tool-db 2.5.0 → 2.5.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 (208) hide show
  1. package/README.md +82 -1
  2. package/bundle.js +1 -0
  3. package/dist/adapters-base/networkAdapter.d.ts +58 -0
  4. package/dist/adapters-base/networkAdapter.js +216 -0
  5. package/dist/adapters-base/networkAdapter.js.map +1 -0
  6. package/dist/adapters-base/storageAdapter.d.ts +11 -0
  7. package/dist/adapters-base/storageAdapter.js +36 -0
  8. package/dist/adapters-base/storageAdapter.js.map +1 -0
  9. package/dist/adapters-base/userAdapter.d.ts +15 -0
  10. package/dist/adapters-base/userAdapter.js +42 -0
  11. package/dist/adapters-base/userAdapter.js.map +1 -0
  12. package/dist/crdt/baseCrdt.d.ts +9 -0
  13. package/dist/crdt/baseCrdt.js +27 -0
  14. package/dist/crdt/baseCrdt.js.map +1 -0
  15. package/dist/crdt/counterCrdt.d.ts +30 -0
  16. package/dist/crdt/counterCrdt.js +106 -0
  17. package/dist/crdt/counterCrdt.js.map +1 -0
  18. package/dist/crdt/listCrdt.d.ts +42 -0
  19. package/dist/crdt/listCrdt.js +159 -0
  20. package/dist/crdt/listCrdt.js.map +1 -0
  21. package/dist/crdt/mapCrdt.d.ts +32 -0
  22. package/dist/crdt/mapCrdt.js +118 -0
  23. package/dist/crdt/mapCrdt.js.map +1 -0
  24. package/dist/index.d.ts +34 -0
  25. package/dist/index.js +83 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/logger.d.ts +2 -0
  28. package/dist/logger.js +29 -0
  29. package/dist/logger.js.map +1 -0
  30. package/dist/messageHandlers/handleCrdtGet.d.ts +2 -0
  31. package/dist/messageHandlers/handleCrdtGet.js +29 -0
  32. package/dist/messageHandlers/handleCrdtGet.js.map +1 -0
  33. package/dist/messageHandlers/handleCrdtPut.d.ts +2 -0
  34. package/dist/messageHandlers/handleCrdtPut.js +93 -0
  35. package/dist/messageHandlers/handleCrdtPut.js.map +1 -0
  36. package/dist/messageHandlers/handleGet.d.ts +2 -0
  37. package/dist/messageHandlers/handleGet.js +29 -0
  38. package/dist/messageHandlers/handleGet.js.map +1 -0
  39. package/dist/messageHandlers/handlePing.d.ts +2 -0
  40. package/dist/messageHandlers/handlePing.js +36 -0
  41. package/dist/messageHandlers/handlePing.js.map +1 -0
  42. package/dist/messageHandlers/handlePong.d.ts +2 -0
  43. package/dist/messageHandlers/handlePong.js +26 -0
  44. package/dist/messageHandlers/handlePong.js.map +1 -0
  45. package/dist/messageHandlers/handlePut.d.ts +2 -0
  46. package/dist/messageHandlers/handlePut.js +57 -0
  47. package/dist/messageHandlers/handlePut.js.map +1 -0
  48. package/dist/messageHandlers/handleQuery.d.ts +2 -0
  49. package/dist/messageHandlers/handleQuery.js +23 -0
  50. package/dist/messageHandlers/handleQuery.js.map +1 -0
  51. package/dist/messageHandlers/handleSubscribe.d.ts +2 -0
  52. package/dist/messageHandlers/handleSubscribe.js +44 -0
  53. package/dist/messageHandlers/handleSubscribe.js.map +1 -0
  54. package/dist/server.d.ts +1 -0
  55. package/dist/server.js +9 -0
  56. package/dist/server.js.map +1 -0
  57. package/dist/shared.d.ts +2 -0
  58. package/dist/shared.js +7 -0
  59. package/dist/shared.js.map +1 -0
  60. package/dist/toolDbAnonSignIn.d.ts +2 -0
  61. package/dist/toolDbAnonSignIn.js +7 -0
  62. package/dist/toolDbAnonSignIn.js.map +1 -0
  63. package/dist/toolDbClientOnMessage.d.ts +2 -0
  64. package/dist/toolDbClientOnMessage.js +59 -0
  65. package/dist/toolDbClientOnMessage.js.map +1 -0
  66. package/dist/toolDbCrdtGet.d.ts +9 -0
  67. package/dist/toolDbCrdtGet.js +77 -0
  68. package/dist/toolDbCrdtGet.js.map +1 -0
  69. package/dist/toolDbCrdtPut.d.ts +9 -0
  70. package/dist/toolDbCrdtPut.js +63 -0
  71. package/dist/toolDbCrdtPut.js.map +1 -0
  72. package/dist/toolDbGet.d.ts +9 -0
  73. package/dist/toolDbGet.js +77 -0
  74. package/dist/toolDbGet.js.map +1 -0
  75. package/dist/toolDbKeysSignIn.d.ts +2 -0
  76. package/dist/toolDbKeysSignIn.js +16 -0
  77. package/dist/toolDbKeysSignIn.js.map +1 -0
  78. package/dist/toolDbPut.d.ts +9 -0
  79. package/dist/toolDbPut.js +69 -0
  80. package/dist/toolDbPut.js.map +1 -0
  81. package/dist/toolDbQueryKeys.d.ts +8 -0
  82. package/dist/toolDbQueryKeys.js +66 -0
  83. package/dist/toolDbQueryKeys.js.map +1 -0
  84. package/dist/toolDbSignIn.d.ts +2 -0
  85. package/dist/toolDbSignIn.js +27 -0
  86. package/dist/toolDbSignIn.js.map +1 -0
  87. package/dist/toolDbSignUp.d.ts +2 -0
  88. package/dist/toolDbSignUp.js +101 -0
  89. package/dist/toolDbSignUp.js.map +1 -0
  90. package/dist/toolDbSubscribe.d.ts +9 -0
  91. package/dist/toolDbSubscribe.js +50 -0
  92. package/dist/toolDbSubscribe.js.map +1 -0
  93. package/dist/toolDbVerificationWrapper.d.ts +2 -0
  94. package/dist/toolDbVerificationWrapper.js +98 -0
  95. package/dist/toolDbVerificationWrapper.js.map +1 -0
  96. package/dist/tooldb.d.ts +108 -0
  97. package/dist/tooldb.js +286 -0
  98. package/dist/tooldb.js.map +1 -0
  99. package/dist/types/message.d.ts +100 -0
  100. package/dist/types/message.js +17 -0
  101. package/dist/types/message.js.map +1 -0
  102. package/dist/types/tooldb.d.ts +84 -0
  103. package/dist/types/tooldb.js +3 -0
  104. package/dist/types/tooldb.js.map +1 -0
  105. package/dist/utils/catchReturn.d.ts +1 -0
  106. package/dist/utils/catchReturn.js +8 -0
  107. package/dist/utils/catchReturn.js.map +1 -0
  108. package/dist/utils/encoding/arrayBufferToHex.d.ts +1 -0
  109. package/dist/utils/encoding/arrayBufferToHex.js +19 -0
  110. package/dist/utils/encoding/arrayBufferToHex.js.map +1 -0
  111. package/dist/utils/encoding/arrayBufferToString.d.ts +1 -0
  112. package/dist/utils/encoding/arrayBufferToString.js +12 -0
  113. package/dist/utils/encoding/arrayBufferToString.js.map +1 -0
  114. package/dist/utils/encoding/hexToArrayBuffer.d.ts +1 -0
  115. package/dist/utils/encoding/hexToArrayBuffer.js +15 -0
  116. package/dist/utils/encoding/hexToArrayBuffer.js.map +1 -0
  117. package/dist/utils/encoding/hexToString.d.ts +1 -0
  118. package/dist/utils/encoding/hexToString.js +12 -0
  119. package/dist/utils/encoding/hexToString.js.map +1 -0
  120. package/dist/utils/encoding/hexToUint8.d.ts +1 -0
  121. package/dist/utils/encoding/hexToUint8.js +8 -0
  122. package/dist/utils/encoding/hexToUint8.js.map +1 -0
  123. package/dist/utils/encoding/stringToArrayBuffer.d.ts +1 -0
  124. package/dist/utils/encoding/stringToArrayBuffer.js +12 -0
  125. package/dist/utils/encoding/stringToArrayBuffer.js.map +1 -0
  126. package/dist/utils/getPeerSignature.d.ts +2 -0
  127. package/dist/utils/getPeerSignature.js +9 -0
  128. package/dist/utils/getPeerSignature.js.map +1 -0
  129. package/dist/utils/getTimestamp.d.ts +1 -0
  130. package/dist/utils/getTimestamp.js +7 -0
  131. package/dist/utils/getTimestamp.js.map +1 -0
  132. package/dist/utils/proofOfWork.d.ts +4 -0
  133. package/dist/utils/proofOfWork.js +16 -0
  134. package/dist/utils/proofOfWork.js.map +1 -0
  135. package/dist/utils/randomAnimal.d.ts +1 -0
  136. package/dist/utils/randomAnimal.js +77 -0
  137. package/dist/utils/randomAnimal.js.map +1 -0
  138. package/dist/utils/sha1.d.ts +1 -0
  139. package/dist/utils/sha1.js +13 -0
  140. package/dist/utils/sha1.js.map +1 -0
  141. package/dist/utils/sha256.d.ts +3 -0
  142. package/dist/utils/sha256.js +13 -0
  143. package/dist/utils/sha256.js.map +1 -0
  144. package/dist/utils/textRandom.d.ts +1 -0
  145. package/dist/utils/textRandom.js +15 -0
  146. package/dist/utils/textRandom.js.map +1 -0
  147. package/dist/utils/uniq.d.ts +1 -0
  148. package/dist/utils/uniq.js +7 -0
  149. package/dist/utils/uniq.js.map +1 -0
  150. package/dist/utils/verifyMessage.d.ts +8 -0
  151. package/dist/utils/verifyMessage.js +129 -0
  152. package/dist/utils/verifyMessage.js.map +1 -0
  153. package/dist/utils/verifyPeer.d.ts +2 -0
  154. package/dist/utils/verifyPeer.js +15 -0
  155. package/dist/utils/verifyPeer.js.map +1 -0
  156. package/lib/adapters-base/networkAdapter.ts +217 -215
  157. package/lib/adapters-base/storageAdapter.ts +35 -35
  158. package/lib/adapters-base/userAdapter.ts +49 -49
  159. package/lib/crdt/baseCrdt.ts +21 -21
  160. package/lib/crdt/counterCrdt.ts +111 -111
  161. package/lib/crdt/listCrdt.ts +190 -190
  162. package/lib/crdt/mapCrdt.ts +119 -119
  163. package/lib/index.ts +42 -42
  164. package/lib/logger.ts +30 -30
  165. package/lib/messageHandlers/handleCrdtGet.ts +29 -29
  166. package/lib/messageHandlers/handleCrdtPut.ts +118 -118
  167. package/lib/messageHandlers/handleGet.ts +29 -29
  168. package/lib/messageHandlers/handlePing.ts +40 -40
  169. package/lib/messageHandlers/handlePong.ts +30 -30
  170. package/lib/messageHandlers/handlePut.ts +54 -54
  171. package/lib/messageHandlers/handleQuery.ts +25 -25
  172. package/lib/messageHandlers/handleSubscribe.ts +46 -46
  173. package/lib/server.ts +7 -7
  174. package/lib/shared.ts +5 -5
  175. package/lib/toolDbAnonSignIn.ts +5 -5
  176. package/lib/toolDbClientOnMessage.ts +75 -75
  177. package/lib/toolDbCrdtGet.ts +82 -82
  178. package/lib/toolDbCrdtPut.ts +77 -77
  179. package/lib/toolDbGet.ts +80 -80
  180. package/lib/toolDbKeysSignIn.ts +16 -16
  181. package/lib/toolDbPut.ts +83 -83
  182. package/lib/toolDbQueryKeys.ts +64 -64
  183. package/lib/toolDbSignIn.ts +31 -31
  184. package/lib/toolDbSignUp.ts +71 -71
  185. package/lib/toolDbSubscribe.ts +53 -53
  186. package/lib/toolDbVerificationWrapper.ts +55 -55
  187. package/lib/tooldb.ts +316 -314
  188. package/lib/types/message.ts +133 -133
  189. package/lib/types/tooldb.ts +97 -89
  190. package/lib/utils/catchReturn.ts +4 -4
  191. package/lib/utils/encoding/arrayBufferToHex.ts +18 -18
  192. package/lib/utils/encoding/arrayBufferToString.ts +8 -8
  193. package/lib/utils/encoding/hexToArrayBuffer.ts +13 -13
  194. package/lib/utils/encoding/hexToString.ts +8 -8
  195. package/lib/utils/encoding/hexToUint8.ts +5 -5
  196. package/lib/utils/encoding/stringToArrayBuffer.ts +8 -8
  197. package/lib/utils/getPeerSignature.ts +12 -12
  198. package/lib/utils/getTimestamp.ts +3 -3
  199. package/lib/utils/proofOfWork.ts +16 -16
  200. package/lib/utils/randomAnimal.ts +77 -77
  201. package/lib/utils/sha1.ts +7 -7
  202. package/lib/utils/sha256.ts +7 -7
  203. package/lib/utils/textRandom.ts +11 -11
  204. package/lib/utils/uniq.ts +3 -3
  205. package/lib/utils/verifyMessage.ts +88 -88
  206. package/lib/utils/verifyPeer.ts +15 -15
  207. package/package.json +4 -3
  208. package/tsconfig.json +14 -14
@@ -1,71 +1,71 @@
1
- import {
2
- ToolDb,
3
- PutMessage,
4
- textRandom,
5
- VerificationData,
6
- proofOfWork,
7
- sha256,
8
- } from ".";
9
-
10
- export default async function toolDbSignUp(
11
- this: ToolDb,
12
- user: string,
13
- password: string
14
- ): Promise<PutMessage<any>> {
15
- const userRoot = `==${user}`;
16
- return new Promise((resolve, reject) => {
17
- this.getData(userRoot, false, 3000)
18
- .then((data) => {
19
- if (data === null) {
20
- const account = new this.options.userAdapter(this);
21
- account.encryptAccount(sha256(password)).then((userData) => {
22
- const timestamp = new Date().getTime();
23
- const userDataString = `${JSON.stringify(
24
- userData
25
- )}${account.getAddress()}${timestamp}`;
26
-
27
- proofOfWork(userDataString, 0)
28
- .then(({ hash, nonce }) => {
29
- account.signData(hash).then((signature) => {
30
- const signupMessage: VerificationData = {
31
- k: userRoot,
32
- a: account.getAddress() || "",
33
- n: nonce,
34
- t: timestamp,
35
- h: hash,
36
- s: signature,
37
- v: userData,
38
- c: null,
39
- };
40
-
41
- this.logger("SIGNUP PUT", userRoot, signupMessage);
42
-
43
- const finalMsg = {
44
- type: "put",
45
- id: textRandom(10),
46
- to: [],
47
- data: signupMessage,
48
- } as PutMessage;
49
-
50
- this.network.sendToAll(finalMsg);
51
- this.store
52
- .put(userRoot, JSON.stringify(signupMessage))
53
- .catch((e) => {
54
- // do nothing
55
- })
56
- .finally(() => {
57
- resolve(finalMsg);
58
- });
59
- });
60
- })
61
- .catch(reject);
62
- });
63
- } else {
64
- reject(new Error("User already exists!"));
65
- }
66
- })
67
- .catch(() => {
68
- reject(new Error("Could not fetch user"));
69
- });
70
- });
71
- }
1
+ import {
2
+ ToolDb,
3
+ PutMessage,
4
+ textRandom,
5
+ VerificationData,
6
+ proofOfWork,
7
+ sha256,
8
+ } from ".";
9
+
10
+ export default async function toolDbSignUp(
11
+ this: ToolDb,
12
+ user: string,
13
+ password: string
14
+ ): Promise<PutMessage<any>> {
15
+ const userRoot = `==${user}`;
16
+ return new Promise((resolve, reject) => {
17
+ this.getData(userRoot, false, 3000)
18
+ .then((data) => {
19
+ if (data === null) {
20
+ const account = new this.options.userAdapter(this);
21
+ account.encryptAccount(sha256(password)).then((userData) => {
22
+ const timestamp = new Date().getTime();
23
+ const userDataString = `${JSON.stringify(
24
+ userData
25
+ )}${account.getAddress()}${timestamp}`;
26
+
27
+ proofOfWork(userDataString, 0)
28
+ .then(({ hash, nonce }) => {
29
+ account.signData(hash).then((signature) => {
30
+ const signupMessage: VerificationData = {
31
+ k: userRoot,
32
+ a: account.getAddress() || "",
33
+ n: nonce,
34
+ t: timestamp,
35
+ h: hash,
36
+ s: signature,
37
+ v: userData,
38
+ c: null,
39
+ };
40
+
41
+ this.logger("SIGNUP PUT", userRoot, signupMessage);
42
+
43
+ const finalMsg = {
44
+ type: "put",
45
+ id: textRandom(10),
46
+ to: [],
47
+ data: signupMessage,
48
+ } as PutMessage;
49
+
50
+ this.network.sendToAll(finalMsg);
51
+ this.store
52
+ .put(userRoot, JSON.stringify(signupMessage))
53
+ .catch((e) => {
54
+ // do nothing
55
+ })
56
+ .finally(() => {
57
+ resolve(finalMsg);
58
+ });
59
+ });
60
+ })
61
+ .catch(reject);
62
+ });
63
+ } else {
64
+ reject(new Error("User already exists!"));
65
+ }
66
+ })
67
+ .catch(() => {
68
+ reject(new Error("Could not fetch user"));
69
+ });
70
+ });
71
+ }
@@ -1,53 +1,53 @@
1
- import { ToolDb, textRandom } from ".";
2
-
3
- /**
4
- * Subscribe to all PUT updates for this key.
5
- * @param key key of the data
6
- * @param userNamespaced If this key bolongs to a user or its public. Making it
7
- * private will enforce validation for our address and signatures.
8
- * @returns Promise<Data>
9
- */
10
- export default function toolDbSubscribe(
11
- this: ToolDb,
12
- key: string,
13
- userNamespaced = false
14
- ): Promise<void> {
15
- return new Promise((resolve, reject) => {
16
- if (userNamespaced && this.userAccount.getAddress() === undefined) {
17
- reject(new Error("You are not authorized yet!"));
18
- return;
19
- }
20
-
21
- const finalKey = userNamespaced
22
- ? `:${this.userAccount.getAddress()}.${key}`
23
- : key;
24
-
25
- this.logger("SUBSCRIBE", finalKey);
26
-
27
- const msgId = textRandom(10);
28
-
29
- this.network.sendToAll({
30
- type: "subscribe",
31
- key: finalKey,
32
- to: [],
33
- id: msgId,
34
- });
35
-
36
- this.store
37
- .get(finalKey)
38
- .then((data) => {
39
- try {
40
- const message = JSON.parse(data);
41
- this.triggerKeyListener(finalKey, message);
42
- } catch (e) {
43
- // do nothing
44
- }
45
- })
46
- .catch((e) => {
47
- // do nothing
48
- })
49
- .finally(() => {
50
- resolve();
51
- });
52
- });
53
- }
1
+ import { ToolDb, textRandom } from ".";
2
+
3
+ /**
4
+ * Subscribe to all PUT updates for this key.
5
+ * @param key key of the data
6
+ * @param userNamespaced If this key bolongs to a user or its public. Making it
7
+ * private will enforce validation for our address and signatures.
8
+ * @returns Promise<Data>
9
+ */
10
+ export default function toolDbSubscribe(
11
+ this: ToolDb,
12
+ key: string,
13
+ userNamespaced = false
14
+ ): Promise<void> {
15
+ return new Promise((resolve, reject) => {
16
+ if (userNamespaced && this.userAccount.getAddress() === undefined) {
17
+ reject(new Error("You are not authorized yet!"));
18
+ return;
19
+ }
20
+
21
+ const finalKey = userNamespaced
22
+ ? `:${this.userAccount.getAddress()}.${key}`
23
+ : key;
24
+
25
+ this.logger("SUBSCRIBE", finalKey);
26
+
27
+ const msgId = textRandom(10);
28
+
29
+ this.network.sendToAll({
30
+ type: "subscribe",
31
+ key: finalKey,
32
+ to: [],
33
+ id: msgId,
34
+ });
35
+
36
+ this.store
37
+ .get(finalKey)
38
+ .then((data) => {
39
+ try {
40
+ const message = JSON.parse(data);
41
+ this.triggerKeyListener(finalKey, message);
42
+ } catch (e) {
43
+ // do nothing
44
+ }
45
+ })
46
+ .catch((e) => {
47
+ // do nothing
48
+ })
49
+ .finally(() => {
50
+ resolve();
51
+ });
52
+ });
53
+ }
@@ -1,55 +1,55 @@
1
- import { ToolDb, VerificationData, VerifyResult } from ".";
2
-
3
- export default function toolDbVerificationWrapper(
4
- this: ToolDb,
5
- data: VerificationData<any>
6
- ) {
7
- // This wrapper function adds our custom verification functions to all messages.
8
- return new Promise((resolve) => {
9
- this.verifyMessage(data, this.options.pow).then(async (verified) => {
10
- if (verified) {
11
- let skipCustom = true;
12
- this._customVerificator.forEach((listener) => {
13
- if (listener && data.k && data.k.startsWith(listener.key)) {
14
- skipCustom = false;
15
-
16
- let previousData: any = undefined;
17
- // Get the previously stored value of this key
18
- this.store
19
- .get(data.k)
20
- .then((prev) => {
21
- try {
22
- previousData = JSON.parse(prev);
23
- } catch (e) {
24
- // do nothing
25
- }
26
- })
27
- .catch((e) => {
28
- // do nothing
29
- })
30
- .finally(() => {
31
- listener
32
- .fn(data, previousData)
33
- .then((verified: boolean) => {
34
- if (verified) {
35
- resolve(VerifyResult.Verified);
36
- } else {
37
- resolve(VerifyResult.CustomVerificationFailed);
38
- }
39
- })
40
- .catch((e) => {
41
- resolve(VerifyResult.CustomVerificationFailed);
42
- });
43
- });
44
- }
45
- });
46
-
47
- if (skipCustom) {
48
- resolve(verified);
49
- }
50
- } else {
51
- resolve(VerifyResult.InvalidVerification);
52
- }
53
- });
54
- });
55
- }
1
+ import { ToolDb, VerificationData, VerifyResult } from ".";
2
+
3
+ export default function toolDbVerificationWrapper(
4
+ this: ToolDb,
5
+ data: VerificationData<any>
6
+ ) {
7
+ // This wrapper function adds our custom verification functions to all messages.
8
+ return new Promise((resolve) => {
9
+ this.verifyMessage(data, this.options.pow).then(async (verified) => {
10
+ if (verified) {
11
+ let skipCustom = true;
12
+ this._customVerificator.forEach((listener) => {
13
+ if (listener && data.k && data.k.startsWith(listener.key)) {
14
+ skipCustom = false;
15
+
16
+ let previousData: any = undefined;
17
+ // Get the previously stored value of this key
18
+ this.store
19
+ .get(data.k)
20
+ .then((prev) => {
21
+ try {
22
+ previousData = JSON.parse(prev);
23
+ } catch (e) {
24
+ // do nothing
25
+ }
26
+ })
27
+ .catch((e) => {
28
+ // do nothing
29
+ })
30
+ .finally(() => {
31
+ listener
32
+ .fn(data, previousData)
33
+ .then((verified: boolean) => {
34
+ if (verified) {
35
+ resolve(VerifyResult.Verified);
36
+ } else {
37
+ resolve(VerifyResult.CustomVerificationFailed);
38
+ }
39
+ })
40
+ .catch((e) => {
41
+ resolve(VerifyResult.CustomVerificationFailed);
42
+ });
43
+ });
44
+ }
45
+ });
46
+
47
+ if (skipCustom) {
48
+ resolve(verified);
49
+ }
50
+ } else {
51
+ resolve(VerifyResult.InvalidVerification);
52
+ }
53
+ });
54
+ });
55
+ }