symmetry-core
Version:
Use this repository to become an inference provider on the Symmetry network programmatically.
110 lines (109 loc) • 3.92 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectionManager = void 0;
const chalk_1 = __importDefault(require("chalk"));
const hypercore_crypto_1 = __importDefault(require("hypercore-crypto"));
const hyperswarm_1 = __importDefault(require("hyperswarm"));
const package_json_1 = require("../package.json");
const logger_1 = require("./logger");
class ConnectionManager {
constructor(config) {
this._currentPeer = null;
this._heartbeatInterval = null;
this._isConnecting = false;
this._serverSwarm = null;
this._config = config;
}
get isConnected() {
return this._currentPeer !== null && this._currentPeer.writable;
}
get currentPeer() {
return this._currentPeer;
}
async connect() {
try {
if (this._serverSwarm) {
await this._serverSwarm.destroy();
}
logger_1.logger.info(`🔗 Connecting to server using symmetry-core version ${package_json_1.version}`);
this._serverSwarm = new hyperswarm_1.default(this._config.swarmOptions);
this._serverSwarm.join(hypercore_crypto_1.default.discoveryKey(this._config.serverKey), {
client: true,
server: false,
});
this._serverSwarm.flush();
this._serverSwarm.on("connection", this.handleConnection.bind(this));
this._serverSwarm.on("error", this.handleSwarmError.bind(this));
}
catch (error) {
this._isConnecting = false;
const err = error;
logger_1.logger.error(`Failed to create server connection: ${err.message}`);
this.handleDisconnection();
}
}
handleConnection(peer) {
this._isConnecting = false;
this._currentPeer = peer;
logger_1.logger.info(chalk_1.default.green("🔗 Connected to server."));
peer.on("close", () => {
if (!this._isConnecting) {
this.handleDisconnection();
}
});
peer.on("error", (err) => {
logger_1.logger.error(`Server connection error: ${err.message}`);
if (!this._isConnecting) {
this.handleDisconnection();
}
});
this.startHeartbeat(peer);
this._config.onConnection(peer);
}
handleSwarmError(error) {
logger_1.logger.error(`Swarm error: ${error.message}`);
this.handleDisconnection();
}
handleDisconnection() {
var _a, _b;
if (this._isConnecting)
return;
logger_1.logger.warning("📡 Disconnected from server");
this.cleanup();
(_b = (_a = this._config).onDisconnection) === null || _b === void 0 ? void 0 : _b.call(_a);
}
startHeartbeat(peer) {
if (this._heartbeatInterval) {
clearInterval(this._heartbeatInterval);
}
this._heartbeatInterval = setInterval(() => {
if (peer.writable) {
peer.write(JSON.stringify({ key: "heartbeat" }));
}
else {
this.handleDisconnection();
}
}, 10000);
}
cleanup() {
if (this._heartbeatInterval) {
clearInterval(this._heartbeatInterval);
this._heartbeatInterval = null;
}
if (this._currentPeer) {
this._currentPeer.destroy();
this._currentPeer = null;
}
}
async destroy() {
var _a;
this._isConnecting = false;
this.cleanup();
await ((_a = this._serverSwarm) === null || _a === void 0 ? void 0 : _a.destroy());
this._serverSwarm = null;
}
}
exports.ConnectionManager = ConnectionManager;