@leofcoin/peernet 0.10.7 → 0.11.1
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/coverage/lcov-report/block-navigation.js +8 -0
- package/coverage/lcov-report/codec-format-interface.js.html +224 -120
- package/coverage/lcov-report/dht-response.js.html +44 -39
- package/coverage/lcov-report/index.html +39 -64
- package/coverage/lcov-report/sorter.js +26 -0
- package/coverage/lcov.info +164 -424
- package/dist/browser/peernet.js +101788 -93018
- package/dist/commonjs/client-1a1f75e6.js +324 -0
- package/dist/commonjs/{codec-6367213c.js → codec-8c8c652f.js} +198 -188
- package/dist/commonjs/codec-format-interface.js +169 -152
- package/dist/commonjs/codec.js +4 -4
- package/dist/commonjs/dht-response.js +13 -13
- package/dist/commonjs/dht.js +24 -24
- package/dist/commonjs/hash.js +151 -141
- package/dist/commonjs/{http-a94c5a81.js → http-7bbac90a.js} +19 -15
- package/dist/commonjs/peernet-message.js +13 -13
- package/dist/commonjs/peernet.js +1901 -1794
- package/dist/commonjs/request.js +13 -13
- package/dist/commonjs/response.js +13 -13
- package/dist/module/peernet.js +2460 -2346
- package/index.html +4 -6
- package/package.json +22 -14
- package/rollup.config.js +33 -5
- package/rollup0.config.js +7 -0
- package/src/client.js +75 -75
- package/src/codec/codec-format-interface.js +172 -155
- package/src/codec/codec.js +124 -114
- package/src/codec/codecs.js +79 -79
- package/src/dht/dht.js +121 -121
- package/src/discovery/peer-discovery.js +75 -75
- package/src/handlers/message.js +52 -52
- package/src/hash/hash.js +155 -145
- package/src/http/client/http-client.js +44 -44
- 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 +14 -15
- 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/peer.js +67 -67
- package/src/peernet.js +612 -697
- package/src/proto/chat-message.proto.js +7 -7
- package/src/utils/utils.js +78 -78
- 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/codec.js.html +0 -677
- package/coverage/lcov-report/hash.js.html +0 -551
- 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-42a6e555.js +0 -324
- package/dist/commonjs/http-43f4fafe.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
|
+
}
|
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
import protons from 'protons'
|
|
2
|
-
import proto from './../proto/dht-response.proto.js'
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
1
|
+
import protons from 'protons'
|
|
2
|
+
import proto from './../proto/dht-response.proto.js'
|
|
3
|
+
import CodecFormat from './../codec/codec-format-interface.js'
|
|
4
|
+
|
|
5
|
+
export default class DHTMessageResponse extends CodecFormat {
|
|
6
|
+
get keys() {
|
|
7
|
+
return ['hash', 'has']
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
constructor(data) {
|
|
11
|
+
const name = 'peernet-dht-response'
|
|
12
|
+
super(data, protons(proto).PeernetDHTMessageResponse, {name})
|
|
13
|
+
}
|
|
14
|
+
}
|
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/peer.js
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
export default class PeernetPeer {
|
|
2
|
-
constructor(id, connection) {
|
|
3
|
-
this._events = {}
|
|
4
|
-
this.bw = {
|
|
5
|
-
up: 0,
|
|
6
|
-
down: 0,
|
|
7
|
-
}
|
|
8
|
-
this.id = id
|
|
9
|
-
this.connection = connection
|
|
10
|
-
|
|
11
|
-
this.connection.on('data', (message) => {
|
|
12
|
-
this.bw.down += message.length
|
|
13
|
-
pubsub.publish('peernet.data', JSON.parse(message.toString()))
|
|
14
|
-
})
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
request(data) {
|
|
18
|
-
return new Promise((resolve, reject) => {
|
|
19
|
-
const id = Math.random().toString(36).slice(-12)
|
|
20
|
-
data = Buffer.from(JSON.stringify({id, data}))
|
|
21
|
-
const _onData = (message) => {
|
|
22
|
-
if (message.id !== id) return
|
|
23
|
-
|
|
24
|
-
resolve(message.data)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
pubsub.subscribe('peernet.data', _onData)
|
|
28
|
-
|
|
29
|
-
// cleanup subscriptions
|
|
30
|
-
setTimeout(() => {
|
|
31
|
-
pubsub.unsubscribe('peernet.data', _onData)
|
|
32
|
-
}, 5000);
|
|
33
|
-
|
|
34
|
-
this.write(data)
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
write(data) {
|
|
39
|
-
if (!Buffer.isBuffer(data)) data = Buffer.from(data)
|
|
40
|
-
|
|
41
|
-
this.bw.up += data.length
|
|
42
|
-
this.connection.write(data)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
on(event = 'peernet.data', cb) {
|
|
46
|
-
this._events[event] = cb
|
|
47
|
-
pubsub.subscribe(event, cb)
|
|
48
|
-
// this.connection.on(event, cb)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
removeListener(event = 'data', cb) {
|
|
52
|
-
delete this._events[event]
|
|
53
|
-
pubsub.unsubscribe(event, cb)
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
close() {
|
|
57
|
-
for (const event of Object.keys(this._events)) {
|
|
58
|
-
pubsub.unsubscribe(event, this._events[event])
|
|
59
|
-
}
|
|
60
|
-
this._events = []
|
|
61
|
-
|
|
62
|
-
for (const event of this.connection._events.data) {
|
|
63
|
-
this.connection.removeListener('data', event)
|
|
64
|
-
}
|
|
65
|
-
this.connection.destroy()
|
|
66
|
-
}
|
|
67
|
-
}
|
|
1
|
+
export default class PeernetPeer {
|
|
2
|
+
constructor(id, connection) {
|
|
3
|
+
this._events = {}
|
|
4
|
+
this.bw = {
|
|
5
|
+
up: 0,
|
|
6
|
+
down: 0,
|
|
7
|
+
}
|
|
8
|
+
this.id = id
|
|
9
|
+
this.connection = connection
|
|
10
|
+
|
|
11
|
+
this.connection.on('data', (message) => {
|
|
12
|
+
this.bw.down += message.length
|
|
13
|
+
pubsub.publish('peernet.data', JSON.parse(message.toString()))
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
request(data) {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
const id = Math.random().toString(36).slice(-12)
|
|
20
|
+
data = Buffer.from(JSON.stringify({id, data}))
|
|
21
|
+
const _onData = (message) => {
|
|
22
|
+
if (message.id !== id) return
|
|
23
|
+
|
|
24
|
+
resolve(message.data)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
pubsub.subscribe('peernet.data', _onData)
|
|
28
|
+
|
|
29
|
+
// cleanup subscriptions
|
|
30
|
+
setTimeout(() => {
|
|
31
|
+
pubsub.unsubscribe('peernet.data', _onData)
|
|
32
|
+
}, 5000);
|
|
33
|
+
|
|
34
|
+
this.write(data)
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
write(data) {
|
|
39
|
+
if (!Buffer.isBuffer(data)) data = Buffer.from(data)
|
|
40
|
+
|
|
41
|
+
this.bw.up += data.length
|
|
42
|
+
this.connection.write(data)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
on(event = 'peernet.data', cb) {
|
|
46
|
+
this._events[event] = cb
|
|
47
|
+
pubsub.subscribe(event, cb)
|
|
48
|
+
// this.connection.on(event, cb)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
removeListener(event = 'data', cb) {
|
|
52
|
+
delete this._events[event]
|
|
53
|
+
pubsub.unsubscribe(event, cb)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
close() {
|
|
57
|
+
for (const event of Object.keys(this._events)) {
|
|
58
|
+
pubsub.unsubscribe(event, this._events[event])
|
|
59
|
+
}
|
|
60
|
+
this._events = []
|
|
61
|
+
|
|
62
|
+
for (const event of this.connection._events.data) {
|
|
63
|
+
this.connection.removeListener('data', event)
|
|
64
|
+
}
|
|
65
|
+
this.connection.destroy()
|
|
66
|
+
}
|
|
67
|
+
}
|