UNPKG

@mollitia/redis

Version:
131 lines (130 loc) 4.28 kB
var h = Object.defineProperty; var d = (n, e, t) => e in n ? h(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t; var l = (n, e, t) => (d(n, typeof e != "symbol" ? e + "" : e, t), t); import * as c from "mollitia"; import { createClient as u } from "redis"; class g { constructor(e) { l(this, "client"); l(this, "prefix", "mollitia"); l(this, "initializePromise"); l(this, "logger"); const t = { socket: { reconnectStrategy: () => 500 }, disableOfflineQueue: !0 //disableOfflineQueue should be set to true to avoid blocking requests when Redis is down }; e.password && (t.password = e.password), e.username && (t.username = e.username), e.url ? t.url = e.url : (t.socket.host = e.host, t.socket.port = e.port), this.client = u(t), this.logger = e.logger, this.initializePromise = new Promise((s) => { this.client.on("ready", () => { var i; (i = this.logger) == null || i.debug("Redis Ready"), s(); }), this.client.on("error", () => { var i; (i = this.logger) == null || i.debug("Redis Connection Error"); }); }), this.client.connect(); } async getState(e) { const t = {}; await this.initializePromise; const s = await this.client.keys(`${this.prefix}::module::${e}::*`); for (const i of s) { const a = await this.client.get(i); if (a) try { t[i.substring(i.lastIndexOf("::") + 2)] = JSON.parse(a); } catch { } } return t; } async setState(e, t, s = 0) { await this.initializePromise; for await (const i of t) { const a = this.getKeyName(e, i.key); i.value ? (await this.client.set(a, JSON.stringify(i.value)), s && this.client.expire(a, Math.ceil(s / 1e3))) : await this.client.del(a); } } async clearState(e) { try { const t = await this.client.keys(`${this.prefix}::module::${e}::*`); for (const s of t) await this.client.del(s); } catch { } } getKeyName(e, t) { return t ? `${this.prefix}::module::${e}::${t}` : `${this.prefix}::module::${e}`; } } const w = "0.0.2", x = []; class S { constructor(e) { l(this, "redis"); l(this, "getMaxDelay", 500); l(this, "setMaxDelay", 500); l(this, "ttl", 0); l(this, "logger"); var t; if (this.logger = e.logger, !e.url && (!e.host || !e.port)) { (t = this.logger) == null || t.warn("Redis configuration is invalid"); return; } this.redis = new g(e), this.getMaxDelay = e.getMaxDelay || 500, this.setMaxDelay = e.setMaxDelay ?? 500, this.ttl = e.ttl || 0; } async getStateWithRedis(e, t) { return new Promise((s, i) => { const a = setTimeout(() => { i("Getting the state from Redis timed out."); }, t); try { this.redis.getState(e).then((r) => { clearTimeout(a), s(r); }); } catch { clearTimeout(a), i("Error occurred while trying to get the state from Redis."); } }); } async setStateWithRedis(e, t, s, i) { return new Promise((a, r) => { const o = setTimeout(() => { r("Setting the state in Redis timed out."); }, s); try { this.redis.setState(e, t, i).then(() => { clearTimeout(o), a(); }); } catch { clearTimeout(o), r("Error occurred while trying to set the state in Redis"); } }); } moduleOverride(e, t, s, i) { e.getState = async () => this.getStateWithRedis(e.name, t), e.setState = async (a, r = 0) => this.setStateWithRedis(e.name, a, s, r || i), e.clearState = async () => this.redis.clearState(e.name); } onModuleCreate(e, t) { var s; if ((s = t.redis) != null && s.use && this.redis) { const i = t.redis.getMaxDelay || this.getMaxDelay, a = t.redis.setMaxDelay ?? this.setMaxDelay, r = t.redis.ttl || this.ttl; switch (e.constructor.name) { case c.Ratelimit.name: { this.moduleOverride(e, i, a, r); break; } case c.SlidingCountBreaker.name: case c.SlidingTimeBreaker.name: { this.moduleOverride(e, i, a, r); break; } } } } } export { S as RedisAddon, x as modules, w as version };