libhyperproxy/dist/peer.js

59 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-01-12 17:50:38 +00:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Peer {
_proxy;
_peer;
_muxer;
_onopen;
_onreceive;
_onsend;
_onclose;
2023-03-03 10:22:55 +00:00
_onchannel;
2023-02-26 03:16:59 +00:00
_emulateWebsocket;
2023-04-16 02:17:57 +00:00
constructor({ proxy, peer, muxer, onopen, onreceive, onsend, onclose, onchannel, emulateWebsocket = false, }) {
2023-01-12 17:50:38 +00:00
this._proxy = proxy;
this._peer = peer;
this._muxer = muxer;
2023-03-02 10:37:30 +00:00
this._onopen = onopen?.bind(undefined, this);
this._onreceive = onreceive?.bind(undefined, this);
this._onsend = onsend?.bind(undefined, this);
this._onclose = onclose?.bind(undefined, this);
2023-03-03 10:22:55 +00:00
this._onchannel = onchannel?.bind(undefined, this);
2023-02-26 03:16:59 +00:00
this._emulateWebsocket = emulateWebsocket;
2023-01-12 17:50:38 +00:00
}
2023-03-05 08:00:28 +00:00
_socket;
get socket() {
return this._socket;
}
2023-03-02 10:41:30 +00:00
_channel;
get channel() {
return this._channel;
}
2023-04-16 02:17:57 +00:00
async initChannel() {
2023-01-12 17:50:38 +00:00
const self = this;
2023-04-09 00:56:45 +00:00
this._channel = await this._muxer.createChannel({
2023-01-12 17:50:38 +00:00
protocol: this._proxy.protocol,
2023-04-16 11:11:59 +00:00
onopen: async (m) => {
await this.handleChannelOnOpen(m);
// @ts-ignore
await this._onopen?.(this._channel);
},
onclose: async (socket) => {
await this.handleChannelOnClose(socket);
// @ts-ignore
await this._onclose?.();
},
2023-01-12 17:50:38 +00:00
});
2023-04-16 02:17:57 +00:00
await this.initMessages();
2023-03-03 10:22:55 +00:00
await this._onchannel?.(this._channel);
2023-03-02 10:26:40 +00:00
await this._channel.open();
2023-04-16 11:11:59 +00:00
this._proxy.emit("peerChannelOpen", this);
2023-01-12 17:50:38 +00:00
}
2023-04-16 02:17:57 +00:00
async init() {
await this.initSocket();
await this.initChannel();
2023-04-09 16:15:38 +00:00
}
2023-04-16 02:17:57 +00:00
async initMessages() { }
2023-04-09 16:15:38 +00:00
}
2023-04-16 02:17:57 +00:00
exports.default = Peer;