Merge pull request #1 from feross/feross/request-handler-changes

Replace the request handler option by exposing the Swarm
This commit is contained in:
Yoann Ciabaud 2017-03-12 09:10:09 +01:00 committed by GitHub
commit a77873a765
4 changed files with 46 additions and 56 deletions

View File

@ -190,11 +190,11 @@ HTTPTracker.prototype._onAnnounceResponse = function (data) {
self._trackerId = trackerId self._trackerId = trackerId
} }
self.client.emit('update', { var response = Object.assign({}, data, {
announce: self.announceUrl, announce: self.announceUrl,
complete: data.complete, infoHash: common.binaryToHex(data.info_hash)
incomplete: data.incomplete
}) })
self.client.emit('update', response)
var addrs var addrs
if (Buffer.isBuffer(data.peers)) { if (Buffer.isBuffer(data.peers)) {
@ -248,15 +248,12 @@ HTTPTracker.prototype._onScrapeResponse = function (data) {
} }
keys.forEach(function (infoHash) { keys.forEach(function (infoHash) {
var response = data[infoHash]
// TODO: optionally handle data.flags.min_request_interval // TODO: optionally handle data.flags.min_request_interval
// (separate from announce interval) // (separate from announce interval)
self.client.emit('scrape', { var response = Object.assign(data[infoHash], {
announce: self.announceUrl, announce: self.announceUrl,
infoHash: common.binaryToHex(infoHash), infoHash: common.binaryToHex(infoHash)
complete: response.complete,
incomplete: response.incomplete,
downloaded: response.downloaded
}) })
self.client.emit('scrape', response)
}) })
} }

View File

@ -266,8 +266,11 @@ WebSocketTracker.prototype._onAnnounceResponse = function (data) {
} }
if (data.complete != null) { if (data.complete != null) {
data.announce = self.announceUrl var response = Object.assign({}, data, {
self.client.emit('update', data) announce: self.announceUrl,
infoHash: common.binaryToHex(data.info_hash)
})
self.client.emit('update', response)
} }
var peer var peer
@ -323,16 +326,13 @@ WebSocketTracker.prototype._onScrapeResponse = function (data) {
} }
keys.forEach(function (infoHash) { keys.forEach(function (infoHash) {
var response = data[infoHash]
// TODO: optionally handle data.flags.min_request_interval // TODO: optionally handle data.flags.min_request_interval
// (separate from announce interval) // (separate from announce interval)
self.client.emit('scrape', { var response = Object.assign(data[infoHash], {
announce: self.announceUrl, announce: self.announceUrl,
infoHash: common.binaryToHex(infoHash), infoHash: common.binaryToHex(infoHash)
complete: response.complete,
incomplete: response.incomplete,
downloaded: response.downloaded
}) })
self.client.emit('scrape', response)
}) })
} }

View File

