Skip to content

Commit 65c02dd

Browse files
authored
server: Improve style of announce/filter logic
- Filter before potentially creating a swarm that is not needed - Unify getSwarm()/createSwarm() into a getOrCreateSwarm() function
1 parent c98c40a commit 65c02dd

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

server.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -656,31 +656,35 @@ Server.prototype._onRequest = function (params, cb) {
656656

657657
Server.prototype._onAnnounce = function (params, cb) {
658658
var self = this
659+
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)
659664

660-
self.getSwarm(params.info_hash, function (err, swarm) {
661-
if (err) return cb(err)
662-
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
}
676+
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+
}
684688

685689
function announce (swarm) {
686690
if (!params.event || params.event === 'empty') params.event = 'update'

0 commit comments

Comments
 (0)