JavaScript Standard Style

https://github.com/feross/standard
This commit is contained in:
Feross Aboukhadijeh 2015-01-26 18:16:01 -08:00
parent 556f672028
commit ebb86f728f
9 changed files with 17 additions and 27 deletions

View File

@ -10,8 +10,6 @@ var EventEmitter = require('events').EventEmitter
var extend = require('extend.js') var extend = require('extend.js')
var get = require('simple-get') var get = require('simple-get')
var hat = require('hat') var hat = require('hat')
var http = require('http')
var https = require('https')
var inherits = require('inherits') var inherits = require('inherits')
var once = require('once') var once = require('once')
var url = require('url') var url = require('url')

View File

@ -32,8 +32,7 @@ function parseHttpRequest (req, options) {
? req.headers['x-forwarded-for'] || req.connection.remoteAddress ? req.headers['x-forwarded-for'] || req.connection.remoteAddress
: req.connection.remoteAddress.replace(REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4 : 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 params.addr = (common.IPV6_RE.test(params.ip) ? '[' + params.ip + ']' : params.ip) + ':' + params.port
} else if (s[0] === '/scrape') {
} else if (s[0] === '/scrape') { // unofficial scrape message
params.action = common.ACTIONS.SCRAPE params.action = common.ACTIONS.SCRAPE
if (typeof params.info_hash === 'string') if (typeof params.info_hash === 'string')
params.info_hash = [ params.info_hash ] params.info_hash = [ params.info_hash ]
@ -45,7 +44,6 @@ function parseHttpRequest (req, options) {
return common.binaryToHex(binaryInfoHash) return common.binaryToHex(binaryInfoHash)
}) })
} }
} else { } else {
throw new Error('Invalid action in HTTP request: ' + params.action) throw new Error('Invalid action in HTTP request: ' + params.action)
} }

View File

@ -23,7 +23,6 @@ function parseUdpRequest (msg, rinfo) {
if (params.action === common.ACTIONS.CONNECT) { if (params.action === common.ACTIONS.CONNECT) {
// No further params // No further params
} else if (params.action === common.ACTIONS.ANNOUNCE) { } else if (params.action === common.ACTIONS.ANNOUNCE) {
params.info_hash = msg.slice(16, 36).toString('hex') // 20 bytes params.info_hash = msg.slice(16, 36).toString('hex') // 20 bytes
params.peer_id = msg.slice(36, 56).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.port = msg.readUInt16BE(96) || rinfo.port // optional
params.addr = params.ip + ':' + params.port // TODO: ipv6 brackets params.addr = params.ip + ':' + params.port // TODO: ipv6 brackets
params.compact = 1 // udp is always compact params.compact = 1 // udp is always compact
} else if (params.action === common.ACTIONS.SCRAPE) { // scrape message } else if (params.action === common.ACTIONS.SCRAPE) { // scrape message
// TODO: support multiple info_hash scrape // TODO: support multiple info_hash scrape
if (msg.length > 36) throw new Error('multiple info_hash scrape not supported') if (msg.length > 36) throw new Error('multiple info_hash scrape not supported')
params.info_hash = [ msg.slice(16, 36).toString('hex') ] // 20 bytes params.info_hash = [ msg.slice(16, 36).toString('hex') ] // 20 bytes
} else { } else {
throw new Error('Invalid action in UDP packet: ' + params.action) throw new Error('Invalid action in UDP packet: ' + params.action)
} }

View File

@ -28,7 +28,6 @@ Swarm.prototype.announce = function (params, cb) {
incomplete: self.incomplete, incomplete: self.incomplete,
peers: self._getPeers(params.numwant) peers: self._getPeers(params.numwant)
}) })
} else { } else {
cb(new Error('invalid event')) cb(new Error('invalid event'))
} }

View File

@ -32,6 +32,7 @@
"devDependencies": { "devDependencies": {
"magnet-uri": "^4.0.0", "magnet-uri": "^4.0.0",
"parse-torrent": "^3.0.1", "parse-torrent": "^3.0.1",
"standard": "^1.0.0",
"tape": "^3.0.3" "tape": "^3.0.3"
}, },
"homepage": "http://webtorrent.io", "homepage": "http://webtorrent.io",
@ -52,6 +53,6 @@
"url": "git://github.com/feross/bittorrent-tracker.git" "url": "git://github.com/feross/bittorrent-tracker.git"
}, },
"scripts": { "scripts": {
"test": "tape test/*.js" "test": "standard && tape test/*.js"
} }
} }

View File

