"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/redis-cache-adapter.ts var _cacheinterop = require('@soluble/cache-interop'); // src/redis-async.util.ts var _util = require('util'); var getAsyncRedisClient = (redis) => { return { get: _util.promisify.call(void 0, redis.get).bind(redis), set: _util.promisify.call(void 0, redis.set).bind(redis), setex: _util.promisify.call(void 0, redis.setex).bind(redis), exists: _util.promisify.call(void 0, redis.exists).bind(redis), del: _util.promisify.call(void 0, redis.del).bind(redis), flushdb: _util.promisify.call(void 0, redis.flushdb).bind(redis) }; }; // src/redis-connection.ts var RedisConnection = class { constructor(redis) { this.quit = async () => { const asyncQuit = _util.promisify.call(void 0, this.redis.quit).bind(this.redis); return asyncQuit().then((reply) => reply === "OK").catch((_e) => { return false; }); }; this.redis = redis; } /** * Access directly the wrapped ioredis connection * Warning: this is not guarantee by cache-interop API stability */ getNativeConnection() { return this.redis; } }; // src/redis-connection.factory.ts var _redis = require('redis'); // src/redis-dsn.util.ts var _dsnparser = require('@soluble/dsn-parser'); var getTlsOptions = (driver, host) => { if (driver !== "rediss") { return null; } const tldHostname = /\.[a-z]{1,10}$/i.test(host); return { ...tldHostname ? { host } : {} }; }; var getRedisOptionsFromDsn = (dsn, clientOptions, dsnOverrides) => { const parsed = _dsnparser.parseDsn.call(void 0, dsn, { lowercaseDriver: true, overrides: dsnOverrides || {} }); if (!parsed.success) { throw new Error(`Can't parse DSN, reason ${parsed.reason}`); } const { driver, host, port, user, pass, db } = { ...parsed.value, ..._nullishCoalesce(dsnOverrides, () => ( {})) }; if (!["redis", "rediss"].includes(driver)) { throw new Error(`Unsupported driver '${driver}', must be redis or rediss`); } const tlsOptions = getTlsOptions(driver, host); return { ...{ host, ...port !== void 0 ? { port } : {}, ...user !== void 0 ? { username: user } : {}, ...pass !== void 0 ? { auth_pass: pass } : {}, ...tlsOptions !== null ? { tls: tlsOptions } : {}, ...db !== void 0 ? { db } : {} }, ..._nullishCoalesce(clientOptions, () => ( {})) }; }; // src/redis-connection.factory.ts var isNativeRedisClient = (val) => { return val instanceof _redis.RedisClient; }; var createRedisNativeConnection = (options) => { const opts = typeof options === "string" ? getRedisOptionsFromDsn(options) : options; if (!opts) { throw new Error("Invalid redis connection options"); } return _redis.createClient.call(void 0, opts); }; var createRedisConnection = (conn) => { if (isNativeRedisClient(conn)) { return new RedisConnection(conn); } return new RedisConnection(createRedisNativeConnection(conn)); }; // src/redis-cache-adapter.ts var RedisCacheAdapter = class extends _cacheinterop.AbstractCacheAdapter { /** * @throws Error if redis connection can't be created */ constructor(options) { super(); this.get = async (key, options) => { if (!_cacheinterop.Guards.isValidCacheKey(key)) { return _cacheinterop.CacheItemFactory.fromInvalidCacheKey(key); } const { defaultValue = null, disableCache = false } = _nullishCoalesce(options, () => ( {})); if (disableCache) { return _cacheinterop.CacheItemFactory.fromCacheMiss({ key, defaultValue }); } let data; try { data = await this.asyncRedis.get(key); } catch (e) { return _cacheinterop.CacheItemFactory.fromErr({ key, error: this.errorHelper.getCacheException( ["get", key], "READ_ERROR", e ) }); } const noData = data === null; return _cacheinterop.CacheItemFactory.fromOk({ key, data: noData ? defaultValue : data, isHit: !noData }); }; this.set = async (key, value, options) => { if (!_cacheinterop.Guards.isValidCacheKey(key)) { return this.errorHelper.getInvalidCacheKeyException(["set", key]); } const { ttl = 0, disableCache = false } = _nullishCoalesce(options, () => ( {})); if (disableCache) { return false; } let v = value; if (_cacheinterop.Guards.isCacheValueProviderFn(value)) { try { v = await _cacheinterop.executeValueProviderFn.call(void 0, value); } catch (e) { return this.errorHelper.getCacheProviderException("set", e); } } if (v === null) return true; if (!_cacheinterop.Guards.isValidRedisValue(v)) { return this.errorHelper.getUnsupportedValueException(["set", key], v); } const setOp = ttl > 0 ? this.asyncRedis.setex(key, ttl, v) : this.asyncRedis.set(key, v); return setOp.then((reply) => reply === "OK").catch((e) => { return this.errorHelper.getCacheException( ["set", key], "WRITE_ERROR", e ); }); }; this.has = async (key, options) => { var _a; if (!_cacheinterop.Guards.isValidCacheKey(key)) { (_a = options == null ? void 0 : options.onError) == null ? void 0 : _a.call( options, this.errorHelper.getInvalidCacheKeyException(["has", key]) ); return void 0; } const { disableCache = false } = _nullishCoalesce(options, () => ( {})); if (disableCache) { return false; } return this.asyncRedis.exists(key).then((count) => count === 1).catch((e) => { var _a2; (_a2 = options == null ? void 0 : options.onError) == null ? void 0 : _a2.call( options, this.errorHelper.getCacheException(["has", key], "COMMAND_ERROR", e) ); return void 0; }); }; this.delete = async (key, options) => { if (!_cacheinterop.Guards.isValidCacheKey(key)) { return this.errorHelper.getInvalidCacheKeyException(["delete", key]); } const { disableCache = false } = _nullishCoalesce(options, () => ( {})); if (disableCache) { return false; } return this.asyncRedis.del(key).then((count) => count === 1).catch((e) => { return this.errorHelper.getCacheException( ["delete", key], "WRITE_ERROR", e ); }); }; this.clear = async () => { return this.asyncRedis.flushdb().then((_reply) => true).catch((e) => { return this.errorHelper.getCacheException("clear", "COMMAND_ERROR", e); }); }; this.getConnection = () => { return this.conn; }; const { connection } = options; this.adapterName = RedisCacheAdapter.prototype.constructor.name; this.conn = connection instanceof RedisConnection ? connection : createRedisConnection(connection); this.redis = this.conn.getNativeConnection(); this.asyncRedis = getAsyncRedisClient(this.redis); } }; exports.RedisCacheAdapter = RedisCacheAdapter; exports.RedisConnection = RedisConnection; exports.createRedisConnection = createRedisConnection; exports.createRedisNativeConnection = createRedisNativeConnection; exports.getRedisOptionsFromDsn = getRedisOptionsFromDsn; //# sourceMappingURL=index.cjs.map