Skip to content

Commit 2966165

Browse files
committed
BREAKING: Client() takes single opts object now
To use the client, you used to pass in four arguments: `new Client(peerId, port, parsedTorrent, opts)` Now, passing in the torrent is no longer required, just the `announce` and `infoHash` properties. This decouples this package from `parse-torrent`. All options get passed in together now: new Client({ infoHash: '', // hex string or Buffer peerId: '', // hex string or Buffer announce: [], // list of tracker server urls port: 6881 // torrent client port, (in browser, optional) }) All the normal optional arguments (rtcConfig, wrtc, etc.) can still be passed in with the rest of these options. Fixes webtorrent#118. Fixes webtorrent#115. Added ws tests for scrape.
1 parent 7a28963 commit 2966165

File tree

9 files changed

+217
-151
lines changed

9 files changed

+217
-151
lines changed

README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,15 @@ To connect to a tracker, just do this:
5050

5151
```js
5252
var Client = require('bittorrent-tracker')
53-
var parseTorrent = require('parse-torrent')
54-
var fs = require('fs')
5553

56-
var torrent = fs.readFileSync(__dirname + '/torrents/name.torrent')
57-
var parsedTorrent = parseTorrent(torrent) // { infoHash: 'xxx', length: xx, announce: ['xx', 'xx'] }
58-
59-
var peerId = new Buffer('01234567890123456789')
60-
var port = 6881
54+
var requiredOpts = {
55+
infoHash: new Buffer('012345678901234567890'), // hex string or Buffer
56+
peerId: new Buffer('01234567890123456789'), // hex string or Buffer
57+
announce: [], // list of tracker server urls
58+
port: 6881 // torrent client port, (in browser, optional)
59+
}
6160

62-
// optional options dictionary
63-
var opts = {
61+
var optionalOpts = {
6462
// RTCPeerConnection config object (only used in browser)
6563
rtcConfig: {},
6664
// custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc)
@@ -77,7 +75,7 @@ var opts = {
7775
}
7876
}
7977

80-
var client = new Client(peerId, port, parsedTorrent)
78+
var client = new Client(requiredOpts)
8179

8280
client.on('error', function (err) {
8381
// fatal client error!

client.js

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,37 +21,42 @@ inherits(Client, EventEmitter)
2121
*
2222
* Find torrent peers, to help a torrent client participate in a torrent swarm.
2323
*
24-
* @param {string|Buffer} peerId peer id
25-
* @param {Number} port torrent client listening port
26-
* @param {Object} torrent parsed torrent
27-
* @param {Object} opts options object
28-
* @param {Number} opts.rtcConfig RTCPeerConnection configuration object
29-
* @param {Number} opts.wrtc custom webrtc impl (useful in node.js)
30-
* @param {function} opts.getAnnounceOpts callback to provide data to tracker
24+
* @param {Object} opts options object
25+
* @param {string|Buffer} opts.infoHash torrent info hash
26+
* @param {string|Buffer} opts.peerId peer id
27+
* @param {string|Array.<string>} opts.announce announce
28+
* @param {number} opts.port torrent client listening port
29+
* @param {function} opts.getAnnounceOpts callback to provide data to tracker
30+
* @param {number} opts.rtcConfig RTCPeerConnection configuration object
31+
* @param {number} opts.wrtc custom webrtc impl (useful in node.js)
3132
*/
32-
function Client (peerId, port, torrent, opts) {
33+
function Client (opts) {
3334
var self = this
34-
if (!(self instanceof Client)) return new Client(peerId, port, torrent, opts)
35+
if (!(self instanceof Client)) return new Client(opts)
3536
EventEmitter.call(self)
3637
if (!opts) opts = {}
3738

39+
if (!opts.peerId) throw new Error('Option `peerId` is required')
40+
if (!opts.infoHash) throw new Error('Option `infoHash` is required')
41+
if (!opts.announce) throw new Error('Option `announce` is required')
42+
if (!process.browser && !opts.port) throw new Error('Option `port` is required')
43+
3844
// required
39-
self.peerId = typeof peerId === 'string'
40-
? peerId
41-
: peerId.toString('hex')
42-
self.peerIdBuffer = new Buffer(self.peerId, 'hex')
43-
self._peerIdBinary = self.peerIdBuffer.toString('binary')
44-
45-
self.infoHash = typeof torrent.infoHash === 'string'
46-
? torrent.infoHash
47-
: torrent.infoHash.toString('hex')
48-
self.infoHashBuffer = new Buffer(self.infoHash, 'hex')
49-
self._infoHashBinary = self.infoHashBuffer.toString('binary')
50-
51-
self.torrentLength = torrent.length
52-
self.destroyed = false
45+
self.peerId = typeof opts.peerId === 'string'
46+
? opts.peerId
47+
: opts.peerId.toString('hex')
48+
self._peerIdBuffer = new Buffer(self.peerId, 'hex')
49+
self._peerIdBinary = self._peerIdBuffer.toString('binary')
50+
51+
self.infoHash = typeof opts.infoHash === 'string'
52+
? opts.infoHash
53+
: opts.infoHash.toString('hex')
54+
self._infoHashBuffer = new Buffer(self.infoHash, 'hex')
55+
self._infoHashBinary = self._infoHashBuffer.toString('binary')
5356

54-
self._port = port
57+
self._port = opts.port
58+
59+
self.destroyed = false
5560

5661
self._rtcConfig = opts.rtcConfig
5762
self._wrtc = opts.wrtc
@@ -61,11 +66,11 @@ function Client (peerId, port, torrent, opts) {
6166

6267
var webrtcSupport = !!self._wrtc || typeof window !== 'undefined'
6368

64-
var announce = (typeof torrent.announce === 'string')
65-
? [ torrent.announce ]
66-
: torrent.announce == null
69+
var announce = (typeof opts.announce === 'string')
70+
? [ opts.announce ]
71+
: opts.announce == null
6772
? []
68-
: torrent.announce
73+
: opts.announce
6974

7075
announce = announce.map(function (announceUrl) {
7176
announceUrl = announceUrl.toString()
@@ -112,23 +117,27 @@ function Client (peerId, port, torrent, opts) {
112117
* Simple convenience function to scrape a tracker for an info hash without needing to
113118
* create a Client, pass it a parsed torrent, etc. Support scraping a tracker for multiple
114119
* torrents at the same time.
115-
* @param {string} announceUrl
116-
* @param {string|Array.<string>} infoHash
120+
* @params {Object} opts
121+
* @param {string|Array.<string>} opts.infoHash
122+
* @param {string} opts.announce
117123
* @param {function} cb
118124
*/
119-
Client.scrape = function (announceUrl, infoHash, cb) {
125+
Client.scrape = function (opts, cb) {
120126
cb = once(cb)
121127

122-
var peerId = new Buffer('01234567890123456789') // dummy value
123-
var port = 6881 // dummy value
124-
var torrent = {
125-
infoHash: Array.isArray(infoHash) ? infoHash[0] : infoHash,
126-
announce: [ announceUrl ]
127-
}
128-
var client = new Client(peerId, port, torrent)
128+
if (!opts.infoHash) throw new Error('Option `infoHash` is required')
129+
if (!opts.announce) throw new Error('Option `announce` is required')
130+
131+
var clientOpts = extend(opts, {
132+
infoHash: Array.isArray(opts.infoHash) ? opts.infoHash[0] : opts.infoHash,
133+
peerId: new Buffer('01234567890123456789'), // dummy value
134+
port: 6881 // dummy value
135+
})
136+
137+
var client = new Client(clientOpts)
129138
client.once('error', cb)
130139

131-
var len = Array.isArray(infoHash) ? infoHash.length : 1
140+
var len = Array.isArray(opts.infoHash) ? opts.infoHash.length : 1
132141
var results = {}
133142
client.on('scrape', function (data) {
134143
len -= 1
@@ -144,10 +153,11 @@ Client.scrape = function (announceUrl, infoHash, cb) {
144153
}
145154
})
146155

147-
infoHash = Array.isArray(infoHash)
148-
? infoHash.map(function (infoHash) { return new Buffer(infoHash, 'hex') })
149-
: new Buffer(infoHash, 'hex')
150-
client.scrape({ infoHash: infoHash })
156+
opts.infoHash = Array.isArray(opts.infoHash)
157+
? opts.infoHash.map(function (infoHash) { return new Buffer(infoHash, 'hex') })
158+
: new Buffer(opts.infoHash, 'hex')
159+
client.scrape({ infoHash: opts.infoHash })
160+
return client
151161
}
152162

153163
/**
@@ -198,9 +208,6 @@ Client.prototype.complete = function (opts) {
198208
var self = this
199209
debug('send `complete`')
200210
if (!opts) opts = {}
201-
if (opts.downloaded == null && self.torrentLength != null) {
202-
opts.downloaded = self.torrentLength
203-
}
204211
opts = self._defaultAnnounceOpts(opts)
205212
opts.event = 'completed'
206213
self._announce(opts)
@@ -277,10 +284,6 @@ Client.prototype._defaultAnnounceOpts = function (opts) {
277284
if (opts.uploaded == null) opts.uploaded = 0
278285
if (opts.downloaded == null) opts.downloaded = 0
279286

280-
if (opts.left == null && self.torrentLength != null) {
281-
opts.left = self.torrentLength - opts.downloaded
282-
}
283-
284287
if (self._getAnnounceOpts) opts = extend(opts, self._getAnnounceOpts())
285288
return opts
286289
}

lib/client/udp-tracker.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ UDPTracker.prototype._request = function (opts) {
193193
connectionId,
194194
common.toUInt32(common.ACTIONS.ANNOUNCE),
195195
transactionId,
196-
self.client.infoHashBuffer,
197-
self.client.peerIdBuffer,
196+
self.client._infoHashBuffer,
197+
self.client._peerIdBuffer,
198198
toUInt64(opts.downloaded),
199199
opts.left != null ? toUInt64(opts.left) : new Buffer('FFFFFFFFFFFFFFFF', 'hex'),
200200
toUInt64(opts.uploaded),
@@ -211,7 +211,7 @@ UDPTracker.prototype._request = function (opts) {
211211

212212
var infoHash = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
213213
? Buffer.concat(opts.infoHash)
214-
: (opts.infoHash || self.client.infoHashBuffer)
214+
: (opts.infoHash || self.client._infoHashBuffer)
215215

216216
send(Buffer.concat([
217217
connectionId,

test/client-large-torrent.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
var Client = require('../')
22
var common = require('./common')
3-
var extend = require('xtend')
43
var fixtures = require('webtorrent-fixtures')
54
var test = require('tape')
65

@@ -9,11 +8,14 @@ var peerId = new Buffer('01234567890123456789')
98
function testLargeTorrent (t, serverType) {
109
t.plan(9)
1110

12-
var parsedTorrent = extend(fixtures.sintel.parsedTorrent)
13-
1411
common.createServer(t, serverType, function (server, announceUrl) {
15-
parsedTorrent.announce = [ announceUrl ]
16-
var client = new Client(peerId, 6881, parsedTorrent, { wrtc: {} })
12+
var client = new Client({
13+
infoHash: fixtures.sintel.parsedTorrent.infoHash,
14+
peerId: peerId,
15+
port: 6881,
16+
announce: announceUrl,
17+
wrtc: {}
18+
})
1719

1820
if (serverType === 'ws') common.mockWebsocketTracker(client)
1921
client.on('error', function (err) { t.error(err) })

test/client-magnet.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ function testMagnet (t, serverType) {
1212
var parsedTorrent = magnet(fixtures.leaves.magnetURI)
1313

1414
common.createServer(t, serverType, function (server, announceUrl) {
15-
parsedTorrent.announce = [ announceUrl ]
16-
17-
var client = new Client(peerId, 6881, parsedTorrent, { wrtc: {} })
15+
var client = new Client({
16+
infoHash: parsedTorrent.infoHash,
17+
announce: announceUrl,
18+
peerId: peerId,
19+
port: 6881,
20+
wrtc: {}
21+
})
1822

1923
if (serverType === 'ws') common.mockWebsocketTracker(client)
2024
client.on('error', function (err) { t.error(err) })

0 commit comments

Comments
 (0)