Skip to content

Commit 5472803

Browse files
committed
Added events for tracking download status
Signed-off-by: Robert Gogete <gogeterobert@yahoo.com>
1 parent c09747b commit 5472803

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/client.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import https from "node:https";
33
import WebTorrent from "webtorrent";
44
import { URL } from "node:url";
55
import { Readable } from "node:stream";
6+
import { EventEmitter } from "node:events";
67
import { IFileClient, HostCapabilities } from "./interfaces";
78
import { 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

tests/client.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,9 @@ describe('FileClient', () => {
540540
const mockTorrent = {
541541
name: 'small-file.txt',
542542
length: 512, // 512 bytes file
543+
downloaded: 0,
544+
downloadSpeed: 1024,
545+
progress: 0,
543546
files: [{
544547
name: 'small-file.txt',
545548
createReadStream: jest.fn().mockReturnValue(mockStream)

0 commit comments

Comments
 (0)