-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
32 lines (29 loc) · 922 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
var { EventEmitter } = require('events')
module.exports = Replicate
function Replicate (storage, protocol, opts) {
var self = this
if (!(self instanceof Replicate)) {
return new Replicate(storage, protocol, opts)
}
self._protocol = protocol
self._storage = storage
protocol.on('discovery-key', function (dkey) {
storage.fromDiscoveryKey(dkey, function (err, key) {
if (err) return self.emit('error', err)
if (!key) return
storage.get(key, function (err, feed) {
if (err) self.emit('error', err)
else feed.replicate(self._protocol, opts)
})
})
})
}
Replicate.prototype = Object.create(EventEmitter.prototype)
Replicate.prototype.open = function (key, opts) {
var self = this
if (!opts) opts = {}
self._storage.getOrCreateRemote(key, function (err, feed) {
if (err) return self.emit('error', err)
feed.replicate(self._protocol, opts)
})
}