@hashgraph/hedera-local
Version:
Developer tooling for running Local Hedera Network (Consensus + Mirror Nodes).
114 lines • 5.28 kB
JavaScript
;
// SPDX-License-Identifier: Apache-2.0
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConnectionService = void 0;
const net_1 = __importDefault(require("net"));
const LoggerService_1 = require("./LoggerService");
const ServiceLocator_1 = require("./ServiceLocator");
const CLIService_1 = require("./CLIService");
const LocalNodeErrors_1 = require("../Errors/LocalNodeErrors");
const debounce_1 = __importDefault(require("../utils/debounce"));
/**
* ConnectionService is a service class that handles network connections.
* It implements the IService interface.
* It uses the 'net' module to create connections and check their status.
*
* @class
* @public
*/
class ConnectionService {
/**
* Constructs a new instance of the ConnectionService.
* Initializes the logger and CLI service, and logs the initialization of the connection service.
*/
constructor() {
this.serviceName = ConnectionService.name;
this.logger = ServiceLocator_1.ServiceLocator.Current.get(LoggerService_1.LoggerService.name);
this.cliService = ServiceLocator_1.ServiceLocator.Current.get(CLIService_1.CLIService.name);
this.logger.trace('Connection Service Initialized!', this.serviceName);
this.debouncedErrorLog = (0, debounce_1.default)((message) => {
this.logger.info(message, this.serviceName);
}, 5000);
}
/**
* Waits for the local node to fire up by continuously trying to establish a connection.
* If the connection is not ready after 100 retries, it throws a CONNECTION_ERROR.
*
* @param {number} port - The port to connect to.
* @throws CONNECTION_ERROR if the port is not ready after a certain number of retries.
* @returns {Promise<void>} A promise that resolves when the port is ready for connection.
* @public
*/
waitForFiringUp(port, serviceName) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const { host } = this.cliService.getCurrentArgv();
let isReady = false;
// this means that we wait around 100 seconds, normal consensus node startup takes around 60 seconds
let retries = parseInt((_a = process.env.FIRING_UP_RETRY_ATTEMPTS) !== null && _a !== void 0 ? _a : '100');
while (!isReady) {
net_1.default
.createConnection(port, host)
.on('data', () => {
isReady = true;
})
.on('error', (err) => {
if (err.code === 'ECONNREFUSED') {
this.debouncedErrorLog(`${serviceName} not yet available at: ${host}:${port}. Retrying...`);
}
else {
this.logger.error(err.message, this.serviceName);
}
});
retries--;
yield new Promise((r) => setTimeout(r, 100));
if (retries < 0) {
throw LocalNodeErrors_1.Errors.CONNECTION_ERROR(port);
}
}
});
}
/**
* Checks the connection to the local node.
* If the connection is not established within a timeout of 3000ms, it rejects the promise with 'timeout'.
* If the connection is established, it resolves the promise and ends the socket.
* If there is an error during the connection, it rejects the promise with the error.
*
* @param {number} port - The port to connect to.
* @public
* @returns {Promise<void>} - A promise that resolves when the connection is established, and rejects when there is an error or timeout.
*/
checkConnection(port) {
const { host } = this.cliService.getCurrentArgv();
return new Promise((resolve, reject) => {
const timeout = 3000;
const timer = setTimeout(() => {
reject('timeout');
socket.end();
}, timeout);
let socket = net_1.default.createConnection(port, host, () => {
clearTimeout(timer);
resolve();
socket.end();
});
socket.on('error', (err) => {
clearTimeout(timer);
reject(err);
});
});
}
}
exports.ConnectionService = ConnectionService;
//# sourceMappingURL=ConnectionService.js.map