@@ -3,6 +3,7 @@ import https from "node:https";
33import WebTorrent from "webtorrent" ;
44import { URL } from "node:url" ;
55import { Readable } from "node:stream" ;
6+ import { EventEmitter } from "node:events" ;
67import { IFileClient , HostCapabilities } from "./interfaces" ;
78import { GunRegistry } from "./registry/gun-registry" ;
89
@@ -25,13 +26,23 @@ export interface FileClientOptions {
2526 logger ?: Logger ; // Optional logger for debug output
2627}
2728
28- export class FileClient implements IFileClient {
29+ export interface DownloadProgressEvent {
30+ downloaded : number ; // Total bytes downloaded
31+ downloadSpeed : number ; // Current download speed in bytes/second
32+ progress : number ; // Progress as a decimal between 0 and 1
33+ name : string ; // Torrent/file name
34+ magnetUri : string ; // The magnet URI being downloaded
35+ }
36+
37+ export class FileClient extends EventEmitter implements IFileClient {
2938 private gunRegistry : GunRegistry ;
3039 private webTorrentClient : WebTorrent . Instance | null = null ;
3140 private options : FileClientOptions ;
3241 private logger : Logger ;
3342
3443 constructor ( options : FileClientOptions = { } ) {
44+ super ( ) ; // Call EventEmitter constructor
45+
3546 this . options = {
3647 peers : options . peers || [ "http://dig-relay-prod.eba-2cmanxbe.us-east-1.elasticbeanstalk.com/gun" ] ,
3748 namespace : options . namespace || "dig-nat-tools" ,
@@ -394,6 +405,26 @@ export class FileClient implements IFileClient {
394405 infoHash : torrent ?. infoHash
395406 } ) ;
396407 } ) ;
408+
409+ // Add download progress event emission
410+ torrent . on ( "download" , ( _bytes : number ) => {
411+ const progressData : DownloadProgressEvent = {
412+ downloaded : torrent ! . downloaded ,
413+ downloadSpeed : torrent ! . downloadSpeed ,
414+ progress : torrent ! . progress * 100 ,
415+ name : torrent ! . name || 'Unknown' ,
416+ magnetUri : magnetUri
417+ } ;
418+
419+ this . logger . debug ( `📊 Download progress: ${ ( progressData . progress * 100 ) . toFixed ( 1 ) } % - ${ progressData . name } ` , {
420+ downloaded : `${ ( progressData . downloaded / ( 1024 * 1024 ) ) . toFixed ( 2 ) } MB` ,
421+ speed : `${ ( progressData . downloadSpeed / ( 1024 * 1024 ) ) . toFixed ( 2 ) } MB/s` ,
422+ progress : `${ ( progressData . progress * 100 ) . toFixed ( 1 ) } %`
423+ } ) ;
424+
425+ // Emit the download event that external code can listen to
426+ this . emit ( 'download' , progressData ) ;
427+ } ) ;
397428 } ) ;
398429 }
399430
0 commit comments