Skip to content

Commit c8ceaee

Browse files
committed
replace self with this
1 parent 386e0a5 commit c8ceaee

File tree

4 files changed

+183
-209
lines changed

4 files changed

+183
-209
lines changed

lib/client/http-tracker.js

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,81 +20,78 @@ class HTTPTracker extends Tracker {
2020
constructor (client, announceUrl, opts) {
2121
super(client, announceUrl)
2222

23-
const self = this
2423
debug('new http tracker %s', announceUrl)
2524

2625
// Determine scrape url (if http tracker supports it)
27-
self.scrapeUrl = null
26+
this.scrapeUrl = null
2827

29-
const match = self.announceUrl.match(HTTP_SCRAPE_SUPPORT)
28+
const match = this.announceUrl.match(HTTP_SCRAPE_SUPPORT)
3029
if (match) {
31-
const pre = self.announceUrl.slice(0, match.index)
32-
const post = self.announceUrl.slice(match.index + 9)
33-
self.scrapeUrl = `${pre}/scrape${post}`
30+
const pre = this.announceUrl.slice(0, match.index)
31+
const post = this.announceUrl.slice(match.index + 9)
32+
this.scrapeUrl = `${pre}/scrape${post}`
3433
}
3534

36-
self.cleanupFns = []
37-
self.maybeDestroyCleanup = null
35+
this.cleanupFns = []
36+
this.maybeDestroyCleanup = null
3837
}
3938

4039
announce (opts) {
41-
const self = this
42-
if (self.destroyed) return
40+
if (this.destroyed) return
4341

4442
const params = Object.assign({}, opts, {
4543
compact: (opts.compact == null) ? 1 : opts.compact,
46-
info_hash: self.client._infoHashBinary,
47-
peer_id: self.client._peerIdBinary,
48-
port: self.client._port
44+
info_hash: this.client._infoHashBinary,
45+
peer_id: this.client._peerIdBinary,
46+
port: this.client._port
4947
})
50-
if (self._trackerId) params.trackerid = self._trackerId
48+
if (this._trackerId) params.trackerid = this._trackerId
5149

52-
self._request(self.announceUrl, params, (err, data) => {
53-
if (err) return self.client.emit('warning', err)
54-
self._onAnnounceResponse(data)
50+
this._request(this.announceUrl, params, (err, data) => {
51+
if (err) return this.client.emit('warning', err)
52+
this._onAnnounceResponse(data)
5553
})
5654
}
5755

5856
scrape (opts) {
59-
const self = this
60-
if (self.destroyed) return
57+
if (this.destroyed) return
6158

62-
if (!self.scrapeUrl) {
63-
self.client.emit('error', new Error(`scrape not supported ${self.announceUrl}`))
59+
if (!this.scrapeUrl) {
60+
this.client.emit('error', new Error(`scrape not supported ${this.announceUrl}`))
6461
return
6562
}
6663

6764
const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
6865
? opts.infoHash.map(infoHash => {
6966
return infoHash.toString('binary')
7067
})
71-
: (opts.infoHash && opts.infoHash.toString('binary')) || self.client._infoHashBinary
68+
: (opts.infoHash && opts.infoHash.toString('binary')) || this.client._infoHashBinary
7269
const params = {
7370
info_hash: infoHashes
7471
}
75-
self._request(self.scrapeUrl, params, (err, data) => {
76-
if (err) return self.client.emit('warning', err)
77-
self._onScrapeResponse(data)
72+
this._request(this.scrapeUrl, params, (err, data) => {
73+
if (err) return this.client.emit('warning', err)
74+
this._onScrapeResponse(data)
7875
})
7976
}
8077

8178
destroy (cb) {
8279
const self = this
83-
if (self.destroyed) return cb(null)
84-
self.destroyed = true
85-
clearInterval(self.interval)
80+
if (this.destroyed) return cb(null)
81+
this.destroyed = true
82+
clearInterval(this.interval)
8683

8784
// If there are no pending requests, destroy immediately.
88-
if (self.cleanupFns.length === 0) return destroyCleanup()
85+
if (this.cleanupFns.length === 0) return destroyCleanup()
8986

9087
// Otherwise, wait a short time for pending requests to complete, then force
9188
// destroy them.
9289
var timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT)
9390

9491
// But, if all pending requests complete before the timeout fires, do cleanup
9592
// right away.
96-
self.maybeDestroyCleanup = () => {
97-
if (self.cleanupFns.length === 0) destroyCleanup()
93+
this.maybeDestroyCleanup = () => {
94+
if (this.cleanupFns.length === 0) destroyCleanup()
9895
}
9996

10097
function destroyCleanup () {
@@ -116,13 +113,13 @@ class HTTPTracker extends Tracker {
116113
const u = requestUrl + (!requestUrl.includes('?') ? '?' : '&') +
117114
common.querystringStringify(params)
118115

119-
self.cleanupFns.push(cleanup)
116+
this.cleanupFns.push(cleanup)
120117

121118
let request = get.concat({
122119
url: u,
123120
timeout: common.REQUEST_TIMEOUT,
124121
headers: {
125-
'user-agent': self.client._userAgent || ''
122+
'user-agent': this.client._userAgent || ''
126123
}
127124
}, onResponse)
128125

@@ -171,38 +168,36 @@ class HTTPTracker extends Tracker {
171168
}
172169

173170
_onAnnounceResponse (data) {
174-
const self = this
175-
176171
const interval = data.interval || data['min interval']
177-
if (interval) self.setInterval(interval * 1000)
172+
if (interval) this.setInterval(interval * 1000)
178173

179174
const trackerId = data['tracker id']
180175
if (trackerId) {
181176
// If absent, do not discard previous trackerId value
182-
self._trackerId = trackerId
177+
this._trackerId = trackerId
183178
}
184179

185180
const response = Object.assign({}, data, {
186-
announce: self.announceUrl,
181+
announce: this.announceUrl,
187182
infoHash: common.binaryToHex(data.info_hash)
188183
})
189-
self.client.emit('update', response)
184+
this.client.emit('update', response)
190185

191186
let addrs
192187
if (Buffer.isBuffer(data.peers)) {
193188
// tracker returned compact response
194189
try {
195190
addrs = compact2string.multi(data.peers)
196191
} catch (err) {
197-
return self.client.emit('warning', err)
192+
return this.client.emit('warning', err)
198193
}
199194
addrs.forEach(addr => {
200-
self.client.emit('peer', addr)
195+
this.client.emit('peer', addr)
201196
})
202197
} else if (Array.isArray(data.peers)) {
203198
// tracker returned normal response
204199
data.peers.forEach(peer => {
205-
self.client.emit('peer', `${peer.ip}:${peer.port}`)
200+
this.client.emit('peer', `${peer.ip}:${peer.port}`)
206201
})
207202
}
208203

@@ -211,42 +206,41 @@ class HTTPTracker extends Tracker {
211206
try {
212207
addrs = compact2string.multi6(data.peers6)
213208
} catch (err) {
214-
return self.client.emit('warning', err)
209+
return this.client.emit('warning', err)
215210
}
216211
addrs.forEach(addr => {
217-
self.client.emit('peer', addr)
212+
this.client.emit('peer', addr)
218213
})
219214
} else if (Array.isArray(data.peers6)) {
220215
// tracker returned normal response
221216
data.peers6.forEach(peer => {
222217
const ip = /^\[/.test(peer.ip) || !/:/.test(peer.ip)
223218
? peer.ip /* ipv6 w/ brackets or domain name */
224219
: `[${peer.ip}]` /* ipv6 without brackets */
225-
self.client.emit('peer', `${ip}:${peer.port}`)
220+
this.client.emit('peer', `${ip}:${peer.port}`)
226221
})
227222
}
228223
}
229224

230225
_onScrapeResponse (data) {
231-
const self = this
232226
// NOTE: the unofficial spec says to use the 'files' key, 'host' has been
233227
// seen in practice
234228
data = data.files || data.host || {}
235229

236230
const keys = Object.keys(data)
237231
if (keys.length === 0) {
238-
self.client.emit('warning', new Error('invalid scrape response'))
232+
this.client.emit('warning', new Error('invalid scrape response'))
239233
return
240234
}
241235

242236
keys.forEach(infoHash => {
243237
// TODO: optionally handle data.flags.min_request_interval
244238
// (separate from announce interval)
245239
const response = Object.assign(data[infoHash], {
246-
announce: self.announceUrl,
240+
announce: this.announceUrl,
247241
infoHash: common.binaryToHex(infoHash)
248242
})
249-
self.client.emit('scrape', response)
243+
this.client.emit('scrape', response)
250244
})
251245
}
252246
}

lib/client/tracker.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,23 @@ class Tracker extends EventEmitter {
44
constructor (client, announceUrl) {
55
super()
66

7-
const self = this
8-
self.client = client
9-
self.announceUrl = announceUrl
7+
this.client = client
8+
this.announceUrl = announceUrl
109

11-
self.interval = null
12-
self.destroyed = false
10+
this.interval = null
11+
this.destroyed = false
1312
}
1413

1514
setInterval (intervalMs) {
16-
const self = this
17-
if (intervalMs == null) intervalMs = self.DEFAULT_ANNOUNCE_INTERVAL
15+
if (intervalMs == null) intervalMs = this.DEFAULT_ANNOUNCE_INTERVAL
1816

19-
clearInterval(self.interval)
17+
clearInterval(this.interval)
2018

2119
if (intervalMs) {
22-
self.interval = setInterval(() => {
23-
self.announce(self.client._defaultAnnounceOpts())
20+
this.interval = setInterval(() => {
21+
this.announce(this.client._defaultAnnounceOpts())
2422
}, intervalMs)
25-
if (self.interval.unref) self.interval.unref()
23+
if (this.interval.unref) this.interval.unref()
2624
}
2725
}
2826
}

lib/client/udp-tracker.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,40 @@ const Tracker = require('./tracker')
2020
class UDPTracker extends Tracker {
2121
constructor (client, announceUrl, opts) {
2222
super(client, announceUrl)
23-
const self = this
2423
debug('new udp tracker %s', announceUrl)
2524

26-
self.cleanupFns = []
27-
self.maybeDestroyCleanup = null
25+
this.cleanupFns = []
26+
this.maybeDestroyCleanup = null
2827
}
2928

3029
announce (opts) {
31-
const self = this
32-
if (self.destroyed) return
33-
self._request(opts)
30+
if (this.destroyed) return
31+
this._request(opts)
3432
}
3533

3634
scrape (opts) {
37-
const self = this
38-
if (self.destroyed) return
35+
if (this.destroyed) return
3936
opts._scrape = true
40-
self._request(opts) // udp scrape uses same announce url
37+
this._request(opts) // udp scrape uses same announce url
4138
}
4239

4340
destroy (cb) {
4441
const self = this
45-
if (self.destroyed) return cb(null)
46-
self.destroyed = true
47-
clearInterval(self.interval)
42+
if (this.destroyed) return cb(null)
43+
this.destroyed = true
44+
clearInterval(this.interval)
4845

4946
// If there are no pending requests, destroy immediately.
50-
if (self.cleanupFns.length === 0) return destroyCleanup()
47+
if (this.cleanupFns.length === 0) return destroyCleanup()
5148

5249
// Otherwise, wait a short time for pending requests to complete, then force
5350
// destroy them.
5451
var timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT)
5552

5653
// But, if all pending requests complete before the timeout fires, do cleanup
5754
// right away.
58-
self.maybeDestroyCleanup = () => {
59-
if (self.cleanupFns.length === 0) destroyCleanup()
55+
this.maybeDestroyCleanup = () => {
56+
if (this.cleanupFns.length === 0) destroyCleanup()
6057
}
6158

6259
function destroyCleanup () {
@@ -76,7 +73,7 @@ class UDPTracker extends Tracker {
7673
_request (opts) {
7774
const self = this
7875
if (!opts) opts = {}
79-
const parsedUrl = url.parse(self.announceUrl)
76+
const parsedUrl = url.parse(this.announceUrl)
8077
let transactionId = genTransactionId()
8178
let socket = dgram.createSocket('udp4')
8279

@@ -88,7 +85,7 @@ class UDPTracker extends Tracker {
8885
}, common.REQUEST_TIMEOUT)
8986
if (timeout.unref) timeout.unref()
9087

91-
self.cleanupFns.push(cleanup)
88+
this.cleanupFns.push(cleanup)
9289

9390
send(Buffer.concat([
9491
common.CONNECTION_ID,

0 commit comments

Comments
 (0)