From 10d309a8d6eb1e005dfa78cd93f20d30c5eea50b Mon Sep 17 00:00:00 2001 From: Daniel Lugo Date: Tue, 14 Sep 2021 13:29:28 -0400 Subject: [PATCH] Better logging for connected peers --- utils/GunSmith/GunSmith.js | 3 +++ utils/GunSmith/GunT.ts | 17 +++++++++++++++++ utils/GunSmith/gun.js | 13 +++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/utils/GunSmith/GunSmith.js b/utils/GunSmith/GunSmith.js index 43f0dd2c..e5c302ba 100644 --- a/utils/GunSmith/GunSmith.js +++ b/utils/GunSmith/GunSmith.js @@ -268,6 +268,9 @@ function createReplica(path, afterMap = false) { // TODO } }, + back() { + throw new Error('Do not use back() on a GunSmith node.') + }, get(key) { if (afterMap) { throw new Error( diff --git a/utils/GunSmith/GunT.ts b/utils/GunSmith/GunT.ts index d62ea531..47147da3 100644 --- a/utils/GunSmith/GunT.ts +++ b/utils/GunSmith/GunT.ts @@ -60,6 +60,23 @@ namespace GunT { export interface GUNNode { _: Soul + /** + * Used only inside the subprocess. + */ + back( + path: 'opt' + ): { + peers: Record< + string, + { + url: string + id: string + wire?: { + readyState: number + } + } + > + } get(key: string): GUNNode load(this: GUNNode, cb?: LoadListener): GUNNode map(): GUNNode diff --git a/utils/GunSmith/gun.js b/utils/GunSmith/gun.js index 812d89a8..3b728c05 100644 --- a/utils/GunSmith/gun.js +++ b/utils/GunSmith/gun.js @@ -71,9 +71,18 @@ const handleMsg = msg => { } if (msg.type === 'init') { gun = /** @type {any} */ (new Gun(msg.opts)) + + let lastPeers = '' setInterval(() => { - // @ts-expect-error - console.log(Object.keys(gun.back('opt').peers)) + const newPeers = JSON.stringify( + Object.values(gun.back('opt').peers) + .filter(p => p.wire && p.wire.readyState) + .map(p => p.url) + ) + if (newPeers !== lastPeers) { + console.log('Connected peers:', newPeers) + lastPeers = newPeers + } }, 2000) user = gun.user() }