UNPKG

@dazejs/framework

Version:

Daze.js - A powerful web framework for Node.js

170 lines 5.7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Session = void 0; const uuid = __importStar(require("uuid")); const container_1 = require("../../container"); const symbols = __importStar(require("../../symbol")); const utils_1 = require("../../utils"); const defaultOpts = { store: 'cookie', key: 'dazejs:sess', httpOnly: true, signed: false, autoCommit: true, }; const ONE_DAY = 86400000; const EXTRA_STROES = new Set([ 'redis', ]); class Session { constructor(request, options = {}) { this.app = container_1.Container.get('app'); this.session = {}; this.id = ''; this.request = request; this.options = Object.assign(defaultOpts, this.app.get('config').get('session', {}), options); this.initializeStore(); } async initializeStore() { var _a; if (!this.options.store || this.options.store === 'cookie' || !EXTRA_STROES.has(this.options.store)) return; const Store = (await (_a = `./stores/${this.options.store.toLowerCase()}`, Promise.resolve().then(() => __importStar(require(_a))))).default; this.store = new Store(this.app); } async loadSession() { await this.initializeStore(); if (this.store) { await this.loadFromExtraStore(); return; } this.loadFromCookieStore(); } loadFromCookieStore() { const cookie = this.request.cookies.get(this.options.key, this.options); if (!cookie) { return this.generate(); } let json = {}; try { json = utils_1.Str.decodeBASE64(cookie); } catch (err) { return this.generate(); } if (!this.verify(json)) { return this.generate(); } return this.generate(json); } async loadFromExtraStore() { this.id = this.request.cookies.get(this.options.key, this.options) || this.generateSessionId(); const json = await this.store.get(this.id, this.options.maxAge); if (!this.verify(json)) { return this.generate(); } return this.generate(json); } verify(session) { if (!session || typeof session !== 'object') return false; if (session._expire && session._expire < Date.now()) return false; return true; } generate(data = {}) { if (!this.session) this.session = {}; const maxAge = this.options.maxAge ? this.options.maxAge : ONE_DAY; this.session = data; this.session._expire = maxAge + Date.now(); this.session._maxAge = maxAge; return this.session; } generateSessionId() { return uuid.v4(); } get(key, defaultVal) { if (!key) return this.session; return this.session[key] || defaultVal; } set(key, value) { if (!this.session) this.session = {}; this.session[key] = value; return this; } remove(key) { if (!this.session) this.session = {}; delete this.session[key]; return this; } push(key, value) { const arr = this.get(key, []); arr.push(value); this.set(key, arr); return this; } flash(key, value) { this.set(key, value); this.push(symbols.SESSION.NEW_FLASHS, key); this.updateOldFlashSession(key); } ageFlashSession() { const oldFlashs = this.get(symbols.SESSION.OLD_FLASHS, []); for (const old of oldFlashs) { this.remove(old); } this.set(symbols.SESSION.OLD_FLASHS, this.get(symbols.SESSION.NEW_FLASHS, [])); this.set(symbols.SESSION.NEW_FLASHS, []); } updateOldFlashSession(...keys) { const oldFlashs = this.get(symbols.SESSION.OLD_FLASHS, []); const _keys = oldFlashs.filter((key) => !keys.includes(key)); this.set(symbols.SESSION.OLD_FLASHS, _keys); return this; } async commit() { this.ageFlashSession(); if (!this.store) { const encodedSession = utils_1.Str.encodeBASE64(this.session); this.request.cookies.set(this.options.key, encodedSession, this.options); } else { await this.store.set(this.id, this.session); this.request.cookies.set(this.options.key, this.id, this.options); } } async autoCommit() { if (this.options.autoCommit === true) { await this.commit(); } } } exports.Session = Session; //# sourceMappingURL=index.js.map