Skip to content

Commit b5ba8ae

Browse files
committed
Removed timeouts as they are handled by webtorrent and removed gunjs logs
Signed-off-by: Robert Gogete <[email protected]>
1 parent 83ef9e6 commit b5ba8ae

File tree

4 files changed

+29
-149
lines changed

4 files changed

+29
-149
lines changed

src/client.ts

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ interface Logger {
1515
}
1616

1717
export interface DownloadOptions {
18-
timeout?: number;
1918
onProgress?: (downloaded: number, total: number) => void;
2019
maxFileSizeBytes?: number; // Maximum allowed file size for downloads (in bytes)
2120
}
2221

2322
export interface FileClientOptions {
2423
peers?: string[]; // Gun.js peer URLs
2524
namespace?: string; // Gun.js namespace
26-
timeout?: number; // Download timeout
2725
logger?: Logger; // Optional logger for debug output
2826
}
2927

@@ -37,7 +35,6 @@ export class FileClient implements IFileClient {
3735
this.options = {
3836
peers: options.peers || ["http://dig-relay-prod.eba-2cmanxbe.us-east-1.elasticbeanstalk.com/gun"],
3937
namespace: options.namespace || "dig-nat-tools",
40-
timeout: options.timeout || 30000,
4138
};
4239

4340
// Create a default logger that only shows warnings and errors if none provided
@@ -311,35 +308,12 @@ export class FileClient implements IFileClient {
311308
return;
312309
}
313310

314-
// Timeout handler with more details
315-
const timeoutMs = options.timeout || this.options.timeout;
316-
const timeout = setTimeout(() => {
317-
this.logger.error("⏰ WebTorrent download timeout:", {
318-
timeoutMs: timeoutMs,
319-
torrentName: torrent?.name,
320-
torrentLength: torrent?.length,
321-
filesCount: torrent?.files?.length,
322-
peersCount: torrent?.numPeers,
323-
downloaded: torrent?.downloaded,
324-
uploaded: torrent?.uploaded,
325-
downloadSpeed: torrent?.downloadSpeed,
326-
progress: torrent?.progress,
327-
magnetUri: magnetUri.substring(0, 100) + '...'
328-
});
329-
330-
if (torrent) {
331-
torrent.destroy();
332-
}
333-
reject(new Error(`WebTorrent download timeout after ${timeoutMs}ms`));
334-
}, timeoutMs);
335-
336311
torrent.on("ready", () => {
337312
this.logger.debug(
338313
`✅ Torrent ready! File: ${torrent!.name}, Size: ${torrent!.length} bytes, Files: ${torrent!.files.length}`
339314
);
340315

341316
if (torrent!.files.length === 0) {
342-
clearTimeout(timeout);
343317
torrent!.destroy();
344318
this.logger.error("❌ No files in torrent", {
345319
name: torrent!.name,
@@ -352,7 +326,6 @@ export class FileClient implements IFileClient {
352326

353327
// Check file size against maximum allowed size
354328
if (options.maxFileSizeBytes && torrent!.length > options.maxFileSizeBytes) {
355-
clearTimeout(timeout);
356329
torrent!.destroy();
357330
const fileSizeMB = (torrent!.length / (1024 * 1024)).toFixed(2);
358331
const maxSizeMB = (options.maxFileSizeBytes / (1024 * 1024)).toFixed(2);
@@ -376,7 +349,6 @@ export class FileClient implements IFileClient {
376349
});
377350

378351
stream.on("end", () => {
379-
clearTimeout(timeout);
380352
const buffer = Buffer.concat(chunks);
381353
this.logger.debug(
382354
`✅ WebTorrent download completed! ${buffer.length} bytes`
@@ -388,7 +360,6 @@ export class FileClient implements IFileClient {
388360
});
389361

390362
stream.on("error", (error: unknown) => {
391-
clearTimeout(timeout);
392363
torrent!.destroy();
393364
this.logger.error("❌ Stream error during download:", {
394365
...this.serializeError(error),
@@ -401,8 +372,7 @@ export class FileClient implements IFileClient {
401372

402373
// Enhanced torrent error handling
403374
torrent.on("error", (error: unknown) => {
404-
clearTimeout(timeout);
405-
this.logger.error(`❌ WebTorrent torrent error:`, {
375+
this.logger.debug(`❌ WebTorrent torrent error:`, {
406376
...this.serializeError(error),
407377
magnetUri: magnetUri.substring(0, 100) + '...',
408378
infoHash: torrent?.infoHash,
@@ -413,13 +383,13 @@ export class FileClient implements IFileClient {
413383

414384
// Add additional torrent event listeners for debugging
415385
torrent.on("warning", (warning: unknown) => {
416-
this.logger.warn("⚠️ WebTorrent warning:", {
386+
this.logger.debug("⚠️ WebTorrent warning:", {
417387
...this.serializeError(warning)
418388
});
419389
});
420390

421391
torrent.on("noPeers", () => {
422-
this.logger.warn("⚠️ No peers found for torrent", {
392+
this.logger.debug("⚠️ No peers found for torrent", {
423393
magnetUri: magnetUri.substring(0, 100) + '...',
424394
infoHash: torrent?.infoHash
425395
});
@@ -492,7 +462,7 @@ export class FileClient implements IFileClient {
492462
url: string,
493463
options: DownloadOptions = {}
494464
): Promise<Buffer> {
495-
const { timeout = 30000, onProgress } = options;
465+
const { onProgress } = options;
496466

497467
return new Promise<Buffer>((resolve, reject) => {
498468
// Parse the URL
@@ -503,7 +473,6 @@ export class FileClient implements IFileClient {
503473

504474
const req = protocol.get(
505475
url,
506-
{ timeout },
507476
(res: http.IncomingMessage) => {
508477
if (res.statusCode !== 200) {
509478
return reject(
@@ -538,11 +507,6 @@ export class FileClient implements IFileClient {
538507
req.on("error", (err: Error) => {
539508
reject(err);
540509
});
541-
542-
req.on("timeout", () => {
543-
req.destroy();
544-
reject(new Error("Download timed out"));
545-
});
546510
});
547511
}
548512

@@ -554,10 +518,8 @@ export class FileClient implements IFileClient {
554518
*/
555519
public static async downloadAsStreamStatic(
556520
url: string,
557-
options: DownloadOptions = {}
521+
_options: DownloadOptions = {}
558522
): Promise<Readable> {
559-
const { timeout = 30000 } = options;
560-
561523
return new Promise<Readable>((resolve, reject) => {
562524
// Parse the URL
563525
const parsedUrl = new URL(url);
@@ -567,7 +529,6 @@ export class FileClient implements IFileClient {
567529

568530
const req = protocol.get(
569531
url,
570-
{ timeout },
571532
(res: http.IncomingMessage) => {
572533
if (res.statusCode !== 200) {
573534
return reject(
@@ -584,11 +545,6 @@ export class FileClient implements IFileClient {
584545
req.on("error", (err: Error) => {
585546
reject(err);
586547
});
587-
588-
req.on("timeout", () => {
589-
req.destroy();
590-
reject(new Error("Download timed out"));
591-
});
592548
});
593549
}
594550

@@ -636,11 +592,6 @@ export class FileClient implements IFileClient {
636592
req.on("error", () => {
637593
resolve(false);
638594
});
639-
640-
req.on("timeout", () => {
641-
req.destroy();
642-
resolve(false);
643-
});
644595
} catch {
645596
resolve(false);
646597
}

src/host.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -455,17 +455,12 @@ export class FileHost implements IFileHost {
455455
throw new Error("WebTorrent client not initialized");
456456
}
457457

458-
return new Promise((resolve, reject) => {
459-
const timeout = setTimeout(() => {
460-
reject(new Error("WebTorrent initialization timeout"));
461-
}, 10000); // 10 second timeout
462-
458+
return new Promise((resolve) => {
463459
// WebTorrent is ready when it's initialized and can accept operations
464460
// We'll give it a small delay to ensure it's fully initialized
465461
this.logger.debug(`⏳ Waiting for WebTorrent client to be ready...`);
466462

467463
setTimeout(() => {
468-
clearTimeout(timeout);
469464
this.logger.debug(`🎯 WebTorrent client is ready`);
470465
resolve();
471466
}, 1000); // Give WebTorrent 1 second to initialize properly
@@ -586,13 +581,8 @@ export class FileHost implements IFileHost {
586581
this.logger.debug(`🔄 Starting WebTorrent seeding for ${filename}...`);
587582

588583
// Seed the file from its original location and wait for the torrent to be ready
589-
await new Promise<void>((resolve, reject) => {
590-
const seedTimeout = setTimeout(() => {
591-
reject(new Error("WebTorrent seeding timeout"));
592-
}, 30000); // 30 second timeout for seeding
593-
584+
await new Promise<void>((resolve) => {
594585
this.webTorrentClient!.seed(filePath, (torrent) => {
595-
clearTimeout(seedTimeout);
596586
const magnetURI = torrent.magnetURI;
597587
this.magnetUris.set(filename, magnetURI);
598588
this.logger.debug(`🧲 WebTorrent seeding started for ${filename}`);

src/registry/gun-registry.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1+
// 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+
113
import Gun from "gun";
214
import "gun/lib/webrtc.js";
315
import { HostCapabilities } from "../interfaces";
416

17+
// Restore console.log after Gun modules are loaded
18+
setTimeout(() => {
19+
console.log = originalConsoleLog;
20+
}, 500);
21+
522
// Import Logger from dig-node if available, otherwise define a minimal interface
623
interface Logger {
724
debug(message: string, ...args: unknown[]): void;
@@ -80,6 +97,7 @@ export class GunRegistry {
8097
radisk: false,
8198
axe: false,
8299
});
100+
83101
this.isGunAvailable = true;
84102
this.logger.debug("Gun.js registry initialized with WebRTC and mesh networking");
85103
this.logger.debug(`🔧 WebRTC enabled with ${this.options.webrtc?.iceServers?.length || 0} ICE servers`);

0 commit comments

Comments
 (0)