Source: store.js

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InMemoryStore = void 0;
/**
 * In-memory store implementation for rate limiting.
 * Suitable for single-instance deployments or development.
 * For distributed deployments, use a custom store backed by Redis or similar.
 */
class InMemoryStore {
    constructor(maxAgeMs = 60000) {
        this.store = new Map();
        this.maxAge = maxAgeMs;
        // Periodically clean up old entries
        this.cleanupInterval = setInterval(() => {
            this.cleanup();
        }, Math.max(5000, maxAgeMs / 2));
        this.cleanupInterval.unref();
    }
    async get(key) {
        const entry = this.store.get(key);
        return entry ? deepCopy(entry) : null;
    }
    async set(key, entry) {
        this.store.set(key, deepCopy(entry));
    }
    async reset(key) {
        this.store.delete(key);
    }
    cleanup() {
        const now = Date.now();
        const keysToDelete = [];
        for (const [key, entry] of this.store.entries()) {
            const lastAccessedAt = ('lastAccessedAt' in entry ? entry.lastAccessedAt : now);
            if (now - lastAccessedAt > this.maxAge) {
                keysToDelete.push(key);
            }
        }
        for (const key of keysToDelete) {
            this.store.delete(key);
        }
    }
    /**
     * Destroy the store and clear cleanup interval
     */
    destroy() {
        clearInterval(this.cleanupInterval);
        this.store.clear();
    }
    /**
     * Get current number of tracked clients
     */
    size() {
        return this.store.size;
    }
}
exports.InMemoryStore = InMemoryStore;
function deepCopy(value) {
    return JSON.parse(JSON.stringify(value));
}
//# sourceMappingURL=store.js.map