@leofcoin/peernet 0.9.9 → 0.9.12

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.
@@ -32,6 +32,7 @@ var MultiWallet__default = /*#__PURE__*/_interopDefaultLegacy(MultiWallet);
32
32
 
33
33
  class PeernetPeer {
34
34
  constructor(id, connection) {
35
+ this._events = {};
35
36
  this.id = id;
36
37
  this.connection = connection;
37
38
 
@@ -65,13 +66,27 @@ class PeernetPeer {
65
66
  }
66
67
 
67
68
  on(event = 'peernet.data', cb) {
69
+ this._events[event] = cb;
68
70
  pubsub.subscribe(event, cb);
69
71
  // this.connection.on(event, cb)
70
72
  }
71
73
 
72
74
  removeListener(event = 'data', cb) {
75
+ delete this._events[event];
73
76
  pubsub.unsubscribe(event, cb);
74
77
  }
78
+
79
+ close() {
80
+ for (const event of Object.keys(this._events)) {
81
+ pubsub.unsubscribe(event, this._events[event]);
82
+ }
83
+ this._events = [];
84
+
85
+ for (const event of this.connection._events.data) {
86
+ this.connection.removeListener('data', event);
87
+ }
88
+ this.connection.destroy();
89
+ }
75
90
  }
76
91
 
77
92
  /**
@@ -106,6 +121,10 @@ class PeernetClient {
106
121
 
107
122
  this.p2p.on('peerclose', (peer) => {
108
123
  // TODO: close peernetPeer
124
+ const peernetPeer = connections.get(peer.id);
125
+ if (peernetPeer) {
126
+ peernetPeer.close();
127
+ }
109
128
  connections.delete(peer.id);
110
129
  pubsub.publish('peer:disconnected', peer);
111
130
  });
@@ -261,7 +280,7 @@ class LeofcoinStorage$1 {
261
280
 
262
281
  }
263
282
 
264
- var version = "0.9.7";
283
+ var version = "0.9.11";
265
284
 
266
285
  var api$1 = {
267
286
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -1689,7 +1708,7 @@ class PeerDiscovery {
1689
1708
  const connections = peernet.peerMap.get(id);
1690
1709
  if (connections.indexOf(peer.id) === -1) {
1691
1710
  connections.push(peer.id);
1692
- peernet.peerMap.set(from, connections);
1711
+ peernet.peerMap.set(peer.id, connections);
1693
1712
  }
1694
1713
  }
1695
1714
  return id
@@ -2130,18 +2149,18 @@ class Peernet {
2130
2149
  this.peerId = id;
2131
2150
 
2132
2151
  pubsub.subscribe('peer:discovered', async (peer) => {
2133
- this._peerHandler.discover(peer);
2134
2152
  peer.on('peernet.data', async (message) => {
2135
2153
  const id = message.id;
2136
2154
  message = new PeernetMessage(Buffer.from(message.data.data));
2137
2155
  const proto = protoFor(message.decoded.data);
2138
2156
  await this._protoHandler({id, proto}, peer);
2139
- const fulldId = this._getPeerId(peer.id);
2140
- if (fulldId && this._discovered.indexOf(peer.id) === -1) {
2141
- this._discovered.push(peer.id);
2142
- pubsub.publish('peer:connected', peer);
2143
- }
2144
2157
  });
2158
+ await this._peerHandler.discover(peer);
2159
+ const fulldId = this._getPeerId(peer.id);
2160
+ if (fulldId && this._discovered.indexOf(peer.id) === -1) {
2161
+ this._discovered.push(peer.id);
2162
+ pubsub.publish('peer:connected', peer);
2163
+ }
2145
2164
  });
2146
2165
  pubsub.subscribe('peer:disconnected', async (peer) => {
2147
2166
  let index = this._discovered.indexOf(peer.id);
@@ -2174,6 +2193,9 @@ class Peernet {
2174
2193
  * @type {PeernetClient}
2175
2194
  */
2176
2195
  this.client = new PeernetClient({...options, id});
2196
+ if (globalThis.onbeforeunload) {
2197
+ globalThis.addEventListener('beforeunload', async () => this.client.close());
2198
+ }
2177
2199
  return this
2178
2200
  }
2179
2201
 
@@ -2271,7 +2293,7 @@ class Peernet {
2271
2293
  }
2272
2294
 
