@trap_stevo/timelyst
Version:
Forging the next evolution in intelligent data control, this powerhouse solution enables unparalleled management of dynamic lists across any application domain. Blacklist enforcement, whitelist access, behavioral tagging, expiration policies, and real-tim
938 lines (936 loc) • 50.6 kB
JavaScript
"use strict";
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 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 _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 _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 _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 _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == _typeof(e) || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); }
function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
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 EventEmitter = require("events");
var path = require("path");
var _require = require("@trap_stevo/timetide"),
toMilliseconds = _require.toMilliseconds,
toSeconds = _require.toSeconds;
var MemoryManager = require("./HUDManagers/MemoryManager.js");
var TemporalStorageManager;
var _Timelyst_brand = /*#__PURE__*/new WeakSet();
var _searchIndex = /*#__PURE__*/new WeakMap();
var Timelyst = /*#__PURE__*/function (_EventEmitter) {
function Timelyst() {
var _this;
var inputOptions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, Timelyst);
_this = _callSuper(this, Timelyst);
// --- Entry Data Management Functionality ---
_classPrivateMethodInitSpec(_this, _Timelyst_brand);
// --- Search Management Functionality ---
_classPrivateFieldInitSpec(_this, _searchIndex, new Map());
var options = Object.assign({
enableOfflineStorage: false,
autoCleanupInterval: null,
basePath: path.resolve(process.cwd(), "timelyst_db")
}, inputOptions);
_this.core = new MemoryManager();
_this.core.on("entry-expired", function () {
var _this2;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return (_this2 = _this).emit.apply(_this2, ["entry-expired"].concat(args));
});
_this.core.on("entry-removed", function () {
var _this3;
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return (_this3 = _this).emit.apply(_this3, ["entry-removed"].concat(args));
});
_this.core.on("entry-added", function () {
var _this4;
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
return (_this4 = _this).emit.apply(_this4, ["entry-added"].concat(args));
});
if (options.enableOfflineStorage) {
try {
TemporalStorageManager = require("./HUDManagers/TemporalStorageManager.js");
_this.storage = new TemporalStorageManager(options);
} catch (error) {
_this.storage = null;
}
} else if (!options.enableOfflineStorage) {
_this.storage = null;
}
_this.listConfigs = new Map();
if (options.autoCleanupInterval) {
var interval = toMilliseconds(options.autoCleanupInterval);
_this.startAutoCleanup(interval);
}
_this.searchIndexPath = path.join(options.basePath, options.searchIndexerName || "search-index.json");
_assertClassBrand(_Timelyst_brand, _this, _loadSearchIndex).call(_this);
return _this;
}
_inherits(Timelyst, _EventEmitter);
return _createClass(Timelyst, [{
key: "createList",
value: // --- List Management ---
function () {
var _createList = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee(name) {
var config,
shardCount,
i,
shard,
all,
_args = arguments;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
config = _args.length > 1 && _args[1] !== undefined ? _args[1] : {};
if (!this.listConfigs.has(name)) {
_context.next = 3;
break;
}
return _context.abrupt("return", false);
case 3:
this.listConfigs.set(name, config);
shardCount = config.shardCount || 1;
i = 0;
case 6:
if (!(i < shardCount)) {
_context.next = 22;
break;
}
shard = shardCount === 1 ? name : "".concat(name, ":shard:").concat(i);
_context.next = 10;
return this.core.create(shard, config);
case 10:
if (!this.storage) {
_context.next = 19;
break;
}
_context.next = 13;
return this.storage.create(shard, config);
case 13:
_context.next = 15;
return this.storage.getAll(shard);
case 15:
all = _context.sent;
if (!Array.isArray(all)) {
_context.next = 19;
break;
}
_context.next = 19;
return this.core.addMany(shard, all.map(function (entry) {
return {
key: entry.key,
metadata: entry
};
}));
case 19:
i++;
_context.next = 6;
break;
case 22:
return _context.abrupt("return", true);
case 23:
case "end":
return _context.stop();
}
}, _callee, this);
}));
function createList(_x) {
return _createList.apply(this, arguments);
}
return createList;
}()
}, {
key: "deleteList",
value: function () {
var _deleteList = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2(name) {
return _regeneratorRuntime().wrap(function _callee2$(_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
this.listConfigs["delete"](name);
_context2.next = 3;
return this.core["delete"](name);
case 3:
if (!this.storage) {
_context2.next = 6;
break;
}
_context2.next = 6;
return this.storage["delete"](name);
case 6:
return _context2.abrupt("return", true);
case 7:
case "end":
return _context2.stop();
}
}, _callee2, this);
}));
function deleteList(_x2) {
return _deleteList.apply(this, arguments);
}
return deleteList;
}()
}, {
key: "addToList",
value: function () {
var _addToList = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee3(name, key) {
var metadata,
shard,
prepared,
_args3 = arguments;
return _regeneratorRuntime().wrap(function _callee3$(_context3) {
while (1) switch (_context3.prev = _context3.next) {
case 0:
metadata = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : {};
shard = _assertClassBrand(_Timelyst_brand, this, _getShardName).call(this, name, key);
prepared = _assertClassBrand(_Timelyst_brand, this, _prepareMetadata).call(this, metadata);
_context3.next = 5;
return this.core.add(shard, key, prepared);
case 5:
if (!this.storage) {
_context3.next = 8;
break;
}
_context3.next = 8;
return this.storage.add(shard, key, prepared);
case 8:
return _context3.abrupt("return", true);
case 9:
case "end":
return _context3.stop();
}
}, _callee3, this);
}));
function addToList(_x3, _x4) {
return _addToList.apply(this, arguments);
}
return addToList;
}()
}, {
key: "addManyToList",
value: function () {
var _addManyToList = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee4(name) {
var entries,
shardMap,
_iterator,
_step,
entry,
key,
_entry$metadata,
metadata,
_shard,
results,
shard,
res,
_args4 = arguments;
return _regeneratorRuntime().wrap(function _callee4$(_context4) {
while (1) switch (_context4.prev = _context4.next) {
case 0:
entries = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : [];
if (Array.isArray(entries)) {
_context4.next = 3;
break;
}
return _context4.abrupt("return", false);
case 3:
shardMap = {};
_iterator = _createForOfIteratorHelper(entries);
_context4.prev = 5;
_iterator.s();
case 7:
if ((_step = _iterator.n()).done) {
_context4.next = 17;
break;
}
entry = _step.value;
key = entry.key, _entry$metadata = entry.metadata, metadata = _entry$metadata === void 0 ? {} : _entry$metadata;
if (key) {
_context4.next = 12;
break;
}
return _context4.abrupt("continue", 15);
case 12:
_shard = _assertClassBrand(_Timelyst_brand, this, _getShardName).call(this, name, key);
if (!shardMap[_shard]) {
shardMap[_shard] = [];
}
shardMap[_shard].push({
key: key,
metadata: _assertClassBrand(_Timelyst_brand, this, _prepareMetadata).call(this, metadata)
});
case 15:
_context4.next = 7;
break;
case 17:
_context4.next = 22;
break;
case 19:
_context4.prev = 19;
_context4.t0 = _context4["catch"](5);
_iterator.e(_context4.t0);
case 22:
_context4.prev = 22;
_iterator.f();
return _context4.finish(22);
case 25:
results = [];
_context4.t1 = _regeneratorRuntime().keys(shardMap);
case 27:
if ((_context4.t2 = _context4.t1()).done) {
_context4.next = 38;
break;
}
shard = _context4.t2.value;
_context4.next = 31;
return this.core.addMany(shard, shardMap[shard]);
case 31:
res = _context4.sent;
if (!this.storage) {
_context4.next = 35;
break;
}
_context4.next = 35;
return this.storage.addMany(shard, shardMap[shard]);
case 35:
results.push.apply(results, _toConsumableArray(res));
_context4.next = 27;
break;
case 38:
return _context4.abrupt("return", results);
case 39:
case "end":
return _context4.stop();
}
}, _callee4, this, [[5, 19, 22, 25]]);
}));
function addManyToList(_x5) {
return _addManyToList.apply(this, arguments);
}
return addManyToList;
}()
}, {
key: "removeFromList",
value: function () {
var _removeFromList = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee5(name, key) {
var shard, existed;
return _regeneratorRuntime().wrap(function _callee5$(_context5) {
while (1) switch (_context5.prev = _context5.next) {
case 0:
shard = _assertClassBrand(_Timelyst_brand, this, _getShardName).call(this, name, key);
_context5.next = 3;
return this.core.remove(shard, key);
case 3:
existed = _context5.sent;
if (!this.storage) {
_context5.next = 7;
break;
}
_context5.next = 7;
return this.storage.remove(shard, key);
case 7:
return _context5.abrupt("return", true);
case 8:
case "end":
return _context5.stop();
}
}, _callee5, this);
}));
function removeFromList(_x6, _x7) {
return _removeFromList.apply(this, arguments);
}
return removeFromList;
}() // --- List Search Management ---
}, {
key: "listOperational",
value: function listOperational(name) {
return this.core.listOperational(name);
}
}, {
key: "inList",
value: function () {
var _inList = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee6(name, key) {
var shard;
return _regeneratorRuntime().wrap(function _callee6$(_context6) {
while (1) switch (_context6.prev = _context6.next) {
case 0:
shard = _assertClassBrand(_Timelyst_brand, this, _getShardName).call(this, name, key);
_context6.next = 3;
return this.core.has(shard, key);
case 3:
return _context6.abrupt("return", _context6.sent);
case 4:
case "end":
return _context6.stop();
}
}, _callee6, this);
}));
function inList(_x8, _x9) {
return _inList.apply(this, arguments);
}
return inList;
}()
}, {
key: "getListContents",
value: function () {
var _getListContents = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee7(name) {
var config, shardCount, combined, i, shard, entries;
return _regeneratorRuntime().wrap(function _callee7$(_context7) {
while (1) switch (_context7.prev = _context7.next) {
case 0:
config = this.listConfigs.get(name) || {};
shardCount = config.shardCount || 1;
combined = [];
i = 0;
case 4:
if (!(i < shardCount)) {
_context7.next = 13;
break;
}
shard = shardCount === 1 ? name : "".concat(name, ":shard:").concat(i);
_context7.next = 8;
return this.core.getAll(shard);
case 8:
entries = _context7.sent;
combined.push.apply(combined, _toConsumableArray(entries));
case 10:
i++;
_context7.next = 4;
break;
case 13:
return _context7.abrupt("return", combined);
case 14:
case "end":
return _context7.stop();
}
}, _callee7, this);
}));
function getListContents(_x10) {
return _getListContents.apply(this, arguments);
}
return getListContents;
}()
}, {
key: "getListContentsPaginated",
value: function () {
var _getListContentsPaginated = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee8(name) {
var options,
_options$offset,
offset,
_options$limit,
limit,
all,
_args8 = arguments;
return _regeneratorRuntime().wrap(function _callee8$(_context8) {
while (1) switch (_context8.prev = _context8.next) {
case 0:
options = _args8.length > 1 && _args8[1] !== undefined ? _args8[1] : {};
_options$offset = options.offset, offset = _options$offset === void 0 ? 0 : _options$offset, _options$limit = options.limit, limit = _options$limit === void 0 ? 100 : _options$limit;
_context8.next = 4;
return this.getListContents(name);
case 4:
all = _context8.sent;
return _context8.abrupt("return", all.slice(offset, offset + limit));
case 6:
case "end":
return _context8.stop();
}
}, _callee8, this);
}));
function getListContentsPaginated(_x11) {
return _getListContentsPaginated.apply(this, arguments);
}
return getListContentsPaginated;
}()
}, {
key: "filterList",
value: function () {
var _filterList = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee9(name) {
var filter,
all,
_filter$tags,
tags,
_filter$priority,
priority,
_filter$minPriority,
minPriority,
_filter$maxPriority,
maxPriority,
_filter$category,
category,
_filter$categoryRegex,
categoryRegex,
_filter$keyRegex,
keyRegex,
_filter$sortBy,
sortBy,
_filter$sortOrder,
sortOrder,
filtered,
_args9 = arguments;
return _regeneratorRuntime().wrap(function _callee9$(_context9) {
while (1) switch (_context9.prev = _context9.next) {
case 0:
filter = _args9.length > 1 && _args9[1] !== undefined ? _args9[1] : {};
_context9.next = 3;
return this.sweepExpired(name);
case 3:
_context9.next = 5;
return this.getListContents(name);
case 5:
all = _context9.sent;
_filter$tags = filter.tags, tags = _filter$tags === void 0 ? [] : _filter$tags, _filter$priority = filter.priority, priority = _filter$priority === void 0 ? null : _filter$priority, _filter$minPriority = filter.minPriority, minPriority = _filter$minPriority === void 0 ? null : _filter$minPriority, _filter$maxPriority = filter.maxPriority, maxPriority = _filter$maxPriority === void 0 ? null : _filter$maxPriority, _filter$category = filter.category, category = _filter$category === void 0 ? null : _filter$category, _filter$categoryRegex = filter.categoryRegex, categoryRegex = _filter$categoryRegex === void 0 ? null : _filter$categoryRegex, _filter$keyRegex = filter.keyRegex, keyRegex = _filter$keyRegex === void 0 ? null : _filter$keyRegex, _filter$sortBy = filter.sortBy, sortBy = _filter$sortBy === void 0 ? null : _filter$sortBy, _filter$sortOrder = filter.sortOrder, sortOrder = _filter$sortOrder === void 0 ? "asc" : _filter$sortOrder;
filtered = all.filter(function (entry) {
if (tags.length && !tags.every(function (tag) {
return entry.tags.includes(tag);
})) {
return false;
}
if (priority !== null && entry.priority !== priority) {
return false;
}
if (minPriority !== null && entry.priority < minPriority) {
return false;
}
if (maxPriority !== null && entry.priority > maxPriority) {
return false;
}
if (category !== null && entry.category !== category) {
return false;
}
if (categoryRegex && !new RegExp(categoryRegex).test(entry.category)) {
return false;
}
if (keyRegex && !new RegExp(keyRegex).test(entry.key)) {
return false;
}
return true;
});
if (sortBy) {
filtered.sort(function (a, b) {
var aVal = a[sortBy];
var bVal = b[sortBy];
if (aVal < bVal) {
return sortOrder === "asc" ? -1 : 1;
}
if (aVal > bVal) {
return sortOrder === "asc" ? 1 : -1;
}
return 0;
});
}
return _context9.abrupt("return", filtered);
case 10:
case "end":
return _context9.stop();
}
}, _callee9, this);
}));
function filterList(_x12) {
return _filterList.apply(this, arguments);
}
return filterList;
}()
}, {
key: "findKeysContaining",
value: function findKeysContaining(keyword) {
keyword = keyword.toLowerCase();
var matchedKeys = [];
var _iterator2 = _createForOfIteratorHelper(_classPrivateFieldGet(_searchIndex, this).entries()),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var _step2$value = _slicedToArray(_step2.value, 1),
key = _step2$value[0];
if (key.toLowerCase().includes(keyword)) {
matchedKeys.push(key);
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
return matchedKeys;
}
}, {
key: "findSimilarKeys",
value: function findSimilarKeys(keyword) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var _options$includeScore = options.includeScoreMap,
includeScoreMap = _options$includeScore === void 0 ? false : _options$includeScore,
_options$baseKeyword = options.baseKeyword,
baseKeyword = _options$baseKeyword === void 0 ? null : _options$baseKeyword,
_options$limit2 = options.limit,
limit = _options$limit2 === void 0 ? null : _options$limit2,
_options$offset2 = options.offset,
offset = _options$offset2 === void 0 ? 0 : _options$offset2;
var matched = [];
var _iterator3 = _createForOfIteratorHelper(_classPrivateFieldGet(_searchIndex, this).entries()),
_step3;
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
var _step3$value = _slicedToArray(_step3.value, 1),
key = _step3$value[0];
var similarity = _assertClassBrand(_Timelyst_brand, this, _calculateSimilarity).call(this, key, keyword);
if (similarity >= threshold) {
matched.push({
key: key,
similarity: similarity
});
}
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
var filtered = matched.map(function (entry) {
var label = entry.similarity >= 0.85 ? "strong" : entry.similarity >= 0.65 ? "medium" : "weak";
return _objectSpread(_objectSpread({}, entry), {}, {
label: label
});
}).filter(function (entry) {
return !labels || labels.includes(entry.label);
});
if (sortOrder === "asc") {
filtered.sort(function (a, b) {
return a.similarity - b.similarity;
});
} else {
filtered.sort(function (a, b) {
return b.similarity - a.similarity;
});
}
return limit && limit > 0 ? filtered.slice(0, limit) : filtered;
}
}, {
key: "getEntriesByKeys",
value: function () {
var _getEntriesByKeys = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee10(list) {
var keys,
options,
_options$includeScore2,
includeScoreMap,
_options$baseKeyword2,
baseKeyword,
_options$limit3,
limit,
_options$offset3,
offset,
results,
scoreMap,
i,
key,
entry,
grouped,
_loop,
_key4,
_args11 = arguments;
return _regeneratorRuntime().wrap(function _callee10$(_context11) {
while (1) switch (_context11.prev = _context11.next) {
case 0:
keys = _args11.length > 1 && _args11[1] !== undefined ? _args11[1] : [];
options = _args11.length > 2 && _args11[2] !== undefined ? _args11[2] : {};
_options$includeScore2 = options.includeScoreMap, includeScoreMap = _options$includeScore2 === void 0 ? false : _options$includeScore2, _options$baseKeyword2 = options.baseKeyword, baseKeyword = _options$baseKeyword2 === void 0 ? null : _options$baseKeyword2, _options$limit3 = options.limit, limit = _options$limit3 === void 0 ? null : _options$limit3, _options$offset3 = options.offset, offset = _options$offset3 === void 0 ? 0 : _options$offset3;
results = [];
scoreMap = {};
i = offset;
case 6:
if (!(i < keys.length)) {
_context11.next = 17;
break;
}
if (!(limit !== null && results.length >= limit)) {
_context11.next = 9;
break;
}
return _context11.abrupt("break", 17);
case 9:
key = keys[i];
_context11.next = 12;
return this.getEntryByKey(list, key);
case 12:
entry = _context11.sent;
if (entry) {
results.push(entry);
if (includeScoreMap && baseKeyword) {
scoreMap[key] = _assertClassBrand(_Timelyst_brand, this, _calculateSimilarity).call(this, baseKeyword, key);
}
}
case 14:
i++;
_context11.next = 6;
break;
case 17:
if (!(options.groupByScore && includeScoreMap)) {
_context11.next = 27;
break;
}
grouped = {};
_loop = /*#__PURE__*/_regeneratorRuntime().mark(function _loop(_key4) {
var score;
return _regeneratorRuntime().wrap(function _loop$(_context10) {
while (1) switch (_context10.prev = _context10.next) {
case 0:
score = scoreMap[_key4].toFixed(2);
if (!grouped[score]) {
grouped[score] = [];
}
grouped[score].push(results.find(function (e) {
return e.key === _key4;
}));
case 3:
case "end":
return _context10.stop();
}
}, _loop);
});
_context11.t0 = _regeneratorRuntime().keys(scoreMap);
case 21:
if ((_context11.t1 = _context11.t0()).done) {
_context11.next = 26;
break;
}
_key4 = _context11.t1.value;
return _context11.delegateYield(_loop(_key4), "t2", 24);
case 24:
_context11.next = 21;
break;
case 26:
return _context11.abrupt("return", {
results: results,
scoreMap: scoreMap,
grouped: grouped
});
case 27:
return _context11.abrupt("return", includeScoreMap ? {
results: results,
scoreMap: scoreMap
} : results);
case 28:
case "end":
return _context11.stop();
}
}, _callee10, this);
}));
function getEntriesByKeys(_x13) {
return _getEntriesByKeys.apply(this, arguments);
}
return getEntriesByKeys;
}()
}, {
key: "getEntryByKey",
value: function () {
var _getEntryByKey = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee11(listName, key) {
var shard, containsKey, allEntries;
return _regeneratorRuntime().wrap(function _callee11$(_context12) {
while (1) switch (_context12.prev = _context12.next) {
case 0:
shard = _assertClassBrand(_Timelyst_brand, this, _getShardName).call(this, listName, key);
_context12.next = 3;
return this.core.has(shard, key);
case 3:
containsKey = _context12.sent;
if (containsKey) {
_context12.next = 6;
break;
}
return _context12.abrupt("return", null);
case 6:
_context12.next = 8;
return this.core.getAll(shard);
case 8:
allEntries = _context12.sent;
return _context12.abrupt("return", allEntries.find(function (e) {
return e.key === key;
}) || null);
case 10:
case "end":
return _context12.stop();
}
}, _callee11, this);
}));
function getEntryByKey(_x14, _x15) {
return _getEntryByKey.apply(this, arguments);
}
return getEntryByKey;
}() // --- List Cleanup Management ---
}, {
key: "sweepExpired",
value: function () {
var _sweepExpired = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee12(name) {
var config, shardCount, i, shard;
return _regeneratorRuntime().wrap(function _callee12$(_context13) {
while (1) switch (_context13.prev = _context13.next) {
case 0:
config = this.listConfigs.get(name) || {};
shardCount = config.shardCount || 1;
i = 0;
case 3:
if (!(i < shardCount)) {
_context13.next = 13;
break;
}
shard = shardCount === 1 ? name : "".concat(name, ":shard:").concat(i);
_context13.next = 7;
return this.core.sweepExpired(shard);
case 7:
if (!this.storage) {
_context13.next = 10;
break;
}
_context13.next = 10;
return this.storage.sweepExpired(shard);
case 10:
i++;
_context13.next = 3;
break;
case 13:
case "end":
return _context13.stop();
}
}, _callee12, this);
}));
function sweepExpired(_x16) {
return _sweepExpired.apply(this, arguments);
}
return sweepExpired;
}()
}, {
key: "startAutoCleanup",
value: function startAutoCleanup() {
var _this5 = this;
var intervalMs = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "10s";
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval);
}
this.cleanupInterval = setInterval(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee13() {
var _iterator4, _step4, name;
return _regeneratorRuntime().wrap(function _callee13$(_context14) {
while (1) switch (_context14.prev = _context14.next) {
case 0:
_iterator4 = _createForOfIteratorHelper(_this5.listConfigs.keys());
_context14.prev = 1;
_iterator4.s();
case 3:
if ((_step4 = _iterator4.n()).done) {
_context14.next = 9;
break;
}
name = _step4.value;
_context14.next = 7;
return _this5.sweepExpired(name);
case 7:
_context14.next = 3;
break;
case 9:
_context14.next = 14;
break;
case 11:
_context14.prev = 11;
_context14.t0 = _context14["catch"](1);
_iterator4.e(_context14.t0);
case 14:
_context14.prev = 14;
_iterator4.f();
return _context14.finish(14);
case 17:
case "end":
return _context14.stop();
}
}, _callee13, null, [[1, 11, 14, 17]]);
})), toMilliseconds(intervalMs || "10s"));
}
}, {
key: "stopAutoCleanup",
value: function stopAutoCleanup() {
if (this.cleanupInterval) {
clearInterval(this.cleanupInterval);
}
}
}]);
}(EventEmitter);
function _getShardName(name, key) {
var config = this.listConfigs.get(name) || {};
var shardCount = config.shardCount || 1;
if (shardCount <= 1) return name;
var hash = _assertClassBrand(_Timelyst_brand, this, _hash).call(this, key);
var shard = hash % shardCount;
return "".concat(name, ":shard:").concat(shard);
}
function _hash(input) {
var hash = 0;
for (var i = 0; i < input.length; i++) {
hash = (hash << 5) - hash + input.charCodeAt(i);
hash |= 0;
}
return Math.abs(hash);
}
function _prepareMetadata() {
var metadata = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var base = _objectSpread({}, metadata);
if (!Array.isArray(base.tags)) {
base.tags = [];
}
if (typeof base.priority !== "number") {
base.priority = 0;
}
if (typeof base.category !== "string") {
base.category = "default";
}
if (typeof base.expiresIn === "string") {
try {
base.expiresIn = toSeconds(base.expiresIn);
} catch (_unused) {}
}
return base;
}
function _calculateSimilarity(a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
if (a === b) {
return 1;
}
var matches = 0;
for (var i = 0; i < Math.min(a.length, b.length); i++) {
if (a[i] === b[i]) {
matches++;
}
}
return matches / Math.max(a.length, b.length);
}
function _persistSearchIndex() {
try