From c88bbb4a62ea1e2bd039b8696113811a60fe7144 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Wed, 28 Oct 2020 18:57:47 -1000 Subject: [PATCH] standard --- bin/cmd.js | 42 +++++++++++++++--------------- examples/express-embed/server.js | 14 +++++----- lib/client/http-tracker.js | 2 +- lib/client/udp-tracker.js | 2 +- lib/client/websocket-tracker.js | 2 +- lib/common-node.js | 6 ++--- lib/common.js | 2 +- lib/server/parse-http.js | 6 ++--- lib/server/parse-udp.js | 20 +++++++-------- lib/server/parse-websocket.js | 2 +- lib/server/swarm.js | 30 +++++++++++----------- test/client-large-torrent.js | 12 ++++----- test/client-magnet.js | 16 ++++++------ test/client-ws-socket-pool.js | 16 ++++++------ test/client.js | 44 ++++++++++++++++---------------- test/common.js | 12 ++++----- test/destroy.js | 14 +++++----- test/evict.js | 32 +++++++++++------------ test/filter.js | 22 ++++++++-------- test/querystring.js | 8 +++--- test/request-handler.js | 18 ++++++------- test/scrape.js | 44 ++++++++++++++++---------------- test/server.js | 36 +++++++++++++------------- test/stats.js | 38 +++++++++++++-------------- 24 files changed, 220 insertions(+), 220 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index cd3673c..089c649 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -1,9 +1,9 @@ #!/usr/bin/env node -var minimist = require('minimist') -var Server = require('../').Server +const minimist = require('minimist') +const Server = require('../').Server -var argv = minimist(process.argv.slice(2), { +const argv = minimist(process.argv.slice(2), { alias: { h: 'help', p: 'port', @@ -70,13 +70,13 @@ if (argv.help) { 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.udp = allFalsy || argv.udp argv.ws = allFalsy || argv.ws -var server = new Server({ +const server = new Server({ http: argv.http, interval: argv.interval, stats: argv.stats, @@ -104,7 +104,7 @@ server.on('stop', function (addr) { if (!argv.quiet) console.log('stop: ' + addr) }) -var hostname = { +const hostname = { http: argv['http-hostname'], udp4: argv['udp-hostname'], udp6: argv['udp6-hostname'] @@ -112,33 +112,33 @@ var hostname = { server.listen(argv.port, hostname, function () { if (server.http && argv.http && !argv.quiet) { - var httpAddr = server.http.address() - var httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost' - var httpPort = httpAddr.port + const httpAddr = server.http.address() + const httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost' + const httpPort = httpAddr.port console.log('HTTP tracker: http://' + httpHost + ':' + httpPort + '/announce') } if (server.udp && !argv.quiet) { - var udpAddr = server.udp.address() - var udpHost = udpAddr.address - var udpPort = udpAddr.port + const udpAddr = server.udp.address() + const udpHost = udpAddr.address + const udpPort = udpAddr.port console.log('UDP tracker: udp://' + udpHost + ':' + udpPort) } if (server.udp6 && !argv.quiet) { - var udp6Addr = server.udp6.address() - var udp6Host = udp6Addr.address !== '::' ? udp6Addr.address : 'localhost' - var udp6Port = udp6Addr.port + const udp6Addr = server.udp6.address() + const udp6Host = udp6Addr.address !== '::' ? udp6Addr.address : 'localhost' + const udp6Port = udp6Addr.port console.log('UDP6 tracker: udp://' + udp6Host + ':' + udp6Port) } if (server.ws && !argv.quiet) { - var wsAddr = server.http.address() - var wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost' - var wsPort = wsAddr.port + const wsAddr = server.http.address() + const wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost' + const wsPort = wsAddr.port console.log('WebSocket tracker: ws://' + wsHost + ':' + wsPort) } if (server.http && argv.stats && !argv.quiet) { - var statsAddr = server.http.address() - var statsHost = statsAddr.address !== '::' ? statsAddr.address : 'localhost' - var statsPort = statsAddr.port + const statsAddr = server.http.address() + const statsHost = statsAddr.address !== '::' ? statsAddr.address : 'localhost' + const statsPort = statsAddr.port console.log('Tracker stats: http://' + statsHost + ':' + statsPort + '/stats') } }) diff --git a/examples/express-embed/server.js b/examples/express-embed/server.js index bbb85b1..ff72147 100755 --- a/examples/express-embed/server.js +++ b/examples/express-embed/server.js @@ -1,27 +1,27 @@ #!/usr/bin/env node -var Server = require('../..').Server -var express = require('express') -var app = express() +const Server = require('../..').Server +const express = require('express') +const app = express() // https://wiki.theory.org/BitTorrentSpecification#peer_id -var whitelist = { +const whitelist = { UT: true // uTorrent } -var server = new Server({ +const server = new Server({ http: false, // we do our own udp: false, // not interested ws: false, // not interested filter: function (params) { // black/whitelist for disallowing/allowing specific clients [default=allow all] // 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] } }) -var onHttpRequest = server.onHttpRequest.bind(server) +const onHttpRequest = server.onHttpRequest.bind(server) app.get('/announce', onHttpRequest) app.get('/scrape', onHttpRequest) diff --git a/lib/client/http-tracker.js b/lib/client/http-tracker.js index bacc4eb..65dafb0 100644 --- a/lib/client/http-tracker.js +++ b/lib/client/http-tracker.js @@ -86,7 +86,7 @@ class HTTPTracker extends Tracker { // Otherwise, wait a short time for pending requests to complete, then force // 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 // right away. diff --git a/lib/client/udp-tracker.js b/lib/client/udp-tracker.js index 5f87640..e2f0c24 100644 --- a/lib/client/udp-tracker.js +++ b/lib/client/udp-tracker.js @@ -46,7 +46,7 @@ class UDPTracker extends Tracker { // Otherwise, wait a short time for pending requests to complete, then force // 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 // right away. diff --git a/lib/client/websocket-tracker.js b/lib/client/websocket-tracker.js index a82f5a6..e64503d 100644 --- a/lib/client/websocket-tracker.js +++ b/lib/client/websocket-tracker.js @@ -134,7 +134,7 @@ class WebSocketTracker extends Tracker { // Otherwise, wait a short time for potential responses to come in from the // 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 // right away. diff --git a/lib/common-node.js b/lib/common-node.js index 7a9e87d..12f5775 100644 --- a/lib/common-node.js +++ b/lib/common-node.js @@ -3,7 +3,7 @@ * 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.IPV6_RE = /^[\da-fA-F:]+$/ @@ -38,7 +38,7 @@ exports.REQUEST_TIMEOUT = 15000 exports.DESTROY_TIMEOUT = 1000 function toUInt32 (n) { - var buf = Buffer.allocUnsafe(4) + const buf = Buffer.allocUnsafe(4) buf.writeUInt32BE(n, 0) return buf } @@ -61,7 +61,7 @@ exports.querystringParse = function (q) { * @return {string} */ 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) { // `escape` doesn't encode the characters @*/+ so we do it manually return '%' + char.charCodeAt(0).toString(16).toUpperCase() diff --git a/lib/common.js b/lib/common.js index f735a85..4e0e039 100644 --- a/lib/common.js +++ b/lib/common.js @@ -19,5 +19,5 @@ exports.hexToBinary = function (str) { return Buffer.from(str, 'hex').toString('binary') } -var config = require('./common-node') +const config = require('./common-node') Object.assign(exports, config) diff --git a/lib/server/parse-http.js b/lib/server/parse-http.js index 85a1c87..763bc53 100644 --- a/lib/server/parse-http.js +++ b/lib/server/parse-http.js @@ -1,11 +1,11 @@ module.exports = parseHttpRequest -var common = require('../common') +const common = require('../common') function parseHttpRequest (req, opts) { if (!opts) opts = {} - var s = req.url.split('?') - var params = common.querystringParse(s[1]) + const s = req.url.split('?') + const params = common.querystringParse(s[1]) params.type = 'http' if (opts.action === 'announce' || s[0] === '/announce') { diff --git a/lib/server/parse-udp.js b/lib/server/parse-udp.js index da32beb..2402863 100644 --- a/lib/server/parse-udp.js +++ b/lib/server/parse-udp.js @@ -1,12 +1,12 @@ module.exports = parseUdpRequest -var ipLib = require('ip') -var common = require('../common') +const ipLib = require('ip') +const common = require('../common') function parseUdpRequest (msg, rinfo) { if (msg.length < 16) throw new Error('received packet is too short') - var params = { + const params = { connectionId: msg.slice(0, 8), // 64-bit action: msg.readUInt32BE(8), transactionId: msg.readUInt32BE(12), @@ -29,7 +29,7 @@ function parseUdpRequest (msg, rinfo) { params.event = common.EVENT_IDS[msg.readUInt32BE(80)] 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 ? ipLib.toString(ip) : rinfo.address @@ -49,8 +49,8 @@ function parseUdpRequest (msg, rinfo) { } else if (params.action === common.ACTIONS.SCRAPE) { // scrape message if ((msg.length - 16) % 20 !== 0) throw new Error('invalid scrape message') params.info_hash = [] - for (var 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 + for (let i = 0, len = (msg.length - 16) / 20; i < len; i += 1) { + const infoHash = msg.slice(16 + (i * 20), 36 + (i * 20)).toString('hex') // 20 bytes params.info_hash.push(infoHash) } } else { @@ -60,16 +60,16 @@ function parseUdpRequest (msg, rinfo) { 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 * lost for big numbers. */ function fromUInt64 (buf) { - var high = buf.readUInt32BE(0) | 0 // force - var low = buf.readUInt32BE(4) | 0 - var lowUnsigned = (low >= 0) ? low : TWO_PWR_32 + low + const high = buf.readUInt32BE(0) | 0 // force + const low = buf.readUInt32BE(4) | 0 + const lowUnsigned = (low >= 0) ? low : TWO_PWR_32 + low return (high * TWO_PWR_32) + lowUnsigned } diff --git a/lib/server/parse-websocket.js b/lib/server/parse-websocket.js index 4416008..c0e3431 100644 --- a/lib/server/parse-websocket.js +++ b/lib/server/parse-websocket.js @@ -1,6 +1,6 @@ module.exports = parseWebSocketRequest -var common = require('../common') +const common = require('../common') function parseWebSocketRequest (socket, opts, params) { if (!opts) opts = {} diff --git a/lib/server/swarm.js b/lib/server/swarm.js index 5c914c9..4cde412 100644 --- a/lib/server/swarm.js +++ b/lib/server/swarm.js @@ -1,14 +1,14 @@ module.exports = Swarm -var arrayRemove = require('unordered-array-remove') -var debug = require('debug')('bittorrent-tracker:swarm') -var LRU = require('lru') -var randomIterate = require('random-iterate') +const arrayRemove = require('unordered-array-remove') +const debug = require('debug')('bittorrent-tracker:swarm') +const LRU = require('lru') +const randomIterate = require('random-iterate') // Regard this as the default implementation of an interface that you // need to support when overriding Server.createSwarm() and Server.getSwarm() function Swarm (infoHash, server) { - var self = this + const self = this self.infoHash = infoHash self.complete = 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 // so the stats get updated correctly. self.peers.on('evict', function (data) { - var peer = data.value - var params = { + const peer = data.value + const params = { type: peer.type, event: 'stopped', numwant: 0, @@ -34,10 +34,10 @@ function Swarm (infoHash, server) { } Swarm.prototype.announce = function (params, cb) { - var self = this - var id = params.type === 'ws' ? params.peer_id : params.addr + const self = this + const id = params.type === 'ws' ? params.peer_id : params.addr // 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') { 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 // swarms that this peer is participating in. 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) } @@ -133,12 +133,12 @@ Swarm.prototype._onAnnounceUpdate = function (params, peer, id) { } Swarm.prototype._getPeers = function (numwant, ownPeerId, isWebRTC) { - var peers = [] - var ite = randomIterate(this.peers.keys) - var peerId + const peers = [] + const ite = randomIterate(this.peers.keys) + let peerId while ((peerId = ite()) && peers.length < numwant) { // 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 (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 diff --git a/test/client-large-torrent.js b/test/client-large-torrent.js index 7820216..cec42a0 100644 --- a/test/client-large-torrent.js +++ b/test/client-large-torrent.js @@ -1,15 +1,15 @@ -var Client = require('../') -var common = require('./common') -var fixtures = require('webtorrent-fixtures') -var test = require('tape') +const Client = require('../') +const common = require('./common') +const fixtures = require('webtorrent-fixtures') +const test = require('tape') -var peerId = Buffer.from('01234567890123456789') +const peerId = Buffer.from('01234567890123456789') function testLargeTorrent (t, serverType) { t.plan(9) common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + const client = new Client({ infoHash: fixtures.sintel.parsedTorrent.infoHash, peerId, port: 6881, diff --git a/test/client-magnet.js b/test/client-magnet.js index 994473e..8bbe905 100644 --- a/test/client-magnet.js +++ b/test/client-magnet.js @@ -1,18 +1,18 @@ -var Client = require('../') -var common = require('./common') -var fixtures = require('webtorrent-fixtures') -var magnet = require('magnet-uri') -var test = require('tape') +const Client = require('../') +const common = require('./common') +const fixtures = require('webtorrent-fixtures') +const magnet = require('magnet-uri') +const test = require('tape') -var peerId = Buffer.from('01234567890123456789') +const peerId = Buffer.from('01234567890123456789') function testMagnet (t, serverType) { t.plan(9) - var parsedTorrent = magnet(fixtures.leaves.magnetURI) + const parsedTorrent = magnet(fixtures.leaves.magnetURI) common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + const client = new Client({ infoHash: parsedTorrent.infoHash, announce: announceUrl, peerId, diff --git a/test/client-ws-socket-pool.js b/test/client-ws-socket-pool.js index 04ed642..605dce6 100644 --- a/test/client-ws-socket-pool.js +++ b/test/client-ws-socket-pool.js @@ -1,16 +1,16 @@ -var Client = require('../') -var common = require('./common') -var fixtures = require('webtorrent-fixtures') -var test = require('tape') +const Client = require('../') +const common = require('./common') +const fixtures = require('webtorrent-fixtures') +const test = require('tape') -var peerId = Buffer.from('01234567890123456789') -var port = 6681 +const peerId = Buffer.from('01234567890123456789') +const port = 6681 test('ensure client.destroy() callback is called with re-used websockets in socketPool', function (t) { t.plan(4) common.createServer(t, 'ws', function (server, announceUrl) { - var client1 = new Client({ + const client1 = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId, @@ -27,7 +27,7 @@ test('ensure client.destroy() callback is called with re-used websockets in sock client1.once('update', function () { t.pass('got client1 update') // 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 announce: announceUrl, peerId, diff --git a/test/client.js b/test/client.js index f1c8cef..829574d 100644 --- a/test/client.js +++ b/test/client.js @@ -1,18 +1,18 @@ -var Client = require('../') -var common = require('./common') -var fixtures = require('webtorrent-fixtures') -var test = require('tape') +const Client = require('../') +const common = require('./common') +const fixtures = require('webtorrent-fixtures') +const test = require('tape') -var peerId1 = Buffer.from('01234567890123456789') -var peerId2 = Buffer.from('12345678901234567890') -var peerId3 = Buffer.from('23456789012345678901') -var port = 6881 +const peerId1 = Buffer.from('01234567890123456789') +const peerId2 = Buffer.from('12345678901234567890') +const peerId3 = Buffer.from('23456789012345678901') +const port = 6881 function testClientStart (t, serverType) { t.plan(4) common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, @@ -58,7 +58,7 @@ function testClientStop (t, serverType) { t.plan(4) common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, @@ -106,7 +106,7 @@ function testClientStopDestroy (t, serverType) { t.plan(2) common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, @@ -160,7 +160,7 @@ function testClientUpdate (t, serverType) { t.plan(4) common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, @@ -213,7 +213,7 @@ function testClientScrape (t, serverType) { t.plan(4) common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, @@ -255,7 +255,7 @@ function testClientAnnounceWithParams (t, serverType) { t.plan(5) common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, @@ -303,7 +303,7 @@ function testClientGetAnnounceOpts (t, serverType) { t.plan(5) common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, @@ -354,7 +354,7 @@ function testClientAnnounceWithNumWant (t, serverType) { t.plan(4) common.createServer(t, serverType, function (server, announceUrl) { - var client1 = new Client({ + const client1 = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: [announceUrl], peerId: peerId1, @@ -368,7 +368,7 @@ function testClientAnnounceWithNumWant (t, serverType) { client1.start() client1.once('update', function () { - var client2 = new Client({ + const client2 = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId2, @@ -382,7 +382,7 @@ function testClientAnnounceWithNumWant (t, serverType) { client2.start() client2.once('update', function () { - var client3 = new Client({ + const client3 = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId3, @@ -398,7 +398,7 @@ function testClientAnnounceWithNumWant (t, serverType) { client3.on('peer', function () { t.pass('got one peer (this should only fire once)') - var num = 3 + let num = 3 function tryCloseServer () { num -= 1 if (num === 0) server.close() @@ -445,7 +445,7 @@ test('http: userAgent', function (t) { t.ok(req.headers['user-agent'].indexOf('WebTorrent') !== -1) }) - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, @@ -472,7 +472,7 @@ function testSupportedTracker (t, serverType) { t.plan(1) common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, @@ -510,7 +510,7 @@ test('ws: valid tracker port', function (t) { function testUnsupportedTracker (t, announceUrl) { t.plan(1) - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: peerId1, diff --git a/test/common.js b/test/common.js index 0f872e3..9282284 100644 --- a/test/common.js +++ b/test/common.js @@ -1,4 +1,4 @@ -var Server = require('../').Server +const Server = require('../').Server exports.createServer = function (t, opts, cb) { if (typeof opts === 'string') opts = { serverType: opts } @@ -7,14 +7,14 @@ exports.createServer = function (t, opts, cb) { opts.udp = (opts.serverType === 'udp') 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('warning', function (err) { t.error(err) }) server.listen(0, function () { - var port = server[opts.serverType].address().port - var announceUrl + const port = server[opts.serverType].address().port + let announceUrl if (opts.serverType === 'http') { announceUrl = 'http://127.0.0.1:' + port + '/announce' } else if (opts.serverType === 'udp') { @@ -29,8 +29,8 @@ exports.createServer = function (t, opts, cb) { exports.mockWebsocketTracker = function (client) { client._trackers[0]._generateOffers = function (numwant, cb) { - var offers = [] - for (var i = 0; i < numwant; i++) { + const offers = [] + for (let i = 0; i < numwant; i++) { offers.push({ fake_offer: 'fake_offer_' + i }) } process.nextTick(function () { diff --git a/test/destroy.js b/test/destroy.js index 57aa0b9..5888491 100644 --- a/test/destroy.js +++ b/test/destroy.js @@ -1,16 +1,16 @@ -var Client = require('../') -var common = require('./common') -var fixtures = require('webtorrent-fixtures') -var test = require('tape') +const Client = require('../') +const common = require('./common') +const fixtures = require('webtorrent-fixtures') +const test = require('tape') -var peerId = Buffer.from('01234567890123456789') -var port = 6881 +const peerId = Buffer.from('01234567890123456789') +const port = 6881 function testNoEventsAfterDestroy (t, serverType) { t.plan(1) common.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId, diff --git a/test/evict.js b/test/evict.js index 196a8f2..75d590c 100644 --- a/test/evict.js +++ b/test/evict.js @@ -1,21 +1,21 @@ -var Client = require('../') -var common = require('./common') -var test = require('tape') -var wrtc = require('wrtc') +const Client = require('../') +const common = require('./common') +const test = require('tape') +const wrtc = require('wrtc') -var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705' -var peerId = Buffer.from('01234567890123456789') -var peerId2 = Buffer.from('12345678901234567890') -var peerId3 = Buffer.from('23456789012345678901') +const infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705' +const peerId = Buffer.from('01234567890123456789') +const peerId2 = Buffer.from('12345678901234567890') +const peerId3 = Buffer.from('23456789012345678901') function serverTest (t, serverType, serverFamily) { t.plan(10) - var hostname = serverFamily === 'inet6' + const hostname = serverFamily === 'inet6' ? '[::1]' : '127.0.0.1' - var opts = { + const opts = { serverType, 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) { // Not using announceUrl param from `common.createServer()` since we // want to control IPv4 vs IPv6. - var port = server[serverType].address().port - var announceUrl = serverType + '://' + hostname + ':' + port + '/announce' + const port = server[serverType].address().port + const announceUrl = serverType + '://' + hostname + ':' + port + '/announce' - var client1 = new Client({ + const client1 = new Client({ infoHash, announce: [announceUrl], peerId, @@ -38,7 +38,7 @@ function serverTest (t, serverType, serverFamily) { client1.start() client1.once('update', function (data) { - var client2 = new Client({ + const client2 = new Client({ infoHash, announce: [announceUrl], peerId: peerId2, @@ -56,14 +56,14 @@ function serverTest (t, serverType, serverFamily) { t.equal(swarm.complete + swarm.incomplete, 2) // Ensure that first peer is evicted when a third one is added - var evicted = false + let evicted = false swarm.peers.once('evict', function (evictedPeer) { t.equal(evictedPeer.value.peerId, peerId.toString('hex')) t.equal(swarm.complete + swarm.incomplete, 2) evicted = true }) - var client3 = new Client({ + const client3 = new Client({ infoHash, announce: [announceUrl], peerId: peerId3, diff --git a/test/filter.js b/test/filter.js index 386f17a..21bb4d0 100644 --- a/test/filter.js +++ b/test/filter.js @@ -1,14 +1,14 @@ -var Client = require('../') -var common = require('./common') -var fixtures = require('webtorrent-fixtures') -var test = require('tape') +const Client = require('../') +const common = require('./common') +const fixtures = require('webtorrent-fixtures') +const test = require('tape') -var peerId = Buffer.from('01234567890123456789') +const peerId = Buffer.from('01234567890123456789') function testFilterOption (t, serverType) { 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) { process.nextTick(function () { if (infoHash === fixtures.alice.parsedTorrent.infoHash) { @@ -20,7 +20,7 @@ function testFilterOption (t, serverType) { } common.createServer(t, opts, function (server, announceUrl) { - var client1 = new Client({ + const client1 = new Client({ infoHash: fixtures.alice.parsedTorrent.infoHash, announce: announceUrl, peerId, @@ -37,7 +37,7 @@ function testFilterOption (t, serverType) { client1.destroy(function () { t.pass('client1 destroyed') - var client2 = new Client({ + const client2 = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId, @@ -88,7 +88,7 @@ test('ws: filter option blocks tracker from tracking torrent', function (t) { function testFilterCustomError (t, serverType) { 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) { process.nextTick(function () { if (infoHash === fixtures.alice.parsedTorrent.infoHash) { @@ -100,7 +100,7 @@ function testFilterCustomError (t, serverType) { } common.createServer(t, opts, function (server, announceUrl) { - var client1 = new Client({ + const client1 = new Client({ infoHash: fixtures.alice.parsedTorrent.infoHash, announce: announceUrl, peerId, @@ -116,7 +116,7 @@ function testFilterCustomError (t, serverType) { client1.destroy(function () { t.pass('client1 destroyed') - var client2 = new Client({ + const client2 = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId, diff --git a/test/querystring.js b/test/querystring.js index a8d5bd2..a507c36 100644 --- a/test/querystring.js +++ b/test/querystring.js @@ -1,12 +1,12 @@ -var common = require('../lib/common') -var test = require('tape') +const common = require('../lib/common') +const test = require('tape') // https://github.com/webtorrent/webtorrent/issues/196 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') - 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) // sanity check that encode-decode matches up diff --git a/test/request-handler.js b/test/request-handler.js index a9e1fa3..9eecb99 100644 --- a/test/request-handler.js +++ b/test/request-handler.js @@ -1,15 +1,15 @@ -var Client = require('../') -var common = require('./common') -var fixtures = require('webtorrent-fixtures') -var test = require('tape') -var Server = require('../server') +const Client = require('../') +const common = require('./common') +const fixtures = require('webtorrent-fixtures') +const test = require('tape') +const Server = require('../server') -var peerId = Buffer.from('01234567890123456789') +const peerId = Buffer.from('01234567890123456789') function testRequestHandler (t, serverType) { 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 { announce (params, cb) { @@ -23,14 +23,14 @@ function testRequestHandler (t, serverType) { } // Use a custom Swarm implementation for this test only - var OldSwarm = Server.Swarm + const OldSwarm = Server.Swarm Server.Swarm = Swarm t.on('end', function () { Server.Swarm = OldSwarm }) common.createServer(t, opts, function (server, announceUrl) { - var client1 = new Client({ + const client1 = new Client({ infoHash: fixtures.alice.parsedTorrent.infoHash, announce: announceUrl, peerId, diff --git a/test/scrape.js b/test/scrape.js index 08bf9b7..9323f85 100644 --- a/test/scrape.js +++ b/test/scrape.js @@ -1,17 +1,17 @@ -var bencode = require('bencode') -var Client = require('../') -var common = require('./common') -var commonLib = require('../lib/common') -var commonTest = require('./common') -var fixtures = require('webtorrent-fixtures') -var get = require('simple-get') -var test = require('tape') +const bencode = require('bencode') +const Client = require('../') +const common = require('./common') +const commonLib = require('../lib/common') +const commonTest = require('./common') +const fixtures = require('webtorrent-fixtures') +const get = require('simple-get') +const test = require('tape') -var peerId = Buffer.from('01234567890123456789') +const peerId = Buffer.from('01234567890123456789') function testSingle (t, serverType) { commonTest.createServer(t, serverType, function (server, announceUrl) { - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId, @@ -53,7 +53,7 @@ test('ws: single info_hash scrape', function (t) { function clientScrapeStatic (t, serverType) { commonTest.createServer(t, serverType, function (server, announceUrl) { - var client = Client.scrape({ + const client = Client.scrape({ announce: announceUrl, infoHash: fixtures.leaves.parsedTorrent.infoHash, 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 function clientScrapeStaticInvalid (t, serverType) { - var announceUrl = serverType + '://invalid.lol' + let announceUrl = serverType + '://invalid.lol' if (serverType === 'http') announceUrl += '/announce' - var client = Client.scrape({ + const client = Client.scrape({ announce: announceUrl, infoHash: fixtures.leaves.parsedTorrent.infoHash, wrtc: {} @@ -113,8 +113,8 @@ test('ws: scrape using Client.scrape static method (invalid url)', function (t) }) function clientScrapeMulti (t, serverType) { - var infoHash1 = fixtures.leaves.parsedTorrent.infoHash - var infoHash2 = fixtures.alice.parsedTorrent.infoHash + const infoHash1 = fixtures.leaves.parsedTorrent.infoHash + const infoHash2 = fixtures.alice.parsedTorrent.infoHash commonTest.createServer(t, serverType, function (server, announceUrl) { 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) { t.plan(13) - var binaryInfoHash1 = commonLib.hexToBinary(fixtures.leaves.parsedTorrent.infoHash) - var binaryInfoHash2 = commonLib.hexToBinary(fixtures.alice.parsedTorrent.infoHash) + const binaryInfoHash1 = commonLib.hexToBinary(fixtures.leaves.parsedTorrent.infoHash) + const binaryInfoHash2 = commonLib.hexToBinary(fixtures.alice.parsedTorrent.infoHash) 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] }) @@ -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) { 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) { - var scrapeUrl = announceUrl.replace('/announce', '/scrape') + const scrapeUrl = announceUrl.replace('/announce', '/scrape') // announce a torrent to the tracker - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId, diff --git a/test/server.js b/test/server.js index 98576a0..0742e95 100644 --- a/test/server.js +++ b/test/server.js @@ -1,34 +1,34 @@ -var Client = require('../') -var common = require('./common') -var test = require('tape') -var wrtc = require('wrtc') +const Client = require('../') +const common = require('./common') +const test = require('tape') +const wrtc = require('wrtc') -var infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705' -var peerId = Buffer.from('01234567890123456789') -var peerId2 = Buffer.from('12345678901234567890') -var peerId3 = Buffer.from('23456789012345678901') +const infoHash = '4cb67059ed6bd08362da625b3ae77f6f4a075705' +const peerId = Buffer.from('01234567890123456789') +const peerId2 = Buffer.from('12345678901234567890') +const peerId3 = Buffer.from('23456789012345678901') function serverTest (t, serverType, serverFamily) { t.plan(40) - var hostname = serverFamily === 'inet6' + const hostname = serverFamily === 'inet6' ? '[::1]' : '127.0.0.1' - var clientIp = serverFamily === 'inet6' + const clientIp = serverFamily === 'inet6' ? '::1' : '127.0.0.1' - var opts = { + const opts = { serverType } common.createServer(t, opts, function (server) { // Not using announceUrl param from `common.createServer()` since we // want to control IPv4 vs IPv6. - var port = server[serverType].address().port - var announceUrl = serverType + '://' + hostname + ':' + port + '/announce' + const port = server[serverType].address().port + const announceUrl = serverType + '://' + hostname + ':' + port + '/announce' - var client1 = new Client({ + const client1 = new Client({ infoHash, announce: [announceUrl], peerId, @@ -56,11 +56,11 @@ function serverTest (t, serverType, serverFamily) { t.equal(swarm.incomplete, 1) t.equal(swarm.peers.length, 1) - var id = serverType === 'ws' + const id = serverType === 'ws' ? peerId.toString('hex') : hostname + ':6881' - var peer = swarm.peers.peek(id) + const peer = swarm.peers.peek(id) t.equal(peer.type, serverType) t.equal(peer.ip, clientIp) t.equal(peer.peerId, peerId.toString('hex')) @@ -88,7 +88,7 @@ function serverTest (t, serverType, serverFamily) { t.equal(data.incomplete, 0) t.equal(typeof data.downloaded, 'number') - var client2 = new Client({ + const client2 = new Client({ infoHash, announce: [announceUrl], peerId: peerId2, @@ -108,7 +108,7 @@ function serverTest (t, serverType, serverFamily) { t.equal(data.complete, 1) t.equal(data.incomplete, 1) - var client3 = new Client({ + const client3 = new Client({ infoHash, announce: [announceUrl], peerId: peerId3, diff --git a/test/stats.js b/test/stats.js index 646b2ca..c13667f 100644 --- a/test/stats.js +++ b/test/stats.js @@ -1,24 +1,24 @@ -var Client = require('../') -var commonTest = require('./common') -var fixtures = require('webtorrent-fixtures') -var get = require('simple-get') -var test = require('tape') +const Client = require('../') +const commonTest = require('./common') +const fixtures = require('webtorrent-fixtures') +const get = require('simple-get') +const test = require('tape') -var peerId = Buffer.from('-WW0091-4ea5886ce160') -var unknownPeerId = Buffer.from('01234567890123456789') +const peerId = Buffer.from('-WW0091-4ea5886ce160') +const unknownPeerId = Buffer.from('01234567890123456789') function parseHtml (html) { - var extractValue = /[^v^h](\d+)/ - var array = html.replace('torrents', '\n').split('\n').filter(function (line) { + const extractValue = /[^v^h](\d+)/ + const array = html.replace('torrents', '\n').split('\n').filter(function (line) { return line && line.trim().length > 0 }).map(function (line) { - var a = extractValue.exec(line) + const a = extractValue.exec(line) if (a) { return parseInt(a[1]) } return null }) - var i = 0 + let i = 0 return { torrents: array[i++], activeTorrents: array[i++], @@ -35,12 +35,12 @@ test('server: get empty stats', function (t) { t.plan(11) 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) { t.error(err) - var stats = parseHtml(data.toString()) + const stats = parseHtml(data.toString()) t.equal(res.statusCode, 200) t.equal(stats.torrents, 0) t.equal(stats.activeTorrents, 0) @@ -60,7 +60,7 @@ test('server: get empty stats with json header', function (t) { t.plan(11) commonTest.createServer(t, 'http', function (server, announceUrl) { - var opts = { + const opts = { url: announceUrl.replace('/announce', '/stats'), headers: { accept: 'application/json' @@ -90,7 +90,7 @@ test('server: get empty stats on stats.json', function (t) { t.plan(11) commonTest.createServer(t, 'http', function (server, announceUrl) { - var opts = { + const opts = { url: announceUrl.replace('/announce', '/stats.json'), json: true } @@ -118,7 +118,7 @@ test('server: get leecher stats.json', function (t) { commonTest.createServer(t, 'http', function (server, announceUrl) { // announce a torrent to the tracker - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId, @@ -130,7 +130,7 @@ test('server: get leecher stats.json', function (t) { client.start() server.once('start', function () { - var opts = { + const opts = { url: announceUrl.replace('/announce', '/stats.json'), json: true } @@ -159,7 +159,7 @@ test('server: get leecher stats.json (unknown peerId)', function (t) { commonTest.createServer(t, 'http', function (server, announceUrl) { // announce a torrent to the tracker - var client = new Client({ + const client = new Client({ infoHash: fixtures.leaves.parsedTorrent.infoHash, announce: announceUrl, peerId: unknownPeerId, @@ -171,7 +171,7 @@ test('server: get leecher stats.json (unknown peerId)', function (t) { client.start() server.once('start', function () { - var opts = { + const opts = { url: announceUrl.replace('/announce', '/stats.json'), json: true }