2023-01-12 17:50:38 +00:00
|
|
|
"use strict";
|
|
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
|
|
};
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
|
const protomux_1 = __importDefault(require("protomux"));
|
|
|
|
const peer_js_1 = __importDefault(require("./peer.js"));
|
|
|
|
class Proxy {
|
|
|
|
_listen;
|
|
|
|
_autostart;
|
2023-03-15 12:14:29 +00:00
|
|
|
constructor({ swarm, protocol, onopen, onreceive, onsend, onclose, onchannel, listen = false, autostart = false, emulateWebsocket = false, createDefaultMessage = true, }) {
|
2023-01-12 17:50:38 +00:00
|
|
|
this._swarm = swarm;
|
|
|
|
this._protocol = protocol;
|
|
|
|
this._listen = listen;
|
|
|
|
this._autostart = autostart;
|
2023-02-26 03:21:39 +00:00
|
|
|
this._socketOptions = {
|
|
|
|
onopen,
|
|
|
|
onreceive,
|
|
|
|
onsend,
|
|
|
|
onclose,
|
2023-03-03 10:22:55 +00:00
|
|
|
onchannel,
|
2023-02-26 03:21:39 +00:00
|
|
|
emulateWebsocket,
|
2023-03-15 12:14:29 +00:00
|
|
|
createDefaultMessage,
|
2023-02-26 03:21:39 +00:00
|
|
|
};
|
2023-01-12 17:50:38 +00:00
|
|
|
this.init();
|
|
|
|
}
|
2023-04-15 23:45:35 +00:00
|
|
|
_socketOptions;
|
|
|
|
get socketOptions() {
|
|
|
|
return this._socketOptions;
|
|
|
|
}
|
2023-01-12 17:50:38 +00:00
|
|
|
_swarm;
|
|
|
|
get swarm() {
|
|
|
|
return this._swarm;
|
|
|
|
}
|
|
|
|
_protocol;
|
|
|
|
get protocol() {
|
|
|
|
return this._protocol;
|
|
|
|
}
|
|
|
|
handlePeer({ peer, muxer, ...options }) {
|
|
|
|
const conn = new peer_js_1.default({ proxy: this, peer, muxer, ...options });
|
|
|
|
conn.init();
|
|
|
|
}
|
|
|
|
_init() {
|
|
|
|
// Implement in subclasses
|
|
|
|
}
|
|
|
|
async init() {
|
|
|
|
if (this._listen) {
|
|
|
|
this._swarm.on("connection", this._handleConnection.bind(this));
|
|
|
|
}
|
|
|
|
await this._init();
|
|
|
|
}
|
|
|
|
_handleConnection(peer) {
|
|
|
|
const muxer = protomux_1.default.from(peer);
|
|
|
|
const handlePeer = this.handlePeer.bind(this, {
|
|
|
|
peer,
|
|
|
|
muxer,
|
|
|
|
...this._socketOptions,
|
|
|
|
});
|
|
|
|
if (this._autostart) {
|
|
|
|
handlePeer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
muxer.pair(this._protocol, handlePeer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.default = Proxy;
|