UNPKG

adauth

Version:

Authenticate against an Active Directory domain via LDAP

61 lines (60 loc) 2.74 kB
"use strict"; var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _Cache_instances, _Cache_logTrace; Object.defineProperty(exports, "__esModule", { value: true }); const assert_1 = __importDefault(require("assert")); const lru_cache_1 = __importDefault(require("lru-cache")); class Cache { constructor(size, expiry, log, name) { _Cache_instances.add(this); assert_1.default.ok(size !== undefined); assert_1.default.ok(expiry !== undefined); this.size = size; this.expiry = expiry * 1000; this.log = log; this.name = name ? name + ' ' : ''; this.items = new lru_cache_1.default({ max: this.size }); } reset() { __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_logTrace).call(this, '%scache reset', this.name); this.items.reset(); } get(key) { assert_1.default.ok(key !== undefined); const cached = this.items.get(key); if (cached) { if (new Date().getTime() - cached.ctime <= this.expiry) { __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_logTrace).call(this, '%scache hit: key="%s": %o', this.name, key, cached); return cached.value; } } __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_logTrace).call(this, '%scache miss: key="%s"', this.name, key); } set(key, value) { assert_1.default.ok(key !== undefined); const item = { value, ctime: new Date().getTime(), }; __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_logTrace).call(this, '%scache set: key="%s": %o', this.name, key, item); this.items.set(key, item); return Object.assign({}, item); } delete(key) { __classPrivateFieldGet(this, _Cache_instances, "m", _Cache_logTrace).call(this, '%scache del: key="%s"', this.name, key); this.items.del(key); } } exports.default = Cache; _Cache_instances = new WeakSet(), _Cache_logTrace = function _Cache_logTrace(formatString, ...args) { if (this.log) { this.log.trace(formatString, ...args); } };