From d1b9047c03203e02dbea816e4ed7b02388217430 Mon Sep 17 00:00:00 2001 From: Justin Kalland Date: Thu, 20 Dec 2018 12:36:54 -0700 Subject: [PATCH 1/4] Remove declarations from inside switch statements --- lib/client/udp-tracker.js | 8 +++++--- server.js | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/client/udp-tracker.js b/lib/client/udp-tracker.js index 239ae12..19bb25d 100644 --- a/lib/client/udp-tracker.js +++ b/lib/client/udp-tracker.js @@ -127,6 +127,9 @@ class UDPTracker extends Tracker { } const action = msg.readUInt32BE(0) + let interval + let addrs + let infoHashes debug('UDP response %s, action %s', self.announceUrl, action) switch (action) { case 0: // handshake @@ -146,7 +149,7 @@ class UDPTracker extends Tracker { if (msg.length < 20) return onError(new Error('invalid announce message')) - const interval = msg.readUInt32BE(8) + interval = msg.readUInt32BE(8) if (interval) self.setInterval(interval * 1000) self.client.emit('update', { @@ -155,7 +158,6 @@ class UDPTracker extends Tracker { incomplete: msg.readUInt32BE(12) }) - let addrs try { addrs = compact2string.multi(msg.slice(20)) } catch (err) { @@ -174,7 +176,7 @@ class UDPTracker extends Tracker { if (msg.length < 20 || (msg.length - 8) % 12 !== 0) { return onError(new Error('invalid scrape message')) } - const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0) + infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0) ? opts.infoHash.map(infoHash => { return infoHash.toString('hex') }) : [ (opts.infoHash && opts.infoHash.toString('hex')) || self.client.infoHash ] diff --git a/server.js b/server.js index 9d29bef..9a68544 100644 --- a/server.js +++ b/server.js @@ -749,6 +749,7 @@ Server.Swarm = Swarm function makeUdpPacket (params) { let packet + let scrapeResponse switch (params.action) { case common.ACTIONS.CONNECT: packet = Buffer.concat([ @@ -768,7 +769,7 @@ function makeUdpPacket (params) { ]) break case common.ACTIONS.SCRAPE: - const scrapeResponse = [ + scrapeResponse = [ common.toUInt32(common.ACTIONS.SCRAPE), common.toUInt32(params.transactionId) ] From bc861ef10a055a139bad35c21831d3dd4487f6b8 Mon Sep 17 00:00:00 2001 From: Justin Kalland Date: Thu, 20 Dec 2018 12:52:05 -0700 Subject: [PATCH 2/4] Revert "Remove declarations from inside switch statements" This reverts commit d1b9047c03203e02dbea816e4ed7b02388217430. --- lib/client/udp-tracker.js | 8 +++----- server.js | 3 +-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/client/udp-tracker.js b/lib/client/udp-tracker.js index 19bb25d..239ae12 100644 --- a/lib/client/udp-tracker.js +++ b/lib/client/udp-tracker.js @@ -127,9 +127,6 @@ class UDPTracker extends Tracker { } const action = msg.readUInt32BE(0) - let interval - let addrs - let infoHashes debug('UDP response %s, action %s', self.announceUrl, action) switch (action) { case 0: // handshake @@ -149,7 +146,7 @@ class UDPTracker extends Tracker { if (msg.length < 20) return onError(new Error('invalid announce message')) - interval = msg.readUInt32BE(8) + const interval = msg.readUInt32BE(8) if (interval) self.setInterval(interval * 1000) self.client.emit('update', { @@ -158,6 +155,7 @@ class UDPTracker extends Tracker { incomplete: msg.readUInt32BE(12) }) + let addrs try { addrs = compact2string.multi(msg.slice(20)) } catch (err) { @@ -176,7 +174,7 @@ class UDPTracker extends Tracker { if (msg.length < 20 || (msg.length - 8) % 12 !== 0) { return onError(new Error('invalid scrape message')) } - infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0) + const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0) ? opts.infoHash.map(infoHash => { return infoHash.toString('hex') }) : [ (opts.infoHash && opts.infoHash.toString('hex')) || self.client.infoHash ] diff --git a/server.js b/server.js index 9a68544..9d29bef 100644 --- a/server.js +++ b/server.js @@ -749,7 +749,6 @@ Server.Swarm = Swarm function makeUdpPacket (params) { let packet - let scrapeResponse switch (params.action) { case common.ACTIONS.CONNECT: packet = Buffer.concat([ @@ -769,7 +768,7 @@ function makeUdpPacket (params) { ]) break case common.ACTIONS.SCRAPE: - scrapeResponse = [ + const scrapeResponse = [ common.toUInt32(common.ACTIONS.SCRAPE), common.toUInt32(params.transactionId) ] From dc79834f771d415e3e88a2c613198df400932838 Mon Sep 17 00:00:00 2001 From: Justin Kalland Date: Thu, 20 Dec 2018 12:59:53 -0700 Subject: [PATCH 3/4] Use blocks with brackets --- lib/client/udp-tracker.js | 19 ++++++++++--------- server.js | 12 ++++++++---- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/client/udp-tracker.js b/lib/client/udp-tracker.js index 239ae12..ffbbcc7 100644 --- a/lib/client/udp-tracker.js +++ b/lib/client/udp-tracker.js @@ -129,7 +129,7 @@ class UDPTracker extends Tracker { const action = msg.readUInt32BE(0) debug('UDP response %s, action %s', self.announceUrl, action) switch (action) { - case 0: // handshake + case 0: { // handshake // Note: no check for `self.destroyed` so that pending messages to the // tracker can still be sent/received even after destroy() is called @@ -139,8 +139,8 @@ class UDPTracker extends Tracker { else announce(msg.slice(8, 16), opts) break - - case 1: // announce + } + case 1: { // announce cleanup() if (self.destroyed) return @@ -166,8 +166,8 @@ class UDPTracker extends Tracker { }) break - - case 2: // scrape + } + case 2: { // scrape cleanup() if (self.destroyed) return @@ -189,8 +189,8 @@ class UDPTracker extends Tracker { } break - - case 3: // error + } + case 3: { // error cleanup() if (self.destroyed) return @@ -198,10 +198,11 @@ class UDPTracker extends Tracker { self.client.emit('warning', new Error(msg.slice(8).toString())) break - - default: + } + default: { onError(new Error('tracker sent invalid action')) break + } } } diff --git a/server.js b/server.js index 9d29bef..10eb839 100644 --- a/server.js +++ b/server.js @@ -750,14 +750,15 @@ Server.Swarm = Swarm function makeUdpPacket (params) { let packet switch (params.action) { - case common.ACTIONS.CONNECT: + case common.ACTIONS.CONNECT: { packet = Buffer.concat([ common.toUInt32(common.ACTIONS.CONNECT), common.toUInt32(params.transactionId), params.connectionId ]) break - case common.ACTIONS.ANNOUNCE: + } + case common.ACTIONS.ANNOUNCE: { packet = Buffer.concat([ common.toUInt32(common.ACTIONS.ANNOUNCE), common.toUInt32(params.transactionId), @@ -767,7 +768,8 @@ function makeUdpPacket (params) { params.peers ]) break - case common.ACTIONS.SCRAPE: + } + case common.ACTIONS.SCRAPE: { const scrapeResponse = [ common.toUInt32(common.ACTIONS.SCRAPE), common.toUInt32(params.transactionId) @@ -782,13 +784,15 @@ function makeUdpPacket (params) { } packet = Buffer.concat(scrapeResponse) break - case common.ACTIONS.ERROR: + } + case common.ACTIONS.ERROR: { packet = Buffer.concat([ common.toUInt32(common.ACTIONS.ERROR), common.toUInt32(params.transactionId || 0), Buffer.from(String(params['failure reason'])) ]) break + } default: throw new Error(`Action not implemented: ${params.action}`) } From dd861404e2cb193da0729077395bad6a0f8285e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Baquero?= Date: Thu, 20 Dec 2018 15:03:15 -0500 Subject: [PATCH 4/4] Leave default case without brackets --- lib/client/udp-tracker.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/client/udp-tracker.js b/lib/client/udp-tracker.js index ffbbcc7..56e1b8a 100644 --- a/lib/client/udp-tracker.js +++ b/lib/client/udp-tracker.js @@ -199,10 +199,9 @@ class UDPTracker extends Tracker { break } - default: { + default: onError(new Error('tracker sent invalid action')) break - } } }