UNPKG

@lock-dev/ip-filter

Version:

IP filtering module for lock.dev security framework

166 lines (161 loc) 4.28 kB
// src/storage/memory.ts import { LRUCache } from "lru-cache"; var MemoryIPCacheStore = class { constructor(config) { this.cache = new LRUCache({ max: config.cacheSize || 1e4, ttl: config.cacheTtl || 36e5, ttlAutopurge: true }); } async init() { return Promise.resolve(); } async get(key) { const value = this.cache.get(key); return value === void 0 ? null : value; } async set(key, value) { this.cache.set(key, value); } async close() { return Promise.resolve(); } }; // src/storage/redis.ts var RedisIPCacheStore = class { constructor(config) { this.keyPrefix = config.redis?.keyPrefix || "ipfilter:"; this.config = config.redis; this.ttl = Math.floor((config.cacheTtl || 36e5) / 1e3); } async init() { try { const { createClient } = await import("redis"); if (this.config.url) { this.client = createClient({ url: this.config.url }); } else { this.client = createClient({ socket: { host: this.config.host || "localhost", port: this.config.port || 6379 }, username: this.config.username, password: this.config.password, database: this.config.database || 0 }); } await this.client.connect(); this.client.on("error", (err) => { console.error("Redis client error:", err); }); } catch (error) { console.error("Failed to initialize Redis client:", error); throw new Error("Redis initialization failed"); } } async get(key) { try { const data = await this.client.get(this.keyPrefix + key); if (data === null) return null; return data === "true"; } catch (error) { console.error("Redis get error:", error); return null; } } async set(key, value) { try { await this.client.set(this.keyPrefix + key, value.toString(), { EX: this.ttl }); } catch (error) { console.error("Redis set error:", error); } } async close() { try { if (this.client) { await this.client.quit(); } } catch (error) { console.error("Redis close error:", error); } } }; // src/storage/upstash.ts var UpstashIPCacheStore = class { constructor(config) { this.keyPrefix = config.upstash?.keyPrefix || "ipfilter:"; this.config = config.upstash; this.ttl = Math.floor((config.cacheTtl || 36e5) / 1e3); } async init() { try { const { Redis } = await import("@upstash/redis"); this.client = new Redis({ url: this.config.url, token: this.config.token }); await this.client.ping(); } catch (error) { console.error("Failed to initialize Upstash client:", error); throw new Error("Upstash initialization failed"); } } async get(key) { try { const data = await this.client.get(this.keyPrefix + key); if (data === null) return null; if (typeof data === "boolean") { return data; } if (typeof data === "string") { return data === "true"; } return null; } catch (error) { console.error("Upstash get error:", error); return null; } } async set(key, value) { try { await this.client.set(this.keyPrefix + key, value.toString(), { ex: this.ttl }); } catch (error) { console.error("Upstash set error:", error); } } async close() { return Promise.resolve(); } }; // src/storage/index.ts async function createCacheStore(config) { const storageType = config.storage || "memory"; let store; switch (storageType) { case "redis": if (!config.redis) { throw new Error("Redis configuration is required when using Redis storage"); } store = new RedisIPCacheStore(config); break; case "upstash": if (!config.upstash) { throw new Error("Upstash configuration is required when using Upstash storage"); } store = new UpstashIPCacheStore(config); break; case "memory": default: store = new MemoryIPCacheStore(config); break; } await store.init(); return store; } export { MemoryIPCacheStore, RedisIPCacheStore, UpstashIPCacheStore, createCacheStore };