From 5a49028c5a6f924d0ac763c8193d712b6540d86d Mon Sep 17 00:00:00 2001 From: rafapaezbas Date: Fri, 23 Jan 2026 00:41:05 +0100 Subject: [PATCH] tweak monitor for download range --- lib/monitor.js | 19 +++++++++++++++++-- test.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/monitor.js b/lib/monitor.js index da1b542..0f1f5be 100644 --- a/lib/monitor.js +++ b/lib/monitor.js @@ -53,6 +53,13 @@ module.exports = class Monitor extends ReadyResource { this.blobs.core.on('peer-remove', this._boundPeerUpdate) this.blobs.core.on('upload', this._boundOnUpload) this.blobs.core.on('download', this._boundOnDownload) + + if (!this.entry) { + this.drive.db.core.on('peer-add', this._boundPeerUpdate) + this.drive.db.core.on('peer-remove', this._boundPeerUpdate) + this.drive.db.core.on('upload', this._boundOnUpload) + this.drive.db.core.on('download', this._boundOnDownload) + } } async _close() { @@ -60,6 +67,14 @@ module.exports = class Monitor extends ReadyResource { this.blobs.core.off('peer-remove', this._boundPeerUpdate) this.blobs.core.off('upload', this._boundOnUpload) this.blobs.core.off('download', this._boundOnDownload) + + if (!this.entry) { + this.drive.db.core.off('peer-add', this._boundPeerUpdate) + this.drive.db.core.off('peer-remove', this._boundPeerUpdate) + this.drive.db.core.off('upload', this._boundOnUpload) + this.drive.db.core.off('download', this._boundOnDownload) + } + this.drive.monitors.delete(this) } @@ -88,8 +103,8 @@ module.exports = class Monitor extends ReadyResource { } _updateStats(speed, stats, index, bytes, from) { - if (!this.entry || this.closing) return - if (!isWithinRange(index, this.entry)) return + if (this.closing) return + if (this.entry && !isWithinRange(index, this.entry)) return if (!stats.startTime) stats.startTime = Date.now() diff --git a/test.js b/test.js index 083945d..705f78f 100644 --- a/test.js +++ b/test.js @@ -1787,6 +1787,34 @@ test('monitor is removed from the Set on close', async (t) => { t.is(drive.monitors.size, 0) }) +test('monitor range download', async (t) => { + const { corestore, drive, swarm, mirror } = await testenv(t) + swarm.on('connection', (conn) => corestore.replicate(conn)) + swarm.join(drive.discoveryKey, { server: true, client: false }) + await swarm.flush() + + mirror.swarm.on('connection', (conn) => mirror.corestore.replicate(conn)) + mirror.swarm.join(drive.discoveryKey, { server: false, client: true }) + await mirror.swarm.flush() + + await drive.put('/file-a', Buffer.alloc(1024)) + await drive.put('/file-b', Buffer.alloc(1024)) + await drive.put('/file-c', Buffer.alloc(1024)) + + await eventFlush() + + const monitor = mirror.drive.monitor('download-monitor') + await monitor.ready() + + const download = await mirror.drive.downloadRange([], [{ start: 0, end: 3 }]) + await download.done() + + t.is(monitor.downloadStats.peers, 1) + t.ok(monitor.downloadStats.speed > 0) + t.ok(monitor.downloadStats.blocks > 0) + t.ok(monitor.downloadStats.totalBytes, 3072) +}) + async function testenv(t) { const { teardown } = t