Skip to content

Commit ba4a52d

Browse files
committed
Fixes for PR webtorrent#125
1 parent 3d753ab commit ba4a52d

File tree

3 files changed

+14
-22
lines changed

3 files changed

+14
-22
lines changed

lib/client/websocket-tracker.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,9 @@ WebSocketTracker.prototype._onSocketData = function (data) {
149149
return
150150
}
151151

152-
if (data.action === common.ACTIONS.ANNOUNCE || data.offer || data.answer) {
152+
if (data.action === 'announce' || data.offer || data.answer) {
153153
self._onAnnounceResponse(data)
154-
} else if (data.action === common.ACTIONS.SCRAPE) {
154+
} else if (data.action === 'scrape') {
155155
self._onScrapeResponse(data)
156156
} else {
157157
throw new Error('invalid action in WS response: ' + data.action)
@@ -245,9 +245,7 @@ WebSocketTracker.prototype._onAnnounceResponse = function (data) {
245245

246246
WebSocketTracker.prototype._onScrapeResponse = function (data) {
247247
var self = this
248-
// NOTE: the unofficial spec says to use the 'files' key, 'host' has been
249-
// seen in practice
250-
data = data.files || data.host || {}
248+
data = data.files || {}
251249

252250
var keys = Object.keys(data)
253251
if (keys.length === 0) {

lib/server/parse-websocket.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function parseWebSocketRequest (socket, opts, params) {
88

99
params.type = 'ws'
1010
params.socket = socket
11-
if (params.action === 'announce' || params.answer || params.offers) {
11+
if (params.action === 'announce' || params.offers || params.answer) {
1212
params.action = common.ACTIONS.ANNOUNCE
1313

1414
if (typeof params.info_hash !== 'string' || params.info_hash.length !== 20) {
@@ -45,8 +45,6 @@ function parseWebSocketRequest (socket, opts, params) {
4545
}
4646
return common.binaryToHex(binaryInfoHash)
4747
})
48-
} else {
49-
params.info_hash = common.binaryToHex(params.info_hash)
5048
}
5149
} else {
5250
throw new Error('invalid action in WS request: ' + params.action)

server.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ Server.prototype._onWebSocketRequest = function (socket, opts, params) {
327327
self._onRequest(params, function (err, response) {
328328
if (err) {
329329
socket.send(JSON.stringify({
330-
action: params.action,
330+
action: params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape',
331331
'failure reason': err.message,
332332
info_hash: common.hexToBinary(params.info_hash)
333333
}), socket.onSend)
@@ -337,22 +337,18 @@ Server.prototype._onWebSocketRequest = function (socket, opts, params) {
337337
}
338338
if (self.destroyed) return
339339

340-
var hashes
341-
if (typeof params.info_hash === 'string') hashes = [ params.info_hash ]
342-
else hashes = params.info_hash
343-
hashes.forEach(function (info_hash) {
344-
if (socket.infoHashes.indexOf(info_hash) === -1) {
345-
socket.infoHashes.push(info_hash)
346-
}
347-
})
340+
response.action = params.action === common.ACTIONS.ANNOUNCE ? 'announce' : 'scrape'
348341

349-
var peers = response.peers
350-
delete response.peers
342+
var peers
343+
if (response.action === 'announce') {
344+
peers = response.peers
345+
delete response.peers
351346

352-
// WebSocket tracker should have a shorter interval – default: 2 minutes
353-
response.interval = Math.ceil(self.intervalMs / 1000 / 5)
347+
response.info_hash = common.hexToBinary(params.info_hash)
354348

355-
response.info_hash = common.hexToBinary(params.info_hash)
349+
// WebSocket tracker should have a shorter interval – default: 2 minutes
350+
response.interval = Math.ceil(self.intervalMs / 1000 / 5)
351+
}
356352

357353
socket.send(JSON.stringify(response), socket.onSend)
358354
debug('sent response %s to %s', JSON.stringify(response), params.peer_id)

0 commit comments

Comments
 (0)