*Need to async fetch rawStream

This commit is contained in:
Derrick Hammer 2023-04-09 12:15:23 -04:00
parent 94e817f045
commit 5c666d38c0
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 23 additions and 2 deletions

View File

@ -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<any>) {
return (
!!obj &&
(typeof obj === "object" || typeof obj === "function") &&
typeof obj.then === "function"
);
}