@@ -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}
0 commit comments