@@ -10,6 +10,10 @@ interface Logger {
1010 error ( message : string , ...args : unknown [ ] ) : void ;
1111}
1212
13+ export interface WebTorrentOptions {
14+ trackers ?: string [ ] ; // Custom tracker URLs
15+ }
16+
1317export interface DownloadProgressEvent {
1418 downloaded : number ; // Total bytes downloaded
1519 downloadSpeed : number ; // Current download speed in bytes/second
@@ -37,6 +41,7 @@ class WebTorrentManager extends EventEmitter {
3741 private logger : Logger ;
3842 private isInitialized : boolean = false ;
3943 private initializationPromise : Promise < void > | null = null ;
44+ private customTrackers : string [ ] = [ ] ;
4045
4146 private constructor ( ) {
4247 super ( ) ;
@@ -65,7 +70,7 @@ class WebTorrentManager extends EventEmitter {
6570 /**
6671 * Initialize the WebTorrent client (only once, atomically)
6772 */
68- public async initialize ( logger ?: Logger ) : Promise < void > {
73+ public async initialize ( logger ?: Logger , options ?: WebTorrentOptions ) : Promise < void > {
6974 // If already initialized, return immediately
7075 if ( this . isInitialized && this . webTorrentClient ) {
7176 return ;
@@ -78,7 +83,7 @@ class WebTorrentManager extends EventEmitter {
7883 }
7984
8085 // Start the initialization process atomically
81- this . initializationPromise = this . performInitialization ( logger ) ;
86+ this . initializationPromise = this . performInitialization ( logger , options ) ;
8287
8388 try {
8489 await this . initializationPromise ;
@@ -91,15 +96,30 @@ class WebTorrentManager extends EventEmitter {
9196 /**
9297 * Perform the actual initialization work
9398 */
94- private async performInitialization ( logger ?: Logger ) : Promise < void > {
99+ private async performInitialization ( logger ?: Logger , options ?: WebTorrentOptions ) : Promise < void > {
95100 if ( logger ) {
96101 this . logger = logger ;
97102 }
98103
104+ // Store custom trackers if provided
105+ if ( options ?. trackers ) {
106+ this . customTrackers = options . trackers ;
107+ this . logger . debug ( `🔧 Custom trackers configured: ${ this . customTrackers . join ( ', ' ) } ` ) ;
108+ }
109+
99110 this . logger . debug ( "🚀 Initializing shared WebTorrent client..." ) ;
100111
101112 try {
102- this . webTorrentClient = new WebTorrent ( ) ;
113+ const webTorrentOptions : WebTorrent . Options = { } ;
114+
115+ // Add custom trackers to WebTorrent client configuration if provided
116+ if ( this . customTrackers . length > 0 ) {
117+ webTorrentOptions . tracker = {
118+ announce : this . customTrackers
119+ } as WebTorrent . Options [ 'tracker' ] ;
120+ }
121+
122+ this . webTorrentClient = new WebTorrent ( webTorrentOptions ) ;
103123
104124 // Set unlimited max listeners for the WebTorrent client (if method exists)
105125 if ( typeof this . webTorrentClient . setMaxListeners === 'function' ) {
@@ -352,7 +372,15 @@ class WebTorrentManager extends EventEmitter {
352372
353373 return new Promise < string > ( ( resolve , reject ) => {
354374 try {
355- this . webTorrentClient ! . seed ( filePath , ( torrent ) => {
375+ const seedOptions : WebTorrent . TorrentOptions = { } ;
376+
377+ // Add custom trackers to the torrent if configured
378+ if ( this . customTrackers . length > 0 ) {
379+ seedOptions . announce = this . customTrackers ;
380+ this . logger . debug ( `🔧 Using custom trackers for seeding: ${ this . customTrackers . join ( ', ' ) } ` ) ;
381+ }
382+
383+ this . webTorrentClient ! . seed ( filePath , seedOptions , ( torrent ) => {
356384 const magnetURI = torrent . magnetURI ;
357385 this . logger . debug ( `🧲 File seeded successfully: ${ filePath } ` ) ;
358386 this . logger . debug ( ` Magnet URI: ${ magnetURI } ` ) ;
0 commit comments