*modify connect to return an existing connection if it exists, and if not, throw a not implemented error

This commit is contained in:
Derrick Hammer 2023-03-19 14:55:23 -04:00
parent bdf0cb705c
commit 1c167aad2a
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 11 additions and 5 deletions

View File

@ -7,10 +7,12 @@
"@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git", "@lumeweb/libkernel-universal": "git+https://git.lumeweb.com/LumeWeb/libkernel-universal.git",
"@noble/hashes": "^1.2.0", "@noble/hashes": "^1.2.0",
"@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git", "@siaweb/libweb": "git+https://git.lumeweb.com/LumeWeb/libsiaweb.git",
"b4a": "^1.6.3",
"backoff.js": "^1.0.4", "backoff.js": "^1.0.4",
"eventemitter3": "^5.0.0" "eventemitter3": "^5.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/b4a": "^1.6.0",
"@types/node": "^18.13.0", "@types/node": "^18.13.0",
"prettier": "^2.8.4", "prettier": "^2.8.4",
"pretty": "^2.0.0", "pretty": "^2.0.0",

View File

@ -2,6 +2,7 @@ import { Buffer } from "buffer";
import { Client, factory } from "@lumeweb/libkernel-universal"; import { Client, factory } from "@lumeweb/libkernel-universal";
import { DataFn, ErrTuple, hexToBuf } from "@siaweb/libweb"; import { DataFn, ErrTuple, hexToBuf } from "@siaweb/libweb";
import { blake2b } from "@noble/hashes/blake2b"; import { blake2b } from "@noble/hashes/blake2b";
import b4a from "b4a";
import type { EventEmitter } from "eventemitter3"; import type { EventEmitter } from "eventemitter3";
@ -56,12 +57,15 @@ export class SwarmClient extends Client {
pubkey = this.handleErrorOrReturn(buf); pubkey = this.handleErrorOrReturn(buf);
} }
const resp = this.callModuleReturn("connect", { let existing = Array.from(this._sockets.values()).filter((socket) => {
pubkey, return b4a.equals(socket.remotePublicKey, pubkey as Uint8Array);
swarm: this.swarm, });
}) as any;
return createSocket(resp.id); if (existing.length) {
return existing[0] as Socket;
}
throw new Error("not implemented");
} }
async init(): Promise<ErrTuple> { async init(): Promise<ErrTuple> {
return await this.callModuleReturn("init", { swarm: this.swarm }); return await this.callModuleReturn("init", { swarm: this.swarm });