holepunch-hop 0.3.9 → 0.4.1

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.1",
4
4
  "description": "data interface to HOP",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/bees.js CHANGED
@@ -52,6 +52,7 @@ class HyperBee extends EventEmitter {
52
52
  const discovery = this.swarm.join(this.dbPublicLibrary.discoveryKey)
53
53
  // Only display the key once the Hyperbee has been announced to the DHT
54
54
  discovery.flushed().then(() => {
55
+ console.log('public library open')
55
56
  })
56
57
 
57
58
  const core2 = this.store.get({ name: 'peerlibrary' })
@@ -875,7 +876,7 @@ class HyperBee extends EventEmitter {
875
876
  */
876
877
  replicatePubliclibrary = async function (dataIn) {
877
878
  // 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') })
879
+ const coreRep = this.store.get({ key: b4a.from(dataIn.data.data.datastores, 'hex') })
879
880
 
880
881
  // create a hyperbee instance using the hypercore instance
881
882
  const beePlib = new Hyperbee(coreRep, {
@@ -895,7 +896,7 @@ class HyperBee extends EventEmitter {
895
896
  await coreRep.update()
896
897
 
897
898
  // 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)
899
+ const boardNXPcontract = await beePlib.get(dataIn.data.data.boardID)
899
900
  let unString = JSON.parse(boardNXPcontract.value)
900
901
  let moduleContracts = []
901
902
  for (let mod of unString.modules) {
@@ -934,8 +935,8 @@ class HyperBee extends EventEmitter {
934
935
  holderConfirm.boardNXP = [boardNXPcontract]
935
936
  holderConfirm.modules = moduleContracts
936
937
  holderConfirm.refcontracts = referenceContracts
937
- this.confirmPubLibList[dataIn.data.datastores] = holderConfirm
938
- this.emit('publibbeebee-notification', dataIn.data)
938
+ this.confirmPubLibList[dataIn.data.data.datastores] = holderConfirm
939
+ this.emit('publibbeebee-notification', dataIn)
939
940
  }
940
941
  // or
941
942
  // 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,13 @@ 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
+ let peerDefaults = this.Peers.peerMatchTopic(message.data.key)
288
+ this.Peers.discoveryMatch(message.data.key)
289
+ } else if (message.action === 'peer-closed') {
290
+ this.flushConnections()
291
+ await this.swarm.destroy()
292
+ // this.flushConnections()
273
293
  }
274
294
  }
275
295
 
@@ -417,6 +437,17 @@ class HolepunchWorker extends EventEmitter {
417
437
  this.readcore = lastBlock
418
438
  }
419
439
 
440
+ /**
441
+ * refresh connection i.e. which peers are live
442
+ * @method flushConnections
443
+ *
444
+ */
445
+ flushConnections = async function () {
446
+ await this.swarm.flush()
447
+ await Promise.all(Array.from(this.swarm.connections).map(e => e.flush()))
448
+ await new Promise(resolve => setImmediate(resolve))
449
+ }
450
+
420
451
  }
421
452
 
422
453
  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,40 @@ 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
+ for (let peer of this.peerNetwork) {
234
+ for (let pconn of connectLivekeys) {
235
+ if (peer.value.livePeerkey === pconn) {
236
+ // check if connect is close?
237
+ let keysNoise = Object.keys(this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed'])
238
+ // console.log(this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed'])
239
+ let closeStatus = this.peerConnect[pconn]['noiseStream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['_writableState']['stream']['rawStream']['_closed']
240
+ if (closeStatus === true) {
241
+ // remove peer & inform beebee
242
+ this.emit('peer-disconnect', { publickey: peer.key })
243
+ }
244
+ }
245
+ }
246
+ }
247
+ })
248
+
249
+ // conn.end()
230
250
  })
231
251
  }
232
252
 
233
- /**
253
+ /**
254
+ *
255
+ * @method updateListen
256
+ *
257
+ */
258
+ updateListen = function (data) {
259
+ // console.log('update listen')
260
+ // console.log(data)
261
+ }
262
+
263
+ /**
234
264
  *
235
265
  * @method assessData data and act
236
266
  *
@@ -239,16 +269,19 @@ class NetworkPeers extends EventEmitter {
239
269
  if (Buffer.isBuffer(data)) {
240
270
  try {
241
271
  let dataShareIn = JSON.parse(data.toString())
272
+ // match current public key to base id of peer
273
+ let peerMatch = this.peerMatchbase(peer)
242
274
  if (dataShareIn.type === 'private-chart') {
243
- this.emit('beebee-data', dataShareIn)
275
+ this.emit('beebee-data', { publickey: peerMatch, data: dataShareIn })
276
+ // two types of chart share above html answer sharing and below from a full bentoboxN1 experiment TODO
244
277
  // need to look at NXP, modules and within for reference contracts.
245
278
  // Need to replicate public library for contracts (repliate hyberbee)
246
279
  // Need to ask for data source e.g. file (replicate hyberdrive)
247
280
  // Lastly put together SafeFlowECS query to produce chart
248
281
  } else if (dataShareIn.type === 'private-cue-space') {
249
- this.emit('cuespace-notification', dataShareIn)
282
+ this.emit('cuespace-notification', { publickey: peerMatch, data: dataShareIn })
250
283
  } else if (dataShareIn.type === 'public-library') {
251
- this.emit('publiclibrarynotification', dataShareIn)
284
+ this.emit('publiclibrarynotification', { publickey: peerMatch, data: dataShareIn })
252
285
  } else if (dataShareIn.type === 'peer') {
253
286
  } else if (dataShareIn.type === 'peer-codename-inform') {
254
287
  // all peer to match publicke to codename then update save and infom beebee
@@ -372,6 +405,61 @@ class NetworkPeers extends EventEmitter {
372
405
  return roleMatch
373
406
  }
374
407
 
408
+ /**
409
+ * match peer key to settings
410
+ * @method peerMatchTopic
411
+ *
412
+ */
413
+ peerMatchTopic = function (pubKey) {
414
+ // first match live pubkey to topic and then use topic to get original
415
+ let peerSettings = {}
416
+ for (let savePeer of this.peerNetwork) {
417
+ if (savePeer.key === pubKey) {
418
+ peerSettings = savePeer
419
+ }
420
+ }
421
+ return peerSettings
422
+ }
423
+
424
+ /**
425
+ * match discovery peer reconnect
426
+ * @method discoveryMatch
427
+ *
428
+ */
429
+ discoveryMatch = function (pubKey) {
430
+ // first match live pubkey to topic and then use topic to get original
431
+ let discoverySettings = {}
432
+ for (let savePeer of this.discoveryList) {
433
+ if (savePeer.peerKey === pubKey) {
434
+ discoverySettings = savePeer.discovery
435
+ }
436
+ }
437
+ // now refresh
438
+ discoverySettings.refresh()
439
+ return true
440
+ }
441
+
442
+ /**
443
+ * match current publickey to peer id i.e. estbalshed on first connect and maps to 'peername' in UX
444
+ * @method peerMatchbase
445
+ *
446
+ */
447
+ peerMatchbase = function (currPubKey) {
448
+ // first match live pubkey to topic and then use topic to get original
449
+ let originalKey = ''
450
+ for (let savePeer of this.peerNetwork) {
451
+ if (savePeer.value.livePeerkey === currPubKey) {
452
+ originalKey = savePeer.key
453
+ }
454
+ }
455
+ // check if first time peer connect
456
+ if (originalKey.length === 0) {
457
+ return currPubKey
458
+ } else {
459
+ return originalKey
460
+ }
461
+ }
462
+
375
463
  /**
376
464
  * split invte code string
377
465
  * @method inviteDecoded
@@ -687,6 +775,7 @@ class NetworkPeers extends EventEmitter {
687
775
  this.topicHolder[topic] = { role: 'server', livePubkey: this.swarm.keyPair.publicKey.toString('hex'), topic: topic, key: topicKeylive, timestamp: '' }
688
776
  this.sendTopicHolder.push({ livePubkey: this.swarm.keyPair.publicKey.toString('hex'), peerKey: peerID, topic: topic })
689
777
  const peerConnect = this.swarm.join(noisePublicKey, { server: true, client: false })
778
+ this.discoveryList.push({ peerKey: peerID, topic: topic, discovery: peerConnect })
690
779
  await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
691
780
  }
692
781
  }
@@ -704,7 +793,8 @@ class NetworkPeers extends EventEmitter {
704
793
  let topicKeylive = noisePublicKey.toString('hex')
705
794
  this.topicHolder[topic] = { role: 'client', topic: topic, key: topicKeylive, peerKey: peerKey, timestamp: '' }
706
795
  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
796
+ this.discoveryList.push({ peerKey: peerKey, topic: topic, discovery: peerConnect })
797
+ await this.swarm.flush() // Waits for the topic to be fully announced on the DHT
708
798
  } else {
709
799
  console.log('key lenght issue')
710
800
  }