@@ -138,7 +138,7 @@ Tracker.prototype.start = function (opts) {
138138 opts . event = 'started'
139139
140140 debug ( 'sent `start` to ' + self . _announceUrl )
141- self . _request ( opts )
141+ self . _announce ( opts )
142142 self . setInterval ( self . _intervalMs ) // start announcing on intervals
143143}
144144
@@ -148,7 +148,7 @@ Tracker.prototype.stop = function (opts) {
148148 opts . event = 'stopped'
149149
150150 debug ( 'sent `stop` to ' + self . _announceUrl )
151- self . _request ( opts )
151+ self . _announce ( opts )
152152 self . setInterval ( 0 ) // stop announcing on intervals
153153}
154154
@@ -159,18 +159,42 @@ Tracker.prototype.complete = function (opts) {
159159 opts . downloaded = opts . downloaded || self . torrentLength || 0
160160
161161 debug ( 'sent `complete` to ' + self . _announceUrl )
162- self . _request ( opts )
162+ self . _announce ( opts )
163163}
164164
165165Tracker . prototype . update = function ( opts ) {
166166 var self = this
167167 opts = opts || { }
168168
169169 debug ( 'sent `update` to ' + self . _announceUrl )
170- self . _request ( opts )
170+ self . _announce ( opts )
171171}
172172
173- Tracker . prototype . scrape = function ( opts ) {
173+ /**
174+ * Send an announce request to the tracker.
175+ * @param {Object } opts
176+ * @param {number= } opts.uploaded
177+ * @param {number= } opts.downloaded
178+ * @param {number= } opts.left (if not set, calculated automatically)
179+ */
180+ Tracker . prototype . _announce = function ( opts ) {
181+ var self = this
182+ opts = extend ( {
183+ uploaded : 0 , // default, user should provide real value
184+ downloaded : 0 // default, user should provide real value
185+ } , opts )
186+
187+ if ( self . client . torrentLength != null && opts . left == null ) {
188+ opts . left = self . client . torrentLength - ( opts . downloaded || 0 )
189+ }
190+
191+ self . _requestImpl ( self . _announceUrl , opts )
192+ }
193+
194+ /**
195+ * Send a scrape request to the tracker.
196+ */
197+ Tracker . prototype . scrape = function ( ) {
174198 var self = this
175199
176200 if ( ! self . _scrapeUrl ) {
@@ -189,12 +213,7 @@ Tracker.prototype.scrape = function (opts) {
189213 }
190214
191215 debug ( 'sent `scrape` to ' + self . _announceUrl )
192-
193- opts = extend ( {
194- info_hash : common . bytewiseEncodeURIComponent ( self . client . _infoHash )
195- } , opts )
196-
197- self . _requestImpl ( self . _scrapeUrl , opts )
216+ self . _requestImpl ( self . _scrapeUrl )
198217}
199218
200219Tracker . prototype . setInterval = function ( intervalMs ) {
@@ -207,35 +226,28 @@ Tracker.prototype.setInterval = function (intervalMs) {
207226 }
208227}
209228
210- /**
211- * Send an announce request to the tracker
212- */
213- Tracker . prototype . _request = function ( opts ) {
229+ Tracker . prototype . _requestHttp = function ( requestUrl , opts ) {
214230 var self = this
215- opts = extend ( {
216- info_hash : common . bytewiseEncodeURIComponent ( self . client . _infoHash ) ,
217- peer_id : common . bytewiseEncodeURIComponent ( self . client . _peerId ) ,
218- port : self . client . _port ,
219- compact : 1 ,
220- numwant : self . client . _numWant ,
221- uploaded : 0 , // default, user should provide real value
222- downloaded : 0 // default, user should provide real value
223- } , opts )
224231
225- if ( self . client . torrentLength !== undefined ) {
226- opts . left = self . client . torrentLength - ( opts . downloaded || 0 )
227- }
228-
229- if ( self . _trackerId ) {
230- opts . trackerid = self . _trackerId
232+ if ( isScrapeUrl ( requestUrl ) ) {
233+ opts = extend ( {
234+ info_hash : self . client . _infoHash . toString ( 'binary' )
235+ } , opts )
236+ } else {
237+ opts = extend ( {
238+ info_hash : self . client . _infoHash . toString ( 'binary' ) ,
239+ peer_id : self . client . _peerId . toString ( 'binary' ) ,
240+ port : self . client . _port ,
241+ compact : 1 ,
242+ numwant : self . client . _numWant
243+ } , opts )
244+
245+ if ( self . _trackerId ) {
246+ opts . trackerid = self . _trackerId
247+ }
231248 }
232249
233- self . _requestImpl ( self . _announceUrl , opts )
234- }
235-
236- Tracker . prototype . _requestHttp = function ( requestUrl , opts ) {
237- var self = this
238- var fullUrl = requestUrl + '?' + querystring . stringify ( opts )
250+ var fullUrl = requestUrl + '?' + common . querystringStringify ( opts )
239251
240252 var req = http . get ( fullUrl , function ( res ) {
241253 if ( res . statusCode !== 200 ) {
@@ -255,6 +267,7 @@ Tracker.prototype._requestHttp = function (requestUrl, opts) {
255267
256268Tracker . prototype . _requestUdp = function ( requestUrl , opts ) {
257269 var self = this
270+ opts = opts || { }
258271 var parsedUrl = url . parse ( requestUrl )
259272 var socket = dgram . createSocket ( 'udp4' )
260273 var transactionId = new Buffer ( hat ( 32 ) , 'hex' )
@@ -294,9 +307,8 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) {
294307 return error ( 'invalid udp handshake' )
295308 }
296309
297- var scrapeStr = 'scrape'
298- if ( requestUrl . substr ( requestUrl . lastIndexOf ( '/' ) + 1 , scrapeStr . length ) === scrapeStr ) {
299- scrape ( msg . slice ( 8 , 16 ) , opts )
310+ if ( isScrapeUrl ( requestUrl ) ) {
311+ scrape ( msg . slice ( 8 , 16 ) )
300312 } else {
301313 announce ( msg . slice ( 8 , 16 ) , opts )
302314 }
@@ -395,7 +407,7 @@ Tracker.prototype._requestUdp = function (requestUrl, opts) {
395407 ] ) )
396408 }
397409
398- function scrape ( connectionId , opts ) {
410+ function scrape ( connectionId ) {
399411 genTransactionId ( )
400412
401413 send ( Buffer . concat ( [
@@ -477,10 +489,6 @@ Tracker.prototype._handleResponse = function (requestUrl, data) {
477489 }
478490}
479491
480- //
481- // HELPERS
482- //
483-
484492function toUInt16 ( n ) {
485493 var buf = new Buffer ( 2 )
486494 buf . writeUInt16BE ( n , 0 )
@@ -499,3 +507,7 @@ function toUInt64 (n) {
499507 }
500508 return Buffer . concat ( [ common . toUInt32 ( 0 ) , common . toUInt32 ( n ) ] )
501509}
510+
511+ function isScrapeUrl ( u ) {
512+ return u . substr ( u . lastIndexOf ( '/' ) + 1 , 'scrape' . length ) === 'scrape'
513+ }
0 commit comments