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).

90 lines (89 loc) 2.65 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var session_exports = {}; __export(session_exports, { createAdapterStore: () => createAdapterStore }); module.exports = __toCommonJS(session_exports); function createAdapterStore(session, defaultTtl = 3600) { const Store = session.Store; class AdapterStore extends Store { adapter; 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 sess === "function") { fn = sess; sess = ttl; ttl = sess?.cookie?.originalMaxAge ? Math.round(sess.cookie.originalMaxAge / 1e3) : defaultTtl; } ttl = ttl || defaultTtl; this.adapter.setSession(sid, ttl, sess, function(err) { fn?.call(this, err); }); } /** * 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; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createAdapterStore }); //# sourceMappingURL=session.js.map