@ -27,15 +27,14 @@ inherits(Server, EventEmitter)
* metrics from clients that help the tracker keep overall statistics about the torrent. * metrics from clients that help the tracker keep overall statistics about the torrent.
* Responses include a peer list that helps the client participate in the torrent. * Responses include a peer list that helps the client participate in the torrent.
* *
* @param {Object} opts options object * @param {Object} opts options object
* @param {Number} opts.interval tell clients to announce on this interval (ms) * @param {Number} opts.interval tell clients to announce on this interval (ms)
* @param {Number} opts.trustProxy trust 'x-forwarded-for' header from reverse proxy * @param {Number} opts.trustProxy trust 'x-forwarded-for' header from reverse proxy
* @param {boolean} opts.http start an http server? (default: true) * @param {boolean} opts.http start an http server? (default: true)
* @param {boolean} opts.udp start a udp server? (default: true) * @param {boolean} opts.udp start a udp server? (default: true)
* @param {boolean} opts.ws start a websocket server? (default: true) * @param {boolean} opts.ws start a websocket server? (default: true)
* @param {boolean} opts.stats enable web-based statistics? (default: true) * @param {boolean} opts.stats enable web-based statistics? (default: true)
* @param {function} opts.filter black/whitelist fn for disallowing/allowing torrents * @param {function} opts.filter black/whitelist fn for disallowing/allowing torrents
* @param {function} opts.requestHandler functions to handle params / response
*/ */
function Server (opts) { function Server (opts) {
var self = this var self = this
@ -65,18 +64,6 @@ function Server (opts) {
self.udp6 = null self.udp6 = null
self.ws = null self.ws = null
self._reqHandler = opts.requestHandler || {}
if (!self._reqHandler.getParams) {
self._reqHandler.getParams = function (params) {
return params
}
}
if (!self._reqHandler.getResponse) {
self._reqHandler.getResponse = function (params, cb) {
return cb
}
}
// start an http tracker unless the user explictly says no // start an http tracker unless the user explictly says no
if (opts.http !== false) { if (opts.http !== false) {
self.http = http.createServer() self.http = http.createServer()
@ -293,6 +280,8 @@ function Server (opts) {
} }
} }
Server.Swarm = Swarm
Server.prototype._onError = function (err) { Server.prototype._onError = function (err) {
var self = this var self = this
self.emit('error', err) self.emit('error', err)
@ -365,7 +354,7 @@ Server.prototype.createSwarm = function (infoHash, cb) {
if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex') if (Buffer.isBuffer(infoHash)) infoHash = infoHash.toString('hex')
process.nextTick(function () { 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) cb(null, swarm)
}) })
} }
@ -649,8 +638,6 @@ Server.prototype._onWebSocketError = function (socket, err) {
Server.prototype._onRequest = function (params, cb) { Server.prototype._onRequest = function (params, cb) {
var self = this var self = this
params = self._reqHandler.getParams(params)
cb = self._reqHandler.getResponse(params, cb)
if (params && params.action === common.ACTIONS.CONNECT) { if (params && params.action === common.ACTIONS.CONNECT) {
cb(null, { action: common.ACTIONS.CONNECT }) cb(null, { action: common.ACTIONS.CONNECT })
} else if (params && params.action === common.ACTIONS.ANNOUNCE) { } else if (params && params.action === common.ACTIONS.ANNOUNCE) {

View File

@ -3,26 +3,33 @@ var Client = require('../')
var common = require('./common') var common = require('./common')
var fixtures = require('webtorrent-fixtures') var fixtures = require('webtorrent-fixtures')
var test = require('tape') var test = require('tape')
var Server = require('../server')
var peerId = Buffer.from('01234567890123456789') var peerId = Buffer.from('01234567890123456789')
function testRequestHandler (t, serverType) { function testRequestHandler (t, serverType) {
t.plan(4) t.plan(5)
var opts = { serverType: serverType } // this is test-suite-only option var opts = { serverType: serverType } // this is test-suite-only option
opts.requestHandler = {
getParams: function (params) { class Swarm extends Server.Swarm {
params.extra = 123 announce (params, cb) {
return params super.announce(params, function (err, response) {
}, if (err) return cb(response)
getResponse: function (params, cb) { response.complete = 246
return function (err, response) { response.extraData = 'hi'
response.complete = params.extra * 2 cb(null, response)
cb(err, 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) { common.createServer(t, opts, function (server, announceUrl) {
var client1 = new Client({ var client1 = new Client({
infoHash: fixtures.alice.parsedTorrent.infoHash, infoHash: fixtures.alice.parsedTorrent.infoHash,
@ -41,6 +48,7 @@ function testRequestHandler (t, serverType) {
client1.once('update', function (data) { client1.once('update', function (data) {
t.equal(data.complete, 246) t.equal(data.complete, 246)
t.equal(data.extraData.toString(), 'hi')
client1.destroy(function () { client1.destroy(function () {
t.pass('client1 destroyed') t.pass('client1 destroyed')
@ -59,10 +67,8 @@ test('http: request handler option intercepts announce requests and responses',
testRequestHandler(t, 'http') testRequestHandler(t, 'http')
}) })
test('udp: request handler option intercepts announce requests and responses', function (t) {
testRequestHandler(t, 'udp')
})
test('ws: request handler option intercepts announce requests and responses', function (t) { test('ws: request handler option intercepts announce requests and responses', function (t) {
testRequestHandler(t, 'ws') 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!