@leofcoin/peernet 0.11.0 → 0.11.3
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/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/codec-format-interface.js.html +637 -0
- package/coverage/lcov-report/dht-response.js.html +193 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +131 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov.info +199 -0
- package/dist/browser/326.peernet.js +1 -0
- package/dist/browser/peernet.js +2 -108930
- package/dist/browser/peernet.js.LICENSE.txt +40 -0
- package/dist/commonjs/{client-bd0caeb7.js → client-1a1f75e6.js} +41 -41
- package/dist/commonjs/{codec-4a768e5e.js → codec-8c8c652f.js} +118 -118
- package/dist/commonjs/codec-format-interface.js +167 -167
- package/dist/commonjs/codec.js +2 -2
- package/dist/commonjs/dht-response.js +11 -11
- package/dist/commonjs/dht.js +2 -2
- package/dist/commonjs/hash.js +149 -149
- package/dist/commonjs/{http-2b0735ef.js → http-e088ccdb.js} +1 -1
- package/dist/commonjs/peernet-message.js +4 -4
- package/dist/commonjs/peernet.js +850 -943
- package/dist/commonjs/request.js +2 -2
- package/dist/commonjs/response.js +3 -3
- package/dist/module/peernet.js +1373 -1466
- package/index.html +2 -2
- package/package.json +6 -21
- package/src/client.js +75 -75
- package/src/codec/codec-format-interface.js +172 -172
- package/src/codec/codec.js +124 -124
- package/src/dht/dht.js +121 -121
- package/src/discovery/peer-discovery.js +75 -75
- package/src/handlers/message.js +2 -4
- package/src/hash/hash.js +155 -155
- package/src/http/client/http-client.js +44 -44
- package/src/messages/dht-response.js +14 -14
- package/src/peer.js +67 -67
- package/src/peernet.js +613 -612
- package/src/proto/chat-message.proto.js +7 -7
- package/src/proto/peernet.proto.js +2 -2
- package/src/proto/response.proto.js +1 -1
- package/src/utils/utils.js +78 -78
- package/test.js +1 -1
- package/webpack.config.js +10 -4
package/src/hash/hash.js
CHANGED
|
@@ -1,155 +1,155 @@
|
|
|
1
|
-
import createKeccakHash from 'keccak';
|
|
2
|
-
import varint from 'varint';
|
|
3
|
-
import bs32 from '@vandeurenglenn/base32';
|
|
4
|
-
import bs58 from '@vandeurenglenn/base58';
|
|
5
|
-
import isHex from 'is-hex';
|
|
6
|
-
import Codec from './../codec/codec';
|
|
7
|
-
|
|
8
|
-
export default class PeernetHash {
|
|
9
|
-
constructor(buffer, options = {}) {
|
|
10
|
-
if (options.name) this.name = options.name
|
|
11
|
-
else this.name = 'disco-hash'
|
|
12
|
-
if (options.codecs) this.codecs = options.codecs
|
|
13
|
-
if (buffer) {
|
|
14
|
-
if (Buffer.isBuffer(buffer)) {
|
|
15
|
-
this.discoCodec = new Codec(buffer, this.codecs)
|
|
16
|
-
const name = this.discoCodec.name
|
|
17
|
-
|
|
18
|
-
if (name) {
|
|
19
|
-
this.name = name
|
|
20
|
-
this.decode(buffer)
|
|
21
|
-
} else {
|
|
22
|
-
this.encode(buffer)
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
if (typeof buffer === 'string') {
|
|
27
|
-
if (isHex(buffer)) this.fromHex(buffer)
|
|
28
|
-
if (bs32.isBase32(buffer)) this.fromBs32(buffer)
|
|
29
|
-
else if (bs58.isBase58(buffer)) this.fromBs58(buffer)
|
|
30
|
-
else throw new Error(`unsupported string ${buffer}`)
|
|
31
|
-
} else if (typeof buffer === 'object') this.fromJSON(buffer)
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
get prefix() {
|
|
36
|
-
const length = this.length
|
|
37
|
-
const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length)
|
|
38
|
-
for (let i = 0; i < this.discoCodec.codecBuffer.length; i++) {
|
|
39
|
-
uint8Array[i] = this.discoCodec.codecBuffer[i]
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
for (let i = uint8Array.length - 1; i < length.length; i++) {
|
|
43
|
-
uint8Array[i] = length[i]
|
|
44
|
-
}
|
|
45
|
-
return uint8Array
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
get length() {
|
|
49
|
-
return varint.encode(this.size)
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
get buffer() {
|
|
53
|
-
return this.hash
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
toHex() {
|
|
57
|
-
return this.hash.toString('hex')
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
fromHex(hex) {
|
|
61
|
-
return this.decode(Buffer.from(hex, 'hex'))
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
fromJSON(json) {
|
|
65
|
-
return this.encode(Buffer.from(JSON.stringify(json)))
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
toBs32() {
|
|
69
|
-
return bs32.encode(this.hash)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
fromBs32(bs) {
|
|
73
|
-
return this.decode(bs32.decode(bs))
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
toBs58() {
|
|
77
|
-
return bs58.encode(this.hash)
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
fromBs58(bs) {
|
|
81
|
-
return this.decode(bs58.decode(bs))
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
toString(encoding = 'utf8') {
|
|
85
|
-
return this.hash.toString(encoding)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
encode(buffer, name) {
|
|
89
|
-
if (!this.name && name) this.name = name;
|
|
90
|
-
if (!buffer) buffer = this.buffer;
|
|
91
|
-
this.discoCodec = new Codec(this.name, this.codecs)
|
|
92
|
-
this.discoCodec.fromName(this.name)
|
|
93
|
-
let hashAlg = this.discoCodec.hashAlg
|
|
94
|
-
if (hashAlg.includes('dbl')) {
|
|
95
|
-
hashAlg = hashAlg.replace('dbl-', '')
|
|
96
|
-
buffer = createKeccakHash(hashAlg.replace('-', '')).update(buffer).digest()
|
|
97
|
-
}
|
|
98
|
-
this.digest = createKeccakHash(hashAlg.replace('-', '')).update(buffer).digest()
|
|
99
|
-
this.size = this.digest.length
|
|
100
|
-
|
|
101
|
-
this.codec = this.discoCodec.encode();
|
|
102
|
-
this.codec = this.discoCodec.codecBuffer
|
|
103
|
-
this.hash = Buffer.concat([
|
|
104
|
-
this.prefix,
|
|
105
|
-
this.digest,
|
|
106
|
-
])
|
|
107
|
-
|
|
108
|
-
return this.hash
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
validate(buffer) {
|
|
112
|
-
if (Buffer.isBuffer(buffer)) {
|
|
113
|
-
const codec = varint.decode(buffer);
|
|
114
|
-
if (this.codecs[codec]) {
|
|
115
|
-
this.decode(buffer)
|
|
116
|
-
} else {
|
|
117
|
-
this.encode(buffer)
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
if (typeof buffer === 'string') {
|
|
121
|
-
if (isHex(buffer)) this.fromHex(buffer)
|
|
122
|
-
if (bs32.test(buffer)) this.fromBs32(buffer)
|
|
123
|
-
}
|
|
124
|
-
if (typeof buffer === 'object') this.fromJSON(buffer)
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
decode(buffer) {
|
|
128
|
-
this.hash = buffer
|
|
129
|
-
const codec = varint.decode(buffer);
|
|
130
|
-
|
|
131
|
-
this.discoCodec = new Codec(codec, this.codecs)
|
|
132
|
-
// TODO: validate codec
|
|
133
|
-
buffer = buffer.slice(varint.decode.bytes);
|
|
134
|
-
this.size = varint.decode(buffer);
|
|
135
|
-
this.digest = buffer.slice(varint.decode.bytes);
|
|
136
|
-
if (this.digest.length !== this.size) {
|
|
137
|
-
throw new Error(`hash length inconsistent: 0x${this.hash.toString('hex')}`)
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// const discoCodec = new Codec(codec, this.codecs)
|
|
141
|
-
|
|
142
|
-
this.name = this.discoCodec.name
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
this.size = this.digest.length
|
|
146
|
-
|
|
147
|
-
return {
|
|
148
|
-
codec: this.codec,
|
|
149
|
-
name: this.name,
|
|
150
|
-
size: this.size,
|
|
151
|
-
length: this.length,
|
|
152
|
-
digest: this.digest,
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
1
|
+
import createKeccakHash from 'keccak';
|
|
2
|
+
import varint from 'varint';
|
|
3
|
+
import bs32 from '@vandeurenglenn/base32';
|
|
4
|
+
import bs58 from '@vandeurenglenn/base58';
|
|
5
|
+
import isHex from '@vandeurenglenn/is-hex';
|
|
6
|
+
import Codec from './../codec/codec';
|
|
7
|
+
|
|
8
|
+
export default class PeernetHash {
|
|
9
|
+
constructor(buffer, options = {}) {
|
|
10
|
+
if (options.name) this.name = options.name
|
|
11
|
+
else this.name = 'disco-hash'
|
|
12
|
+
if (options.codecs) this.codecs = options.codecs
|
|
13
|
+
if (buffer) {
|
|
14
|
+
if (Buffer.isBuffer(buffer)) {
|
|
15
|
+
this.discoCodec = new Codec(buffer, this.codecs)
|
|
16
|
+
const name = this.discoCodec.name
|
|
17
|
+
|
|
18
|
+
if (name) {
|
|
19
|
+
this.name = name
|
|
20
|
+
this.decode(buffer)
|
|
21
|
+
} else {
|
|
22
|
+
this.encode(buffer)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (typeof buffer === 'string') {
|
|
27
|
+
if (isHex(buffer)) this.fromHex(buffer)
|
|
28
|
+
if (bs32.isBase32(buffer)) this.fromBs32(buffer)
|
|
29
|
+
else if (bs58.isBase58(buffer)) this.fromBs58(buffer)
|
|
30
|
+
else throw new Error(`unsupported string ${buffer}`)
|
|
31
|
+
} else if (typeof buffer === 'object') this.fromJSON(buffer)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get prefix() {
|
|
36
|
+
const length = this.length
|
|
37
|
+
const uint8Array = new Uint8Array(length.length + this.discoCodec.codecBuffer.length)
|
|
38
|
+
for (let i = 0; i < this.discoCodec.codecBuffer.length; i++) {
|
|
39
|
+
uint8Array[i] = this.discoCodec.codecBuffer[i]
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (let i = uint8Array.length - 1; i < length.length; i++) {
|
|
43
|
+
uint8Array[i] = length[i]
|
|
44
|
+
}
|
|
45
|
+
return uint8Array
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
get length() {
|
|
49
|
+
return varint.encode(this.size)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get buffer() {
|
|
53
|
+
return this.hash
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
toHex() {
|
|
57
|
+
return this.hash.toString('hex')
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
fromHex(hex) {
|
|
61
|
+
return this.decode(Buffer.from(hex, 'hex'))
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
fromJSON(json) {
|
|
65
|
+
return this.encode(Buffer.from(JSON.stringify(json)))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
toBs32() {
|
|
69
|
+
return bs32.encode(this.hash)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
fromBs32(bs) {
|
|
73
|
+
return this.decode(bs32.decode(bs))
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
toBs58() {
|
|
77
|
+
return bs58.encode(this.hash)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
fromBs58(bs) {
|
|
81
|
+
return this.decode(bs58.decode(bs))
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
toString(encoding = 'utf8') {
|
|
85
|
+
return this.hash.toString(encoding)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
encode(buffer, name) {
|
|
89
|
+
if (!this.name && name) this.name = name;
|
|
90
|
+
if (!buffer) buffer = this.buffer;
|
|
91
|
+
this.discoCodec = new Codec(this.name, this.codecs)
|
|
92
|
+
this.discoCodec.fromName(this.name)
|
|
93
|
+
let hashAlg = this.discoCodec.hashAlg
|
|
94
|
+
if (hashAlg.includes('dbl')) {
|
|
95
|
+
hashAlg = hashAlg.replace('dbl-', '')
|
|
96
|
+
buffer = createKeccakHash(hashAlg.replace('-', '')).update(buffer).digest()
|
|
97
|
+
}
|
|
98
|
+
this.digest = createKeccakHash(hashAlg.replace('-', '')).update(buffer).digest()
|
|
99
|
+
this.size = this.digest.length
|
|
100
|
+
|
|
101
|
+
this.codec = this.discoCodec.encode();
|
|
102
|
+
this.codec = this.discoCodec.codecBuffer
|
|
103
|
+
this.hash = Buffer.concat([
|
|
104
|
+
this.prefix,
|
|
105
|
+
this.digest,
|
|
106
|
+
])
|
|
107
|
+
|
|
108
|
+
return this.hash
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
validate(buffer) {
|
|
112
|
+
if (Buffer.isBuffer(buffer)) {
|
|
113
|
+
const codec = varint.decode(buffer);
|
|
114
|
+
if (this.codecs[codec]) {
|
|
115
|
+
this.decode(buffer)
|
|
116
|
+
} else {
|
|
117
|
+
this.encode(buffer)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (typeof buffer === 'string') {
|
|
121
|
+
if (isHex(buffer)) this.fromHex(buffer)
|
|
122
|
+
if (bs32.test(buffer)) this.fromBs32(buffer)
|
|
123
|
+
}
|
|
124
|
+
if (typeof buffer === 'object') this.fromJSON(buffer)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
decode(buffer) {
|
|
128
|
+
this.hash = buffer
|
|
129
|
+
const codec = varint.decode(buffer);
|
|
130
|
+
|
|
131
|
+
this.discoCodec = new Codec(codec, this.codecs)
|
|
132
|
+
// TODO: validate codec
|
|
133
|
+
buffer = buffer.slice(varint.decode.bytes);
|
|
134
|
+
this.size = varint.decode(buffer);
|
|
135
|
+
this.digest = buffer.slice(varint.decode.bytes);
|
|
136
|
+
if (this.digest.length !== this.size) {
|
|
137
|
+
throw new Error(`hash length inconsistent: 0x${this.hash.toString('hex')}`)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// const discoCodec = new Codec(codec, this.codecs)
|
|
141
|
+
|
|
142
|
+
this.name = this.discoCodec.name
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
this.size = this.digest.length
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
codec: this.codec,
|
|
149
|
+
name: this.name,
|
|
150
|
+
size: this.size,
|
|
151
|
+
length: this.length,
|
|
152
|
+
digest: this.digest,
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import Client from './../../../node_modules/socket-request-client/dist/es/index'
|
|
2
|
-
|
|
3
|
-
export default class HttpClientApi {
|
|
4
|
-
constructor(config = {}) {
|
|
5
|
-
if (!config.apiPath) config.apiPath = 'api'
|
|
6
|
-
|
|
7
|
-
const address = `ws://${config.host}:${config.port}`
|
|
8
|
-
|
|
9
|
-
this.apiUrl = (url) => `${address}/${url}`;
|
|
10
|
-
return (async () => {
|
|
11
|
-
this.client = await Client(address, config.protocol, {pubsub: config.pubsub, retry: 3000})
|
|
12
|
-
return this
|
|
13
|
-
})()
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
async get(url, obj) {
|
|
17
|
-
const headers = {}
|
|
18
|
-
let body = null
|
|
19
|
-
let method = 'GET'
|
|
20
|
-
if (obj) {
|
|
21
|
-
method = 'POST'
|
|
22
|
-
headers['Content-Type'] = 'application/json'
|
|
23
|
-
body = JSON.stringify(obj)
|
|
24
|
-
}
|
|
25
|
-
let response = await this.client.request(url, {headers, body, method})
|
|
26
|
-
const type = response.headers.get('content-type').split(';')[0]
|
|
27
|
-
if (type==='application/json') response = await response.json()
|
|
28
|
-
return response
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async put(url, obj) {
|
|
32
|
-
const headers = {}
|
|
33
|
-
let body = {}
|
|
34
|
-
if (obj) {
|
|
35
|
-
headers['Content-Type'] = 'application/json'
|
|
36
|
-
body = JSON.stringify(obj)
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
let response = await fetch(this.apiUrl(url), {method: 'PUT', headers, body})
|
|
40
|
-
const type = response.headers.get('content-type').split(';')[0]
|
|
41
|
-
if (type==='application/json') response = await response.json()
|
|
42
|
-
return response
|
|
43
|
-
}
|
|
44
|
-
}
|
|
1
|
+
import Client from './../../../node_modules/socket-request-client/dist/es/index'
|
|
2
|
+
|
|
3
|
+
export default class HttpClientApi {
|
|
4
|
+
constructor(config = {}) {
|
|
5
|
+
if (!config.apiPath) config.apiPath = 'api'
|
|
6
|
+
|
|
7
|
+
const address = `ws://${config.host}:${config.port}`
|
|
8
|
+
|
|
9
|
+
this.apiUrl = (url) => `${address}/${url}`;
|
|
10
|
+
return (async () => {
|
|
11
|
+
this.client = await Client(address, config.protocol, {pubsub: config.pubsub, retry: 3000})
|
|
12
|
+
return this
|
|
13
|
+
})()
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async get(url, obj) {
|
|
17
|
+
const headers = {}
|
|
18
|
+
let body = null
|
|
19
|
+
let method = 'GET'
|
|
20
|
+
if (obj) {
|
|
21
|
+
method = 'POST'
|
|
22
|
+
headers['Content-Type'] = 'application/json'
|
|
23
|
+
body = JSON.stringify(obj)
|
|
24
|
+
}
|
|
25
|
+
let response = await this.client.request(url, {headers, body, method})
|
|
26
|
+
const type = response.headers.get('content-type').split(';')[0]
|
|
27
|
+
if (type==='application/json') response = await response.json()
|
|
28
|
+
return response
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async put(url, obj) {
|
|
32
|
+
const headers = {}
|
|
33
|
+
let body = {}
|
|
34
|
+
if (obj) {
|
|
35
|
+
headers['Content-Type'] = 'application/json'
|
|
36
|
+
body = JSON.stringify(obj)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let response = await fetch(this.apiUrl(url), {method: 'PUT', headers, body})
|
|
40
|
+
const type = response.headers.get('content-type').split(';')[0]
|
|
41
|
+
if (type==='application/json') response = await response.json()
|
|
42
|
+
return response
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -1,14 +1,14 @@
|
|
|
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
|
-
}
|
|
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/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
|
+
}
|