UNPKG

hono-sess

Version:

A Simple Session Middleware for Hono

64 lines 1.72 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Session = void 0; const cookie_1 = require("./cookie"); class Session { #req; #_id; cookie; get id() { return this.#_id; } constructor(req, data) { this.#req = req; this.#_id = req.sessionID; if (req.session?.cookie) { this.cookie = new cookie_1.Cookie(req.session.cookie); } else { this.cookie = new cookie_1.Cookie(); } if (typeof data === 'object' && data !== null) { for (const prop in data) { if (!(prop in this)) { this[prop] = data[prop]; } } } } touch() { return this.resetMaxAge(); } resetMaxAge() { this.cookie.maxAge = this.cookie.originalMaxAge ?? undefined; return this; } save(fn) { this.#req.sessionStore.set(this.id, this, fn || (() => { })); return this; } reload(fn) { const req = this.#req; const store = this.#req.sessionStore; store.get(this.id, (err, sess) => { if (err) return fn(err); if (!sess) return fn(new Error('failed to load session')); store.createSession(req, sess); fn(); }); return this; } destroy(fn) { delete this.#req.session; this.#req.sessionStore.destroy(this.id, fn || (() => { })); return this; } regenerate(fn) { this.#req.sessionStore.regenerate(this.#req, fn); return this; } } exports.Session = Session; //# sourceMappingURL=session.js.map