UNPKG

@lock-dev/geo-block

Version:

Geographic blocking module for lock.dev security framework

180 lines (174 loc) 4.92 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __esm = (fn, res) => function __init() { return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/storage/redis.ts var RedisGeoCacheStore = class { constructor(config) { this.keyPrefix = config.redis?.keyPrefix || "geocache:"; this.config = config.redis; this.ttl = config.cacheTtl || 3600; } 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 }, 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(ip) { try { const data = await this.client.get(this.keyPrefix + ip); return data ? JSON.parse(data) : null; } catch (error) { console.error("Redis get error:", error); return null; } } async set(ip, value) { try { await this.client.set(this.keyPrefix + ip, JSON.stringify(value), { 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 UpstashGeoCacheStore = class { constructor(config) { this.keyPrefix = config.upstash?.keyPrefix || "geocache:"; this.config = config.upstash; this.ttl = config.cacheTtl || 3600; } 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(ip) { try { const data = await this.client.get(this.keyPrefix + ip); return data ? JSON.parse(data) : null; } catch (error) { console.error("Upstash get error:", error); return null; } } async set(ip, value) { try { await this.client.set(this.keyPrefix + ip, JSON.stringify(value), { ex: this.ttl }); } catch (error) { console.error("Upstash set error:", error); } } async close() { return Promise.resolve(); } }; // src/storage/memory.ts import { LRUCache } from "lru-cache"; var MemoryGeoCacheStore = class { constructor(config) { this.cache = new LRUCache({ max: config.cacheSize || 1e4, ttl: config.cacheTtl || 36e5, ttlAutopurge: true }); } async init() { return Promise.resolve(); } async get(ip) { return this.cache.get(ip) || null; } async set(ip, value) { this.cache.set(ip, value); } 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 RedisGeoCacheStore(config); break; case "upstash": if (!config.upstash) { throw new Error("Upstash configuration is required when using Upstash storage"); } store = new UpstashGeoCacheStore(config); break; case "memory": default: store = new MemoryGeoCacheStore(config); break; } await store.init(); return store; } export { __esm, __export, __toCommonJS, RedisGeoCacheStore, UpstashGeoCacheStore, MemoryGeoCacheStore, createCacheStore };