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.
- package/README.md +82 -82
- package/bundle.js +1 -1
- package/dist/adapters-base/networkAdapter.d.ts +58 -58
- package/dist/adapters-base/networkAdapter.js +215 -215
- package/dist/adapters-base/storageAdapter.d.ts +11 -11
- package/dist/adapters-base/storageAdapter.js +35 -35
- package/dist/adapters-base/userAdapter.d.ts +15 -15
- package/dist/adapters-base/userAdapter.js +41 -41
- package/dist/crdt/baseCrdt.d.ts +9 -9
- package/dist/crdt/baseCrdt.js +26 -26
- package/dist/crdt/counterCrdt.d.ts +30 -30
- package/dist/crdt/counterCrdt.js +105 -105
- package/dist/crdt/listCrdt.d.ts +42 -42
- package/dist/crdt/listCrdt.js +158 -158
- package/dist/crdt/mapCrdt.d.ts +32 -32
- package/dist/crdt/mapCrdt.js +117 -117
- package/dist/index.d.ts +34 -34
- package/dist/index.js +82 -82
- package/dist/logger.d.ts +2 -2
- package/dist/logger.js +28 -28
- package/dist/messageHandlers/handleCrdtGet.d.ts +2 -2
- package/dist/messageHandlers/handleCrdtGet.js +49 -28
- package/dist/messageHandlers/handleCrdtGet.js.map +1 -1
- package/dist/messageHandlers/handleCrdtPut.d.ts +2 -2
- package/dist/messageHandlers/handleCrdtPut.js +102 -92
- package/dist/messageHandlers/handleCrdtPut.js.map +1 -1
- package/dist/messageHandlers/handleGet.d.ts +2 -2
- package/dist/messageHandlers/handleGet.js +49 -28
- package/dist/messageHandlers/handleGet.js.map +1 -1
- package/dist/messageHandlers/handlePing.d.ts +2 -2
- package/dist/messageHandlers/handlePing.js +35 -35
- package/dist/messageHandlers/handlePing.js.map +1 -1
- package/dist/messageHandlers/handlePong.d.ts +2 -2
- package/dist/messageHandlers/handlePong.js +25 -25
- package/dist/messageHandlers/handlePut.d.ts +2 -2
- package/dist/messageHandlers/handlePut.js +77 -56
- package/dist/messageHandlers/handlePut.js.map +1 -1
- package/dist/messageHandlers/handleQuery.d.ts +2 -2
- package/dist/messageHandlers/handleQuery.js +43 -22
- package/dist/messageHandlers/handleQuery.js.map +1 -1
- package/dist/messageHandlers/handleSubscribe.d.ts +2 -2
- package/dist/messageHandlers/handleSubscribe.js +43 -43
- package/dist/server.d.ts +1 -1
- package/dist/server.js +8 -8
- package/dist/shared.d.ts +2 -2
- package/dist/shared.js +6 -6
- package/dist/toolDbAnonSignIn.d.ts +2 -2
- package/dist/toolDbAnonSignIn.js +6 -6
- package/dist/toolDbClientOnMessage.d.ts +2 -2
- package/dist/toolDbClientOnMessage.js +58 -58
- package/dist/toolDbCrdtGet.d.ts +9 -9
- package/dist/toolDbCrdtGet.js +76 -76
- package/dist/toolDbCrdtGet.js.map +1 -1
- package/dist/toolDbCrdtPut.d.ts +9 -9
- package/dist/toolDbCrdtPut.js +62 -62
- package/dist/toolDbCrdtPut.js.map +1 -1
- package/dist/toolDbGet.d.ts +9 -9
- package/dist/toolDbGet.js +76 -76
- package/dist/toolDbGet.js.map +1 -1
- package/dist/toolDbKeysSignIn.d.ts +2 -2
- package/dist/toolDbKeysSignIn.js +15 -15
- package/dist/toolDbPut.d.ts +9 -9
- package/dist/toolDbPut.js +68 -68
- package/dist/toolDbPut.js.map +1 -1
- package/dist/toolDbQueryKeys.d.ts +8 -8
- package/dist/toolDbQueryKeys.js +65 -65
- package/dist/toolDbQueryKeys.js.map +1 -1
- package/dist/toolDbSignIn.d.ts +2 -2
- package/dist/toolDbSignIn.js +26 -26
- package/dist/toolDbSignIn.js.map +1 -1
- package/dist/toolDbSignUp.d.ts +2 -2
- package/dist/toolDbSignUp.js +100 -100
- package/dist/toolDbSignUp.js.map +1 -1
- package/dist/toolDbSubscribe.d.ts +9 -9
- package/dist/toolDbSubscribe.js +49 -49
- package/dist/toolDbSubscribe.js.map +1 -1
- package/dist/toolDbVerificationWrapper.d.ts +2 -2
- package/dist/toolDbVerificationWrapper.js +97 -97
- package/dist/tooldb.d.ts +108 -108
- package/dist/tooldb.js +285 -285
- package/dist/types/message.d.ts +100 -100
- package/dist/types/message.js +16 -16
- package/dist/types/tooldb.d.ts +84 -84
- package/dist/types/tooldb.js +2 -2
- package/dist/utils/catchReturn.d.ts +1 -1
- package/dist/utils/catchReturn.js +7 -7
- package/dist/utils/encoding/arrayBufferToHex.d.ts +1 -1
- package/dist/utils/encoding/arrayBufferToHex.js +18 -18
- package/dist/utils/encoding/arrayBufferToString.d.ts +1 -1
- package/dist/utils/encoding/arrayBufferToString.js +11 -11
- package/dist/utils/encoding/hexToArrayBuffer.d.ts +1 -1
- package/dist/utils/encoding/hexToArrayBuffer.js +14 -14
- package/dist/utils/encoding/hexToString.d.ts +1 -1
- package/dist/utils/encoding/hexToString.js +11 -11
- package/dist/utils/encoding/hexToUint8.d.ts +1 -1
- package/dist/utils/encoding/hexToUint8.js +7 -7
- package/dist/utils/encoding/stringToArrayBuffer.d.ts +1 -1
- package/dist/utils/encoding/stringToArrayBuffer.js +11 -11
- package/dist/utils/getPeerSignature.d.ts +2 -2
- package/dist/utils/getPeerSignature.js +8 -8
- package/dist/utils/getTimestamp.d.ts +1 -1
- package/dist/utils/getTimestamp.js +6 -6
- package/dist/utils/proofOfWork.d.ts +4 -4
- package/dist/utils/proofOfWork.js +15 -15
- package/dist/utils/randomAnimal.d.ts +1 -1
- package/dist/utils/randomAnimal.js +76 -76
- package/dist/utils/sha1.d.ts +1 -1
- package/dist/utils/sha1.js +12 -12
- package/dist/utils/sha256.d.ts +3 -3
- package/dist/utils/sha256.js +12 -12
- package/dist/utils/textRandom.d.ts +1 -1
- package/dist/utils/textRandom.js +14 -14
- package/dist/utils/uniq.d.ts +1 -1
- package/dist/utils/uniq.js +6 -6
- package/dist/utils/verifyMessage.d.ts +8 -8
- package/dist/utils/verifyMessage.js +128 -128
- package/dist/utils/verifyPeer.d.ts +2 -2
- package/dist/utils/verifyPeer.js +14 -14
- package/lib/adapters-base/networkAdapter.ts +217 -217
- package/lib/adapters-base/storageAdapter.ts +35 -35
- package/lib/adapters-base/userAdapter.ts +49 -49
- package/lib/crdt/baseCrdt.ts +21 -21
- package/lib/crdt/counterCrdt.ts +111 -111
- package/lib/crdt/listCrdt.ts +190 -190
- package/lib/crdt/mapCrdt.ts +119 -119
- package/lib/index.ts +42 -42
- package/lib/logger.ts +30 -30
- package/lib/messageHandlers/handleCrdtGet.ts +34 -29
- package/lib/messageHandlers/handleCrdtPut.ts +123 -118
- package/lib/messageHandlers/handleGet.ts +34 -29
- package/lib/messageHandlers/handlePing.ts +40 -40
- package/lib/messageHandlers/handlePong.ts +30 -30
- package/lib/messageHandlers/handlePut.ts +59 -54
- package/lib/messageHandlers/handleQuery.ts +30 -25
- package/lib/messageHandlers/handleSubscribe.ts +46 -46
- package/lib/server.ts +7 -7
- package/lib/shared.ts +5 -5
- package/lib/toolDbAnonSignIn.ts +5 -5
- package/lib/toolDbClientOnMessage.ts +75 -75
- package/lib/toolDbCrdtGet.ts +83 -82
- package/lib/toolDbCrdtPut.ts +78 -77
- package/lib/toolDbGet.ts +81 -80
- package/lib/toolDbKeysSignIn.ts +16 -16
- package/lib/toolDbPut.ts +84 -83
- package/lib/toolDbQueryKeys.ts +65 -64
- package/lib/toolDbSignIn.ts +32 -31
- package/lib/toolDbSignUp.ts +72 -71
- package/lib/toolDbSubscribe.ts +54 -53
- package/lib/toolDbVerificationWrapper.ts +55 -55
- package/lib/tooldb.ts +316 -316
- package/lib/types/message.ts +133 -133
- package/lib/types/tooldb.ts +97 -97
- package/lib/utils/catchReturn.ts +4 -4
- package/lib/utils/encoding/arrayBufferToHex.ts +18 -18
- package/lib/utils/encoding/arrayBufferToString.ts +8 -8
- package/lib/utils/encoding/hexToArrayBuffer.ts +13 -13
- package/lib/utils/encoding/hexToString.ts +8 -8
- package/lib/utils/encoding/hexToUint8.ts +5 -5
- package/lib/utils/encoding/stringToArrayBuffer.ts +8 -8
- package/lib/utils/getPeerSignature.ts +12 -12
- package/lib/utils/getTimestamp.ts +3 -3
- package/lib/utils/proofOfWork.ts +16 -16
- package/lib/utils/randomAnimal.ts +77 -77
- package/lib/utils/sha1.ts +7 -7
- package/lib/utils/sha256.ts +7 -7
- package/lib/utils/textRandom.ts +11 -11
- package/lib/utils/uniq.ts +3 -3
- package/lib/utils/verifyMessage.ts +88 -88
- package/lib/utils/verifyPeer.ts +15 -15
- package/package.json +2 -2
- 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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
.
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
this.
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
+
}
|
package/lib/toolDbKeysSignIn.ts
CHANGED
|
@@ -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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
this.
|
|
71
|
-
|
|
72
|
-
.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
+
}
|
package/lib/toolDbQueryKeys.ts
CHANGED
|
@@ -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
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
let
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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
|
+
}
|
package/lib/toolDbSignIn.ts
CHANGED
|
@@ -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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
+
}
|