From e3c65bc2a32e72e0dc2106eab56aec527e098b77 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 11 Mar 2016 17:21:19 -0800 Subject: [PATCH] PR Feedback for #120 --- lib/common-node.js | 5 ----- lib/server/parse-http.js | 2 +- lib/server/parse-udp.js | 2 +- lib/server/parse-websocket.js | 2 +- lib/server/swarm.js | 7 +++---- 5 files changed, 6 insertions(+), 12 deletions(-) diff --git a/lib/common-node.js b/lib/common-node.js index b017bc0..7573367 100644 --- a/lib/common-node.js +++ b/lib/common-node.js @@ -24,11 +24,6 @@ exports.EVENT_NAMES = { started: 'start', stopped: 'stop' } -exports.PEER_TYPES = { - http: 'http', - udp: 'udp', - websocket: 'ws' -} function toUInt32 (n) { var buf = new Buffer(4) diff --git a/lib/server/parse-http.js b/lib/server/parse-http.js index 4681e43..9e83a59 100644 --- a/lib/server/parse-http.js +++ b/lib/server/parse-http.js @@ -6,10 +6,10 @@ function parseHttpRequest (req, opts) { if (!opts) opts = {} var s = req.url.split('?') var params = common.querystringParse(s[1]) + params.type = 'http' if (opts.action === 'announce' || s[0] === '/announce') { params.action = common.ACTIONS.ANNOUNCE - params.type = common.PEER_TYPES.http if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) { throw new Error('invalid info_hash') diff --git a/lib/server/parse-udp.js b/lib/server/parse-udp.js index 6524ce2..78694d1 100644 --- a/lib/server/parse-udp.js +++ b/lib/server/parse-udp.js @@ -11,7 +11,7 @@ function parseUdpRequest (msg, rinfo) { connectionId: msg.slice(0, 8), // 64-bit action: msg.readUInt32BE(8), transactionId: msg.readUInt32BE(12), - type: common.PEER_TYPES.udp + type: 'udp' } if (!bufferEqual(params.connectionId, common.CONNECTION_ID)) { diff --git a/lib/server/parse-websocket.js b/lib/server/parse-websocket.js index a28f5aa..9847efb 100644 --- a/lib/server/parse-websocket.js +++ b/lib/server/parse-websocket.js @@ -7,7 +7,7 @@ function parseWebSocketRequest (socket, opts, params) { params = JSON.parse(params) // may throw params.action = common.ACTIONS.ANNOUNCE - params.type = common.PEER_TYPES.websocket + params.type = 'ws' params.socket = socket if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) { diff --git a/lib/server/swarm.js b/lib/server/swarm.js index e6d51db..4f2a927 100644 --- a/lib/server/swarm.js +++ b/lib/server/swarm.js @@ -2,7 +2,6 @@ module.exports = Swarm var debug = require('debug')('bittorrent-tracker') var randomIterate = require('random-iterate') -var common = require('../common') // Regard this as the default implementation of an interface that you // need to support when overriding Server.createSwarm() and Server.getSwarm() @@ -14,7 +13,7 @@ function Swarm (infoHash, server) { Swarm.prototype.announce = function (params, cb) { var self = this - var id = params.type === common.PEER_TYPES.websocket ? params.peer_id : params.addr + var id = params.type === 'ws' ? params.peer_id : params.addr var peer = self.peers[id] if (params.event === 'started') { @@ -51,7 +50,7 @@ Swarm.prototype._onAnnounceStarted = function (params, peer) { if (params.left === 0) this.complete += 1 else this.incomplete += 1 - var id = params.type === common.PEER_TYPES.websocket ? params.peer_id : params.addr + var id = params.type === 'ws' ? params.peer_id : params.addr peer = this.peers[id] = { complete: params.left === 0, peerId: params.peer_id, // as hex @@ -69,7 +68,7 @@ Swarm.prototype._onAnnounceStopped = function (params, peer) { if (peer.complete) this.complete -= 1 else this.incomplete -= 1 - var id = params.type === common.PEER_TYPES.websocket ? params.peer_id : params.addr + var id = params.type === 'ws' ? params.peer_id : params.addr delete this.peers[id] }