diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2016-05-05 16:12:58 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2016-05-05 16:12:58 +0300 |
commit | f09c24ab46b6748c6948eedb8af85075ca71692f (patch) | |
tree | 330c1078c2a7bf36b9575d8de9059825cff60f29 | |
parent | 43aeb613cadb0fa986902f20c66f64e76cc427a4 (diff) | |
parent | 97130eb7de2db1eac9f91482ca92b2da3b975eab (diff) |
Merge pull request #775 from harperj/no-peers
Add 'noPeers' event to torrents
-rw-r--r-- | docs/api.md | 4 | ||||
-rw-r--r-- | lib/torrent.js | 2 | ||||
-rw-r--r-- | test/node/download-dht-torrent.js | 11 | ||||
-rw-r--r-- | test/node/download-tracker-magnet.js | 6 |
4 files changed, 20 insertions, 3 deletions
diff --git a/docs/api.md b/docs/api.md index 6c47901..a4ebe55 100644 --- a/docs/api.md +++ b/docs/api.md @@ -422,6 +422,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` diff --git a/lib/torrent.js b/lib/torrent.js index d4c55e3..810e10d 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -333,10 +333,12 @@ Torrent.prototype._onListening = function () { function onTrackerAnnounce () { self.emit('trackerAnnounce') + if (self.numPeers === 0) self.emit('noPeers', 'tracker') } function onDHTAnnounce () { self.emit('dhtAnnounce') + 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 20c7b0c..0bc6961 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 }) @@ -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 a310350..7224895 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) { |