@leofcoin/peernet 0.10.6 → 0.11.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/LICENSE +1 -1
- package/README.md +49 -49
- package/dist/browser/peernet.js +101814 -92947
- package/dist/commonjs/client-bd0caeb7.js +324 -0
- package/dist/commonjs/{codec-6367213c.js → codec-4a768e5e.js} +99 -89
- package/dist/commonjs/codec-format-interface.js +41 -24
- package/dist/commonjs/codec.js +3 -3
- package/dist/commonjs/dht-response.js +3 -3
- package/dist/commonjs/dht.js +23 -23
- package/dist/commonjs/hash.js +17 -7
- package/dist/commonjs/{http-42a6e555.js → http-2b0735ef.js} +19 -15
- package/dist/commonjs/peernet-message.js +12 -12
- package/dist/commonjs/peernet.js +1164 -966
- package/dist/commonjs/request.js +12 -12
- package/dist/commonjs/response.js +12 -12
- package/dist/module/peernet.js +1325 -1120
- package/index.html +4 -6
- package/package.json +31 -7
- package/rollup.config.js +33 -5
- package/rollup0.config.js +7 -0
- package/src/codec/codec-format-interface.js +40 -23
- package/src/codec/codec.js +21 -11
- package/src/codec/codecs.js +79 -79
- package/src/handlers/message.js +52 -52
- package/src/hash/hash.js +16 -6
- package/src/http/client/http-client.js +1 -1
- package/src/messages/chat-message.js +14 -14
- package/src/messages/data-response.js +14 -14
- package/src/messages/data.js +18 -18
- package/src/messages/dht-response.js +0 -1
- package/src/messages/dht.js +25 -25
- package/src/messages/peer-response.js +14 -14
- package/src/messages/peer.js +14 -14
- package/src/messages/peernet-message.js +14 -14
- package/src/messages/ps.js +14 -14
- package/src/messages/request.js +14 -14
- package/src/messages/response.js +14 -14
- package/src/peernet.js +31 -116
- package/test/codec.js +3 -2
- package/test/messages.js +7 -4
- package/test.js +11 -4
- package/webpack.config.js +35 -0
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -79
- package/coverage/lcov-report/codec-format-interface.js.html +0 -533
- package/coverage/lcov-report/codec.js.html +0 -677
- package/coverage/lcov-report/dht-response.js.html +0 -188
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/hash.js.html +0 -551
- package/coverage/lcov-report/index.html +0 -156
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -170
- package/coverage/lcov.info +0 -459
- package/debug.log +0 -3
- package/dist/browser/peernet.js.tmp-browserify-14074318104595318069 +0 -0
- package/dist/browser/peernet.js.tmp-browserify-45407634493269122267 +0 -0
- package/dist/browser/peernet.js.tmp-browserify-53722389064799025427 +0 -0
- package/dist/browser/peernet.js.tmp-browserify-96323030449218949300 +0 -0
- package/dist/codec/codec-format-interface.js +0 -433
- package/dist/codec/codec.js +0 -199
- package/dist/commonjs/codec-73adfc0f.js +0 -205
- package/dist/commonjs/http-2c603501.js +0 -324
- package/dist/commonjs/http-43f4fafe.js +0 -324
- package/dist/commonjs/http-a94c5a81.js +0 -324
- package/dist/commonjs/peernet-message-b6925673.js +0 -32
- package/dist/hash/hash.js +0 -340
- package/dist/messages/dht-response.js +0 -454
- package/dist/messages/dht.js +0 -453
- package/dist/messages/peernet.js +0 -456
- package/dist/module/http-273664bd.js +0 -317
- package/dist/module/http-8fe3c0d7.js +0 -317
- package/dist/module/http-c780c991.js +0 -317
- package/dist/module/http-f13e0d77.js +0 -317
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
|
-
import proto from './../proto/chat-message.proto.js'
|
|
3
|
-
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
-
|
|
5
|
-
export default class ChatMessage extends CodecFormat {
|
|
6
|
-
get keys() {
|
|
7
|
-
return ['author', 'value', 'timestamp', 'files']
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
constructor(buffer) {
|
|
11
|
-
const name = 'chat-message'
|
|
12
|
-
super(buffer, protons(proto).ChatMessage, {name})
|
|
13
|
-
}
|
|
14
|
-
}
|
|
1
|
+
import protons from 'protons'
|
|
2
|
+
import proto from './../proto/chat-message.proto.js'
|
|
3
|
+
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
+
|
|
5
|
+
export default class ChatMessage extends CodecFormat {
|
|
6
|
+
get keys() {
|
|
7
|
+
return ['author', 'value', 'timestamp', 'files']
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
constructor(buffer) {
|
|
11
|
+
const name = 'chat-message'
|
|
12
|
+
super(buffer, protons(proto).ChatMessage, {name})
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
|
-
import proto from './../proto/data-response.proto.js'
|
|
3
|
-
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
-
|
|
5
|
-
export default class DataMessageResponse extends CodecFormat {
|
|
6
|
-
get keys() {
|
|
7
|
-
return ['hash', 'data']
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
constructor(data) {
|
|
11
|
-
const name = 'peernet-data-response'
|
|
12
|
-
super(data, protons(proto).PeernetDataMessageResponse, {name})
|
|
13
|
-
}
|
|
14
|
-
}
|
|
1
|
+
import protons from 'protons'
|
|
2
|
+
import proto from './../proto/data-response.proto.js'
|
|
3
|
+
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
+
|
|
5
|
+
export default class DataMessageResponse extends CodecFormat {
|
|
6
|
+
get keys() {
|
|
7
|
+
return ['hash', 'data']
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
constructor(data) {
|
|
11
|
+
const name = 'peernet-data-response'
|
|
12
|
+
super(data, protons(proto).PeernetDataMessageResponse, {name})
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/messages/data.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
|
-
import proto from './../proto/data.proto.js'
|
|
3
|
-
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @extends {CodecFormat}
|
|
7
|
-
*/
|
|
8
|
-
export default class DataMessage extends CodecFormat {
|
|
9
|
-
get keys() {
|
|
10
|
-
return ['hash', 'store']
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
|
|
14
|
-
*/
|
|
15
|
-
constructor(data) {
|
|
16
|
-
super(data, protons(proto).PeernetDataMessage, {name: 'peernet-data'})
|
|
17
|
-
}
|
|
18
|
-
}
|
|
1
|
+
import protons from 'protons'
|
|
2
|
+
import proto from './../proto/data.proto.js'
|
|
3
|
+
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @extends {CodecFormat}
|
|
7
|
+
*/
|
|
8
|
+
export default class DataMessage extends CodecFormat {
|
|
9
|
+
get keys() {
|
|
10
|
+
return ['hash', 'store']
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* @param {Buffer|String|Object|DataMessage} data - The data needed to create the DataMessage
|
|
14
|
+
*/
|
|
15
|
+
constructor(data) {
|
|
16
|
+
super(data, protons(proto).PeernetDataMessage, {name: 'peernet-data'})
|
|
17
|
+
}
|
|
18
|
+
}
|
package/src/messages/dht.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
|
-
import proto from './../proto/dht.proto.js'
|
|
3
|
-
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @example `
|
|
7
|
-
new DHTMessage(hash, store)
|
|
8
|
-
// store = optional if not set, peernet checks every store
|
|
9
|
-
let message = new DHTMessage('hashmvbs124xcfd...', 'transaction')
|
|
10
|
-
message = new DHTMessage('hashmvbs124xcfd...', 'block')
|
|
11
|
-
`
|
|
12
|
-
*/
|
|
13
|
-
export default class DHTMessage extends CodecFormat {
|
|
14
|
-
/**
|
|
15
|
-
*
|
|
16
|
-
*/
|
|
17
|
-
get keys() {
|
|
18
|
-
return ['hash', 'store']
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
constructor(data) {
|
|
22
|
-
const name = 'peernet-dht'
|
|
23
|
-
super(data, protons(proto).PeernetDHTMessage, {name})
|
|
24
|
-
}
|
|
25
|
-
}
|
|
1
|
+
import protons from 'protons'
|
|
2
|
+
import proto from './../proto/dht.proto.js'
|
|
3
|
+
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @example `
|
|
7
|
+
new DHTMessage(hash, store)
|
|
8
|
+
// store = optional if not set, peernet checks every store
|
|
9
|
+
let message = new DHTMessage('hashmvbs124xcfd...', 'transaction')
|
|
10
|
+
message = new DHTMessage('hashmvbs124xcfd...', 'block')
|
|
11
|
+
`
|
|
12
|
+
*/
|
|
13
|
+
export default class DHTMessage extends CodecFormat {
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
get keys() {
|
|
18
|
+
return ['hash', 'store']
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
constructor(data) {
|
|
22
|
+
const name = 'peernet-dht'
|
|
23
|
+
super(data, protons(proto).PeernetDHTMessage, {name})
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
|
-
import proto from './../proto/peer-response.proto.js'
|
|
3
|
-
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
-
|
|
5
|
-
export default class PeerMessageResponse extends CodecFormat {
|
|
6
|
-
get keys() {
|
|
7
|
-
return ['id']
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
constructor(data) {
|
|
11
|
-
const name = 'peernet-peer-response'
|
|
12
|
-
super(data, protons(proto).PeernetPeerMessageResponse, {name})
|
|
13
|
-
}
|
|
14
|
-
}
|
|
1
|
+
import protons from 'protons'
|
|
2
|
+
import proto from './../proto/peer-response.proto.js'
|
|
3
|
+
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
+
|
|
5
|
+
export default class PeerMessageResponse extends CodecFormat {
|
|
6
|
+
get keys() {
|
|
7
|
+
return ['id']
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
constructor(data) {
|
|
11
|
+
const name = 'peernet-peer-response'
|
|
12
|
+
super(data, protons(proto).PeernetPeerMessageResponse, {name})
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/messages/peer.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
|
-
import proto from './../proto/peer.proto.js'
|
|
3
|
-
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
-
|
|
5
|
-
export default class PeerMessage extends CodecFormat {
|
|
6
|
-
get keys() {
|
|
7
|
-
return ['id']
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
constructor(data) {
|
|
11
|
-
const name = 'peernet-peer'
|
|
12
|
-
super(data, protons(proto).PeernetPeerMessage, {name})
|
|
13
|
-
}
|
|
14
|
-
}
|
|
1
|
+
import protons from 'protons'
|
|
2
|
+
import proto from './../proto/peer.proto.js'
|
|
3
|
+
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
+
|
|
5
|
+
export default class PeerMessage extends CodecFormat {
|
|
6
|
+
get keys() {
|
|
7
|
+
return ['id']
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
constructor(data) {
|
|
11
|
+
const name = 'peernet-peer'
|
|
12
|
+
super(data, protons(proto).PeernetPeerMessage, {name})
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
|
-
import proto from './../proto/peernet.proto.js'
|
|
3
|
-
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
-
|
|
5
|
-
export default class PeernetMessage extends CodecFormat {
|
|
6
|
-
get keys() {
|
|
7
|
-
return ['data', 'signature', 'from', 'to', 'id']
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
constructor(buffer) {
|
|
11
|
-
const name = 'peernet-message'
|
|
12
|
-
super(buffer, protons(proto).PeernetMessage, {name})
|
|
13
|
-
}
|
|
14
|
-
}
|
|
1
|
+
import protons from 'protons'
|
|
2
|
+
import proto from './../proto/peernet.proto.js'
|
|
3
|
+
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
+
|
|
5
|
+
export default class PeernetMessage extends CodecFormat {
|
|
6
|
+
get keys() {
|
|
7
|
+
return ['data', 'signature', 'from', 'to', 'id']
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
constructor(buffer) {
|
|
11
|
+
const name = 'peernet-message'
|
|
12
|
+
super(buffer, protons(proto).PeernetMessage, {name})
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/messages/ps.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
|
-
import proto from './../proto/ps.proto.js'
|
|
3
|
-
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
-
|
|
5
|
-
export default class PsMessage extends CodecFormat {
|
|
6
|
-
get keys() {
|
|
7
|
-
return ['data', 'topic']
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
constructor(buffer) {
|
|
11
|
-
const name = 'peernet-ps'
|
|
12
|
-
super(buffer, protons(proto).PsMessage, {name})
|
|
13
|
-
}
|
|
14
|
-
}
|
|
1
|
+
import protons from 'protons'
|
|
2
|
+
import proto from './../proto/ps.proto.js'
|
|
3
|
+
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
+
|
|
5
|
+
export default class PsMessage extends CodecFormat {
|
|
6
|
+
get keys() {
|
|
7
|
+
return ['data', 'topic']
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
constructor(buffer) {
|
|
11
|
+
const name = 'peernet-ps'
|
|
12
|
+
super(buffer, protons(proto).PsMessage, {name})
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/messages/request.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
|
-
import proto from './../proto/request.proto.js'
|
|
3
|
-
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
-
|
|
5
|
-
export default class RequestMessage extends CodecFormat {
|
|
6
|
-
get keys() {
|
|
7
|
-
return ['request']
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
constructor(data) {
|
|
11
|
-
const name = 'peernet-request'
|
|
12
|
-
super(data, protons(proto).PeernetRequestMessage, {name})
|
|
13
|
-
}
|
|
14
|
-
}
|
|
1
|
+
import protons from 'protons'
|
|
2
|
+
import proto from './../proto/request.proto.js'
|
|
3
|
+
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
+
|
|
5
|
+
export default class RequestMessage extends CodecFormat {
|
|
6
|
+
get keys() {
|
|
7
|
+
return ['request']
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
constructor(data) {
|
|
11
|
+
const name = 'peernet-request'
|
|
12
|
+
super(data, protons(proto).PeernetRequestMessage, {name})
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/messages/response.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
|
-
import proto from './../proto/response.proto.js'
|
|
3
|
-
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
-
|
|
5
|
-
export default class ResponseMessage extends CodecFormat {
|
|
6
|
-
get keys() {
|
|
7
|
-
return ['response']
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
constructor(data) {
|
|
11
|
-
const name = 'peernet-response'
|
|
12
|
-
super(data, protons(proto).PeernetResponseMessage, {name})
|
|
13
|
-
}
|
|
14
|
-
}
|
|
1
|
+
import protons from 'protons'
|
|
2
|
+
import proto from './../proto/response.proto.js'
|
|
3
|
+
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
+
|
|
5
|
+
export default class ResponseMessage extends CodecFormat {
|
|
6
|
+
get keys() {
|
|
7
|
+
return ['response']
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
constructor(data) {
|
|
11
|
+
const name = 'peernet-response'
|
|
12
|
+
super(data, protons(proto).PeernetResponseMessage, {name})
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/peernet.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import Client from './client'
|
|
1
|
+
import Client from './../node_modules/@leofcoin/peernet-swarm/dist/es/client.js'
|
|
3
2
|
import LeofcoinStorage from '@leofcoin/storage'
|
|
4
|
-
import http from './http/http.js'
|
|
5
|
-
import httpClient from './http/client/client.js'
|
|
6
3
|
import LeofcoinStorageClient from './http/client/storage.js'
|
|
7
4
|
import PeernetMessage from './messages/peernet-message.js'
|
|
8
5
|
import DHTMessage from './messages/dht.js'
|
|
@@ -20,14 +17,13 @@ import DHT from './dht/dht.js'
|
|
|
20
17
|
import Hash from './hash/hash'
|
|
21
18
|
import codecs from './codec/codecs'
|
|
22
19
|
import { debug, protoFor, target } from './utils/utils.js'
|
|
23
|
-
import generateAccount from
|
|
24
|
-
'./../node_modules/@leofcoin/generate-account/dist/module/generate-account.js'
|
|
20
|
+
import generateAccount from '@leofcoin/generate-account'
|
|
25
21
|
import MessageHandler from './handlers/message.js'
|
|
26
22
|
import { encapsulatedError, dhtError,
|
|
27
23
|
nothingFoundError } from './errors/errors.js'
|
|
28
24
|
|
|
29
25
|
globalThis.leofcoin = globalThis.leofcoin || {}
|
|
30
|
-
globalThis.globalSub = globalThis.globalSub || new
|
|
26
|
+
globalThis.globalSub = globalThis.globalSub || new PubSub({verbose: true})
|
|
31
27
|
|
|
32
28
|
/**
|
|
33
29
|
* @access public
|
|
@@ -171,13 +167,7 @@ export default class Peernet {
|
|
|
171
167
|
const {daemon, environment} = await target()
|
|
172
168
|
this.hasDaemon = daemon
|
|
173
169
|
|
|
174
|
-
|
|
175
|
-
globalThis.peernet.client = await httpClient({
|
|
176
|
-
protocol: 'peernet-v0.1.0', host: '127.0.0.1', port: options.port,
|
|
177
|
-
})
|
|
178
|
-
} else {
|
|
179
|
-
if (environment !== 'browser') http(options)
|
|
180
|
-
}
|
|
170
|
+
HTTP_IMPORT
|
|
181
171
|
|
|
182
172
|
for (const store of this.defaultStores) {
|
|
183
173
|
await this.addStore(store, options.storePrefix, options.root)
|
|
@@ -203,42 +193,11 @@ export default class Peernet {
|
|
|
203
193
|
}
|
|
204
194
|
}
|
|
205
195
|
this._peerHandler = new PeerDiscovery(this.id)
|
|
206
|
-
|
|
207
|
-
const id = Buffer.from(this.id.slice(0, 32))
|
|
208
|
-
this.peerId = id
|
|
209
|
-
|
|
210
|
-
pubsub.subscribe('peer:discovered', async (peer) => {
|
|
211
|
-
peer.on('peernet.data', async (message) => {
|
|
212
|
-
const id = message.id;
|
|
213
|
-
message = new PeernetMessage(Buffer.from(message.data.data));
|
|
214
|
-
const proto = protoFor(message.decoded.data);
|
|
215
|
-
await this._protoHandler({id, proto}, peer);
|
|
216
|
-
});
|
|
217
|
-
await this._peerHandler.discover(peer);
|
|
218
|
-
const fulldId = this._getPeerId(peer.id);
|
|
219
|
-
if (fulldId && this._discovered.indexOf(peer.id) === -1) {
|
|
220
|
-
this._discovered.push(peer.id);
|
|
221
|
-
pubsub.publish('peer:connected', peer);
|
|
222
|
-
}
|
|
223
|
-
});
|
|
224
|
-
pubsub.subscribe('peer:disconnected', async (peer) => {
|
|
225
|
-
let index = this._discovered.indexOf(peer.id)
|
|
226
|
-
if (index !== -1) this._discovered.splice(index, 1)
|
|
227
|
-
const id = this._getPeerId(peer.id)
|
|
228
|
-
let peerIds = this.peerMap.get(id)
|
|
229
|
-
|
|
230
|
-
if (peerIds) {
|
|
231
|
-
index = peerIds.indexOf(peer.id)
|
|
232
|
-
if (index !== -1) peerIds.splice(index, 1)
|
|
233
|
-
} else {
|
|
234
|
-
peerIds = []
|
|
235
|
-
}
|
|
196
|
+
this.peerId = this.id
|
|
236
197
|
|
|
237
|
-
if (peerIds.length === 0) this.peerMap.delete(id)
|
|
238
|
-
else this.peerMap.set(id, peerIds)
|
|
239
|
-
})
|
|
240
198
|
pubsub.subscribe('peer:connected', async (peer) => {
|
|
241
|
-
console.log(
|
|
199
|
+
console.log(peer);
|
|
200
|
+
// console.log({connected: peer.id, as: this._getPeerId(peer.id) });
|
|
242
201
|
// peer.on('peernet.data', async (message) => {
|
|
243
202
|
// const id = message.id
|
|
244
203
|
// message = new PeernetMessage(Buffer.from(message.data.data))
|
|
@@ -247,11 +206,25 @@ export default class Peernet {
|
|
|
247
206
|
// })
|
|
248
207
|
})
|
|
249
208
|
|
|
209
|
+
pubsub.subscribe('peer:data', async message => {
|
|
210
|
+
if (!message.data) return
|
|
211
|
+
const {id, data} = JSON.parse(new TextDecoder().decode(message.data))
|
|
212
|
+
const uint8Array = new Uint8Array(Object.keys(data).length)
|
|
213
|
+
for (var i = 0; i < Object.keys(data).length; i++) {
|
|
214
|
+
uint8Array[i] = data[i]
|
|
215
|
+
}
|
|
216
|
+
message = new PeernetMessage(uint8Array)
|
|
217
|
+
const proto = protoFor(message.decoded.data)
|
|
218
|
+
|
|
219
|
+
const from = new TextDecoder().decode(message.decoded.from)
|
|
220
|
+
this._protoHandler({id, proto}, this.client.connections[from], from)
|
|
221
|
+
})
|
|
222
|
+
|
|
250
223
|
/**
|
|
251
224
|
* @access public
|
|
252
225
|
* @type {PeernetClient}
|
|
253
226
|
*/
|
|
254
|
-
this.client = new Client(
|
|
227
|
+
this.client = new Client(this.id)
|
|
255
228
|
if (globalThis.onbeforeunload) {
|
|
256
229
|
globalThis.addEventListener('beforeunload', async () => this.client.close());
|
|
257
230
|
}
|
|
@@ -276,55 +249,10 @@ export default class Peernet {
|
|
|
276
249
|
* @param {Buffer} message - peernet message
|
|
277
250
|
* @param {PeernetPeer} peer - peernet peer
|
|
278
251
|
*/
|
|
279
|
-
async _protoHandler(message, peer) {
|
|
252
|
+
async _protoHandler(message, peer, from) {
|
|
280
253
|
const {id, proto} = message
|
|
281
254
|
this.bw.down += proto.encoded.length
|
|
282
|
-
|
|
283
|
-
const from = proto.decoded.id
|
|
284
|
-
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
|
|
285
|
-
else {
|
|
286
|
-
const connections = this.peerMap.get(from)
|
|
287
|
-
if (connections.indexOf(peer.id) === -1) {
|
|
288
|
-
connections.push(peer.id)
|
|
289
|
-
this.peerMap.set(from, connections)
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
const data = new PeerMessageResponse({id: this.id})
|
|
293
|
-
const node = await this.prepareMessage(from, data.encoded)
|
|
294
|
-
|
|
295
|
-
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
|
|
296
|
-
this.bw.up += node.encoded.length
|
|
297
|
-
} else if (proto.name === 'peernet-peer-response') {
|
|
298
|
-
const from = proto.decoded.id
|
|
299
|
-
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
|
|
300
|
-
else {
|
|
301
|
-
const connections = this.peerMap.get(from)
|
|
302
|
-
if (connections.indexOf(peer.id) === -1) {
|
|
303
|
-
connections.push(peer.id)
|
|
304
|
-
this.peerMap.set(from, connections)
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
} else {
|
|
308
|
-
let from = this._getPeerId(peer.id)
|
|
309
|
-
if (!from) {
|
|
310
|
-
const data = new PeerMessage({id: this.id})
|
|
311
|
-
const node = await this.prepareMessage(peer.id, data.encoded)
|
|
312
|
-
this.bw.up += node.encoded.length
|
|
313
|
-
let response = await peer.request(node.encoded)
|
|
314
|
-
response = protoFor(response)
|
|
315
|
-
|
|
316
|
-
response = new PeerMessageResponse(response.decoded.data)
|
|
317
|
-
|
|
318
|
-
from = response.decoded.id
|
|
319
|
-
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
|
|
320
|
-
else {
|
|
321
|
-
const connections = this.peerMap.get(from)
|
|
322
|
-
if (connections.indexOf(peer.id) === -1) {
|
|
323
|
-
connections.push(peer.id)
|
|
324
|
-
this.peerMap.set(from, connections)
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
}
|
|
255
|
+
|
|
328
256
|
if (proto.name === 'peernet-dht') {
|
|
329
257
|
let { hash, store } = proto.decoded
|
|
330
258
|
let has;
|
|
@@ -339,7 +267,7 @@ export default class Peernet {
|
|
|
339
267
|
const data = new DHTMessageResponse({hash, has})
|
|
340
268
|
const node = await this.prepareMessage(from, data.encoded)
|
|
341
269
|
|
|
342
|
-
peer.
|
|
270
|
+
peer.send(Buffer.from(JSON.stringify({id, data: node.encoded})))
|
|
343
271
|
this.bw.up += node.encoded.length
|
|
344
272
|
} else if (proto.name === 'peernet-data') {
|
|
345
273
|
let { hash, store } = proto.decoded
|
|
@@ -347,7 +275,7 @@ export default class Peernet {
|
|
|
347
275
|
if (!store) {
|
|
348
276
|
store = await this.whichStore([...this.stores], hash)
|
|
349
277
|
} else {
|
|
350
|
-
store = globalThis
|
|
278
|
+
store = globalThis[`${store}Store`]
|
|
351
279
|
}
|
|
352
280
|
if (store && !store.private) {
|
|
353
281
|
data = await store.get(hash)
|
|
@@ -356,26 +284,13 @@ export default class Peernet {
|
|
|
356
284
|
data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
|
|
357
285
|
|
|
358
286
|
const node = await this.prepareMessage(from, data.encoded)
|
|
359
|
-
peer.
|
|
287
|
+
peer.send(Buffer.from(JSON.stringify({id, data: node.encoded})))
|
|
360
288
|
this.bw.up += node.encoded.length
|
|
361
289
|
}
|
|
362
290
|
} else {
|
|
363
291
|
// ban (trying to access private st)
|
|
364
292
|
}
|
|
365
293
|
|
|
366
|
-
} else if (proto.name === 'peernet-peer') {
|
|
367
|
-
const from = proto.decoded.id
|
|
368
|
-
if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
|
|
369
|
-
else {
|
|
370
|
-
const connections = this.peerMap.get(from)
|
|
371
|
-
connections.push(peer.id)
|
|
372
|
-
this.peerMap.set(from, connections)
|
|
373
|
-
}
|
|
374
|
-
const data = new PeerMessage({id: this.id})
|
|
375
|
-
const node = await this.prepareMessage(from, data.encoded)
|
|
376
|
-
|
|
377
|
-
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
|
|
378
|
-
this.bw.up += node.encoded.length
|
|
379
294
|
} else if (proto.name === 'peernet-request') {
|
|
380
295
|
// TODO: make dynamic
|
|
381
296
|
// exposeddevapi[proto.decoded.request](proto.decoded.params)
|
|
@@ -383,14 +298,14 @@ export default class Peernet {
|
|
|
383
298
|
if (method) {
|
|
384
299
|
const data = await method()
|
|
385
300
|
const node = await this.prepareMessage(from, data.encoded)
|
|
386
|
-
peer.
|
|
301
|
+
peer.send(new TextEncoder().encode(JSON.stringify({id, data: node.encoded})))
|
|
387
302
|
this.bw.up += node.encoded.length
|
|
388
303
|
}
|
|
389
304
|
} else if (proto.name === 'peernet-ps' &&
|
|
390
305
|
this._getPeerId(peer.id) !== this.id.toString()) {
|
|
391
306
|
globalSub.publish(proto.decoded.topic.toString(), proto.decoded.data.toString())
|
|
392
307
|
}
|
|
393
|
-
}
|
|
308
|
+
// }
|
|
394
309
|
}
|
|
395
310
|
|
|
396
311
|
/**
|
|
@@ -505,7 +420,7 @@ export default class Peernet {
|
|
|
505
420
|
if (this._getPeerId(peer.id) === id) return peer
|
|
506
421
|
})
|
|
507
422
|
|
|
508
|
-
let data = new DataMessage({hash, store})
|
|
423
|
+
let data = new DataMessage({hash, store: store.name ? store.name : store});
|
|
509
424
|
|
|
510
425
|
const node = await this.prepareMessage(id, data.encoded)
|
|
511
426
|
if (closest[0]) data = await closest[0].request(node.encoded)
|
|
@@ -656,7 +571,7 @@ export default class Peernet {
|
|
|
656
571
|
if (peer.connection._connected) {
|
|
657
572
|
if (peer.id.toString() !== this.peerId.toString()) {
|
|
658
573
|
const node = await this.prepareMessage(peer.id, data.encoded)
|
|
659
|
-
peer.
|
|
574
|
+
peer.send(Buffer.from(JSON.stringify({id, data: node.encoded})))
|
|
660
575
|
}
|
|
661
576
|
} else {
|
|
662
577
|
this.removePeer(peer)
|
package/test/codec.js
CHANGED
|
@@ -3,17 +3,18 @@ const Codec = require('./../dist/commonjs/codec.js')
|
|
|
3
3
|
|
|
4
4
|
const block = '4lmacqas6kpnmvehxpmlvsndgydw5i46mf2pgnaub72eug63dozkw4oqxhk3fazbhkguvlhslcaeqm5jtwghxdk5nvmuakrs7yi2gl6xqmmmw';
|
|
5
5
|
const itx = '5hmacqb6ao252jmbhkhu5isr4yua3jnkhgb5ejjbiomrlzglwarlb777wq7aw5nfz5m4pubx5uffaxp2cqc4ckrh4xirss4nkujvcjbnmomtm';
|
|
6
|
-
const dht = '
|
|
6
|
+
const dht = 'b4wiyebqbigjfvm5mizik7ke3v6lyhoahecqiwergnl3cytarf5bd2qvqase'
|
|
7
7
|
const message = '4xnmcay'
|
|
8
8
|
const data = '4hemcay'
|
|
9
9
|
globalThis.peernet = {};
|
|
10
|
+
console.log(parseInt('706468', 16));
|
|
10
11
|
|
|
11
12
|
test('peernet dht codec', tape => {
|
|
12
13
|
tape.plan(2)
|
|
13
14
|
|
|
14
15
|
const codec = new Codec(dht)
|
|
15
16
|
const codec2 = new Codec(codec.encoded)
|
|
16
|
-
tape.equal(codec.name, 'peernet-dht', `name from encoded is valid ${codec.name}`)
|
|
17
|
+
tape.equal(codec.name, 'peernet-dht-response', `name from encoded is valid ${codec.name}`)
|
|
17
18
|
tape.equal(codec.name, codec2.name, `decoded name is valid ${codec.name}`)
|
|
18
19
|
})
|
|
19
20
|
|
package/test/messages.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
const test = require('tape')
|
|
2
2
|
const Node = require('./../dist/commonjs/dht-response.js')
|
|
3
3
|
|
|
4
|
-
const
|
|
5
|
-
const responseHash = '6lemcazaudes2wowemufpvcn27f4dxadsbielcitgv5rmjqis6qr5ikyajca'
|
|
4
|
+
const responseHash = 'cqbigjfvm5mizik7ke3v6lyhoahecqiwergnl3cytarf5bd2qvqase'
|
|
6
5
|
|
|
7
6
|
const block = '4lmacqas6kpnmvehxpmlvsndgydw5i46mf2pgnaub72eug63dozkw4oqxhk3fazbhkguvlhslcaeqm5jtwghxdk5nvmuakrs7yi2gl6xqmmmw';
|
|
8
7
|
const itx = '5hmacqb6ao252jmbhkhu5isr4yua3jnkhgb5ejjbiomrlzglwarlb777wq7aw5nfz5m4pubx5uffaxp2cqc4ckrh4xirss4nkujvcjbnmomtm';
|
|
@@ -10,12 +9,16 @@ const dht = '5demcay'
|
|
|
10
9
|
|
|
11
10
|
const message = '4xnmcay'
|
|
12
11
|
const data = '4hemcay'
|
|
13
|
-
|
|
12
|
+
globalThis.peernet = {
|
|
13
|
+
codecs: {}
|
|
14
|
+
}
|
|
14
15
|
test('peernet message dht-response', tape => {
|
|
15
16
|
tape.plan(4)
|
|
16
17
|
const n = new Node({ hash: 'hello', has: true })
|
|
17
|
-
|
|
18
|
+
console.log(n.hash);
|
|
19
|
+
const node = new Node(n.encoded)
|
|
18
20
|
const node2 = new Node(node.encoded)
|
|
21
|
+
|
|
19
22
|
tape.equal(node.name, 'peernet-dht-response', `name from encoded is valid ${node.name}`)
|
|
20
23
|
tape.equal(node.name, node2.name, `decoded name is valid ${node.name}`)
|
|
21
24
|
tape.equal(node.hash, responseHash, `hash from encoded is valid ${node.hash}`)
|