Skip to content

Commit 1a7a02a

Browse files
committed
added periodic rebroadcasting
Signed-off-by: Robert Gogete <[email protected]>
1 parent a35eba4 commit 1a7a02a

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ The package includes a complete example that automatically shares and discovers
7676
The example will:
7777
- Seed all `*.dig` files from `~/.dig` directory
7878
- Share their magnet URIs via Gun.js registry
79-
- Periodically discover magnet URIs from other peers
80-
- Download files that you don't already have
79+
- **Every 30 seconds:**
80+
- Rebroadcast all seeded magnet URIs (to keep them fresh in the registry)
81+
- Discover magnet URIs from other peers
82+
- Download files that you don't already have
8183
- Automatically seed downloaded files
8284

8385
You can create test files:

examples/nat-tools-example.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ async function seedAllDigFiles(natTools) {
9696
return seededFiles;
9797
}
9898

99+
/**
100+
* Rebroadcast all currently seeded magnet URIs
101+
*/
102+
async function rebroadcastMagnetUris(natTools) {
103+
try {
104+
const seededFiles = natTools.getSeededFiles();
105+
106+
if (seededFiles.size === 0) {
107+
logger.debug('No files to rebroadcast');
108+
return;
109+
}
110+
111+
logger.info(`📡 Rebroadcasting ${seededFiles.size} magnet URIs...`);
112+
113+
const count = await natTools.rebroadcastMagnetUris();
114+
115+
logger.info(`✅ Rebroadcast ${count} magnet URIs successfully`);
116+
} catch (error) {
117+
logger.error('❌ Error during rebroadcast:', error.message);
118+
}
119+
}
120+
99121
/**
100122
* Discover and download new files
101123
*/
@@ -238,12 +260,16 @@ async function main() {
238260
await seedAllDigFiles(natTools);
239261

240262
logger.info('='.repeat(60));
241-
logger.info('🔄 Starting periodic discovery...');
263+
logger.info('🔄 Starting periodic discovery and rebroadcast...');
242264
logger.info(' Press Ctrl+C to stop');
243265
logger.info('='.repeat(60));
244266

245-
// Start periodic discovery and download
246-
const discoveryInterval = setInterval(async () => {
267+
// Start periodic discovery, download, and rebroadcast
268+
const periodicInterval = setInterval(async () => {
269+
// Rebroadcast magnet URIs to keep them fresh
270+
await rebroadcastMagnetUris(natTools);
271+
272+
// Discover and download new files
247273
await discoverAndDownload(natTools);
248274
}, CONFIG.discoveryIntervalMs);
249275

@@ -253,7 +279,7 @@ async function main() {
253279
// Handle graceful shutdown
254280
process.on('SIGINT', async () => {
255281
logger.info('\n\n🛑 Shutting down gracefully...');
256-
clearInterval(discoveryInterval);
282+
clearInterval(periodicInterval);
257283

258284
try {
259285
await natTools.destroy();

src/nat-tools.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,41 @@ export class NatTools {
214214
return this.registry.isAvailable();
215215
}
216216

217+
/**
218+
* Rebroadcast all currently seeded magnet URIs to refresh their timestamps
219+
* This should be called periodically to keep magnet URIs fresh in the registry
220+
* @returns Number of magnet URIs successfully rebroadcast
221+
*/
222+
public async rebroadcastMagnetUris(): Promise<number> {
223+
if (!this.isInitialized) {
224+
throw new Error("NatTools not initialized. Call initialize() first.");
225+
}
226+
227+
if (this.seededMagnetUris.size === 0) {
228+
this.logger.debug("No magnet URIs to rebroadcast");
229+
return 0;
230+
}
231+
232+
this.logger.debug(`📡 Rebroadcasting ${this.seededMagnetUris.size} magnet URIs...`);
233+
234+
let successCount = 0;
235+
236+
for (const [filePath, magnetUri] of this.seededMagnetUris) {
237+
try {
238+
// Re-share the magnet URI to update its timestamp in the registry
239+
await this.registry.shareMagnetUri(magnetUri);
240+
successCount++;
241+
this.logger.debug(` ✅ Rebroadcast: ${path.basename(filePath)}`);
242+
} catch (error) {
243+
this.logger.warn(` ⚠️ Failed to rebroadcast ${path.basename(filePath)}:`, error);
244+
}
245+
}
246+
247+
this.logger.debug(`✅ Rebroadcast ${successCount}/${this.seededMagnetUris.size} magnet URIs`);
248+
249+
return successCount;
250+
}
251+
217252
/**
218253
* Clean up resources
219254
*/

0 commit comments

Comments
 (0)