Skip to content

Commit e5994d2

Browse files
author
Diego Rodriguez Baquero
committed
fix: modernize
1 parent 8a97b7e commit e5994d2

27 files changed

+490
-512
lines changed

bin/cmd.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ if (argv.version) {
3939
}
4040

4141
if (argv.help) {
42-
console.log(function () {
42+
console.log((() => {
4343
/*
4444
bittorrent-tracker - Start a bittorrent tracker server
4545
@@ -64,7 +64,7 @@ if (argv.help) {
6464
-v, --version print the current version
6565
6666
*/
67-
}.toString().split(/\n/).slice(2, -2).join('\n'))
67+
}).toString().split(/\n/).slice(2, -2).join('\n'))
6868
process.exit(0)
6969
}
7070

@@ -85,23 +85,23 @@ const server = new Server({
8585
ws: argv.ws
8686
})
8787

88-
server.on('error', function (err) {
89-
if (!argv.silent) console.error('ERROR: ' + err.message)
88+
server.on('error', err => {
89+
if (!argv.silent) console.error(`ERROR: ${err.message}`)
9090
})
91-
server.on('warning', function (err) {
92-
if (!argv.quiet) console.log('WARNING: ' + err.message)
91+
server.on('warning', err => {
92+
if (!argv.quiet) console.log(`WARNING: ${err.message}`)
9393
})
94-
server.on('update', function (addr) {
95-
if (!argv.quiet) console.log('update: ' + addr)
94+
server.on('update', addr => {
95+
if (!argv.quiet) console.log(`update: ${addr}`)
9696
})
97-
server.on('complete', function (addr) {
98-
if (!argv.quiet) console.log('complete: ' + addr)
97+
server.on('complete', addr => {
98+
if (!argv.quiet) console.log(`complete: ${addr}`)
9999
})
100-
server.on('start', function (addr) {
101-
if (!argv.quiet) console.log('start: ' + addr)
100+
server.on('start', addr => {
101+
if (!argv.quiet) console.log(`start: ${addr}`)
102102
})
103-
server.on('stop', function (addr) {
104-
if (!argv.quiet) console.log('stop: ' + addr)
103+
server.on('stop', addr => {
104+
if (!argv.quiet) console.log(`stop: ${addr}`)
105105
})
106106

107107
const hostname = {
@@ -110,35 +110,35 @@ const hostname = {
110110
udp6: argv['udp6-hostname']
111111
}
112112

113-
server.listen(argv.port, hostname, function () {
113+
server.listen(argv.port, hostname, () => {
114114
if (server.http && argv.http && !argv.quiet) {
115115
const httpAddr = server.http.address()
116116
const httpHost = httpAddr.address !== '::' ? httpAddr.address : 'localhost'
117117
const httpPort = httpAddr.port
118-
console.log('HTTP tracker: http://' + httpHost + ':' + httpPort + '/announce')
118+
console.log(`HTTP tracker: http://${httpHost}:${httpPort}/announce`)
119119
}
120120
if (server.udp && !argv.quiet) {
121121
const udpAddr = server.udp.address()
122122
const udpHost = udpAddr.address
123123
const udpPort = udpAddr.port
124-
console.log('UDP tracker: udp://' + udpHost + ':' + udpPort)
124+
console.log(`UDP tracker: udp://${udpHost}:${udpPort}`)
125125
}
126126
if (server.udp6 && !argv.quiet) {
127127
const udp6Addr = server.udp6.address()
128128
const udp6Host = udp6Addr.address !== '::' ? udp6Addr.address : 'localhost'
129129
const udp6Port = udp6Addr.port
130-
console.log('UDP6 tracker: udp://' + udp6Host + ':' + udp6Port)
130+
console.log(`UDP6 tracker: udp://${udp6Host}:${udp6Port}`)
131131
}
132132
if (server.ws && !argv.quiet) {
133133
const wsAddr = server.http.address()
134134
const wsHost = wsAddr.address !== '::' ? wsAddr.address : 'localhost'
135135
const wsPort = wsAddr.port
136-
console.log('WebSocket tracker: ws://' + wsHost + ':' + wsPort)
136+
console.log(`WebSocket tracker: ws://${wsHost}:${wsPort}`)
137137
}
138138
if (server.http && argv.stats && !argv.quiet) {
139139
const statsAddr = server.http.address()
140140
const statsHost = statsAddr.address !== '::' ? statsAddr.address : 'localhost'
141141
const statsPort = statsAddr.port
142-
console.log('Tracker stats: http://' + statsHost + ':' + statsPort + '/stats')
142+
console.log(`Tracker stats: http://${statsHost}:${statsPort}/stats`)
143143
}
144144
})

client.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,7 @@ Client.scrape = (opts, cb) => {
281281
})
282282

283283
opts.infoHash = Array.isArray(opts.infoHash)
284-
? opts.infoHash.map(infoHash => {
285-
return Buffer.from(infoHash, 'hex')
286-
})
284+
? opts.infoHash.map(infoHash => Buffer.from(infoHash, 'hex'))
287285
: Buffer.from(opts.infoHash, 'hex')
288286
client.scrape({ infoHash: opts.infoHash })
289287
return client

examples/express-embed/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const server = new Server({
1313
http: false, // we do our own
1414
udp: false, // not interested
1515
ws: false, // not interested
16-
filter: function (params) {
16+
filter (params) {
1717
// black/whitelist for disallowing/allowing specific clients [default=allow all]
1818
// this example only allows the uTorrent client
1919
const client = params.peer_id[1] + params.peer_id[2]

lib/client/http-tracker.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const HTTP_SCRAPE_SUPPORT = /\/(announce)[^/]*$/
1717
* @param {Object} opts options object
1818
*/
1919
class HTTPTracker extends Tracker {
20-
constructor (client, announceUrl, opts) {
20+
constructor (client, announceUrl) {
2121
super(client, announceUrl)
2222

2323
debug('new http tracker %s', announceUrl)
@@ -62,9 +62,7 @@ class HTTPTracker extends Tracker {
6262
}
6363

6464
const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
65-
? opts.infoHash.map(infoHash => {
66-
return infoHash.toString('binary')
67-
})
65+
? opts.infoHash.map(infoHash => infoHash.toString('binary'))
6866
: (opts.infoHash && opts.infoHash.toString('binary')) || this.client._infoHashBinary
6967
const params = {
7068
info_hash: infoHashes

lib/client/udp-tracker.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const Tracker = require('./tracker')
1616
* @param {Object} opts options object
1717
*/
1818
class UDPTracker extends Tracker {
19-
constructor (client, announceUrl, opts) {
19+
constructor (client, announceUrl) {
2020
super(client, announceUrl)
2121
debug('new udp tracker %s', announceUrl)
2222

@@ -181,7 +181,7 @@ class UDPTracker extends Tracker {
181181
return onError(new Error('invalid scrape message'))
182182
}
183183
const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
184-
? opts.infoHash.map(infoHash => { return infoHash.toString('hex') })
184+
? opts.infoHash.map(infoHash => infoHash.toString('hex'))
185185
: [(opts.infoHash && opts.infoHash.toString('hex')) || self.client.infoHash]
186186

187187
for (let i = 0, len = (msg.length - 8) / 12; i < len; i += 1) {

lib/client/websocket-tracker.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const RECONNECT_VARIANCE = 5 * 60 * 1000
1717
const OFFER_TIMEOUT = 50 * 1000
1818

1919
class WebSocketTracker extends Tracker {
20-
constructor (client, announceUrl, opts) {
20+
constructor (client, announceUrl) {
2121
super(client, announceUrl)
2222
debug('new websocket tracker %s', announceUrl)
2323

@@ -76,9 +76,7 @@ class WebSocketTracker extends Tracker {
7676
}
7777

7878
const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
79-
? opts.infoHash.map(infoHash => {
80-
return infoHash.toString('binary')
81-
})
79+
? opts.infoHash.map(infoHash => infoHash.toString('binary'))
8280
: (opts.infoHash && opts.infoHash.toString('binary')) || this.client._infoHashBinary
8381
const params = {
8482
action: 'scrape',

lib/common-node.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,17 @@ exports.toUInt32 = toUInt32
5050
* @param {string} q
5151
* @return {Object}
5252
*/
53-
exports.querystringParse = function (q) {
54-
return querystring.parse(q, null, null, { decodeURIComponent: unescape })
55-
}
53+
exports.querystringParse = q => querystring.parse(q, null, null, { decodeURIComponent: unescape })
5654

5755
/**
5856
* `querystring.stringify` using `escape` instead of encodeURIComponent, since bittorrent
5957
* clients send non-UTF8 querystrings
6058
* @param {Object} obj
6159
* @return {string}
6260
*/
63-
exports.querystringStringify = function (obj) {
61+
exports.querystringStringify = obj => {
6462
let ret = querystring.stringify(obj, null, null, { encodeURIComponent: escape })
65-
ret = ret.replace(/[@*/+]/g, function (char) {
66-
// `escape` doesn't encode the characters @*/+ so we do it manually
67-
return '%' + char.charCodeAt(0).toString(16).toUpperCase()
68-
})
63+
ret = ret.replace(/[@*/+]/g, char => // `escape` doesn't encode the characters @*/+ so we do it manually
64+
`%${char.charCodeAt(0).toString(16).toUpperCase()}`)
6965
return ret
7066
}

lib/common.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
exports.DEFAULT_ANNOUNCE_PEERS = 50
66
exports.MAX_ANNOUNCE_PEERS = 82
77

8-
exports.binaryToHex = function (str) {
8+
exports.binaryToHex = str => {
99
if (typeof str !== 'string') {
1010
str = String(str)
1111
}
1212
return Buffer.from(str, 'binary').toString('hex')
1313
}
1414

15-
exports.hexToBinary = function (str) {
15+
exports.hexToBinary = str => {
1616
if (typeof str !== 'string') {
1717
str = String(str)
1818
}
@@ -31,7 +31,7 @@ exports.hexToBinary = function (str) {
3131
// Bug reports:
3232
// - Chrome: https://bugs.chromium.org/p/chromium/issues/detail?id=734880
3333
// - Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1374505
34-
exports.parseUrl = function (str) {
34+
exports.parseUrl = str => {
3535
const url = new URL(str.replace(/^udp:/, 'http:'))
3636

3737
if (str.match(/^udp:/)) {

lib/server/parse-http.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@ function parseHttpRequest (req, opts) {
3636
params.ip = opts.trustProxy
3737
? req.headers['x-forwarded-for'] || req.connection.remoteAddress
3838
: req.connection.remoteAddress.replace(common.REMOVE_IPV4_MAPPED_IPV6_RE, '') // force ipv4
39-
params.addr = (common.IPV6_RE.test(params.ip) ? '[' + params.ip + ']' : params.ip) + ':' + params.port
39+
params.addr = `${common.IPV6_RE.test(params.ip) ? `[${params.ip}]` : params.ip}:${params.port}`
4040

4141
params.headers = req.headers
4242
} else if (opts.action === 'scrape' || s[0] === '/scrape') {
4343
params.action = common.ACTIONS.SCRAPE
4444

4545
if (typeof params.info_hash === 'string') params.info_hash = [params.info_hash]
4646
if (Array.isArray(params.info_hash)) {
47-
params.info_hash = params.info_hash.map(function (binaryInfoHash) {
47+
params.info_hash = params.info_hash.map(binaryInfoHash => {
4848
if (typeof binaryInfoHash !== 'string' || binaryInfoHash.length !== 20) {
4949
throw new Error('invalid info_hash')
5050
}
5151
return common.binaryToHex(binaryInfoHash)
5252
})
5353
}
5454
} else {
55-
throw new Error('invalid action in HTTP request: ' + req.url)
55+
throw new Error(`invalid action in HTTP request: ${req.url}`)
5656
}
5757

5858
return params

lib/server/parse-udp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function parseUdpRequest (msg, rinfo) {
4444
)
4545

4646
params.port = msg.readUInt16BE(96) || rinfo.port // optional
47-
params.addr = params.ip + ':' + params.port // TODO: ipv6 brackets
47+
params.addr = `${params.ip}:${params.port}` // TODO: ipv6 brackets
4848
params.compact = 1 // udp is always compact
4949
} else if (params.action === common.ACTIONS.SCRAPE) { // scrape message
5050
if ((msg.length - 16) % 20 !== 0) throw new Error('invalid scrape message')
@@ -54,7 +54,7 @@ function parseUdpRequest (msg, rinfo) {
5454
params.info_hash.push(infoHash)
5555
}
5656
} else {
57-
throw new Error('Invalid action in UDP packet: ' + params.action)
57+
throw new Error(`Invalid action in UDP packet: ${params.action}`)
5858
}
5959

6060
return params

0 commit comments

Comments
 (0)