UNPKG

@thorium-dev-group/x402-mcp-extension

Version:
77 lines 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InMemoryStorage = void 0; class InMemoryStorage { static instance; storage = new Map(); defaultTtl; maxSize; constructor(config = {}) { this.defaultTtl = config.ttl || 300000; this.maxSize = config.maxSize || 10000; } static getInstance(config = {}) { if (!InMemoryStorage.instance) { InMemoryStorage.instance = new InMemoryStorage(config); } return InMemoryStorage.instance; } static create(config = {}) { return new InMemoryStorage(config); } async get(key) { const entry = this.storage.get(key); if (!entry) return null; if (entry.ttl && Date.now() - entry.timestamp > entry.ttl) { this.storage.delete(key); return null; } return entry.value; } async set(key, value, ttl) { this.cleanupExpired(); if (this.storage.size >= this.maxSize) { this.evictOldest(); } this.storage.set(key, { value, timestamp: Date.now(), ttl: ttl || this.defaultTtl, }); } async has(key) { const value = await this.get(key); return value !== null; } async delete(key) { this.cleanupExpired(); return this.storage.delete(key); } async clear() { this.storage.clear(); } cleanupExpired() { const now = Date.now(); for (const [key, entry] of this.storage.entries()) { if (entry.ttl && now - entry.timestamp > entry.ttl) { this.storage.delete(key); } } } evictOldest() { const entries = Array.from(this.storage.entries()); entries.sort((a, b) => a[1].timestamp - b[1].timestamp); const toEvict = Math.ceil(this.maxSize * 0.1); for (let i = 0; i < toEvict && i < entries.length; i++) { this.storage.delete(entries[i][0]); } } static destroy() { if (InMemoryStorage.instance) { InMemoryStorage.instance = null; } } } exports.InMemoryStorage = InMemoryStorage; //# sourceMappingURL=InMemoryStorage.js.map