Skip to content

Commit 0b369ab

Browse files
committed
Removed capabilities from gunjs when a torrent is removed
Signed-off-by: Robert Gogete <[email protected]>
1 parent b7d8bde commit 0b369ab

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

src/host.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ export class FileHost implements IFileHost {
572572
* Remove a shared file
573573
* This removes the file from both HTTP and WebTorrent sharing
574574
*/
575-
public unshareFile(filename: string, deleteFile: boolean = false): boolean {
575+
public async unshareFile(filename: string, deleteFile: boolean = false): Promise<boolean> {
576576
this.logger.debug(`📤 Unsharing file: ${filename}`);
577577

578578
const wasShared = this.sharedFiles.has(filename);
@@ -592,6 +592,25 @@ export class FileHost implements IFileHost {
592592
}
593593
}
594594

595+
// Update Gun.js registry to remove the unshared file's magnet URI
596+
if (this.gunRegistry && this.capabilities && wasShared) {
597+
try {
598+
this.logger.debug(`🔄 Updating Gun.js registry after unsharing ${filename}...`);
599+
600+
// Update the current capabilities with the updated magnet URI list
601+
if (this.capabilities.webTorrent) {
602+
this.capabilities.webTorrent.magnetUris = Array.from(
603+
this.magnetUris.values()
604+
);
605+
}
606+
607+
await this.gunRegistry.register(this.capabilities);
608+
this.logger.debug(`✅ Updated Gun.js registry after unsharing ${filename}`);
609+
} catch (error) {
610+
this.logger.warn(`⚠️ Failed to update Gun.js registry after unsharing:`, error);
611+
}
612+
}
613+
595614
// Optionally delete the original file
596615
if (deleteFile && filePath && fs.existsSync(filePath)) {
597616
try {

src/interfaces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ export interface IFileHost {
5353
* Remove a shared file by its filename
5454
* @param filename filename of the file to unshare
5555
* @param deleteFile Whether to delete the original file from disk (default: false)
56-
* @returns True if file was found and removed from tracking, false otherwise
56+
* @returns Promise that resolves to true if file was found and removed from tracking, false otherwise
5757
*/
58-
unshareFile(filename: string, deleteFile?: boolean): boolean;
58+
unshareFile(filename: string, deleteFile?: boolean): Promise<boolean>;
5959

6060
/**
6161
* Get a list of currently shared filenames

src/webtorrent-manager.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ class WebTorrentManager extends EventEmitter {
9999
this.logger.debug("🚀 Initializing shared WebTorrent client...");
100100

101101
try {
102-
this.webTorrentClient = new WebTorrent({
103-
dht: { port: 30987 },
104-
tracker: { port: 30987 }
105-
});
102+
this.webTorrentClient = new WebTorrent();
106103

107104
// Set unlimited max listeners for the WebTorrent client (if method exists)
108105
if (typeof this.webTorrentClient.setMaxListeners === 'function') {

tests/host.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -433,21 +433,21 @@ describe('FileHost', () => {
433433
it('should unshare a file', async () => {
434434
const hash = await fileHost.shareFile('/test/file.txt');
435435

436-
const result = fileHost.unshareFile(hash);
436+
const result = await fileHost.unshareFile(hash);
437437

438438
expect(result).toBe(true);
439439
expect(fileHost.getSharedFiles()).not.toContain(hash);
440440
});
441441

442-
it('should return false for non-existent file', () => {
443-
const result = fileHost.unshareFile('non-existent-file.txt');
442+
it('should return false for non-existent file', async () => {
443+
const result = await fileHost.unshareFile('non-existent-file.txt');
444444
expect(result).toBe(false);
445445
});
446446

447447
it('should delete file when deleteFile is true', async () => {
448448
const filename = await fileHost.shareFile('/test/file.txt');
449449

450-
fileHost.unshareFile(filename, true);
450+
await fileHost.unshareFile(filename, true);
451451

452452
expect(mockFs.unlinkSync).toHaveBeenCalledWith('/test/file.txt');
453453
});
@@ -458,7 +458,7 @@ describe('FileHost', () => {
458458

459459
const filename = await host.shareFile('/test/file.txt');
460460

461-
host.unshareFile(filename);
461+
await host.unshareFile(filename);
462462

463463
expect(mockWebTorrentManager.removeTorrent).toHaveBeenCalledWith('magnet:?xt=urn:btih:test-hash&dn=test-file');
464464
}, 10000);

0 commit comments

Comments
 (0)