Skip to content

Commit b3400a9

Browse files
authored
Merge pull request #275 from webtorrent/feross-patch-1
server: Improve style of announce/filter logic
2 parents 3968a80 + afcb6df commit b3400a9

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

server.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -657,31 +657,35 @@ Server.prototype._onRequest = function (params, cb) {
657657
Server.prototype._onAnnounce = function (params, cb) {
658658
var self = this
659659

660-
self.getSwarm(params.info_hash, function (err, swarm) {
661-
if (err) return cb(err)
660+
if (self._filter) {
661+
self._filter(params.info_hash, params, function (err) {
662+
// Presence of `err` means that this announce request is disallowed
663+
if (err) return cb(err)
662664

663-
if (self._filter) {
664-
self._filter(params.info_hash, params, function (err) {
665-
// Precense of err means that this torrent or user is disallowd
666-
if (err) cb(err)
667-
else {
668-
if (swarm) announce(swarm)
669-
else createSwarm()
670-
}
665+
getOrCreateSwarm(function (err, swarm) {
666+
if (err) return cb(err)
667+
announce(swarm)
671668
})
672-
} else {
673-
if (swarm) announce(swarm)
674-
else createSwarm()
675-
}
676-
})
677-
678-
function createSwarm () {
679-
self.createSwarm(params.info_hash, function (err, swarm) {
669+
})
670+
} else {
671+
getOrCreateSwarm(function (err, swarm) {
680672
if (err) return cb(err)
681673
announce(swarm)
682674
})
683675
}
684676

677+
// Get existing swarm, or create one if one does not exist
678+
function getOrCreateSwarm (cb) {
679+
self.getSwarm(params.info_hash, function (err, swarm) {
680+
if (err) return cb(err)
681+
if (swarm) return cb(null, swarm)
682+
self.createSwarm(params.info_hash, function (err, swarm) {
683+
if (err) return cb(err)
684+
cb(null, swarm)
685+
})
686+
})
687+
}
688+
685689
function announce (swarm) {
686690
if (!params.event || params.event === 'empty') params.event = 'update'
687691
swarm.announce(params, function (err, response) {

0 commit comments

Comments
 (0)