Fixes for PR #125

This commit is contained in:
Feross Aboukhadijeh 2016-03-15 20:06:39 -07:00
parent 3d753ab805
commit ba4a52d2d1
3 changed files with 14 additions and 22 deletions

View File

@ -149,9 +149,9 @@ WebSocketTracker.prototype._onSocketData = function (data) {
return return
} }
if (data.action === common.ACTIONS.ANNOUNCE || data.offer || data.answer) { if (data.action === 'announce' || data.offer || data.answer) {
self._onAnnounceResponse(data) self._onAnnounceResponse(data)
} else if (data.action === common.ACTIONS.SCRAPE) { } else if (data.action === 'scrape') {
self._onScrapeResponse(data) self._onScrapeResponse(data)
} else { } else {
throw new Error('invalid action in WS response: ' + data.action) throw new Error('invalid action in WS response: ' + data.action)
@ -245,9 +245,7 @@ WebSocketTracker.prototype._onAnnounceResponse = function (data) {
WebSocketTracker.prototype._onScrapeResponse = function (data) { WebSocketTracker.prototype._onScrapeResponse = function (data) {
var self = this var self = this
// NOTE: the unofficial spec says to use the 'files' key, 'host' has been data = data.files || {}
// seen in practice
data = data.files || data.host || {}
var keys = Object.keys(data) var keys = Object.keys(data)
if (keys.length === 0) { if (keys.length === 0) {

View File

@ -8,7 +8,7 @@ function parseWebSocketRequest (socket, opts, params) {
params.type = 'ws' params.type = 'ws'
params.socket = socket params.socket = socket
if (params.action === 'announce' || params.answer || params.offers) { if (params.action === 'announce' || params.offers || params.answer) {
params.action = common.ACTIONS.ANNOUNCE params.action = common.ACTIONS.ANNOUNCE
if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) { if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) {
@ -45,8 +45,6 @@ function parseWebSocketRequest (socket, opts, params) {
} }
return common.binaryToHex(binaryInfoHash) return common.binaryToHex(binaryInfoHash)
}) })
} else {
params.info_hash = common.binaryToHex(params.info_hash)
} }
} else { } else {
throw new Error('invalid action in WS request: ' + params.action) throw new Error('invalid action in WS request: ' + params.action)

View File

@ -327,7 +327,7 @@ Server.prototype._onWebSocketRequest = function (socket, opts, params) {
self._onRequest(params, function (err, response) { self._onRequest(params, function (err, response) {
if (err) { if (err) {
socket.send(JSON.stringify({ socket.send(JSON.stringify({
action: params.action, action: params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape',
'failure reason': err.message, 'failure reason': err.message,
info_hash: common.hexToBinary(params.info_hash) info_hash: common.hexToBinary(params.info_hash)
}), socket.onSend) }), socket.onSend)
@ -337,22 +337,18 @@ Server.prototype._onWebSocketRequest = function (socket, opts, params) {
} }
if (self.destroyed) return if (self.destroyed) return
var hashes response.action = params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape'
if (typeof params.info_hash === 'string') hashes = [ params.info_hash ]
else hashes = params.info_hash
hashes.forEach(function (info_hash) {
if (socket.infoHashes.indexOf(info_hash) === -1) {
socket.infoHashes.push(info_hash)
}
})
var peers = response.peers var peers
delete response.peers if (response.action === 'announce') {
peers = response.peers
delete response.peers
// WebSocket tracker should have a shorter interval default: 2 minutes response.info_hash = common.hexToBinary(params.info_hash)
response.interval = Math.ceil(self.intervalMs / 1000 / 5)
response.info_hash = common.hexToBinary(params.info_hash) // WebSocket tracker should have a shorter interval default: 2 minutes
response.interval = Math.ceil(self.intervalMs / 1000 / 5)
}
socket.send(JSON.stringify(response), socket.onSend) socket.send(JSON.stringify(response), socket.onSend)
debug('sent response %s to %s', JSON.stringify(response), params.peer_id) debug('sent response %s to %s', JSON.stringify(response), params.peer_id)