From cd7fcb2d797cccf9507557493146e6a498f16861 Mon Sep 17 00:00:00 2001 From: Jonathan Harper Date: Tue, 26 Apr 2016 20:59:29 -0700 Subject: [PATCH 1/3] Torrent emits 'noPeers' even when swarm is empty after announce --- lib/torrent.js | 2 ++ test/node/download-dht-torrent.js | 9 ++++++++- test/node/download-tracker-magnet.js | 6 +++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/torrent.js b/lib/torrent.js index b574fbb4..afd06910 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -333,10 +333,12 @@ Torrent.prototype._onListening = function () { function onTrackerAnnounce () { self.emit('trackerAnnounce') + if (self.swarm.numPeers === 0) self.emit('noPeers', 'tracker') } function onDHTAnnounce () { self.emit('dhtAnnounce') + if (self.swarm.numPeers === 0) self.emit('noPeers', 'dht') } function onWarning (err) { diff --git a/test/node/download-dht-torrent.js b/test/node/download-dht-torrent.js index 20c7b0c4..874288de 100644 --- a/test/node/download-dht-torrent.js +++ b/test/node/download-dht-torrent.js @@ -53,10 +53,17 @@ test('Download using DHT (via .torrent file)', function (t) { maybeDone(null) }) + torrent.on('noPeers', function (announceType) { + t.equal(announceType, 'dht', 'noPeers event seen with correct announceType') + noPeersFound = true + maybeDone(null) + }) + var announced = false var loaded = false + var noPeersFound = false function maybeDone (err) { - if ((announced && loaded) || err) cb(err, client1) + if ((announced && loaded && noPeersFound) || err) cb(err, client1) } }, diff --git a/test/node/download-tracker-magnet.js b/test/node/download-tracker-magnet.js index a3103502..7224895f 100644 --- a/test/node/download-tracker-magnet.js +++ b/test/node/download-tracker-magnet.js @@ -15,7 +15,7 @@ test('Download using HTTP tracker (via magnet uri)', function (t) { }) function magnetDownloadTest (t, serverType) { - t.plan(9) + t.plan(10) var tracker = new TrackerServer( serverType === 'udp' ? { http: false, ws: false } : { udp: false, ws: false } @@ -59,6 +59,10 @@ function magnetDownloadTest (t, serverType) { 'Leaves of Grass by Walt Whitman.epub' ] + torrent.on('noPeers', function (announceType) { + t.equal(announceType, 'tracker', 'noPeers event seen with correct announceType') + }) + t.deepEqual(torrent.files.map(function (file) { return file.name }), names) torrent.load(fs.createReadStream(fixtures.leaves.contentPath), function (err) { From 98248991cf7aa909044b5461969ed55ad4a9c8d9 Mon Sep 17 00:00:00 2001 From: Jonathan Harper Date: Tue, 26 Apr 2016 21:05:00 -0700 Subject: [PATCH 2/3] Document 'noPeers' torrent event --- docs/api.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/api.md b/docs/api.md index b0e3ebaf..99e3814b 100644 --- a/docs/api.md +++ b/docs/api.md @@ -413,6 +413,10 @@ See the `bittorrent-protocol` [extension api docs](https://github.com/feross/bittorrent-protocol#extension-api) for more information on how to define a protocol extension. +## `torrent.on('noPeers', function (announceType) {})` + +Emitted whenever a DHT or tracker announce occurs, but no peers have been found. `announceType` is either `'tracker'` or `'dht'` depending on which announce occurred to trigger this event. Note that if you're attempting to discover peers from both a tracker and a DHT, you'll see this event separately for each. + # File API ## `file.name` From 97130eb7de2db1eac9f91482ca92b2da3b975eab Mon Sep 17 00:00:00 2001 From: Jonathan Harper Date: Tue, 26 Apr 2016 21:19:54 -0700 Subject: [PATCH 3/3] Update 'noPeers' to stop using deprecated 'torrent.swarm' --- lib/torrent.js | 4 ++-- test/node/download-dht-torrent.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/torrent.js b/lib/torrent.js index afd06910..ab84a9a5 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -333,12 +333,12 @@ Torrent.prototype._onListening = function () { function onTrackerAnnounce () { self.emit('trackerAnnounce') - if (self.swarm.numPeers === 0) self.emit('noPeers', 'tracker') + if (self.numPeers === 0) self.emit('noPeers', 'tracker') } function onDHTAnnounce () { self.emit('dhtAnnounce') - if (self.swarm.numPeers === 0) self.emit('noPeers', 'dht') + if (self.numPeers === 0) self.emit('noPeers', 'dht') } function onWarning (err) { diff --git a/test/node/download-dht-torrent.js b/test/node/download-dht-torrent.js index 874288de..0bc69614 100644 --- a/test/node/download-dht-torrent.js +++ b/test/node/download-dht-torrent.js @@ -6,7 +6,7 @@ var test = require('tape') var WebTorrent = require('../../') test('Download using DHT (via .torrent file)', function (t) { - t.plan(9) + t.plan(10) var dhtServer = new DHT({ bootstrap: false })