@leofcoin/peernet 0.8.7 → 0.8.9

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.
@@ -86,10 +86,11 @@ globalThis.pubsub = globalThis.pubsub || new PubSub__default['default']({verbose
86
86
  class PeernetClient {
87
87
  constructor(options = {}) {
88
88
  if (!options.id) options.id = Buffer.from('00000000000000000000000000000000');
89
-
89
+ if (!options.networkVersion) options.networkVersion = 'v0.1.0';
90
+ if (!options.networkName) options.networkName = 'peernet';
90
91
  this.id = options.id;
91
92
 
92
- this.topic = Buffer.from(sha256__default['default']('peernet-v0.1.0').toString());
93
+ this.topic = Buffer.from(sha256__default['default'](`${option.networkName}-${options.networkVersion}`).toString());
93
94
 
94
95
  const trackers = [
95
96
  'wss://star.leofcoin.org:7575',
@@ -111,71 +112,6 @@ class PeernetClient {
111
112
 
112
113
  this.p2p.start();
113
114
 
114
-
115
- // this.sw.join(this.topic.slice(0, 32))
116
- //
117
- // const arrayOfPeers = this.sw.getPeers()
118
- //
119
- // this.sw.on('connection', async (connection, info) => {
120
- // // connected
121
- // const id = info.id.toString()
122
- // if (id === this.id.toString()) return
123
- //
124
- // const channel = connection.channelName
125
- // let channels = [channel]
126
- // let peer
127
- // if (connections.has(id)) {
128
- // const value = connections.get(id)
129
- // const _channels = value.channels
130
- // channels = [...channels, ..._channels]
131
- // peer = value.peer
132
- // } else {
133
- // peer = new PeernetPeer(id, connection)
134
- // pubsub.publish('peer:connected', peer)
135
- // }
136
- // connections.set(id, {channels, peer})
137
- // // connection.on('data', (data) => console.log({data}))
138
- // // connection.write('peerId')
139
- // });
140
- //
141
- // this.sw.on('candidates-updated', async (channel, candidates) => {
142
- // // TODO: Channels ...
143
- // for (const candidate of candidates) {
144
- // const id = candidate.toString()
145
- // let channels = [channel]
146
- // if (id === this.id.toString()) return
147
- //
148
- // if (!connections.has(id)) {
149
- // try {
150
- // const connection = await this.sw.connect(channel, candidate)
151
- // const peer = new PeernetPeer(id, connection)
152
- // connections.set(id, {channels, peer})
153
- // if (recentConnections.has(id)) {
154
- // setTimeout(() => {
155
- // pubsub.publish('peer:connected', peer)
156
- // }, 1000)
157
- // } else {
158
- // pubsub.publish('peer:connected', peer)
159
- // }
160
- // } catch (e) {
161
- // console.warn(e);
162
- // }
163
- // } else {
164
- // const value = connections.get(id)
165
- // const _channels = value.channels
166
- // channels = [...channels, ..._channels]
167
- // connections.set(id, {channels, peer: value.peer})
168
- // }
169
- // }
170
- // })
171
- //
172
- // this.sw.on('connection-closed', (connection, info) => {
173
- // if (!recentConnections.has(info.id.toString())) {
174
- // recentConnections.set(info.id.toString(), new Date().getTime())
175
- // }
176
- // if (connections.has(info.id.toString())) connections.delete(info.id.toString())
177
- // })
178
- //
179
115
  if (globalThis.process) {
180
116
  process.on('SIGINT', async () => {
181
117
  console.log('Caught interrupt signal');
@@ -325,7 +261,7 @@ class LeofcoinStorage$1 {
325
261
 
326
262
  }
327
263
 
328
- var version = "0.8.6";
264
+ var version = "0.8.9";
329
265
 
330
266
  var api$1 = {
331
267
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -1729,14 +1665,10 @@ class PeerDiscovery {
1729
1665
  const connections = peernet.peerMap.get(id);
1730
1666
  if (connections.indexOf(peer.id) === -1) {
1731
1667
  connections.push(peer.id);
1732
- peernet.peerMap.set(id, connections);
1668
+ peernet.peerMap.set(from, connections);
1733
1669
  }
1734
1670
  }
1735
- return {peer, id}
1736
- }
1737
-
1738
- async _cleanedUpIfNeeded() {
1739
-
1671
+ return id
1740
1672
  }
1741
1673
 
1742
1674
  async discoverHandler(message, peer) {
@@ -2165,15 +2097,11 @@ class Peernet {
2165
2097
 
2166
2098
  pubsub.subscribe('peer:discovered', async (peer) => {
2167
2099
  this._peerHandler.discover(peer);
2168
-
2169
2100
  peer.on('peernet.data', async (message) => {
2170
2101
  const id = message.id;
2171
- message = new peernet.protos['peernet-message'](Buffer.from(message.data.data));
2172
- console.log(message);
2173
- // message = new PeernetMessage(Buffer.from(message.data.data))
2102
+ message = new PeernetMessage(Buffer.from(message.data.data));
2174
2103
  const proto = protoFor(message.decoded.data);
2175
2104
  await this._protoHandler({id, proto}, peer);
2176
-
2177
2105
  const fulldId = this._getPeerId(peer.id);
2178
2106
  if (fulldId && this._discovered.indexOf(peer.id) === -1) {
2179
2107
  this._discovered.push(peer.id);
@@ -2181,7 +2109,6 @@ class Peernet {
2181
2109
  }
2182
2110
  });
2183
2111
  });
2184
-
2185
2112
  pubsub.subscribe('peer:disconnected', async (peer) => {
2186
2113
  let index = this._discovered.indexOf(peer.id);
2187
2114
  if (index !== -1) this._discovered.splice(index, 1);
@@ -2242,40 +2169,40 @@ class Peernet {
2242
2169
  this.peerMap.set(from, connections);
2243
2170
  }
2244
2171
  }
2245
- const data = new peernet.protos['peernet-peer-response']({id: this.id});
2246
- const node = await this.prepareMessage(peer.id, data.encoded);
2172
+ const data = new PeerMessageResponse({id: this.id});
2173
+ const node = await this.prepareMessage(from, data.encoded);
2247
2174
 
2248
2175
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2249
- // } else if (proto.name === 'peernet-peer-response') {
2250
- // const from = proto.decoded.id
2251
- // if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
2252
- // else {
2253
- // const connections = this.peerMap.get(from)
2254
- // if (connections.indexOf(peer.id) === -1) {
2255
- // connections.push(peer.id)
2256
- // this.peerMap.set(from, connections)
2257
- // }
2258
- // }
2176
+ } else if (proto.name === 'peernet-peer-response') {
2177
+ const from = proto.decoded.id;
2178
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2179
+ else {
2180
+ const connections = this.peerMap.get(from);
2181
+ if (connections.indexOf(peer.id) === -1) {
2182
+ connections.push(peer.id);
2183
+ this.peerMap.set(from, connections);
2184
+ }
2185
+ }
2259
2186
  } else {
2260
- const from = this._getPeerId(peer.id);
2261
- // if (!from) {
2262
- // const data = new PeerMessage({id: this.id})
2263
- // const node = await this.prepareMessage(peer.id, data.encoded)
2264
- //
2265
- // let response = await peer.request(node.encoded)
2266
- // response = protoFor(response)
2267
- // response = new PeerMessageResponse(response.decoded.data)
2268
- //
2269
- // from = response.decoded.id
2270
- // if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
2271
- // else {
2272
- // const connections = this.peerMap.get(from)
2273
- // if (connections.indexOf(peer.id) === -1) {
2274
- // connections.push(peer.id)
2275
- // this.peerMap.set(from, connections)
2276
- // }
2277
- // }
2278
- // }
2187
+ let from = this._getPeerId(peer.id);
2188
+ if (!from) {
2189
+ const data = new PeerMessage({id: this.id});
2190
+ const node = await this.prepareMessage(peer.id, data.encoded);
2191
+
2192
+ let response = await peer.request(node.encoded);
2193
+ response = protoFor(response);
2194
+ response = new PeerMessageResponse(response.decoded.data);
2195
+
2196
+ from = response.decoded.id;
2197
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2198
+ else {
2199
+ const connections = this.peerMap.get(from);
2200
+ if (connections.indexOf(peer.id) === -1) {
2201
+ connections.push(peer.id);
2202
+ this.peerMap.set(from, connections);
2203
+ }
2204
+ }
2205
+ }
2279
2206
  if (proto.name === 'peernet-dht') {
2280
2207
  let { hash, store } = proto.decoded;
2281
2208
  let has;
@@ -2311,6 +2238,18 @@ class Peernet {
2311
2238
  const node = await this.prepareMessage(from, data.encoded);
2312
2239
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2313
2240
  }
2241
+ } else if (proto.name === 'peernet-peer') {
2242
+ const from = proto.decoded.id;
2243
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2244
+ else {
2245
+ const connections = this.peerMap.get(from);
2246
+ connections.push(peer.id);
2247
+ this.peerMap.set(from, connections);
2248
+ }
2249
+ const data = new PeerMessage({id: this.id});
2250
+ const node = await this.prepareMessage(from, data.encoded);
2251
+
2252
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2314
2253
  } else if (proto.name === 'peernet-request') {
2315
2254
  // TODO: make dynamic
2316
2255
  // exposeddevapi[proto.decoded.request](proto.decoded.params)
@@ -89,10 +89,11 @@ globalThis.pubsub = globalThis.pubsub || new PubSub__default['default']({verbose
89
89
  class PeernetClient {
90
90
  constructor(options = {}) {
91
91
  if (!options.id) options.id = Buffer.from('00000000000000000000000000000000');
92
-
92
+ if (!options.networkVersion) options.networkVersion = 'v0.1.0';
93
+ if (!options.networkName) options.networkName = 'peernet';
93
94
  this.id = options.id;
94
95
 
95
- this.topic = Buffer.from(sha256__default['default']('peernet-v0.1.0').toString());
96
+ this.topic = Buffer.from(sha256__default['default'](`${option.networkName}-${options.networkVersion}`).toString());
96
97
 
97
98
  const trackers = [
98
99
  'wss://star.leofcoin.org:7575',
@@ -114,71 +115,6 @@ class PeernetClient {
114
115
 
115
116
  this.p2p.start();
116
117
 
117
-
118
- // this.sw.join(this.topic.slice(0, 32))
119
- //
120
- // const arrayOfPeers = this.sw.getPeers()
121
- //
122
- // this.sw.on('connection', async (connection, info) => {
123
- // // connected
124
- // const id = info.id.toString()
125
- // if (id === this.id.toString()) return
126
- //
127
- // const channel = connection.channelName
128
- // let channels = [channel]
129
- // let peer
130
- // if (connections.has(id)) {
131
- // const value = connections.get(id)
132
- // const _channels = value.channels
133
- // channels = [...channels, ..._channels]
134
- // peer = value.peer
135
- // } else {
136
- // peer = new PeernetPeer(id, connection)
137
- // pubsub.publish('peer:connected', peer)
138
- // }
139
- // connections.set(id, {channels, peer})
140
- // // connection.on('data', (data) => console.log({data}))
141
- // // connection.write('peerId')
142
- // });
143
- //
144
- // this.sw.on('candidates-updated', async (channel, candidates) => {
145
- // // TODO: Channels ...
146
- // for (const candidate of candidates) {
147
- // const id = candidate.toString()
148
- // let channels = [channel]
149
- // if (id === this.id.toString()) return
150
- //
151
- // if (!connections.has(id)) {
152
- // try {
153
- // const connection = await this.sw.connect(channel, candidate)
154
- // const peer = new PeernetPeer(id, connection)
155
- // connections.set(id, {channels, peer})
156
- // if (recentConnections.has(id)) {
157
- // setTimeout(() => {
158
- // pubsub.publish('peer:connected', peer)
159
- // }, 1000)
160
- // } else {
161
- // pubsub.publish('peer:connected', peer)
162
- // }
163
- // } catch (e) {
164
- // console.warn(e);
165
- // }
166
- // } else {
167
- // const value = connections.get(id)
168
- // const _channels = value.channels
169
- // channels = [...channels, ..._channels]
170
- // connections.set(id, {channels, peer: value.peer})
171
- // }
172
- // }
173
- // })
174
- //
175
- // this.sw.on('connection-closed', (connection, info) => {
176
- // if (!recentConnections.has(info.id.toString())) {
177
- // recentConnections.set(info.id.toString(), new Date().getTime())
178
- // }
179
- // if (connections.has(info.id.toString())) connections.delete(info.id.toString())
180
- // })
181
- //
182
118
  if (globalThis.process) {
183
119
  process.on('SIGINT', async () => {
184
120
  console.log('Caught interrupt signal');
@@ -328,7 +264,7 @@ class LeofcoinStorage$1 {
328
264
 
329
265
  }
330
266
 
331
- var version = "0.8.6";
267
+ var version = "0.8.9";
332
268
 
333
269
  var api$1 = {
334
270
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -1169,14 +1105,10 @@ class PeerDiscovery {
1169
1105
  const connections = peernet.peerMap.get(id);
1170
1106
  if (connections.indexOf(peer.id) === -1) {
1171
1107
  connections.push(peer.id);
1172
- peernet.peerMap.set(id, connections);
1108
+ peernet.peerMap.set(from, connections);
1173
1109
  }
1174
1110
  }
1175
- return {peer, id}
1176
- }
1177
-
1178
- async _cleanedUpIfNeeded() {
1179
-
1111
+ return id
1180
1112
  }
1181
1113
 
1182
1114
  async discoverHandler(message, peer) {
@@ -1605,15 +1537,11 @@ class Peernet {
1605
1537
 
1606
1538
  pubsub.subscribe('peer:discovered', async (peer) => {
1607
1539
  this._peerHandler.discover(peer);
1608
-
1609
1540
  peer.on('peernet.data', async (message) => {
1610
1541
  const id = message.id;
1611
- message = new peernet.protos['peernet-message'](Buffer.from(message.data.data));
1612
- console.log(message);
1613
- // message = new PeernetMessage(Buffer.from(message.data.data))
1542
+ message = new peernetMessage(Buffer.from(message.data.data));
1614
1543
  const proto = protoFor(message.decoded.data);
1615
1544
  await this._protoHandler({id, proto}, peer);
1616
-
1617
1545
  const fulldId = this._getPeerId(peer.id);
1618
1546
  if (fulldId && this._discovered.indexOf(peer.id) === -1) {
1619
1547
  this._discovered.push(peer.id);
@@ -1621,7 +1549,6 @@ class Peernet {
1621
1549
  }
1622
1550
  });
1623
1551
  });
1624
-
1625
1552
  pubsub.subscribe('peer:disconnected', async (peer) => {
1626
1553
  let index = this._discovered.indexOf(peer.id);
1627
1554
  if (index !== -1) this._discovered.splice(index, 1);
@@ -1682,40 +1609,40 @@ class Peernet {
1682
1609
  this.peerMap.set(from, connections);
1683
1610
  }
1684
1611
  }
1685
- const data = new peernet.protos['peernet-peer-response']({id: this.id});
1686
- const node = await this.prepareMessage(peer.id, data.encoded);
1612
+ const data = new PeerMessageResponse({id: this.id});
1613
+ const node = await this.prepareMessage(from, data.encoded);
1687
1614
 
1688
1615
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
1689
- // } else if (proto.name === 'peernet-peer-response') {
1690
- // const from = proto.decoded.id
1691
- // if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
1692
- // else {
1693
- // const connections = this.peerMap.get(from)
1694
- // if (connections.indexOf(peer.id) === -1) {
1695
- // connections.push(peer.id)
1696
- // this.peerMap.set(from, connections)
1697
- // }
1698
- // }
1616
+ } else if (proto.name === 'peernet-peer-response') {
1617
+ const from = proto.decoded.id;
1618
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
1619
+ else {
1620
+ const connections = this.peerMap.get(from);
1621
+ if (connections.indexOf(peer.id) === -1) {
1622
+ connections.push(peer.id);
1623
+ this.peerMap.set(from, connections);
1624
+ }
1625
+ }
1699
1626
  } else {
1700
- const from = this._getPeerId(peer.id);
1701
- // if (!from) {
1702
- // const data = new PeerMessage({id: this.id})
1703
- // const node = await this.prepareMessage(peer.id, data.encoded)
1704
- //
1705
- // let response = await peer.request(node.encoded)
1706
- // response = protoFor(response)
1707
- // response = new PeerMessageResponse(response.decoded.data)
1708
- //
1709
- // from = response.decoded.id
1710
- // if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
1711
- // else {
1712
- // const connections = this.peerMap.get(from)
1713
- // if (connections.indexOf(peer.id) === -1) {
1714
- // connections.push(peer.id)
1715
- // this.peerMap.set(from, connections)
1716
- // }
1717
- // }
1718
- // }
1627
+ let from = this._getPeerId(peer.id);
1628
+ if (!from) {
1629
+ const data = new PeerMessage({id: this.id});
1630
+ const node = await this.prepareMessage(peer.id, data.encoded);
1631
+
1632
+ let response = await peer.request(node.encoded);
1633
+ response = protoFor(response);
1634
+ response = new PeerMessageResponse(response.decoded.data);
1635
+
1636
+ from = response.decoded.id;
1637
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
1638
+ else {
1639
+ const connections = this.peerMap.get(from);
1640
+ if (connections.indexOf(peer.id) === -1) {
1641
+ connections.push(peer.id);
1642
+ this.peerMap.set(from, connections);
1643
+ }
1644
+ }
1645
+ }
1719
1646
  if (proto.name === 'peernet-dht') {
1720
1647
  let { hash, store } = proto.decoded;
1721
1648
  let has;
@@ -1751,6 +1678,18 @@ class Peernet {
1751
1678
  const node = await this.prepareMessage(from, data.encoded);
1752
1679
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
1753
1680
  }
1681
+ } else if (proto.name === 'peernet-peer') {
1682
+ const from = proto.decoded.id;
1683
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
1684
+ else {
1685
+ const connections = this.peerMap.get(from);
1686
+ connections.push(peer.id);
1687
+ this.peerMap.set(from, connections);
1688
+ }
1689
+ const data = new PeerMessage({id: this.id});
1690
+ const node = await this.prepareMessage(from, data.encoded);
1691
+
1692
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
1754
1693
  } else if (proto.name === 'peernet-request') {
1755
1694
  // TODO: make dynamic
1756
1695
  // exposeddevapi[proto.decoded.request](proto.decoded.params)
@@ -69,10 +69,11 @@ globalThis.pubsub = globalThis.pubsub || new PubSub({verbose: false});
69
69
  class PeernetClient {
70
70
  constructor(options = {}) {
71
71
  if (!options.id) options.id = Buffer.from('00000000000000000000000000000000');
72
-
72
+ if (!options.networkVersion) options.networkVersion = 'v0.1.0';
73
+ if (!options.networkName) options.networkName = 'peernet';
73
74
  this.id = options.id;
74
75
 
75
- this.topic = Buffer.from(sha256('peernet-v0.1.0').toString());
76
+ this.topic = Buffer.from(sha256(`${option.networkName}-${options.networkVersion}`).toString());
76
77
 
77
78
  const trackers = [
78
79
  'wss://star.leofcoin.org:7575',
@@ -94,71 +95,6 @@ class PeernetClient {
94
95
 
95
96
  this.p2p.start();
96
97
 
97
-
98
- // this.sw.join(this.topic.slice(0, 32))
99
- //
100
- // const arrayOfPeers = this.sw.getPeers()
101
- //
102
- // this.sw.on('connection', async (connection, info) => {
103
- // // connected
104
- // const id = info.id.toString()
105
- // if (id === this.id.toString()) return
106
- //
107
- // const channel = connection.channelName
108
- // let channels = [channel]
109
- // let peer
110
- // if (connections.has(id)) {
111
- // const value = connections.get(id)
112
- // const _channels = value.channels
113
- // channels = [...channels, ..._channels]
114
- // peer = value.peer
115
- // } else {
116
- // peer = new PeernetPeer(id, connection)
117
- // pubsub.publish('peer:connected', peer)
118
- // }
119
- // connections.set(id, {channels, peer})
120
- // // connection.on('data', (data) => console.log({data}))
121
- // // connection.write('peerId')
122
- // });
123
- //
124
- // this.sw.on('candidates-updated', async (channel, candidates) => {
125
- // // TODO: Channels ...
126
- // for (const candidate of candidates) {
127
- // const id = candidate.toString()
128
- // let channels = [channel]
129
- // if (id === this.id.toString()) return
130
- //
131
- // if (!connections.has(id)) {
132
- // try {
133
- // const connection = await this.sw.connect(channel, candidate)
134
- // const peer = new PeernetPeer(id, connection)
135
- // connections.set(id, {channels, peer})
136
- // if (recentConnections.has(id)) {
137
- // setTimeout(() => {
138
- // pubsub.publish('peer:connected', peer)
139
- // }, 1000)
140
- // } else {
141
- // pubsub.publish('peer:connected', peer)
142
- // }
143
- // } catch (e) {
144
- // console.warn(e);
145
- // }
146
- // } else {
147
- // const value = connections.get(id)
148
- // const _channels = value.channels
149
- // channels = [...channels, ..._channels]
150
- // connections.set(id, {channels, peer: value.peer})
151
- // }
152
- // }
153
- // })
154
- //
155
- // this.sw.on('connection-closed', (connection, info) => {
156
- // if (!recentConnections.has(info.id.toString())) {
157
- // recentConnections.set(info.id.toString(), new Date().getTime())
158
- // }
159
- // if (connections.has(info.id.toString())) connections.delete(info.id.toString())
160
- // })
161
- //
162
98
  if (globalThis.process) {
163
99
  process.on('SIGINT', async () => {
164
100
  console.log('Caught interrupt signal');
@@ -308,7 +244,7 @@ class LeofcoinStorage$1 {
308
244
 
309
245
  }
310
246
 
311
- var version = "0.8.6";
247
+ var version = "0.8.9";
312
248
 
313
249
  var api$1 = {
314
250
  version: ({send}) => send({client: '@peernet/api/http', version}),
@@ -1712,14 +1648,10 @@ class PeerDiscovery {
1712
1648
  const connections = peernet.peerMap.get(id);
1713
1649
  if (connections.indexOf(peer.id) === -1) {
1714
1650
  connections.push(peer.id);
1715
- peernet.peerMap.set(id, connections);
1651
+ peernet.peerMap.set(from, connections);
1716
1652
  }
1717
1653
  }
1718
- return {peer, id}
1719
- }
1720
-
1721
- async _cleanedUpIfNeeded() {
1722
-
1654
+ return id
1723
1655
  }
1724
1656
 
1725
1657
  async discoverHandler(message, peer) {
@@ -2148,15 +2080,11 @@ class Peernet {
2148
2080
 
2149
2081
  pubsub.subscribe('peer:discovered', async (peer) => {
2150
2082
  this._peerHandler.discover(peer);
2151
-
2152
2083
  peer.on('peernet.data', async (message) => {
2153
2084
  const id = message.id;
2154
- message = new peernet.protos['peernet-message'](Buffer.from(message.data.data));
2155
- console.log(message);
2156
- // message = new PeernetMessage(Buffer.from(message.data.data))
2085
+ message = new PeernetMessage(Buffer.from(message.data.data));
2157
2086
  const proto = protoFor(message.decoded.data);
2158
2087
  await this._protoHandler({id, proto}, peer);
2159
-
2160
2088
  const fulldId = this._getPeerId(peer.id);
2161
2089
  if (fulldId && this._discovered.indexOf(peer.id) === -1) {
2162
2090
  this._discovered.push(peer.id);
@@ -2164,7 +2092,6 @@ class Peernet {
2164
2092
  }
2165
2093
  });
2166
2094
  });
2167
-
2168
2095
  pubsub.subscribe('peer:disconnected', async (peer) => {
2169
2096
  let index = this._discovered.indexOf(peer.id);
2170
2097
  if (index !== -1) this._discovered.splice(index, 1);
@@ -2225,40 +2152,40 @@ class Peernet {
2225
2152
  this.peerMap.set(from, connections);
2226
2153
  }
2227
2154
  }
2228
- const data = new peernet.protos['peernet-peer-response']({id: this.id});
2229
- const node = await this.prepareMessage(peer.id, data.encoded);
2155
+ const data = new PeerMessageResponse({id: this.id});
2156
+ const node = await this.prepareMessage(from, data.encoded);
2230
2157
 
2231
2158
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2232
- // } else if (proto.name === 'peernet-peer-response') {
2233
- // const from = proto.decoded.id
2234
- // if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
2235
- // else {
2236
- // const connections = this.peerMap.get(from)
2237
- // if (connections.indexOf(peer.id) === -1) {
2238
- // connections.push(peer.id)
2239
- // this.peerMap.set(from, connections)
2240
- // }
2241
- // }
2159
+ } else if (proto.name === 'peernet-peer-response') {
2160
+ const from = proto.decoded.id;
2161
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2162
+ else {
2163
+ const connections = this.peerMap.get(from);
2164
+ if (connections.indexOf(peer.id) === -1) {
2165
+ connections.push(peer.id);
2166
+ this.peerMap.set(from, connections);
2167
+ }
2168
+ }
2242
2169
  } else {
2243
- const from = this._getPeerId(peer.id);
2244
- // if (!from) {
2245
- // const data = new PeerMessage({id: this.id})
2246
- // const node = await this.prepareMessage(peer.id, data.encoded)
2247
- //
2248
- // let response = await peer.request(node.encoded)
2249
- // response = protoFor(response)
2250
- // response = new PeerMessageResponse(response.decoded.data)
2251
- //
2252
- // from = response.decoded.id
2253
- // if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id])
2254
- // else {
2255
- // const connections = this.peerMap.get(from)
2256
- // if (connections.indexOf(peer.id) === -1) {
2257
- // connections.push(peer.id)
2258
- // this.peerMap.set(from, connections)
2259
- // }
2260
- // }
2261
- // }
2170
+ let from = this._getPeerId(peer.id);
2171
+ if (!from) {
2172
+ const data = new PeerMessage({id: this.id});
2173
+ const node = await this.prepareMessage(peer.id, data.encoded);
2174
+
2175
+ let response = await peer.request(node.encoded);
2176
+ response = protoFor(response);
2177
+ response = new PeerMessageResponse(response.decoded.data);
2178
+
2179
+ from = response.decoded.id;
2180
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2181
+ else {
2182
+ const connections = this.peerMap.get(from);
2183
+ if (connections.indexOf(peer.id) === -1) {
2184
+ connections.push(peer.id);
2185
+ this.peerMap.set(from, connections);
2186
+ }
2187
+ }
2188
+ }
2262
2189
  if (proto.name === 'peernet-dht') {
2263
2190
  let { hash, store } = proto.decoded;
2264
2191
  let has;
@@ -2294,6 +2221,18 @@ class Peernet {
2294
2221
  const node = await this.prepareMessage(from, data.encoded);
2295
2222
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2296
2223
  }
2224
+ } else if (proto.name === 'peernet-peer') {
2225
+ const from = proto.decoded.id;
2226
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2227
+ else {
2228
+ const connections = this.peerMap.get(from);
2229
+ connections.push(peer.id);
2230
+ this.peerMap.set(from, connections);
2231
+ }
2232
+ const data = new PeerMessage({id: this.id});
2233
+ const node = await this.prepareMessage(from, data.encoded);
2234
+
2235
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2297
2236
  } else if (proto.name === 'peernet-request') {
2298
2237
  // TODO: make dynamic
2299
2238
  // exposeddevapi[proto.decoded.request](proto.decoded.params)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.8.7",
3
+ "version": "0.8.9",
4
4
  "description": "",
5
5
  "main": "dist/commonjs/peernet.js",
6
6
  "module": "dist/module/peernet.js",