diff --git a/lib/client/http-tracker.js b/lib/client/http-tracker.js index 629ae16..2cffc2a 100644 --- a/lib/client/http-tracker.js +++ b/lib/client/http-tracker.js @@ -190,11 +190,11 @@ HTTPTracker.prototype._onAnnounceResponse = function (data) { self._trackerId = trackerId } - self.client.emit('update', { + var response = Object.assign({}, data, { announce: self.announceUrl, - complete: data.complete, - incomplete: data.incomplete + infoHash: common.binaryToHex(data.info_hash) }) + self.client.emit('update', response) var addrs if (Buffer.isBuffer(data.peers)) { @@ -248,15 +248,12 @@ HTTPTracker.prototype._onScrapeResponse = function (data) { } keys.forEach(function (infoHash) { - var response = data[infoHash] // TODO: optionally handle data.flags.min_request_interval // (separate from announce interval) - self.client.emit('scrape', { + var response = Object.assign(data[infoHash], { announce: self.announceUrl, - infoHash: common.binaryToHex(infoHash), - complete: response.complete, - incomplete: response.incomplete, - downloaded: response.downloaded + infoHash: common.binaryToHex(infoHash) }) + self.client.emit('scrape', response) }) } diff --git a/lib/client/websocket-tracker.js b/lib/client/websocket-tracker.js index 894408e..058bc28 100644 --- a/lib/client/websocket-tracker.js +++ b/lib/client/websocket-tracker.js @@ -266,11 +266,11 @@ WebSocketTracker.prototype._onAnnounceResponse = function (data) { } if (data.complete != null) { - self.client.emit('update', { + var response = Object.assign({}, data, { announce: self.announceUrl, - complete: data.complete, - incomplete: data.incomplete + infoHash: common.binaryToHex(data.info_hash) }) + self.client.emit('update', response) } var peer @@ -326,16 +326,13 @@ WebSocketTracker.prototype._onScrapeResponse = function (data) { } keys.forEach(function (infoHash) { - var response = data[infoHash] // TODO: optionally handle data.flags.min_request_interval // (separate from announce interval) - self.client.emit('scrape', { + var response = Object.assign(data[infoHash], { announce: self.announceUrl, - infoHash: common.binaryToHex(infoHash), - complete: response.complete, - incomplete: response.incomplete, - downloaded: response.downloaded + infoHash: common.binaryToHex(infoHash) }) + self.client.emit('scrape', response) }) } diff --git a/server.js b/server.js index 3723516..419073b 100644 --- a/server.js +++ b/server.js @@ -280,6 +280,8 @@ function Server (opts) { } } +Server.Swarm = Swarm + Server.prototype._onError = function (err) { var self = this self.emit('error', err) @@ -352,7 +354,7 @@ Server.prototype.createSwarm = function (infoHash, cb) { if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex') process.nextTick(function () { - var swarm = self.torrents[infoHash] = new Swarm(infoHash, self) + var swarm = self.torrents[infoHash] = new Server.Swarm(infoHash, self) cb(null, swarm) }) } diff --git a/test/request-handler.js b/test/request-handler.js new file mode 100644 index 0000000..e04e665 --- /dev/null +++ b/test/request-handler.js @@ -0,0 +1,74 @@ +var Buffer = require('safe-buffer').Buffer +var Client = require('../') +var common = require('./common') +var fixtures = require('webtorrent-fixtures') +var test = require('tape') +var Server = require('../server') + +var peerId = Buffer.from('01234567890123456789') + +function testRequestHandler (t, serverType) { + t.plan(5) + + var opts = { serverType: serverType } // this is test-suite-only option + + class Swarm extends Server.Swarm { + announce (params, cb) { + super.announce(params, function (err, response) { + if (err) return cb(response) + response.complete = 246 + response.extraData = 'hi' + cb(null, response) + }) + } + } + + // Use a custom Swarm implementation for this test only + var OldSwarm = Server.Swarm + Server.Swarm = Swarm + t.on('end', function () { + Server.Swarm = OldSwarm + }) + + common.createServer(t, opts, function (server, announceUrl) { + var client1 = new Client({ + infoHash: fixtures.alice.parsedTorrent.infoHash, + announce: announceUrl, + peerId: peerId, + port: 6881, + wrtc: {} + }) + + client1.on('error', function (err) { t.error(err) }) + if (serverType === 'ws') common.mockWebsocketTracker(client1) + + server.once('start', function () { + t.pass('got start message from client1') + }) + + client1.once('update', function (data) { + t.equal(data.complete, 246) + t.equal(data.extraData.toString(), 'hi') + + client1.destroy(function () { + t.pass('client1 destroyed') + }) + + server.close(function () { + t.pass('server destroyed') + }) + }) + + client1.start() + }) +} + +test('http: request handler option intercepts announce requests and responses', function (t) { + testRequestHandler(t, 'http') +}) + +test('ws: request handler option intercepts announce requests and responses', function (t) { + testRequestHandler(t, 'ws') +}) + +// NOTE: it's not possible to include extra data in a UDP response, because it's compact and accepts only params that are in the spec!