@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
264 lines (263 loc) • 12.7 kB
JavaScript
;
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 _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 _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
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 path = require("path");
var fs = require("fs");
var _StarMesh_brand = /*#__PURE__*/new WeakSet();
var StarMesh = /*#__PURE__*/function () {
function StarMesh(_ref) {
var _this = this;
var vault = _ref.vault,
_vaultID = _ref.vaultID,
_ref$peers = _ref.peers,
peers = _ref$peers === void 0 ? [] : _ref$peers,
_ref$collections = _ref.collections,
collections = _ref$collections === void 0 ? {} : _ref$collections,
_ref$broadcastInterva = _ref.broadcastInterval,
broadcastInterval = _ref$broadcastInterva === void 0 ? null : _ref$broadcastInterva,
_ref$syncStatePath = _ref.syncStatePath,
syncStatePath = _ref$syncStatePath === void 0 ? ".star-vault.sync.json" : _ref$syncStatePath;
_classCallCheck(this, StarMesh);
_classPrivateMethodInitSpec(this, _StarMesh_brand);
this.collections = collections || {};
this.processedEvents = new Set();
this.peerGroups = new Map();
this.peers = new Map();
this.vaultID = _vaultID;
this.eventQueue = [];
this.vault = vault;
this.broadcastInterval = broadcastInterval;
this.meshBroadcastPaused = false;
this.meshBroadcastHandle = null;
this.peerFilters = new Map();
this.dataFilters = new Map();
this.syncStatePath = syncStatePath;
this.syncCheckpointMap = _assertClassBrand(_StarMesh_brand, this, _loadSyncState).call(this);
(peers || []).forEach(function (peer) {
return _this.addPeer(peer);
});
if (broadcastInterval) {
_assertClassBrand(_StarMesh_brand, this, _startMeshBroadcast).call(this);
}
}
return _createClass(StarMesh, [{
key: "setSyncCheckpoint",
value: function setSyncCheckpoint(peerID, collection, eventID) {
var key = "".concat(collection, ":").concat(peerID);
this.syncCheckpointMap.lastProcessed[key] = eventID;
_assertClassBrand(_StarMesh_brand, this, _saveSyncState).call(this);
}
}, {
key: "getSyncCheckpoint",
value: function getSyncCheckpoint(peerID, collection) {
var key = "".concat(collection, ":").concat(peerID);
return this.syncCheckpointMap.lastProcessed[key] || null;
}
}, {
key: "setBroadcastFilterForVault",
value: function setBroadcastFilterForVault(vaultID, onBroadcastFilter) {
if (typeof onBroadcastFilter === "function") {
this.peerFilters.set(vaultID, onBroadcastFilter);
}
}
}, {
key: "setDataFilterForVault",
value: function setDataFilterForVault(vaultID, onDataFilter) {
if (typeof onDataFilter === "function") {
this.dataFilters.set(vaultID, onDataFilter);
}
}
}, {
key: "restartMeshBroadcast",
value: function restartMeshBroadcast(newInterval) {
if (!newInterval) {
return;
}
this.broadcastInterval = newInterval;
_assertClassBrand(_StarMesh_brand, this, _startMeshBroadcast).call(this);
}
}, {
key: "pauseMeshBroadcast",
value: function pauseMeshBroadcast() {
this.meshBroadcastPaused = true;
}
}, {
key: "resumeMeshBroadcast",
value: function resumeMeshBroadcast() {
this.meshBroadcastPaused = false;
}
}, {
key: "stopMeshBroadcast",
value: function stopMeshBroadcast() {
if (this.meshBroadcastHandle) {
clearInterval(this.meshBroadcastHandle);
this.broadcastIntervalHandle = null;
return true;
}
return false;
}
}, {
key: "onVaultReconnect",
value: function onVaultReconnect(peerID, handler) {
var _this2 = this;
var collections = Object.keys(this.collections);
collections.forEach(function (collection) {
_assertClassBrand(_StarMesh_brand, _this2, _replaySyncLog).call(_this2, peerID, collection, function (entry) {
handler(peerID, collection, entry);
});
});
}
}, {
key: "syncSnapshotToVault",
value: function syncSnapshotToVault(peerID, collection, handler) {
var records = this.vault.query(collection).execute();
var _iterator = _createForOfIteratorHelper(records),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var record = _step.value;
var eventID = "snapshot_".concat(record.id);
handler(peerID, collection, {
eventID: eventID,
data: record
});
this.setSyncCheckpoint(peerID, collection, eventID);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
}]);
}();
function _loadSyncState() {
try {
if (fs.existsSync(this.syncStatePath)) {
return JSON.parse(fs.readFileSync(this.syncStatePath, "utf8"));
}
} catch (error) {
console.warn("[StarVault | Star mesh] Did not load sync state ~", error);
}
return {
lastProcessed: {}
};
}
function _saveSyncState() {
try {
fs.writeFileSync(this.syncStatePath, JSON.stringify(this.syncCheckpointMap, null, 2));
} catch (error) {
console.warn("[StarVault | Star mesh] Did not save sync state ~", error);
}
}
function _startMeshBroadcast() {
var _this3 = this;
if (this.meshBroadcastHandle) {
clearInterval(this.meshBroadcastHandle);
}
this.meshBroadcastHandle = setInterval(function () {
if (_this3.meshBroadcastPaused) {
return;
}
var _iterator2 = _createForOfIteratorHelper(_this3.peers),
_step2;
try {
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
var _step2$value = _slicedToArray(_step2.value, 2),
vaultID = _step2$value[0],
peer = _step2$value[1];
var collectionFilter = _this3.peerFilters.get(vaultID);
var dataFilter = _this3.dataFilters.get(vaultID);
var _loop = function _loop() {
var _this3$vault$wal, _this3$vault$wal$reco;
var collection = _Object$keys[_i];
if (collectionFilter && typeof collectionFilter === "function" && !collectionFilter(collection)) {
return 0; // continue
}
var log = (_this3$vault$wal = _this3.vault.wal) === null || _this3$vault$wal === void 0 || (_this3$vault$wal$reco = _this3$vault$wal.recoverWrites) === null || _this3$vault$wal$reco === void 0 ? void 0 : _this3$vault$wal$reco.call(_this3$vault$wal, collection);
if (!Array.isArray(log)) {
return 0; // continue
}
var lastEventID = _this3.getSyncCheckpoint(vaultID, collection);
var startIndex = 0;
if (lastEventID) {
var index = log.findIndex(function (entry) {
return entry.eventID === lastEventID;
});
if (index !== -1) {
startIndex = index + 1;
}
}
for (var i = startIndex; i < log.length; i++) {
var entry = log[i];
if (!entry || !entry.eventID) {
continue;
}
if (typeof dataFilter === "function" && !dataFilter(entry)) {
continue;
}
if (typeof peer.send === "function") {
peer.send({
from: _this3.vaultID,
to: vaultID,
collection: collection,
entry: entry
});
}
_this3.setSyncCheckpoint(vaultID, collection, entry.eventID);
}
},
_ret;
for (var _i = 0, _Object$keys = Object.keys(_this3.collections); _i < _Object$keys.length; _i++) {
_ret = _loop();
if (_ret === 0) continue;
}
}
} catch (err) {
_iterator2.e(err);
} finally {
_iterator2.f();
}
}, this.broadcastInterval);
}
function _replaySyncLog(peerID, collection, onReplay) {
var _this$vault$wal, _this$vault$wal$recov;
var lastEventID = this.getSyncCheckpoint(peerID, collection);
var log = (_this$vault$wal = this.vault.wal) === null || _this$vault$wal === void 0 || (_this$vault$wal$recov = _this$vault$wal.recoverWrites) === null || _this$vault$wal$recov === void 0 ? void 0 : _this$vault$wal$recov.call(_this$vault$wal, collection);
if (!Array.isArray(log)) {
return;
}
var startIndex = 0;
if (lastEventID) {
var index = log.findIndex(function (entry) {
return entry.eventID === lastEventID;
});
if (index !== -1) {
startIndex = index + 1;
}
}
for (var i = startIndex; i < log.length; i++) {
var entry = log[i];
if (!entry || !entry.eventID) {
continue;
}
onReplay(entry);
this.setSyncCheckpoint(peerID, collection, entry.eventID);
}
}
;
module.exports = StarMesh;