mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-18 20:21:36 +00:00
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 #118. Fixes #115. Added ws tests for scrape.
This commit is contained in:
parent
7a289638df
commit
2966165a8f
18
README.md
18
README.md
@ -50,17 +50,15 @@ To connect to a tracker, just do this:
|
||||
|
||||
```js
|
||||
var Client = require('bittorrent-tracker')
|
||||
var parseTorrent = require('parse-torrent')
|
||||
var fs = require('fs')
|
||||
|
||||
var torrent = fs.readFileSync(__dirname + '/torrents/name.torrent')
|
||||
var parsedTorrent = parseTorrent(torrent) // { infoHash: 'xxx', length: xx, announce: ['xx', 'xx'] }
|
||||
var requiredOpts = {
|
||||
infoHash: new Buffer('012345678901234567890'), // hex string or Buffer
|
||||
peerId: new Buffer('01234567890123456789'), // hex string or Buffer
|
||||
announce: [], // list of tracker server urls
|
||||
port: 6881 // torrent client port, (in browser, optional)
|
||||
}
|
||||
|
||||
var peerId = new Buffer('01234567890123456789')
|
||||
var port = 6881
|
||||
|
||||
// optional options dictionary
|
||||
var opts = {
|
||||
var optionalOpts = {
|
||||
// RTCPeerConnection config object (only used in browser)
|
||||
rtcConfig: {},
|
||||
// custom webrtc impl, useful in node to specify [wrtc](https://npmjs.com/package/wrtc)
|
||||
@ -77,7 +75,7 @@ var opts = {
|
||||
}
|
||||
}
|
||||
|
||||
var client = new Client(peerId, port, parsedTorrent)
|
||||
var client = new Client(requiredOpts)
|
||||
|
||||
client.on('error', function (err) {
|
||||
// fatal client error!
|
||||
|
95
client.js
95
client.js
@ -21,38 +21,43 @@ inherits(Client, EventEmitter)
|
||||
*
|
||||
* Find torrent peers, to help a torrent client participate in a torrent swarm.
|
||||
*
|
||||
* @param {string|Buffer} peerId peer id
|
||||
* @param {Number} port torrent client listening port
|
||||
* @param {Object} torrent parsed torrent
|
||||
* @param {Object} opts options object
|
||||
* @param {Number} opts.rtcConfig RTCPeerConnection configuration object
|
||||
* @param {Number} opts.wrtc custom webrtc impl (useful in node.js)
|
||||
* @param {string|Buffer} opts.infoHash torrent info hash
|
||||
* @param {string|Buffer} opts.peerId peer id
|
||||
* @param {string|Array.<string>} opts.announce announce
|
||||
* @param {number} opts.port torrent client listening port
|
||||
* @param {function} opts.getAnnounceOpts callback to provide data to tracker
|
||||
* @param {number} opts.rtcConfig RTCPeerConnection configuration object
|
||||
* @param {number} opts.wrtc custom webrtc impl (useful in node.js)
|
||||
*/
|
||||
function Client (peerId, port, torrent, opts) {
|
||||
function Client (opts) {
|
||||
var self = this
|
||||
if (!(self instanceof Client)) return new Client(peerId, port, torrent, opts)
|
||||
if (!(self instanceof Client)) return new Client(opts)
|
||||
EventEmitter.call(self)
|
||||
if (!opts) opts = {}
|
||||
|
||||
if (!opts.peerId) throw new Error('Option `peerId` is required')
|
||||
if (!opts.infoHash) throw new Error('Option `infoHash` is required')
|
||||
if (!opts.announce) throw new Error('Option `announce` is required')
|
||||
if (!process.browser && !opts.port) throw new Error('Option `port` is required')
|
||||
|
||||
// required
|
||||
self.peerId = typeof peerId === 'string'
|
||||
? peerId
|
||||
: peerId.toString('hex')
|
||||
self.peerIdBuffer = new Buffer(self.peerId, 'hex')
|
||||
self._peerIdBinary = self.peerIdBuffer.toString('binary')
|
||||
self.peerId = typeof opts.peerId === 'string'
|
||||
? opts.peerId
|
||||
: opts.peerId.toString('hex')
|
||||
self._peerIdBuffer = new Buffer(self.peerId, 'hex')
|
||||
self._peerIdBinary = self._peerIdBuffer.toString('binary')
|
||||
|
||||
self.infoHash = typeof torrent.infoHash === 'string'
|
||||
? torrent.infoHash
|
||||
: torrent.infoHash.toString('hex')
|
||||
self.infoHashBuffer = new Buffer(self.infoHash, 'hex')
|
||||
self._infoHashBinary = self.infoHashBuffer.toString('binary')
|
||||
self.infoHash = typeof opts.infoHash === 'string'
|
||||
? opts.infoHash
|
||||
: opts.infoHash.toString('hex')
|
||||
self._infoHashBuffer = new Buffer(self.infoHash, 'hex')
|
||||
self._infoHashBinary = self._infoHashBuffer.toString('binary')
|
||||
|
||||
self._port = opts.port
|
||||
|
||||
self.torrentLength = torrent.length
|
||||
self.destroyed = false
|
||||
|
||||
self._port = port
|
||||
|
||||
self._rtcConfig = opts.rtcConfig
|
||||
self._wrtc = opts.wrtc
|
||||
self._getAnnounceOpts = opts.getAnnounceOpts
|
||||
@ -61,11 +66,11 @@ function Client (peerId, port, torrent, opts) {
|
||||
|
||||
var webrtcSupport = !!self._wrtc || typeof window !== 'undefined'
|
||||
|
||||
var announce = (typeof torrent.announce === 'string')
|
||||
? [ torrent.announce ]
|
||||
: torrent.announce == null
|
||||
var announce = (typeof opts.announce === 'string')
|
||||
? [ opts.announce ]
|
||||
: opts.announce == null
|
||||
? []
|
||||
: torrent.announce
|
||||
: opts.announce
|
||||
|
||||
announce = announce.map(function (announceUrl) {
|
||||
announceUrl = announceUrl.toString()
|
||||
@ -112,23 +117,27 @@ function Client (peerId, port, torrent, opts) {
|
||||
* Simple convenience function to scrape a tracker for an info hash without needing to
|
||||
* create a Client, pass it a parsed torrent, etc. Support scraping a tracker for multiple
|
||||
* torrents at the same time.
|
||||
* @param {string} announceUrl
|
||||
* @param {string|Array.<string>} infoHash
|
||||
* @params {Object} opts
|
||||
* @param {string|Array.<string>} opts.infoHash
|
||||
* @param {string} opts.announce
|
||||
* @param {function} cb
|
||||
*/
|
||||
Client.scrape = function (announceUrl, infoHash, cb) {
|
||||
Client.scrape = function (opts, cb) {
|
||||
cb = once(cb)
|
||||
|
||||
var peerId = new Buffer('01234567890123456789') // dummy value
|
||||
var port = 6881 // dummy value
|
||||
var torrent = {
|
||||
infoHash: Array.isArray(infoHash) ? infoHash[0] : infoHash,
|
||||
announce: [ announceUrl ]
|
||||
}
|
||||
var client = new Client(peerId, port, torrent)
|
||||
if (!opts.infoHash) throw new Error('Option `infoHash` is required')
|
||||
if (!opts.announce) throw new Error('Option `announce` is required')
|
||||
|
||||
var clientOpts = extend(opts, {
|
||||
infoHash: Array.isArray(opts.infoHash) ? opts.infoHash[0] : opts.infoHash,
|
||||
peerId: new Buffer('01234567890123456789'), // dummy value
|
||||
port: 6881 // dummy value
|
||||
})
|
||||
|
||||
var client = new Client(clientOpts)
|
||||
client.once('error', cb)
|
||||
|
||||
var len = Array.isArray(infoHash) ? infoHash.length : 1
|
||||
var len = Array.isArray(opts.infoHash) ? opts.infoHash.length : 1
|
||||
var results = {}
|
||||
client.on('scrape', function (data) {
|
||||
len -= 1
|
||||
@ -144,10 +153,11 @@ Client.scrape = function (announceUrl, infoHash, cb) {
|
||||
}
|
||||
})
|
||||
|
||||
infoHash = Array.isArray(infoHash)
|
||||
? infoHash.map(function (infoHash) { return new Buffer(infoHash, 'hex') })
|
||||
: new Buffer(infoHash, 'hex')
|
||||
client.scrape({ infoHash: infoHash })
|
||||
opts.infoHash = Array.isArray(opts.infoHash)
|
||||
? opts.infoHash.map(function (infoHash) { return new Buffer(infoHash, 'hex') })
|
||||
: new Buffer(opts.infoHash, 'hex')
|
||||
client.scrape({ infoHash: opts.infoHash })
|
||||
return client
|
||||
}
|
||||
|
||||
/**
|
||||
@ -198,9 +208,6 @@ Client.prototype.complete = function (opts) {
|
||||
var self = this
|
||||
debug('send `complete`')
|
||||
if (!opts) opts = {}
|
||||
if (opts.downloaded == null && self.torrentLength != null) {
|
||||
opts.downloaded = self.torrentLength
|
||||
}
|
||||
opts = self._defaultAnnounceOpts(opts)
|
||||
opts.event = 'completed'
|
||||
self._announce(opts)
|
||||
@ -277,10 +284,6 @@ Client.prototype._defaultAnnounceOpts = function (opts) {
|
||||
if (opts.uploaded == null) opts.uploaded = 0
|
||||
if (opts.downloaded == null) opts.downloaded = 0
|
||||
|
||||
if (opts.left == null && self.torrentLength != null) {
|
||||
opts.left = self.torrentLength - opts.downloaded
|
||||
}
|
||||
|
||||
if (self._getAnnounceOpts) opts = extend(opts, self._getAnnounceOpts())
|
||||
return opts
|
||||
}
|
||||
|
@ -193,8 +193,8 @@ UDPTracker.prototype._request = function (opts) {
|
||||
connectionId,
|
||||
common.toUInt32(common.ACTIONS.ANNOUNCE),
|
||||
transactionId,
|
||||
self.client.infoHashBuffer,
|
||||
self.client.peerIdBuffer,
|
||||
self.client._infoHashBuffer,
|
||||
self.client._peerIdBuffer,
|
||||
toUInt64(opts.downloaded),
|
||||
opts.left != null ? toUInt64(opts.left) : new Buffer('FFFFFFFFFFFFFFFF', 'hex'),
|
||||
toUInt64(opts.uploaded),
|
||||
@ -211,7 +211,7 @@ UDPTracker.prototype._request = function (opts) {
|
||||
|
||||
var infoHash = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
|
||||
? Buffer.concat(opts.infoHash)
|
||||
: (opts.infoHash || self.client.infoHashBuffer)
|
||||
: (opts.infoHash || self.client._infoHashBuffer)
|
||||
|
||||
send(Buffer.concat([
|
||||
connectionId,
|
||||
|
@ -1,6 +1,5 @@
|
||||
var Client = require('../')
|
||||
var common = require('./common')
|
||||
var extend = require('xtend')
|
||||
var fixtures = require('webtorrent-fixtures')
|
||||
var test = require('tape')
|
||||
|
||||
@ -9,11 +8,14 @@ var peerId = new Buffer('01234567890123456789')
|
||||
function testLargeTorrent (t, serverType) {
|
||||
t.plan(9)
|
||||
|
||||
var parsedTorrent = extend(fixtures.sintel.parsedTorrent)
|
||||
|
||||
common.createServer(t, serverType, function (server, announceUrl) {
|
||||
parsedTorrent.announce = [ announceUrl ]
|
||||
var client = new Client(peerId, 6881, parsedTorrent, { wrtc: {} })
|
||||
var client = new Client({
|
||||
infoHash: fixtures.sintel.parsedTorrent.infoHash,
|
||||
peerId: peerId,
|
||||
port: 6881,
|
||||
announce: announceUrl,
|
||||
wrtc: {}
|
||||
})
|
||||
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||
client.on('error', function (err) { t.error(err) })
|
||||
|
@ -12,9 +12,13 @@ function testMagnet (t, serverType) {
|
||||
var parsedTorrent = magnet(fixtures.leaves.magnetURI)
|
||||
|
||||
common.createServer(t, serverType, function (server, announceUrl) {
|
||||
parsedTorrent.announce = [ announceUrl ]
|
||||
|
||||
var client = new Client(peerId, 6881, parsedTorrent, { wrtc: {} })
|
||||
var client = new Client({
|
||||
infoHash: parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId,
|
||||
port: 6881,
|
||||
wrtc: {}
|
||||
})
|
||||
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||
client.on('error', function (err) { t.error(err) })
|
||||
|
@ -1,6 +1,5 @@
|
||||
var Client = require('../')
|
||||
var common = require('./common')
|
||||
var extend = require('xtend')
|
||||
var fixtures = require('webtorrent-fixtures')
|
||||
var test = require('tape')
|
||||
|
||||
@ -12,11 +11,14 @@ var port = 6881
|
||||
function testClientStart (t, serverType) {
|
||||
t.plan(4)
|
||||
|
||||
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
|
||||
|
||||
common.createServer(t, serverType, function (server, announceUrl) {
|
||||
parsedTorrent.announce = [ announceUrl ]
|
||||
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
|
||||
var client = new Client({
|
||||
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId1,
|
||||
port: port,
|
||||
wrtc: {}
|
||||
})
|
||||
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||
client.on('error', function (err) { t.error(err) })
|
||||
@ -55,11 +57,14 @@ test('ws: client.start()', function (t) {
|
||||
function testClientStop (t, serverType) {
|
||||
t.plan(3)
|
||||
|
||||
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
|
||||
|
||||
common.createServer(t, serverType, function (server, announceUrl) {
|
||||
parsedTorrent.announce = [ announceUrl ]
|
||||
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
|
||||
var client = new Client({
|
||||
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId1,
|
||||
port: port,
|
||||
wrtc: {}
|
||||
})
|
||||
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||
client.on('error', function (err) { t.error(err) })
|
||||
@ -98,11 +103,14 @@ test('ws: client.stop()', function (t) {
|
||||
function testClientUpdate (t, serverType) {
|
||||
t.plan(4)
|
||||
|
||||
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
|
||||
|
||||
common.createServer(t, serverType, function (server, announceUrl) {
|
||||
parsedTorrent.announce = [ announceUrl ]
|
||||
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
|
||||
var client = new Client({
|
||||
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId1,
|
||||
port: port,
|
||||
wrtc: {}
|
||||
})
|
||||
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||
client.on('error', function (err) { t.error(err) })
|
||||
@ -148,11 +156,14 @@ test('ws: client.update()', function (t) {
|
||||
function testClientScrape (t, serverType) {
|
||||
t.plan(4)
|
||||
|
||||
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
|
||||
|
||||
common.createServer(t, serverType, function (server, announceUrl) {
|
||||
parsedTorrent.announce = [ announceUrl ]
|
||||
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
|
||||
var client = new Client({
|
||||
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId1,
|
||||
port: port,
|
||||
wrtc: {}
|
||||
})
|
||||
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||
client.on('error', function (err) { t.error(err) })
|
||||
@ -188,11 +199,14 @@ test('udp: client.scrape()', function (t) {
|
||||
function testClientAnnounceWithParams (t, serverType) {
|
||||
t.plan(5)
|
||||
|
||||
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
|
||||
|
||||
common.createServer(t, serverType, function (server, announceUrl) {
|
||||
parsedTorrent.announce = [ announceUrl ]
|
||||
var client = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
|
||||
var client = new Client({
|
||||
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId1,
|
||||
port: port,
|
||||
wrtc: {}
|
||||
})
|
||||
|
||||
server.on('start', function (peer, params) {
|
||||
t.equal(params.testParam, 'this is a test')
|
||||
@ -233,19 +247,19 @@ test('ws: client.announce() with params', function (t) {
|
||||
function testClientGetAnnounceOpts (t, serverType) {
|
||||
t.plan(5)
|
||||
|
||||
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
|
||||
|
||||
common.createServer(t, serverType, function (server, announceUrl) {
|
||||
parsedTorrent.announce = [ announceUrl ]
|
||||
var opts = {
|
||||
var client = new Client({
|
||||
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId1,
|
||||
port: port,
|
||||
getAnnounceOpts: function () {
|
||||
return {
|
||||
testParam: 'this is a test'
|
||||
}
|
||||
},
|
||||
wrtc: {}
|
||||
}
|
||||
var client = new Client(peerId1, port, parsedTorrent, opts)
|
||||
})
|
||||
|
||||
server.on('start', function (peer, params) {
|
||||
t.equal(params.testParam, 'this is a test')
|
||||
@ -284,11 +298,14 @@ test('ws: client `opts.getAnnounceOpts`', function (t) {
|
||||
function testClientAnnounceWithNumWant (t, serverType) {
|
||||
t.plan(4)
|
||||
|
||||
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
|
||||
|
||||
common.createServer(t, serverType, function (server, announceUrl) {
|
||||
parsedTorrent.announce = [ announceUrl ]
|
||||
var client1 = new Client(peerId1, port, parsedTorrent, { wrtc: {} })
|
||||
var client1 = new Client({
|
||||
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||
announce: [ announceUrl ],
|
||||
peerId: peerId1,
|
||||
port: port,
|
||||
wrtc: {}
|
||||
})
|
||||
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client1)
|
||||
client1.on('error', function (err) { t.error(err) })
|
||||
@ -296,7 +313,13 @@ function testClientAnnounceWithNumWant (t, serverType) {
|
||||
|
||||
client1.start()
|
||||
client1.once('update', function () {
|
||||
var client2 = new Client(peerId2, port + 1, parsedTorrent, { wrtc: {} })
|
||||
var client2 = new Client({
|
||||
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId2,
|
||||
port: port + 1,
|
||||
wrtc: {}
|
||||
})
|
||||
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client2)
|
||||
client2.on('error', function (err) { t.error(err) })
|
||||
@ -304,7 +327,13 @@ function testClientAnnounceWithNumWant (t, serverType) {
|
||||
|
||||
client2.start()
|
||||
client2.once('update', function () {
|
||||
var client3 = new Client(peerId3, port + 2, parsedTorrent, { wrtc: {} })
|
||||
var client3 = new Client({
|
||||
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId3,
|
||||
port: port + 2,
|
||||
wrtc: {}
|
||||
})
|
||||
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client3)
|
||||
client3.on('error', function (err) { t.error(err) })
|
||||
|
@ -1,6 +1,5 @@
|
||||
var Client = require('../')
|
||||
var common = require('./common')
|
||||
var extend = require('xtend')
|
||||
var fixtures = require('webtorrent-fixtures')
|
||||
var test = require('tape')
|
||||
|
||||
@ -9,21 +8,21 @@ var peerId = new Buffer('01234567890123456789')
|
||||
function testFilterOption (t, serverType) {
|
||||
t.plan(8)
|
||||
|
||||
var parsedAlice = extend(fixtures.alice.parsedTorrent)
|
||||
var parsedLeaves = extend(fixtures.leaves.parsedTorrent)
|
||||
|
||||
var opts = { serverType: serverType } // this is test-suite-only option
|
||||
opts.filter = function (infoHash, params, cb) {
|
||||
process.nextTick(function () {
|
||||
cb(infoHash !== parsedAlice.infoHash)
|
||||
cb(infoHash !== fixtures.alice.parsedTorrent.infoHash)
|
||||
})
|
||||
}
|
||||
|
||||
common.createServer(t, opts, function (server, announceUrl) {
|
||||
parsedAlice.announce = [ announceUrl ]
|
||||
parsedLeaves.announce = [ announceUrl ]
|
||||
|
||||
var client = new Client(peerId, 6881, parsedAlice, { wrtc: {} })
|
||||
var client = new Client({
|
||||
infoHash: fixtures.alice.parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId,
|
||||
port: 6881,
|
||||
wrtc: {}
|
||||
})
|
||||
|
||||
client.on('error', function (err) { t.error(err) })
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||
@ -33,7 +32,13 @@ function testFilterOption (t, serverType) {
|
||||
|
||||
client.destroy(function () {
|
||||
t.pass('client destroyed')
|
||||
client = new Client(peerId, 6881, parsedLeaves, { wrtc: {} })
|
||||
client = new Client({
|
||||
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId,
|
||||
port: 6881,
|
||||
wrtc: {}
|
||||
})
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||
|
||||
client.on('error', function (err) { t.error(err) })
|
||||
@ -78,22 +83,21 @@ test('ws: filter option blocks tracker from tracking torrent', function (t) {
|
||||
function testFilterCustomError (t, serverType) {
|
||||
t.plan(8)
|
||||
|
||||
var parsedLeaves = extend(fixtures.leaves.parsedTorrent)
|
||||
var parsedAlice = extend(fixtures.alice.parsedTorrent)
|
||||
|
||||
var opts = { serverType: serverType } // this is test-suite-only option
|
||||
opts.filter = function (infoHash, params, cb) {
|
||||
process.nextTick(function () {
|
||||
if (infoHash === parsedAlice.infoHash) cb(new Error('alice blocked'))
|
||||
if (infoHash === fixtures.alice.parsedTorrent.infoHash) cb(new Error('alice blocked'))
|
||||
else cb(true)
|
||||
})
|
||||
}
|
||||
|
||||
common.createServer(t, opts, function (server, announceUrl) {
|
||||
parsedAlice.announce = [ announceUrl ]
|
||||
parsedLeaves.announce = [ announceUrl ]
|
||||
|
||||
var client = new Client(peerId, 6881, parsedAlice, { wrtc: {} })
|
||||
var client = new Client({
|
||||
infoHash: fixtures.alice.parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId, port: 6881,
|
||||
wrtc: {}
|
||||
})
|
||||
|
||||
client.on('error', function (err) { t.error(err) })
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||
@ -103,7 +107,13 @@ function testFilterCustomError (t, serverType) {
|
||||
|
||||
client.destroy(function () {
|
||||
t.pass('client destroyed')
|
||||
client = new Client(peerId, 6881, parsedLeaves, { wrtc: {} })
|
||||
client = new Client({
|
||||
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId,
|
||||
port: 6881,
|
||||
wrtc: {}
|
||||
})
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||
|
||||
client.on('error', function (err) { t.error(err) })
|
||||
|
@ -1,8 +1,8 @@
|
||||
var bencode = require('bencode')
|
||||
var Client = require('../')
|
||||
var common = require('./common')
|
||||
var commonLib = require('../lib/common')
|
||||
var commonTest = require('./common')
|
||||
var extend = require('xtend')
|
||||
var fixtures = require('webtorrent-fixtures')
|
||||
var get = require('simple-get')
|
||||
var test = require('tape')
|
||||
@ -10,25 +10,24 @@ var test = require('tape')
|
||||
var peerId = new Buffer('01234567890123456789')
|
||||
|
||||
function testSingle (t, serverType) {
|
||||
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
|
||||
|
||||
commonTest.createServer(t, serverType, function (server, announceUrl) {
|
||||
parsedTorrent.announce = [ announceUrl ]
|
||||
var client = new Client(peerId, 6881, parsedTorrent)
|
||||
|
||||
client.on('error', function (err) {
|
||||
t.error(err)
|
||||
var client = new Client({
|
||||
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId,
|
||||
port: 6881,
|
||||
wrtc: {}
|
||||
})
|
||||
|
||||
client.on('warning', function (err) {
|
||||
t.error(err)
|
||||
})
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||
client.on('error', function (err) { t.error(err) })
|
||||
client.on('warning', function (err) { t.error(err) })
|
||||
|
||||
client.scrape()
|
||||
|
||||
client.on('scrape', function (data) {
|
||||
t.equal(data.announce, announceUrl)
|
||||
t.equal(data.infoHash, parsedTorrent.infoHash)
|
||||
t.equal(data.infoHash, fixtures.leaves.parsedTorrent.infoHash)
|
||||
t.equal(typeof data.complete, 'number')
|
||||
t.equal(typeof data.incomplete, 'number')
|
||||
t.equal(typeof data.downloaded, 'number')
|
||||
@ -48,9 +47,17 @@ test('udp: single info_hash scrape', function (t) {
|
||||
testSingle(t, 'udp')
|
||||
})
|
||||
|
||||
test('ws: single info_hash scrape', function (t) {
|
||||
testSingle(t, 'ws')
|
||||
})
|
||||
|
||||
function clientScrapeStatic (t, serverType) {
|
||||
commonTest.createServer(t, serverType, function (server, announceUrl) {
|
||||
Client.scrape(announceUrl, fixtures.leaves.parsedTorrent.infoHash, function (err, data) {
|
||||
var client = Client.scrape({
|
||||
announce: announceUrl,
|
||||
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||
wrtc: {}
|
||||
}, function (err, data) {
|
||||
t.error(err)
|
||||
t.equal(data.announce, announceUrl)
|
||||
t.equal(data.infoHash, fixtures.leaves.parsedTorrent.infoHash)
|
||||
@ -61,6 +68,7 @@ function clientScrapeStatic (t, serverType) {
|
||||
t.end()
|
||||
})
|
||||
})
|
||||
if (serverType === 'ws') common.mockWebsocketTracker(client)
|
||||
})
|
||||
}
|
||||
|
||||
@ -72,12 +80,19 @@ test('udp: scrape using Client.scrape static method', function (t) {
|
||||
clientScrapeStatic(t, 'udp')
|
||||
})
|
||||
|
||||
test('ws: scrape using Client.scrape static method', function (t) {
|
||||
clientScrapeStatic(t, 'ws')
|
||||
})
|
||||
|
||||
function clientScrapeMulti (t, serverType) {
|
||||
var infoHash1 = fixtures.leaves.parsedTorrent.infoHash
|
||||
var infoHash2 = fixtures.alice.parsedTorrent.infoHash
|
||||
|
||||
commonTest.createServer(t, serverType, function (server, announceUrl) {
|
||||
Client.scrape(announceUrl, [ infoHash1, infoHash2 ], function (err, results) {
|
||||
Client.scrape({
|
||||
infoHash: [ infoHash1, infoHash2 ],
|
||||
announce: announceUrl
|
||||
}, function (err, results) {
|
||||
t.error(err)
|
||||
|
||||
t.equal(results[infoHash1].announce, announceUrl)
|
||||
@ -147,16 +162,18 @@ test('server: multiple info_hash scrape (manual http request)', function (t) {
|
||||
test('server: all info_hash scrape (manual http request)', function (t) {
|
||||
t.plan(10)
|
||||
|
||||
var parsedTorrent = extend(fixtures.leaves.parsedTorrent)
|
||||
var binaryInfoHash = commonLib.hexToBinary(parsedTorrent.infoHash)
|
||||
var binaryInfoHash = commonLib.hexToBinary(fixtures.leaves.parsedTorrent.infoHash)
|
||||
|
||||
commonTest.createServer(t, 'http', function (server, announceUrl) {
|
||||
var scrapeUrl = announceUrl.replace('/announce', '/scrape')
|
||||
|
||||
parsedTorrent.announce = [ announceUrl ]
|
||||
|
||||
// announce a torrent to the tracker
|
||||
var client = new Client(peerId, 6881, parsedTorrent)
|
||||
var client = new Client({
|
||||
infoHash: fixtures.leaves.parsedTorrent.infoHash,
|
||||
announce: announceUrl,
|
||||
peerId: peerId,
|
||||
port: 6881
|
||||
})
|
||||
client.on('error', function (err) { t.error(err) })
|
||||
client.on('warning', function (err) { t.error(err) })
|
||||
|
||||
|
@ -11,7 +11,6 @@ wrtc.electronDaemon.once('ready', function () {
|
||||
var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
|
||||
var peerId = new Buffer('01234567890123456789')
|
||||
var peerId2 = new Buffer('12345678901234567890')
|
||||
var torrentLength = 50000
|
||||
|
||||
function serverTest (t, serverType, serverFamily) {
|
||||
t.plan(30)
|
||||
@ -27,11 +26,13 @@ function serverTest (t, serverType, serverFamily) {
|
||||
var port = server[serverType].address().port
|
||||
var announceUrl = serverType + '://' + hostname + ':' + port + '/announce'
|
||||
|
||||
var client1 = new Client(peerId, 6881, {
|
||||
var client1 = new Client({
|
||||
infoHash: infoHash,
|
||||
length: torrentLength,
|
||||
announce: [ announceUrl ]
|
||||
}, { wrtc: wrtc })
|
||||
announce: [ announceUrl ],
|
||||
peerId: peerId,
|
||||
port: 6881,
|
||||
wrtc: wrtc
|
||||
})
|
||||
|
||||
client1.start()
|
||||
|
||||
@ -83,11 +84,13 @@ function serverTest (t, serverType, serverFamily) {
|
||||
t.equal(typeof data.incomplete, 'number')
|
||||
t.equal(typeof data.downloaded, 'number')
|
||||
|
||||
var client2 = new Client(peerId2, 6882, {
|
||||
var client2 = new Client({
|
||||
infoHash: infoHash,
|
||||
length: torrentLength,
|
||||
announce: [ announceUrl ]
|
||||
}, { wrtc: wrtc })
|
||||
announce: [ announceUrl ],
|
||||
peerId: peerId2,
|
||||
port: 6882,
|
||||
wrtc: wrtc
|
||||
})
|
||||
|
||||
client2.start()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user