From 1c167aad2ab26a25744b9b05b8a7037d897ebc2e Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 19 Mar 2023 14:55:23 -0400 Subject: [PATCH] *modify connect to return an existing connection if it exists, and if not, throw a not implemented error --- package.json | 2 ++ src/index.ts | 14 +++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 36b67ec..9015fe5 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,12 @@ "@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git", "@noble/hashes": "^1.2.0", "@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git", + "b4a": "^1.6.3", "backoff.js": "^1.0.4", "eventemitter3": "^5.0.0" }, "devDependencies": { + "@types/b4a": "^1.6.0", "@types/node": "^18.13.0", "prettier": "^2.8.4", "pretty": "^2.0.0", diff --git a/src/index.ts b/src/index.ts index 9981094..f2af4c3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,6 +2,7 @@ import { Buffer } from "buffer"; import { Client, factory } from "@lumeweb/libkernel-universal"; import { DataFn, ErrTuple, hexToBuf } from "@siaweb/libweb"; import { blake2b } from "@noble/hashes/blake2b"; +import b4a from "b4a"; import type { EventEmitter } from "eventemitter3"; @@ -56,12 +57,15 @@ export class SwarmClient extends Client { pubkey = this.handleErrorOrReturn(buf); } - const resp = this.callModuleReturn("connect", { - pubkey, - swarm: this.swarm, - }) as any; + let existing = Array.from(this._sockets.values()).filter((socket) => { + return b4a.equals(socket.remotePublicKey, pubkey as Uint8Array); + }); - return createSocket(resp.id); + if (existing.length) { + return existing[0] as Socket; + } + + throw new Error("not implemented"); } async init(): Promise { return await this.callModuleReturn("init", { swarm: this.swarm });