UNPKG

@minimaltech/node-infra

Version:

Minimal Technology NodeJS Infrastructure - Loopback 4 Framework

141 lines 6.44 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseNetworkTcpClient = void 0; const base_helper_1 = require("../../base/base.helper"); const isEmpty_1 = __importDefault(require("lodash/isEmpty")); const DEFAULT_MAX_RETRY = 5; class BaseNetworkTcpClient extends base_helper_1.BaseHelper { constructor(opts) { var _a, _b, _c, _d, _e, _f, _g, _h; super({ scope: (_b = (_a = opts.scope) !== null && _a !== void 0 ? _a : opts.identifier) !== null && _b !== void 0 ? _b : BaseNetworkTcpClient.name, identifier: opts.identifier, }); this.retry = { maxReconnect: DEFAULT_MAX_RETRY, currentReconnect: 0, }; this.options = opts.options; this.retry = { maxReconnect: (_c = opts.maxRetry) !== null && _c !== void 0 ? _c : DEFAULT_MAX_RETRY, currentReconnect: 0, }; this.createClientFn = opts.createClientFn; this.onConnected = (_d = opts === null || opts === void 0 ? void 0 : opts.onConnected) !== null && _d !== void 0 ? _d : this.handleConnected; this.onData = (_e = opts === null || opts === void 0 ? void 0 : opts.onData) !== null && _e !== void 0 ? _e : this.handleData; this.onClosed = (_f = opts === null || opts === void 0 ? void 0 : opts.onClosed) !== null && _f !== void 0 ? _f : this.handleClosed; this.onError = (_g = opts === null || opts === void 0 ? void 0 : opts.onError) !== null && _g !== void 0 ? _g : this.handleError; this.reconnect = (_h = opts === null || opts === void 0 ? void 0 : opts.reconnect) !== null && _h !== void 0 ? _h : false; this.encoding = opts === null || opts === void 0 ? void 0 : opts.encoding; } getClient() { return this.client; } handleConnected() { this.logger.info('[handleConnected][%s] Connected to TCP Server | Options: %j', this.identifier, this.options); this.retry.currentReconnect = 0; } handleData(_opts) { } handleClosed() { this.logger.info('[handleClosed][%s] Closed connection TCP Server | Options: %j', this.identifier, this.options); } handleError(error) { this.logger.error('[handleError][%s] Connection error | Options: %j | Error: %s', this.identifier, this.options, error); if (!this.reconnect || this.retry.currentReconnect >= this.retry.maxReconnect) { return; } const { currentReconnect, maxReconnect } = this.retry; if (maxReconnect > -1 && currentReconnect >= maxReconnect) { this.logger.info('[handleData] Exceeded max retry to reconnect! Max: %d | Current: %d', maxReconnect, currentReconnect); return; } this.reconnectTimeout = setTimeout(() => { var _a; (_a = this.client) === null || _a === void 0 ? void 0 : _a.destroy(); this.client = null; this.logger.info('[handleClosed][%s] Retrying to establish TCP Connection | Options: %j', this.identifier, this.options); this.connect({ resetReconnectCounter: false }); this.retry.currentReconnect++; }, 5000); } connect(opts) { var _a; if (this.isConnected()) { this.logger.info('[connect][%s] NetworkTcpClient is already initialized!', this.identifier); return; } if ((0, isEmpty_1.default)(this.options)) { this.logger.info('[connect][%s] Cannot init TCP Client with null options', this.identifier); return; } this.logger.info('[connect][%s] New network tcp client | Options: %s', this.identifier, this.options); if (opts === null || opts === void 0 ? void 0 : opts.resetReconnectCounter) { this.retry.currentReconnect = 0; } if (this.client !== null && this.client !== undefined) { (_a = this.client) === null || _a === void 0 ? void 0 : _a.destroy(); this.client = null; } this.client = this.createClientFn(this.options, () => { var _a; if (!this.client) { this.logger.error('[createClientFn] Failed to initialize socket client!'); return; } (_a = this.onConnected) === null || _a === void 0 ? void 0 : _a.call(this, { client: this.client }); }); if (this.encoding) { this.client.setEncoding(this.encoding); } this.client.on('data', (message) => { this.onData({ identifier: this.identifier, message }); }); this.client.on('close', () => { var _a; if (!this.client) { return; } (_a = this.onClosed) === null || _a === void 0 ? void 0 : _a.call(this, { client: this.client }); }); this.client.on('error', error => { var _a; (_a = this.onError) === null || _a === void 0 ? void 0 : _a.call(this, error); }); } disconnect() { var _a; if (!this.client) { this.logger.info('[disconnect][%s] NetworkTcpClient is not initialized yet!', this.identifier); return; } (_a = this.client) === null || _a === void 0 ? void 0 : _a.destroy(); this.client = null; clearTimeout(this.reconnectTimeout); this.reconnectTimeout = null; this.logger.info('[disconnect][%s] NetworkTcpClient is destroyed!', this.identifier); } forceReconnect() { this.disconnect(); this.connect({ resetReconnectCounter: true }); } isConnected() { return this.client && this.client.readyState !== 'closed'; } emit(opts) { if (!this.client) { this.logger.info('[emit][%s] TPC Client is not configured yet!', this.identifier); return; } const { payload } = opts; if (!(payload === null || payload === void 0 ? void 0 : payload.length)) { this.logger.info('[emit][%s] Invalid payload to write to TCP Socket!', this.identifier); return; } this.client.write(payload); } } exports.BaseNetworkTcpClient = BaseNetworkTcpClient; //# sourceMappingURL=base-tcp-client.helper.js.map