holepunch-hop 0.1.7 → 0.1.8

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.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "data interface to HOP",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/bees.js CHANGED
@@ -36,7 +36,7 @@ class HyperBee extends EventEmitter {
36
36
 
37
37
  /**
38
38
  * setup hypercore protocol
39
- * @method startHyperbee
39
+ * @method setupHyperbee
40
40
  *
41
41
  */
42
42
  setupHyperbee = async function () {
@@ -48,7 +48,7 @@ class HyperBee extends EventEmitter {
48
48
  valueEncoding: 'json' // same options as above
49
49
  })
50
50
  await this.dbPublicLibrary.ready()
51
- beePubkeys.push({'pubilclibrary': b4a.toString(core.key, 'hex')})
51
+ beePubkeys.push({ store: 'pubilclibrary', pubkey: b4a.toString(core.key, 'hex')})
52
52
  // console.log(this.dbPublicLibrary._feed)
53
53
  // this.client.replicate(this.dbPublicLibrary.feed)
54
54
 
@@ -58,7 +58,7 @@ class HyperBee extends EventEmitter {
58
58
  valueEncoding: 'json' // same options as above
59
59
  })
60
60
  await this.dbPeerLibrary.ready()
61
- beePubkeys.push({'peerlibrary': b4a.toString(core2.key, 'hex')})
61
+ beePubkeys.push({store:'peerlibrary', pubkey: b4a.toString(core2.key, 'hex')})
62
62
 
63
63
  const core6 = this.core.get({ name: 'peers' })
