UNPKG

@iobroker/js-controller-common-db

Version:

The Library contains the common utils for the ioBroker controller which can be used by db classes too, as they do not rely on the db (circular dependencies).

77 lines 2.46 kB
/** * Function to create an AdapterStore constructor * * @param session The session object * @param defaultTtl the default time to live * @returns the constructor to create a new AdapterStore */ export function createAdapterStore(session, defaultTtl = 3600) { const Store = session.Store; class AdapterStore extends Store { constructor(options) { super(options); this.adapter = options.adapter; options = options || {}; if (!options.cookie) { options.cookie = { maxAge: defaultTtl }; } Store.call(this, options); } /** * Attempt to fetch session by the given `sid`. * * @param sid Session ID * @param fn callback */ get(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 sid Session ID * @param ttl Time to live * @param sess the session * @param fn callback */ set(sid, ttl, sess, fn) { if (typeof ttl === 'object') { fn = sess; sess = ttl; // analyse if the session is stored directly from express session ttl = sess && sess.cookie && sess.cookie.originalMaxAge ? Math.round(sess.cookie.originalMaxAge / 1000) : defaultTtl; } ttl = ttl || defaultTtl; this.adapter.setSession(sid, ttl, sess, function () { // @ts-expect-error fix later // eslint-disable-next-line prefer-rest-params fn && fn.apply(this, arguments); }); // do not use here => !!! } /** * Destroy the session associated with the given `sid`. * * @param sid Session ID * @param fn callback */ destroy(sid, fn) { this.adapter.destroySession(sid, fn); } } return AdapterStore; } //# sourceMappingURL=session.js.map