From 65c02dd153772044fda1e57175c3b3cfbb2e10cb Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sun, 29 Apr 2018 21:17:57 -0700 Subject: [PATCH 1/2] server: Improve style of announce/filter logic - Filter before potentially creating a swarm that is not needed - Unify getSwarm()/createSwarm() into a getOrCreateSwarm() function --- server.js | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/server.js b/server.js index 15ec93d..7fb01ee 100644 --- a/server.js +++ b/server.js @@ -656,31 +656,35 @@ Server.prototype._onRequest = function (params, cb) { Server.prototype._onAnnounce = function (params, cb) { var self = this + + if (self._filter) { + self._filter(params.info_hash, params, function (err) { + // Presence of `err` means that this announce request is disallowed + if (err) return cb(err) - self.getSwarm(params.info_hash, function (err, swarm) { - if (err) return cb(err) - - if (self._filter) { - self._filter(params.info_hash, params, function (err) { - // Precense of err means that this torrent or user is disallowd - if (err) cb(err) - else { - if (swarm) announce(swarm) - else createSwarm() - } + getOrCreateSwarm(function (err, swarm) { + if (err) return cb(err) + announce(swarm) }) - } else { - if (swarm) announce(swarm) - else createSwarm() - } - }) - - function createSwarm () { - self.createSwarm(params.info_hash, function (err, swarm) { + }) + } else { + getOrCreateSwarm(function (err, swarm) { if (err) return cb(err) announce(swarm) }) } + + // Get existing swarm, or create one if one does not exist + function getOrCreateSwarm (cb) { + self.getSwarm(params.info_hash, function (err, swarm) { + if (err) return cb(err) + if (swarm) return cb(null, swarm) + self.createSwarm(params.info_hash, function (err, swarm) { + if (err) return cb(err) + cb(null, swarm) + }) + }) + } function announce (swarm) { if (!params.event || params.event === 'empty') params.event = 'update' From afcb6df085db62489678765cb347a9e867fc2405 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sun, 29 Apr 2018 21:20:51 -0700 Subject: [PATCH 2/2] standard --- server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index 7fb01ee..4f90569 100644 --- a/server.js +++ b/server.js @@ -656,7 +656,7 @@ Server.prototype._onRequest = function (params, cb) { Server.prototype._onAnnounce = function (params, cb) { var self = this - + if (self._filter) { self._filter(params.info_hash, params, function (err) { // Presence of `err` means that this announce request is disallowed @@ -673,7 +673,7 @@ Server.prototype._onAnnounce = function (params, cb) { announce(swarm) }) } - + // Get existing swarm, or create one if one does not exist function getOrCreateSwarm (cb) { self.getSwarm(params.info_hash, function (err, swarm) {