Skip to content

Commit 66b71db

Browse files
committed
Merge pull request webtorrent#46 from feross/astro-fixes
swarm.announce() should always call callback
2 parents 20cb182 + c75ca05 commit 66b71db

File tree

4 files changed

+32
-43
lines changed

4 files changed

+32
-43
lines changed

lib/parse_http.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1+
module.exports = parseHttpRequest
2+
13
var common = require('./common')
24

35
var REMOVE_IPV6_RE = /^::ffff:/
46

5-
module.exports = parseHttpRequest
6-
77
function parseHttpRequest (req, options) {
88
var s = req.url.split('?')
99
var params = common.querystringParse(s[1])
1010

1111
if (s[0] === '/announce') {
1212
params.action = common.ACTIONS.ANNOUNCE
13-
13+
1414
params.peer_id = typeof params.peer_id === 'string' && common.binaryToUtf8(params.peer_id)
1515
params.port = Number(params.port)
1616

lib/parse_udp.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
module.exports = parseUdpRequest
2+
13
var bufferEqual = require('buffer-equal')
24
var ipLib = require('ip')
35
var common = require('./common')
46

5-
6-
module.exports = parseUdpRequest
7-
87
function parseUdpRequest (msg, rinfo) {
98
if (msg.length < 16) {
109
throw new Error('received packet is too short')
@@ -20,7 +19,7 @@ function parseUdpRequest (msg, rinfo) {
2019
transactionId: msg.readUInt32BE(12)
2120
}
2221

23-
// TODO: randomize:
22+
// TODO: randomize
2423
if (!bufferEqual(params.connectionId, common.CONNECTION_ID)) {
2524
throw new Error('received packet with invalid connection id')
2625
}

lib/swarm.js

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
var debug = require('debug')('bittorrent-tracker')
2-
31
module.exports = Swarm
42

3+
var debug = require('debug')('bittorrent-tracker')
4+
55
// Regard this as the default implementation of an interface that you
66
// need to support when overriding Server.getSwarm()
77
function Swarm (infoHash, server) {
@@ -19,30 +19,27 @@ Swarm.prototype.announce = function (params, cb) {
1919
if (!params.event || params.event === 'empty') params.event = 'update'
2020
var fn = '_onAnnounce_' + params.event
2121
if (self[fn]) {
22-
self[fn](params, peer, function (err) {
23-
// event processed, prepare response:
22+
self[fn](params, peer) // process event
2423

25-
if (params.left === 0 && peer) peer.complete = true
24+
if (params.left === 0 && peer) peer.complete = true
2625

27-
// send peers
28-
var peers = self._getPeers(params.numwant)
29-
30-
cb(null, {
31-
complete: self.complete,
32-
incomplete: self.incomplete,
33-
peers: peers
34-
})
26+
cb(null, {
27+
complete: self.complete,
28+
incomplete: self.incomplete,
29+
peers: self._getPeers(params.numwant)
3530
})
31+
3632
} else {
3733
cb(new Error('invalid event'))
3834
}
3935
}
4036

41-
Swarm.prototype._onAnnounce_started = function (params, peer, cb) {
37+
Swarm.prototype._onAnnounce_started = function (params, peer) {
4238
if (peer) {
4339
debug('unexpected `started` event from peer that is already in swarm')
44-
return this._onAnnounce_update() // treat as an update
40+
return this._onAnnounce_update(params, peer) // treat as an update
4541
}
42+
4643
if (params.left === 0) this.complete += 1
4744
else this.incomplete += 1
4845
peer = this.peers[params.addr] = {
@@ -51,48 +48,42 @@ Swarm.prototype._onAnnounce_started = function (params, peer, cb) {
5148
peerId: params.peer_id
5249
}
5350
this.emit('start', params.addr)
54-
55-
cb()
5651
}
5752

58-
Swarm.prototype._onAnnounce_stopped = function (params, peer, cb) {
53+
Swarm.prototype._onAnnounce_stopped = function (params, peer) {
5954
if (!peer) {
6055
debug('unexpected `stopped` event from peer that is not in swarm')
6156
return // do nothing
6257
}
58+
6359
if (peer.complete) this.complete -= 1
6460
else this.incomplete -= 1
6561
this.peers[params.addr] = null
6662
this.emit('stop', params.addr)
67-
68-
cb()
6963
}
7064

71-
Swarm.prototype._onAnnounce_completed = function (params, peer, cb) {
65+
Swarm.prototype._onAnnounce_completed = function (params, peer) {
7266
if (!peer) {
7367
debug('unexpected `completed` event from peer that is not in swarm')
74-
return start() // treat as a start
68+
return this._onAnnounce_started(params, peer) // treat as a start
7569
}
7670
if (peer.complete) {
7771
debug('unexpected `completed` event from peer that is already marked as completed')
7872
return // do nothing
7973
}
74+
8075
this.complete += 1
8176
this.incomplete -= 1
8277
peer.complete = true
8378
this.emit('complete', params.addr)
84-
85-
cb()
8679
}
8780

88-
Swarm.prototype._onAnnounce_update = function (params, peer, cb) {
81+
Swarm.prototype._onAnnounce_update = function (params, peer) {
8982
if (!peer) {
9083
debug('unexpected `update` event from peer that is not in swarm')
91-
return start() // treat as a start
84+
return this._onAnnounce_started(params, peer) // treat as a start
9285
}
9386
this.emit('update', params.addr)
94-
95-
cb()
9687
}
9788

9889
Swarm.prototype._getPeers = function (numwant) {
@@ -110,7 +101,7 @@ Swarm.prototype._getPeers = function (numwant) {
110101
return peers
111102
}
112103

113-
Swarm.prototype.scrape = function (infoHash, params, cb) {
104+
Swarm.prototype.scrape = function (params, cb) {
114105
cb(null, {
115106
complete: this.complete,
116107
incomplete: this.incomplete

server.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ Server.prototype._onHttpRequest = function (req, res) {
147147
// even though it's an error for the client, it's just a warning for the server.
148148
// don't crash the server because a client sent bad data :)
149149
self.emit('warning', error)
150-
150+
151151
return
152152
}
153153

@@ -185,7 +185,7 @@ Server.prototype._onUdpRequest = function (msg, rinfo) {
185185
'failure reason': err.message
186186
}
187187
}
188-
188+
189189
var socket = dgram.createSocket('udp4')
190190
response.transactionId = params.transactionId
191191
response.connectionId = params.connectionId
@@ -199,7 +199,6 @@ Server.prototype._onUdpRequest = function (msg, rinfo) {
199199
}
200200

201201
Server.prototype._onRequest = function (params, cb) {
202-
var response
203202
if (params && params.action === common.ACTIONS.CONNECT) {
204203
cb(null, { action: common.ACTIONS.CONNECT })
205204
} else if (params && params.action === common.ACTIONS.ANNOUNCE) {
@@ -230,21 +229,21 @@ Server.prototype._onAnnounce = function (params, cb) {
230229

231230
Server.prototype._onScrape = function (params, cb) {
232231
var self = this
233-
232+
234233
if (typeof params.info_hash === 'string') {
235234
params.info_hash = [ params.info_hash ]
236235
} else if (params.info_hash == null) {
237236
// if info_hash param is omitted, stats for all torrents are returned
238237
// TODO: make this configurable!
239238
params.info_hash = Object.keys(self.torrents)
240239
}
241-
240+
242241
if (!Array.isArray(params.info_hash)) {
243242
var err = new Error('invalid info_hash')
244243
self.emit('warning', err)
245244
return cb(err)
246245
}
247-
246+
248247
var response = {
249248
action: common.ACTIONS.SCRAPE,
250249
files: {},
@@ -256,7 +255,7 @@ Server.prototype._onScrape = function (params, cb) {
256255
series(params.info_hash.map(function (infoHash) {
257256
var swarm = self.getSwarm(infoHash)
258257
return function (cb) {
259-
swarm.scrape(infoHash, params, function (err, scrapeInfo) {
258+
swarm.scrape(params, function (err, scrapeInfo) {
260259
cb(err, scrapeInfo && {
261260
infoHash: infoHash,
262261
complete: scrapeInfo.complete || 0,

0 commit comments

Comments
 (0)