holepunch-hop 0.2.9 → 0.3.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.2.9",
3
+ "version": "0.3.0",
4
4
  "description": "data interface to HOP",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/bees.js CHANGED
@@ -4,7 +4,7 @@
4
4
  *
5
5
  * @class HypBee
6
6
  * @package HypBee
7
- * @copyright Copyright (c) 2022 James Littlejohn
7
+ * @copyright Copyright (c) 2024 James Littlejohn
8
8
  * @license http://www.gnu.org/licenses/old-licenses/gpl-3.0.html
9
9
  * @version $Id$
10
10
  */
@@ -20,6 +20,7 @@ class HyperBee extends EventEmitter {
20
20
  this.store = store
21
21
  this.swarm = swarm
22
22
  this.liveBees = {}
23
+ this.confirmPubLibList = {}
23
24
  }
24
25
 
25
26
  /**
@@ -45,14 +46,12 @@ class HyperBee extends EventEmitter {
45
46
  valueEncoding: 'json' // same options as above
46
47
  })
47
48
  await this.dbPublicLibrary.ready()
48
- beePubkeys.push({ store: 'pubilclibrary', pubkey: b4a.toString(core.key, 'hex')})
49
- // console.log(this.dbPublicLibrary._feed)
49
+ beePubkeys.push({ store: 'publiclibrary', pubkey: b4a.toString(core.key, 'hex')})
50
50
  // allow other peer access to public library (need to check for DDOS ie over asked)
51
51
  // join a topic
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('bee key:', b4a.toString(core.key, 'hex'))
56
55
  })
57
56
 
58
57
 
@@ -113,7 +112,6 @@ class HyperBee extends EventEmitter {
113
112
  *
114
113
  */
