Skip to content

Commit dec377e

Browse files
committed
Emit event on metadata to be able to calculate file size in node
Signed-off-by: Robert Gogete <gogeterobert@yahoo.com>
1 parent 5472803 commit dec377e

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/client.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ export interface DownloadProgressEvent {
3434
magnetUri: string; // The magnet URI being downloaded
3535
}
3636

37+
export interface MetadataEvent {
38+
name: string; // Torrent/file name
39+
size: number; // Total size in bytes
40+
magnetUri: string; // The magnet URI
41+
infoHash: string; // The torrent info hash
42+
}
43+
3744
export class FileClient extends EventEmitter implements IFileClient {
3845
private gunRegistry: GunRegistry;
3946
private webTorrentClient: WebTorrent.Instance | null = null;
@@ -319,6 +326,21 @@ export class FileClient extends EventEmitter implements IFileClient {
319326
return;
320327
}
321328

329+
// Add metadata event emission
330+
torrent.on("metadata", () => {
331+
this.logger.debug(`📋 Torrent metadata ready: ${torrent!.name}, Size: ${torrent!.length} bytes`);
332+
333+
const metadataData: MetadataEvent = {
334+
name: torrent!.name || 'Unknown',
335+
size: torrent!.length,
336+
magnetUri: magnetUri,
337+
infoHash: torrent!.infoHash || 'Unknown'
338+
};
339+
340+
// Emit the metadata event that external code can listen to
341+
this.emit('metadata', metadataData);
342+
});
343+
322344
torrent.on("ready", () => {
323345
this.logger.debug(
324346
`✅ Torrent ready! File: ${torrent!.name}, Size: ${torrent!.length} bytes, Files: ${torrent!.files.length}`
@@ -410,7 +432,7 @@ export class FileClient extends EventEmitter implements IFileClient {
410432
torrent.on("download", (_bytes: number) => {
411433
const progressData: DownloadProgressEvent = {
412434
downloaded: torrent!.downloaded,
413-
downloadSpeed: torrent!.downloadSpeed,
435+
downloadSpeed: torrent!.downloadSpeed * 100,
414436
progress: torrent!.progress * 100,
415437
name: torrent!.name || 'Unknown',
416438
magnetUri: magnetUri

src/registry/gun-registry.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
11
// Set up console.log filtering before importing Gun to suppress welcome message
2-
const originalConsoleLog = console.log;
3-
console.log = (...args: unknown[]): void => {
4-
const message = args.join(' ');
5-
// Block Gun.js welcome message specifically
6-
if (!message.includes('Hello wonderful person') &&
7-
!message.includes('Thanks for using GUN') &&
8-
!message.includes('chat.gun.eco')) {
9-
originalConsoleLog(...args);
10-
}
11-
};
12-
132
import Gun from "gun";
143
import "gun/lib/webrtc.js";
154
import { HostCapabilities } from "../interfaces";
165

17-
// Restore console.log after Gun modules are loaded
18-
setTimeout(() => {
19-
console.log = originalConsoleLog;
20-
}, 500);
21-
226
// Import Logger from dig-node if available, otherwise define a minimal interface
237
interface Logger {
248
debug(message: string, ...args: unknown[]): void;

0 commit comments

Comments
 (0)