holepunch-hop 0.3.3 → 0.3.4
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 +1 -1
- package/src/bees.js +50 -12
- package/src/index.js +91 -61
- package/src/peers.js +112 -62
package/package.json
CHANGED
package/src/bees.js
CHANGED
|
@@ -46,15 +46,14 @@ class HyperBee extends EventEmitter {
|
|
|
46
46
|
valueEncoding: 'json' // same options as above
|
|
47
47
|
})
|
|
48
48
|
await this.dbPublicLibrary.ready()
|
|
49
|
-
beePubkeys.push({ store: 'publiclibrary', pubkey: b4a.toString(
|
|
49
|
+
beePubkeys.push({ store: 'publiclibrary', pubkey: b4a.toString(this.dbPublicLibrary.key, 'hex')})
|
|
50
50
|
// allow other peer access to public library (need to check for DDOS ie over asked)
|
|
51
|
-
// join a topic
|
|
51
|
+
// join a topic for network
|
|
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
55
|
})
|
|
56
56
|
|
|
57
|
-
|
|
58
57
|
const core2 = this.store.get({ name: 'peerlibrary' })
|
|
59
58
|
this.dbPeerLibrary = new Hyperbee(core2, {
|
|
60
59
|
keyEncoding: 'utf-8', // can be set to undefined (binary), utf-8, ascii or and abstract-encoding
|
|
@@ -149,12 +148,6 @@ class HyperBee extends EventEmitter {
|
|
|
149
148
|
})
|
|
150
149
|
await this.dbBentomedia.ready()
|
|
151
150
|
beePubkeys.push({store:'bentomedia', pubkey: b4a.toString(core12.key, 'hex')})
|
|
152
|
-
|
|
153
|
-
// await this.deleteBentocue({ cueid: '7da21b8d50c72f94595fd9617bfa2609c90d5d9c'})
|
|
154
|
-
// await this.deleteBentocue({ cueid: '8220c425a534634a44a69b6f56babaf26edf2a01'})
|
|
155
|
-
// await this.deleteBentocue({ cueid: 'ec4890f6c0b0d8f4a313136b4e7c9f425b1977a5'})
|
|
156
|
-
// await this.deleteBentocue({ cueid: 'eea2825cc91a1093594dffa9a953cbce3f77860c'})
|
|
157
|
-
// await this.deleteBentocue({ cueid: 'eee097d964fae67a6dd39abc02b0e9fd7c5f71bc'})
|
|
158
151
|
|
|
159
152
|
this.emit('hbee-live')
|
|
160
153
|
// return beePubkeys
|
|
@@ -172,8 +165,6 @@ class HyperBee extends EventEmitter {
|
|
|
172
165
|
*
|
|
173
166
|
*/
|
|
174
167
|
savePubliclibrary = async function (refContract) {
|
|
175
|
-
console.log('savePubliclibrary')
|
|
176
|
-
console.log(refContract)
|
|
177
168
|
let beeSave = await this.dbPublicLibrary.put(refContract.data.hash, refContract.data.contract)
|
|
178
169
|
// go query the key are return the info. to ensure data save asplanned.
|
|
179
170
|
let saveCheck = await this.getPublicLibrary(refContract.data.hash)
|
|
@@ -416,13 +407,60 @@ class HyperBee extends EventEmitter {
|
|
|
416
407
|
* @method deleteBentomedia
|
|
417
408
|
*/
|
|
418
409
|
deleteBentomedia = async function (media) {
|
|
419
|
-
console.log(media)
|
|
420
410
|
const deleteStatus = await this.dbBentomedia.del(media.id)
|
|
421
411
|
let deleteInfo = {}
|
|
422
412
|
deleteInfo.spaceid = media.id
|
|
423
413
|
return deleteInfo
|
|
424
414
|
}
|
|
425
415
|
|
|
416
|
+
/** PEERS */
|
|
417
|
+
/**
|
|
418
|
+
* save research
|
|
419
|
+
* @method savePeer
|
|
420
|
+
*
|
|
421
|
+
*/
|
|
422
|
+
savePeer = async function (peerInfo) {
|
|
423
|
+
await this.dbPeers.put(peerInfo.publickey, peerInfo)
|
|
424
|
+
let checkSave = await this.getPeer(peerInfo.publickey)
|
|
425
|
+
return checkSave
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* get one peer by publickey
|
|
430
|
+
* @method getPeer
|
|
431
|
+
*
|
|
432
|
+
*/
|
|
433
|
+
getPeer = async function (key) {
|
|
434
|
+
const nodeData = await this.dbPeers.get(key)
|
|
435
|
+
return nodeData
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* get all peers
|
|
440
|
+
* @method getPeersHistory
|
|
441
|
+
*
|
|
442
|
+
*/
|
|
443
|
+
getPeersHistory = async function (key) {
|
|
444
|
+
const peerHistory = await this.dbPeers.createReadStream()
|
|
445
|
+
let peerData = []
|
|
446
|
+
for await (const { key, value } of peerHistory) {
|
|
447
|
+
peerData.push({ key, value })
|
|
448
|
+
}
|
|
449
|
+
return peerData
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* delete contract
|
|
454
|
+
* @method deletePeer
|
|
455
|
+
*/
|
|
456
|
+
deletePeer = async function (pubkey) {
|
|
457
|
+
const deleteStatus = await this.dbPeers.del(pubkey)
|
|
458
|
+
let deleteInfo = {}
|
|
459
|
+
deleteInfo.publickey = pubkey
|
|
460
|
+
return deleteInfo
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
|
|
426
464
|
/** RESEARCH */
|
|
427
465
|
/**
|
|
428
466
|
* save research
|
package/src/index.js
CHANGED
|
@@ -48,7 +48,7 @@ class HolepunchWorker extends EventEmitter {
|
|
|
48
48
|
this.store = new Corestore(os.homedir() + '/.hop-storage')
|
|
49
49
|
this.swarm = new Hyperswarm()
|
|
50
50
|
// make replication possible
|
|
51
|
-
|
|
51
|
+
this.swarm.on('connection', conn => this.store.replicate(conn))
|
|
52
52
|
goodbye(() => this.swarm.destroy())
|
|
53
53
|
this.BeeData = new BeeWorker(this.store, this.swarm)
|
|
54
54
|
this.DriveFiles = new DriveWorker(this.store, this.swarm)
|
|
@@ -97,69 +97,60 @@ class HolepunchWorker extends EventEmitter {
|
|
|
97
97
|
this.Peers.on('peer-network', (data) => {
|
|
98
98
|
this.wsocket.send(JSON.stringify(data))
|
|
99
99
|
})
|
|
100
|
-
// peer connection active for first time
|
|
100
|
+
// peer connection active for first time
|
|
101
101
|
this.Peers.on('peer-connect', (data) => {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
console.log(taskCheck)
|
|
120
|
-
let peerFirstID = holderCheck[0]
|
|
121
|
-
console.log(peerFirstID)
|
|
122
|
-
console.log(this.Peers.peerHolder)
|
|
123
|
-
// which direction connection, client or from a peer on the network?
|
|
124
|
-
if (this.Peers.peerHolder[peerFirstID]?.data !== undefined) {
|
|
125
|
-
let dataLive = this.Peers.peerHolder[peerFirstID].data.name
|
|
126
|
-
if (dataLive === 'cue-space') {
|
|
127
|
-
this.Peers.writeToCueSpace(this.Peers.peerHolder[peerFirstID].publickey)
|
|
128
|
-
} else {
|
|
129
|
-
this.Peers.writeToPublicLibrary(data)
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
} else {
|
|
133
|
-
console.log('write to entwoir on start')
|
|
134
|
-
this.Peers.writeTonetwork(data)
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
102
|
+
//this.warmPeerPrepare(data)
|
|
103
|
+
})
|
|
104
|
+
// share connection failed
|
|
105
|
+
this.Peers.on('peer-share-fail', (data) => {
|
|
106
|
+
let peerFail = {}
|
|
107
|
+
peerFail.type = 'account'
|
|
108
|
+
peerFail.action = 'peer-share-fail'
|
|
109
|
+
peerFail.data = { publickey: data }
|
|
110
|
+
this.wsocket.send(JSON.stringify(peerFail))
|
|
111
|
+
})
|
|
112
|
+
// save peer topic
|
|
113
|
+
this.Peers.on('peer-topic-save', async (data) => {
|
|
114
|
+
await this.emit('peer-topic-save', data)
|
|
115
|
+
})
|
|
116
|
+
// peer reconnection topic ie. able to reconnect again
|
|
117
|
+
this.Peers.on('peer-reconnect-topic', (data) => {
|
|
118
|
+
this.emit('peer-reconnect-topic', data)
|
|
138
119
|
})
|
|
139
120
|
// data for beebee
|
|
140
121
|
this.Peers.on('beebee-data', (data) => {
|
|
141
122
|
this.emit('peer-topeer', data)
|
|
142
123
|
})
|
|
124
|
+
// cue space share
|
|
143
125
|
this.Peers.on('cuespace-notification', (data) => {
|
|
144
|
-
console.log('cue space ntototoo')
|
|
145
126
|
this.emit('peer-cuespace', data)
|
|
146
127
|
})
|
|
147
128
|
// public library notification
|
|
148
129
|
this.Peers.on('publiclibrarynotification', (data) => {
|
|
149
130
|
this.BeeData.replicatePubliclibrary(data)
|
|
150
131
|
})
|
|
132
|
+
// beebee notifications public
|
|
151
133
|
this.BeeData.on('publibbeebee-notification', (data) => {
|
|
152
134
|
this.emit('beebee-publib-notification', data)
|
|
153
135
|
})
|
|
154
136
|
// new warm incoming peer
|
|
155
|
-
this.Peers.on('connect-warm', (data) => {
|
|
137
|
+
this.Peers.on('connect-warm-first', (data) => {
|
|
138
|
+
let peerInfo = this.Peers.peerHolder[data]
|
|
139
|
+
if (peerInfo === undefined) {
|
|
140
|
+
// receiving peer
|
|
141
|
+
peerInfo = { name: 'peernew'}
|
|
142
|
+
}
|
|
156
143
|
let peerId = {}
|
|
157
|
-
peerId.name =
|
|
144
|
+
peerId.name = peerInfo.name
|
|
158
145
|
peerId.publickey = data
|
|
159
|
-
peerId.
|
|
146
|
+
peerId.longterm = true
|
|
147
|
+
peerId.settopic = false
|
|
148
|
+
peerId.topic = ''
|
|
149
|
+
peerId.live = false
|
|
160
150
|
this.warmPeers.push(peerId)
|
|
161
|
-
this.emit('peer-incoming', peerId)
|
|
151
|
+
this.emit('peer-incoming-save', peerId)
|
|
162
152
|
})
|
|
153
|
+
// drive listener
|
|
163
154
|
this.DriveFiles.on('largefile-save', (data) => {
|
|
164
155
|
this.emit('drive-save-large', data)
|
|
165
156
|
})
|
|
@@ -179,31 +170,22 @@ class HolepunchWorker extends EventEmitter {
|
|
|
179
170
|
peerMatch = true
|
|
180
171
|
}
|
|
181
172
|
}
|
|
182
|
-
|
|
183
|
-
|
|
173
|
+
if (message.task === 'peer-share-invite') {
|
|
174
|
+
// keep track of role, reciving or extended invite
|
|
175
|
+
this.Peers.setRole(message.data.publickey)
|
|
184
176
|
if (peerMatch === true) {
|
|
185
177
|
this.Peers.peerAlreadyJoinSetData(message.data)
|
|
186
|
-
this.Peers.writeTonetwork(message.data.publickey)
|
|
178
|
+
// this.Peers.writeTonetwork(message.data.publickey)
|
|
179
|
+
this.warmPeerPrepare(message.data.publickey, true)
|
|
187
180
|
} else {
|
|
188
181
|
this.warmPeers.push(message.data)
|
|
189
|
-
this.Peers.peerJoin(message.data)
|
|
190
|
-
}
|
|
191
|
-
} else if (message.task = 'cue-space') {
|
|
192
|
-
console.log('cus space send path')
|
|
193
|
-
console.log(peerMatch)
|
|
194
|
-
if (peerMatch === true) {
|
|
195
|
-
console.log('already connect space')
|
|
196
182
|
this.Peers.peerAlreadyJoinSetData(message.data)
|
|
197
|
-
this.Peers.writeToCueSpace(message.data.publickey)
|
|
198
|
-
} else {
|
|
199
|
-
console.log('first space send')
|
|
200
|
-
this.warmPeers.push(message.data)
|
|
201
183
|
this.Peers.peerJoin(message.data)
|
|
202
|
-
// this.Peers.peerAlreadyJoinSetData(message.data)
|
|
203
|
-
// this.Peers.writeToCueSpace(message.data.publickey)
|
|
204
|
-
console.log('space suce peepr ot sent')
|
|
205
184
|
}
|
|
206
|
-
} else if (message.task === 'peer-
|
|
185
|
+
} else if (message.task === 'peer-share-topic') {
|
|
186
|
+
// existing peers reconnecting via topic
|
|
187
|
+
this.Peers.topicConnect(message.data)
|
|
188
|
+
} else if (message.task === 'public-n1-experiment') {
|
|
207
189
|
if (peerMatch === true) {
|
|
208
190
|
this.Peers.peerAlreadyJoinSetData(message.data)
|
|
209
191
|
this.Peers.writeToPublicLibrary(message.data.publickey)
|
|
@@ -214,6 +196,16 @@ class HolepunchWorker extends EventEmitter {
|
|
|
214
196
|
this.Peers.peerAlreadyJoinSetData(message.data)
|
|
215
197
|
this.Peers.writeToPublicLibrary(message.data.publickey)
|
|
216
198
|
}
|
|
199
|
+
} else if (message.task = 'cue-space') {
|
|
200
|
+
if (peerMatch === true) {
|
|
201
|
+
this.Peers.peerAlreadyJoinSetData(message.data)
|
|
202
|
+
this.Peers.writeToCueSpace(message.data.publickey)
|
|
203
|
+
} else {
|
|
204
|
+
this.warmPeers.push(message.data)
|
|
205
|
+
this.Peers.peerJoin(message.data)
|
|
206
|
+
// this.Peers.peerAlreadyJoinSetData(message.data)
|
|
207
|
+
// this.Peers.writeToCueSpace(message.data.publickey)
|
|
208
|
+
}
|
|
217
209
|
} else if (message.task === 'peer-write') {
|
|
218
210
|
this.emit('peer-write', message.data)
|
|
219
211
|
} else if (message.task === 'topic') {
|
|
@@ -222,6 +214,44 @@ class HolepunchWorker extends EventEmitter {
|
|
|
222
214
|
}
|
|
223
215
|
}
|
|
224
216
|
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* prepare data to send to a warm peer
|
|
220
|
+
* @method warmPeerPrepare
|
|
221
|
+
*/
|
|
222
|
+
warmPeerPrepare = function (data, existing) {
|
|
223
|
+
// two checks, if topic send to other peer
|
|
224
|
+
if (existing !== true) {
|
|
225
|
+
let peerRole = this.Peers.peersRole[data]
|
|
226
|
+
if (peerRole === undefined) {
|
|
227
|
+
this.Peers.writeTonetworkTopic(data)
|
|
228
|
+
} else {
|
|
229
|
+
console.log(' this peer set the topic')
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
// if data within coming then process that
|
|
233
|
+
let peerDataExist = this.Peers.peerHolder[data]
|
|
234
|
+
if (peerDataExist === undefined) {
|
|
235
|
+
} else {
|
|
236
|
+
// what type of data being shared?
|
|
237
|
+
// check for data along with new peer?
|
|
238
|
+
if (peerDataExist.data !== undefined) {
|
|
239
|
+
if (peerDataExist.data.type === 'private-chart') {
|
|
240
|
+
this.Peers.writeTonetworkData(data, peerDataExist.data)
|
|
241
|
+
} else if (peerDataExist.data.type === 'private-cue-space') {
|
|
242
|
+
this.Peers.writeToCueSpace(this.Peers.peerHolder[peerFirstID].publickey)
|
|
243
|
+
} else if (peerDataExist.data.type === 'public-n1-experiment') {
|
|
244
|
+
this.Peers.writeTonetworkData(data, peerDataExist.data)
|
|
245
|
+
} else if (peerDataExist.data.type === 'public-library') {
|
|
246
|
+
this.Peers.writeToPublicLibrary(data)
|
|
247
|
+
} else if (peerDataExist.data.type === 'text-message') {
|
|
248
|
+
// simpole text message
|
|
249
|
+
this.Peers.writeTonetwork(data)
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
225
255
|
/**
|
|
226
256
|
* corestore test example
|
|
227
257
|
* @method testCorestore
|
package/src/peers.js
CHANGED
|
@@ -9,9 +9,6 @@
|
|
|
9
9
|
* @version $Id$
|
|
10
10
|
*/
|
|
11
11
|
import EventEmitter from 'events'
|
|
12
|
-
// import DHT from '@hyperswarm/dht'
|
|
13
|
-
import goodbye from 'graceful-goodbye'
|
|
14
|
-
import b4a from 'b4a'
|
|
15
12
|
|
|
16
13
|
|
|
17
14
|
class NetworkPeers extends EventEmitter {
|
|
@@ -24,6 +21,7 @@ class NetworkPeers extends EventEmitter {
|
|
|
24
21
|
this.drive = {}
|
|
25
22
|
this.peerHolder = {}
|
|
26
23
|
this.peerConnect = {}
|
|
24
|
+
this.peersRole = {}
|
|
27
25
|
}
|
|
28
26
|
|
|
29
27
|
/**
|
|
@@ -32,6 +30,17 @@ class NetworkPeers extends EventEmitter {
|
|
|
32
30
|
*
|
|
33
31
|
*/
|
|
34
32
|
networkKeys = function () {
|
|
33
|
+
// console.log('swarm on start')
|
|
34
|
+
// console.log(this.swarm._discovery) // .toString('hex'))
|
|
35
|
+
|
|
36
|
+
/*this.swarm._discovery.forEach((value, key) => {
|
|
37
|
+
console.log('key')
|
|
38
|
+
console.log(key)
|
|
39
|
+
console.log('-----------swarm discovery IN-------------------')
|
|
40
|
+
console.log(Object.keys(value))
|
|
41
|
+
console.log(value.topic)
|
|
42
|
+
console.log(value.topic.toString('hex'))
|
|
43
|
+
}) */
|
|
35
44
|
let peerNxKeys = {}
|
|
36
45
|
peerNxKeys.publickey = this.swarm.keyPair.publicKey.toString('hex')
|
|
37
46
|
let networkMessage = {}
|
|
@@ -48,15 +57,14 @@ class NetworkPeers extends EventEmitter {
|
|
|
48
57
|
* @method listenNetwork
|
|
49
58
|
*
|
|
50
59
|
*/
|
|
51
|
-
listenNetwork = function
|
|
60
|
+
listenNetwork = function () {
|
|
52
61
|
this.swarm.on('connection', (conn, info) => {
|
|
53
|
-
//
|
|
54
|
-
this.store.replicate(conn)
|
|
55
|
-
// listener to write message to peers or network partial or broadcast
|
|
62
|
+
// save peer connection instance for ongoing communication
|
|
56
63
|
let publicKeylive = info.publicKey.toString('hex')
|
|
57
|
-
this.emit('connect-warm', publicKeylive)
|
|
58
64
|
this.peerConnect[publicKeylive] = conn
|
|
59
|
-
this.emit('
|
|
65
|
+
this.emit('connect-warm-first', publicKeylive)
|
|
66
|
+
// listen for replication NEED UPTATED LOGIC
|
|
67
|
+
this.store.replicate(conn)
|
|
60
68
|
// process network message
|
|
61
69
|
conn.on('data', data =>
|
|
62
70
|
// assess data
|
|
@@ -66,6 +74,15 @@ class NetworkPeers extends EventEmitter {
|
|
|
66
74
|
})
|
|
67
75
|
}
|
|
68
76
|
|
|
77
|
+
/**
|
|
78
|
+
* set role in peer to peer relationship, invte or receive?
|
|
79
|
+
* @method setRole
|
|
80
|
+
*
|
|
81
|
+
*/
|
|
82
|
+
setRole = function (pubKey) {
|
|
83
|
+
let setRole = { send: 'prime' , invite: pubKey}
|
|
84
|
+
this.peersRole[pubKey] = setRole
|
|
85
|
+
}
|
|
69
86
|
/**
|
|
70
87
|
*
|
|
71
88
|
* @method assessData data and act
|
|
@@ -75,21 +92,21 @@ class NetworkPeers extends EventEmitter {
|
|
|
75
92
|
if (Buffer.isBuffer(data)) {
|
|
76
93
|
try {
|
|
77
94
|
let dataShareIn = JSON.parse(data.toString())
|
|
78
|
-
|
|
79
|
-
console.log(dataShareIn.type)
|
|
80
|
-
if (dataShareIn.type === 'chart') {
|
|
95
|
+
if (dataShareIn.type === 'private-chart') {
|
|
81
96
|
this.emit('beebee-data', dataShareIn)
|
|
82
97
|
// need to look at NXP, modules and within for reference contracts.
|
|
83
98
|
// Need to replicate public library for contracts (repliate hyberbee)
|
|
84
99
|
// Need to ask for data source e.g. file (replicate hyberdrive)
|
|
85
100
|
// Lastly put together SafeFlowECS query to produce chart
|
|
86
|
-
} else if (dataShareIn.type === 'cue-space') {
|
|
101
|
+
} else if (dataShareIn.type === 'private-cue-space') {
|
|
87
102
|
this.emit('cuespace-notification', dataShareIn)
|
|
88
103
|
} else if (dataShareIn.type === 'public-library') {
|
|
89
104
|
this.emit('publiclibrarynotification', dataShareIn)
|
|
90
105
|
} else if (dataShareIn.type === 'peer') {
|
|
106
|
+
} else if (dataShareIn.type === 'topic-reconnect') {
|
|
107
|
+
// peer has share a topic for future reconnect
|
|
108
|
+
this.emit('peer-reconnect-topic', dataShareIn)
|
|
91
109
|
}
|
|
92
|
-
console.log(a)
|
|
93
110
|
} catch (e) {
|
|
94
111
|
return console.error('ignore err')
|
|
95
112
|
}
|
|
@@ -101,22 +118,40 @@ class NetworkPeers extends EventEmitter {
|
|
|
101
118
|
* @method writeTonetwork
|
|
102
119
|
*
|
|
103
120
|
*/
|
|
104
|
-
writeTonetwork = function (
|
|
121
|
+
writeTonetwork = function (data, messType) {
|
|
105
122
|
// check this peer has asked for chart data
|
|
106
|
-
let
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
123
|
+
let dataSend = data
|
|
124
|
+
this.peerConnect[data].write(JSON.stringify(dataSend))
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* write message to network
|
|
129
|
+
* @method writeTonetworkTopic
|
|
130
|
+
*
|
|
131
|
+
*/
|
|
132
|
+
writeTonetworkTopic = function (publickey) {
|
|
133
|
+
let topicGeneration = 'kprel135811'
|
|
134
|
+
// need to save the topic initiator of warm peer save relationship
|
|
135
|
+
this.emit('peer-topic-save', { peerkey: publickey, topic: topicGeneration })
|
|
136
|
+
// send to other peer topic to allow reconnection in future
|
|
137
|
+
let topicShare = {}
|
|
138
|
+
topicShare.type = 'topic-reconnect'
|
|
139
|
+
topicShare.publickey = this.swarm.keyPair.publicKey.toString('hex')
|
|
140
|
+
topicShare.data = topicGeneration
|
|
141
|
+
// inform peer that topic has been created
|
|
142
|
+
this.peerConnect[publickey].write(JSON.stringify(topicShare))
|
|
118
143
|
}
|
|
119
144
|
|
|
145
|
+
/**
|
|
146
|
+
* write message to network
|
|
147
|
+
* @method writeTonetworkData
|
|
148
|
+
*
|
|
149
|
+
*/
|
|
150
|
+
writeTonetworkData = function (publickey, dataShare) {
|
|
151
|
+
this.peerConnect[publickey].write(JSON.stringify(dataShare))
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
|
|
120
155
|
/**
|
|
121
156
|
* write message connect public library
|
|
122
157
|
* @method writeToPublicLibrary
|
|
@@ -148,10 +183,7 @@ class NetworkPeers extends EventEmitter {
|
|
|
148
183
|
let spaceTrue = publickey in this.peerHolder
|
|
149
184
|
if (connectTrue === true && spaceTrue === true) {
|
|
150
185
|
let libraryData = this.peerHolder[publickey]
|
|
151
|
-
|
|
152
|
-
dataShare.data = libraryData.data
|
|
153
|
-
dataShare.type = 'cue-space'
|
|
154
|
-
this.peerConnect[publickey].write(JSON.stringify(dataShare))
|
|
186
|
+
this.peerConnect[publickey].write(JSON.stringify(libraryData))
|
|
155
187
|
} else {
|
|
156
188
|
console.log('no cuespace to write ie share with a peer')
|
|
157
189
|
}
|
|
@@ -159,13 +191,13 @@ class NetworkPeers extends EventEmitter {
|
|
|
159
191
|
|
|
160
192
|
|
|
161
193
|
/**
|
|
162
|
-
* join peer to peer private (server)
|
|
194
|
+
* join peer to peer direct private (server)
|
|
163
195
|
* @method peerJoin
|
|
164
196
|
*
|
|
165
197
|
*/
|
|
166
198
|
peerJoin = function (peerContext) {
|
|
167
|
-
|
|
168
|
-
|
|
199
|
+
// set timeer to inform if not connection can be established
|
|
200
|
+
this. checkTimerConnection(peerContext.publickey)
|
|
169
201
|
this.peerHolder[peerContext.publickey] = peerContext
|
|
170
202
|
const noisePublicKey = Buffer.from(peerContext.publickey, 'hex') // must be 32 bytes
|
|
171
203
|
if (noisePublicKey.length === 32) {
|
|
@@ -173,6 +205,37 @@ class NetworkPeers extends EventEmitter {
|
|
|
173
205
|
}
|
|
174
206
|
}
|
|
175
207
|
|
|
208
|
+
/**
|
|
209
|
+
* give 2 seconds for connection to establish
|
|
210
|
+
* @method checkTimerConnection
|
|
211
|
+
*
|
|
212
|
+
*/
|
|
213
|
+
checkTimerConnection (key) {
|
|
214
|
+
// if peerconnect not set the inform beebee not connection accepted try again
|
|
215
|
+
let localthis = this
|
|
216
|
+
// setTimeout(checkPeerState(localthis, key), 2000)
|
|
217
|
+
setTimeout(() => checkPeerState(localthis, key), 6000)
|
|
218
|
+
|
|
219
|
+
function checkPeerState (localthis, publicKeylive) {
|
|
220
|
+
if (localthis.peerConnect[publicKeylive] === undefined) {
|
|
221
|
+
// failed peer connection
|
|
222
|
+
localthis.emit('peer-share-fail', publicKeylive)
|
|
223
|
+
} else {
|
|
224
|
+
// connnection established
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* leave a direct peer connection
|
|
231
|
+
* @method peerLeave
|
|
232
|
+
*
|
|
233
|
+
*/
|
|
234
|
+
peerLeave = function (peerLeaveKey) {
|
|
235
|
+
this.peerHolder[peerLeaveKey] = {}
|
|
236
|
+
this.swarm.leavePeer(peerLeaveKey)
|
|
237
|
+
}
|
|
238
|
+
|
|
176
239
|
/**
|
|
177
240
|
* already joined but keep track context data
|
|
178
241
|
* @method peerAlreadyJoinSetData
|
|
@@ -180,6 +243,7 @@ class NetworkPeers extends EventEmitter {
|
|
|
180
243
|
*/
|
|
181
244
|
peerAlreadyJoinSetData = function (peerContext) {
|
|
182
245
|
this.peerHolder[peerContext.publickey] = peerContext
|
|
246
|
+
return true
|
|
183
247
|
}
|
|
184
248
|
|
|
185
249
|
|
|
@@ -193,48 +257,34 @@ class NetworkPeers extends EventEmitter {
|
|
|
193
257
|
}
|
|
194
258
|
|
|
195
259
|
/**
|
|
196
|
-
*
|
|
197
|
-
* @method
|
|
260
|
+
* out message topics as a client
|
|
261
|
+
* @method topicConnect
|
|
198
262
|
*
|
|
199
263
|
*/
|
|
200
|
-
|
|
264
|
+
topicConnect = async function (topic) {
|
|
201
265
|
const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
|
|
202
|
-
const peerConnect = this.swarm.join(noisePublicKey, { server:
|
|
266
|
+
const peerConnect = this.swarm.join(noisePublicKey, { server: true, client: false })
|
|
203
267
|
await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
|
|
204
268
|
}
|
|
205
269
|
|
|
206
270
|
/**
|
|
207
|
-
*
|
|
208
|
-
* @method
|
|
271
|
+
* out message topics as a client
|
|
272
|
+
* @method topicListen
|
|
209
273
|
*
|
|
210
274
|
*/
|
|
211
|
-
|
|
212
|
-
|
|
275
|
+
topicListen = async function (topic) {
|
|
276
|
+
const noisePublicKey = Buffer.alloc(32).fill(topic) // A topic must be 32 bytes
|
|
277
|
+
const peerConnect = this.swarm.join(noisePublicKey, { server: false, client: true })
|
|
278
|
+
await peerConnect.flushed() // Waits for the topic to be fully announced on the DHT
|
|
213
279
|
}
|
|
214
280
|
|
|
215
281
|
/**
|
|
216
|
-
*
|
|
217
|
-
* @method
|
|
282
|
+
* leave topic
|
|
283
|
+
* @method leaveTopic
|
|
218
284
|
*
|
|
219
285
|
*/
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
// This keypair is your peer identifier in the DHT
|
|
224
|
-
const keyPair = DHT.keyPair()
|
|
225
|
-
|
|
226
|
-
const server = dht.createServer(conn => {
|
|
227
|
-
console.log('got connection!')
|
|
228
|
-
process.stdin.pipe(conn).pipe(process.stdout)
|
|
229
|
-
})
|
|
230
|
-
|
|
231
|
-
server.listen(keyPair).then(() => {
|
|
232
|
-
console.log('listening on:', b4a.toString(keyPair.publicKey, 'hex'))
|
|
233
|
-
})
|
|
234
|
-
|
|
235
|
-
// Unnannounce the public key before exiting the process
|
|
236
|
-
// (This is not a requirement, but it helps avoid DHT pollution)
|
|
237
|
-
goodbye(() => server.close()) */
|
|
286
|
+
leaveTopic = async function (topic) {
|
|
287
|
+
await this.swarm.leave(topic)
|
|
238
288
|
}
|
|
239
289
|
|
|
240
290
|
}
|