UNPKG

winston-openobserve

Version:
127 lines 6.54 kB
"use strict"; var _Sender_instances, _Sender_options, _Sender_req, _Sender_data, _Sender_tmpData, _Sender_timer, _Sender_sending, _Sender_pushBatch, _Sender_batchSend, _Sender_send, _Sender_swapData, _Sender_cleanTmpData; Object.defineProperty(exports, "__esModule", { value: true }); exports.Sender = void 0; const tslib_1 = require("tslib"); const ky_1 = tslib_1.__importDefault(require("ky")); class Sender { constructor(options) { _Sender_instances.add(this); _Sender_options.set(this, void 0); _Sender_req.set(this, void 0); _Sender_data.set(this, void 0); _Sender_tmpData.set(this, void 0); _Sender_timer.set(this, void 0); _Sender_sending.set(this, void 0); _Sender_swapData.set(this, () => { const tmpData = Object.values(tslib_1.__classPrivateFieldGet(this, _Sender_tmpData, "f")); if (Array.isArray(tmpData) && tmpData.length > 0) { tslib_1.__classPrivateFieldSet(this, _Sender_data, tslib_1.__classPrivateFieldGet(this, _Sender_tmpData, "f"), "f"); tslib_1.__classPrivateFieldGet(this, _Sender_instances, "m", _Sender_cleanTmpData).call(this); } else { this.clean(); } }); tslib_1.__classPrivateFieldSet(this, _Sender_options, options, "f"); tslib_1.__classPrivateFieldSet(this, _Sender_data, {}, "f"); tslib_1.__classPrivateFieldSet(this, _Sender_tmpData, {}, "f"); tslib_1.__classPrivateFieldSet(this, _Sender_sending, false, "f"); const { basicAuth } = options; let authVal; if (basicAuth) { authVal = 'Basic ' + Buffer.from(`${basicAuth.username}:${basicAuth.password}`).toString('base64'); } tslib_1.__classPrivateFieldSet(this, _Sender_req, ky_1.default.create({ prefixUrl: options.host, timeout: options.timeout, headers: Object.assign({}, { contentType: 'application/json' }, options.headers, { Authorization: authVal, }), }), "f"); const interval = Number(tslib_1.__classPrivateFieldGet(this, _Sender_options, "f").interval); tslib_1.__classPrivateFieldSet(this, _Sender_timer, setInterval(tslib_1.__classPrivateFieldGet(this, _Sender_instances, "m", _Sender_batchSend).bind(this), interval), "f"); } push(entity, bulk) { return tslib_1.__awaiter(this, void 0, void 0, function* () { let { orgId, streamName } = entity; let slug = `${orgId}/${streamName}`; if (bulk) { tslib_1.__classPrivateFieldGet(this, _Sender_instances, "m", _Sender_pushBatch).call(this, tslib_1.__classPrivateFieldGet(this, _Sender_sending, "f") ? tslib_1.__classPrivateFieldGet(this, _Sender_tmpData, "f") : tslib_1.__classPrivateFieldGet(this, _Sender_data, "f"), slug, entity); } else { yield tslib_1.__classPrivateFieldGet(this, _Sender_instances, "m", _Sender_send).call(this, orgId, streamName, [entity]); } }); } clean() { tslib_1.__classPrivateFieldSet(this, _Sender_data, null, "f"); tslib_1.__classPrivateFieldSet(this, _Sender_data, {}, "f"); } close() { console.debug('winston-openobserve closed'); this.clean(); clearInterval(tslib_1.__classPrivateFieldGet(this, _Sender_timer, "f")); } } exports.Sender = Sender; _Sender_options = new WeakMap(), _Sender_req = new WeakMap(), _Sender_data = new WeakMap(), _Sender_tmpData = new WeakMap(), _Sender_timer = new WeakMap(), _Sender_sending = new WeakMap(), _Sender_swapData = new WeakMap(), _Sender_instances = new WeakSet(), _Sender_pushBatch = function _Sender_pushBatch(data, slug, entity) { if (Array.isArray(data[slug])) { data[slug].push(entity); } else { data[slug] = [entity]; } }, _Sender_batchSend = function _Sender_batchSend() { var _a, e_1, _b, _c; return tslib_1.__awaiter(this, void 0, void 0, function* () { try { try { for (var _d = true, _e = tslib_1.__asyncValues(Object.entries(tslib_1.__classPrivateFieldGet(this, _Sender_data, "f"))), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) { _c = _f.value; _d = false; const [key, value] = _c; const [orgId, streamName] = key.split('/'); yield tslib_1.__classPrivateFieldGet(this, _Sender_instances, "m", _Sender_send).call(this, orgId, streamName, value); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_1) throw e_1.error; } } } catch (error) { console.error(error); } }); }, _Sender_send = function _Sender_send(orgId, streamName, body) { return tslib_1.__awaiter(this, void 0, void 0, function* () { tslib_1.__classPrivateFieldSet(this, _Sender_sending, true, "f"); return tslib_1.__classPrivateFieldGet(this, _Sender_req, "f") .post(`api/${orgId}/${streamName}/_json`, { json: body, }) .then(tslib_1.__classPrivateFieldGet(this, _Sender_swapData, "f")) .catch((err) => { var _a, _b, _c, _d, _e; console.error(err); if (((_a = err === null || err === void 0 ? void 0 : err.cause) === null || _a === void 0 ? void 0 : _a.code) === 'ECONNREFUSED') { (_c = (_b = tslib_1.__classPrivateFieldGet(this, _Sender_options, "f")).onConnectionError) === null || _c === void 0 ? void 0 : _c.call(_b, err, this.close.bind(this)); } tslib_1.__classPrivateFieldGet(this, _Sender_options, "f").cleanOnRequestError && this.clean(); (_e = (_d = tslib_1.__classPrivateFieldGet(this, _Sender_options, "f")).onRequestError) === null || _e === void 0 ? void 0 : _e.call(_d, err); }) .finally(() => { tslib_1.__classPrivateFieldSet(this, _Sender_sending, false, "f"); }); }); }, _Sender_cleanTmpData = function _Sender_cleanTmpData() { tslib_1.__classPrivateFieldSet(this, _Sender_tmpData, null, "f"); tslib_1.__classPrivateFieldSet(this, _Sender_tmpData, {}, "f"); }; //# sourceMappingURL=sender.js.map