Skip to content

Commit b34fc10

Browse files
committed
fixes for PR webtorrent#107
1 parent 1db2cb8 commit b34fc10

File tree

4 files changed

+18
-54
lines changed

4 files changed

+18
-54
lines changed

client.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
module.exports = Client
22

3-
var EventEmitter = require('events').EventEmitter
43
var debug = require('debug')('bittorrent-tracker')
4+
var EventEmitter = require('events').EventEmitter
5+
var extend = require('xtend')
56
var inherits = require('inherits')
67
var once = require('once')
78
var parallel = require('run-parallel')
@@ -20,12 +21,13 @@ inherits(Client, EventEmitter)
2021
*
2122
* Find torrent peers, to help a torrent client participate in a torrent swarm.
2223
*
23-
* @param {string|Buffer} peerId peer id
24-
* @param {Number} port torrent client listening port
25-
* @param {Object} torrent parsed torrent
26-
* @param {Object} opts options object
27-
* @param {Number} opts.rtcConfig RTCPeerConnection configuration object
28-
* @param {Number} opts.wrtc custom webrtc implementation
24+
* @param {string|Buffer} peerId peer id
25+
* @param {Number} port torrent client listening port
26+
* @param {Object} torrent parsed torrent
27+
* @param {Object} opts options object
28+
* @param {Number} opts.rtcConfig RTCPeerConnection configuration object
29+
* @param {Number} opts.wrtc custom webrtc implementation
30+
* @param {Object} opts.getAnnounceOpts callback to provide data to tracker
2931
*/
3032
function Client (peerId, port, torrent, opts) {
3133
var self = this
@@ -270,9 +272,7 @@ Client.prototype._defaultAnnounceOpts = function (opts) {
270272
opts.left = self.torrentLength - opts.downloaded
271273
}
272274

273-
if (opts.getAnnounceOpts == null) {
274-
opts.getAnnounceOpts = self._getAnnounceOpts
275-
}
275+
if (self._getAnnounceOpts) opts = extend(opts, self._getAnnounceOpts())
276276

277277
return opts
278278
}

lib/client/http-tracker.js

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module.exports = HTTPTracker
33
var bencode = require('bencode')
44
var compact2string = require('compact2string')
55
var debug = require('debug')('bittorrent-tracker:http-tracker')
6+
var extend = require('xtend')
67
var get = require('simple-get')
78
var inherits = require('inherits')
89

@@ -40,27 +41,12 @@ HTTPTracker.prototype.announce = function (opts) {
4041
var self = this
4142
if (self.destroyed) return
4243

43-
// Refresh opts if the callback is provided
44-
var cbopts
45-
if (opts.getAnnounceOpts) {
46-
cbopts = opts.getAnnounceOpts()
47-
if (cbopts.uploaded) opts.uploaded = cbopts.uploaded
48-
if (cbopts.downloaded) opts.downloaded = cbopts.downloaded
49-
if (cbopts.left) opts.left = cbopts.left
50-
}
51-
52-
var params = {
53-
numwant: opts.numwant,
54-
uploaded: opts.uploaded,
55-
downloaded: opts.downloaded,
56-
left: opts.left,
57-
event: opts.event,
44+
var params = extend(opts, {
5845
compact: (opts.compact == null) ? 1 : opts.compact,
5946
info_hash: self.client._infoHashBinary,
6047
peer_id: self.client._peerIdBinary,
61-
port: self.client._port,
62-
extras: cbopts && cbopts.extraAnnounceOpts
63-
}
48+
port: self.client._port
49+
})
6450
if (self._trackerId) params.trackerid = self._trackerId
6551

6652
self._request(self.announceUrl, params, self._onAnnounceResponse.bind(self))

lib/client/udp-tracker.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,6 @@ UDPTracker.prototype._request = function (opts) {
189189
function announce (connectionId, opts) {
190190
transactionId = genTransactionId()
191191

192-
// Refresh opts if the callback is provided
193-
var cbopts
194-
if (opts.getAnnounceOpts) {
195-
cbopts = opts.getAnnounceOpts()
196-
if (cbopts.uploaded) opts.uploaded = cbopts.uploaded
197-
if (cbopts.downloaded) opts.downloaded = cbopts.downloaded
198-
if (cbopts.left) opts.left = cbopts.left
199-
}
200-
201192
send(Buffer.concat([
202193
connectionId,
203194
common.toUInt32(common.ACTIONS.ANNOUNCE),

lib/client/websocket-tracker.js

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module.exports = WebSocketTracker
22

33
var debug = require('debug')('bittorrent-tracker:websocket-tracker')
4+
var extend = require('xtend')
45
var hat = require('hat')
56
var inherits = require('inherits')
67
var Peer = require('simple-peer')
@@ -44,27 +45,13 @@ WebSocketTracker.prototype.announce = function (opts) {
4445
// Limit the number of offers that are generated, since it can be slow
4546
var numwant = Math.min(opts.numwant, 10)
4647

47-
// Refresh opts if the callback is provided
48-
var cbopts
49-
if (opts.getAnnounceOpts) {
50-
cbopts = opts.getAnnounceOpts()
51-
if (cbopts.uploaded) opts.uploaded = cbopts.uploaded
52-
if (cbopts.downloaded) opts.downloaded = cbopts.downloaded
53-
if (cbopts.left) opts.left = cbopts.left
54-
}
55-
5648
self._generateOffers(numwant, function (offers) {
57-
var params = {
49+
var params = extend(opts, {
5850
numwant: numwant,
59-
uploaded: opts.uploaded || 0,
60-
downloaded: opts.downloaded,
61-
left: opts.left,
62-
event: opts.event,
6351
info_hash: self.client._infoHashBinary,
6452
peer_id: self.client._peerIdBinary,
65-
offers: offers,
66-
extras: cbopts && cbopts.extraAnnounceOpts
67-
}
53+
offers: offers
54+
})
6855
if (self._trackerId) params.trackerid = self._trackerId
6956

7057
self._send(params)

0 commit comments

Comments
 (0)