UNPKG

@trap_stevo/iotide

Version:

Provides the ultimate solution for real-time event management. Designed to make your applications dynamic and responsive, IoTide offers unparalleled flexibility in handling events on-the-fly. Effortlessly create interactive web applications, live data das

156 lines (155 loc) 7 kB
"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 _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 crypto = require("crypto"); var path = require("path"); var fs = require("fs"); var _TidalCore_brand = /*#__PURE__*/new WeakSet(); var TidalCore = /*#__PURE__*/function () { function TidalCore() { var dirPath = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : path.join("iotide_core"); var filePath = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : path.join(".tideprint"); var coreConfigurations = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; _classCallCheck(this, TidalCore); _classPrivateMethodInitSpec(this, _TidalCore_brand); var _coreConfigurations$v = coreConfigurations.validate, validate = _coreConfigurations$v === void 0 ? function () { return true; } : _coreConfigurations$v, _coreConfigurations$t = coreConfigurations.tidalOffset, tidalOffset = _coreConfigurations$t === void 0 ? null : _coreConfigurations$t, _coreConfigurations$s = coreConfigurations.secureMode, secureMode = _coreConfigurations$s === void 0 ? true : _coreConfigurations$s, _coreConfigurations$p = coreConfigurations.persist, persist = _coreConfigurations$p === void 0 ? false : _coreConfigurations$p; this.tidalOffset = tidalOffset; this.secureMode = secureMode; this.validate = validate; this.dirPath = dirPath; this.persist = persist; this.filePath = path.join(dirPath, filePath); } return _createClass(TidalCore, [{ key: "exists", value: function exists() { return this.persist && fs.existsSync(this.filePath); } }, { key: "read", value: function read() { if (!this.exists()) { return null; } try { var data = fs.readFileSync(this.filePath, "utf8").trim(); if (this.tidalOffset) { data = _assertClassBrand(_TidalCore_brand, this, _decrypt).call(this, data); } return this.validate(data) ? data : null; } catch (error) { console.log("⚠️ Did not read from TidalCore ~", error); return null; } } }, { key: "write", value: function write(value) { if (!this.persist) { return false; } try { if (!fs.existsSync(this.dirPath)) { fs.mkdirSync(this.dirPath, { recursive: true, mode: 448 }); } var data = value; if (this.tidalOffset) { data = _assertClassBrand(_TidalCore_brand, this, _encrypt).call(this, value); } var options = { encoding: "utf8" }; if (this.secureMode) { options.mode = 384; } fs.writeFileSync(this.filePath, data, options); return true; } catch (error) { console.warn("⚠️ Did not write to TidalCore ~", error); return false; } } }, { key: "getOrGenerate", value: function getOrGenerate() { var generator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () { return crypto.randomUUID(); }; var existing = this.read(); if (existing) { return existing; } var newValue = generator(); this.write(newValue); return newValue; } }, { key: "generateTideprint", value: function generateTideprint() { var generator = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () { return crypto.randomUUID(); }; var includeRaw = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; var newTideprint = generator(); var encrypted = this.tidalOffset ? _assertClassBrand(_TidalCore_brand, this, _encrypt).call(this, newTideprint) : newTideprint; return includeRaw ? { tideprint: newTideprint, encrypted: encrypted } : encrypted; } }, { key: "updateTideprint", value: function updateTideprint() { var newValue = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.generateTideprint(function () { return crypto.randomUUID(); }, true).tideprint; if (!this.validate(newValue)) { console.warn("⚠️ Did not update Tideprint. Provide a valid Tideprint."); return false; } return this.write(newValue); } }]); }(); function _deriveKey() { return crypto.createHash("sha256").update(this.tidalOffset || "").digest(); } function _encrypt(data) { var iv = crypto.randomBytes(12); var key = _assertClassBrand(_TidalCore_brand, this, _deriveKey).call(this); var cipher = crypto.createCipheriv("aes-256-gcm", key, iv); var encrypted = Buffer.concat([cipher.update(data, "utf8"), cipher["final"]()]); var tag = cipher.getAuthTag(); return Buffer.concat([iv, tag, encrypted]).toString("base64"); } function _decrypt(data) { var buffer = Buffer.from(data, "base64"); var iv = buffer.slice(0, 12); var tag = buffer.slice(12, 28); var encrypted = buffer.slice(28); var key = _assertClassBrand(_TidalCore_brand, this, _deriveKey).call(this); var decipher = crypto.createDecipheriv("aes-256-gcm", key, iv); decipher.setAuthTag(tag); return decipher.update(encrypted) + decipher["final"]("utf8"); } ; module.exports = TidalCore;