-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhyper.js
More file actions
38 lines (32 loc) · 892 Bytes
/
hyper.js
File metadata and controls
38 lines (32 loc) · 892 Bytes
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
const crypto = require('crypto');
const duplexify = require('duplexify');
const hyperswarm = require('hyperswarm');
function initiate (topic, opts) {
let net = hyperswarm()
// look for peers listed under this topic
var topicBuffer = crypto.createHash('sha256')
.update(topic)
.digest()
net.join(topicBuffer, opts)
return net
}
var hyperTopic;
exports.connect = function (topic, cb) {
hyperTopic = topic;
var net = initiate(topic, {
lookup: true, // find & connect to peers
announce: true
})
net.on('connection', (socket, details) => {
if (details.peer)
console.error('hyper-lru connected to', details.peer.host, details.peer.port)
cb(null, socket)
// we have received everything
socket.on('end', function (topic) {
if (topic) net.leave(topic)
})
socket.on('error', function (e) {
console.error(e);
})
})
}