From ef76b3f3b6beee87f57d74addd0ca2ef2c517b6d Mon Sep 17 00:00:00 2001 From: Paul Sharypov Date: Wed, 1 Jun 2022 18:23:38 +0300 Subject: [PATCH] feat(events): Support of `paused` client event (#411) * feat: Added `paused` client event * fix(events): fixed 'invalid event' response on 'paused' request from client * fix(styles): fixed extra semicolon --- lib/common-node.js | 8 +++++--- lib/server/swarm.js | 11 +++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/common-node.js b/lib/common-node.js index b570f2b..dab4b67 100644 --- a/lib/common-node.js +++ b/lib/common-node.js @@ -11,18 +11,20 @@ exports.REMOVE_IPV4_MAPPED_IPV6_RE = /^::ffff:/ exports.CONNECTION_ID = Buffer.concat([toUInt32(0x417), toUInt32(0x27101980)]) exports.ACTIONS = { CONNECT: 0, ANNOUNCE: 1, SCRAPE: 2, ERROR: 3 } -exports.EVENTS = { update: 0, completed: 1, started: 2, stopped: 3 } +exports.EVENTS = { update: 0, completed: 1, started: 2, stopped: 3, paused: 4 } exports.EVENT_IDS = { 0: 'update', 1: 'completed', 2: 'started', - 3: 'stopped' + 3: 'stopped', + 4: 'paused' } exports.EVENT_NAMES = { update: 'update', completed: 'complete', started: 'start', - stopped: 'stop' + stopped: 'stop', + paused: 'pause' } /** diff --git a/lib/server/swarm.js b/lib/server/swarm.js index 8194270..c360213 100644 --- a/lib/server/swarm.js +++ b/lib/server/swarm.js @@ -47,6 +47,8 @@ class Swarm { self._onAnnounceCompleted(params, peer, id) } else if (params.event === 'update') { self._onAnnounceUpdate(params, peer, id) + } else if (params.event === 'paused') { + self._onAnnouncePaused(params, peer, id) } else { cb(new Error('invalid event')) return @@ -132,6 +134,15 @@ class Swarm { this.peers.set(id, peer) } + _onAnnouncePaused (params, peer, id) { + if (!peer) { + debug('unexpected `paused` event from peer that is not in swarm') + return this._onAnnounceStarted(params, peer, id) // treat as a start + } + + this._onAnnounceUpdate(params, peer, id) + } + _getPeers (numwant, ownPeerId, isWebRTC) { const peers = [] const ite = randomIterate(this.peers.keys)