-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathswarm.js
60 lines (50 loc) · 1.53 KB
/
swarm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
const pump = require('pump')
const Swarm = require('hyperswarm')
const DHT = require('hyperdht')
const debug = require('debug')('cabal')
const crypto = require('hypercore-crypto')
const bootstrapNodes = require('./bootstrap_nodes')
module.exports = function (cabal, opts, cb) {
if (typeof opts === 'function') {
cb = opts
opts = {}
}
cb = cb || function () {}
opts = opts || {}
cabal.getLocalKey(function (err, localKey) {
if (err) return cb(err)
const discoveryKey = crypto.discoveryKey(Buffer.from(cabal.key, 'hex'))
const dht = new DHT({bootstrap: bootstrapNodes})
opts.dht = dht
const swarm = new Swarm(opts)
const disco = swarm.join(discoveryKey, {
server: true,
client: true
})
swarm.__shutdown = async function () {
await disco.destroy()
await swarm.destroy()
}
swarm.on('connection', function (socket, info) {
let remoteKey
var r = cabal.replicate(info.client)
pump(socket, r, socket, function (err) {
if (err) debug('ERROR', err)
if (remoteKey) cabal._removeConnection(remoteKey)
})
const ext = r.registerExtension('peer-id', {
encoding: 'json',
onmessage (message, peer) {
if (remoteKey) return
if (!message.id) return
const buf = Buffer.from(message.id, 'hex')
if (!buf || buf.length !== 32) return
remoteKey = message.id
cabal._addConnection(remoteKey)
}
})
ext.send({id:localKey})
})
cb(null, swarm)
})
}