@leofcoin/peernet 0.9.8 → 0.9.11
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/dist/browser/peernet.js +674 -652
- package/dist/commonjs/peernet.js +674 -652
- package/dist/module/peernet.js +31 -9
- package/package.json +1 -1
- package/src/client.js +4 -0
- package/src/discovery/peer-discovery.js +1 -1
- package/src/peer.js +15 -0
- package/src/peernet.js +16 -13
package/dist/module/peernet.js
CHANGED
|
@@ -15,6 +15,7 @@ import MultiWallet from '@leofcoin/multi-wallet';
|
|
|
15
15
|
|
|
16
16
|
class PeernetPeer {
|
|
17
17
|
constructor(id, connection) {
|
|
18
|
+
this._events = {};
|
|
18
19
|
this.id = id;
|
|
19
20
|
this.connection = connection;
|
|
20
21
|
|
|
@@ -48,13 +49,27 @@ class PeernetPeer {
|
|
|
48
49
|
}
|
|
49
50
|
|
|
50
51
|
on(event = 'peernet.data', cb) {
|
|
52
|
+
this._events[event] = cb;
|
|
51
53
|
pubsub.subscribe(event, cb);
|
|
52
54
|
// this.connection.on(event, cb)
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
removeListener(event = 'data', cb) {
|
|
58
|
+
delete this._events[event];
|
|
56
59
|
pubsub.unsubscribe(event, cb);
|
|
57
60
|
}
|
|
61
|
+
|
|
62
|
+
close() {
|
|
63
|
+
for (const event of Object.keys(this._events)) {
|
|
64
|
+
pubsub.unsubscribe(event, this._events[event]);
|
|
65
|
+
}
|
|
66
|
+
this._events = [];
|
|
67
|
+
|
|
68
|
+
for (const event of this.connection._events.data) {
|
|
69
|
+
this.connection.removeListener('data', event);
|
|
70
|
+
}
|
|
71
|
+
this.connection.destroy();
|
|
72
|
+
}
|
|
58
73
|
}
|
|
59
74
|
|
|
60
75
|
/**
|
|
@@ -89,6 +104,10 @@ class PeernetClient {
|
|
|
89
104
|
|
|
90
105
|
this.p2p.on('peerclose', (peer) => {
|
|
91
106
|
// TODO: close peernetPeer
|
|
107
|
+
const peernetPeer = connections.get(peer.id);
|
|
108
|
+
if (peernetPeer) {
|
|
109
|
+
peernetPeer.close();
|
|
110
|
+
}
|
|
92
111
|
connections.delete(peer.id);
|
|
93
112
|
pubsub.publish('peer:disconnected', peer);
|
|
94
113
|
});
|
|
@@ -244,7 +263,7 @@ class LeofcoinStorage$1 {
|
|
|
244
263
|
|
|
245
264
|
}
|
|
246
265
|
|
|
247
|
-
var version = "0.9.
|
|
266
|
+
var version = "0.9.10";
|
|
248
267
|
|
|
249
268
|
var api$1 = {
|
|
250
269
|
version: ({send}) => send({client: '@peernet/api/http', version}),
|
|
@@ -1672,7 +1691,7 @@ class PeerDiscovery {
|
|
|
1672
1691
|
const connections = peernet.peerMap.get(id);
|
|
1673
1692
|
if (connections.indexOf(peer.id) === -1) {
|
|
1674
1693
|
connections.push(peer.id);
|
|
1675
|
-
peernet.peerMap.set(
|
|
1694
|
+
peernet.peerMap.set(peer.id, connections);
|
|
1676
1695
|
}
|
|
1677
1696
|
}
|
|
1678
1697
|
return id
|
|
@@ -2113,18 +2132,18 @@ class Peernet {
|
|
|
2113
2132
|
this.peerId = id;
|
|
2114
2133
|
|
|
2115
2134
|
pubsub.subscribe('peer:discovered', async (peer) => {
|
|
2116
|
-
this._peerHandler.discover(peer);
|
|
2117
2135
|
peer.on('peernet.data', async (message) => {
|
|
2118
2136
|
const id = message.id;
|
|
2119
2137
|
message = new PeernetMessage(Buffer.from(message.data.data));
|
|
2120
2138
|
const proto = protoFor(message.decoded.data);
|
|
2121
2139
|
await this._protoHandler({id, proto}, peer);
|
|
2122
|
-
const fulldId = this._getPeerId(peer.id);
|
|
2123
|
-
if (fulldId && this._discovered.indexOf(peer.id) === -1) {
|
|
2124
|
-
this._discovered.push(peer.id);
|
|
2125
|
-
pubsub.publish('peer:connected', peer);
|
|
2126
|
-
}
|
|
2127
2140
|
});
|
|
2141
|
+
await this._peerHandler.discover(peer);
|
|
2142
|
+
const fulldId = this._getPeerId(peer.id);
|
|
2143
|
+
if (fulldId && this._discovered.indexOf(peer.id) === -1) {
|
|
2144
|
+
this._discovered.push(peer.id);
|
|
2145
|
+
pubsub.publish('peer:connected', peer);
|
|
2146
|
+
}
|
|
2128
2147
|
});
|
|
2129
2148
|
pubsub.subscribe('peer:disconnected', async (peer) => {
|
|
2130
2149
|
let index = this._discovered.indexOf(peer.id);
|
|
@@ -2157,6 +2176,9 @@ class Peernet {
|
|
|
2157
2176
|
* @type {PeernetClient}
|
|
2158
2177
|
*/
|
|
2159
2178
|
this.client = new PeernetClient({...options, id});
|
|
2179
|
+
if (globalThis.onbeforeunload) {
|
|
2180
|
+
globalThisaddEventListener('beforeunload', async () => this.client.close());
|
|
2181
|
+
}
|
|
2160
2182
|
return this
|
|
2161
2183
|
}
|
|
2162
2184
|
|
|
@@ -2254,7 +2276,7 @@ class Peernet {
|
|
|
2254
2276
|
}
|
|
2255
2277
|
|
|
2256
2278
|
if (data) {
|
|
2257
|
-
data = new DataMessageResponse({hash, data: Buffer.from(data)});
|
|
2279
|
+
data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
|
|
2258
2280
|
|
|
2259
2281
|
const node = await this.prepareMessage(from, data.encoded);
|
|
2260
2282
|
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -37,6 +37,10 @@ export default class PeernetClient {
|
|
|
37
37
|
|
|
38
38
|
this.p2p.on('peerclose', (peer) => {
|
|
39
39
|
// TODO: close peernetPeer
|
|
40
|
+
const peernetPeer = connections.get(peer.id)
|
|
41
|
+
if (peernetPeer) {
|
|
42
|
+
peernetPeer.close()
|
|
43
|
+
}
|
|
40
44
|
connections.delete(peer.id)
|
|
41
45
|
pubsub.publish('peer:disconnected', peer)
|
|
42
46
|
})
|
|
@@ -33,7 +33,7 @@ export default class PeerDiscovery {
|
|
|
33
33
|
const connections = peernet.peerMap.get(id)
|
|
34
34
|
if (connections.indexOf(peer.id) === -1) {
|
|
35
35
|
connections.push(peer.id)
|
|
36
|
-
peernet.peerMap.set(
|
|
36
|
+
peernet.peerMap.set(peer.id, connections)
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
return id
|
package/src/peer.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export default class PeernetPeer {
|
|
2
2
|
constructor(id, connection) {
|
|
3
|
+
this._events = {}
|
|
3
4
|
this.id = id
|
|
4
5
|
this.connection = connection
|
|
5
6
|
|
|
@@ -33,11 +34,25 @@ export default class PeernetPeer {
|
|
|
33
34
|
}
|
|
34
35
|
|
|
35
36
|
on(event = 'peernet.data', cb) {
|
|
37
|
+
this._events[event] = cb
|
|
36
38
|
pubsub.subscribe(event, cb)
|
|
37
39
|
// this.connection.on(event, cb)
|
|
38
40
|
}
|
|
39
41
|
|
|
40
42
|
removeListener(event = 'data', cb) {
|
|
43
|
+
delete this._events[event]
|
|
41
44
|
pubsub.unsubscribe(event, cb)
|
|
42
45
|
}
|
|
46
|
+
|
|
47
|
+
close() {
|
|
48
|
+
for (const event of Object.keys(this._events)) {
|
|
49
|
+
pubsub.unsubscribe(event, this._events[event])
|
|
50
|
+
}
|
|
51
|
+
this._events = []
|
|
52
|
+
|
|
53
|
+
for (const event of this.connection._events.data) {
|
|
54
|
+
this.connection.removeListener('data', event)
|
|
55
|
+
}
|
|
56
|
+
this.connection.destroy()
|
|
57
|
+
}
|
|
43
58
|
}
|
package/src/peernet.js
CHANGED
|
@@ -204,19 +204,19 @@ export default class Peernet {
|
|
|
204
204
|
this.peerId = id
|
|
205
205
|
|
|
206
206
|
pubsub.subscribe('peer:discovered', async (peer) => {
|
|
207
|
-
this._peerHandler.discover(peer)
|
|
208
207
|
peer.on('peernet.data', async (message) => {
|
|
209
|
-
const id = message.id
|
|
210
|
-
message = new PeernetMessage(Buffer.from(message.data.data))
|
|
211
|
-
const proto = protoFor(message.decoded.data)
|
|
212
|
-
await this._protoHandler({id, proto}, peer)
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
208
|
+
const id = message.id;
|
|
209
|
+
message = new PeernetMessage(Buffer.from(message.data.data));
|
|
210
|
+
const proto = protoFor(message.decoded.data);
|
|
211
|
+
await this._protoHandler({id, proto}, peer);
|
|
212
|
+
});
|
|
213
|
+
await this._peerHandler.discover(peer);
|
|
214
|
+
const fulldId = this._getPeerId(peer.id);
|
|
215
|
+
if (fulldId && this._discovered.indexOf(peer.id) === -1) {
|
|
216
|
+
this._discovered.push(peer.id);
|
|
217
|
+
pubsub.publish('peer:connected', peer);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
220
|
pubsub.subscribe('peer:disconnected', async (peer) => {
|
|
221
221
|
let index = this._discovered.indexOf(peer.id)
|
|
222
222
|
if (index !== -1) this._discovered.splice(index, 1)
|
|
@@ -248,6 +248,9 @@ export default class Peernet {
|
|
|
248
248
|
* @type {PeernetClient}
|
|
249
249
|
*/
|
|
250
250
|
this.client = new Client({...options, id})
|
|
251
|
+
if (globalThis.onbeforeunload) {
|
|
252
|
+
globalThisaddEventListener('beforeunload', async () => this.client.close());
|
|
253
|
+
}
|
|
251
254
|
return this
|
|
252
255
|
}
|
|
253
256
|
|
|
@@ -345,7 +348,7 @@ export default class Peernet {
|
|
|
345
348
|
}
|
|
346
349
|
|
|
347
350
|
if (data) {
|
|
348
|
-
data = new DataMessageResponse({hash, data: Buffer.from(data)})
|
|
351
|
+
data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
|
|
349
352
|
|
|
350
353
|
const node = await this.prepareMessage(from, data.encoded)
|
|
351
354
|
peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})))
|