From ebb86f728fe986140ef050a4a7faa42aaa41e25a Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Mon, 26 Jan 2015 18:16:01 -0800 Subject: [PATCH] JavaScript Standard Style https://github.com/feross/standard --- client.js | 2 -- lib/parse_http.js | 4 +--- lib/parse_udp.js | 3 --- lib/swarm.js | 1 - package.json | 3 ++- server.js | 26 ++++++++++++++------------ test/client-large-torrent.js | 1 - test/client.js | 2 -- test/scrape.js | 2 -- 9 files changed, 17 insertions(+), 27 deletions(-) diff --git a/client.js b/client.js index e7161ed..3513448 100644 --- a/client.js +++ b/client.js @@ -10,8 +10,6 @@ var EventEmitter = require('events').EventEmitter var extend = require('extend.js') var get = require('simple-get') var hat = require('hat') -var http = require('http') -var https = require('https') var inherits = require('inherits') var once = require('once') var url = require('url') diff --git a/lib/parse_http.js b/lib/parse_http.js index fe1c17b..eaacbaf 100644 --- a/lib/parse_http.js +++ b/lib/parse_http.js @@ -32,8 +32,7 @@ function parseHttpRequest (req, options) { ? req.headers['x-forwarded-for'] || req.connection.remoteAddress : req.connection.remoteAddress.replace(REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4 params.addr = (common.IPV6_RE.test(params.ip) ? '[' + params.ip + ']' : params.ip) + ':' + params.port - - } else if (s[0] === '/scrape') { // unofficial scrape message + } else if (s[0] === '/scrape') { params.action = common.ACTIONS.SCRAPE if (typeof params.info_hash === 'string') params.info_hash = [ params.info_hash ] @@ -45,7 +44,6 @@ function parseHttpRequest (req, options) { return common.binaryToHex(binaryInfoHash) }) } - } else { throw new Error('Invalid action in HTTP request: ' + params.action) } diff --git a/lib/parse_udp.js b/lib/parse_udp.js index 0e7a008..3fac4ec 100644 --- a/lib/parse_udp.js +++ b/lib/parse_udp.js @@ -23,7 +23,6 @@ function parseUdpRequest (msg, rinfo) { if (params.action === common.ACTIONS.CONNECT) { // No further params - } else if (params.action === common.ACTIONS.ANNOUNCE) { params.info_hash = msg.slice(16, 36).toString('hex') // 20 bytes params.peer_id = msg.slice(36, 56).toString('hex') // 20 bytes @@ -51,13 +50,11 @@ function parseUdpRequest (msg, rinfo) { params.port = msg.readUInt16BE(96) || rinfo.port // optional params.addr = params.ip + ':' + params.port // TODO: ipv6 brackets params.compact = 1 // udp is always compact - } else if (params.action === common.ACTIONS.SCRAPE) { // scrape message // TODO: support multiple info_hash scrape if (msg.length > 36) throw new Error('multiple info_hash scrape not supported') params.info_hash = [ msg.slice(16, 36).toString('hex') ] // 20 bytes - } else { throw new Error('Invalid action in UDP packet: ' + params.action) } diff --git a/lib/swarm.js b/lib/swarm.js index b7c2baa..9d8bcbe 100644 --- a/lib/swarm.js +++ b/lib/swarm.js @@ -28,7 +28,6 @@ Swarm.prototype.announce = function (params, cb) { incomplete: self.incomplete, peers: self._getPeers(params.numwant) }) - } else { cb(new Error('invalid event')) } diff --git a/package.json b/package.json index 7f40189..b6a4d95 100644 --- a/package.json +++ b/package.json @@ -32,6 +32,7 @@ "devDependencies": { "magnet-uri": "^4.0.0", "parse-torrent": "^3.0.1", + "standard": "^1.0.0", "tape": "^3.0.3" }, "homepage": "http://webtorrent.io", @@ -52,6 +53,6 @@ "url": "git://github.com/feross/bittorrent-tracker.git" }, "scripts": { - "test": "tape test/*.js" + "test": "standard && tape test/*.js" } } diff --git a/server.js b/server.js index 82c5974..b2f6133 100644 --- a/server.js +++ b/server.js @@ -18,7 +18,6 @@ var parseUdpRequest = require('./lib/parse_udp') // Use random port above 1024 portfinder.basePort = Math.floor(Math.random() * 60000) + 1025 - inherits(Server, EventEmitter) /** @@ -183,11 +182,7 @@ Server.prototype.onUdpRequest = function (msg, rinfo) { response.transactionId = params.transactionId response.connectionId = params.connectionId var buf = makeUdpPacket(response) - self._udpSocket.send(buf, 0, buf.length, rinfo.port, rinfo.address, function () { - try { - socket.close() - } catch (err) {} - }) + self._udpSocket.send(buf, 0, buf.length, rinfo.port, rinfo.address) }) } @@ -274,15 +269,17 @@ Server.prototype._onScrape = function (params, cb) { } function makeUdpPacket (params) { + var packet switch (params.action) { case common.ACTIONS.CONNECT: - return Buffer.concat([ + packet = Buffer.concat([ common.toUInt32(common.ACTIONS.CONNECT), common.toUInt32(params.transactionId), params.connectionId ]) + break case common.ACTIONS.ANNOUNCE: - return Buffer.concat([ + packet = Buffer.concat([ common.toUInt32(common.ACTIONS.ANNOUNCE), common.toUInt32(params.transactionId), common.toUInt32(params.interval), @@ -290,6 +287,7 @@ function makeUdpPacket (params) { common.toUInt32(params.complete), params.peers ]) + break case common.ACTIONS.SCRAPE: var firstInfoHash = Object.keys(params.files)[0] var scrapeInfo = firstInfoHash ? { @@ -297,20 +295,24 @@ function makeUdpPacket (params) { incomplete: params.files[firstInfoHash].incomplete, completed: params.files[firstInfoHash].complete // TODO: this only provides a lower-bound } : {} - return Buffer.concat([ + packet = Buffer.concat([ common.toUInt32(common.ACTIONS.SCRAPE), common.toUInt32(params.transactionId), common.toUInt32(scrapeInfo.complete), common.toUInt32(scrapeInfo.completed), common.toUInt32(scrapeInfo.incomplete) ]) + break case common.ACTIONS.ERROR: - return Buffer.concat([ + packet = Buffer.concat([ common.toUInt32(common.ACTIONS.ERROR), common.toUInt32(params.transactionId || 0), new Buffer(params.message, 'utf8') ]) - default: - throw new Error('Action not implemented: ' + params.action) + break + default: + throw new Error('Action not implemented: ' + params.action) + break } + return packet } diff --git a/test/client-large-torrent.js b/test/client-large-torrent.js index 5b3308b..74a6646 100644 --- a/test/client-large-torrent.js +++ b/test/client-large-torrent.js @@ -53,7 +53,6 @@ test('large torrent: client.start()', function (t) { t.pass('server close') }) }) - }) }) }) diff --git a/test/client.js b/test/client.js index fd0dbdb..17832ee 100644 --- a/test/client.js +++ b/test/client.js @@ -83,7 +83,6 @@ function testClientStop (t, serverType) { t.pass('server close') }) }) - }, 1000) }) } @@ -113,7 +112,6 @@ function testClientUpdate (t, serverType) { client.start() client.once('update', function () { - client.once('update', function (data) { // received an update! t.equal(data.announce, announceUrl) diff --git a/test/scrape.js b/test/scrape.js index 81e8d53..73d38f3 100644 --- a/test/scrape.js +++ b/test/scrape.js @@ -138,7 +138,6 @@ test('server: all info_hash scrape', function (t) { parsedBitlove.announce = [ announceUrl ] server.once('listening', function () { - // announce a torrent to the tracker var client = new Client(peerId, port, parsedBitlove) client.on('error', function (err) { @@ -147,7 +146,6 @@ test('server: all info_hash scrape', function (t) { client.start() server.once('start', function () { - // now do a scrape of everything by omitting the info_hash param get.concat(scrapeUrl, function (err, data, res) { if (err) throw err