n8n-bookstack-agent-tool
Version:
Comprehensive BookStack API integration tool for n8n AI Agent with MCP framework compatibility
137 lines • 3.83 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BookStackCache = exports.CacheManager = void 0;
class CacheManager {
constructor(config = {}) {
this.config = {
defaultTtl: 300000, // 5 minutes
maxSize: 1000,
enabled: true,
...config
};
this.cache = new Map();
}
get(key) {
if (!this.config.enabled) {
return null;
}
const entry = this.cache.get(key);
if (!entry) {
return null;
}
const now = Date.now();
if (now - entry.timestamp > entry.ttl) {
this.cache.delete(key);
return null;
}
return entry.data;
}
set(key, data, ttl) {
if (!this.config.enabled) {
return;
}
if (this.cache.size >= this.config.maxSize) {
this.evictOldest();
}
const entry = {
data,
timestamp: Date.now(),
ttl: ttl || this.config.defaultTtl
};
this.cache.set(key, entry);
}
delete(key) {
return this.cache.delete(key);
}
clear() {
this.cache.clear();
}
has(key) {
if (!this.config.enabled) {
return false;
}
const entry = this.cache.get(key);
if (!entry) {
return false;
}
const now = Date.now();
if (now - entry.timestamp > entry.ttl) {
this.cache.delete(key);
return false;
}
return true;
}
size() {
return this.cache.size;
}
getStats() {
return {
size: this.cache.size,
maxSize: this.config.maxSize,
enabled: this.config.enabled
};
}
evictOldest() {
let oldestKey = null;
let oldestTimestamp = Date.now();
for (const [key, entry] of this.cache.entries()) {
if (entry.timestamp < oldestTimestamp) {
oldestTimestamp = entry.timestamp;
oldestKey = key;
}
}
if (oldestKey) {
this.cache.delete(oldestKey);
}
}
static generateCacheKey(resource, operation, params = {}) {
const sortedParams = Object.keys(params)
.sort()
.reduce((acc, key) => {
acc[key] = params[key];
return acc;
}, {});
return `${resource}:${operation}:${JSON.stringify(sortedParams)}`;
}
}
exports.CacheManager = CacheManager;
class BookStackCache extends CacheManager {
constructor(config = {}) {
super({
defaultTtl: 300000, // 5 minutes for BookStack data
maxSize: 500,
enabled: true,
...config
});
}
cacheResource(resource, id, data, ttl) {
const key = `${resource}:${id}`;
this.set(key, data, ttl);
}
getResource(resource, id) {
const key = `${resource}:${id}`;
return this.get(key);
}
cacheList(resource, params, data, ttl) {
const key = CacheManager.generateCacheKey(resource, 'list', params);
this.set(key, data, ttl);
}
getList(resource, params) {
const key = CacheManager.generateCacheKey(resource, 'list', params);
return this.get(key);
}
invalidateResource(resource, id) {
if (id) {
this.delete(`${resource}:${id}`);
}
const keysToDelete = [];
for (const [key] of this.cache.entries()) {
if (key.startsWith(`${resource}:list:`)) {
keysToDelete.push(key);
}
}
keysToDelete.forEach(key => this.delete(key));
}
}
exports.BookStackCache = BookStackCache;
//# sourceMappingURL=cache-manager.js.map