UNPKG

yunkong2.js-controller

Version:

Updated by reinstall.js on 2018-06-11T15:19:56.688Z

73 lines (60 loc) 1.89 kB
'use strict'; module.exports = function (session, defaultTtl) { var Store = session.Store; defaultTtl = defaultTtl || 3600; function AdapterStore(options) { var that = this; this.adapter = options.adapter; options = options || {}; if (!options.cookie) { options.cookie = {maxAge: defaultTtl}; } Store.call(this, options); } // Object.getPrototypeOf(AdapterStore.prototype) ? // AdapterStore.prototype.__proto__ = Store.prototype; AdapterStore.prototype = Object.create(Store.prototype); /** * Attempt to fetch session by the given `sid`. * * @param {String} sid * @param {Function} fn * @api public */ AdapterStore.prototype.get = function (sid, fn) { this.adapter.getSession(sid, obj => { if (obj) { if (fn) return fn(null, obj); } else { if (fn) return fn(); } }); }; /** * Commit the given `sess` object associated with the given `sid`. * * @param {String} sid * @param {Session} sess * @param {Function} fn * @api public */ AdapterStore.prototype.set = function (sid, ttl, sess, fn) { if (typeof ttl === 'object') { fn = sess; sess = ttl; ttl = defaultTtl; } ttl = ttl || defaultTtl; this.adapter.setSession(sid, ttl, sess, function () {fn && fn.apply(this, arguments)}); // do not use here => !!! }; /** * Destroy the session associated with the given `sid`. * * @param {String} sid * @api public */ AdapterStore.prototype.destroy = function (sid, fn) { this.adapter.destroySession(sid, fn); }; return AdapterStore; };