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