diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2016-04-21 13:10:29 +0300 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2016-04-21 13:10:29 +0300 |
commit | fa2a6c541849853ded575b95ee424ab94c0936a4 (patch) | |
tree | cf58865f0c2b84e26fb5899de41bd92628109d87 | |
parent | 597d1ce10ce20ae33b0118d85cfdbc8cbabcb32a (diff) |
fix more bugs
-rw-r--r-- | index.js | 15 | ||||
-rw-r--r-- | lib/torrent.js | 35 |
2 files changed, 30 insertions, 20 deletions
@@ -278,21 +278,24 @@ WebTorrent.prototype.seed = function (input, opts, onseed) { else cb(null, item) } }), function (err, input) { - if (err) return torrent._destroy(err) if (self.destroyed) return + if (err) return torrent._destroy(err) + createTorrent.parseInput(input, opts, function (err, files) { - if (err) return torrent._destroy(err) if (self.destroyed) return - streams = files.map(function (file) { return file.getStream }) + if (err) return torrent._destroy(err) + + streams = files.map(function (file) { + return file.getStream + }) createTorrent(input, opts, function (err, torrentBuf) { - if (err) return torrent._destroy(err) if (self.destroyed) return + if (err) return torrent._destroy(err) var existingTorrent = self.get(torrentBuf) if (existingTorrent) { - torrent._destroy(new Error('Cannot add duplicate torrent ' + torrent.infoHash)) - _onseed(existingTorrent) + torrent._destroy(new Error('Cannot add duplicate torrent ' + existingTorrent.infoHash)) } else { torrent._onTorrentId(torrentBuf) } diff --git a/lib/torrent.js b/lib/torrent.js index a105148..f24b52c 100644 --- a/lib/torrent.js +++ b/lib/torrent.js @@ -1,5 +1,3 @@ -// TODO: cleanup event listeners - /* global URL, Blob */ module.exports = Torrent @@ -315,25 +313,34 @@ Torrent.prototype._onListening = function () { tracker: self.client.tracker && trackerOpts, port: self.client.torrentPort }) - self.discovery.on('error', function (err) { + + self.discovery.on('error', onError) + self.discovery.on('peer', onPeer) + self.discovery.on('trackerAnnounce', onTrackerAnnounce) + self.discovery.on('dhtAnnounce', onDHTAnnounce) + self.discovery.on('warning', onWarning) + + function onError (err) { self._destroy(err) - }) - self.discovery.on('peer', function (peer) { + } + + function onPeer (peer) { // Don't create new outgoing TCP connections when torrent is done if (typeof peer === 'string' && self.done) return self.addPeer(peer) - }) + } - // expose discovery events - self.discovery.on('trackerAnnounce', function () { + function onTrackerAnnounce () { self.emit('trackerAnnounce') - }) - self.discovery.on('dhtAnnounce', function () { + } + + function onDHTAnnounce () { self.emit('dhtAnnounce') - }) - self.discovery.on('warning', function (err) { + } + + function onWarning (err) { self.emit('warning', err) - }) + } // if full metadata was included in initial torrent id, use it immediately. Otherwise, // wait for torrent-discovery to find peers and ut_metadata to get the metadata. @@ -611,7 +618,7 @@ Torrent.prototype.addPeer = function (peer) { if (host && self.client.blocked.contains(host)) { self._debug('ignoring peer: blocked %s', peer) - if (typeof peer === 'string') peer.destroy() + if (typeof peer !== 'string') peer.destroy() self.emit('blockedPeer', peer) return false } |