@elsikora/nestjs-crud-automator
Version:
A library for automating the creation of CRUD operations in NestJS.
90 lines (86 loc) • 3.62 kB
JavaScript
;
var cacheMode_enum = require('../../../../enum/class/authorization/cache-mode.enum.js');
require('../../../../enum/class/authorization/decision-type.enum.js');
require('../../../../enum/class/authorization/effect.enum.js');
require('../../../../enum/class/authorization/mode.enum.js');
require('../../../../enum/class/authorization/permission-match.enum.js');
require('../../../../enum/class/authorization/policy/attachment-type.enum.js');
require('../../../../enum/class/authorization/policy/on-type.enum.js');
require('../../../../enum/class/authorization/policy/source-type.enum.js');
require('../../../../enum/class/authorization/policy/status.enum.js');
require('../../../../enum/class/authorization/principal-type.enum.js');
var exception_utility = require('../../../../utility/error/exception.utility.js');
var formatUnknownForLog_utility = require('../../../../utility/format-unknown-for-log.utility.js');
class ApiAuthorizationResolverCache {
CACHE = new Map();
options;
constructor(options) {
this.configure(options);
}
clear() {
this.CACHE.clear();
}
configure(options) {
this.clear();
const mode = options?.mode;
if (mode === undefined || mode === cacheMode_enum.EApiAuthorizationCacheMode.SOURCE_FIRST) {
this.options = undefined;
return;
}
if (mode !== cacheMode_enum.EApiAuthorizationCacheMode.MEMORY) {
throw exception_utility.ErrorException(`Unknown authorization cache mode "${formatUnknownForLog_utility.FormatUnknownForLog(mode)}"`);
}
const memoryOptions = options;
if (!Number.isSafeInteger(memoryOptions.maxEntries) || memoryOptions.maxEntries <= 0) {
throw exception_utility.ErrorException("Authorization MEMORY cache maxEntries must be a positive safe integer");
}
if (!Number.isSafeInteger(memoryOptions.ttlMs) || memoryOptions.ttlMs <= 0) {
throw exception_utility.ErrorException("Authorization MEMORY cache ttlMs must be a positive safe integer");
}
this.options = {
maxEntries: memoryOptions.maxEntries,
mode: cacheMode_enum.EApiAuthorizationCacheMode.MEMORY,
ttlMs: memoryOptions.ttlMs,
};
}
get(key) {
const options = this.getMemoryOptions();
if (!options) {
return undefined;
}
const entry = this.CACHE.get(key);
if (!entry) {
return undefined;
}
if (Date.now() - entry.cachedAt >= options.ttlMs) {
this.CACHE.delete(key);
return undefined;
}
return entry.value;
}
set(key, value) {
const options = this.getMemoryOptions();
if (!options) {
return;
}
for (const [cachedKey, entry] of this.CACHE.entries()) {
if (Date.now() - entry.cachedAt >= options.ttlMs) {
this.CACHE.delete(cachedKey);
}
}
this.CACHE.delete(key);
while (this.CACHE.size >= options.maxEntries) {
const oldestKey = this.CACHE.keys().next().value;
if (oldestKey === undefined) {
break;
}
this.CACHE.delete(oldestKey);
}
this.CACHE.set(key, { cachedAt: Date.now(), value });
}
getMemoryOptions() {
return this.options?.mode === cacheMode_enum.EApiAuthorizationCacheMode.MEMORY ? this.options : undefined;
}
}
exports.ApiAuthorizationResolverCache = ApiAuthorizationResolverCache;
//# sourceMappingURL=cache.class.js.map