tool-db 2.3.1 → 2.5.0
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 +1 -74
- package/lib/adapters-base/networkAdapter.ts +215 -0
- package/lib/adapters-base/storageAdapter.ts +35 -0
- package/lib/adapters-base/userAdapter.ts +49 -0
- package/lib/crdt/baseCrdt.ts +21 -0
- package/lib/crdt/counterCrdt.ts +111 -0
- package/lib/crdt/listCrdt.ts +190 -0
- package/lib/crdt/mapCrdt.ts +119 -0
- package/{dist/index.d.ts → lib/index.ts} +8 -5
- package/lib/logger.ts +30 -0
- package/lib/messageHandlers/handleCrdtGet.ts +29 -0
- package/lib/messageHandlers/handleCrdtPut.ts +118 -0
- package/lib/messageHandlers/handleGet.ts +29 -0
- package/lib/messageHandlers/handlePing.ts +40 -0
- package/lib/messageHandlers/handlePong.ts +30 -0
- package/lib/messageHandlers/handlePut.ts +54 -0
- package/lib/messageHandlers/handleQuery.ts +25 -0
- package/lib/messageHandlers/handleSubscribe.ts +46 -0
- package/lib/server.ts +7 -0
- package/lib/shared.ts +5 -0
- package/lib/toolDbAnonSignIn.ts +5 -0
- package/lib/toolDbClientOnMessage.ts +75 -0
- package/lib/toolDbCrdtGet.ts +82 -0
- package/lib/toolDbCrdtPut.ts +77 -0
- package/lib/toolDbGet.ts +80 -0
- package/lib/toolDbKeysSignIn.ts +16 -0
- package/lib/toolDbPut.ts +83 -0
- package/lib/toolDbQueryKeys.ts +64 -0
- package/lib/toolDbSignIn.ts +31 -0
- package/lib/toolDbSignUp.ts +71 -0
- package/lib/toolDbSubscribe.ts +53 -0
- package/lib/toolDbVerificationWrapper.ts +55 -0
- package/lib/tooldb.ts +314 -0
- package/lib/types/message.ts +133 -0
- package/lib/types/tooldb.ts +89 -0
- package/lib/utils/catchReturn.ts +4 -0
- package/lib/utils/encoding/arrayBufferToHex.ts +18 -0
- package/lib/utils/encoding/arrayBufferToString.ts +8 -0
- package/lib/utils/encoding/hexToArrayBuffer.ts +13 -0
- package/lib/utils/encoding/hexToString.ts +8 -0
- package/lib/utils/encoding/hexToUint8.ts +5 -0
- package/lib/utils/encoding/stringToArrayBuffer.ts +8 -0
- package/lib/utils/getPeerSignature.ts +12 -0
- package/lib/utils/getTimestamp.ts +3 -0
- package/lib/utils/proofOfWork.ts +16 -0
- package/lib/utils/randomAnimal.ts +77 -0
- package/lib/utils/sha1.ts +7 -0
- package/lib/utils/sha256.ts +7 -0
- package/lib/utils/textRandom.ts +11 -0
- package/lib/utils/uniq.ts +3 -0
- package/lib/utils/verifyMessage.ts +88 -0
- package/lib/utils/verifyPeer.ts +15 -0
- package/package.json +31 -65
- package/tsconfig.json +10 -72
- package/.eslintrc.json +0 -74
- package/.github/workflows/main.yml +0 -29
- package/_____peer/000005.ldb +0 -0
- package/_____peer/000008.log +0 -0
- package/_____peer/CURRENT +0 -1
- package/_____peer/LOCK +0 -0
- package/_____peer/LOG +0 -0
- package/_____peer/LOG.old +0 -0
- package/_____peer/MANIFEST-000007 +0 -0
- package/_____peer_test-alice/000003.log +0 -0
- package/_____peer_test-alice/CURRENT +0 -1
- package/_____peer_test-alice/LOCK +0 -0
- package/_____peer_test-alice/LOG +0 -1
- package/_____peer_test-alice/MANIFEST-000002 +0 -0
- package/_____peer_test-base-client/000005.ldb +0 -0
- package/_____peer_test-base-client/000006.log +0 -0
- package/_____peer_test-base-client/CURRENT +0 -1
- package/_____peer_test-base-client/LOCK +0 -0
- package/_____peer_test-base-client/LOG +0 -5
- package/_____peer_test-base-client/LOG.old +0 -1
- package/_____peer_test-base-client/MANIFEST-000004 +0 -0
- package/_____peer_test-base-server/000005.ldb +0 -0
- package/_____peer_test-base-server/000006.log +0 -0
- package/_____peer_test-base-server/CURRENT +0 -1
- package/_____peer_test-base-server/LOCK +0 -0
- package/_____peer_test-base-server/LOG +0 -5
- package/_____peer_test-base-server/LOG.old +0 -1
- package/_____peer_test-base-server/MANIFEST-000004 +0 -0
- package/_____peer_test-bob/000003.log +0 -0
- package/_____peer_test-bob/CURRENT +0 -1
- package/_____peer_test-bob/LOCK +0 -0
- package/_____peer_test-bob/LOG +0 -1
- package/_____peer_test-bob/MANIFEST-000002 +0 -0
- package/_____peer_test-chris/000003.log +0 -0
- package/_____peer_test-chris/CURRENT +0 -1
- package/_____peer_test-chris/LOCK +0 -0
- package/_____peer_test-chris/LOG +0 -1
- package/_____peer_test-chris/MANIFEST-000002 +0 -0
- package/_____peer_test-node-a/000003.log +0 -0
- package/_____peer_test-node-a/CURRENT +0 -1
- package/_____peer_test-node-a/LOCK +0 -0
- package/_____peer_test-node-a/LOG +0 -1
- package/_____peer_test-node-a/MANIFEST-000002 +0 -0
- package/_____peer_test-node-b/000003.log +0 -0
- package/_____peer_test-node-b/CURRENT +0 -1
- package/_____peer_test-node-b/LOCK +0 -0
- package/_____peer_test-node-b/LOG +0 -1
- package/_____peer_test-node-b/MANIFEST-000002 +0 -0
- package/_____peer_test-store/000003.log +0 -0
- package/_____peer_test-store/CURRENT +0 -1
- package/_____peer_test-store/LOCK +0 -0
- package/_____peer_test-store/LOG +0 -1
- package/_____peer_test-store/MANIFEST-000002 +0 -0
- package/_____peer_test-verify-a/000003.log +0 -0
- package/_____peer_test-verify-a/CURRENT +0 -1
- package/_____peer_test-verify-a/LOCK +0 -0
- package/_____peer_test-verify-a/LOG +0 -1
- package/_____peer_test-verify-a/MANIFEST-000002 +0 -0
- package/babel.config.js +0 -6
- package/bundle.js +0 -1
- package/custom-test-env.js +0 -28
- package/dist/__tests__ copy/base64.d.ts +0 -1
- package/dist/__tests__ copy/base64.js +0 -61
- package/dist/__tests__ copy/base64.js.map +0 -1
- package/dist/__tests__ copy/encryption.d.ts +0 -1
- package/dist/__tests__ copy/encryption.js +0 -68
- package/dist/__tests__ copy/encryption.js.map +0 -1
- package/dist/__tests__ copy/keys.d.ts +0 -1
- package/dist/__tests__ copy/keys.js +0 -238
- package/dist/__tests__ copy/keys.js.map +0 -1
- package/dist/__tests__ copy/network.d.ts +0 -1
- package/dist/__tests__ copy/network.js +0 -206
- package/dist/__tests__ copy/network.js.map +0 -1
- package/dist/__tests__ copy/pow.d.ts +0 -1
- package/dist/__tests__ copy/pow.js +0 -25
- package/dist/__tests__ copy/pow.js.map +0 -1
- package/dist/__tests__ copy/sha1.d.ts +0 -1
- package/dist/__tests__ copy/sha1.js +0 -16
- package/dist/__tests__ copy/sha1.js.map +0 -1
- package/dist/__tests__ copy/sha256.d.ts +0 -1
- package/dist/__tests__ copy/sha256.js +0 -16
- package/dist/__tests__ copy/sha256.js.map +0 -1
- package/dist/__tests__ copy/textRandom.d.ts +0 -1
- package/dist/__tests__ copy/textRandom.js +0 -17
- package/dist/__tests__ copy/textRandom.js.map +0 -1
- package/dist/adapters/toolDbIndexedb.d.ts +0 -9
- package/dist/adapters/toolDbIndexedb.js +0 -131
- package/dist/adapters/toolDbIndexedb.js.map +0 -1
- package/dist/adapters/toolDbLeveldb.d.ts +0 -8
- package/dist/adapters/toolDbLeveldb.js +0 -109
- package/dist/adapters/toolDbLeveldb.js.map +0 -1
- package/dist/adapters/toolDbWeb3User.d.ts +0 -18
- package/dist/adapters/toolDbWeb3User.js +0 -74
- package/dist/adapters/toolDbWeb3User.js.map +0 -1
- package/dist/adapters/toolDbWebrtc.d.ts +0 -58
- package/dist/adapters/toolDbWebrtc.js +0 -487
- package/dist/adapters/toolDbWebrtc.js.map +0 -1
- package/dist/adapters/toolDbWebsocket.d.ts +0 -18
- package/dist/adapters/toolDbWebsocket.js +0 -176
- package/dist/adapters/toolDbWebsocket.js.map +0 -1
- package/dist/adapters-base/networkAdapter.d.ts +0 -58
- package/dist/adapters-base/networkAdapter.js +0 -214
- package/dist/adapters-base/networkAdapter.js.map +0 -1
- package/dist/adapters-base/storageAdapter.d.ts +0 -11
- package/dist/adapters-base/storageAdapter.js +0 -36
- package/dist/adapters-base/storageAdapter.js.map +0 -1
- package/dist/adapters-base/userAdapter.d.ts +0 -15
- package/dist/adapters-base/userAdapter.js +0 -42
- package/dist/adapters-base/userAdapter.js.map +0 -1
- package/dist/bundle.js +0 -69086
- package/dist/chain/Block.d.ts +0 -29
- package/dist/chain/Block.js +0 -142
- package/dist/chain/Block.js.map +0 -1
- package/dist/chain/ToolChain.d.ts +0 -27
- package/dist/chain/ToolChain.js +0 -253
- package/dist/chain/ToolChain.js.map +0 -1
- package/dist/chain/Tx.d.ts +0 -50
- package/dist/chain/Tx.js +0 -54
- package/dist/chain/Tx.js.map +0 -1
- package/dist/chain/Wallet.d.ts +0 -9
- package/dist/chain/Wallet.js +0 -37
- package/dist/chain/Wallet.js.map +0 -1
- package/dist/chain/isValidChain.d.ts +0 -1
- package/dist/chain/isValidChain.js +0 -37
- package/dist/chain/isValidChain.js.map +0 -1
- package/dist/chain/mempool.d.ts +0 -0
- package/dist/chain/mempool.js +0 -2
- package/dist/chain/mempool.js.map +0 -1
- package/dist/crdt/baseCrdt.d.ts +0 -9
- package/dist/crdt/baseCrdt.js +0 -27
- package/dist/crdt/baseCrdt.js.map +0 -1
- package/dist/crdt/counterCrdt.d.ts +0 -30
- package/dist/crdt/counterCrdt.js +0 -106
- package/dist/crdt/counterCrdt.js.map +0 -1
- package/dist/crdt/listCrdt.d.ts +0 -42
- package/dist/crdt/listCrdt.js +0 -159
- package/dist/crdt/listCrdt.js.map +0 -1
- package/dist/crdt/mapCrdt.d.ts +0 -32
- package/dist/crdt/mapCrdt.js +0 -118
- package/dist/crdt/mapCrdt.js.map +0 -1
- package/dist/getCrypto.d.ts +0 -1
- package/dist/getCrypto.js +0 -10
- package/dist/getCrypto.js.map +0 -1
- package/dist/index.js +0 -93
- package/dist/index.js.map +0 -1
- package/dist/infuraFilecoin.d.ts +0 -0
- package/dist/infuraFilecoin.js +0 -2
- package/dist/infuraFilecoin.js.map +0 -1
- package/dist/loadCrdtDocument.d.ts +0 -3
- package/dist/loadCrdtDocument.js +0 -43
- package/dist/loadCrdtDocument.js.map +0 -1
- package/dist/logger.d.ts +0 -2
- package/dist/logger.js +0 -29
- package/dist/logger.js.map +0 -1
- package/dist/makeTx.d.ts +0 -1
- package/dist/makeTx.js +0 -27
- package/dist/makeTx.js.map +0 -1
- package/dist/messageHandlers/handleCrdt.d.ts +0 -2
- package/dist/messageHandlers/handleCrdt.js +0 -15
- package/dist/messageHandlers/handleCrdt.js.map +0 -1
- package/dist/messageHandlers/handleCrdtGet.d.ts +0 -2
- package/dist/messageHandlers/handleCrdtGet.js +0 -29
- package/dist/messageHandlers/handleCrdtGet.js.map +0 -1
- package/dist/messageHandlers/handleCrdtPut.d.ts +0 -2
- package/dist/messageHandlers/handleCrdtPut.js +0 -93
- package/dist/messageHandlers/handleCrdtPut.js.map +0 -1
- package/dist/messageHandlers/handleGet.d.ts +0 -2
- package/dist/messageHandlers/handleGet.js +0 -29
- package/dist/messageHandlers/handleGet.js.map +0 -1
- package/dist/messageHandlers/handleJoin.d.ts +0 -2
- package/dist/messageHandlers/handleJoin.js +0 -36
- package/dist/messageHandlers/handleJoin.js.map +0 -1
- package/dist/messageHandlers/handlePing.d.ts +0 -2
- package/dist/messageHandlers/handlePing.js +0 -36
- package/dist/messageHandlers/handlePing.js.map +0 -1
- package/dist/messageHandlers/handlePong.d.ts +0 -2
- package/dist/messageHandlers/handlePong.js +0 -26
- package/dist/messageHandlers/handlePong.js.map +0 -1
- package/dist/messageHandlers/handlePut.d.ts +0 -2
- package/dist/messageHandlers/handlePut.js +0 -57
- package/dist/messageHandlers/handlePut.js.map +0 -1
- package/dist/messageHandlers/handleQuery.d.ts +0 -2
- package/dist/messageHandlers/handleQuery.js +0 -23
- package/dist/messageHandlers/handleQuery.js.map +0 -1
- package/dist/messageHandlers/handleServers.d.ts +0 -2
- package/dist/messageHandlers/handleServers.js +0 -23
- package/dist/messageHandlers/handleServers.js.map +0 -1
- package/dist/messageHandlers/handleSubscribe.d.ts +0 -2
- package/dist/messageHandlers/handleSubscribe.js +0 -44
- package/dist/messageHandlers/handleSubscribe.js.map +0 -1
- package/dist/sendJoin.d.ts +0 -2
- package/dist/sendJoin.js +0 -31
- package/dist/sendJoin.js.map +0 -1
- package/dist/server.d.ts +0 -1
- package/dist/server.js +0 -9
- package/dist/server.js.map +0 -1
- package/dist/shared.d.ts +0 -2
- package/dist/shared.js +0 -7
- package/dist/shared.js.map +0 -1
- package/dist/test.d.ts +0 -1
- package/dist/test.js +0 -17
- package/dist/test.js.map +0 -1
- package/dist/toolChainServer.d.ts +0 -8
- package/dist/toolChainServer.js +0 -60
- package/dist/toolChainServer.js.map +0 -1
- package/dist/toolDbAnonSignIn.d.ts +0 -2
- package/dist/toolDbAnonSignIn.js +0 -7
- package/dist/toolDbAnonSignIn.js.map +0 -1
- package/dist/toolDbClientOnMessage.d.ts +0 -2
- package/dist/toolDbClientOnMessage.js +0 -59
- package/dist/toolDbClientOnMessage.js.map +0 -1
- package/dist/toolDbCrdtGet.d.ts +0 -9
- package/dist/toolDbCrdtGet.js +0 -77
- package/dist/toolDbCrdtGet.js.map +0 -1
- package/dist/toolDbCrdtPut.d.ts +0 -9
- package/dist/toolDbCrdtPut.js +0 -63
- package/dist/toolDbCrdtPut.js.map +0 -1
- package/dist/toolDbGet.d.ts +0 -9
- package/dist/toolDbGet.js +0 -77
- package/dist/toolDbGet.js.map +0 -1
- package/dist/toolDbGetPubKey.d.ts +0 -2
- package/dist/toolDbGetPubKey.js +0 -18
- package/dist/toolDbGetPubKey.js.map +0 -1
- package/dist/toolDbKeysSignIn.d.ts +0 -2
- package/dist/toolDbKeysSignIn.js +0 -16
- package/dist/toolDbKeysSignIn.js.map +0 -1
- package/dist/toolDbNetwork.d.ts +0 -32
- package/dist/toolDbNetwork.js +0 -296
- package/dist/toolDbNetwork.js.map +0 -1
- package/dist/toolDbNetworkAdapter.d.ts +0 -57
- package/dist/toolDbNetworkAdapter.js +0 -188
- package/dist/toolDbNetworkAdapter.js.map +0 -1
- package/dist/toolDbPut.d.ts +0 -9
- package/dist/toolDbPut.js +0 -69
- package/dist/toolDbPut.js.map +0 -1
- package/dist/toolDbQueryKeys.d.ts +0 -8
- package/dist/toolDbQueryKeys.js +0 -66
- package/dist/toolDbQueryKeys.js.map +0 -1
- package/dist/toolDbSignIn.d.ts +0 -2
- package/dist/toolDbSignIn.js +0 -27
- package/dist/toolDbSignIn.js.map +0 -1
- package/dist/toolDbSignUp.d.ts +0 -2
- package/dist/toolDbSignUp.js +0 -101
- package/dist/toolDbSignUp.js.map +0 -1
- package/dist/toolDbSubscribe.d.ts +0 -9
- package/dist/toolDbSubscribe.js +0 -50
- package/dist/toolDbSubscribe.js.map +0 -1
- package/dist/toolDbVerificationWrapper.d.ts +0 -2
- package/dist/toolDbVerificationWrapper.js +0 -98
- package/dist/toolDbVerificationWrapper.js.map +0 -1
- package/dist/toolDbWeb3User.d.ts +0 -20
- package/dist/toolDbWeb3User.js +0 -75
- package/dist/toolDbWeb3User.js.map +0 -1
- package/dist/toolDbWebrtc.d.ts +0 -75
- package/dist/toolDbWebrtc.js +0 -472
- package/dist/toolDbWebrtc.js.map +0 -1
- package/dist/toolDbWebsocket.d.ts +0 -19
- package/dist/toolDbWebsocket.js +0 -187
- package/dist/toolDbWebsocket.js.map +0 -1
- package/dist/toolchain.d.ts +0 -1
- package/dist/toolchain.js +0 -8
- package/dist/toolchain.js.map +0 -1
- package/dist/tooldb.d.ts +0 -108
- package/dist/tooldb.js +0 -285
- package/dist/tooldb.js.map +0 -1
- package/dist/types/graph.d.ts +0 -0
- package/dist/types/graph.js +0 -2
- package/dist/types/graph.js.map +0 -1
- package/dist/types/message.d.ts +0 -100
- package/dist/types/message.js +0 -17
- package/dist/types/message.js.map +0 -1
- package/dist/types/tooldb.d.ts +0 -79
- package/dist/types/tooldb.js +0 -3
- package/dist/types/tooldb.js.map +0 -1
- package/dist/utils/arrayBufferToBase64.d.ts +0 -1
- package/dist/utils/arrayBufferToBase64.js +0 -12
- package/dist/utils/arrayBufferToBase64.js.map +0 -1
- package/dist/utils/arrayBufferToHex.d.ts +0 -1
- package/dist/utils/arrayBufferToHex.js +0 -19
- package/dist/utils/arrayBufferToHex.js.map +0 -1
- package/dist/utils/arrayBufferToString.d.ts +0 -1
- package/dist/utils/arrayBufferToString.js +0 -12
- package/dist/utils/arrayBufferToString.js.map +0 -1
- package/dist/utils/base64ToArrayBuffer.d.ts +0 -1
- package/dist/utils/base64ToArrayBuffer.js +0 -12
- package/dist/utils/base64ToArrayBuffer.js.map +0 -1
- package/dist/utils/base64ToBinaryChange.d.ts +0 -2
- package/dist/utils/base64ToBinaryChange.js +0 -18
- package/dist/utils/base64ToBinaryChange.js.map +0 -1
- package/dist/utils/base64ToBinaryDocument.d.ts +0 -2
- package/dist/utils/base64ToBinaryDocument.js +0 -18
- package/dist/utils/base64ToBinaryDocument.js.map +0 -1
- package/dist/utils/base64ToHex.d.ts +0 -1
- package/dist/utils/base64ToHex.js +0 -13
- package/dist/utils/base64ToHex.js.map +0 -1
- package/dist/utils/base64ToUint8.d.ts +0 -1
- package/dist/utils/base64ToUint8.js +0 -17
- package/dist/utils/base64ToUint8.js.map +0 -1
- package/dist/utils/catchReturn.d.ts +0 -1
- package/dist/utils/catchReturn.js +0 -8
- package/dist/utils/catchReturn.js.map +0 -1
- package/dist/utils/crypto/base64KeyToHex.d.ts +0 -1
- package/dist/utils/crypto/base64KeyToHex.js +0 -24
- package/dist/utils/crypto/base64KeyToHex.js.map +0 -1
- package/dist/utils/crypto/base64PublicKeyToHex.d.ts +0 -1
- package/dist/utils/crypto/base64PublicKeyToHex.js +0 -22
- package/dist/utils/crypto/base64PublicKeyToHex.js.map +0 -1
- package/dist/utils/crypto/decodeKeyString.d.ts +0 -1
- package/dist/utils/crypto/decodeKeyString.js +0 -13
- package/dist/utils/crypto/decodeKeyString.js.map +0 -1
- package/dist/utils/crypto/decryptData.d.ts +0 -1
- package/dist/utils/crypto/decryptData.js +0 -28
- package/dist/utils/crypto/decryptData.js.map +0 -1
- package/dist/utils/crypto/decryptWithPass.d.ts +0 -1
- package/dist/utils/crypto/decryptWithPass.js +0 -23
- package/dist/utils/crypto/decryptWithPass.js.map +0 -1
- package/dist/utils/crypto/deriveSecret.d.ts +0 -1
- package/dist/utils/crypto/deriveSecret.js +0 -18
- package/dist/utils/crypto/deriveSecret.js.map +0 -1
- package/dist/utils/crypto/encodeKeyString.d.ts +0 -1
- package/dist/utils/crypto/encodeKeyString.js +0 -13
- package/dist/utils/crypto/encodeKeyString.js.map +0 -1
- package/dist/utils/crypto/encryptData.d.ts +0 -1
- package/dist/utils/crypto/encryptData.js +0 -30
- package/dist/utils/crypto/encryptData.js.map +0 -1
- package/dist/utils/crypto/encryptWithPass.d.ts +0 -1
- package/dist/utils/crypto/encryptWithPass.js +0 -26
- package/dist/utils/crypto/encryptWithPass.js.map +0 -1
- package/dist/utils/crypto/exportKey.d.ts +0 -1
- package/dist/utils/crypto/exportKey.js +0 -14
- package/dist/utils/crypto/exportKey.js.map +0 -1
- package/dist/utils/crypto/exportKeyAsHex.d.ts +0 -1
- package/dist/utils/crypto/exportKeyAsHex.js +0 -18
- package/dist/utils/crypto/exportKeyAsHex.js.map +0 -1
- package/dist/utils/crypto/generateKeyFromPassword.d.ts +0 -1
- package/dist/utils/crypto/generateKeyFromPassword.js +0 -25
- package/dist/utils/crypto/generateKeyFromPassword.js.map +0 -1
- package/dist/utils/crypto/generateKeyPair.d.ts +0 -1
- package/dist/utils/crypto/generateKeyPair.js +0 -17
- package/dist/utils/crypto/generateKeyPair.js.map +0 -1
- package/dist/utils/crypto/generateKeysComb.d.ts +0 -4
- package/dist/utils/crypto/generateKeysComb.js +0 -60
- package/dist/utils/crypto/generateKeysComb.js.map +0 -1
- package/dist/utils/crypto/importKey.d.ts +0 -1
- package/dist/utils/crypto/importKey.js +0 -23
- package/dist/utils/crypto/importKey.js.map +0 -1
- package/dist/utils/crypto/loadKeysComb.d.ts +0 -5
- package/dist/utils/crypto/loadKeysComb.js +0 -90
- package/dist/utils/crypto/loadKeysComb.js.map +0 -1
- package/dist/utils/crypto/loadSignKeys.d.ts +0 -2
- package/dist/utils/crypto/loadSignKeys.js +0 -78
- package/dist/utils/crypto/loadSignKeys.js.map +0 -1
- package/dist/utils/crypto/recoverPubKey.d.ts +0 -1
- package/dist/utils/crypto/recoverPubKey.js +0 -27
- package/dist/utils/crypto/recoverPubKey.js.map +0 -1
- package/dist/utils/crypto/recoverPubKeyFromPrivate.d.ts +0 -1
- package/dist/utils/crypto/recoverPubKeyFromPrivate.js +0 -16
- package/dist/utils/crypto/recoverPubKeyFromPrivate.js.map +0 -1
- package/dist/utils/crypto/saveKeysComb.d.ts +0 -2
- package/dist/utils/crypto/saveKeysComb.js +0 -75
- package/dist/utils/crypto/saveKeysComb.js.map +0 -1
- package/dist/utils/crypto/saveSignKeys.d.ts +0 -2
- package/dist/utils/crypto/saveSignKeys.js +0 -67
- package/dist/utils/crypto/saveSignKeys.js.map +0 -1
- package/dist/utils/crypto/verifyData.d.ts +0 -1
- package/dist/utils/crypto/verifyData.js +0 -20
- package/dist/utils/crypto/verifyData.js.map +0 -1
- package/dist/utils/encoding/arrayBufferToBase64.d.ts +0 -1
- package/dist/utils/encoding/arrayBufferToBase64.js +0 -12
- package/dist/utils/encoding/arrayBufferToBase64.js.map +0 -1
- package/dist/utils/encoding/arrayBufferToHex.d.ts +0 -1
- package/dist/utils/encoding/arrayBufferToHex.js +0 -19
- package/dist/utils/encoding/arrayBufferToHex.js.map +0 -1
- package/dist/utils/encoding/arrayBufferToString.d.ts +0 -1
- package/dist/utils/encoding/arrayBufferToString.js +0 -12
- package/dist/utils/encoding/arrayBufferToString.js.map +0 -1
- package/dist/utils/encoding/base64ToArrayBuffer.d.ts +0 -1
- package/dist/utils/encoding/base64ToArrayBuffer.js +0 -12
- package/dist/utils/encoding/base64ToArrayBuffer.js.map +0 -1
- package/dist/utils/encoding/base64ToBinaryChange.d.ts +0 -2
- package/dist/utils/encoding/base64ToBinaryChange.js +0 -18
- package/dist/utils/encoding/base64ToBinaryChange.js.map +0 -1
- package/dist/utils/encoding/base64ToBinaryDocument.d.ts +0 -2
- package/dist/utils/encoding/base64ToBinaryDocument.js +0 -18
- package/dist/utils/encoding/base64ToBinaryDocument.js.map +0 -1
- package/dist/utils/encoding/base64ToHex.d.ts +0 -1
- package/dist/utils/encoding/base64ToHex.js +0 -13
- package/dist/utils/encoding/base64ToHex.js.map +0 -1
- package/dist/utils/encoding/base64ToUint8.d.ts +0 -1
- package/dist/utils/encoding/base64ToUint8.js +0 -17
- package/dist/utils/encoding/base64ToUint8.js.map +0 -1
- package/dist/utils/encoding/fromBase64.d.ts +0 -1
- package/dist/utils/encoding/fromBase64.js +0 -7
- package/dist/utils/encoding/fromBase64.js.map +0 -1
- package/dist/utils/encoding/hexToArrayBuffer.d.ts +0 -1
- package/dist/utils/encoding/hexToArrayBuffer.js +0 -15
- package/dist/utils/encoding/hexToArrayBuffer.js.map +0 -1
- package/dist/utils/encoding/hexToBase64.d.ts +0 -1
- package/dist/utils/encoding/hexToBase64.js +0 -12
- package/dist/utils/encoding/hexToBase64.js.map +0 -1
- package/dist/utils/encoding/hexToString.d.ts +0 -1
- package/dist/utils/encoding/hexToString.js +0 -12
- package/dist/utils/encoding/hexToString.js.map +0 -1
- package/dist/utils/encoding/hexToUint8.d.ts +0 -1
- package/dist/utils/encoding/hexToUint8.js +0 -8
- package/dist/utils/encoding/hexToUint8.js.map +0 -1
- package/dist/utils/encoding/stringToArrayBuffer.d.ts +0 -1
- package/dist/utils/encoding/stringToArrayBuffer.js +0 -12
- package/dist/utils/encoding/stringToArrayBuffer.js.map +0 -1
- package/dist/utils/encoding/toBase64.d.ts +0 -1
- package/dist/utils/encoding/toBase64.js +0 -7
- package/dist/utils/encoding/toBase64.js.map +0 -1
- package/dist/utils/encoding/uint8ArrayToHex.d.ts +0 -1
- package/dist/utils/encoding/uint8ArrayToHex.js +0 -7
- package/dist/utils/encoding/uint8ArrayToHex.js.map +0 -1
- package/dist/utils/encoding/uint8ToBase64.d.ts +0 -1
- package/dist/utils/encoding/uint8ToBase64.js +0 -15
- package/dist/utils/encoding/uint8ToBase64.js.map +0 -1
- package/dist/utils/fromBase64.d.ts +0 -1
- package/dist/utils/fromBase64.js +0 -7
- package/dist/utils/fromBase64.js.map +0 -1
- package/dist/utils/generateIv.d.ts +0 -1
- package/dist/utils/generateIv.js +0 -12
- package/dist/utils/generateIv.js.map +0 -1
- package/dist/utils/getPeerSignature.d.ts +0 -2
- package/dist/utils/getPeerSignature.js +0 -9
- package/dist/utils/getPeerSignature.js.map +0 -1
- package/dist/utils/getTimestamp.d.ts +0 -1
- package/dist/utils/getTimestamp.js +0 -7
- package/dist/utils/getTimestamp.js.map +0 -1
- package/dist/utils/hexToArrayBuffer.d.ts +0 -1
- package/dist/utils/hexToArrayBuffer.js +0 -15
- package/dist/utils/hexToArrayBuffer.js.map +0 -1
- package/dist/utils/hexToBase64.d.ts +0 -1
- package/dist/utils/hexToBase64.js +0 -12
- package/dist/utils/hexToBase64.js.map +0 -1
- package/dist/utils/hexToString.d.ts +0 -1
- package/dist/utils/hexToString.js +0 -12
- package/dist/utils/hexToString.js.map +0 -1
- package/dist/utils/hexToUint8.d.ts +0 -1
- package/dist/utils/hexToUint8.js +0 -8
- package/dist/utils/hexToUint8.js.map +0 -1
- package/dist/utils/indexedb.d.ts +0 -2
- package/dist/utils/indexedb.js +0 -116
- package/dist/utils/indexedb.js.map +0 -1
- package/dist/utils/leveldb.d.ts +0 -2
- package/dist/utils/leveldb.js +0 -83
- package/dist/utils/leveldb.js.map +0 -1
- package/dist/utils/proofOfWork.d.ts +0 -4
- package/dist/utils/proofOfWork.js +0 -16
- package/dist/utils/proofOfWork.js.map +0 -1
- package/dist/utils/randomAnimal.d.ts +0 -1
- package/dist/utils/randomAnimal.js +0 -77
- package/dist/utils/randomAnimal.js.map +0 -1
- package/dist/utils/sha1.d.ts +0 -1
- package/dist/utils/sha1.js +0 -13
- package/dist/utils/sha1.js.map +0 -1
- package/dist/utils/sha256.d.ts +0 -3
- package/dist/utils/sha256.js +0 -13
- package/dist/utils/sha256.js.map +0 -1
- package/dist/utils/signData.d.ts +0 -1
- package/dist/utils/signData.js +0 -19
- package/dist/utils/signData.js.map +0 -1
- package/dist/utils/stringToArrayBuffer.d.ts +0 -1
- package/dist/utils/stringToArrayBuffer.js +0 -12
- package/dist/utils/stringToArrayBuffer.js.map +0 -1
- package/dist/utils/textRandom.d.ts +0 -1
- package/dist/utils/textRandom.js +0 -15
- package/dist/utils/textRandom.js.map +0 -1
- package/dist/utils/toBase64.d.ts +0 -1
- package/dist/utils/toBase64.js +0 -7
- package/dist/utils/toBase64.js.map +0 -1
- package/dist/utils/uint8ArrayToHex.d.ts +0 -2
- package/dist/utils/uint8ArrayToHex.js +0 -7
- package/dist/utils/uint8ArrayToHex.js.map +0 -1
- package/dist/utils/uint8ToBase64.d.ts +0 -2
- package/dist/utils/uint8ToBase64.js +0 -15
- package/dist/utils/uint8ToBase64.js.map +0 -1
- package/dist/utils/uniq.d.ts +0 -1
- package/dist/utils/uniq.js +0 -7
- package/dist/utils/uniq.js.map +0 -1
- package/dist/utils/verifyMessage.d.ts +0 -8
- package/dist/utils/verifyMessage.js +0 -129
- package/dist/utils/verifyMessage.js.map +0 -1
- package/dist/utils/verifyPeer.d.ts +0 -2
- package/dist/utils/verifyPeer.js +0 -15
- package/dist/utils/verifyPeer.js.map +0 -1
- package/jest.config.ts +0 -195
- package/jest.setup.js +0 -0
- package/test-alice/000216.ldb +0 -0
- package/test-alice/000218.ldb +0 -0
- package/test-alice/000221.ldb +0 -0
- package/test-alice/000224.ldb +0 -0
- package/test-alice/000225.log +0 -0
- package/test-alice/CURRENT +0 -1
- package/test-alice/LOCK +0 -0
- package/test-alice/LOG +0 -5
- package/test-alice/LOG.old +0 -5
- package/test-alice/MANIFEST-000223 +0 -0
- package/test-base-client/000019.log +0 -0
- package/test-base-client/CURRENT +0 -1
- package/test-base-client/LOCK +0 -0
- package/test-base-client/LOG +0 -3
- package/test-base-client/LOG.old +0 -3
- package/test-base-client/MANIFEST-000018 +0 -0
- package/test-base-server/000019.log +0 -0
- package/test-base-server/CURRENT +0 -1
- package/test-base-server/LOCK +0 -0
- package/test-base-server/LOG +0 -3
- package/test-base-server/LOG.old +0 -3
- package/test-base-server/MANIFEST-000018 +0 -0
- package/test-bob/000231.ldb +0 -0
- package/test-bob/000233.ldb +0 -0
- package/test-bob/000234.log +0 -0
- package/test-bob/CURRENT +0 -1
- package/test-bob/LOCK +0 -0
- package/test-bob/LOG +0 -5
- package/test-bob/LOG.old +0 -14
- package/test-bob/MANIFEST-000232 +0 -0
- package/test-chris/000005.ldb +0 -0
- package/test-chris/000008.ldb +0 -0
- package/test-chris/000009.log +0 -0
- package/test-chris/CURRENT +0 -1
- package/test-chris/LOCK +0 -0
- package/test-chris/LOG +0 -5
- package/test-chris/LOG.old +0 -5
- package/test-chris/MANIFEST-000007 +0 -0
- package/test-node-a/000246.ldb +0 -0
- package/test-node-a/000248.ldb +0 -0
- package/test-node-a/000249.log +0 -0
- package/test-node-a/CURRENT +0 -1
- package/test-node-a/LOCK +0 -0
- package/test-node-a/LOG +0 -5
- package/test-node-a/LOG.old +0 -14
- package/test-node-a/MANIFEST-000247 +0 -0
- package/test-node-b/000246.ldb +0 -0
- package/test-node-b/000248.ldb +0 -0
- package/test-node-b/000249.log +0 -0
- package/test-node-b/CURRENT +0 -1
- package/test-node-b/LOCK +0 -0
- package/test-node-b/LOG +0 -5
- package/test-node-b/LOG.old +0 -14
- package/test-node-b/MANIFEST-000247 +0 -0
- package/test-store/000005.ldb +0 -0
- package/test-store/000008.ldb +0 -0
- package/test-store/000011.ldb +0 -0
- package/test-store/000014.ldb +0 -0
- package/test-store/000017.ldb +0 -0
- package/test-store/000018.log +0 -0
- package/test-store/CURRENT +0 -1
- package/test-store/LOCK +0 -0
- package/test-store/LOG +0 -6
- package/test-store/LOG.old +0 -6
- package/test-store/MANIFEST-000016 +0 -0
- package/test-verify-a/000059.ldb +0 -0
- package/test-verify-a/000066.log +0 -0
- package/test-verify-a/CURRENT +0 -1
- package/test-verify-a/LOCK +0 -0
- package/test-verify-a/LOG +0 -3
- package/test-verify-a/LOG.old +0 -3
- package/test-verify-a/MANIFEST-000065 +0 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { BaseCrdt, CRDT_MAP } from "..";
|
|
2
|
+
|
|
3
|
+
export type MapOperations = "SET" | "DEL";
|
|
4
|
+
|
|
5
|
+
export interface ChangeMapBase<T> {
|
|
6
|
+
t: MapOperations;
|
|
7
|
+
a: string;
|
|
8
|
+
k: string;
|
|
9
|
+
i: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface SetMapChange<T> extends ChangeMapBase<T> {
|
|
13
|
+
t: "SET";
|
|
14
|
+
v: T;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface DelMapChange<T> extends ChangeMapBase<T> {
|
|
18
|
+
t: "DEL";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type MapChanges<T> = SetMapChange<T> | DelMapChange<T>;
|
|
22
|
+
|
|
23
|
+
export class MapCrdt<T> extends BaseCrdt<T, MapChanges<T>, Record<string, T>> {
|
|
24
|
+
public type = CRDT_MAP;
|
|
25
|
+
|
|
26
|
+
private _changes: MapChanges<T>[] = [];
|
|
27
|
+
|
|
28
|
+
private _author = "";
|
|
29
|
+
|
|
30
|
+
private _value: Record<string, T> = {};
|
|
31
|
+
|
|
32
|
+
private _keyIndex: Record<string, number> = {};
|
|
33
|
+
|
|
34
|
+
private _lastUpdateSize: number = 0;
|
|
35
|
+
|
|
36
|
+
constructor(author: string, changes?: MapChanges<T>[]) {
|
|
37
|
+
super();
|
|
38
|
+
this._author = author;
|
|
39
|
+
if (changes) {
|
|
40
|
+
this.mergeChanges(changes);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
this.calculate();
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
changesSort(a: MapChanges<T>, b: MapChanges<T>) {
|
|
47
|
+
if (a.i < b.i) return -1;
|
|
48
|
+
if (a.i > b.i) return 1;
|
|
49
|
+
if (a.t === "SET" && b.t === "DEL") return -1;
|
|
50
|
+
if (a.t === "DEL" && b.t === "SET") return 1;
|
|
51
|
+
if (a.a < b.a) return -1;
|
|
52
|
+
if (a.a > b.a) return 1;
|
|
53
|
+
return 0; // Should never be equal!
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
calculate() {
|
|
57
|
+
const temp: Record<string, T> = {};
|
|
58
|
+
// Only update if we have new changes
|
|
59
|
+
if (Object.values(this._changes).length !== this._lastUpdateSize) {
|
|
60
|
+
this._changes.sort(this.changesSort).forEach((change) => {
|
|
61
|
+
// Here we apply the model properties
|
|
62
|
+
// Since this is a KV store we dont need much logic,
|
|
63
|
+
// except for the sorting of the changes. Then we just apply them.
|
|
64
|
+
if (change.t === "SET") {
|
|
65
|
+
temp[change.k] = change.v;
|
|
66
|
+
} else if (change.t === "DEL") {
|
|
67
|
+
delete temp[change.k];
|
|
68
|
+
}
|
|
69
|
+
this._keyIndex[change.k] = change.i;
|
|
70
|
+
});
|
|
71
|
+
this._lastUpdateSize = Object.values(temp).length;
|
|
72
|
+
this._value = temp;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get value(): Record<string, T> {
|
|
77
|
+
this.calculate();
|
|
78
|
+
return this._value;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
public mergeChanges(newChanges: MapChanges<T>[]) {
|
|
82
|
+
newChanges.forEach((change) => {
|
|
83
|
+
// Filter by author and index
|
|
84
|
+
const filtered = this._changes.filter(
|
|
85
|
+
(c) => c.i === change.i && c.a === change.a && c.t === change.t
|
|
86
|
+
);
|
|
87
|
+
// Only add if there are not matches
|
|
88
|
+
if (filtered.length === 0) {
|
|
89
|
+
this._changes.push(change);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
this.calculate();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
public getChanges(): MapChanges<T>[] {
|
|
96
|
+
return this._changes;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
public SET(key: string, value: T) {
|
|
100
|
+
this._keyIndex[key] = (this._keyIndex[key] || 0) + 1;
|
|
101
|
+
this._changes.push({
|
|
102
|
+
t: "SET",
|
|
103
|
+
k: key,
|
|
104
|
+
a: this._author,
|
|
105
|
+
v: value,
|
|
106
|
+
i: this._keyIndex[key],
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
public DEL(key: string) {
|
|
111
|
+
this._keyIndex[key] = (this._keyIndex[key] || 0) + 1;
|
|
112
|
+
this._changes.push({
|
|
113
|
+
t: "DEL",
|
|
114
|
+
k: key,
|
|
115
|
+
a: this._author,
|
|
116
|
+
i: this._keyIndex[key],
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
@@ -2,32 +2,34 @@ export { default as proofOfWork } from "./utils/proofOfWork";
|
|
|
2
2
|
export { default as sha1 } from "./utils/sha1";
|
|
3
3
|
export { default as sha256 } from "./utils/sha256";
|
|
4
4
|
export { default as uniq } from "./utils/uniq";
|
|
5
|
+
|
|
5
6
|
export { default as randomAnimal } from "./utils/randomAnimal";
|
|
6
7
|
export { default as verifyMessage } from "./utils/verifyMessage";
|
|
7
8
|
export { default as getPeerSignature } from "./utils/getPeerSignature";
|
|
8
9
|
export { default as verifyPeer } from "./utils/verifyPeer";
|
|
9
10
|
export { default as textRandom } from "./utils/textRandom";
|
|
11
|
+
|
|
10
12
|
export { default as catchReturn } from "./utils/catchReturn";
|
|
13
|
+
|
|
11
14
|
export { default as stringToArrayBuffer } from "./utils/encoding/stringToArrayBuffer";
|
|
12
15
|
export { default as arrayBufferToString } from "./utils/encoding/arrayBufferToString";
|
|
16
|
+
|
|
13
17
|
export { default as arrayBufferToHex } from "./utils/encoding/arrayBufferToHex";
|
|
14
18
|
export { default as hexToArrayBuffer } from "./utils/encoding/hexToArrayBuffer";
|
|
15
19
|
export { default as hexToString } from "./utils/encoding/hexToString";
|
|
16
20
|
export { default as hexToUint8 } from "./utils/encoding/hexToUint8";
|
|
21
|
+
|
|
17
22
|
export { default as ToolDbNetworkAdapter } from "./adapters-base/networkAdapter";
|
|
18
23
|
export { default as ToolDbStorageAdapter } from "./adapters-base/storageAdapter";
|
|
19
24
|
export { default as ToolDbUserAdapter } from "./adapters-base/userAdapter";
|
|
20
|
-
|
|
21
|
-
export { default as ToolDbWebrtc } from "./adapters/toolDbWebrtc";
|
|
22
|
-
export { default as ToolDbWeb3User } from "./adapters/toolDbWeb3User";
|
|
23
|
-
export { default as ToolDbIndexedb } from "./adapters/toolDbIndexedb";
|
|
24
|
-
export { default as ToolDbLeveldb } from "./adapters/toolDbLeveldb";
|
|
25
|
+
|
|
25
26
|
export * from "./types/tooldb";
|
|
26
27
|
export * from "./types/message";
|
|
27
28
|
export * from "./crdt/baseCrdt";
|
|
28
29
|
export * from "./crdt/counterCrdt";
|
|
29
30
|
export * from "./crdt/listCrdt";
|
|
30
31
|
export * from "./crdt/mapCrdt";
|
|
32
|
+
|
|
31
33
|
export { default as handleSubscribe } from "./messageHandlers/handleSubscribe";
|
|
32
34
|
export { default as handleCrdtGet } from "./messageHandlers/handleCrdtGet";
|
|
33
35
|
export { default as handleCrdtPut } from "./messageHandlers/handleCrdtPut";
|
|
@@ -36,4 +38,5 @@ export { default as handlePing } from "./messageHandlers/handlePing";
|
|
|
36
38
|
export { default as handlePong } from "./messageHandlers/handlePong";
|
|
37
39
|
export { default as handleGet } from "./messageHandlers/handleGet";
|
|
38
40
|
export { default as handlePut } from "./messageHandlers/handlePut";
|
|
41
|
+
|
|
39
42
|
export { default as ToolDb } from "./tooldb";
|
package/lib/logger.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ToolDb, sha1 } from ".";
|
|
2
|
+
|
|
3
|
+
export default function logger(
|
|
4
|
+
this: ToolDb,
|
|
5
|
+
arg0: any,
|
|
6
|
+
arg1?: any,
|
|
7
|
+
arg2?: any,
|
|
8
|
+
arg3?: any,
|
|
9
|
+
arg4?: any,
|
|
10
|
+
arg5?: any
|
|
11
|
+
) {
|
|
12
|
+
if (this.options.debug) {
|
|
13
|
+
const isNode = typeof window === "undefined";
|
|
14
|
+
const title = isNode
|
|
15
|
+
? this.options.storageName
|
|
16
|
+
: "%c" + this.options.storageName;
|
|
17
|
+
|
|
18
|
+
const style = isNode
|
|
19
|
+
? ""
|
|
20
|
+
: "background: #" +
|
|
21
|
+
sha1(this.options.storageName).slice(-6) +
|
|
22
|
+
"; padding: 2px; color: black";
|
|
23
|
+
|
|
24
|
+
console.log(
|
|
25
|
+
title,
|
|
26
|
+
style,
|
|
27
|
+
...[arg0, arg1, arg2, arg3, arg4, arg4, arg5].slice(0, arguments.length)
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ToolDb, CrdtGetMessage, CrdtPutMessage } from "..";
|
|
2
|
+
|
|
3
|
+
export default function handleCrdtGet(
|
|
4
|
+
this: ToolDb,
|
|
5
|
+
message: CrdtGetMessage,
|
|
6
|
+
remotePeerId: string
|
|
7
|
+
) {
|
|
8
|
+
this.store
|
|
9
|
+
.get(message.key)
|
|
10
|
+
.then((data) => {
|
|
11
|
+
try {
|
|
12
|
+
// Use the id of the get so the other client knows we are replying
|
|
13
|
+
const oldData = {
|
|
14
|
+
type: "crdtPut",
|
|
15
|
+
data: JSON.parse(data),
|
|
16
|
+
to: [],
|
|
17
|
+
id: message.id,
|
|
18
|
+
} as CrdtPutMessage;
|
|
19
|
+
this.network.sendToClientId(remotePeerId, oldData);
|
|
20
|
+
} catch (e) {
|
|
21
|
+
// socket.send(data);
|
|
22
|
+
// do nothing
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
.catch((e) => {
|
|
26
|
+
this.logger("Local key not found, relay", JSON.stringify(message));
|
|
27
|
+
this.network.sendToAll(message, false);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ToolDb,
|
|
3
|
+
VerifyResult,
|
|
4
|
+
CrdtPutMessage,
|
|
5
|
+
VerificationData,
|
|
6
|
+
MapCrdt,
|
|
7
|
+
MapChanges,
|
|
8
|
+
CRDT_COUNTER,
|
|
9
|
+
CRDT_LIST,
|
|
10
|
+
CRDT_MAP,
|
|
11
|
+
ListCrdt,
|
|
12
|
+
ListChanges,
|
|
13
|
+
CounterCrdt,
|
|
14
|
+
CounterChanges,
|
|
15
|
+
} from "..";
|
|
16
|
+
|
|
17
|
+
import toolDbVerificationWrapper from "../toolDbVerificationWrapper";
|
|
18
|
+
|
|
19
|
+
export default function handleCrdtPut(
|
|
20
|
+
this: ToolDb,
|
|
21
|
+
message: CrdtPutMessage,
|
|
22
|
+
remotePeerId: string
|
|
23
|
+
) {
|
|
24
|
+
toolDbVerificationWrapper.call(this, message.data).then((value) => {
|
|
25
|
+
// this.logger("Verification wrapper result: ", value, message.k);
|
|
26
|
+
if (value === VerifyResult.Verified) {
|
|
27
|
+
this.emit("crdtput", message);
|
|
28
|
+
this.emit("data", message.data);
|
|
29
|
+
this.emit("verified", message);
|
|
30
|
+
// relay to other servers !!!
|
|
31
|
+
this.network.sendToAll(message, true);
|
|
32
|
+
|
|
33
|
+
this.store
|
|
34
|
+
.get(message.data.k)
|
|
35
|
+
.then((oldData) => {
|
|
36
|
+
try {
|
|
37
|
+
const parsedOldData: VerificationData<any> = JSON.parse(oldData);
|
|
38
|
+
|
|
39
|
+
let newMessage = message;
|
|
40
|
+
|
|
41
|
+
// Merge old document with new data incoming and save it
|
|
42
|
+
// Add handles for all kinds of CRDT we add
|
|
43
|
+
let oldDoc:
|
|
44
|
+
| MapCrdt<any>
|
|
45
|
+
| ListCrdt<any>
|
|
46
|
+
| CounterCrdt<any>
|
|
47
|
+
| undefined;
|
|
48
|
+
|
|
49
|
+
if (parsedOldData.c === CRDT_MAP) {
|
|
50
|
+
oldDoc = new MapCrdt(
|
|
51
|
+
this.userAccount.getAddress() || "",
|
|
52
|
+
parsedOldData.v
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (parsedOldData.c === CRDT_LIST) {
|
|
57
|
+
oldDoc = new ListCrdt(
|
|
58
|
+
this.userAccount.getAddress() || "",
|
|
59
|
+
parsedOldData.v
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (parsedOldData.c === CRDT_COUNTER) {
|
|
64
|
+
oldDoc = new CounterCrdt(
|
|
65
|
+
this.userAccount.getAddress() || "",
|
|
66
|
+
parsedOldData.v
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let changesMerged:
|
|
71
|
+
| MapChanges<any>[]
|
|
72
|
+
| ListChanges<any>[]
|
|
73
|
+
| CounterChanges[] = [];
|
|
74
|
+
|
|
75
|
+
if (oldDoc) {
|
|
76
|
+
oldDoc.mergeChanges(message.data.v);
|
|
77
|
+
changesMerged = oldDoc.getChanges();
|
|
78
|
+
}
|
|
79
|
+
newMessage = {
|
|
80
|
+
...message,
|
|
81
|
+
};
|
|
82
|
+
newMessage.data.v = changesMerged;
|
|
83
|
+
|
|
84
|
+
if (parsedOldData.t < message.data.t) {
|
|
85
|
+
const key = newMessage.data.k;
|
|
86
|
+
this.triggerKeyListener(key, newMessage.data);
|
|
87
|
+
this.store
|
|
88
|
+
.put(newMessage.data.k, JSON.stringify(newMessage.data))
|
|
89
|
+
.catch((e) => {
|
|
90
|
+
// do nothing
|
|
91
|
+
});
|
|
92
|
+
} else {
|
|
93
|
+
const key = message.data.k;
|
|
94
|
+
this.triggerKeyListener(key, parsedOldData);
|
|
95
|
+
}
|
|
96
|
+
// } else {
|
|
97
|
+
// this.logger(
|
|
98
|
+
// `${message.k} has old data, but its newer. old ${parsedOldData.t} < new ${message.t}`
|
|
99
|
+
// );
|
|
100
|
+
// }
|
|
101
|
+
} catch (e) {
|
|
102
|
+
this.logger("Couldnt parse crdt data", oldData, e);
|
|
103
|
+
}
|
|
104
|
+
})
|
|
105
|
+
.catch((e) => {
|
|
106
|
+
const key = message.data.k;
|
|
107
|
+
this.triggerKeyListener(key, message.data);
|
|
108
|
+
this.store
|
|
109
|
+
.put(message.data.k, JSON.stringify(message.data))
|
|
110
|
+
.catch((e) => {
|
|
111
|
+
// do nothing
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
} else {
|
|
115
|
+
this.logger("unverified message: ", value, message);
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ToolDb, GetMessage, PutMessage } from "..";
|
|
2
|
+
|
|
3
|
+
export default function handleGet(
|
|
4
|
+
this: ToolDb,
|
|
5
|
+
message: GetMessage,
|
|
6
|
+
remotePeerId: string
|
|
7
|
+
) {
|
|
8
|
+
this.store
|
|
9
|
+
.get(message.key)
|
|
10
|
+
.then((data) => {
|
|
11
|
+
try {
|
|
12
|
+
// Use the id of the get so the other client knows we are replying
|
|
13
|
+
const oldData = {
|
|
14
|
+
type: "put",
|
|
15
|
+
data: JSON.parse(data),
|
|
16
|
+
to: [],
|
|
17
|
+
id: message.id,
|
|
18
|
+
} as PutMessage;
|
|
19
|
+
this.network.sendToClientId(remotePeerId, oldData);
|
|
20
|
+
} catch (e) {
|
|
21
|
+
// socket.send(data);
|
|
22
|
+
// do nothing
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
.catch((e) => {
|
|
26
|
+
this.logger("Local key not found, relay", JSON.stringify(message));
|
|
27
|
+
this.network.sendToAll(message, false);
|
|
28
|
+
});
|
|
29
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ToolDb, verifyPeer, PingMessage, PongMessage } from "..";
|
|
2
|
+
|
|
3
|
+
export default function handlePing(
|
|
4
|
+
this: ToolDb,
|
|
5
|
+
message: PingMessage,
|
|
6
|
+
remotePeerId: string
|
|
7
|
+
) {
|
|
8
|
+
if (!this.isConnected) {
|
|
9
|
+
this.isConnected = true;
|
|
10
|
+
this.onConnect();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
verifyPeer(this, message.peer).then((verified) => {
|
|
14
|
+
// Verify integrity and topic
|
|
15
|
+
if (verified && message.peer.topic === this.options.topic) {
|
|
16
|
+
// Add this peer to our list of peers
|
|
17
|
+
const filteredPeers = this.serverPeers.filter(
|
|
18
|
+
(p) => p.address === message.peer.address
|
|
19
|
+
);
|
|
20
|
+
if (filteredPeers.length === 0 && message.isServer) {
|
|
21
|
+
// Add this peer to the list
|
|
22
|
+
this.serverPeers.push(message.peer);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
this.network.sendToClientId(remotePeerId, {
|
|
26
|
+
type: "pong",
|
|
27
|
+
isServer: this.options.server,
|
|
28
|
+
clientId: this.network.getClientAddress(),
|
|
29
|
+
to: [],
|
|
30
|
+
servers: this.serverPeers,
|
|
31
|
+
id: message.id,
|
|
32
|
+
} as PongMessage);
|
|
33
|
+
|
|
34
|
+
this.onPeerConnect(this.peerAccount.getAddress() || "");
|
|
35
|
+
} else {
|
|
36
|
+
this.logger("Blocked a remote peer from joining; ", message);
|
|
37
|
+
// Drop connection here!
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { ToolDb, verifyPeer, PongMessage } from "..";
|
|
2
|
+
|
|
3
|
+
export default function handlePong(
|
|
4
|
+
this: ToolDb,
|
|
5
|
+
message: PongMessage,
|
|
6
|
+
remotePeerId: string
|
|
7
|
+
) {
|
|
8
|
+
if (!this.isConnected) {
|
|
9
|
+
this.isConnected = true;
|
|
10
|
+
this.onConnect();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
message.servers.forEach((peer) => {
|
|
14
|
+
verifyPeer(this, peer).then((verified) => {
|
|
15
|
+
// Verify integrity and topic
|
|
16
|
+
if (verified && peer.topic === this.options.topic) {
|
|
17
|
+
// Add this peer to our list of peers
|
|
18
|
+
const filteredPeers = this.serverPeers.filter(
|
|
19
|
+
(p) => p.address === peer.address
|
|
20
|
+
);
|
|
21
|
+
if (filteredPeers.length === 0 && peer.host && peer.port) {
|
|
22
|
+
// Add this peer to the list
|
|
23
|
+
this.serverPeers.push(peer);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
this.onPeerConnect(this.peerAccount.getAddress() || "");
|
|
30
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ToolDb, PutMessage, VerifyResult } from "..";
|
|
2
|
+
|
|
3
|
+
import toolDbVerificationWrapper from "../toolDbVerificationWrapper";
|
|
4
|
+
|
|
5
|
+
export default function handlePut(
|
|
6
|
+
this: ToolDb,
|
|
7
|
+
message: PutMessage,
|
|
8
|
+
remotePeerId: string
|
|
9
|
+
) {
|
|
10
|
+
toolDbVerificationWrapper.call(this, message.data).then((value) => {
|
|
11
|
+
// this.logger("Verification wrapper result: ", value, message.k);
|
|
12
|
+
if (value === VerifyResult.Verified) {
|
|
13
|
+
this.emit("put", message);
|
|
14
|
+
this.emit("data", message.data);
|
|
15
|
+
this.emit("verified", message);
|
|
16
|
+
// relay to other servers !!!
|
|
17
|
+
this.network.sendToAll(message, true);
|
|
18
|
+
|
|
19
|
+
this.store
|
|
20
|
+
.get(message.data.k)
|
|
21
|
+
.then((oldData) => {
|
|
22
|
+
const parsedOldData = JSON.parse(oldData);
|
|
23
|
+
if (parsedOldData.t < message.data.t) {
|
|
24
|
+
const key = message.data.k;
|
|
25
|
+
this.triggerKeyListener(key, message.data);
|
|
26
|
+
this.store
|
|
27
|
+
.put(message.data.k, JSON.stringify(message.data))
|
|
28
|
+
.catch((e) => {
|
|
29
|
+
// do nothing
|
|
30
|
+
});
|
|
31
|
+
} else {
|
|
32
|
+
const key = message.data.k;
|
|
33
|
+
this.triggerKeyListener(key, parsedOldData);
|
|
34
|
+
}
|
|
35
|
+
// } else {
|
|
36
|
+
// this.logger(
|
|
37
|
+
// `${message.k} has old data, but its newer. old ${parsedOldData.t} < new ${message.t}`
|
|
38
|
+
// );
|
|
39
|
+
// }
|
|
40
|
+
})
|
|
41
|
+
.catch((e) => {
|
|
42
|
+
const key = message.data.k;
|
|
43
|
+
this.triggerKeyListener(key, message.data);
|
|
44
|
+
this.store
|
|
45
|
+
.put(message.data.k, JSON.stringify(message.data))
|
|
46
|
+
.catch((e) => {
|
|
47
|
+
//
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
} else {
|
|
51
|
+
this.logger("unverified message: ", value, message);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ToolDb, QueryAckMessage, QueryMessage } from "..";
|
|
2
|
+
|
|
3
|
+
export default function handleQuery(
|
|
4
|
+
this: ToolDb,
|
|
5
|
+
message: QueryMessage,
|
|
6
|
+
remotePeerId: string
|
|
7
|
+
) {
|
|
8
|
+
this.store
|
|
9
|
+
.query(message.key)
|
|
10
|
+
.then((keys) => {
|
|
11
|
+
this.network.sendToClientId(remotePeerId, {
|
|
12
|
+
type: "queryAck",
|
|
13
|
+
id: message.id,
|
|
14
|
+
to: [],
|
|
15
|
+
keys,
|
|
16
|
+
} as QueryAckMessage);
|
|
17
|
+
})
|
|
18
|
+
.catch((e) => {
|
|
19
|
+
// do nothing
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
if (this.options.server) {
|
|
23
|
+
this.network.sendToAll(message, true);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ToolDb, PutMessage, SubscribeMessage } from "..";
|
|
2
|
+
|
|
3
|
+
export default function handleSubscribe(
|
|
4
|
+
this: ToolDb,
|
|
5
|
+
message: SubscribeMessage,
|
|
6
|
+
remotePeerId: string
|
|
7
|
+
) {
|
|
8
|
+
const subId = remotePeerId + "-" + message.key;
|
|
9
|
+
if (!this.subscriptions.includes(subId)) {
|
|
10
|
+
this.subscriptions.push(subId);
|
|
11
|
+
|
|
12
|
+
this.addKeyListener(message.key, (msg) => {
|
|
13
|
+
if (remotePeerId) {
|
|
14
|
+
// We do not reply to the socket directly
|
|
15
|
+
// instead we use the client id, in case the socket reconnects
|
|
16
|
+
const newMsg: PutMessage = {
|
|
17
|
+
data: msg,
|
|
18
|
+
id: message.id,
|
|
19
|
+
type: "put",
|
|
20
|
+
to: [],
|
|
21
|
+
};
|
|
22
|
+
this.network.sendToClientId(remotePeerId, newMsg);
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// basically the exact same as GET, below
|
|
28
|
+
this.store
|
|
29
|
+
.get(message.key)
|
|
30
|
+
.then((data) => {
|
|
31
|
+
try {
|
|
32
|
+
const oldData: PutMessage = {
|
|
33
|
+
data: JSON.parse(data),
|
|
34
|
+
id: message.id,
|
|
35
|
+
type: "put",
|
|
36
|
+
to: [],
|
|
37
|
+
};
|
|
38
|
+
this.network.sendToClientId(remotePeerId, oldData);
|
|
39
|
+
} catch (e) {
|
|
40
|
+
// do nothing
|
|
41
|
+
}
|
|
42
|
+
})
|
|
43
|
+
.catch((e) => {
|
|
44
|
+
// do nothing
|
|
45
|
+
});
|
|
46
|
+
}
|
package/lib/server.ts
ADDED
package/lib/shared.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { ToolDb, ToolDbMessage } from ".";
|
|
2
|
+
|
|
3
|
+
export default function toolDbClientOnMessage(
|
|
4
|
+
this: ToolDb,
|
|
5
|
+
message: ToolDbMessage,
|
|
6
|
+
remotePeerId: string
|
|
7
|
+
) {
|
|
8
|
+
if (!this.processedIds[message.type]) {
|
|
9
|
+
this.processedIds[message.type] = [];
|
|
10
|
+
}
|
|
11
|
+
if (this.processedIds[message.type].includes(message.id)) {
|
|
12
|
+
// this.logger(
|
|
13
|
+
// `Already processed this message > ${message.type} from ${remotePeerId}`
|
|
14
|
+
// );
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
this.emit("message", message, remotePeerId);
|
|
19
|
+
|
|
20
|
+
this.processedIds[message.type].push(message.id);
|
|
21
|
+
|
|
22
|
+
this.logger(`Got message ${message.type} from ${remotePeerId}`);
|
|
23
|
+
this.logger(message);
|
|
24
|
+
|
|
25
|
+
// Check if we are listening for this ID
|
|
26
|
+
if (message.id) {
|
|
27
|
+
const msgId = message.id;
|
|
28
|
+
if (this._idListeners[msgId]) {
|
|
29
|
+
this._idListeners[msgId](message);
|
|
30
|
+
this.removeIdListener(msgId);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (message === undefined || message.type === undefined) {
|
|
35
|
+
this.logger("Message is invalid!", message, typeof message);
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
switch (message.type) {
|
|
40
|
+
case "ping":
|
|
41
|
+
this.handlePing(message, remotePeerId);
|
|
42
|
+
break;
|
|
43
|
+
|
|
44
|
+
case "pong":
|
|
45
|
+
this.handlePong(message, remotePeerId);
|
|
46
|
+
break;
|
|
47
|
+
|
|
48
|
+
case "subscribe":
|
|
49
|
+
this.handleSubscribe(message, remotePeerId);
|
|
50
|
+
break;
|
|
51
|
+
|
|
52
|
+
case "get":
|
|
53
|
+
this.handleGet(message, remotePeerId);
|
|
54
|
+
break;
|
|
55
|
+
|
|
56
|
+
case "put":
|
|
57
|
+
this.handlePut(message, remotePeerId);
|
|
58
|
+
break;
|
|
59
|
+
|
|
60
|
+
case "crdtPut":
|
|
61
|
+
this.handleCrdtPut(message, remotePeerId);
|
|
62
|
+
break;
|
|
63
|
+
|
|
64
|
+
case "crdtGet":
|
|
65
|
+
this.handleCrdtGet(message, remotePeerId);
|
|
66
|
+
break;
|
|
67
|
+
|
|
68
|
+
case "query":
|
|
69
|
+
this.handleQuery(message, remotePeerId);
|
|
70
|
+
break;
|
|
71
|
+
|
|
72
|
+
default:
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|