Merge pull request #212 from feross/filter-cb

BREAKING: change how the filter function works
This commit is contained in:
Feross Aboukhadijeh 2017-03-01 22:57:09 -08:00 committed by GitHub
commit e4eb1a8069
3 changed files with 24 additions and 14 deletions

View File

@ -157,10 +157,14 @@ var server = new Server({
// This example only allows one torrent. // This example only allows one torrent.
var allowed = (infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa') var allowed = (infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa')
cb(allowed) if (allowed) {
// If the callback is passed `null`, the torrent will be allowed.
// In addition to returning a boolean (`true` for allowed, `false` for disallowed), cb(null)
// you can return an `Error` object to disallow and provide a custom reason. } else {
// If the callback is passed an `Error` object, the torrent will be disallowed
// and the error's `message` property will be given as the reason.
cb(new Error('disallowed torrent'))
}
} }
}) })

View File

@ -661,11 +661,10 @@ Server.prototype._onAnnounce = function (params, cb) {
function createSwarm () { function createSwarm () {
if (self._filter) { if (self._filter) {
self._filter(params.info_hash, params, function (allowed) { self._filter(params.info_hash, params, function (err) {
if (allowed instanceof Error) { // Precense of err means that this info_hash is disallowed
cb(allowed) if (err) {
} else if (!allowed) { cb(err)
cb(new Error('disallowed info_hash'))
} else { } else {
self.createSwarm(params.info_hash, function (err, swarm) { self.createSwarm(params.info_hash, function (err, swarm) {
if (err) return cb(err) if (err) return cb(err)

View File

@ -12,7 +12,11 @@ function testFilterOption (t, serverType) {
var opts = { serverType: serverType } // this is test-suite-only option var opts = { serverType: serverType } // this is test-suite-only option
opts.filter = function (infoHash, params, cb) { opts.filter = function (infoHash, params, cb) {
process.nextTick(function () { process.nextTick(function () {
cb(infoHash !== fixtures.alice.parsedTorrent.infoHash) if (infoHash === fixtures.alice.parsedTorrent.infoHash) {
cb(new Error('disallowed info_hash (Alice)'))
} else {
cb(null)
}
}) })
} }
@ -29,7 +33,7 @@ function testFilterOption (t, serverType) {
if (serverType === 'ws') common.mockWebsocketTracker(client1) if (serverType === 'ws') common.mockWebsocketTracker(client1)
client1.once('warning', function (err) { client1.once('warning', function (err) {
t.ok(/disallowed info_hash/.test(err.message), 'got client warning') t.ok(err.message.includes('disallowed info_hash (Alice)'), 'got client warning')
client1.destroy(function () { client1.destroy(function () {
t.pass('client1 destroyed') t.pass('client1 destroyed')
@ -62,7 +66,7 @@ function testFilterOption (t, serverType) {
server.removeAllListeners('warning') server.removeAllListeners('warning')
server.once('warning', function (err) { server.once('warning', function (err) {
t.ok(/disallowed info_hash/.test(err.message), 'got server warning') t.ok(err.message.includes('disallowed info_hash (Alice)'), 'got server warning')
t.equal(Object.keys(server.torrents).length, 0) t.equal(Object.keys(server.torrents).length, 0)
}) })
@ -88,8 +92,11 @@ function testFilterCustomError (t, serverType) {
var opts = { serverType: serverType } // this is test-suite-only option var opts = { serverType: serverType } // this is test-suite-only option
opts.filter = function (infoHash, params, cb) { opts.filter = function (infoHash, params, cb) {
process.nextTick(function () { process.nextTick(function () {
if (infoHash === fixtures.alice.parsedTorrent.infoHash) cb(new Error('alice blocked')) if (infoHash === fixtures.alice.parsedTorrent.infoHash) {
else cb(true) cb(new Error('alice blocked'))
} else {
cb(null)
}
}) })
} }