From 5c666d38c0d352b321a9c93bec4e336929bd72fd Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sun, 9 Apr 2023 12:15:23 -0400 Subject: [PATCH] *Need to async fetch rawStream --- src/peer.ts | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/peer.ts b/src/peer.ts index b82c807..a16d4cf 100644 --- a/src/peer.ts +++ b/src/peer.ts @@ -101,9 +101,10 @@ export default class Peer { async init() { const self = this; let pipe; + const raw = await maybeGetAsyncProperty(self._peer.rawStream); this._socket = new Socket({ - remoteAddress: self._peer.rawStream.remoteHost, - remotePort: self._peer.rawStream.remotePort, + remoteAddress: raw.remoteHost, + remotePort: raw.remotePort, remotePublicKey: self._peer.remotePublicKey, async write(data: any, cb: Function) { if (pipe) { @@ -157,3 +158,23 @@ export default class Peer { await this._channel.open(); } } + +async function maybeGetAsyncProperty(object: any) { + if (typeof object === "function") { + object = object(); + } + + if (isPromise(object)) { + object = await object; + } + + return object; +} + +function isPromise(obj: Promise) { + return ( + !!obj && + (typeof obj === "object" || typeof obj === "function") && + typeof obj.then === "function" + ); +}