Skip to content

Commit 1db2cb8

Browse files
committed
Merge pull request #107 from yciabaud/announce-extension
Provide a way to give extra data on announce
2 parents d313e9e + f1669a5 commit 1db2cb8

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

client.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ function Client (peerId, port, torrent, opts) {
5353

5454
self._rtcConfig = opts.rtcConfig
5555
self._wrtc = opts.wrtc
56+
self._getAnnounceOpts = opts.getAnnounceOpts
5657

5758
debug('new client %s', self.infoHash)
5859

@@ -268,5 +269,10 @@ Client.prototype._defaultAnnounceOpts = function (opts) {
268269
if (opts.left == null && self.torrentLength != null) {
269270
opts.left = self.torrentLength - opts.downloaded
270271
}
272+
273+
if (opts.getAnnounceOpts == null) {
274+
opts.getAnnounceOpts = self._getAnnounceOpts
275+
}
276+
271277
return opts
272278
}

lib/client/http-tracker.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,26 @@ HTTPTracker.prototype.announce = function (opts) {
4040
var self = this
4141
if (self.destroyed) return
4242

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+
4352
var params = {
4453
numwant: opts.numwant,
4554
uploaded: opts.uploaded,
4655
downloaded: opts.downloaded,
56+
left: opts.left,
4757
event: opts.event,
4858
compact: (opts.compact == null) ? 1 : opts.compact,
4959
info_hash: self.client._infoHashBinary,
5060
peer_id: self.client._peerIdBinary,
51-
port: self.client._port
61+
port: self.client._port,
62+
extras: cbopts && cbopts.extraAnnounceOpts
5263
}
5364
if (self._trackerId) params.trackerid = self._trackerId
5465

lib/client/udp-tracker.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,15 @@ 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+
192201
send(Buffer.concat([
193202
connectionId,
194203
common.toUInt32(common.ACTIONS.ANNOUNCE),

lib/client/websocket-tracker.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,26 @@ WebSocketTracker.prototype.announce = function (opts) {
4444
// Limit the number of offers that are generated, since it can be slow
4545
var numwant = Math.min(opts.numwant, 10)
4646

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+
4756
self._generateOffers(numwant, function (offers) {
4857
var params = {
4958
numwant: numwant,
5059
uploaded: opts.uploaded || 0,
5160
downloaded: opts.downloaded,
61+
left: opts.left,
5262
event: opts.event,
5363
info_hash: self.client._infoHashBinary,
5464
peer_id: self.client._peerIdBinary,
55-
offers: offers
65+
offers: offers,
66+
extras: cbopts && cbopts.extraAnnounceOpts
5667
}
5768
if (self._trackerId) params.trackerid = self._trackerId
5869

0 commit comments

Comments
 (0)