@@ -52,6 +52,8 @@ export class FastTracker implements Tracker {
5252 } else {
5353 throw new TrackerError ( "unknown announce event" ) ;
5454 }
55+ } else if ( action === "scrape" ) {
56+ this . processScrape ( json , peer ) ;
5557 } else {
5658 new TrackerError ( "unknown action" ) ;
5759 }
@@ -243,6 +245,58 @@ export class FastTracker implements Tracker {
243245 ( peer as InternalPeerContext ) . swarms = undefined ;
244246 }
245247
248+ private processScrape ( json : any , peer : PeerContext ) {
249+ const infoHash : any = json . info_hash ;
250+ const files : { [ key : string ] : { complete : number , incomplete : number , downloaded : number } } = { } ;
251+
252+ if ( infoHash === undefined ) {
253+ for ( const swarm of this . swarms . values ( ) ) {
254+ files [ swarm . infoHash ] = {
255+ complete : swarm . completedCount ,
256+ incomplete : swarm . peersOrdered . length - swarm . completedCount ,
257+ downloaded : swarm . completedCount
258+ } ;
259+ }
260+ } else if ( infoHash instanceof Array ) {
261+ for ( const singleInfoHash of infoHash ) {
262+ const swarm = this . swarms . get ( singleInfoHash ) ;
263+ if ( swarm !== undefined ) {
264+ files [ singleInfoHash ] = {
265+ complete : swarm . completedCount ,
266+ incomplete : swarm . peersOrdered . length - swarm . completedCount ,
267+ downloaded : swarm . completedCount
268+ } ;
269+ } else if ( typeof singleInfoHash === "string" ) {
270+ files [ singleInfoHash ] = {
271+ complete : 0 ,
272+ incomplete : 0 ,
273+ downloaded : 0
274+ } ;
275+ }
276+ }
277+ } else {
278+ const swarm = this . swarms . get ( infoHash ) ;
279+ if ( swarm !== undefined ) {
280+ files [ infoHash ] = {
281+ complete : swarm . completedCount ,
282+ incomplete : swarm . peersOrdered . length - swarm . completedCount ,
283+ downloaded : swarm . completedCount
284+ } ;
285+ } else if ( typeof infoHash === "string" ) {
286+ files [ infoHash ] = {
287+ complete : 0 ,
288+ incomplete : 0 ,
289+ downloaded : 0
290+ } ;
291+ }
292+ }
293+
294+ peer . sendMessage ( {
295+ action : "scrape" ,
296+ files : files
297+ } ) ;
298+ }
299+
246300 public get stats ( ) {
247301 let peersCount = 0 ;
248302 for ( const swarm of this . swarms . values ( ) ) {
0 commit comments