@ -18,7 +18,6 @@ var parseUdpRequest = require('./lib/parse_udp')
// Use random port above 1024 // Use random port above 1024
portfinder.basePort = Math.floor(Math.random() * 60000) + 1025 portfinder.basePort = Math.floor(Math.random() * 60000) + 1025
inherits(Server, EventEmitter) inherits(Server, EventEmitter)
/** /**
@ -183,11 +182,7 @@ Server.prototype.onUdpRequest = function (msg, rinfo) {
response.transactionId = params.transactionId response.transactionId = params.transactionId
response.connectionId = params.connectionId response.connectionId = params.connectionId
var buf = makeUdpPacket(response) var buf = makeUdpPacket(response)
self._udpSocket.send(buf, 0, buf.length, rinfo.port, rinfo.address, function () { self._udpSocket.send(buf, 0, buf.length, rinfo.port, rinfo.address)
try {
socket.close()
} catch (err) {}
})
}) })
} }
@ -274,15 +269,17 @@ Server.prototype._onScrape = function (params, cb) {
} }
function makeUdpPacket (params) { function makeUdpPacket (params) {
var packet
switch (params.action) { switch (params.action) {
case common.ACTIONS.CONNECT: case common.ACTIONS.CONNECT:
return Buffer.concat([ packet = Buffer.concat([
common.toUInt32(common.ACTIONS.CONNECT), common.toUInt32(common.ACTIONS.CONNECT),
common.toUInt32(params.transactionId), common.toUInt32(params.transactionId),
params.connectionId params.connectionId
]) ])
break
case common.ACTIONS.ANNOUNCE: case common.ACTIONS.ANNOUNCE:
return Buffer.concat([ packet = Buffer.concat([
common.toUInt32(common.ACTIONS.ANNOUNCE), common.toUInt32(common.ACTIONS.ANNOUNCE),
common.toUInt32(params.transactionId), common.toUInt32(params.transactionId),
common.toUInt32(params.interval), common.toUInt32(params.interval),
@ -290,6 +287,7 @@ function makeUdpPacket (params) {
common.toUInt32(params.complete), common.toUInt32(params.complete),
params.peers params.peers
]) ])
break
case common.ACTIONS.SCRAPE: case common.ACTIONS.SCRAPE:
var firstInfoHash = Object.keys(params.files)[0] var firstInfoHash = Object.keys(params.files)[0]
var scrapeInfo = firstInfoHash ? { var scrapeInfo = firstInfoHash ? {
@ -297,20 +295,24 @@ function makeUdpPacket (params) {
incomplete: params.files[firstInfoHash].incomplete, incomplete: params.files[firstInfoHash].incomplete,
completed: params.files[firstInfoHash].complete // TODO: this only provides a lower-bound 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(common.ACTIONS.SCRAPE),
common.toUInt32(params.transactionId), common.toUInt32(params.transactionId),
common.toUInt32(scrapeInfo.complete), common.toUInt32(scrapeInfo.complete),
common.toUInt32(scrapeInfo.completed), common.toUInt32(scrapeInfo.completed),
common.toUInt32(scrapeInfo.incomplete) common.toUInt32(scrapeInfo.incomplete)
]) ])
break
case common.ACTIONS.ERROR: case common.ACTIONS.ERROR:
return Buffer.concat([ packet = Buffer.concat([
common.toUInt32(common.ACTIONS.ERROR), common.toUInt32(common.ACTIONS.ERROR),
common.toUInt32(params.transactionId || 0), common.toUInt32(params.transactionId || 0),
new Buffer(params.message, 'utf8') new Buffer(params.message, 'utf8')
]) ])
break
default: default:
throw new Error('Action not implemented: ' + params.action) throw new Error('Action not implemented: ' + params.action)
break
} }
return packet
} }

View File

@ -53,7 +53,6 @@ test('large torrent: client.start()', function (t) {
t.pass('server close') t.pass('server close')
}) })
}) })
}) })
}) })
}) })

View File

@ -83,7 +83,6 @@ function testClientStop (t, serverType) {
t.pass('server close') t.pass('server close')
}) })
}) })
}, 1000) }, 1000)
}) })
} }
@ -113,7 +112,6 @@ function testClientUpdate (t, serverType) {
client.start() client.start()
client.once('update', function () { client.once('update', function () {
client.once('update', function (data) { client.once('update', function (data) {
// received an update! // received an update!
t.equal(data.announce, announceUrl) t.equal(data.announce, announceUrl)

View File

@ -138,7 +138,6 @@ test('server: all info_hash scrape', function (t) {
parsedBitlove.announce = [ announceUrl ] parsedBitlove.announce = [ announceUrl ]
server.once('listening', function () { server.once('listening', function () {
// announce a torrent to the tracker // announce a torrent to the tracker
var client = new Client(peerId, port, parsedBitlove) var client = new Client(peerId, port, parsedBitlove)
client.on('error', function (err) { client.on('error', function (err) {
@ -147,7 +146,6 @@ test('server: all info_hash scrape', function (t) {
client.start() client.start()
server.once('start', function () { server.once('start', function () {
// now do a scrape of everything by omitting the info_hash param // now do a scrape of everything by omitting the info_hash param
get.concat(scrapeUrl, function (err, data, res) { get.concat(scrapeUrl, function (err, data, res) {
if (err) throw err if (err) throw err