@trap_stevo/filetide
Version:
Revolutionizing real-time file transfer with seamless, instant communication across any device. Deliver files instantly, regardless of platform, and experience unparalleled speed and control in managing transfers. Elevate your file-sharing capabilities wi
144 lines (143 loc) • 6.45 kB
JavaScript
"use strict";
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"); }
const LogicTide = require("@trap_stevo/logictide");
const Timelyst = require("@trap_stevo/timelyst");
var _onEntryAddedHooks = /*#__PURE__*/new WeakMap();
var _onExpireHooks = /*#__PURE__*/new WeakMap();
var _manager = /*#__PURE__*/new WeakMap();
var _config = /*#__PURE__*/new WeakMap();
var _lists = /*#__PURE__*/new WeakMap();
var _TideSentinelManager_brand = /*#__PURE__*/new WeakSet();
class TideSentinelManager {
constructor(config = {}) {
_classPrivateMethodInitSpec(this, _TideSentinelManager_brand);
_classPrivateFieldInitSpec(this, _onEntryAddedHooks, {});
_classPrivateFieldInitSpec(this, _onExpireHooks, {});
_classPrivateFieldInitSpec(this, _manager, void 0);
_classPrivateFieldInitSpec(this, _config, void 0);
_classPrivateFieldInitSpec(this, _lists, void 0);
const {
lists = {},
enableOfflineStorage = true,
autoCleanupInterval = "15s",
basePath = LogicTide.runSync("tide-sentinel-path"),
...rest
} = config;
_classPrivateFieldSet(_config, this, {
enableOfflineStorage,
autoCleanupInterval,
basePath,
...rest
});
_classPrivateFieldSet(_lists, this, lists);
_classPrivateFieldSet(_manager, this, new Timelyst(_classPrivateFieldGet(_config, this)));
_classPrivateFieldGet(_manager, this).on("entry-expired", (list, key, entry) => {
const hook = _classPrivateFieldGet(_onExpireHooks, this)[list];
if (typeof hook === "function") {
hook(key, entry);
}
});
_classPrivateFieldGet(_manager, this).on("entry-added", (list, key, entry) => {
const hook = _classPrivateFieldGet(_onEntryAddedHooks, this)[list];
if (typeof hook === "function") {
hook(key, entry);
}
});
}
registerEntryAddedHook(list, callback) {
if (typeof callback === "function") {
_classPrivateFieldGet(_onEntryAddedHooks, this)[list] = callback;
}
}
registerExpireHook(list, callback) {
if (typeof callback === "function") {
_classPrivateFieldGet(_onExpireHooks, this)[list] = callback;
}
}
async createList(name, options = {}) {
_classPrivateFieldGet(_lists, this)[name] = options;
return _classPrivateFieldGet(_manager, this).createList(name, options);
}
async deleteList(name) {
delete _classPrivateFieldGet(_lists, this)[name];
return _classPrivateFieldGet(_manager, this).deleteList(name);
}
async reconfigureList(name, newOptions = {}) {
Object.assign(_classPrivateFieldGet(_lists, this)[name] = _classPrivateFieldGet(_lists, this)[name] || {}, newOptions);
await _classPrivateFieldGet(_manager, this).deleteList(name);
return _classPrivateFieldGet(_manager, this).createList(name, _classPrivateFieldGet(_lists, this)[name]);
}
async banClient(clientID, options = {}) {
const {
reason = "Unspecified.",
duration = null,
tags = [],
priority = 10,
category = "ban",
details = {}
} = options;
await _assertClassBrand(_TideSentinelManager_brand, this, _ensureList).call(this, "banned-tiders");
return _classPrivateFieldGet(_manager, this).addToList("banned-tiders", clientID, {
expiresIn: duration,
priority,
category,
tags,
details
});
}
async unbanClient(clientID) {
await _assertClassBrand(_TideSentinelManager_brand, this, _ensureList).call(this, "banned-tiders");
return _classPrivateFieldGet(_manager, this).removeFromList("banned-tiders", clientID);
}
async clientBanned(clientID) {
await _assertClassBrand(_TideSentinelManager_brand, this, _ensureList).call(this, "banned-tiders");
return _classPrivateFieldGet(_manager, this).inList("banned-tiders", clientID);
}
async getBanEntry(clientID) {
await _assertClassBrand(_TideSentinelManager_brand, this, _ensureList).call(this, "banned-tiders");
return _classPrivateFieldGet(_manager, this).getEntryByKey("banned-tiders", clientID);
}
async filterBannedClients(filter = {}) {
await _assertClassBrand(_TideSentinelManager_brand, this, _ensureList).call(this, "banned-tiders");
return _classPrivateFieldGet(_manager, this).filterList("banned-tiders", filter);
}
async addToList(list, key, data) {
await _assertClassBrand(_TideSentinelManager_brand, this, _ensureList).call(this, list);
return _classPrivateFieldGet(_manager, this).addToList(list, key, data);
}
async removeFromList(list, key) {
await _assertClassBrand(_TideSentinelManager_brand, this, _ensureList).call(this, list);
return _classPrivateFieldGet(_manager, this).removeFromList(list, key);
}
async inList(list, key) {
await _assertClassBrand(_TideSentinelManager_brand, this, _ensureList).call(this, list);
return _classPrivateFieldGet(_manager, this).inList(list, key);
}
async getEntry(list, key) {
await _assertClassBrand(_TideSentinelManager_brand, this, _ensureList).call(this, list);
return _classPrivateFieldGet(_manager, this).getEntryByKey(list, key);
}
async filterList(list, filter = {}) {
await _assertClassBrand(_TideSentinelManager_brand, this, _ensureList).call(this, list);
return _classPrivateFieldGet(_manager, this).filterList(list, filter);
}
async findSimilarKeys(key, options = {}) {
return _classPrivateFieldGet(_manager, this).findSimilarKeys(key, options);
}
}
async function _ensureList(name) {
if (!_classPrivateFieldGet(_lists, this)[name]) {
_classPrivateFieldGet(_lists, this)[name] = {};
}
const exists = await _classPrivateFieldGet(_manager, this).listOperational(name);
if (!exists) {
await _classPrivateFieldGet(_manager, this).createList(name, _classPrivateFieldGet(_lists, this)[name]);
}
}
;
module.exports = TideSentinelManager;