From 1338282cc92fc83b5be4714144d54ba38e0b400a Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sun, 3 May 2015 16:38:16 -0700 Subject: [PATCH] prevent negative complete/incomplete numbers Fixes #65 Fixes #72 --- lib/swarm.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/swarm.js b/lib/swarm.js index adf8406..5faa3c2 100644 --- a/lib/swarm.js +++ b/lib/swarm.js @@ -19,8 +19,6 @@ Swarm.prototype.announce = function (params, cb) { if (self[fn]) { self[fn](params, peer) // process event - if (params.left === 0 && peer) peer.complete = true - cb(null, { complete: self.complete, incomplete: self.incomplete, @@ -86,6 +84,12 @@ Swarm.prototype._onAnnounce_update = function (params, peer) { debug('unexpected `update` event from peer that is not in swarm') return this._onAnnounce_started(params, peer) // treat as a start } + + if (!peer.complete && params.left === 0) { + this.complete += 1 + this.incomplete -= 1 + peer.complete = true + } } // TODO: randomize the peers that are given out