From 4290641f94a2be7b81be18897c24240df63aede6 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Wed, 31 Aug 2022 14:30:16 -0400 Subject: [PATCH] *Add API to create multiple networks *Add a default network --- src/index.ts | 70 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4fad3e9..31aae2a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,9 +10,25 @@ import type { RPCRequest, RPCResponse } from "@lumeweb/relay-types"; onmessage = handleMessage; -const network = new RpcNetwork(new DHT()); -const dht = network.dht as DHT; +function idFactory(start = 1, step = 1, limit = 2 ** 32) { + let id = start; + return function nextId() { + const nextId = id; + id += step; + if (id >= limit) id = start; + return nextId; + }; +} + +const nextId = idFactory(1); + +let defaultNetwork: RpcNetwork; + +const networkInstances = new Map(); + +addHandler("presentSeed", handlePresentSeed); +addHandler("createNetwork", handleCreateNetwork); addHandler("addRelay", handleAddRelay); addHandler("removeRelay", handleRemoveRelay); addHandler("clearRelays", handleClearRelays); @@ -21,6 +37,16 @@ addHandler("streamingQuery", handleStreamingQuery); addHandler("wisdomQuery", handleWisdomQuery); addHandler("ready", handleReady); +async function handlePresentSeed() { + if (!defaultNetwork) { + defaultNetwork = networkInstances.get(await createNetwork()) as RpcNetwork; + } +} + +async function handleCreateNetwork(aq: ActiveQuery) { + aq.respond(await createNetwork(false)); +} + async function handleAddRelay(aq: ActiveQuery) { const { pubkey = null } = aq.callerInput; @@ -29,9 +55,11 @@ async function handleAddRelay(aq: ActiveQuery) { return; } + const network = getNetwork(aq); + network.addRelay(pubkey); try { - await dht.addRelay(pubkey); + await network.dht.addRelay(pubkey); } catch (e: any) {} aq.respond(); @@ -45,13 +73,14 @@ function handleRemoveRelay(aq: ActiveQuery) { return; } - aq.respond(network.removeRelay(pubkey)); + aq.respond(getNetwork(aq).removeRelay(pubkey)); } async function handleClearRelays(aq: ActiveQuery) { + const network = getNetwork(aq); network.clearRelays(); - await dht.clearRelays(); + await network.dht.clearRelays(); aq.respond(); } @@ -76,6 +105,8 @@ async function handleSimpleQuery(aq: ActiveQuery) { return; } + const network = getNetwork(aq); + let resp: RPCResponse | null = null; try { @@ -126,6 +157,8 @@ async function handleStreamingQuery(aq: ActiveQuery) { return; } + const network = getNetwork(aq); + let resp: RPCResponse | null = null; try { @@ -161,6 +194,8 @@ async function handleWisdomQuery(aq: ActiveQuery) { return; } + const network = getNetwork(aq); + let resp: RPCResponse | null = null; try { @@ -185,6 +220,29 @@ async function handleWisdomQuery(aq: ActiveQuery) { } async function handleReady(aq: ActiveQuery) { - await network.ready; + await getNetwork(aq).ready; aq.respond(); } +async function createNetwork(def = true): Promise { + const dhtInstance = new RpcNetwork(new DHT(def)); + const id = nextId(); + networkInstances.set(id, dhtInstance); + + return id; +} + +function getNetwork(aq: ActiveQuery): RpcNetwork { + const { network = null } = aq.callerInput; + + if (!network) { + return defaultNetwork; + } + + if (!networkInstances.has(network)) { + const err = "Invalid network id"; + aq.reject(err); + throw err; + } + + return networkInstances.get(network) as RpcNetwork; +}