@leofcoin/peernet 0.8.7 → 0.8.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.
@@ -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'](`${options.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.10";
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) {
@@ -1535,8 +1467,14 @@ class Peernet {
1535
1467
  * @property {Object} peer Instance of Peer
1536
1468
  */
1537
1469
  this.dht = new DhtEarth();
1470
+ /**
1471
+ * @type {Map}
1472
+ * @property {Object} peer Instance of Peer
1473
+ */
1538
1474
  this.peerMap = new Map();
1539
1475
  this.stores = [];
1476
+ this.storePrefix = options.storePrefix;
1477
+ this.root = options.root;
1540
1478
 
1541
1479
  /**
1542
1480
  * proto Object containing protos
@@ -1563,7 +1501,6 @@ class Peernet {
1563
1501
 
1564
1502
  this._messageHandler = new MessageHandler(this.network);
1565
1503
 
1566
-
1567
1504
  const {daemon, environment} = await target();
1568
1505
  this.hasDaemon = daemon;
1569
1506
 
@@ -1605,15 +1542,11 @@ class Peernet {
1605
1542
 
1606
1543
  pubsub.subscribe('peer:discovered', async (peer) => {
1607
1544
  this._peerHandler.discover(peer);
1608
-
1609
1545
  peer.on('peernet.data', async (message) => {
1610
1546
  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))
1547
+ message = new peernetMessage(Buffer.from(message.data.data));
1614
1548
  const proto = protoFor(message.decoded.data);
1615
1549
  await this._protoHandler({id, proto}, peer);
1616
-
1617
1550
  const fulldId = this._getPeerId(peer.id);
1618
1551
  if (fulldId && this._discovered.indexOf(peer.id) === -1) {
1619
1552
  this._discovered.push(peer.id);
@@ -1621,7 +1554,6 @@ class Peernet {
1621
1554
  }
1622
1555
  });
1623
1556
  });
1624
-
1625
1557
  pubsub.subscribe('peer:disconnected', async (peer) => {
1626
1558
  let index = this._discovered.indexOf(peer.id);
1627
1559
  if (index !== -1) this._discovered.splice(index, 1);
@@ -1682,40 +1614,40 @@ class Peernet {
1682
1614
  this.peerMap.set(from, connections);
1683
1615
  }
1684
1616
  }
1685
- const data = new peernet.protos['peernet-peer-response']({id: this.id});
1686
- const node = await this.prepareMessage(peer.id, data.encoded);
1617
+ const data = new PeerMessageResponse({id: this.id});
1618
+ const node = await this.prepareMessage(from, data.encoded);
1687
1619
 
1688
1620
  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
- // }
1621
+ } else if (proto.name === 'peernet-peer-response') {
1622
+ const from = proto.decoded.id;
1623
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
1624
+ else {
1625
+ const connections = this.peerMap.get(from);
1626
+ if (connections.indexOf(peer.id) === -1) {
1627
+ connections.push(peer.id);
1628
+ this.peerMap.set(from, connections);
1629
+ }
1630
+ }
1699
1631
  } 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
- // }
1632
+ let from = this._getPeerId(peer.id);
1633
+ if (!from) {
1634
+ const data = new PeerMessage({id: this.id});
1635
+ const node = await this.prepareMessage(peer.id, data.encoded);
1636
+
1637
+ let response = await peer.request(node.encoded);
1638
+ response = protoFor(response);
1639
+ response = new PeerMessageResponse(response.decoded.data);
1640
+
1641
+ from = response.decoded.id;
1642
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
1643
+ else {
1644
+ const connections = this.peerMap.get(from);
1645
+ if (connections.indexOf(peer.id) === -1) {
1646
+ connections.push(peer.id);
1647
+ this.peerMap.set(from, connections);
1648
+ }
1649
+ }
1650
+ }
1719
1651
  if (proto.name === 'peernet-dht') {
1720
1652
  let { hash, store } = proto.decoded;
1721
1653
  let has;
@@ -1751,6 +1683,18 @@ class Peernet {
1751
1683
  const node = await this.prepareMessage(from, data.encoded);
1752
1684
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
1753
1685
  }
1686
+ } else if (proto.name === 'peernet-peer') {
1687
+ const from = proto.decoded.id;
1688
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
1689
+ else {
1690
+ const connections = this.peerMap.get(from);
1691
+ connections.push(peer.id);
1692
+ this.peerMap.set(from, connections);
1693
+ }
1694
+ const data = new PeerMessage({id: this.id});
1695
+ const node = await this.prepareMessage(from, data.encoded);
1696
+
1697
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
1754
1698
  } else if (proto.name === 'peernet-request') {
1755
1699
  // TODO: make dynamic
1756
1700
  // 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(`${options.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.10";
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) {
@@ -2078,8 +2010,14 @@ class Peernet {
2078
2010
  * @property {Object} peer Instance of Peer
2079
2011
  */
2080
2012
  this.dht = new DhtEarth();
2013
+ /**
2014
+ * @type {Map}
2015
+ * @property {Object} peer Instance of Peer
2016
+ */
2081
2017
  this.peerMap = new Map();
2082
2018
  this.stores = [];
2019
+ this.storePrefix = options.storePrefix;
2020
+ this.root = options.root;
2083
2021
 
2084
2022
  /**
2085
2023
  * proto Object containing protos
@@ -2106,7 +2044,6 @@ class Peernet {
2106
2044
 
2107
2045
  this._messageHandler = new MessageHandler(this.network);
2108
2046
 
2109
-
2110
2047
  const {daemon, environment} = await target();
2111
2048
  this.hasDaemon = daemon;
2112
2049
 
@@ -2148,15 +2085,11 @@ class Peernet {
2148
2085
 
2149
2086
  pubsub.subscribe('peer:discovered', async (peer) => {
2150
2087
  this._peerHandler.discover(peer);
2151
-
2152
2088
  peer.on('peernet.data', async (message) => {
2153
2089
  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))
2090
+ message = new PeernetMessage(Buffer.from(message.data.data));
2157
2091
  const proto = protoFor(message.decoded.data);
2158
2092
  await this._protoHandler({id, proto}, peer);
2159
-
2160
2093
  const fulldId = this._getPeerId(peer.id);
2161
2094
  if (fulldId && this._discovered.indexOf(peer.id) === -1) {
2162
2095
  this._discovered.push(peer.id);
@@ -2164,7 +2097,6 @@ class Peernet {
2164
2097
  }
2165
2098
  });
2166
2099
  });
2167
-
2168
2100
  pubsub.subscribe('peer:disconnected', async (peer) => {
2169
2101
  let index = this._discovered.indexOf(peer.id);
2170
2102
  if (index !== -1) this._discovered.splice(index, 1);
@@ -2225,40 +2157,40 @@ class Peernet {
2225
2157
  this.peerMap.set(from, connections);
2226
2158
  }
2227
2159
  }
2228
- const data = new peernet.protos['peernet-peer-response']({id: this.id});
2229
- const node = await this.prepareMessage(peer.id, data.encoded);
2160
+ const data = new PeerMessageResponse({id: this.id});
2161
+ const node = await this.prepareMessage(from, data.encoded);
2230
2162
 
2231
2163
  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
- // }
2164
+ } else if (proto.name === 'peernet-peer-response') {
2165
+ const from = proto.decoded.id;
2166
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2167
+ else {
2168
+ const connections = this.peerMap.get(from);
2169
+ if (connections.indexOf(peer.id) === -1) {
2170
+ connections.push(peer.id);
2171
+ this.peerMap.set(from, connections);
2172
+ }
2173
+ }
2242
2174
  } 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
- // }
2175
+ let from = this._getPeerId(peer.id);
2176
+ if (!from) {
2177
+ const data = new PeerMessage({id: this.id});
2178
+ const node = await this.prepareMessage(peer.id, data.encoded);
2179
+
2180
+ let response = await peer.request(node.encoded);
2181
+ response = protoFor(response);
2182
+ response = new PeerMessageResponse(response.decoded.data);
2183
+
2184
+ from = response.decoded.id;
2185
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2186
+ else {
2187
+ const connections = this.peerMap.get(from);
2188
+ if (connections.indexOf(peer.id) === -1) {
2189
+ connections.push(peer.id);
2190
+ this.peerMap.set(from, connections);
2191
+ }
2192
+ }
2193
+ }
2262
2194
  if (proto.name === 'peernet-dht') {
2263
2195
  let { hash, store } = proto.decoded;
2264
2196
  let has;
@@ -2294,6 +2226,18 @@ class Peernet {
2294
2226
  const node = await this.prepareMessage(from, data.encoded);
2295
2227
  peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2296
2228
  }
2229
+ } else if (proto.name === 'peernet-peer') {
2230
+ const from = proto.decoded.id;
2231
+ if (!this.peerMap.has(from)) this.peerMap.set(from, [peer.id]);
2232
+ else {
2233
+ const connections = this.peerMap.get(from);
2234
+ connections.push(peer.id);
2235
+ this.peerMap.set(from, connections);
2236
+ }
2237
+ const data = new PeerMessage({id: this.id});
2238
+ const node = await this.prepareMessage(from, data.encoded);
2239
+
2240
+ peer.write(Buffer.from(JSON.stringify({id, data: node.encoded})));
2297
2241
  } else if (proto.name === 'peernet-request') {
2298
2242
  // TODO: make dynamic
2299
2243
  // 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.11",
4
4
  "description": "",
5
5
  "main": "dist/commonjs/peernet.js",
6
6
  "module": "dist/module/peernet.js",
package/test/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  require('./messages')
2
2
  require('./codec')
3
- require('./peernet')
3
+ // require('./peernet')
4
4
  // require('./hash')