Skip to content

Commit e4eb1a8

Browse files
authored
Merge pull request webtorrent#212 from feross/filter-cb
BREAKING: change how the filter function works
2 parents eef8bdd + 40202a0 commit e4eb1a8

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,14 @@ var server = new Server({
157157
// This example only allows one torrent.
158158

159159
var allowed = (infoHash === 'aaa67059ed6bd08362da625b3ae77f6f4a075aaa')
160-
cb(allowed)
161-
162-
// In addition to returning a boolean (`true` for allowed, `false` for disallowed),
163-
// you can return an `Error` object to disallow and provide a custom reason.
160+
if (allowed) {
161+
// If the callback is passed `null`, the torrent will be allowed.
162+
cb(null)
163+
} else {
164+
// If the callback is passed an `Error` object, the torrent will be disallowed
165+
// and the error's `message` property will be given as the reason.
166+
cb(new Error('disallowed torrent'))
167+
}
164168
}
165169
})
166170

server.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -661,11 +661,10 @@ Server.prototype._onAnnounce = function (params, cb) {
661661

662662
function createSwarm () {
663663
if (self._filter) {
664-
self._filter(params.info_hash, params, function (allowed) {
665-
if (allowed instanceof Error) {
666-
cb(allowed)
667-
} else if (!allowed) {
668-
cb(new Error('disallowed info_hash'))
664+
self._filter(params.info_hash, params, function (err) {
665+
// Precense of err means that this info_hash is disallowed
666+
if (err) {
667+
cb(err)
669668
} else {
670669
self.createSwarm(params.info_hash, function (err, swarm) {
671670
if (err) return cb(err)

test/filter.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ function testFilterOption (t, serverType) {
1212
var opts = { serverType: serverType } // this is test-suite-only option
1313
opts.filter = function (infoHash, params, cb) {
1414
process.nextTick(function () {
15-
cb(infoHash !== fixtures.alice.parsedTorrent.infoHash)
15+
if (infoHash === fixtures.alice.parsedTorrent.infoHash) {
16+
cb(new Error('disallowed info_hash (Alice)'))
17+
} else {
18+
cb(null)
19+
}
1620
})
1721
}
1822

@@ -29,7 +33,7 @@ function testFilterOption (t, serverType) {
2933
if (serverType === 'ws') common.mockWebsocketTracker(client1)
3034

3135
client1.once('warning', function (err) {
32-
t.ok(/disallowed info_hash/.test(err.message), 'got client warning')
36+
t.ok(err.message.includes('disallowed info_hash (Alice)'), 'got client warning')
3337

3438
client1.destroy(function () {
3539
t.pass('client1 destroyed')
@@ -62,7 +66,7 @@ function testFilterOption (t, serverType) {
6266

6367
server.removeAllListeners('warning')
6468
server.once('warning', function (err) {
65-
t.ok(/disallowed info_hash/.test(err.message), 'got server warning')
69+
t.ok(err.message.includes('disallowed info_hash (Alice)'), 'got server warning')
6670
t.equal(Object.keys(server.torrents).length, 0)
6771
})
6872

@@ -88,8 +92,11 @@ function testFilterCustomError (t, serverType) {
8892
var opts = { serverType: serverType } // this is test-suite-only option
8993
opts.filter = function (infoHash, params, cb) {
9094
process.nextTick(function () {
91-
if (infoHash === fixtures.alice.parsedTorrent.infoHash) cb(new Error('alice blocked'))
92-
else cb(true)
95+
if (infoHash === fixtures.alice.parsedTorrent.infoHash) {
96+
cb(new Error('alice blocked'))
97+
} else {
98+
cb(null)
99+
}
93100
})
94101
}
95102

0 commit comments

Comments
 (0)