From 21bcebb68986d9940bb8aeacbfddfab16fdea80b Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 27 Jul 2022 00:37:30 -0400 Subject: [PATCH] *Only choose a random index if we have more than 1 relay --- src/index.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 53f64c5..1167b95 100644 --- a/src/index.ts +++ b/src/index.ts @@ -113,11 +113,13 @@ export default class DHT { throw new Error("Failed to find an available relay"); } - const node = this._activeRelays.get( - [...this._activeRelays.keys()][ - await randomNumber(0, this._activeRelays.size - 1) - ] - ); + let index = 0; + + if (this._activeRelays.size > 1) { + index = await randomNumber(0, this._activeRelays.size - 1); + } + + const node = this._activeRelays.get([...this._activeRelays.keys()][index]); return node.connect(pubkey, options); }