2273
2295
  if (data) {
2274
- data = new DataMessageResponse({hash, data: Buffer.from(data)});
2296
+ data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
2275
2297
 
2276
2298
  const node = await this.prepareMessage(from, data.encoded);
2277
2299
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
@@ -35,6 +35,7 @@ var MultiWallet__default = /*#__PURE__*/_interopDefaultLegacy(MultiWallet);
35
35
 
36
36
  class PeernetPeer {
37
37
  constructor(id, connection) {
38
+ this._events = {};
38
39
  this.id = id;
39
40
  this.connection = connection;
40
41
 
@@ -68,13 +69,27 @@ class PeernetPeer {
68
69
  }
69
70
 
70
71
  on(event = 'peernet.data', cb) {
72
+ this._events[event] = cb;
71
73
  pubsub.subscribe(event, cb);
72
74
  // this.connection.on(event, cb)
73
75
  }
74
76
 
75
77
  removeListener(event = 'data', cb) {
78
+ delete this._events[event];
76
79
  pubsub.unsubscribe(event, cb);
77
80
  }
81
+
82
+ close() {
83
+ for (const event of Object.keys(this._events)) {
84
+ pubsub.unsubscribe(event, this._events[event]);
85
+ }
86
+ this._events = [];
87
+
88
+ for (const event of this.connection._events.data) {
89
+ this.connection.removeListener('data', event);
90
+ }
91
+ this.connection.destroy();
92
+ }
78
93
  }
79
94
 
80
95
  /**
@@ -109,6 +124,10 @@ class PeernetClient {
109
124
 
110
125
  this.p2p.on('peerclose', (peer) => {
111
126
  // TODO: close peernetPeer
127
+ const peernetPeer = connections.get(peer.id);
128
+ if (peernetPeer) {
129
+ peernetPeer.close();
130
+ }
112
131
  connections.delete(peer.id);
113
132
  pubsub.publish('peer:disconnected', peer);
114
133
  });
@@ -264,7 +283,7 @@ class LeofcoinStorage$1 {
264
283
 
265
284
  }
266
285
 
267
- var version = "0.9.7";
286
+ var version = "0.9.11";
268
287
 
269
288
  var api$1 = {
270
289
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -1124,7 +1143,7 @@ class PeerDiscovery {
1124
1143
  const connections = peernet.peerMap.get(id);
1125
1144
  if (connections.indexOf(peer.id) === -1) {
1126
1145
  connections.push(peer.id);
1127
- peernet.peerMap.set(from, connections);
1146
+ peernet.peerMap.set(peer.id, connections);
1128
1147
  }
1129
1148
  }
1130
1149
  return id
@@ -1565,18 +1584,18 @@ class Peernet {
1565
1584
  this.peerId = id;
1566
1585
 
1567
1586
  pubsub.subscribe('peer:discovered', async (peer) => {
1568
- this._peerHandler.discover(peer);
1569
1587
  peer.on('peernet.data', async (message) => {
1570
1588
  const id = message.id;
1571
1589
  message = new peernetMessage(Buffer.from(message.data.data));
1572
1590
  const proto = protoFor(message.decoded.data);
1573
1591
  await this._protoHandler({id, proto}, peer);
1574
- const fulldId = this._getPeerId(peer.id);
1575
- if (fulldId && this._discovered.indexOf(peer.id) === -1) {
1576
- this._discovered.push(peer.id);
1577
- pubsub.publish('peer:connected', peer);
1578
- }
1579
1592
  });
1593
+ await this._peerHandler.discover(peer);
1594
+ const fulldId = this._getPeerId(peer.id);
1595
+ if (fulldId && this._discovered.indexOf(peer.id) === -1) {
1596
+ this._discovered.push(peer.id);
1597
+ pubsub.publish('peer:connected', peer);
1598
+ }
1580
1599
  });
1581
1600
  pubsub.subscribe('peer:disconnected', async (peer) => {
1582
1601
  let index = this._discovered.indexOf(peer.id);
@@ -1609,6 +1628,9 @@ class Peernet {
1609
1628
  * @type {PeernetClient}
1610
1629
  */
1611
1630
  this.client = new PeernetClient({...options, id});
1631
+ if (globalThis.onbeforeunload) {
1632
+ globalThis.addEventListener('beforeunload', async () => this.client.close());
1633
+ }
1612
1634
  return this
1613
1635
  }
1614
1636
 
@@ -1706,7 +1728,7 @@ class Peernet {
1706
1728
  }
1707
1729
 
1708
1730
  if (data) {
1709
- data = new DataMessageResponse({hash, data: Buffer.from(data)});
1731
+ data = new DataMessageResponse({hash, data: data.decoded ? Buffer.from(JSON.stringify(data)) : Buffer.from(data)});
1710
1732
 
1711
1733
  const node = await this.prepareMessage(from, data.encoded);
1712
1734
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
@@ -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.7";
266
+ var version = "0.9.11";
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(from, connections);
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
+ globalThis.addEventListener('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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.9.9",
3
+ "version": "0.9.12",
4
4
  "description": "",
5
5
  "main": "dist/commonjs/peernet.js",
6
6
  "module": "dist/module/peernet.js",
@@ -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(from, connections)
36
+ peernet.peerMap.set(peer.id, connections)
37
37
  }
38
38
  }
39
39
  return id
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
- const fulldId = this._getPeerId(peer.id)
214
- if (fulldId && this._discovered.indexOf(peer.id) === -1) {
215
- this._discovered.push(peer.id)
216
- pubsub.publish('peer:connected', peer)
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
+ globalThis.addEventListener('beforeunload', async () => this.client.close());
253
+ }
251
254
  return this
252
255
  }
253
256