UNPKG

@trap_stevo/star-vault

Version:

Deterministic data engine that eliminates query-time joins and enables normalized data execution. Architect secure, scalable, real-time systems with integrated sharding, encryption, and event-driven data flows. Manage hierarchical structures, execute adva

858 lines (856 loc) 127 kB
"use strict"; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(r) { if (Array.isArray(r)) return r; } function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return e; }; var t, e = {}, r = Object.prototype, n = r.hasOwnProperty, o = Object.defineProperty || function (t, e, r) { t[e] = r.value; }, i = "function" == typeof Symbol ? Symbol : {}, a = i.iterator || "@@iterator", c = i.asyncIterator || "@@asyncIterator", u = i.toStringTag || "@@toStringTag"; function define(t, e, r) { return Object.defineProperty(t, e, { value: r, enumerable: !0, configurable: !0, writable: !0 }), t[e]; } try { define({}, ""); } catch (t) { define = function define(t, e, r) { return t[e] = r; }; } function wrap(t, e, r, n) { var i = e && e.prototype instanceof Generator ? e : Generator, a = Object.create(i.prototype), c = new Context(n || []); return o(a, "_invoke", { value: makeInvokeMethod(t, r, c) }), a; } function tryCatch(t, e, r) { try { return { type: "normal", arg: t.call(e, r) }; } catch (t) { return { type: "throw", arg: t }; } } e.wrap = wrap; var h = "suspendedStart", l = "suspendedYield", f = "executing", s = "completed", y = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var p = {}; define(p, a, function () { return this; }); var d = Object.getPrototypeOf, v = d && d(d(values([]))); v && v !== r && n.call(v, a) && (p = v); var g = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(p); function defineIteratorMethods(t) { ["next", "throw", "return"].forEach(function (e) { define(t, e, function (t) { return this._invoke(e, t); }); }); } function AsyncIterator(t, e) { function invoke(r, o, i, a) { var c = tryCatch(t[r], t, o); if ("throw" !== c.type) { var u = c.arg, h = u.value; return h && "object" == _typeof(h) && n.call(h, "__await") ? e.resolve(h.__await).then(function (t) { invoke("next", t, i, a); }, function (t) { invoke("throw", t, i, a); }) : e.resolve(h).then(function (t) { u.value = t, i(u); }, function (t) { return invoke("throw", t, i, a); }); } a(c.arg); } var r; o(this, "_invoke", { value: function value(t, n) { function callInvokeWithMethodAndArg() { return new e(function (e, r) { invoke(t, n, e, r); }); } return r = r ? r.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); } }); } function makeInvokeMethod(e, r, n) { var o = h; return function (i, a) { if (o === f) throw Error("Generator is already running"); if (o === s) { if ("throw" === i) throw a; return { value: t, done: !0 }; } for (n.method = i, n.arg = a;;) { var c = n.delegate; if (c) { var u = maybeInvokeDelegate(c, n); if (u) { if (u === y) continue; return u; } } if ("next" === n.method) n.sent = n._sent = n.arg;else if ("throw" === n.method) { if (o === h) throw o = s, n.arg; n.dispatchException(n.arg); } else "return" === n.method && n.abrupt("return", n.arg); o = f; var p = tryCatch(e, r, n); if ("normal" === p.type) { if (o = n.done ? s : l, p.arg === y) continue; return { value: p.arg, done: n.done }; } "throw" === p.type && (o = s, n.method = "throw", n.arg = p.arg); } }; } function maybeInvokeDelegate(e, r) { var n = r.method, o = e.iterator[n]; if (o === t) return r.delegate = null, "throw" === n && e.iterator["return"] && (r.method = "return", r.arg = t, maybeInvokeDelegate(e, r), "throw" === r.method) || "return" !== n && (r.method = "throw", r.arg = new TypeError("The iterator does not provide a '" + n + "' method")), y; var i = tryCatch(o, e.iterator, r.arg); if ("throw" === i.type) return r.method = "throw", r.arg = i.arg, r.delegate = null, y; var a = i.arg; return a ? a.done ? (r[e.resultName] = a.value, r.next = e.nextLoc, "return" !== r.method && (r.method = "next", r.arg = t), r.delegate = null, y) : a : (r.method = "throw", r.arg = new TypeError("iterator result is not an object"), r.delegate = null, y); } function pushTryEntry(t) { var e = { tryLoc: t[0] }; 1 in t && (e.catchLoc = t[1]), 2 in t && (e.finallyLoc = t[2], e.afterLoc = t[3]), this.tryEntries.push(e); } function resetTryEntry(t) { var e = t.completion || {}; e.type = "normal", delete e.arg, t.completion = e; } function Context(t) { this.tryEntries = [{ tryLoc: "root" }], t.forEach(pushTryEntry, this), this.reset(!0); } function values(e) { if (e || "" === e) { var r = e[a]; if (r) return r.call(e); if ("function" == typeof e.next) return e; if (!isNaN(e.length)) { var o = -1, i = function next() { for (; ++o < e.length;) if (n.call(e, o)) return next.value = e[o], next.done = !1, next; return next.value = t, next.done = !0, next; }; return i.next = i; } } throw new TypeError(_typeof(e) + " is not iterable"); } return GeneratorFunction.prototype = GeneratorFunctionPrototype, o(g, "constructor", { value: GeneratorFunctionPrototype, configurable: !0 }), o(GeneratorFunctionPrototype, "constructor", { value: GeneratorFunction, configurable: !0 }), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, u, "GeneratorFunction"), e.isGeneratorFunction = function (t) { var e = "function" == typeof t && t.constructor; return !!e && (e === GeneratorFunction || "GeneratorFunction" === (e.displayName || e.name)); }, e.mark = function (t) { return Object.setPrototypeOf ? Object.setPrototypeOf(t, GeneratorFunctionPrototype) : (t.__proto__ = GeneratorFunctionPrototype, define(t, u, "GeneratorFunction")), t.prototype = Object.create(g), t; }, e.awrap = function (t) { return { __await: t }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, c, function () { return this; }), e.AsyncIterator = AsyncIterator, e.async = function (t, r, n, o, i) { void 0 === i && (i = Promise); var a = new AsyncIterator(wrap(t, r, n, o), i); return e.isGeneratorFunction(r) ? a : a.next().then(function (t) { return t.done ? t.value : a.next(); }); }, defineIteratorMethods(g), define(g, u, "Generator"), define(g, a, function () { return this; }), define(g, "toString", function () { return "[object Generator]"; }), e.keys = function (t) { var e = Object(t), r = []; for (var n in e) r.push(n); return r.reverse(), function next() { for (; r.length;) { var t = r.pop(); if (t in e) return next.value = t, next.done = !1, next; } return next.done = !0, next; }; }, e.values = values, Context.prototype = { constructor: Context, reset: function reset(e) { if (this.prev = 0, this.next = 0, this.sent = this._sent = t, this.done = !1, this.delegate = null, this.method = "next", this.arg = t, this.tryEntries.forEach(resetTryEntry), !e) for (var r in this) "t" === r.charAt(0) && n.call(this, r) && !isNaN(+r.slice(1)) && (this[r] = t); }, stop: function stop() { this.done = !0; var t = this.tryEntries[0].completion; if ("throw" === t.type) throw t.arg; return this.rval; }, dispatchException: function dispatchException(e) { if (this.done) throw e; var r = this; function handle(n, o) { return a.type = "throw", a.arg = e, r.next = n, o && (r.method = "next", r.arg = t), !!o; } for (var o = this.tryEntries.length - 1; o >= 0; --o) { var i = this.tryEntries[o], a = i.completion; if ("root" === i.tryLoc) return handle("end"); if (i.tryLoc <= this.prev) { var c = n.call(i, "catchLoc"), u = n.call(i, "finallyLoc"); if (c && u) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } else if (c) { if (this.prev < i.catchLoc) return handle(i.catchLoc, !0); } else { if (!u) throw Error("try statement without catch or finally"); if (this.prev < i.finallyLoc) return handle(i.finallyLoc); } } } }, abrupt: function abrupt(t, e) { for (var r = this.tryEntries.length - 1; r >= 0; --r) { var o = this.tryEntries[r]; if (o.tryLoc <= this.prev && n.call(o, "finallyLoc") && this.prev < o.finallyLoc) { var i = o; break; } } i && ("break" === t || "continue" === t) && i.tryLoc <= e && e <= i.finallyLoc && (i = null); var a = i ? i.completion : {}; return a.type = t, a.arg = e, i ? (this.method = "next", this.next = i.finallyLoc, y) : this.complete(a); }, complete: function complete(t, e) { if ("throw" === t.type) throw t.arg; return "break" === t.type || "continue" === t.type ? this.next = t.arg : "return" === t.type ? (this.rval = this.arg = t.arg, this.method = "return", this.next = "end") : "normal" === t.type && e && (this.next = e), y; }, finish: function finish(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.finallyLoc === t) return this.complete(r.completion, r.afterLoc), resetTryEntry(r), y; } }, "catch": function _catch(t) { for (var e = this.tryEntries.length - 1; e >= 0; --e) { var r = this.tryEntries[e]; if (r.tryLoc === t) { var n = r.completion; if ("throw" === n.type) { var o = n.arg; resetTryEntry(r); } return o; } } throw Error("illegal catch attempt"); }, delegateYield: function delegateYield(e, r, n) { return this.delegate = { iterator: values(e), resultName: r, nextLoc: n }, "next" === this.method && (this.arg = t), y; } }, e; } function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); } function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); } function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); } function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); } function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); } function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; } function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); } var crypto = require("crypto"); var path = require("path"); var fs = require("fs"); var fsp = fs.promises; var EngineUtilityManager = require("../HUDManagers/EngineUtilityManager"); var _chains = /*#__PURE__*/new WeakMap(); var _pool = /*#__PURE__*/new WeakMap(); var _lastRotateAttemptAt = /*#__PURE__*/new WeakMap(); var _lastRetentionSweepAt = /*#__PURE__*/new WeakMap(); var _retentionSweepTimer = /*#__PURE__*/new WeakMap(); var _rotateRetry = /*#__PURE__*/new WeakMap(); var _idleCloseMs = /*#__PURE__*/new WeakMap(); var _debounceMS = /*#__PURE__*/new WeakMap(); var _fsync = /*#__PURE__*/new WeakMap(); var _lockMS = /*#__PURE__*/new WeakMap(); var _enabled = /*#__PURE__*/new WeakMap(); var _checksumsEnabled = /*#__PURE__*/new WeakMap(); var _flushInProgress = /*#__PURE__*/new WeakMap(); var _deferred = /*#__PURE__*/new WeakMap(); var _maxPendingIntentsPerFile = /*#__PURE__*/new WeakMap(); var _maxAppendChunkChars = /*#__PURE__*/new WeakMap(); var _minFlushIntervalMs = /*#__PURE__*/new WeakMap(); var _maxFlushIntervalMs = /*#__PURE__*/new WeakMap(); var _yieldEveryChunks = /*#__PURE__*/new WeakMap(); var _coalesceEnabled = /*#__PURE__*/new WeakMap(); var _coalesceBucketMs = /*#__PURE__*/new WeakMap(); var _coalesceMaxBuckets = /*#__PURE__*/new WeakMap(); var _coalesceActionsAllow = /*#__PURE__*/new WeakMap(); var _coalesceAllocateBucketsAt = /*#__PURE__*/new WeakMap(); var _coalesceMinWindowBuckets = /*#__PURE__*/new WeakMap(); var _coalesceIncludeChecksumInDedupe = /*#__PURE__*/new WeakMap(); var _seqByFileKey = /*#__PURE__*/new WeakMap(); var _runID = /*#__PURE__*/new WeakMap(); var _StarLogger_brand = /*#__PURE__*/new WeakSet(); var StarLogger = /*#__PURE__*/function () { function StarLogger(logPath) { var _this = this; var maxLogSizeBytes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5 * 1024 * 1024; var retention = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "30d"; var dirMode = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 448; var fileMode = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 384; _classCallCheck(this, StarLogger); _classPrivateMethodInitSpec(this, _StarLogger_brand); _classPrivateFieldInitSpec(this, _chains, new Map()); _classPrivateFieldInitSpec(this, _pool, new Map()); _classPrivateFieldInitSpec(this, _lastRotateAttemptAt, new Map()); _classPrivateFieldInitSpec(this, _lastRetentionSweepAt, 0); _classPrivateFieldInitSpec(this, _retentionSweepTimer, null); _classPrivateFieldInitSpec(this, _rotateRetry, { retries: 3, delayMs: 25 }); _classPrivateFieldInitSpec(this, _idleCloseMs, 30000); _classPrivateFieldInitSpec(this, _debounceMS, 0); _classPrivateFieldInitSpec(this, _fsync, false); _classPrivateFieldInitSpec(this, _lockMS, 50); _classPrivateFieldInitSpec(this, _enabled, true); _classPrivateFieldInitSpec(this, _checksumsEnabled, true); _classPrivateFieldInitSpec(this, _flushInProgress, false); _classPrivateFieldInitSpec(this, _deferred, new Map()); _classPrivateFieldInitSpec(this, _maxPendingIntentsPerFile, 20000); _classPrivateFieldInitSpec(this, _maxAppendChunkChars, 4 * 1024 * 1024); _classPrivateFieldInitSpec(this, _minFlushIntervalMs, 75); _classPrivateFieldInitSpec(this, _maxFlushIntervalMs, 750); _classPrivateFieldInitSpec(this, _yieldEveryChunks, 0); _classPrivateFieldInitSpec(this, _coalesceEnabled, true); _classPrivateFieldInitSpec(this, _coalesceBucketMs, 100); _classPrivateFieldInitSpec(this, _coalesceMaxBuckets, 64); _classPrivateFieldInitSpec(this, _coalesceActionsAllow, null); _classPrivateFieldInitSpec(this, _coalesceAllocateBucketsAt, 6); _classPrivateFieldInitSpec(this, _coalesceMinWindowBuckets, 2); _classPrivateFieldInitSpec(this, _coalesceIncludeChecksumInDedupe, false); _classPrivateFieldInitSpec(this, _seqByFileKey, new Map()); _classPrivateFieldInitSpec(this, _runID, ""); this.logPath = logPath; this.maxLogSizeBytes = EngineUtilityManager.convertSize(maxLogSizeBytes); this.retentionMs = EngineUtilityManager.convertDuration(retention); this.fileMode = fileMode; this.dirMode = dirMode; this.decryptor = null; this.encryptor = null; try { _classPrivateFieldSet(_runID, this, crypto.randomBytes(8).toString("hex")); } catch (_unused) { _classPrivateFieldSet(_runID, this, String(Date.now()) + "-" + String(Math.random()).slice(2)); } if (!fs.existsSync(logPath)) { fs.mkdirSync(logPath, { recursive: true, mode: this.dirMode }); } process.on("beforeExit", /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee() { return _regeneratorRuntime().wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _context.prev = 0; _context.next = 3; return _this.shutdown(); case 3: _context.next = 7; break; case 5: _context.prev = 5; _context.t0 = _context["catch"](0); case 7: case "end": return _context.stop(); } }, _callee, null, [[0, 5]]); }))); process.on("exit", function () { try { _assertClassBrand(_StarLogger_brand, _this, _flushDeferredSyncNow).call(_this); } catch (_unused3) {} try { _assertClassBrand(_StarLogger_brand, _this, _closeAllSync).call(_this); } catch (_unused4) {} }); } return _createClass(StarLogger, [{ key: "setEnabled", value: function setEnabled(enabled) { _classPrivateFieldSet(_enabled, this, enabled === true); return _classPrivateFieldGet(_enabled, this); } }, { key: "isEnabled", value: function isEnabled() { return _classPrivateFieldGet(_enabled, this) === true; } }, { key: "setChecksumsEnabled", value: function setChecksumsEnabled(enabled) { _classPrivateFieldSet(_checksumsEnabled, this, enabled === true); return _classPrivateFieldGet(_checksumsEnabled, this); } }, { key: "setDecryptor", value: function setDecryptor(fn) { this.decryptor = typeof fn === "function" ? fn : null; return this; } }, { key: "setEncryptor", value: function setEncryptor(fn) { this.encryptor = typeof fn === "function" ? fn : null; return this; } }, { key: "setIdleCloseMs", value: function setIdleCloseMs(ms) { if (Number.isFinite(ms) && ms >= 0) { _classPrivateFieldSet(_idleCloseMs, this, ms); } return _classPrivateFieldGet(_idleCloseMs, this); } }, { key: "setLockMs", value: function setLockMs(ms) { if (Number.isFinite(ms) && ms >= 0) { _classPrivateFieldSet(_lockMS, this, ms); } return _classPrivateFieldGet(_lockMS, this); } }, { key: "setDebounceMs", value: function setDebounceMs(ms) { if (Number.isFinite(ms) && ms >= 0) { _classPrivateFieldSet(_debounceMS, this, ms); } return _classPrivateFieldGet(_debounceMS, this); } }, { key: "setFsync", value: function setFsync(enabled) { _classPrivateFieldSet(_fsync, this, enabled === true); return _classPrivateFieldGet(_fsync, this); } }, { key: "setMaxPendingIntentsPerFile", value: function setMaxPendingIntentsPerFile(n) { if (Number.isFinite(n) && n >= 0) { _classPrivateFieldSet(_maxPendingIntentsPerFile, this, n | 0); } return _classPrivateFieldGet(_maxPendingIntentsPerFile, this); } }, { key: "setMaxAppendChunkChars", value: function setMaxAppendChunkChars(n) { if (Number.isFinite(n) && n >= 0) { _classPrivateFieldSet(_maxAppendChunkChars, this, n | 0); } return _classPrivateFieldGet(_maxAppendChunkChars, this); } }, { key: "setMinFlushIntervalMs", value: function setMinFlushIntervalMs(ms) { if (Number.isFinite(ms) && ms >= 0) { _classPrivateFieldSet(_minFlushIntervalMs, this, ms | 0); } return _classPrivateFieldGet(_minFlushIntervalMs, this); } }, { key: "setMaxFlushIntervalMs", value: function setMaxFlushIntervalMs(ms) { if (Number.isFinite(ms) && ms >= 0) { _classPrivateFieldSet(_maxFlushIntervalMs, this, ms | 0); } return _classPrivateFieldGet(_maxFlushIntervalMs, this); } }, { key: "setYieldEveryChunks", value: function setYieldEveryChunks(n) { if (Number.isFinite(n) && n >= 0) { _classPrivateFieldSet(_yieldEveryChunks, this, n | 0); } return _classPrivateFieldGet(_yieldEveryChunks, this); } }, { key: "setCoalesceEnabled", value: function setCoalesceEnabled(enabled) { _classPrivateFieldSet(_coalesceEnabled, this, enabled === true); return _classPrivateFieldGet(_coalesceEnabled, this); } }, { key: "setCoalesceBucketMs", value: function setCoalesceBucketMs(ms) { if (Number.isFinite(ms) && ms > 0) { _classPrivateFieldSet(_coalesceBucketMs, this, ms | 0); } return _classPrivateFieldGet(_coalesceBucketMs, this); } }, { key: "setCoalesceMaxBuckets", value: function setCoalesceMaxBuckets(n) { if (Number.isFinite(n) && n > 0) { _classPrivateFieldSet(_coalesceMaxBuckets, this, n | 0); } return _classPrivateFieldGet(_coalesceMaxBuckets, this); } }, { key: "setCoalesceAllocateBucketsAt", value: function setCoalesceAllocateBucketsAt(n) { if (Number.isFinite(n) && n > 1) { _classPrivateFieldSet(_coalesceAllocateBucketsAt, this, n | 0); } return _classPrivateFieldGet(_coalesceAllocateBucketsAt, this); } }, { key: "setCoalesceMinWindowBuckets", value: function setCoalesceMinWindowBuckets(n) { if (Number.isFinite(n) && n > 1) { _classPrivateFieldSet(_coalesceMinWindowBuckets, this, n | 0); } return _classPrivateFieldGet(_coalesceMinWindowBuckets, this); } }, { key: "setCoalesceIncludeChecksumInDedupe", value: function setCoalesceIncludeChecksumInDedupe(enabled) { _classPrivateFieldSet(_coalesceIncludeChecksumInDedupe, this, enabled === true); return _classPrivateFieldGet(_coalesceIncludeChecksumInDedupe, this); } }, { key: "setCoalesceActions", value: function setCoalesceActions(actions) { if (!actions) { _classPrivateFieldSet(_coalesceActionsAllow, this, null); return null; } var arr = Array.isArray(actions) ? actions : [actions]; var set = new Set(arr.map(function (x) { return String(x || "").toUpperCase(); }).filter(Boolean)); _classPrivateFieldSet(_coalesceActionsAllow, this, set.size ? set : null); return _classPrivateFieldGet(_coalesceActionsAllow, this); } }, { key: "logWrite", value: function logWrite(collection, record) { var clientInfo = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; var action = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : "WRITE"; var metadata = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; if (_classPrivateFieldGet(_enabled, this) !== true) { return; } var fileKey = _assertClassBrand(_StarLogger_brand, this, _fileKeyForCollection).call(this, collection); var actionUpper = String(action || "WRITE").toUpperCase(); var _ref2 = metadata || {}, _ref2$transactionID = _ref2.transactionID, transactionID = _ref2$transactionID === void 0 ? null : _ref2$transactionID, _ref2$idempotencyKey = _ref2.idempotencyKey, idempotencyKey = _ref2$idempotencyKey === void 0 ? null : _ref2$idempotencyKey, _ref2$recordID = _ref2.recordID, recordID = _ref2$recordID === void 0 ? null : _ref2$recordID; var clientKey = _assertClassBrand(_StarLogger_brand, this, _clientKey).call(this, clientInfo); var atMs = Date.now(); var evt = { kind: "write", scope: "collection", collection: String(collection || ""), action: actionUpper, atMs: atMs, client: clientInfo, clientKey: clientKey, record: record, recordID: recordID, transactionID: transactionID, idempotencyKey: idempotencyKey }; _assertClassBrand(_StarLogger_brand, this, _enqueueEventFast).call(this, fileKey, evt); } }, { key: "logSystemAction", value: function logSystemAction(action, collection, details) { var clientInfo = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; var metadata = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; if (_classPrivateFieldGet(_enabled, this) !== true) { return; } var fileKey = _assertClassBrand(_StarLogger_brand, this, _fileKeyForSystem).call(this); var actionUpper = String(action || "SYSTEM").toUpperCase(); var _ref3 = metadata || {}, _ref3$transactionID = _ref3.transactionID, transactionID = _ref3$transactionID === void 0 ? null : _ref3$transactionID, _ref3$idempotencyKey = _ref3.idempotencyKey, idempotencyKey = _ref3$idempotencyKey === void 0 ? null : _ref3$idempotencyKey, _ref3$keyVersion = _ref3.keyVersion, keyVersion = _ref3$keyVersion === void 0 ? 1 : _ref3$keyVersion; var detailsID = details && details.id || null; var detShapeEncrypted = this.encryptor ? 1 : 0; var clientKey = _assertClassBrand(_StarLogger_brand, this, _clientKey).call(this, clientInfo); var atMs = Date.now(); var evt = { kind: "system", scope: "system", collection: String(collection || ""), action: actionUpper, atMs: atMs, client: clientInfo, clientKey: clientKey, details: details, detailsID: detailsID, transactionID: transactionID, idempotencyKey: idempotencyKey, keyVersion: keyVersion, detShapeEncrypted: detShapeEncrypted }; _assertClassBrand(_StarLogger_brand, this, _enqueueEventFast).call(this, fileKey, evt); } }, { key: "logTxBegin", value: function logTxBegin(transactionID) { var clientInfo = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (_classPrivateFieldGet(_enabled, this) !== true) { return; } this.logSystemAction("TX-BEGIN", "system", { id: transactionID }, clientInfo); } }, { key: "logTxCommit", value: function logTxCommit(transactionID) { var clientInfo = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (_classPrivateFieldGet(_enabled, this) !== true) { return; } this.logSystemAction("TX-COMMIT", "system", { id: transactionID }, clientInfo); } }, { key: "logTxAbort", value: function logTxAbort(transactionID) { var clientInfo = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; if (_classPrivateFieldGet(_enabled, this) !== true) { return; } this.logSystemAction("TX-ABORT", "system", { id: transactionID }, clientInfo); } }, { key: "logAudit", value: function logAudit(action, details) { var _metadata$transaction, _metadata$idempotency, _metadata$keyVersion; var clientInfo = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; var metadata = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; if (_classPrivateFieldGet(_enabled, this) !== true) { return; } var fileKey = _assertClassBrand(_StarLogger_brand, this, _fileKeyForAudit).call(this); var actionUpper = String(action || "AUDIT").toUpperCase(); var detShapeEncrypted = this.encryptor ? 1 : 0; var clientKey = _assertClassBrand(_StarLogger_brand, this, _clientKey).call(this, clientInfo); var atMs = Date.now(); var evt = { kind: "audit", scope: "audit", collection: "audit", action: actionUpper, atMs: atMs, client: clientInfo, clientKey: clientKey, details: details, transactionID: (_metadata$transaction = metadata === null || metadata === void 0 ? void 0 : metadata.transactionID) !== null && _metadata$transaction !== void 0 ? _metadata$transaction : null, idempotencyKey: (_metadata$idempotency = metadata === null || metadata === void 0 ? void 0 : metadata.idempotencyKey) !== null && _metadata$idempotency !== void 0 ? _metadata$idempotency : null, keyVersion: (_metadata$keyVersion = metadata === null || metadata === void 0 ? void 0 : metadata.keyVersion) !== null && _metadata$keyVersion !== void 0 ? _metadata$keyVersion : 1, detShapeEncrypted: detShapeEncrypted }; _assertClassBrand(_StarLogger_brand, this, _enqueueEventFast).call(this, fileKey, evt); } }, { key: "recoverWrites", value: function recoverWrites(collection) { var logFile = path.join(this.logPath, "".concat(collection, ".log")); if (!fs.existsSync(logFile)) { return []; } var lines = fs.readFileSync(logFile, "utf-8").split("\n").filter(Boolean); var valid = []; var _iterator = _createForOfIteratorHelper(lines), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var line = _step.value; try { var entry = JSON.parse(line); if (_classPrivateFieldGet(_checksumsEnabled, this) !== true) { valid.push(entry); continue; } var verify = _assertClassBrand(_StarLogger_brand, this, _sha256OfJson).call(this, entry.data); if (!verify) { continue; } if (verify !== entry.checksum) { continue; } valid.push(entry); } catch (_unused5) {} } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return valid; } }, { key: "clearLog", value: function clearLog(collection) { var logFile = path.join(this.logPath, "".concat(collection, ".log")); if (fs.existsSync(logFile)) { fs.unlinkSync(logFile); } _assertClassBrand(_StarLogger_brand, this, _destroyPoolEntry).call(this, logFile); _assertClassBrand(_StarLogger_brand, this, _clearDeferredForFile).call(this, logFile); } }, { key: "flush", value: function () { var _flush = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() { var force, chains, _args2 = arguments; return _regeneratorRuntime().wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: force = _args2.length > 0 && _args2[0] !== undefined ? _args2[0] : false; if (!_classPrivateFieldGet(_flushInProgress, this)) { _context2.next = 3; break; } return _context2.abrupt("return"); case 3: if (!((_classPrivateFieldGet(_deferred, this).size === 0 || _classPrivateFieldGet(_deferred, this).size < 1) && force !== true)) { _context2.next = 5; break; } return _context2.abrupt("return"); case 5: _classPrivateFieldSet(_flushInProgress, this, true); _context2.prev = 6; _context2.next = 9; return _assertClassBrand(_StarLogger_brand, this, _flushDeferredBuckets).call(this, force === true); case 9: chains = Array.from(_classPrivateFieldGet(_chains, this).values()); if (!chains.length) { _context2.next = 13; break; } _context2.next = 13; return Promise.allSettled(chains); case 13: _context2.prev = 13; _classPrivateFieldSet(_flushInProgress, this, false); return _context2.finish(13); case 16: case "end": return _context2.stop(); } }, _callee2, this, [[6,, 13, 16]]); })); function flush() { return _flush.apply(this, arguments); } return flush; }() }, { key: "syncLogs", value: function () { var _syncLogs = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4() { var _this2 = this; var chains, closers, _iterator2, _step2, _loop; return _regeneratorRuntime().wrap(function _callee4$(_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: _context5.next = 2; return this.flush(true); case 2: _context5.prev = 2; chains = Array.from(_classPrivateFieldGet(_chains, this).values()); if (!chains.length) { _context5.next = 7; break; } _context5.next = 7; return Promise.allSettled(chains); case 7: _context5.next = 11; break; case 9: _context5.prev = 9; _context5.t0 = _context5["catch"](2); case 11: closers = []; _iterator2 = _createForOfIteratorHelper(_classPrivateFieldGet(_pool, this)); _context5.prev = 13; _loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop() { var _step2$value, fileKey, entry; return _regeneratorRuntime().wrap(function _loop$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: _step2$value = _slicedToArray(_step2.value, 2), fileKey = _step2$value[0], entry = _step2$value[1]; closers.push(_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3() { return _regeneratorRuntime().wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _context3.prev = 0; if (!entry.handle) { _context3.next = 37; break; } if (!(typeof entry.handle.datasync === "function")) { _context3.next = 12; break; } _context3.prev = 3; _context3.next = 6; return entry.handle.datasync(); case 6: _context3.next = 10; break; case 8: _context3.prev = 8; _context3.t0 = _context3["catch"](3); case 10: _context3.next = 35; break; case 12: if (!(typeof entry.handle.sync === "function")) { _context3.next = 22; break; } _context3.prev = 13; _context3.next = 16; return entry.handle.sync(); case 16: _context3.next = 20; break; case 18: _context3.prev = 18; _context3.t1 = _context3["catch"](13); case 20: _context3.next = 35; break; case 22: if (!(typeof entry.fd === "number")) { _context3.next = 35; break; } _context3.prev = 23; if (!(typeof fs.promises.fdatasync === "function")) { _context3.next = 29; break; } _context3.next = 27; return fs.promises.fdatasync(entry.fd); case 27: _context3.next = 31; break; case 29: _context3.next = 31; return fs.promises.fsync(entry.fd); case 31: _context3.next = 35; break; case 33: _context3.prev = 33; _context3.t2 = _context3["catch"](23); case 35: _context3.next = 50; break; case 37: if (!(typeof entry.fd === "number")) { _context3.next = 50; break; } _context3.prev = 38; if (!(typeof fs.promises.fdatasync === "function")) { _context3.next = 44; break; } _context3.next = 42; return fs.promises.fdatasync(entry.fd); case 42: _context3.next = 46; break; case 44: _context3.next = 46; return fs.promises.fsync(entry.fd); case 46: _context3.next = 50; break; case 48: _context3.prev = 48; _context3.t3 = _context3["catch"](38); case 50: _context3.next = 52; return _assertClassBrand(_StarLogger_brand, _this2, _closeEntry).call(_this2, entry); case 52: _context3.next = 56; break; case 54: _context3.prev = 54; _context3.t4 = _context3["catch"](0); case 56: _context3.prev = 56; _classPrivateFieldGet(_pool, _this2)["delete"](fileKey); return _context3.finish(56); case 59: case "end": return _context3.stop(); } }, _callee3, null, [[0, 54, 56, 59], [3, 8], [13, 18], [23, 33], [38, 48]]); }))()); case 2: case "end": return _context4.stop(); } }, _loop); }); _iterator2.s(); case 16: if ((_step2 = _iterator2.n()).done) { _context5.next = 20; break; } return _context5.delegateYield(_loop(), "t1", 18); case 18: _context5.next = 16; break; case 20: _context5.next = 25; break; case 22: _context5.prev = 22; _context5.t2 = _context5["catch"](13); _iterator2.e(_context5.t2); case 25: _context5.prev = 25; _iterator2.f(); return _context5.finish(25); case 28: if (!closers.length) { _context5.next = 31; break; } _context5.next = 31; return Promise.allSettled(closers); case 31: case "end": return _context5.stop(); } }, _callee4, this, [[2, 9], [13, 22, 25, 28]]); })); function syncLogs() { return _syncLogs.apply(this, arguments); } return syncLogs; }() }, { key: "getRecordHistory", value: function getRecordHistory(collection, id) { var _this3 = this; var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; var includeArchived = options.includeArchived !== false; var includeSystem = options.includeSystem !== false; var verifyChecksums = options.verifyChecksums !== false; var includeDecrypted = options.includeDecrypted === undefined ? !!this.decryptor : options.includeDecrypted === true; var filters = _assertClassBrand(_StarLogger_brand, this, _normalizeActionFilters).call(this, options); var sinceMs = _assertClassBrand(_StarLogger_brand, this, _toMs).call(this, options.since); var untilMs = _assertClassBrand(_StarLogger_brand, this, _toMs).call(this, options.until); var match = typeof options.match === "function" ? options.match : null; var inRange = function inRange(ms) { if (Number.isFinite(sinceMs) && ms < sinceMs) { return false; } if (Number.isFinite(untilMs) && ms > untilMs) { return false; } return true; }; var files = includeArchived ? _assertClassBrand(_StarLogger_brand, this, _listCollectionLogFiles).call(this, collection) : [path.join(this.logPath, "".concat(collection, ".log"))]; var out = []; var _iterator3 = _createForOfIteratorHelper(files), _step3; try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { var file = _step3.value; var _rows = _assertClassBrand(_StarLogger_brand, this, _readJsonlMerged).call(this, file); var _iterator5 = _createForOfIteratorHelper(_rows), _step5; try { for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) { var _row = _step5.value; if (!_row) { continue; } var _action = String(_row.action || "").toUpperCase(); if (!_assertClassBrand(_StarLogger_brand, this, _actionIncluded).call(this, _action, filters)) { continue; } var _tms = _assertClassBrand(_StarLogger_brand, this, _toMs).call(this, _row.timestamp); if (!inRange(_tms || 0)) { continue; } if (verifyChecksums && _classPrivateFieldGet(_checksumsEnabled, this) === true && _row.data && !_assertClassBrand(_StarLogger_brand, this, _verifyWriteChecksum).call(this, _row)) { continue; } var rid = _assertClassBrand(_StarLogger_brand, this, _resolveRowID).call(this, collection, _row); if (rid !== id) { continue; } var data = _row.data; if (includeDecrypted) { var dec = _assertClassBrand(_StarLogger_brand, this, _maybeDecrypt).call(this, collection, _row); if (dec) { data = dec; } } var _entry = { source: "collection", collection: collection, id: rid, action: _action, timestamp: _row.timestamp, timestampMs: _tms || 0, client: _row.client || {}, data: data, checksum: _row.checksum || null, seq: _row.seq, eventID: _row.eventID || null, transactionID: _row.transactionID || null, idempotencyKey: _row.idempotencyKey || null, occurrences: _row.occurrences || 1, firstTimestamp: _row.firstTimestamp || _row.timestamp, lastTimestamp: _row.lastTimestamp || _row.timestamp, bucketMs: _row.bucketMs || null, bucketPairs: _row.bucketPairs || null }; if (match && !match(_entry)) { continue; } out.push(_entry); } } catch (err) { _iterator5.e(err); } finally { _iterator5.f(); } } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } if (includeSystem) { var sysFile = path.join(this.logPath, "system.log"); var rows = _assertClassBrand(_StarLogger_brand, this, _readJsonlMerged).call(this, sysFile); var _iterator4 = _createForOfIteratorHelper(rows), _step4; try { for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) { var row = _step4.value; if (!row || row.collection !== collection) { continue; } var det = _assertClassBrand(_StarLogger_brand, this, _decryptSystemDetails).call(this, row); if (!det || det.id !== id) { continue; } var action = String(row.action || "").toUpperCase(); if (!_assertClassBrand(_StarLogger_brand, this, _actionIncluded).call(this, action, filters)) { continue; } var tms = _assertClassBrand(_StarLogger_brand, this, _toMs).call(this, row.timestamp); if (!inRange(tms || 0)) { continue; } var entry = { source: "system", collection: collection, id: id,