Browse Source

Merge branch 'update-for-hypercore-8' of CoBox/cobox-swarm into development

kyphae 2 years ago
parent
commit
28ef495ab9
3 changed files with 20 additions and 14 deletions
  1. 3
    3
      index.js
  2. 5
    4
      lib/discovery-swarm.js
  3. 12
    7
      lib/hyperswarm.js

+ 3
- 3
index.js View File

@@ -7,9 +7,9 @@ module.exports = function swarm (group, opts = {}) {
7 7
 
8 8
   log(`swarming on ${group.discoveryKey.toString('hex')}`)
9 9
 
10
-  var swarm = opts.hyperswarm
11
-    ? hyperswarm(group, { log })
12
-    : discoverySwarm(group, { log })
10
+  var swarm = opts.discoverySwarm
11
+    ? discoverySwarm(group, { log })
12
+    : hyperswarm(group, { log })
13 13
 
14 14
   return swarm
15 15
 }

+ 5
- 4
lib/discovery-swarm.js View File

@@ -9,16 +9,17 @@ module.exports = function (group, opts) {
9 9
   swarm.on('connection', (connection, peer) => {
10 10
     const peerId = peer.id.toString('hex')
11 11
     log(`connected to peer: ${peerId}`)
12
-
13
-    pump(connection, group.drive.replicate(), connection)
14
-    group._addConnection(peer, () => {})
12
+    swarm.on('handshaking', (connection, info) => {
13
+      pump(connection, group.replicate(info.initiator), connection)
14
+      group._addConnection(peer, () => {})
15
+    })
15 16
   })
16 17
 
17 18
   swarm.on('connection-closed', (connection, peer) => {
18 19
     const peerId = peer.id.toString('hex')
19 20
     log(`disconnected from peer: ${peerId}`)
20 21
 
21
-    group._removeConnection(peer, () => {})
22
+    group._dropConnection(peer, () => {})
22 23
   })
23 24
 
24 25
   swarm.join(group.discoveryKey)

+ 12
- 7
lib/hyperswarm.js View File

@@ -1,9 +1,12 @@
1 1
 const hyperswarm = require('hyperswarm')
2 2
 const pump = require('pump')
3
+const util = require('util')
4
+const assert = require('assert')
3 5
 
4 6
 module.exports = function (group, opts) {
5 7
   var log = opts.log || noop
6 8
   var swarm = hyperswarm()
9
+  assert(group.discoveryKey, 'Cannot read group discovery key')
7 10
 
8 11
   swarm.on('listening', () => {
9 12
     log('listening...')
@@ -15,21 +18,23 @@ module.exports = function (group, opts) {
15 18
   // swarm.on('peer', (peer) => {})
16 19
 
17 20
   swarm.on('connection', (socket, details) => {
18
-    log(`connected ${JSON.stringify(details, null, 4)}`)
19
-
20 21
     // if using hypercore 8, we should be passing details.client to .replicate
21 22
     // pump(socket, group.drive.core.replicate(details.client, { live: true }), socket)
22
-    pump(socket, group.drive.replicate(), socket)
23
+    pump(socket, group.replicate(details.client), socket)
23 24
 
24 25
     // Ideally we need a way to lookup peers public key from their ip address
25 26
     // (which means making a change to multifeed or using decentstack)
26
-    group._addConnection(details.peer.host, () => {})
27
+    if (details.peer) {
28
+      log(`connected ${util.inspect(details, null, 4)}`)
29
+      group._addConnection(details.peer.host, () => {})
30
+    }
27 31
   })
28 32
 
29 33
   swarm.on('disconnection', (socket, details) => {
30
-    log(`disconnected from peer: ${details.peer.host}`)
31
-
32
-    group._removeConnection(details.peer.host, () => {})
34
+    if (details.peer) {
35
+      log(`disconnected from peer: ${details.peer.host}`)
36
+      group._dropConnection(details.peer.host, () => {})
37
+    }
33 38
   })
34 39
   swarm.join(group.discoveryKey, { lookup: true, announce: true })
35 40