@camunda8/sdk
Version:
[](https://www.npmjs.com/package/@camunda8/sdk)
81 lines • 3.09 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.StatefulLogInterceptor = void 0;
const chalk_1 = __importDefault(require("chalk"));
const ZBLogger_1 = require("./ZBLogger");
class StatefulLogInterceptor {
constructor({ characteristics, logConfig, }) {
this.state = 'ERROR';
this.errors = [];
this.logs = [];
this.logError = (err) => this.error(err);
this.logInfo = (msg) => this.info(msg);
this.logDebug = (msg, ...args) => this.log.debug(msg, ...args);
this.logDirect = (msg) => this.log._tag === 'ZBCLIENT' && this.log.info(msg);
this.connectionError = () => {
this.state = 'ERROR';
};
this.ready = () => {
this.state = 'CONNECTED';
if (this.blocking) {
this.blocking = false;
this.emptyLogs();
}
};
this.wrap = (store) => (logmethod) => (msg) => {
if (this.blocking && this.state === 'ERROR') {
store.push(msg);
return;
}
logmethod(msg);
};
// tslint:disable-next-line: member-ordering
this.info = this.wrap(this.logs)((m) => this.log.info(m));
// tslint:disable-next-line: member-ordering
this.error = this.wrap(this.errors)((e) => this.log.error(e));
this.characteristics = characteristics;
this.log = new ZBLogger_1.ZBLogger(logConfig);
this.initialConnection = false;
this.blocking =
characteristics.startupTime > 0 && this.log.loglevel !== 'DEBUG';
if (this.blocking) {
this.logDirect(chalk_1.default.yellowBright('Authenticating client with Camunda Cloud...'));
this.blockingTimer = setTimeout(() => {
if (!this.blocking) {
return;
}
this.blocking = false;
return this.state === 'ERROR' ? this.emptyErrors() : this.emptyLogs();
}, this.characteristics.startupTime);
}
}
close() {
if (this.blockingTimer) {
clearTimeout(this.blockingTimer);
}
}
emptyErrors() {
if (this.errors.length === 0) {
return;
}
this.errors.forEach((err) => this.logError(err));
this.logDirect(chalk_1.default.redBright('Error connecting to Camunda Cloud.'));
this.errors = [];
}
emptyLogs() {
if (!this.initialConnection) {
this.initialConnection = true;
this.logDirect(chalk_1.default.greenBright('Established encrypted connection to Camunda Cloud.'));
}
if (this.logs.length === 0) {
return;
}
this.logs.forEach((msg) => this.logInfo(msg));
this.logs = [];
}
}
exports.StatefulLogInterceptor = StatefulLogInterceptor;
//# sourceMappingURL=StatefulLogInterceptor.js.map