UNPKG

logsdx

Version:

log streaming with dx on the 🧠

127 lines (123 loc) • 3.82 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } var _class;// src/logenhancer.ts var LogEnhancer = (_class = class { __init() {this.plugins = []} __init2() {this.parsers = []} __init3() {this.clients = []} constructor(options = {}) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);_class.prototype.__init3.call(this); this.debug = _nullishCoalesce(options.debug, () => ( false)); if (options.plugins) { this.loadPlugins(options.plugins); } if (options.parsers) { this.loadParsers(options.parsers); } if (options.clients) { this.loadClients(options.clients); } } async loadPlugins(plugins) { for (const plugin of plugins) { if (typeof plugin === "string") { try { const mod = await Promise.resolve().then(() => _interopRequireWildcard(require(plugin))); this.use(mod.default); } catch (err) { if (this.debug) { console.error(`Failed to load plugin: ${plugin}`, err); } } } else { this.use(plugin); } } } async loadParsers(parsers) { for (const parser of parsers) { if (typeof parser === "string") { try { const mod = await Promise.resolve().then(() => _interopRequireWildcard(require(parser))); this.addParser(mod.default); } catch (err) { if (this.debug) { console.error(`Failed to load parser: ${parser}`, err); } } } else { this.addParser(parser); } } } async loadClients(clients) { for (const client of clients) { if (typeof client === "string") { try { const mod = await Promise.resolve().then(() => _interopRequireWildcard(require(client))); this.addClient(mod.default); } catch (err) { if (this.debug) { console.error(`Failed to load client: ${client}`, err); } } } else { this.addClient(client); } } } use(plugin) { if (this.debug) { console.log(`Adding plugin: ${plugin.name}`); } this.plugins.push(plugin); } addParser(parser) { if (this.debug) { console.log(`Adding parser: ${parser.name}`); } this.parsers.push(parser); } addClient(client) { if (this.debug) { console.log(`Adding client: ${client.name}`); } this.clients.push(client); } process(line) { const context = this.parsers.reduce((acc, parser) => { try { return { ...acc, ...parser.parse(line) }; } catch (err) { if (this.debug) { console.error(`Parser error in ${parser.name}:`, err); } return acc; } }, {}); let result = line; for (const plugin of this.plugins) { try { result = plugin.enhance(result); } catch (err) { if (this.debug) { console.error(`Plugin error in ${plugin.name}:`, err); } } } for (const client of this.clients) { try { client.write(result); } catch (err) { if (this.debug) { console.error(`Client error in ${client.name}:`, err); } } } return result; } reset() { this.plugins = []; this.parsers = []; this.clients = []; } }, _class); exports.LogEnhancer = LogEnhancer; //# sourceMappingURL=chunk-BNOJIPVJ.cjs.map