64
64
  this.dbPeers = new Hyperbee(core6, {
@@ -66,7 +66,7 @@ class HyperBee extends EventEmitter {
66
66
  valueEncoding: 'json' // same options as above
67
67
  })
68
68
  await this.dbPeers.ready()
69
- beePubkeys.push({'peers': b4a.toString(core6.key, 'hex')})
69
+ beePubkeys.push({store:'peers', pubkey: b4a.toString(core6.key, 'hex')})
70
70
 
71
71
  const core3 = this.core.get({ name: 'bentospaces' })
72
72
  this.dbBentospaces = new Hyperbee(core3, {
@@ -74,7 +74,7 @@ class HyperBee extends EventEmitter {
74
74
  valueEncoding: 'json' // same options as above
75
75
  })
76
76
  await this.dbBentospaces.ready()
77
- beePubkeys.push({'bentospaces': b4a.toString(core3.key, 'hex')})
77
+ beePubkeys.push({store:'bentospaces', pubkey: b4a.toString(core3.key, 'hex')})
78
78
 
79
79
  const core4 = this.core.get({ name: 'hopresults' })
80
80
  this.dbHOPresults = new Hyperbee(core4, {
@@ -83,7 +83,7 @@ class HyperBee extends EventEmitter {
83
83
  })
84
84
  await this.dbHOPresults.ready()
85
85
  // this.client.replicate(this.dbHOPresults.feed)
86
- beePubkeys.push({'hopresults': b4a.toString(core4.key, 'hex')})
86
+ beePubkeys.push({store:'hopresults', pubkey: b4a.toString(core4.key, 'hex')})
87
87
 
88
88
  const core5 = this.core.get({ name: 'kbledger' })
89
89
  this.dbKBledger = new Hyperbee(core5, {
@@ -92,12 +92,13 @@ class HyperBee extends EventEmitter {
92
92
  })
93
93
  await this.dbKBledger.ready()
94
94
  // this.client.replicate(this.dbKBledger.feed)
95
- beePubkeys.push({'kbledger': b4a.toString(core5.key, 'hex')})
95
+ beePubkeys.push({store:'kbledger', pubkey: b4a.toString(core5.key, 'hex')})
96
96
  console.log('hyperbees-live')
97
97
  this.emit('hbee-live')
98
98
  // return beePubkeys
99
99
  let startBeePubkey = {}
100
- startBeePubkey.type = 'hyperbee-pubkeys'
100
+ startBeePubkey.type = 'account'
101
+ startBeePubkey.action = 'hyperbee-pubkeys'
101
102
  startBeePubkey.data = beePubkeys
102
103
  this.liveBees = startBeePubkey
103
104
  this.wsocket.send(JSON.stringify(startBeePubkey))
package/src/drive.js CHANGED
@@ -63,7 +63,7 @@ class HypDrive extends EventEmitter {
63
63
  *
64
64
  */
65
65
  hyperdriveWritestream = async function (fileData) {
66
- let localthis = this
66
+ /* let localthis = this
67
67
  const ws = this.drive.createWriteStream('/blob.txt')
68
68
 
69
69
  this.wsocketwrite('Hello, ')
@@ -73,7 +73,7 @@ class HypDrive extends EventEmitter {
73
73
  this.wsocketon('close', function () {
74
74
  const rs = localthis.drive.createReadStream('/blob.txt')
75
75
  rs.pipe(process.stdout) // prints Hello, world!
76
- })
76
+ }) */
77
77
  }
78
78
 
79
79
  /**
@@ -133,6 +133,23 @@ class HypDrive extends EventEmitter {
133
133
  return confirmSave
134
134
  }
135
135
 
136
+ /**
137
+ * save csv data to hyperdrive file
138
+ * @method saveCSVfilecontent
139
+ *
140
+ */
141
+ saveCSVfilecontent = async function (fData) {
142
+ // extract header info first
143
+ let headerInfo = this.fileUtility.webCSVparse(fData)
144
+ let hyperdrivePath = 'csv/' + fData.data[0].name
145
+ let confirmSave = await this.drive.put(hyperdrivePath, fData.data[0].content)
146
+ let saveStatus = {}
147
+ saveStatus.save = confirmSave
148
+ saveStatus.headerinfo = headerInfo
149
+ return saveStatus
150
+ }
151
+
152
+
136
153
  /**
137
154
  * save to hyperdrive file
138
155
  * @method hyperdriveFilesave
@@ -191,6 +208,8 @@ class HypDrive extends EventEmitter {
191
208
  *
192
209
  */
193
210
  hyperdriveLocalfile = async function (path) {
211
+ console.log('hoelpuch--localfile')
212
+ console.log(path)
194
213
  // File reads to buffer and recreate file
195
214
  // const bufFromGet2 = await this.drive.get(path)
196
215
  const { value: entry } = await this.drive.entry(path)
package/src/fileParser.js CHANGED
@@ -91,13 +91,25 @@ FileParser.prototype.localFileParse = async function (o, ws) {
91
91
  }
92
92
 
93
93
  /**
94
- * files from cloud
95
- * @method webFileParse
94
+ * csv content files from web
95
+ * @method webCSVparse
96
96
  *
97
97
  */
98
- FileParser.prototype.webFileParse = async function (o, ws) {
99
- const localthis = this
100
- let dataWeb = await axios.get(o.data.websource)
98
+ FileParser.prototype.webCSVparse = function (fData) {
99
+ // match name row number
100
+ let lcounter = 0
101
+ let match = ''
102
+ fData.data[0].content.split(/\r?\n/).forEach(line => {
103
+ lcounter++
104
+ if (lcounter === (parseInt(fData.data[0].info.cnumber) +1 )) {
105
+ match = line
106
+ }
107
+ })
108
+
109
+ let headerInfo = this.extractCSVheaders(fData.data[0], match)
110
+ return headerInfo
111
+ /*
112
+ let dataWeb = await axios.get(content.websource)
101
113
  .catch(function (error) {
102
114
  // handle error
103
115
  console.log(error)
@@ -107,18 +119,19 @@ FileParser.prototype.webFileParse = async function (o, ws) {
107
119
  let match = []
108
120
  dataSource.split(/\r\n|\n/).forEach(line => {
109
121
  lcounter++
110
- if (lcounter === (parseInt(o.data.info.cnumber) +1 )) {
122
+ if (lcounter === (parseInt(content.info.cnumber) +1 )) {
111
123
  match = line
112
124
  }
113
125
  })
126
+ */
114
127
  // create new file name hash of source url
115
- const hashURL = crypto.createHash('sha256').update(o.data.websource).digest('hex')
116
- const fileNewName = hashURL + '.csv'
128
+ // const hashURL = crypto.createHash('sha256').update(content.websource).digest('hex')
129
+ // const fileNewName = hashURL + '.csv'
117
130
  // localthis.linesLimit = lines.slice(0, 30)
118
- let headerInfo = localthis.extractCSVheaders(o, match)
119
- let newPathFile = localthis.saveOriginalProtocolWeb(o, dataSource, fileNewName)
120
- const praser = await localthis.readFileStream(newPathFile, headerInfo)
121
- this.convertJSON(o, ws, headerInfo, praser, 'web', fileNewName)
131
+ // let headerInfo = this.extractCSVheaders(content, match)
132
+ // let newPathFile = localthis.saveOriginalProtocolWeb(content, dataSource, fileNewName)
133
+ // const praser = await localthis.readFileStream(newPathFile, headerInfo)
134
+ // this.convertJSON(o, ws, headerInfo, praser, 'web', fileNewName)
122
135
  }
123
136
 
124
137
  /**
@@ -184,18 +197,18 @@ FileParser.prototype.extractJSONkeys = function (o) {
184
197
  * @method extractCSVheaders
185
198
  *
186
199
  */
187
- FileParser.prototype.extractCSVheaders = function (o, lineData) {
200
+ FileParser.prototype.extractCSVheaders = function (data, lineData) {
188
201
  let delimiter = ''
189
- if (o.data[0].info.delimiter === 'tab') {
202
+ if (data.info.delimiter === 'tab') {
190
203
  delimiter = "\t"
191
- } else if (o.data[0].info.delimiter === ';') {
204
+ } else if (data.info.delimiter === ';') {
192
205
  delimiter = ";"
193
206
  } else {
194
207
  delimiter = ","
195
208
  }
196
209
  let splitWords = lineData.split(delimiter)
197
210
  const headerSet = splitWords
198
- let dataline = parseInt(o.data[0].info.dataline)
211
+ let dataline = parseInt(data.info.dataline)
199
212
 
200
213
  let headerInfo = {}
201
214
  headerInfo.headerset = headerSet
package/src/index.js CHANGED
@@ -34,7 +34,9 @@ class HolepunchWorker extends EventEmitter {
34
34
  this.core3 = {}
35
35
  this.discKeypeer = ''
36
36
  this.readcore = null
37
+ this.warmPeers = []
37
38
  this.startHolepunch()
39
+ this.networkListeners()
38
40
  }
39
41
 
40
42
  /**
@@ -48,7 +50,7 @@ class HolepunchWorker extends EventEmitter {
48
50
  goodbye(() => this.swarm.destroy())
49
51
  this.BeeData = new BeeWorker(this.store, this.swarm)
50
52
  this.DriveFiles = new DriveWorker(this.store, this.swarm)
51
- this.Peers = new PeerWorker()
53
+ this.Peers = new PeerWorker(this.store, this.swarm)
52
54
  }
53
55
 
54
56
  /**
@@ -59,6 +61,7 @@ class HolepunchWorker extends EventEmitter {
59
61
  activateHypercores = async function () {
60
62
  await this.DriveFiles.setupHyperdrive()
61
63
  await this.BeeData.setupHyperbee()
64
+ this.Peers.networkKeys()
62
65
  this.emit('hcores-active')
63
66
  }
64
67
 
@@ -68,20 +71,78 @@ class HolepunchWorker extends EventEmitter {
68
71
  *
69
72
  */
70
73
  setWebsocket = function (ws) {
74
+ console.log('webcocket live HP')
71
75
  this.wsocket = ws
72
76
  this.BeeData.setWebsocket(ws)
73
77
  this.DriveFiles.setWebsocket(ws)
74
78
  this.activateHypercores()
75
79
  }
76
80
 
81
+
82
+ /**
83
+ * listen for outputs from workers
84
+ * @method networkListeners
85
+ *
86
+ */
87
+ networkListeners = function () {
88
+ this.Peers.on('peer-network', (data) => {
89
+ this.wsocket.send(JSON.stringify(data))
90
+ })
91
+ // peer connection active
92
+ this.Peers.on('peer-connect', (data) => {
93
+ this.Peers.writeTonetwork(data)
94
+ })
95
+ // data for beebee
96
+ this.Peers.on('beebee-data', (data) => {
97
+ this.emit('peer-topeer', data)
98
+ })
99
+ // new warm incoming peer
100
+ this.Peers.on('connect-warm', (data) => {
101
+ let peerId = {}
102
+ peerId.name = ''
103
+ peerId.publickkey = data
104
+ peerId.datastore = ''
105
+ this.warmPeers.push(peerId)
106
+ })
107
+ }
108
+
109
+ /**
110
+ * manage flow to network of peers and data
111
+ * @method networkPath
112
+ *
113
+ */
114
+ networkPath = function (message) {
115
+ if (message.action === 'share') {
116
+ if (message.task === 'peer-join') {
117
+ // has the peer joined already?
118
+ let peerMatch = false
119
+ for (let wpeer of this.warmPeers) {
120
+ if (wpeer.publickey = message.data.publickey) {
121
+ peerMatch = true
122
+ }
123
+ }
124
+ if (peerMatch === true) {
125
+ this.Peers.peerAlreadyJoin(message.data)
126
+ this.Peers.writeTonetwork(message.data.publickey)
127
+ } else {
128
+ this.warmPeers.push(message.data)
129
+ this.Peers.peerJoin(message.data)
130
+ }
131
+ } else if (message.task === 'peer-write') {
132
+ this.emit('peer-write', message.data)
133
+ } else if (message.task === 'topic') {
134
+ // this.Peers.peerTopic(message.data.topic)
135
+ }
136
+ }
137
+ }
138
+
77
139
  /**
78
140
  * corestore test example
79
141
  * @method testCorestore
80
142
  *
81
143
  */
82
144
  testCoreStore = async function () {
83
-
84
- // A name is a purely-local, and maps to a key pair. It's not visible to readers.
145
+ // A name is a purely-local, and maps to a key pair. It's not visible to readers.
85
146
  // Since a name always corresponds to a key pair, these are all writable
86
147
  this.core1 = this.store.get({ name: 'core-1', valueEncoding: 'json' })
87
148
  this.core2 = this.store.get({ name: 'core-2' })
package/src/peers.js CHANGED
@@ -2,8 +2,8 @@
2
2
  /**
3
3
  * Manage Peers connections
4
4
  *
5
- * @class HypPeers
6
- * @package HypPeers
5
+ * @class NetworkPeers
6
+ * @package NetworkPeers
7
7
  * @copyright Copyright (c) 2022 James Littlejohn
8
8
  * @license http://www.gnu.org/licenses/old-licenses/gpl-3.0.html
9
9
  * @version $Id$
@@ -14,15 +14,157 @@ import goodbye from 'graceful-goodbye'
14
14
  import b4a from 'b4a'
15
15
 
16
16
 
17
- class HypPeers extends EventEmitter {
17
+ class NetworkPeers extends EventEmitter {
18
18
 
19
19
  constructor(core, swarm) {
20
20
  super()
21
+ console.log('peer manager')
21
22
  this.hello = 'hyperpeers'
22
23
  this.core = core
23
24
  this.swarm = swarm
24
25
  this.drive = {}
25
- // this.setupHyerPeers()
26
+ this.peerHolder = {}
27
+ this.peerConnect = {}
28
+ }
29
+
30
+ /**
31
+ * public/piv key on DHT
32
+ * @method networkKeys
33
+ *
34
+ */
35
+ networkKeys = function () {
36
+ console.log('hyperswarm begin1')
37
+ let peerNxKeys = {}
38
+ peerNxKeys.publickey = this.swarm.keyPair.publicKey.toString('hex')
39
+ let networkMessage = {}
40
+ networkMessage.type = 'account'
41
+ networkMessage.action = 'network-keys'
42
+ networkMessage.data = peerNxKeys
43
+ this.emit('peer-network', networkMessage)
44
+ this.listenNetwork()
45
+ this.peerJoinClient()
46
+ }
47
+
48
+ /**
49
+ * connection listen
50
+ * @method listenNetwork
51
+ *
52
+ */
53
+ listenNetwork = function () {
54
+ console.log('listen network')
55
+ this.swarm.on('connection', (conn, info) => {
56
+ // listener to write message to peers or network partial or broadcast
57
+ console.log('swarm connection recieved')
58
+ let publicKeylive = info.publicKey.toString('hex')
59
+ this.emit('connect-warm', publicKeylive)
60
+ this.peerConnect[publicKeylive] = conn
61
+ this.emit('peer-connect', publicKeylive)
62
+ // process network message
63
+ conn.on('data', data =>
64
+ // console.log('recieve network message:', data.toString()),
65
+ // console.log(data.toString()),
66
+ // console.log('emit to be verified and acted upon appropriately'),
67
+ // assess data
68
+ this.assessData(publicKeylive, data)
69
+ )
70
+ //conn.end()
71
+ })
72
+ }
73
+
74
+ /**
75
+ *
76
+ * @method assessData data and act
77
+ *
78
+ */
79
+ assessData = function (peer, data) {
80
+ console.log('assess---data receive peer-----------')
81
+ let dataShareIn = JSON.parse(data.toString())
82
+ if (dataShareIn.type === 'chart') {
83
+ console.log('start chart protocol from peer')
84
+ this.emit('beebee-data', dataShareIn)
85
+ // need to look at NXP, modules and within for reference contracts.
86
+ // Need to replicate public library for contracts (repliate hyberbee)
87
+ // Need to ask for data source e.g. file (replicate hyberdrive)
88
+ // Lastly put together SafeFlowECS query to produce chart
89
+ } else if (dataShareIn.type === 'peer') {
90
+ console.log('message from peer on connect')
91
+ console.log(dataShareIn)
92
+ }
93
+ }
94
+
95
+ /**
96
+ * write message to network
97
+ * @method writeTonetwork
98
+ *
99
+ */
100
+ writeTonetwork = function (publickey) {
101
+ // check this peer has asked for chart data
102
+ let connectTrue = publickey in this.peerConnect
103
+ let chartTrue = publickey in this.peerHolder
104
+ if (connectTrue === true && chartTrue === true) {
105
+ let chartData = this.peerHolder[publickey]
106
+ let dataShare = {}
107
+ dataShare.hop = chartData.hop
108
+ dataShare.data = chartData.data
109
+ dataShare.type = 'chart'
110
+ this.peerConnect[publickey].write(JSON.stringify(dataShare))
111
+ } else {
112
+ console.log('non chart write')
113
+ }
114
+ }
115
+
116
+
117
+ /**
118
+ * join peer to peer private (server)
119
+ * @method peerJoin
120
+ *
121
+ */
122
+ peerJoin = function (peerContext) {
123
+ this.peerHolder[peerContext.publickey] = peerContext
124
+ const noisePublicKey = Buffer.from(peerContext.publickey, 'hex') // must be 32 bytes
125
+ if (noisePublicKey.length === 32) {
126
+ const peerConnect = this.swarm.joinPeer(noisePublicKey, { server: true, client: false })
127
+ }
128
+ }
129
+
130
+ /**
131
+ * already joined but keep track context data
132
+ * @method peerAlreadyJoin
133
+ *
134
+ */
135
+ peerAlreadyJoin = function (peerContext) {
136
+ console.log('aleardy joined')
137
+ this.peerHolder[peerContext.publickey] = peerContext
138
+ }
139
+
140
+
141
+ /**
142
+ * join peer to peer private (client)
143
+ * @method peerJoinClient
144
+ *
145
+ */
146
+ peerJoinClient = function () {
147
+ this.swarm.listen()
148
+ }
149
+
150
+ /**
151
+ * listen for topics as a client
152
+ * @method listenClient
153
+ *
154
+ */
155
+ listenClient = async function (topic) {
156
+ const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
157
+ const peerConnect = this.swarm.join(noisePublicKey, { server: false, client: true })
158
+ await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
159
+ }
160
+
161
+ /**
162
+ *
163
+ * @method listen
164
+ *
165
+ */
166
+ listen = function () {
167
+ console.log('listen')
26
168
  }
27
169
 
28
170
  /**
@@ -52,4 +194,4 @@ class HypPeers extends EventEmitter {
52
194
 
53
195
  }
54
196
 
55
- export default HypPeers
197
+ export default NetworkPeers