115
114
  savePubliclibrary = async function (refContract) {
116
- console.log(refContract)
117
115
  let beeSave = await this.dbPublicLibrary.put(refContract.data.hash, refContract.data.contract)
118
116
  // go query the key are return the info. to ensure data save asplanned.
119
117
  let saveCheck = await this.getPublicLibrary(refContract.data.hash)
@@ -444,9 +442,9 @@ class HyperBee extends EventEmitter {
444
442
  * @method replicatePubliclibrary
445
443
  *
446
444
  */
447
- replicatePubliclibrary = async function (keylib) {
445
+ replicatePubliclibrary = async function (dataIn) {
448
446
  // create or get the hypercore using the public key supplied as command-line argument
449
- const coreRep = this.store.get({ key: b4a.from(keylib, 'hex') })
447
+ const coreRep = this.store.get({ key: b4a.from(dataIn.data.datastores, 'hex') })
450
448
 
451
449
  // create a hyperbee instance using the hypercore instance
452
450
  const beePlib = new Hyperbee(coreRep, {
@@ -465,23 +463,83 @@ class HyperBee extends EventEmitter {
465
463
 
466
464
  await coreRep.update()
467
465
 
466
+ // 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
467
+ const boardNXPcontract = await beePlib.get(dataIn.data.boardID)
468
+ let unString = JSON.parse(boardNXPcontract.value)
469
+ let moduleContracts = []
470
+ for (let mod of unString.modules) {
471
+ let modC = await beePlib.get(mod)
472
+ moduleContracts.push(modC)
473
+ }
474
+ // next reference contracts, then ref within refs i.e. packaging datatypes
475
+ let referenceContracts = []
476
+ for (let modRef of moduleContracts) {
477
+ let unString = JSON.parse(modRef.value)
478
+ if (unString.style === 'packaging') {
479
+ // get list of datatypes
480
+ for (let ref of unString.info.value.concept.tablestructure) {
481
+ if (ref?.refcontract) {
482
+ let refC = await beePlib.get(ref.refcontract)
483
+ referenceContracts.push(refC)
484
+ }
485
+ }
486
+ } else if (unString.style === 'compute') {
487
+ // console.log('compute TODO')
488
+ // console.log(unString)
489
+ } else if (unString.style === 'visualise') {
490
+ // console.log('visualise TODO')
491
+ // console.log(unString)
492
+ } else if (unString.style === 'question') {
493
+ let questRef = {}
494
+ questRef.key = unString.info.key
495
+ questRef.value = JSON.stringify(unString.info.value)
496
+ referenceContracts.push(questRef)
497
+ }
498
+ }
499
+ // notify and get confirmation to accept and save to public library
500
+ if (moduleContracts.length > 0) {
501
+ // keep hold of data ready to be confirmed
502
+ let holderConfirm = {}
503
+ holderConfirm.boardNXP = [boardNXPcontract]
504
+ holderConfirm.modules = moduleContracts
505
+ holderConfirm.refcontracts = referenceContracts
506
+ this.confirmPubLibList[dataIn.data.datastores] = holderConfirm
507
+ this.emit('publibbeebee-notification', dataIn.data)
508
+ }
509
+ // or
468
510
  // now ask for whole of public library
469
511
  /* read all of repliate connect bee and displays */
470
512
  const chathistoryData = beePlib.createReadStream() // { gt: 'a', lt: 'z' }) // anything >a and <z
471
513
  let chatData = []
472
514
  for await (const { key, value } of chathistoryData) {
473
- chatData.push({ key, value })
515
+ if (key === dataIn.data.boardID) {
516
+ chatData.push({ key, value })
517
+ }
474
518
  }
475
-
476
519
  // now replicate with peer own public library in whole or per nxp
477
- let savePublib = await this.updatePublicLibrary(beePlib)
520
+ /* let savePublib = await this.updatePublicLibrary(beePlib)
478
521
  let repMessage = {}
479
522
  repMessage.type = 'library'
480
523
  repMessage.action = 'replicate-publiclibrary'
481
524
  repMessage.task = 'replicate'
482
525
  repMessage.reftype = 'publiclibrary'
483
526
  repMessage.data = savePublib
484
- return repMessage
527
+ return repMessage */
528
+ }
529
+
530
+
531
+ /**
532
+ * peer confirmed add to public library
533
+ * @method addConfrimPublicLibrary
534
+ *
535
+ */
536
+ addConfrimPublicLibrary = async function (data) {
537
+ // add board nxp
538
+ await this.updatePublicLibrary(this.confirmPubLibList[data.datastores].boardNXP)
539
+ // add modules
540
+ await this.updatePublicLibrary(this.confirmPubLibList[data.datastores].modules)
541
+ // add reference
542
+ await this.updatePublicLibrary(this.confirmPubLibList[data.datastores].refcontracts)
485
543
  }
486
544
 
487
545
  /**
@@ -489,17 +547,10 @@ class HyperBee extends EventEmitter {
489
547
  * @method updatePublicLibrary
490
548
  *
491
549
  */
492
- updatePublicLibrary = async function (updateLib) {
493
- /* read all of repliate connect bee and displays */
494
- const chathistoryData = updateLib.createReadStream() // { gt: 'a', lt: 'z' }) // anything >a and <z
495
- let libData = []
496
- for await (const { key, value } of chathistoryData) {
497
- libData.push({ key, value })
498
- }
499
-
550
+ updatePublicLibrary = async function (libContracts) {
500
551
  // save entries required
501
552
  const batch = this.dbPublicLibrary.batch()
502
- for (const { key, value } of libData) {
553
+ for (const { key, value } of libContracts) {
503
554
  await batch.put(key, JSON.parse(value))
504
555
  }
505
556
  await batch.flush()
@@ -577,10 +628,8 @@ class HyperBee extends EventEmitter {
577
628
  let rs = beeResults.createReadStream() // anything >=a and <=d
578
629
 
579
630
  for await (const { key, value } of rs) {
580
- // console.log(`${key} -> ${value}`)
581
631
  // need a save funnction in here
582
632
  if (key === 'bdb6a7db0b479d9b30406cd24f3cc2f315fd3ba0') {
583
- // console.log(`${key} -> ${value}`)
584
633
  let dataR = {}
585
634
  dataR.hash = key
586
635
  dataR.data = value
package/src/index.js CHANGED
@@ -91,12 +91,27 @@ class HolepunchWorker extends EventEmitter {
91
91
  })
92
92
  // peer connection active
93
93
  this.Peers.on('peer-connect', (data) => {
94
- this.Peers.writeTonetwork(data)
94
+ // if (this.Peers.peerHolder[data].data.boardID !== undefined) {
95
+ // any existing peers
96
+ let holderCheck = Object.keys(this.Peers.peerHolder)
97
+ if (holderCheck.length !== 0) {
98
+ this.Peers.writeToPublicLibrary(data)
99
+ } else {
100
+ this.Peers.writeTonetwork(data)
101
+ }
102
+
95
103
  })
96
104
  // data for beebee
97
105
  this.Peers.on('beebee-data', (data) => {
98
106
  this.emit('peer-topeer', data)
99
107
  })
108
+ // public library notification
109
+ this.Peers.on('publiclibrarynotification', (data) => {
110
+ this.BeeData.replicatePubliclibrary(data)
111
+ })
112
+ this.BeeData.on('publibbeebee-notification', (data) => {
113
+ this.emit('beebee-publib-notification', data)
114
+ })
100
115
  // new warm incoming peer
101
116
  this.Peers.on('connect-warm', (data) => {
102
117
  let peerId = {}
@@ -115,23 +130,33 @@ class HolepunchWorker extends EventEmitter {
115
130
  */
116
131
  networkPath = function (message) {
117
132
  if (message.action === 'share') {
118
- if (message.task === 'peer-join') {
119
- // has the peer joined already?
120
- let peerMatch = false
121
- for (let wpeer of this.warmPeers) {
122
- if (wpeer.publickey = message.data.publickey) {
123
- peerMatch = true
124
- }
133
+ // has the peer joined already?
134
+ let peerMatch = false
135
+ for (let wpeer of this.warmPeers) {
136
+ if (wpeer.publickey = message.data.publickey) {
137
+ peerMatch = true
125
138
  }
139
+ }
140
+
141
+ if (message.task === 'peer-join') {
126
142
  if (peerMatch === true) {
127
- this.Peers.peerAlreadyJoin(message.data)
143
+ this.Peers.peerAlreadyJoinSetData(message.data)
128
144
  this.Peers.writeTonetwork(message.data.publickey)
129
145
  } else {
130
146
  this.warmPeers.push(message.data)
131
147
  this.Peers.peerJoin(message.data)
132
148
  }
133
149
  } else if (message.task === 'peer-board') {
134
- console.log('public board share with peer')
150
+ if (peerMatch === true) {
151
+ this.Peers.peerAlreadyJoinSetData(message.data)
152
+ this.Peers.writeToPublicLibrary(message.data.publickey)
153
+ } else {
154
+ this.warmPeers.push(message.data)
155
+ this.Peers.peerJoin(message.data)
156
+ // now set data and write to public library info.
157
+ this.Peers.peerAlreadyJoinSetData(message.data)
158
+ this.Peers.writeToPublicLibrary(message.data.publickey)
159
+ }
135
160
  } else if (message.task === 'peer-write') {
136
161
  this.emit('peer-write', message.data)
137
162
  } else if (message.task === 'topic') {
package/src/peers.js CHANGED
@@ -18,7 +18,6 @@ class NetworkPeers extends EventEmitter {
18
18
 
19
19
  constructor(store, swarm) {
20
20
  super()
21
- console.log('peer manager')
22
21
  this.hello = 'hyperpeers'
23
22
  this.store = store
24
23
  this.swarm = swarm
@@ -60,9 +59,6 @@ class NetworkPeers extends EventEmitter {
60
59
  this.emit('peer-connect', publicKeylive)
61
60
  // process network message
62
61
  conn.on('data', data =>
63
- // console.log('recieve network message:', data.toString()),
64
- // console.log(data.toString()),
65
- // console.log('emit to be verified and acted upon appropriately'),
66
62
  // assess data
67
63
  this.assessData(publicKeylive, data)
68
64
  )
@@ -76,9 +72,7 @@ class NetworkPeers extends EventEmitter {
76
72
  *
77
73
  */
78
74
  assessData = function (peer, data) {
79
- console.log('assess---data receive peer-----------')
80
75
  if (Buffer.isBuffer(data)) {
81
- console.log('1 buffer')
82
76
  try {
83
77
  let dataShareIn = JSON.parse(data.toString())
84
78
  if (dataShareIn.type === 'chart') {
@@ -87,12 +81,13 @@ class NetworkPeers extends EventEmitter {
87
81
  // Need to replicate public library for contracts (repliate hyberbee)
88
82
  // Need to ask for data source e.g. file (replicate hyberdrive)
89
83
  // Lastly put together SafeFlowECS query to produce chart
84
+ } else if (dataShareIn.type === 'public-library') {
85
+ this.emit('publiclibrarynotification', dataShareIn)
90
86
  } else if (dataShareIn.type === 'peer') {
91
- console.log('3 buffer')
92
87
  }
93
88
  console.log(a)
94
89
  } catch (e) {
95
- return console.error('ignore')
90
+ return console.error('ignore err')
96
91
  }
97
92
  }
98
93
  }
@@ -118,6 +113,27 @@ class NetworkPeers extends EventEmitter {
118
113
  }
119
114
  }
120
115
 
116
+ /**
117
+ * write message connect public library
118
+ * @method writeToPublicLibrary
119
+ *
120
+ */
121
+ writeToPublicLibrary = function (publickey) {
122
+ // check this peer has asked for chart data
123
+ let connectTrue = publickey in this.peerConnect
124
+ let libraryTrue = publickey in this.peerHolder
125
+ if (connectTrue === true && libraryTrue === true) {
126
+ let libraryData = this.peerHolder[publickey]
127
+ let dataShare = {}
128
+ dataShare.data = libraryData.data
129
+ dataShare.type = 'public-library'
130
+ this.peerConnect[publickey].write(JSON.stringify(dataShare))
131
+ } else {
132
+ console.log('no board to write ie share with a peer')
133
+ }
134
+ }
135
+
136
+
121
137
 
122
138
  /**
123
139
  * join peer to peer private (server)
@@ -134,10 +150,10 @@ class NetworkPeers extends EventEmitter {
134
150
 
135
151
  /**
136
152
  * already joined but keep track context data
137
- * @method peerAlreadyJoin
153
+ * @method peerAlreadyJoinSetData
138
154
  *
139
155
  */
140
- peerAlreadyJoin = function (peerContext) {
156
+ peerAlreadyJoinSetData = function (peerContext) {
141
157
  this.peerHolder[peerContext.publickey] = peerContext
142
158
  }
143
159