From 1786e563806e2e12676dfa6d681f2c12b6afc9c0 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Mon, 6 Feb 2023 05:25:00 -0500 Subject: [PATCH] *Change createSocket to call setup which will fetch socket info from socketGetInfo and setup as mock objects --- src/index.ts | 48 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5913d8c..ce8242d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,15 +8,15 @@ export class SwarmClient extends Client { private useDefaultSwarm: boolean; private id: number = 0; - get swarm(): number | undefined { - return this.useDefaultSwarm ? undefined : this.id; - } - constructor(useDefaultDht = true) { super(); this.useDefaultSwarm = useDefaultDht; } + get swarm(): number | undefined { + return this.useDefaultSwarm ? undefined : this.id; + } + public async connect(pubkey: string | Uint8Array): Promise { if (typeof pubkey === "string") { const buf = hexToBuf(pubkey); @@ -39,8 +39,8 @@ export class SwarmClient extends Client { this.connectModule( "listenConnections", { swarm: this.swarm }, - (socketId: any) => { - this.emit("connection", createSocket(socketId)); + async (socketId: any) => { + this.emit("connection", await createSocket(socketId)); } ); } @@ -65,6 +65,12 @@ export class SwarmClient extends Client { } } +interface SocketRawStream { + remoteHost: string; + remotePort: number; + remoteFamily: string; +} + export class Socket extends Client { private id: number; private eventUpdates: { [event: string]: DataFn[] } = {}; @@ -73,6 +79,26 @@ export class Socket extends Client { super(); this.id = id; } + + private _remotePublicKey?: Uint8Array; + + get remotePublicKey(): Uint8Array { + return this._remotePublicKey as Uint8Array; + } + + private _rawStream?: Uint8Array; + + get rawStream(): Uint8Array { + return this._rawStream as Uint8Array; + } + + async setup() { + let info = await this.callModuleReturn("socketGetInfo", { id: this.id }); + + this._remotePublicKey = info.remotePublicKey; + this._rawStream = info.rawStream; + } + on>( event: T, fn: EventEmitter.EventListener, @@ -137,4 +163,12 @@ export class Socket extends Client { const MODULE = "_A7ClA0mSa1-Pg5c4V3C0H_fnhAFjgccITYT83Euc7t_9A"; export const createClient = factory(SwarmClient, MODULE); -const createSocket = factory(Socket, MODULE); + +const socketFactory = factory(Socket, MODULE); +const createSocket = async (...args: any): Promise => { + const socket = socketFactory(...args); + + await socket.setup(); + + return socket; +};