UNPKG

zents

Version:

ZenTS is a Node.js & TypeScript MVC-Framework for building rich web applications, released as free and open-source software under the MIT License. It is designed for building web applications with modern tools and design patterns.

75 lines 2.49 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RedisSessionStoreAdapter = void 0; const enums_1 = require("../../types/enums"); const logger_1 = require("../../log/logger"); const ms_1 = __importDefault(require("ms")); class RedisSessionStoreAdapter { constructor(databaseContainer, providerOptions) { this.redisClient = databaseContainer.get(enums_1.DB_TYPE.REDIS); this.prefix = providerOptions.storePrefix; this.expire = providerOptions.expireInMS; this.keepTTL = providerOptions.redisKeepTTL; } async create(sessionId) { const prefixedSessionId = this.getPrefixedSessionId(sessionId); const record = await this.redisClient.get(prefixedSessionId); if (record) { return; } const options = []; if (this.expire !== -1) { options.push('PX', this.expire); } else { options.push('PX', ms_1.default('7d')); } if (this.keepTTL) { options.push('KEEPTTL'); } await this.redisClient.set(prefixedSessionId, '{}', ...options); } async load(sessionId) { const record = await this.redisClient.get(this.getPrefixedSessionId(sessionId)); let data = {}; if (!record || !record.length) { return data; } try { data = JSON.parse(record); } catch (e) { data = {}; logger_1.log.error(e); } return data; } async persist(sessionId, data) { let record; try { record = JSON.stringify(data); } catch (e) { record = ''; logger_1.log.error(e); } await this.redisClient.set(this.getPrefixedSessionId(sessionId), record); } async remove(sessionId) { if (!(await this.has(sessionId))) { return; } await this.redisClient.del(this.getPrefixedSessionId(sessionId)); } async has(sessionId) { return !!(await this.redisClient.exists(this.getPrefixedSessionId(sessionId))); } getPrefixedSessionId(sessionId) { return `${this.prefix}${sessionId}`; } } exports.RedisSessionStoreAdapter = RedisSessionStoreAdapter; //# sourceMappingURL=RedisSessionStoreAdapter.js.map