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/types/message.ts
CHANGED
|
@@ -1,133 +1,133 @@
|
|
|
1
|
-
import { Peer } from "..";
|
|
2
|
-
|
|
3
|
-
export enum VerifyResult {
|
|
4
|
-
CustomVerificationFailed = "CustomVerificationFailed",
|
|
5
|
-
InvalidData = "InvalidData",
|
|
6
|
-
InvalidVerification = "InvalidVerification",
|
|
7
|
-
CantOverwrite = "CantOverwrite",
|
|
8
|
-
InvalidTimestamp = "InvalidTimestamp",
|
|
9
|
-
AddressMismatch = "AddressMismatch",
|
|
10
|
-
NoProofOfWork = "NoProofOfWork",
|
|
11
|
-
InvalidHashNonce = "InvalidHashNonce",
|
|
12
|
-
InvalidSignature = "InvalidSignature",
|
|
13
|
-
Verified = "Verified",
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export interface VerificationData<T = any> {
|
|
17
|
-
/**
|
|
18
|
-
* Key/id
|
|
19
|
-
*/
|
|
20
|
-
k: string;
|
|
21
|
-
/**
|
|
22
|
-
* Address
|
|
23
|
-
*/
|
|
24
|
-
a: string;
|
|
25
|
-
/**
|
|
26
|
-
* Nonce
|
|
27
|
-
*/
|
|
28
|
-
n: number;
|
|
29
|
-
/**
|
|
30
|
-
* Hash of JSON.stringify(value) + nonce
|
|
31
|
-
*/
|
|
32
|
-
h: string;
|
|
33
|
-
/**
|
|
34
|
-
* Timestamp this was created
|
|
35
|
-
*/
|
|
36
|
-
t: number;
|
|
37
|
-
/**
|
|
38
|
-
* Signature
|
|
39
|
-
*/
|
|
40
|
-
s: string;
|
|
41
|
-
/**
|
|
42
|
-
* Value
|
|
43
|
-
*/
|
|
44
|
-
v: T;
|
|
45
|
-
/**
|
|
46
|
-
* CRDT type (null for regular values)
|
|
47
|
-
*/
|
|
48
|
-
c: string | null;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export type MessageType =
|
|
52
|
-
| "ping"
|
|
53
|
-
| "pong"
|
|
54
|
-
| "query"
|
|
55
|
-
| "queryAck"
|
|
56
|
-
| "subscribe"
|
|
57
|
-
| "get"
|
|
58
|
-
| "put"
|
|
59
|
-
| "crdtPut"
|
|
60
|
-
| "crdtGet"
|
|
61
|
-
| "crdt";
|
|
62
|
-
|
|
63
|
-
export interface BaseMessage {
|
|
64
|
-
type: MessageType;
|
|
65
|
-
/**
|
|
66
|
-
* Unique random id for the message, to ack back
|
|
67
|
-
*/
|
|
68
|
-
id: string;
|
|
69
|
-
/**
|
|
70
|
-
* Who was this message sent to already
|
|
71
|
-
*/
|
|
72
|
-
to: string[];
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export interface PingMessage extends BaseMessage {
|
|
76
|
-
type: "ping";
|
|
77
|
-
isServer: boolean;
|
|
78
|
-
clientId: string;
|
|
79
|
-
peer: Peer;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export interface PongMessage extends BaseMessage {
|
|
83
|
-
type: "pong";
|
|
84
|
-
isServer: boolean;
|
|
85
|
-
clientId: string;
|
|
86
|
-
servers: Peer[];
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export interface QueryMessage extends BaseMessage {
|
|
90
|
-
type: "query";
|
|
91
|
-
key: string; // key we want to get
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export interface QueryAckMessage extends BaseMessage {
|
|
95
|
-
type: "queryAck";
|
|
96
|
-
keys: string[];
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
export interface SubscribeMessage extends BaseMessage {
|
|
100
|
-
type: "subscribe";
|
|
101
|
-
key: string;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export interface GetMessage extends BaseMessage {
|
|
105
|
-
type: "get";
|
|
106
|
-
key: string; // key we want to get
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export interface PutMessage<T = any> extends BaseMessage {
|
|
110
|
-
type: "put";
|
|
111
|
-
data: VerificationData<T>;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export interface CrdtPutMessage<T = any> extends BaseMessage {
|
|
115
|
-
type: "crdtPut";
|
|
116
|
-
data: VerificationData<T>;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export interface CrdtGetMessage<T = any> extends BaseMessage {
|
|
120
|
-
type: "crdtGet";
|
|
121
|
-
key: string;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export type ToolDbMessage =
|
|
125
|
-
| PingMessage
|
|
126
|
-
| PongMessage
|
|
127
|
-
| QueryMessage
|
|
128
|
-
| QueryAckMessage
|
|
129
|
-
| SubscribeMessage
|
|
130
|
-
| GetMessage
|
|
131
|
-
| PutMessage
|
|
132
|
-
| CrdtPutMessage
|
|
133
|
-
| CrdtGetMessage;
|
|
1
|
+
import { Peer } from "..";
|
|
2
|
+
|
|
3
|
+
export enum VerifyResult {
|
|
4
|
+
CustomVerificationFailed = "CustomVerificationFailed",
|
|
5
|
+
InvalidData = "InvalidData",
|
|
6
|
+
InvalidVerification = "InvalidVerification",
|
|
7
|
+
CantOverwrite = "CantOverwrite",
|
|
8
|
+
InvalidTimestamp = "InvalidTimestamp",
|
|
9
|
+
AddressMismatch = "AddressMismatch",
|
|
10
|
+
NoProofOfWork = "NoProofOfWork",
|
|
11
|
+
InvalidHashNonce = "InvalidHashNonce",
|
|
12
|
+
InvalidSignature = "InvalidSignature",
|
|
13
|
+
Verified = "Verified",
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface VerificationData<T = any> {
|
|
17
|
+
/**
|
|
18
|
+
* Key/id
|
|
19
|
+
*/
|
|
20
|
+
k: string;
|
|
21
|
+
/**
|
|
22
|
+
* Address
|
|
23
|
+
*/
|
|
24
|
+
a: string;
|
|
25
|
+
/**
|
|
26
|
+
* Nonce
|
|
27
|
+
*/
|
|
28
|
+
n: number;
|
|
29
|
+
/**
|
|
30
|
+
* Hash of JSON.stringify(value) + nonce
|
|
31
|
+
*/
|
|
32
|
+
h: string;
|
|
33
|
+
/**
|
|
34
|
+
* Timestamp this was created
|
|
35
|
+
*/
|
|
36
|
+
t: number;
|
|
37
|
+
/**
|
|
38
|
+
* Signature
|
|
39
|
+
*/
|
|
40
|
+
s: string;
|
|
41
|
+
/**
|
|
42
|
+
* Value
|
|
43
|
+
*/
|
|
44
|
+
v: T;
|
|
45
|
+
/**
|
|
46
|
+
* CRDT type (null for regular values)
|
|
47
|
+
*/
|
|
48
|
+
c: string | null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type MessageType =
|
|
52
|
+
| "ping"
|
|
53
|
+
| "pong"
|
|
54
|
+
| "query"
|
|
55
|
+
| "queryAck"
|
|
56
|
+
| "subscribe"
|
|
57
|
+
| "get"
|
|
58
|
+
| "put"
|
|
59
|
+
| "crdtPut"
|
|
60
|
+
| "crdtGet"
|
|
61
|
+
| "crdt";
|
|
62
|
+
|
|
63
|
+
export interface BaseMessage {
|
|
64
|
+
type: MessageType;
|
|
65
|
+
/**
|
|
66
|
+
* Unique random id for the message, to ack back
|
|
67
|
+
*/
|
|
68
|
+
id: string;
|
|
69
|
+
/**
|
|
70
|
+
* Who was this message sent to already
|
|
71
|
+
*/
|
|
72
|
+
to: string[];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface PingMessage extends BaseMessage {
|
|
76
|
+
type: "ping";
|
|
77
|
+
isServer: boolean;
|
|
78
|
+
clientId: string;
|
|
79
|
+
peer: Peer;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export interface PongMessage extends BaseMessage {
|
|
83
|
+
type: "pong";
|
|
84
|
+
isServer: boolean;
|
|
85
|
+
clientId: string;
|
|
86
|
+
servers: Peer[];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface QueryMessage extends BaseMessage {
|
|
90
|
+
type: "query";
|
|
91
|
+
key: string; // key we want to get
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface QueryAckMessage extends BaseMessage {
|
|
95
|
+
type: "queryAck";
|
|
96
|
+
keys: string[];
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface SubscribeMessage extends BaseMessage {
|
|
100
|
+
type: "subscribe";
|
|
101
|
+
key: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface GetMessage extends BaseMessage {
|
|
105
|
+
type: "get";
|
|
106
|
+
key: string; // key we want to get
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export interface PutMessage<T = any> extends BaseMessage {
|
|
110
|
+
type: "put";
|
|
111
|
+
data: VerificationData<T>;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export interface CrdtPutMessage<T = any> extends BaseMessage {
|
|
115
|
+
type: "crdtPut";
|
|
116
|
+
data: VerificationData<T>;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface CrdtGetMessage<T = any> extends BaseMessage {
|
|
120
|
+
type: "crdtGet";
|
|
121
|
+
key: string;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export type ToolDbMessage =
|
|
125
|
+
| PingMessage
|
|
126
|
+
| PongMessage
|
|
127
|
+
| QueryMessage
|
|
128
|
+
| QueryAckMessage
|
|
129
|
+
| SubscribeMessage
|
|
130
|
+
| GetMessage
|
|
131
|
+
| PutMessage
|
|
132
|
+
| CrdtPutMessage
|
|
133
|
+
| CrdtGetMessage;
|
package/lib/types/tooldb.ts
CHANGED
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
import { Server as HTTPServer } from "http";
|
|
2
|
-
import { Server as HTTPSServer } from "https";
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
ToolDb,
|
|
6
|
-
ToolDbMessage,
|
|
7
|
-
ToolDbNetworkAdapter,
|
|
8
|
-
ToolDbStorageAdapter,
|
|
9
|
-
ToolDbUserAdapter,
|
|
10
|
-
} from "..";
|
|
11
|
-
|
|
12
|
-
export interface Peer {
|
|
13
|
-
topic: string;
|
|
14
|
-
timestamp: number;
|
|
15
|
-
host: string;
|
|
16
|
-
port: number;
|
|
17
|
-
address: string;
|
|
18
|
-
sig: string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export type ToolDbMessageHandler = (
|
|
22
|
-
this: ToolDb,
|
|
23
|
-
message: ToolDbMessage,
|
|
24
|
-
remotePeerId: string
|
|
25
|
-
) => void;
|
|
26
|
-
|
|
27
|
-
export interface ToolDbOptions {
|
|
28
|
-
/**
|
|
29
|
-
* Show debug console logs
|
|
30
|
-
*/
|
|
31
|
-
debug: boolean;
|
|
32
|
-
/**
|
|
33
|
-
* Array of peers to connect to
|
|
34
|
-
*/
|
|
35
|
-
peers: { host: string; port: number }[];
|
|
36
|
-
/**
|
|
37
|
-
* Max number of tries when a connection fails
|
|
38
|
-
*/
|
|
39
|
-
maxRetries: number;
|
|
40
|
-
/**
|
|
41
|
-
* How long to wait (max) for a debounced key listener recv
|
|
42
|
-
*/
|
|
43
|
-
triggerDebouce: number;
|
|
44
|
-
/**
|
|
45
|
-
* How long to wait between retries
|
|
46
|
-
*/
|
|
47
|
-
wait: number;
|
|
48
|
-
/**
|
|
49
|
-
* Port to listen incoming connections (server only)
|
|
50
|
-
*/
|
|
51
|
-
pow: number;
|
|
52
|
-
/**
|
|
53
|
-
* Whether we are a server or not
|
|
54
|
-
*/
|
|
55
|
-
server: boolean;
|
|
56
|
-
/**
|
|
57
|
-
* A server instance like Express (server only)
|
|
58
|
-
*/
|
|
59
|
-
httpServer: HTTPServer | HTTPSServer | undefined;
|
|
60
|
-
/**
|
|
61
|
-
* Our hostname (server only)
|
|
62
|
-
*/
|
|
63
|
-
host: string;
|
|
64
|
-
/**
|
|
65
|
-
* Port to listen incoming connections (server only, default is 8080)
|
|
66
|
-
*/
|
|
67
|
-
port: number;
|
|
68
|
-
/**
|
|
69
|
-
* If we are listening over SSL or not
|
|
70
|
-
*/
|
|
71
|
-
ssl: boolean;
|
|
72
|
-
/**
|
|
73
|
-
* Our storage namespace (default is "tooldb")
|
|
74
|
-
*/
|
|
75
|
-
storageName: string;
|
|
76
|
-
/**
|
|
77
|
-
* A custom network adapter class
|
|
78
|
-
*/
|
|
79
|
-
networkAdapter: typeof ToolDbNetworkAdapter;
|
|
80
|
-
/**
|
|
81
|
-
* A custom storage adapter function
|
|
82
|
-
*/
|
|
83
|
-
storageAdapter: typeof ToolDbStorageAdapter;
|
|
84
|
-
/**
|
|
85
|
-
* A custom user storage and validation adapter class
|
|
86
|
-
*/
|
|
87
|
-
userAdapter: typeof ToolDbUserAdapter;
|
|
88
|
-
/**
|
|
89
|
-
* The namespace/topic of our app (default is "tool-db-default")
|
|
90
|
-
*/
|
|
91
|
-
topic: string;
|
|
92
|
-
/*
|
|
93
|
-
* The name of the server (works like a domain)
|
|
94
|
-
*/
|
|
95
|
-
serverName: string | undefined;
|
|
96
|
-
// [extra: string]: any;
|
|
97
|
-
}
|
|
1
|
+
import { Server as HTTPServer } from "http";
|
|
2
|
+
import { Server as HTTPSServer } from "https";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
ToolDb,
|
|
6
|
+
ToolDbMessage,
|
|
7
|
+
ToolDbNetworkAdapter,
|
|
8
|
+
ToolDbStorageAdapter,
|
|
9
|
+
ToolDbUserAdapter,
|
|
10
|
+
} from "..";
|
|
11
|
+
|
|
12
|
+
export interface Peer {
|
|
13
|
+
topic: string;
|
|
14
|
+
timestamp: number;
|
|
15
|
+
host: string;
|
|
16
|
+
port: number;
|
|
17
|
+
address: string;
|
|
18
|
+
sig: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type ToolDbMessageHandler = (
|
|
22
|
+
this: ToolDb,
|
|
23
|
+
message: ToolDbMessage,
|
|
24
|
+
remotePeerId: string
|
|
25
|
+
) => void;
|
|
26
|
+
|
|
27
|
+
export interface ToolDbOptions {
|
|
28
|
+
/**
|
|
29
|
+
* Show debug console logs
|
|
30
|
+
*/
|
|
31
|
+
debug: boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Array of peers to connect to
|
|
34
|
+
*/
|
|
35
|
+
peers: { host: string; port: number }[];
|
|
36
|
+
/**
|
|
37
|
+
* Max number of tries when a connection fails
|
|
38
|
+
*/
|
|
39
|
+
maxRetries: number;
|
|
40
|
+
/**
|
|
41
|
+
* How long to wait (max) for a debounced key listener recv
|
|
42
|
+
*/
|
|
43
|
+
triggerDebouce: number;
|
|
44
|
+
/**
|
|
45
|
+
* How long to wait between retries
|
|
46
|
+
*/
|
|
47
|
+
wait: number;
|
|
48
|
+
/**
|
|
49
|
+
* Port to listen incoming connections (server only)
|
|
50
|
+
*/
|
|
51
|
+
pow: number;
|
|
52
|
+
/**
|
|
53
|
+
* Whether we are a server or not
|
|
54
|
+
*/
|
|
55
|
+
server: boolean;
|
|
56
|
+
/**
|
|
57
|
+
* A server instance like Express (server only)
|
|
58
|
+
*/
|
|
59
|
+
httpServer: HTTPServer | HTTPSServer | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Our hostname (server only)
|
|
62
|
+
*/
|
|
63
|
+
host: string;
|
|
64
|
+
/**
|
|
65
|
+
* Port to listen incoming connections (server only, default is 8080)
|
|
66
|
+
*/
|
|
67
|
+
port: number;
|
|
68
|
+
/**
|
|
69
|
+
* If we are listening over SSL or not
|
|
70
|
+
*/
|
|
71
|
+
ssl: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Our storage namespace (default is "tooldb")
|
|
74
|
+
*/
|
|
75
|
+
storageName: string;
|
|
76
|
+
/**
|
|
77
|
+
* A custom network adapter class
|
|
78
|
+
*/
|
|
79
|
+
networkAdapter: typeof ToolDbNetworkAdapter;
|
|
80
|
+
/**
|
|
81
|
+
* A custom storage adapter function
|
|
82
|
+
*/
|
|
83
|
+
storageAdapter: typeof ToolDbStorageAdapter;
|
|
84
|
+
/**
|
|
85
|
+
* A custom user storage and validation adapter class
|
|
86
|
+
*/
|
|
87
|
+
userAdapter: typeof ToolDbUserAdapter;
|
|
88
|
+
/**
|
|
89
|
+
* The namespace/topic of our app (default is "tool-db-default")
|
|
90
|
+
*/
|
|
91
|
+
topic: string;
|
|
92
|
+
/*
|
|
93
|
+
* The name of the server (works like a domain)
|
|
94
|
+
*/
|
|
95
|
+
serverName: string | undefined;
|
|
96
|
+
// [extra: string]: any;
|
|
97
|
+
}
|
package/lib/utils/catchReturn.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export default function catchReturn(arg: any) {
|
|
2
|
-
// console.error(arg);
|
|
3
|
-
return undefined;
|
|
4
|
-
}
|
|
1
|
+
export default function catchReturn(arg: any) {
|
|
2
|
+
// console.error(arg);
|
|
3
|
+
return undefined;
|
|
4
|
+
}
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
export default function arrayBufferToHex(buffer: ArrayBuffer) {
|
|
2
|
-
const data_view = new DataView(buffer);
|
|
3
|
-
let iii;
|
|
4
|
-
let len;
|
|
5
|
-
let hex = "";
|
|
6
|
-
let c;
|
|
7
|
-
|
|
8
|
-
for (iii = 0, len = data_view.byteLength; iii < len; iii += 1) {
|
|
9
|
-
c = data_view.getUint8(iii).toString(16);
|
|
10
|
-
if (c.length < 2) {
|
|
11
|
-
c = "0" + c;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
hex += c;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return hex.toLowerCase();
|
|
18
|
-
}
|
|
1
|
+
export default function arrayBufferToHex(buffer: ArrayBuffer) {
|
|
2
|
+
const data_view = new DataView(buffer);
|
|
3
|
+
let iii;
|
|
4
|
+
let len;
|
|
5
|
+
let hex = "";
|
|
6
|
+
let c;
|
|
7
|
+
|
|
8
|
+
for (iii = 0, len = data_view.byteLength; iii < len; iii += 1) {
|
|
9
|
+
c = data_view.getUint8(iii).toString(16);
|
|
10
|
+
if (c.length < 2) {
|
|
11
|
+
c = "0" + c;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
hex += c;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return hex.toLowerCase();
|
|
18
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export default function arrayBufferToString(arr: ArrayBuffer) {
|
|
2
|
-
const byteArray = new Uint8Array(arr);
|
|
3
|
-
let byteString = "";
|
|
4
|
-
for (let i = 0; i < byteArray.byteLength; i += 1) {
|
|
5
|
-
byteString += String.fromCodePoint(byteArray[i]);
|
|
6
|
-
}
|
|
7
|
-
return byteString;
|
|
8
|
-
}
|
|
1
|
+
export default function arrayBufferToString(arr: ArrayBuffer) {
|
|
2
|
+
const byteArray = new Uint8Array(arr);
|
|
3
|
+
let byteString = "";
|
|
4
|
+
for (let i = 0; i < byteArray.byteLength; i += 1) {
|
|
5
|
+
byteString += String.fromCodePoint(byteArray[i]);
|
|
6
|
+
}
|
|
7
|
+
return byteString;
|
|
8
|
+
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
export default function hexToArrayBuffer(hex: string): ArrayBuffer {
|
|
2
|
-
const pairs = hex.toUpperCase().match(/[\dA-F]{2}/gi);
|
|
3
|
-
|
|
4
|
-
if (!pairs) return new Uint8Array();
|
|
5
|
-
|
|
6
|
-
// convert the octets to integers
|
|
7
|
-
const integers = pairs.map((s) => {
|
|
8
|
-
return parseInt(s, 16);
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
const array = new Uint8Array(integers);
|
|
12
|
-
return array.buffer;
|
|
13
|
-
}
|
|
1
|
+
export default function hexToArrayBuffer(hex: string): ArrayBuffer {
|
|
2
|
+
const pairs = hex.toUpperCase().match(/[\dA-F]{2}/gi);
|
|
3
|
+
|
|
4
|
+
if (!pairs) return new Uint8Array();
|
|
5
|
+
|
|
6
|
+
// convert the octets to integers
|
|
7
|
+
const integers = pairs.map((s) => {
|
|
8
|
+
return parseInt(s, 16);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const array = new Uint8Array(integers);
|
|
12
|
+
return array.buffer;
|
|
13
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export default function hexToString(hexString: string) {
|
|
2
|
-
const hex = hexString.toString();
|
|
3
|
-
let str = "";
|
|
4
|
-
for (let n = 0; n < hex.length; n += 2) {
|
|
5
|
-
str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
|
|
6
|
-
}
|
|
7
|
-
return str;
|
|
8
|
-
}
|
|
1
|
+
export default function hexToString(hexString: string) {
|
|
2
|
+
const hex = hexString.toString();
|
|
3
|
+
let str = "";
|
|
4
|
+
for (let n = 0; n < hex.length; n += 2) {
|
|
5
|
+
str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
|
|
6
|
+
}
|
|
7
|
+
return str;
|
|
8
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { hexToArrayBuffer } from "../..";
|
|
2
|
-
|
|
3
|
-
export default function hexToUint8(hex: string) {
|
|
4
|
-
return new Uint8Array(hexToArrayBuffer(hex));
|
|
5
|
-
}
|
|
1
|
+
import { hexToArrayBuffer } from "../..";
|
|
2
|
+
|
|
3
|
+
export default function hexToUint8(hex: string) {
|
|
4
|
+
return new Uint8Array(hexToArrayBuffer(hex));
|
|
5
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export default function stringToArrayBuffer(str: string) {
|
|
2
|
-
const buf = new ArrayBuffer(str.length);
|
|
3
|
-
const bufView = new Uint8Array(buf);
|
|
4
|
-
for (let i = 0, strLen = str.length; i < strLen; i += 1) {
|
|
5
|
-
bufView[i] = str.charCodeAt(i);
|
|
6
|
-
}
|
|
7
|
-
return buf;
|
|
8
|
-
}
|
|
1
|
+
export default function stringToArrayBuffer(str: string) {
|
|
2
|
+
const buf = new ArrayBuffer(str.length);
|
|
3
|
+
const bufView = new Uint8Array(buf);
|
|
4
|
+
for (let i = 0, strLen = str.length; i < strLen; i += 1) {
|
|
5
|
+
bufView[i] = str.charCodeAt(i);
|
|
6
|
+
}
|
|
7
|
+
return buf;
|
|
8
|
+
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { ToolDbUserAdapter, sha256 } from "..";
|
|
2
|
-
|
|
3
|
-
export default function getPeerSignature(
|
|
4
|
-
account: ToolDbUserAdapter,
|
|
5
|
-
topic: string,
|
|
6
|
-
timestamp: number,
|
|
7
|
-
host: string,
|
|
8
|
-
port: number
|
|
9
|
-
) {
|
|
10
|
-
const dataToSign = sha256(`${topic}-${timestamp}-${host}:${port}`);
|
|
11
|
-
return account.signData(dataToSign);
|
|
12
|
-
}
|
|
1
|
+
import { ToolDbUserAdapter, sha256 } from "..";
|
|
2
|
+
|
|
3
|
+
export default function getPeerSignature(
|
|
4
|
+
account: ToolDbUserAdapter,
|
|
5
|
+
topic: string,
|
|
6
|
+
timestamp: number,
|
|
7
|
+
host: string,
|
|
8
|
+
port: number
|
|
9
|
+
) {
|
|
10
|
+
const dataToSign = sha256(`${topic}-${timestamp}-${host}:${port}`);
|
|
11
|
+
return account.signData(dataToSign);
|
|
12
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export default function getTimestamp() {
|
|
2
|
-
return new Date().getTime();
|
|
3
|
-
}
|
|
1
|
+
export default function getTimestamp() {
|
|
2
|
+
return new Date().getTime();
|
|
3
|
+
}
|
package/lib/utils/proofOfWork.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { sha256 } from "..";
|
|
2
|
-
|
|
3
|
-
export default function proofOfWork(
|
|
4
|
-
value: string,
|
|
5
|
-
difficulty: number
|
|
6
|
-
): Promise<{ nonce: number; hash: string }> {
|
|
7
|
-
return new Promise((resolve) => {
|
|
8
|
-
let nonce = 0;
|
|
9
|
-
let hash = sha256(`${value}${nonce}`);
|
|
10
|
-
while (hash.substring(0, difficulty) !== Array(difficulty + 1).join("0")) {
|
|
11
|
-
nonce += 1;
|
|
12
|
-
hash = sha256(`${value}${nonce}`);
|
|
13
|
-
}
|
|
14
|
-
resolve({ nonce, hash });
|
|
15
|
-
});
|
|
16
|
-
}
|
|
1
|
+
import { sha256 } from "..";
|
|
2
|
+
|
|
3
|
+
export default function proofOfWork(
|
|
4
|
+
value: string,
|
|
5
|
+
difficulty: number
|
|
6
|
+
): Promise<{ nonce: number; hash: string }> {
|
|
7
|
+
return new Promise((resolve) => {
|
|
8
|
+
let nonce = 0;
|
|
9
|
+
let hash = sha256(`${value}${nonce}`);
|
|
10
|
+
while (hash.substring(0, difficulty) !== Array(difficulty + 1).join("0")) {
|
|
11
|
+
nonce += 1;
|
|
12
|
+
hash = sha256(`${value}${nonce}`);
|
|
13
|
+
}
|
|
14
|
+
resolve({ nonce, hash });
|
|
15
|
+
});
|
|
16
|
+
}
|