diff --git a/lib/server/swarm.js b/lib/server/swarm.js index f44f3cf..d07f061 100644 --- a/lib/server/swarm.js +++ b/lib/server/swarm.js @@ -21,15 +21,18 @@ Swarm.prototype.announce = function (params, cb) { // Mark the source peer as recently used in cache var peer = self.peers.get(id) - if (params.event === 'started') { + // Get the peer back in swarm if missing + if (params.event === 'started' || !peer) { self._onAnnounceStarted(params, peer) - } else if (params.event === 'stopped') { + } + + if (params.event === 'stopped') { self._onAnnounceStopped(params, peer) } else if (params.event === 'completed') { self._onAnnounceCompleted(params, peer) } else if (params.event === 'update') { self._onAnnounceUpdate(params, peer) - } else { + } else if (params.event !== 'started') { cb(new Error('invalid event')) return } @@ -91,6 +94,8 @@ Swarm.prototype._onAnnounceCompleted = function (params, peer) { this.complete += 1 this.incomplete -= 1 peer.complete = true + var id = params.type === 'ws' ? params.peer_id : params.addr + this.peers.set(id, peer) } Swarm.prototype._onAnnounceUpdate = function (params, peer) { @@ -103,6 +108,8 @@ Swarm.prototype._onAnnounceUpdate = function (params, peer) { this.complete += 1 this.incomplete -= 1 peer.complete = true + var id = params.type === 'ws' ? params.peer_id : params.addr + this.peers.set(id, peer) } }