Compare commits
2 Commits
94e817f045
...
6099a6c4f6
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | 6099a6c4f6 | |
Derrick Hammer | 5c666d38c0 |
|
@ -39,9 +39,10 @@ class Peer {
|
||||||
async init() {
|
async init() {
|
||||||
const self = this;
|
const self = this;
|
||||||
let pipe;
|
let pipe;
|
||||||
|
const raw = await maybeGetAsyncProperty(self._peer.rawStream);
|
||||||
this._socket = new socket_js_1.default({
|
this._socket = new socket_js_1.default({
|
||||||
remoteAddress: self._peer.rawStream.remoteHost,
|
remoteAddress: raw.remoteHost,
|
||||||
remotePort: self._peer.rawStream.remotePort,
|
remotePort: raw.remotePort,
|
||||||
remotePublicKey: self._peer.remotePublicKey,
|
remotePublicKey: self._peer.remotePublicKey,
|
||||||
async write(data, cb) {
|
async write(data, cb) {
|
||||||
if (pipe) {
|
if (pipe) {
|
||||||
|
@ -90,3 +91,17 @@ class Peer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
exports.default = Peer;
|
exports.default = Peer;
|
||||||
|
async function maybeGetAsyncProperty(object) {
|
||||||
|
if (typeof object === "function") {
|
||||||
|
object = object();
|
||||||
|
}
|
||||||
|
if (isPromise(object)) {
|
||||||
|
object = await object;
|
||||||
|
}
|
||||||
|
return object;
|
||||||
|
}
|
||||||
|
function isPromise(obj) {
|
||||||
|
return (!!obj &&
|
||||||
|
(typeof obj === "object" || typeof obj === "function") &&
|
||||||
|
typeof obj.then === "function");
|
||||||
|
}
|
||||||
|
|
25
src/peer.ts
25
src/peer.ts
|
@ -101,9 +101,10 @@ export default class Peer {
|
||||||
async init() {
|
async init() {
|
||||||
const self = this;
|
const self = this;
|
||||||
let pipe;
|
let pipe;
|
||||||
|
const raw = await maybeGetAsyncProperty(self._peer.rawStream);
|
||||||
this._socket = new Socket({
|
this._socket = new Socket({
|
||||||
remoteAddress: self._peer.rawStream.remoteHost,
|
remoteAddress: raw.remoteHost,
|
||||||
remotePort: self._peer.rawStream.remotePort,
|
remotePort: raw.remotePort,
|
||||||
remotePublicKey: self._peer.remotePublicKey,
|
remotePublicKey: self._peer.remotePublicKey,
|
||||||
async write(data: any, cb: Function) {
|
async write(data: any, cb: Function) {
|
||||||
if (pipe) {
|
if (pipe) {
|
||||||
|
@ -157,3 +158,23 @@ export default class Peer {
|
||||||
await this._channel.open();
|
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"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue