holepunch-hop 0.3.9 → 0.4.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "holepunch-hop",
3
- "version": "0.3.9",
3
+ "version": "0.4.0",
4
4
  "description": "data interface to HOP",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/bees.js CHANGED
@@ -875,7 +875,7 @@ class HyperBee extends EventEmitter {
875
875
  */
876
876
  replicatePubliclibrary = async function (dataIn) {
877
877
  // create or get the hypercore using the public key supplied as command-line argument
878
- const coreRep = this.store.get({ key: b4a.from(dataIn.data.datastores, 'hex') })
878
+ const coreRep = this.store.get({ key: b4a.from(dataIn.data.data.datastores, 'hex') })
879
879
 
880
880
  // create a hyperbee instance using the hypercore instance
881
881
  const beePlib = new Hyperbee(coreRep, {
@@ -895,7 +895,7 @@ class HyperBee extends EventEmitter {
895
895
  await coreRep.update()
896
896
 
897
897
  // if provided with specific boarnxp key then just get the contract, extract module contracts and get those contracts and then inform the peer and save to their public library
898
- const boardNXPcontract = await beePlib.get(dataIn.data.boardID)
898
+ const boardNXPcontract = await beePlib.get(dataIn.data.data.boardID)
899
899
  let unString = JSON.parse(boardNXPcontract.value)
900
900
  let moduleContracts = []
901
901
  for (let mod of unString.modules) {
@@ -934,8 +934,8 @@ class HyperBee extends EventEmitter {
934
934
  holderConfirm.boardNXP = [boardNXPcontract]
935
935
  holderConfirm.modules = moduleContracts
936
936
  holderConfirm.refcontracts = referenceContracts
937
- this.confirmPubLibList[dataIn.data.datastores] = holderConfirm
938
- this.emit('publibbeebee-notification', dataIn.data)
937
+ this.confirmPubLibList[dataIn.data.data.datastores] = holderConfirm
938
+ this.emit('publibbeebee-notification', dataIn)
939
939
  }
940
940
  // or
941
941
  // now ask for whole of public library
package/src/index.js CHANGED
@@ -52,6 +52,16 @@ class HolepunchWorker extends EventEmitter {
52
52
  // make replication possible
53
53
  this.swarm.on('connection', conn => this.store.replicate(conn))
54
54
  goodbye(() => this.swarm.destroy())
55
+
56
+ this.swarm.on('update', () => {
57
+ // this.Peers.updateListen(data)
58
+ console.log('update-------')
59
+ console.log(this.swarm.connecting)
60
+ console.log(this.swarm.connections.size)
61
+ console.log(this.swarm.peers.size)
62
+ })
63
+
64
+
55
65
  this.BeeData = new BeeWorker(this.store, this.swarm)
56
66
  this.DriveFiles = new DriveWorker(this.store, this.swarm)
57
67
  this.Peers = new PeerWorker(this.store, this.swarm)
@@ -134,8 +144,8 @@ class HolepunchWorker extends EventEmitter {
134
144
  this.Peers.on('cuespace-notification', (data) => {
135
145
  this.emit('peer-cuespace', data)
136
146
  // check if bentoboxN1 included?
137
- if (data.data.content.bbn1.publicN1contracts.length > 0) {
138
- for (let n1Cont of data.data.content.bbn1.publicN1contracts) {
147
+ if (data.data.data.content.bbn1.publicN1contracts.length > 0) {
148
+ for (let n1Cont of data.data.data.content.bbn1.publicN1contracts) {
139
149
  this.BeeData.replicatePubliclibrary({ data: n1Cont })
140
150
  }
141
151
  }
@@ -181,6 +191,9 @@ class HolepunchWorker extends EventEmitter {
181
191
  peerLive.publickey = data
182
192
  this.emit('peer-live-notify', peerLive)
183
193
  })
194
+ this.Peers.on('peer-disconnect', (data) => {
195
+ this.emit('peer-disconnect-notify', data)
196
+ })
184
197
  // drive listener
185
198
  this.DriveFiles.on('largefile-save', (data) => {
186
199
  this.emit('drive-save-large', data)
@@ -192,7 +205,7 @@ class HolepunchWorker extends EventEmitter {
192
205
  * @method networkPath
193
206
  *
194
207
  */
195
- networkPath = function (message) {
208
+ networkPath = async function (message) {
196
209
  if (message.action === 'share') {
197
210
  // check if joined now?
198
211
  let reEstablishShort = this.Peers.checkConnectivityStatus(message, this.warmPeers, 'invite-gen')
@@ -270,6 +283,16 @@ class HolepunchWorker extends EventEmitter {
270
283
  this.Peers.routeDataPath(reEstablishShort.peer.value.livePeerkey, peerActionData.data)
271
284
  }
272
285
  }
286
+ } else if (message.action === 'retry') {
287
+ console.log('retry')
288
+ let peerDefaults = this.Peers.peerMatchTopic(message.data.key)
289
+ this.Peers.discoveryMatch(message.data.key)
290
+ } else if (message.action === 'peer-closed') {
291
+ console.log('distroy swarm connections')
292
+ console.log(message)
293
+ this.flushConnections()
294
+ await this.swarm.destroy()
295
+ // this.flushConnections()
273
296
  }
274
297
  }
275
298
 
@@ -417,6 +440,21 @@ class HolepunchWorker extends EventEmitter {
417
440
  this.readcore = lastBlock
418
441
  }
419
442
 
443
+ /**
444
+ * refresh connection i.e. which peers are live
445
+ * @method flushConnections
446
+ *
447
+ */
448
+ flushConnections = async function () {
449
+ console.log('flush connections')
450
+ console.log(this.swarm.connections.size)
451
+ console.log(this.swarm.connecting)
452
+ console.log(this.swarm.peers.size)
453
+ await this.swarm.flush()
454
+ await Promise.all(Array.from(this.swarm.connections).map(e => e.flush()))
455
+ await new Promise(resolve => setImmediate(resolve))
456
+ }
457
+
420
458
  }
421
459
 
422
460
  export default HolepunchWorker
package/src/peers.js CHANGED
@@ -27,6 +27,7 @@ class NetworkPeers extends EventEmitter {
27
27
  this.topicHolder = {}
28
28
  this.sendTopicHolder = []
29
29
  this.peersRole = []
30
+ this.discoveryList = []
30
31
  }
31
32
 
32
33
  /**
@@ -226,11 +227,41 @@ class NetworkPeers extends EventEmitter {
226
227
  // assess data
227
228
  this.assessData(publicKeylive, data)
228
229
  )
229
- //conn.end()
230
+
231
+ conn.on('error', data => {
232
+ let connectLivekeys = Object.keys(this.peerConnect)
233
+ // console.log(this.peerNetwork)
234
+ for (let peer of this.peerNetwork) {
235
+ for (let pconn of connectLivekeys) {
236
+ if (peer.value.livePeerkey === pconn) {
237
+ // check if connect is close?
238
+ let keysNoise = Object.keys(this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed'])
239
+ // console.log(this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed'])
240
+ let closeStatus = this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed']
241
+ if (closeStatus === true) {
242
+ // remove peer & inform beebee
243
+ this.emit('peer-disconnect', { publickey: peer.key })
244
+ }
245
+ }
246
+ }
247
+ }
248
+ })
249
+
250
+ // conn.end()
230
251
  })
231
252
  }
232
253
 
233
- /**
254
+ /**
255
+ *
256
+ * @method updateListen
257
+ *
258
+ */
259
+ updateListen = function (data) {
260
+ console.log('update listen')
261
+ console.log(data)
262
+ }
263
+
264
+ /**
234
265
  *
235
266
  * @method assessData data and act
236
267
  *
@@ -239,16 +270,19 @@ class NetworkPeers extends EventEmitter {
239
270
  if (Buffer.isBuffer(data)) {
240
271
  try {
241
272
  let dataShareIn = JSON.parse(data.toString())
273
+ // match current public key to base id of peer
274
+ let peerMatch = this.peerMatchbase(peer)
242
275
  if (dataShareIn.type === 'private-chart') {
243
- this.emit('beebee-data', dataShareIn)
276
+ this.emit('beebee-data', { publickey: peerMatch, data: dataShareIn })
277
+ // two types of chart share above html answer sharing and below from a full bentoboxN1 experiment TODO
244
278
  // need to look at NXP, modules and within for reference contracts.
245
279
  // Need to replicate public library for contracts (repliate hyberbee)
246
280
  // Need to ask for data source e.g. file (replicate hyberdrive)
247
281
  // Lastly put together SafeFlowECS query to produce chart
248
282
  } else if (dataShareIn.type === 'private-cue-space') {
249
- this.emit('cuespace-notification', dataShareIn)
283
+ this.emit('cuespace-notification', { publickey: peerMatch, data: dataShareIn })
250
284
  } else if (dataShareIn.type === 'public-library') {
251
- this.emit('publiclibrarynotification', dataShareIn)
285
+ this.emit('publiclibrarynotification', { publickey: peerMatch, data: dataShareIn })
252
286
  } else if (dataShareIn.type === 'peer') {
253
287
  } else if (dataShareIn.type === 'peer-codename-inform') {
254
288
  // all peer to match publicke to codename then update save and infom beebee
@@ -372,6 +406,56 @@ class NetworkPeers extends EventEmitter {
372
406
  return roleMatch
373
407
  }
374
408
 
409
+ /**
410
+ * match peer key to settings
411
+ * @method peerMatchTopic
412
+ *
413
+ */
414
+ peerMatchTopic = function (pubKey) {
415
+ // first match live pubkey to topic and then use topic to get original
416
+ let peerSettings = {}
417
+ for (let savePeer of this.peerNetwork) {
418
+ if (savePeer.key === pubKey) {
419
+ peerSettings = savePeer
420
+ }
421
+ }
422
+ return peerSettings
423
+ }
424
+
425
+ /**
426
+ * match discovery peer reconnect
427
+ * @method discoveryMatch
428
+ *
429
+ */
430
+ discoveryMatch = function (pubKey) {
431
+ // first match live pubkey to topic and then use topic to get original
432
+ let discoverySettings = {}
433
+ for (let savePeer of this.discoveryList) {
434
+ if (savePeer.peerKey === pubKey) {
435
+ discoverySettings = savePeer.discovery
436
+ }
437
+ }
438
+ // now refresh
439
+ discoverySettings.refresh()
440
+ return true
441
+ }
442
+
443
+ /**
444
+ * match current publickey to peer id i.e. estbalshed on first connect and maps to 'peername' in UX
445
+ * @method peerMatchbase
446
+ *
447
+ */
448
+ peerMatchbase = function (currPubKey) {
449
+ // first match live pubkey to topic and then use topic to get original
450
+ let originalKey = ''
451
+ for (let savePeer of this.peerNetwork) {
452
+ if (savePeer.value.livePeerkey === currPubKey) {
453
+ originalKey = savePeer.key
454
+ }
455
+ }
456
+ return originalKey
457
+ }
458
+
375
459
  /**
376
460
  * split invte code string
377
461
  * @method inviteDecoded
@@ -680,6 +764,7 @@ class NetworkPeers extends EventEmitter {
680
764
  *
681
765
  */
682
766
  topicConnect = async function (peerID, topic) {
767
+ console.log('topic connect')
683
768
  // const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
684
769
  const noisePublicKey = Buffer.from(topic, 'hex') // must be 32 bytes
685
770
  if (noisePublicKey.length === 32) {
@@ -687,6 +772,7 @@ class NetworkPeers extends EventEmitter {
687
772
  this.topicHolder[topic] = { role: 'server', livePubkey: this.swarm.keyPair.publicKey.toString('hex'), topic: topic, key: topicKeylive, timestamp: '' }
688
773
  this.sendTopicHolder.push({ livePubkey: this.swarm.keyPair.publicKey.toString('hex'), peerKey: peerID, topic: topic })
689
774
  const peerConnect = this.swarm.join(noisePublicKey, { server: true, client: false })
775
+ this.discoveryList.push({ peerKey: peerID, topic: topic, discovery: peerConnect })
690
776
  await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
691
777
  }
692
778
  }
@@ -697,6 +783,7 @@ class NetworkPeers extends EventEmitter {
697
783
  *
698
784
  */
699
785
  topicListen = async function (topic, peerKey) {
786
+ console.log('topic listen')
700
787
  // const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
701
788
  // let topicKeylive = noisePublicKey.toString('hex')
702
789
  const noisePublicKey = Buffer.from(topic, 'hex') // must be 32 bytes
@@ -704,7 +791,8 @@ class NetworkPeers extends EventEmitter {
704
791
  let topicKeylive = noisePublicKey.toString('hex')
705
792
  this.topicHolder[topic] = { role: 'client', topic: topic, key: topicKeylive, peerKey: peerKey, timestamp: '' }
706
793
  const peerConnect = this.swarm.join(noisePublicKey, { server: false, client: true })
707
- await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
794
+ this.discoveryList.push({ peerKey: peerKey, topic: topic, discovery: peerConnect })
795
+ await this.swarm.flush() // Waits for the topic to be fully announced on the DHT
708
796
  } else {
709
797
  console.log('key lenght issue')
710
798
  }