This commit is contained in:
Feross Aboukhadijeh 2020-10-28 18:57:47 -10:00
parent e0c0eb8127
commit c88bbb4a62
24 changed files with 220 additions and 220 deletions

View File

@ -1,9 +1,9 @@
#!/usr/bin/env node #!/usr/bin/env node
var minimist = require('minimist') const minimist = require('minimist')
var Server = require('../').Server const Server = require('../').Server
var argv = minimist(process.argv.slice(2), { const argv = minimist(process.argv.slice(2), {
alias: { alias: {
h: 'help', h: 'help',
p: 'port', p: 'port',
@ -70,13 +70,13 @@ if (argv.help) {
if (argv.silent) argv.quiet = true if (argv.silent) argv.quiet = true
var allFalsy = !argv.http && !argv.udp && !argv.ws const allFalsy = !argv.http && !argv.udp && !argv.ws
argv.http = allFalsy || argv.http argv.http = allFalsy || argv.http
argv.udp = allFalsy || argv.udp argv.udp = allFalsy || argv.udp
argv.ws = allFalsy || argv.ws argv.ws = allFalsy || argv.ws
var server = new Server({ const server = new Server({
http: argv.http, http: argv.http,
interval: argv.interval, interval: argv.interval,
stats: argv.stats, stats: argv.stats,
@ -104,7 +104,7 @@ server.on('stop', function (addr) {
if (!argv.quiet) console.log('stop: ' + addr) if (!argv.quiet) console.log('stop: ' + addr)
}) })
var hostname = { const hostname = {
http: argv['http-hostname'], http: argv['http-hostname'],
udp4: argv['udp-hostname'], udp4: argv['udp-hostname'],
udp6: argv['udp6-hostname'] udp6: argv['udp6-hostname']
@ -112,33 +112,33 @@ var hostname = {
server.listen(argv.port, hostname, function () { server.listen(argv.port, hostname, function () {
if (server.http && argv.http && !argv.quiet) { if (server.http && argv.http && !argv.quiet) {
var httpAddr = server.http.address() const httpAddr = server.http.address()
var httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost' const httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost'
var httpPort = httpAddr.port const httpPort = httpAddr.port
console.log('HTTP tracker: http://' + httpHost + ':' + httpPort + '/announce') console.log('HTTP tracker: http://' + httpHost + ':' + httpPort + '/announce')
} }
if (server.udp && !argv.quiet) { if (server.udp && !argv.quiet) {
var udpAddr = server.udp.address() const udpAddr = server.udp.address()
var udpHost = udpAddr.address const udpHost = udpAddr.address
var udpPort = udpAddr.port const udpPort = udpAddr.port
console.log('UDP tracker: udp://' + udpHost + ':' + udpPort) console.log('UDP tracker: udp://' + udpHost + ':' + udpPort)
} }
if (server.udp6 && !argv.quiet) { if (server.udp6 && !argv.quiet) {
var udp6Addr = server.udp6.address() const udp6Addr = server.udp6.address()
var udp6Host = udp6Addr.address !== '::' ? udp6Addr.address : 'localhost' const udp6Host = udp6Addr.address !== '::' ? udp6Addr.address : 'localhost'
var udp6Port = udp6Addr.port const udp6Port = udp6Addr.port
console.log('UDP6 tracker: udp://' + udp6Host + ':' + udp6Port) console.log('UDP6 tracker: udp://' + udp6Host + ':' + udp6Port)
} }
if (server.ws && !argv.quiet) { if (server.ws && !argv.quiet) {
var wsAddr = server.http.address() const wsAddr = server.http.address()
var wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost' const wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost'
var wsPort = wsAddr.port const wsPort = wsAddr.port
console.log('WebSocket tracker: ws://' + wsHost + ':' + wsPort) console.log('WebSocket tracker: ws://' + wsHost + ':' + wsPort)
} }
if (server.http && argv.stats && !argv.quiet) { if (server.http && argv.stats && !argv.quiet) {
var statsAddr = server.http.address() const statsAddr = server.http.address()
var statsHost = statsAddr.address !== '::' ? statsAddr.address : 'localhost' const statsHost = statsAddr.address !== '::' ? statsAddr.address : 'localhost'
var statsPort = statsAddr.port const statsPort = statsAddr.port
console.log('Tracker stats: http://' + statsHost + ':' + statsPort + '/stats') console.log('Tracker stats: http://' + statsHost + ':' + statsPort + '/stats')
} }
}) })

View File

@ -1,27 +1,27 @@
#!/usr/bin/env node #!/usr/bin/env node
var Server = require('../..').Server const Server = require('../..').Server
var express = require('express') const express = require('express')
var app = express() const app = express()
// https://wiki.theory.org/BitTorrentSpecification#peer_id // https://wiki.theory.org/BitTorrentSpecification#peer_id
var whitelist = { const whitelist = {
UT: true // uTorrent UT: true // uTorrent
} }
var server = new Server({ const server = new Server({
http: false, // we do our own http: false, // we do our own
udp: false, // not interested udp: false, // not interested
ws: false, // not interested ws: false, // not interested
filter: function (params) { filter: function (params) {
// black/whitelist for disallowing/allowing specific clients [default=allow all] // black/whitelist for disallowing/allowing specific clients [default=allow all]
// this example only allows the uTorrent client // this example only allows the uTorrent client
var client = params.peer_id[1] + params.peer_id[2] const client = params.peer_id[1] + params.peer_id[2]
return whitelist[client] return whitelist[client]
} }
}) })
var onHttpRequest = server.onHttpRequest.bind(server) const onHttpRequest = server.onHttpRequest.bind(server)
app.get('/announce', onHttpRequest) app.get('/announce', onHttpRequest)
app.get('/scrape', onHttpRequest) app.get('/scrape', onHttpRequest)

View File

@ -86,7 +86,7 @@ class HTTPTracker extends Tracker {
// Otherwise, wait a short time for pending requests to complete, then force // Otherwise, wait a short time for pending requests to complete, then force
// destroy them. // destroy them.
var timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT) let timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT)
// But, if all pending requests complete before the timeout fires, do cleanup // But, if all pending requests complete before the timeout fires, do cleanup
// right away. // right away.

View File

@ -46,7 +46,7 @@ class UDPTracker extends Tracker {
// Otherwise, wait a short time for pending requests to complete, then force // Otherwise, wait a short time for pending requests to complete, then force
// destroy them. // destroy them.
var timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT) let timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT)
// But, if all pending requests complete before the timeout fires, do cleanup // But, if all pending requests complete before the timeout fires, do cleanup
// right away. // right away.

View File

@ -134,7 +134,7 @@ class WebSocketTracker extends Tracker {
// Otherwise, wait a short time for potential responses to come in from the // Otherwise, wait a short time for potential responses to come in from the
// server, then force close the socket. // server, then force close the socket.
var timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT) let timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT)
// But, if a response comes from the server before the timeout fires, do cleanup // But, if a response comes from the server before the timeout fires, do cleanup
// right away. // right away.

View File

@ -3,7 +3,7 @@
* These are separate from common.js so they can be skipped when bundling for the browser. * These are separate from common.js so they can be skipped when bundling for the browser.
*/ */
var querystring = require('querystring') const querystring = require('querystring')
exports.IPV4_RE = /^[\d.]+$/ exports.IPV4_RE = /^[\d.]+$/
exports.IPV6_RE = /^[\da-fA-F:]+$/ exports.IPV6_RE = /^[\da-fA-F:]+$/
@ -38,7 +38,7 @@ exports.REQUEST_TIMEOUT = 15000
exports.DESTROY_TIMEOUT = 1000 exports.DESTROY_TIMEOUT = 1000
function toUInt32 (n) { function toUInt32 (n) {
var buf = Buffer.allocUnsafe(4) const buf = Buffer.allocUnsafe(4)
buf.writeUInt32BE(n, 0) buf.writeUInt32BE(n, 0)
return buf return buf
} }
@ -61,7 +61,7 @@ exports.querystringParse = function (q) {
* @return {string} * @return {string}
*/ */
exports.querystringStringify = function (obj) { exports.querystringStringify = function (obj) {
var ret = querystring.stringify(obj, null, null, { encodeURIComponent: escape }) let ret = querystring.stringify(obj, null, null, { encodeURIComponent: escape })
ret = ret.replace(/[@*/+]/g, function (char) { ret = ret.replace(/[@*/+]/g, function (char) {
// `escape` doesn't encode the characters @*/+ so we do it manually // `escape` doesn't encode the characters @*/+ so we do it manually
return '%' + char.charCodeAt(0).toString(16).toUpperCase() return '%' + char.charCodeAt(0).toString(16).toUpperCase()

View File

@ -19,5 +19,5 @@ exports.hexToBinary = function (str) {
return Buffer.from(str, 'hex').toString('binary') return Buffer.from(str, 'hex').toString('binary')
} }
var config = require('./common-node') const config = require('./common-node')
Object.assign(exports, config) Object.assign(exports, config)

View File

@ -1,11 +1,11 @@
module.exports = parseHttpRequest module.exports = parseHttpRequest
var common = require('../common') const common = require('../common')
function parseHttpRequest (req, opts) { function parseHttpRequest (req, opts) {
if (!opts) opts = {} if (!opts) opts = {}
var s = req.url.split('?') const s = req.url.split('?')
var params = common.querystringParse(s[1]) const params = common.querystringParse(s[1])
params.type = 'http' params.type = 'http'
if (opts.action === 'announce' || s[0] === '/announce') { if (opts.action === 'announce' || s[0] === '/announce') {

View File

@ -1,12 +1,12 @@
module.exports = parseUdpRequest module.exports = parseUdpRequest
var ipLib = require('ip') const ipLib = require('ip')
var common = require('../common') const common = require('../common')
function parseUdpRequest (msg, rinfo) { function parseUdpRequest (msg, rinfo) {
if (msg.length < 16) throw new Error('received packet is too short') if (msg.length < 16) throw new Error('received packet is too short')
var params = { const params = {
connectionId: msg.slice(0, 8), // 64-bit connectionId: msg.slice(0, 8), // 64-bit
action: msg.readUInt32BE(8), action: msg.readUInt32BE(8),
transactionId: msg.readUInt32BE(12), transactionId: msg.readUInt32BE(12),
@ -29,7 +29,7 @@ function parseUdpRequest (msg, rinfo) {
params.event = common.EVENT_IDS[msg.readUInt32BE(80)] params.event = common.EVENT_IDS[msg.readUInt32BE(80)]
if (!params.event) throw new Error('invalid event') // early return if (!params.event) throw new Error('invalid event') // early return
var ip = msg.readUInt32BE(84) // optional const ip = msg.readUInt32BE(84) // optional
params.ip = ip params.ip = ip
? ipLib.toString(ip) ? ipLib.toString(ip)
: rinfo.address : rinfo.address
@ -49,8 +49,8 @@ function parseUdpRequest (msg, rinfo) {
} else if (params.action === common.ACTIONS.SCRAPE) { // scrape message } else if (params.action === common.ACTIONS.SCRAPE) { // scrape message
if ((msg.length - 16) % 20 !== 0) throw new Error('invalid scrape message') if ((msg.length - 16) % 20 !== 0) throw new Error('invalid scrape message')
params.info_hash = [] params.info_hash = []
for (var i = 0, len = (msg.length - 16) / 20; i < len; i += 1) { for (let i = 0, len = (msg.length - 16) / 20; i < len; i += 1) {
var infoHash = msg.slice(16 + (i * 20), 36 + (i * 20)).toString('hex') // 20 bytes const infoHash = msg.slice(16 + (i * 20), 36 + (i * 20)).toString('hex') // 20 bytes
params.info_hash.push(infoHash) params.info_hash.push(infoHash)
} }
} else { } else {
@ -60,16 +60,16 @@ function parseUdpRequest (msg, rinfo) {
return params return params
} }
var TWO_PWR_32 = (1 << 16) * 2 const TWO_PWR_32 = (1 << 16) * 2
/** /**
* Return the closest floating-point representation to the buffer value. Precision will be * Return the closest floating-point representation to the buffer value. Precision will be
* lost for big numbers. * lost for big numbers.
*/ */
function fromUInt64 (buf) { function fromUInt64 (buf) {
var high = buf.readUInt32BE(0) | 0 // force const high = buf.readUInt32BE(0) | 0 // force
var low = buf.readUInt32BE(4) | 0 const low = buf.readUInt32BE(4) | 0
var lowUnsigned = (low >= 0) ? low : TWO_PWR_32 + low const lowUnsigned = (low >= 0) ? low : TWO_PWR_32 + low
return (high * TWO_PWR_32) + lowUnsigned return (high * TWO_PWR_32) + lowUnsigned
} }

View File

@ -1,6 +1,6 @@
module.exports = parseWebSocketRequest module.exports = parseWebSocketRequest
var common = require('../common') const common = require('../common')
function parseWebSocketRequest (socket, opts, params) { function parseWebSocketRequest (socket, opts, params) {
if (!opts) opts = {} if (!opts) opts = {}

View File

@ -1,14 +1,14 @@
module.exports = Swarm module.exports = Swarm
var arrayRemove = require('unordered-array-remove') const arrayRemove = require('unordered-array-remove')
var debug = require('debug')('bittorrent-tracker:swarm') const debug = require('debug')('bittorrent-tracker:swarm')
var LRU = require('lru') const LRU = require('lru')
var randomIterate = require('random-iterate') const randomIterate = require('random-iterate')
// Regard this as the default implementation of an interface that you // Regard this as the default implementation of an interface that you
// need to support when overriding Server.createSwarm() and Server.getSwarm() // need to support when overriding Server.createSwarm() and Server.getSwarm()
function Swarm (infoHash, server) { function Swarm (infoHash, server) {
var self = this const self = this
self.infoHash = infoHash self.infoHash = infoHash
self.complete = 0 self.complete = 0
self.incomplete = 0 self.incomplete = 0
@ -21,8 +21,8 @@ function Swarm (infoHash, server) {
// When a peer is evicted from the LRU store, send a synthetic 'stopped' event // When a peer is evicted from the LRU store, send a synthetic 'stopped' event
// so the stats get updated correctly. // so the stats get updated correctly.
self.peers.on('evict', function (data) { self.peers.on('evict', function (data) {
var peer = data.value const peer = data.value
var params = { const params = {
type: peer.type, type: peer.type,
event: 'stopped', event: 'stopped',
numwant: 0, numwant: 0,
@ -34,10 +34,10 @@ function Swarm (infoHash, server) {
} }
Swarm.prototype.announce = function (params, cb) { Swarm.prototype.announce = function (params, cb) {
var self = this const self = this
var id = params.type === 'ws' ? params.peer_id : params.addr const id = params.type === 'ws' ? params.peer_id : params.addr
// Mark the source peer as recently used in cache // Mark the source peer as recently used in cache
var peer = self.peers.get(id) const peer = self.peers.get(id)
if (params.event === 'started') { if (params.event === 'started') {
self._onAnnounceStarted(params, peer, id) self._onAnnounceStarted(params, peer, id)
@ -95,7 +95,7 @@ Swarm.prototype._onAnnounceStopped = function (params, peer, id) {
// If it's a websocket, remove this swarm's infohash from the list of active // If it's a websocket, remove this swarm's infohash from the list of active
// swarms that this peer is participating in. // swarms that this peer is participating in.
if (peer.socket && !peer.socket.destroyed) { if (peer.socket && !peer.socket.destroyed) {
var index = peer.socket.infoHashes.indexOf(this.infoHash) const index = peer.socket.infoHashes.indexOf(this.infoHash)
arrayRemove(peer.socket.infoHashes, index) arrayRemove(peer.socket.infoHashes, index)
} }
@ -133,12 +133,12 @@ Swarm.prototype._onAnnounceUpdate = function (params, peer, id) {
} }
Swarm.prototype._getPeers = function (numwant, ownPeerId, isWebRTC) { Swarm.prototype._getPeers = function (numwant, ownPeerId, isWebRTC) {
var peers = [] const peers = []
var ite = randomIterate(this.peers.keys) const ite = randomIterate(this.peers.keys)
var peerId let peerId
while ((peerId = ite()) && peers.length < numwant) { while ((peerId = ite()) && peers.length < numwant) {
// Don't mark the peer as most recently used on announce // Don't mark the peer as most recently used on announce
var peer = this.peers.peek(peerId) const peer = this.peers.peek(peerId)
if (!peer) continue if (!peer) continue
if (isWebRTC && peer.peerId === ownPeerId) continue // don't send peer to itself if (isWebRTC && peer.peerId === ownPeerId) continue // don't send peer to itself
if ((isWebRTC && peer.type !== 'ws') || (!isWebRTC && peer.type === 'ws')) continue // send proper peer type if ((isWebRTC && peer.type !== 'ws') || (!isWebRTC && peer.type === 'ws')) continue // send proper peer type

View File

@ -1,15 +1,15 @@
var Client = require('../') const Client = require('../')
var common = require('./common') const common = require('./common')
var fixtures = require('webtorrent-fixtures') const fixtures = require('webtorrent-fixtures')
var test = require('tape') const test = require('tape')
var peerId = Buffer.from('01234567890123456789') const peerId = Buffer.from('01234567890123456789')
function testLargeTorrent (t, serverType) { function testLargeTorrent (t, serverType) {
t.plan(9) t.plan(9)
common.createServer(t, serverType, function (server, announceUrl) { common.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({ const client = new Client({
infoHash: fixtures.sintel.parsedTorrent.infoHash, infoHash: fixtures.sintel.parsedTorrent.infoHash,
peerId, peerId,
port: 6881, port: 6881,

View File

@ -1,18 +1,18 @@
var Client = require('../') const Client = require('../')
var common = require('./common') const common = require('./common')
var fixtures = require('webtorrent-fixtures') const fixtures = require('webtorrent-fixtures')
var magnet = require('magnet-uri') const magnet = require('magnet-uri')
var test = require('tape') const test = require('tape')
var peerId = Buffer.from('01234567890123456789') const peerId = Buffer.from('01234567890123456789')
function testMagnet (t, serverType) { function testMagnet (t, serverType) {
t.plan(9) t.plan(9)
var parsedTorrent = magnet(fixtures.leaves.magnetURI) const parsedTorrent = magnet(fixtures.leaves.magnetURI)
common.createServer(t, serverType, function (server, announceUrl) { common.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({ const client = new Client({
infoHash: parsedTorrent.infoHash, infoHash: parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId, peerId,

View File

@ -1,16 +1,16 @@
var Client = require('../') const Client = require('../')
var common = require('./common') const common = require('./common')
var fixtures = require('webtorrent-fixtures') const fixtures = require('webtorrent-fixtures')
var test = require('tape') const test = require('tape')
var peerId = Buffer.from('01234567890123456789') const peerId = Buffer.from('01234567890123456789')
var port = 6681 const port = 6681
test('ensure client.destroy() callback is called with re-used websockets in socketPool', function (t) { test('ensure client.destroy() callback is called with re-used websockets in socketPool', function (t) {
t.plan(4) t.plan(4)
common.createServer(t, 'ws', function (server, announceUrl) { common.createServer(t, 'ws', function (server, announceUrl) {
var client1 = new Client({ const client1 = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId, peerId,
@ -27,7 +27,7 @@ test('ensure client.destroy() callback is called with re-used websockets in sock
client1.once('update', function () { client1.once('update', function () {
t.pass('got client1 update') t.pass('got client1 update')
// second ws client using same announce url will re-use the same websocket // second ws client using same announce url will re-use the same websocket
var client2 = new Client({ const client2 = new Client({
infoHash: fixtures.alice.parsedTorrent.infoHash, // different info hash infoHash: fixtures.alice.parsedTorrent.infoHash, // different info hash
announce: announceUrl, announce: announceUrl,
peerId, peerId,

View File

@ -1,18 +1,18 @@
var Client = require('../') const Client = require('../')
var common = require('./common') const common = require('./common')
var fixtures = require('webtorrent-fixtures') const fixtures = require('webtorrent-fixtures')
var test = require('tape') const test = require('tape')
var peerId1 = Buffer.from('01234567890123456789') const peerId1 = Buffer.from('01234567890123456789')
var peerId2 = Buffer.from('12345678901234567890') const peerId2 = Buffer.from('12345678901234567890')
var peerId3 = Buffer.from('23456789012345678901') const peerId3 = Buffer.from('23456789012345678901')
var port = 6881 const port = 6881
function testClientStart (t, serverType) { function testClientStart (t, serverType) {
t.plan(4) t.plan(4)
common.createServer(t, serverType, function (server, announceUrl) { common.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId: peerId1, peerId: peerId1,
@ -58,7 +58,7 @@ function testClientStop (t, serverType) {
t.plan(4) t.plan(4)
common.createServer(t, serverType, function (server, announceUrl) { common.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId: peerId1, peerId: peerId1,
@ -106,7 +106,7 @@ function testClientStopDestroy (t, serverType) {
t.plan(2) t.plan(2)
common.createServer(t, serverType, function (server, announceUrl) { common.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId: peerId1, peerId: peerId1,
@ -160,7 +160,7 @@ function testClientUpdate (t, serverType) {
t.plan(4) t.plan(4)
common.createServer(t, serverType, function (server, announceUrl) { common.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId: peerId1, peerId: peerId1,
@ -213,7 +213,7 @@ function testClientScrape (t, serverType) {
t.plan(4) t.plan(4)
common.createServer(t, serverType, function (server, announceUrl) { common.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId: peerId1, peerId: peerId1,
@ -255,7 +255,7 @@ function testClientAnnounceWithParams (t, serverType) {
t.plan(5) t.plan(5)
common.createServer(t, serverType, function (server, announceUrl) { common.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId: peerId1, peerId: peerId1,
@ -303,7 +303,7 @@ function testClientGetAnnounceOpts (t, serverType) {
t.plan(5) t.plan(5)
common.createServer(t, serverType, function (server, announceUrl) { common.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId: peerId1, peerId: peerId1,
@ -354,7 +354,7 @@ function testClientAnnounceWithNumWant (t, serverType) {
t.plan(4) t.plan(4)
common.createServer(t, serverType, function (server, announceUrl) { common.createServer(t, serverType, function (server, announceUrl) {
var client1 = new Client({ const client1 = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: [announceUrl], announce: [announceUrl],
peerId: peerId1, peerId: peerId1,
@ -368,7 +368,7 @@ function testClientAnnounceWithNumWant (t, serverType) {
client1.start() client1.start()
client1.once('update', function () { client1.once('update', function () {
var client2 = new Client({ const client2 = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId: peerId2, peerId: peerId2,
@ -382,7 +382,7 @@ function testClientAnnounceWithNumWant (t, serverType) {
client2.start() client2.start()
client2.once('update', function () { client2.once('update', function () {
var client3 = new Client({ const client3 = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId: peerId3, peerId: peerId3,
@ -398,7 +398,7 @@ function testClientAnnounceWithNumWant (t, serverType) {
client3.on('peer', function () { client3.on('peer', function () {
t.pass('got one peer (this should only fire once)') t.pass('got one peer (this should only fire once)')
var num = 3 let num = 3
function tryCloseServer () { function tryCloseServer () {
num -= 1 num -= 1
if (num === 0) server.close() if (num === 0) server.close()
@ -445,7 +445,7 @@ test('http: userAgent', function (t) {
t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1) t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1)
}) })
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId: peerId1, peerId: peerId1,
@ -472,7 +472,7 @@ function testSupportedTracker (t, serverType) {
t.plan(1) t.plan(1)
common.createServer(t, serverType, function (server, announceUrl) { common.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId: peerId1, peerId: peerId1,
@ -510,7 +510,7 @@ test('ws: valid tracker port', function (t) {
function testUnsupportedTracker (t, announceUrl) { function testUnsupportedTracker (t, announceUrl) {
t.plan(1) t.plan(1)
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId: peerId1, peerId: peerId1,

View File

@ -1,4 +1,4 @@
var Server = require('../').Server const Server = require('../').Server
exports.createServer = function (t, opts, cb) { exports.createServer = function (t, opts, cb) {
if (typeof opts === 'string') opts = { serverType: opts } if (typeof opts === 'string') opts = { serverType: opts }
@ -7,14 +7,14 @@ exports.createServer = function (t, opts, cb) {
opts.udp = (opts.serverType === 'udp') opts.udp = (opts.serverType === 'udp')
opts.ws = (opts.serverType === 'ws') opts.ws = (opts.serverType === 'ws')
var server = new Server(opts) const server = new Server(opts)
server.on('error', function (err) { t.error(err) }) server.on('error', function (err) { t.error(err) })
server.on('warning', function (err) { t.error(err) }) server.on('warning', function (err) { t.error(err) })
server.listen(0, function () { server.listen(0, function () {
var port = server[opts.serverType].address().port const port = server[opts.serverType].address().port
var announceUrl let announceUrl
if (opts.serverType === 'http') { if (opts.serverType === 'http') {
announceUrl = 'http://127.0.0.1:' + port + '/announce' announceUrl = 'http://127.0.0.1:' + port + '/announce'
} else if (opts.serverType === 'udp') { } else if (opts.serverType === 'udp') {
@ -29,8 +29,8 @@ exports.createServer = function (t, opts, cb) {
exports.mockWebsocketTracker = function (client) { exports.mockWebsocketTracker = function (client) {
client._trackers[0]._generateOffers = function (numwant, cb) { client._trackers[0]._generateOffers = function (numwant, cb) {
var offers = [] const offers = []
for (var i = 0; i < numwant; i++) { for (let i = 0; i < numwant; i++) {
offers.push({ fake_offer: 'fake_offer_' + i }) offers.push({ fake_offer: 'fake_offer_' + i })
} }
process.nextTick(function () { process.nextTick(function () {

View File

@ -1,16 +1,16 @@
var Client = require('../') const Client = require('../')
var common = require('./common') const common = require('./common')
var fixtures = require('webtorrent-fixtures') const fixtures = require('webtorrent-fixtures')
var test = require('tape') const test = require('tape')
var peerId = Buffer.from('01234567890123456789') const peerId = Buffer.from('01234567890123456789')
var port = 6881 const port = 6881
function testNoEventsAfterDestroy (t, serverType) { function testNoEventsAfterDestroy (t, serverType) {
t.plan(1) t.plan(1)
common.createServer(t, serverType, function (server, announceUrl) { common.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId, peerId,

View File

@ -1,21 +1,21 @@
var Client = require('../') const Client = require('../')
var common = require('./common') const common = require('./common')
var test = require('tape') const test = require('tape')
var wrtc = require('wrtc') const wrtc = require('wrtc')
var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705' const infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
var peerId = Buffer.from('01234567890123456789') const peerId = Buffer.from('01234567890123456789')
var peerId2 = Buffer.from('12345678901234567890') const peerId2 = Buffer.from('12345678901234567890')
var peerId3 = Buffer.from('23456789012345678901') const peerId3 = Buffer.from('23456789012345678901')
function serverTest (t, serverType, serverFamily) { function serverTest (t, serverType, serverFamily) {
t.plan(10) t.plan(10)
var hostname = serverFamily === 'inet6' const hostname = serverFamily === 'inet6'
? '[::1]' ? '[::1]'
: '127.0.0.1' : '127.0.0.1'
var opts = { const opts = {
serverType, serverType,
peersCacheLength: 2 // LRU cache can only contain a max of 2 peers peersCacheLength: 2 // LRU cache can only contain a max of 2 peers
} }
@ -23,10 +23,10 @@ function serverTest (t, serverType, serverFamily) {
common.createServer(t, opts, function (server) { common.createServer(t, opts, function (server) {
// Not using announceUrl param from `common.createServer()` since we // Not using announceUrl param from `common.createServer()` since we
// want to control IPv4 vs IPv6. // want to control IPv4 vs IPv6.
var port = server[serverType].address().port const port = server[serverType].address().port
var announceUrl = serverType + '://' + hostname + ':' + port + '/announce' const announceUrl = serverType + '://' + hostname + ':' + port + '/announce'
var client1 = new Client({ const client1 = new Client({
infoHash, infoHash,
announce: [announceUrl], announce: [announceUrl],
peerId, peerId,
@ -38,7 +38,7 @@ function serverTest (t, serverType, serverFamily) {
client1.start() client1.start()
client1.once('update', function (data) { client1.once('update', function (data) {
var client2 = new Client({ const client2 = new Client({
infoHash, infoHash,
announce: [announceUrl], announce: [announceUrl],
peerId: peerId2, peerId: peerId2,
@ -56,14 +56,14 @@ function serverTest (t, serverType, serverFamily) {
t.equal(swarm.complete + swarm.incomplete, 2) t.equal(swarm.complete + swarm.incomplete, 2)
// Ensure that first peer is evicted when a third one is added // Ensure that first peer is evicted when a third one is added
var evicted = false let evicted = false
swarm.peers.once('evict', function (evictedPeer) { swarm.peers.once('evict', function (evictedPeer) {
t.equal(evictedPeer.value.peerId, peerId.toString('hex')) t.equal(evictedPeer.value.peerId, peerId.toString('hex'))
t.equal(swarm.complete + swarm.incomplete, 2) t.equal(swarm.complete + swarm.incomplete, 2)
evicted = true evicted = true
}) })
var client3 = new Client({ const client3 = new Client({
infoHash, infoHash,
announce: [announceUrl], announce: [announceUrl],
peerId: peerId3, peerId: peerId3,

View File

@ -1,14 +1,14 @@
var Client = require('../') const Client = require('../')
var common = require('./common') const common = require('./common')
var fixtures = require('webtorrent-fixtures') const fixtures = require('webtorrent-fixtures')
var test = require('tape') const test = require('tape')
var peerId = Buffer.from('01234567890123456789') const peerId = Buffer.from('01234567890123456789')
function testFilterOption (t, serverType) { function testFilterOption (t, serverType) {
t.plan(8) t.plan(8)
var opts = { serverType } // this is test-suite-only option const opts = { 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 === fixtures.alice.parsedTorrent.infoHash) { if (infoHash === fixtures.alice.parsedTorrent.infoHash) {
@ -20,7 +20,7 @@ function testFilterOption (t, serverType) {
} }
common.createServer(t, opts, function (server, announceUrl) { common.createServer(t, opts, function (server, announceUrl) {
var client1 = new Client({ const client1 = new Client({
infoHash: fixtures.alice.parsedTorrent.infoHash, infoHash: fixtures.alice.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId, peerId,
@ -37,7 +37,7 @@ function testFilterOption (t, serverType) {
client1.destroy(function () { client1.destroy(function () {
t.pass('client1 destroyed') t.pass('client1 destroyed')
var client2 = new Client({ const client2 = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId, peerId,
@ -88,7 +88,7 @@ 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 opts = { serverType } // this is test-suite-only option const opts = { 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 === fixtures.alice.parsedTorrent.infoHash) { if (infoHash === fixtures.alice.parsedTorrent.infoHash) {
@ -100,7 +100,7 @@ function testFilterCustomError (t, serverType) {
} }
common.createServer(t, opts, function (server, announceUrl) { common.createServer(t, opts, function (server, announceUrl) {
var client1 = new Client({ const client1 = new Client({
infoHash: fixtures.alice.parsedTorrent.infoHash, infoHash: fixtures.alice.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId, peerId,
@ -116,7 +116,7 @@ function testFilterCustomError (t, serverType) {
client1.destroy(function () { client1.destroy(function () {
t.pass('client1 destroyed') t.pass('client1 destroyed')
var client2 = new Client({ const client2 = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId, peerId,

View File

@ -1,12 +1,12 @@
var common = require('../lib/common') const common = require('../lib/common')
var test = require('tape') const test = require('tape')
// https://github.com/webtorrent/webtorrent/issues/196 // https://github.com/webtorrent/webtorrent/issues/196
test('encode special chars +* in http tracker urls', function (t) { test('encode special chars +* in http tracker urls', function (t) {
var q = Object.create(null) const q = Object.create(null)
q.info_hash = Buffer.from('a2a15537542b22925ad10486bf7a8b2a9c42f0d1', 'hex').toString('binary') q.info_hash = Buffer.from('a2a15537542b22925ad10486bf7a8b2a9c42f0d1', 'hex').toString('binary')
var encoded = 'info_hash=%A2%A1U7T%2B%22%92Z%D1%04%86%BFz%8B%2A%9CB%F0%D1' const encoded = 'info_hash=%A2%A1U7T%2B%22%92Z%D1%04%86%BFz%8B%2A%9CB%F0%D1'
t.equal(common.querystringStringify(q), encoded) t.equal(common.querystringStringify(q), encoded)
// sanity check that encode-decode matches up // sanity check that encode-decode matches up

View File

@ -1,15 +1,15 @@
var Client = require('../') const Client = require('../')
var common = require('./common') const common = require('./common')
var fixtures = require('webtorrent-fixtures') const fixtures = require('webtorrent-fixtures')
var test = require('tape') const test = require('tape')
var Server = require('../server') const Server = require('../server')
var peerId = Buffer.from('01234567890123456789') const peerId = Buffer.from('01234567890123456789')
function testRequestHandler (t, serverType) { function testRequestHandler (t, serverType) {
t.plan(5) t.plan(5)
var opts = { serverType } // this is test-suite-only option const opts = { serverType } // this is test-suite-only option
class Swarm extends Server.Swarm { class Swarm extends Server.Swarm {
announce (params, cb) { announce (params, cb) {
@ -23,14 +23,14 @@ function testRequestHandler (t, serverType) {
} }
// Use a custom Swarm implementation for this test only // Use a custom Swarm implementation for this test only
var OldSwarm = Server.Swarm const OldSwarm = Server.Swarm
Server.Swarm = Swarm Server.Swarm = Swarm
t.on('end', function () { t.on('end', function () {
Server.Swarm = OldSwarm Server.Swarm = OldSwarm
}) })
common.createServer(t, opts, function (server, announceUrl) { common.createServer(t, opts, function (server, announceUrl) {
var client1 = new Client({ const client1 = new Client({
infoHash: fixtures.alice.parsedTorrent.infoHash, infoHash: fixtures.alice.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId, peerId,

View File

@ -1,17 +1,17 @@
var bencode = require('bencode') const bencode = require('bencode')
var Client = require('../') const Client = require('../')
var common = require('./common') const common = require('./common')
var commonLib = require('../lib/common') const commonLib = require('../lib/common')
var commonTest = require('./common') const commonTest = require('./common')
var fixtures = require('webtorrent-fixtures') const fixtures = require('webtorrent-fixtures')
var get = require('simple-get') const get = require('simple-get')
var test = require('tape') const test = require('tape')
var peerId = Buffer.from('01234567890123456789') const peerId = Buffer.from('01234567890123456789')
function testSingle (t, serverType) { function testSingle (t, serverType) {
commonTest.createServer(t, serverType, function (server, announceUrl) { commonTest.createServer(t, serverType, function (server, announceUrl) {
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId, peerId,
@ -53,7 +53,7 @@ test('ws: single info_hash scrape', function (t) {
function clientScrapeStatic (t, serverType) { function clientScrapeStatic (t, serverType) {
commonTest.createServer(t, serverType, function (server, announceUrl) { commonTest.createServer(t, serverType, function (server, announceUrl) {
var client = Client.scrape({ const client = Client.scrape({
announce: announceUrl, announce: announceUrl,
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
wrtc: {} wrtc: {}
@ -86,10 +86,10 @@ test('ws: scrape using Client.scrape static method', function (t) {
// Ensure the callback function gets called when an invalid url is passed // Ensure the callback function gets called when an invalid url is passed
function clientScrapeStaticInvalid (t, serverType) { function clientScrapeStaticInvalid (t, serverType) {
var announceUrl = serverType + '://invalid.lol' let announceUrl = serverType + '://invalid.lol'
if (serverType === 'http') announceUrl += '/announce' if (serverType === 'http') announceUrl += '/announce'
var client = Client.scrape({ const client = Client.scrape({
announce: announceUrl, announce: announceUrl,
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
wrtc: {} wrtc: {}
@ -113,8 +113,8 @@ test('ws: scrape using Client.scrape static method (invalid url)', function (t)
}) })
function clientScrapeMulti (t, serverType) { function clientScrapeMulti (t, serverType) {
var infoHash1 = fixtures.leaves.parsedTorrent.infoHash const infoHash1 = fixtures.leaves.parsedTorrent.infoHash
var infoHash2 = fixtures.alice.parsedTorrent.infoHash const infoHash2 = fixtures.alice.parsedTorrent.infoHash
commonTest.createServer(t, serverType, function (server, announceUrl) { commonTest.createServer(t, serverType, function (server, announceUrl) {
Client.scrape({ Client.scrape({
@ -153,13 +153,13 @@ test('udp: MULTI scrape using Client.scrape static method', function (t) {
test('server: multiple info_hash scrape (manual http request)', function (t) { test('server: multiple info_hash scrape (manual http request)', function (t) {
t.plan(13) t.plan(13)
var binaryInfoHash1 = commonLib.hexToBinary(fixtures.leaves.parsedTorrent.infoHash) const binaryInfoHash1 = commonLib.hexToBinary(fixtures.leaves.parsedTorrent.infoHash)
var binaryInfoHash2 = commonLib.hexToBinary(fixtures.alice.parsedTorrent.infoHash) const binaryInfoHash2 = commonLib.hexToBinary(fixtures.alice.parsedTorrent.infoHash)
commonTest.createServer(t, 'http', function (server, announceUrl) { commonTest.createServer(t, 'http', function (server, announceUrl) {
var scrapeUrl = announceUrl.replace('/announce', '/scrape') const scrapeUrl = announceUrl.replace('/announce', '/scrape')
var url = scrapeUrl + '?' + commonLib.querystringStringify({ const url = scrapeUrl + '?' + commonLib.querystringStringify({
info_hash: [binaryInfoHash1, binaryInfoHash2] info_hash: [binaryInfoHash1, binaryInfoHash2]
}) })
@ -190,13 +190,13 @@ 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 binaryInfoHash = commonLib.hexToBinary(fixtures.leaves.parsedTorrent.infoHash) const binaryInfoHash = commonLib.hexToBinary(fixtures.leaves.parsedTorrent.infoHash)
commonTest.createServer(t, 'http', function (server, announceUrl) { commonTest.createServer(t, 'http', function (server, announceUrl) {
var scrapeUrl = announceUrl.replace('/announce', '/scrape') const scrapeUrl = announceUrl.replace('/announce', '/scrape')
// announce a torrent to the tracker // announce a torrent to the tracker
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId, peerId,

View File

@ -1,34 +1,34 @@
var Client = require('../') const Client = require('../')
var common = require('./common') const common = require('./common')
var test = require('tape') const test = require('tape')
var wrtc = require('wrtc') const wrtc = require('wrtc')
var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705' const infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705'
var peerId = Buffer.from('01234567890123456789') const peerId = Buffer.from('01234567890123456789')
var peerId2 = Buffer.from('12345678901234567890') const peerId2 = Buffer.from('12345678901234567890')
var peerId3 = Buffer.from('23456789012345678901') const peerId3 = Buffer.from('23456789012345678901')
function serverTest (t, serverType, serverFamily) { function serverTest (t, serverType, serverFamily) {
t.plan(40) t.plan(40)
var hostname = serverFamily === 'inet6' const hostname = serverFamily === 'inet6'
? '[::1]' ? '[::1]'
: '127.0.0.1' : '127.0.0.1'
var clientIp = serverFamily === 'inet6' const clientIp = serverFamily === 'inet6'
? '::1' ? '::1'
: '127.0.0.1' : '127.0.0.1'
var opts = { const opts = {
serverType serverType
} }
common.createServer(t, opts, function (server) { common.createServer(t, opts, function (server) {
// Not using announceUrl param from `common.createServer()` since we // Not using announceUrl param from `common.createServer()` since we
// want to control IPv4 vs IPv6. // want to control IPv4 vs IPv6.
var port = server[serverType].address().port const port = server[serverType].address().port
var announceUrl = serverType + '://' + hostname + ':' + port + '/announce' const announceUrl = serverType + '://' + hostname + ':' + port + '/announce'
var client1 = new Client({ const client1 = new Client({
infoHash, infoHash,
announce: [announceUrl], announce: [announceUrl],
peerId, peerId,
@ -56,11 +56,11 @@ function serverTest (t, serverType, serverFamily) {
t.equal(swarm.incomplete, 1) t.equal(swarm.incomplete, 1)
t.equal(swarm.peers.length, 1) t.equal(swarm.peers.length, 1)
var id = serverType === 'ws' const id = serverType === 'ws'
? peerId.toString('hex') ? peerId.toString('hex')
: hostname + ':6881' : hostname + ':6881'
var peer = swarm.peers.peek(id) const peer = swarm.peers.peek(id)
t.equal(peer.type, serverType) t.equal(peer.type, serverType)
t.equal(peer.ip, clientIp) t.equal(peer.ip, clientIp)
t.equal(peer.peerId, peerId.toString('hex')) t.equal(peer.peerId, peerId.toString('hex'))
@ -88,7 +88,7 @@ function serverTest (t, serverType, serverFamily) {
t.equal(data.incomplete, 0) t.equal(data.incomplete, 0)
t.equal(typeof data.downloaded, 'number') t.equal(typeof data.downloaded, 'number')
var client2 = new Client({ const client2 = new Client({
infoHash, infoHash,
announce: [announceUrl], announce: [announceUrl],
peerId: peerId2, peerId: peerId2,
@ -108,7 +108,7 @@ function serverTest (t, serverType, serverFamily) {
t.equal(data.complete, 1) t.equal(data.complete, 1)
t.equal(data.incomplete, 1) t.equal(data.incomplete, 1)
var client3 = new Client({ const client3 = new Client({
infoHash, infoHash,
announce: [announceUrl], announce: [announceUrl],
peerId: peerId3, peerId: peerId3,

View File

@ -1,24 +1,24 @@
var Client = require('../') const Client = require('../')
var commonTest = require('./common') const commonTest = require('./common')
var fixtures = require('webtorrent-fixtures') const fixtures = require('webtorrent-fixtures')
var get = require('simple-get') const get = require('simple-get')
var test = require('tape') const test = require('tape')
var peerId = Buffer.from('-WW0091-4ea5886ce160') const peerId = Buffer.from('-WW0091-4ea5886ce160')
var unknownPeerId = Buffer.from('01234567890123456789') const unknownPeerId = Buffer.from('01234567890123456789')
function parseHtml (html) { function parseHtml (html) {
var extractValue = /[^v^h](\d+)/ const extractValue = /[^v^h](\d+)/
var array = html.replace('torrents', '\n').split('\n').filter(function (line) { const array = html.replace('torrents', '\n').split('\n').filter(function (line) {
return line && line.trim().length > 0 return line && line.trim().length > 0
}).map(function (line) { }).map(function (line) {
var a = extractValue.exec(line) const a = extractValue.exec(line)
if (a) { if (a) {
return parseInt(a[1]) return parseInt(a[1])
} }
return null return null
}) })
var i = 0 let i = 0
return { return {
torrents: array[i++], torrents: array[i++],
activeTorrents: array[i++], activeTorrents: array[i++],
@ -35,12 +35,12 @@ test('server: get empty stats', function (t) {
t.plan(11) t.plan(11)
commonTest.createServer(t, 'http', function (server, announceUrl) { commonTest.createServer(t, 'http', function (server, announceUrl) {
var url = announceUrl.replace('/announce', '/stats') const url = announceUrl.replace('/announce', '/stats')
get.concat(url, function (err, res, data) { get.concat(url, function (err, res, data) {
t.error(err) t.error(err)
var stats = parseHtml(data.toString()) const stats = parseHtml(data.toString())
t.equal(res.statusCode, 200) t.equal(res.statusCode, 200)
t.equal(stats.torrents, 0) t.equal(stats.torrents, 0)
t.equal(stats.activeTorrents, 0) t.equal(stats.activeTorrents, 0)
@ -60,7 +60,7 @@ test('server: get empty stats with json header', function (t) {
t.plan(11) t.plan(11)
commonTest.createServer(t, 'http', function (server, announceUrl) { commonTest.createServer(t, 'http', function (server, announceUrl) {
var opts = { const opts = {
url: announceUrl.replace('/announce', '/stats'), url: announceUrl.replace('/announce', '/stats'),
headers: { headers: {
accept: 'application/json' accept: 'application/json'
@ -90,7 +90,7 @@ test('server: get empty stats on stats.json', function (t) {
t.plan(11) t.plan(11)
commonTest.createServer(t, 'http', function (server, announceUrl) { commonTest.createServer(t, 'http', function (server, announceUrl) {
var opts = { const opts = {
url: announceUrl.replace('/announce', '/stats.json'), url: announceUrl.replace('/announce', '/stats.json'),
json: true json: true
} }
@ -118,7 +118,7 @@ test('server: get leecher stats.json', function (t) {
commonTest.createServer(t, 'http', function (server, announceUrl) { commonTest.createServer(t, 'http', function (server, announceUrl) {
// announce a torrent to the tracker // announce a torrent to the tracker
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId, peerId,
@ -130,7 +130,7 @@ test('server: get leecher stats.json', function (t) {
client.start() client.start()
server.once('start', function () { server.once('start', function () {
var opts = { const opts = {
url: announceUrl.replace('/announce', '/stats.json'), url: announceUrl.replace('/announce', '/stats.json'),
json: true json: true
} }
@ -159,7 +159,7 @@ test('server: get leecher stats.json (unknown peerId)', function (t) {
commonTest.createServer(t, 'http', function (server, announceUrl) { commonTest.createServer(t, 'http', function (server, announceUrl) {
// announce a torrent to the tracker // announce a torrent to the tracker
var client = new Client({ const client = new Client({
infoHash: fixtures.leaves.parsedTorrent.infoHash, infoHash: fixtures.leaves.parsedTorrent.infoHash,
announce: announceUrl, announce: announceUrl,
peerId: unknownPeerId, peerId: unknownPeerId,
@ -171,7 +171,7 @@ test('server: get leecher stats.json (unknown peerId)', function (t) {
client.start() client.start()
server.once('start', function () { server.once('start', function () {
var opts = { const opts = {
url: announceUrl.replace('/announce', '/stats.json'), url: announceUrl.replace('/announce', '/stats.json'),
json: true json: true
} }