-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
41 lines (34 loc) · 965 Bytes
/
index.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
var createSwarm = require('hyperdiscovery')
var createStream = require('hypercore-stream')
module.exports = function (key, opts) {
if (!opts) opts = {}
var stream = createStream(key, opts)
var swarm
if (!opts.static || !stream.write) {
swarm = createSwarm(stream.feed, { port: null })
patch(stream, swarm)
if (opts.exit) {
stream.on('end', function () {
swarm.close()
})
stream.on('finish', function () {
swarm.close()
})
}
} else {
stream.on('finish', function () {
if (!opts.exit) {
swarm = createSwarm(stream.feed, { port: null })
patch(stream, swarm)
}
})
}
return stream
}
function patch (stream, swarm) {
swarm.on('error', stream.emit.bind(stream, 'error'))
swarm.on('connection', stream.emit.bind(stream, 'connection'))
swarm.on('close', stream.emit.bind(stream, 'close'))
stream.swarm = swarm
stream.close = swarm.close.bind(swarm)
}