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

View File

@ -8,7 +8,7 @@ function parseWebSocketRequest (socket, opts, params) {
params.type = 'ws'
params.socket = socket
if (params.action === 'announce' || params.answer || params.offers) {
if (params.action === 'announce' || params.offers || params.answer) {
params.action = common.ACTIONS.ANNOUNCE
if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) {
@ -45,8 +45,6 @@ function parseWebSocketRequest (socket, opts, params) {
}
return common.binaryToHex(binaryInfoHash)
})
} else {
params.info_hash = common.binaryToHex(params.info_hash)
}
} else {
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) {
if (err) {
socket.send(JSON.stringify({
action: params.action,
action: params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape',
'failure reason': err.message,
info_hash: common.hexToBinary(params.info_hash)
}), socket.onSend)
@ -337,22 +337,18 @@ Server.prototype._onWebSocketRequest = function (socket, opts, params) {
}
if (self.destroyed) return
var hashes
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)
}
})
response.action = params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape'
var peers = response.peers
delete response.peers
var peers
if (response.action === 'announce') {
peers = response.peers
delete response.peers
// WebSocket tracker should have a shorter interval default: 2 minutes
response.interval = Math.ceil(self.intervalMs / 1000 / 5)
response.info_hash = common.hexToBinary(params.info_hash)
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)
debug('sent response %s to %s', JSON.stringify(response), params.peer_id)