From 80f89866050263a9c0e6477aa573cd0c4fe2c5ef Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Fri, 17 Feb 2017 09:57:14 +0100 Subject: [PATCH 1/2] Provide context data to getAnnounceOpts --- README.md | 5 +++-- client.js | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d8348898..9abcecf5 100644 --- a/README.md +++ b/README.md @@ -59,9 +59,10 @@ var requiredOpts = { } var optionalOpts = { - getAnnounceOpts: function () { + getAnnounceOpts: function (opts) { // Provide a callback that will be called whenever announce() is called - // internally (on timer), or by the user + // internally (on timer), or by the user. + // this refers to the tracker and opts to the current announce options. return { uploaded: 0, downloaded: 0, diff --git a/client.js b/client.js index f29e962d..0d6bda08 100644 --- a/client.js +++ b/client.js @@ -61,7 +61,7 @@ function Client (opts) { self.destroyed = false self._port = opts.port - self._getAnnounceOpts = opts.getAnnounceOpts + self._getAnnounceOpts = opts.getAnnounceOpts && opts.getAnnounceOpts.bind(self) self._rtcConfig = opts.rtcConfig self._userAgent = opts.userAgent @@ -296,6 +296,6 @@ Client.prototype._defaultAnnounceOpts = function (opts) { if (opts.uploaded == null) opts.uploaded = 0 if (opts.downloaded == null) opts.downloaded = 0 - if (self._getAnnounceOpts) opts = extend(opts, self._getAnnounceOpts()) + if (self._getAnnounceOpts) opts = extend(opts, self._getAnnounceOpts(opts)) return opts } From accce996edad4a6dcb74d000ead25fb379bf899c Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Fri, 17 Feb 2017 10:06:30 +0100 Subject: [PATCH 2/2] Update test --- test/client.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/client.js b/test/client.js index 9858bf55..d894ea6e 100644 --- a/test/client.js +++ b/test/client.js @@ -301,7 +301,7 @@ test('ws: client.announce() with params', function (t) { }) function testClientGetAnnounceOpts (t, serverType) { - t.plan(5) + t.plan(7) common.createServer(t, serverType, function (server, announceUrl) { var client = new Client({ @@ -309,8 +309,10 @@ function testClientGetAnnounceOpts (t, serverType) { announce: announceUrl, peerId: peerId1, port: port, - getAnnounceOpts: function () { + getAnnounceOpts: function (opts) { return { + infoHash: this.infoHash, + optsDL: opts.downloaded, testParam: 'this is a test' } }, @@ -319,6 +321,8 @@ function testClientGetAnnounceOpts (t, serverType) { server.on('start', function (peer, params) { t.equal(params.testParam, 'this is a test') + t.equal(params.infoHash, fixtures.leaves.parsedTorrent.infoHash) + t.equal(params.optsDL, params.downloaded) }) if (serverType === 'ws') common.mockWebsocketTracker(client)