1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | return new (P || (P = Promise))(function (resolve, reject) {
|
4 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
5 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
6 | function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
|
7 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
8 | });
|
9 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | const rxjs_1 = require("rxjs");
|
12 | class SessionsManager {
|
13 | constructor(config) {
|
14 | this.config = config;
|
15 | this.contexts = {};
|
16 | }
|
17 | getSessionContext(sessionId) {
|
18 | return __awaiter(this, void 0, void 0, function* () {
|
19 | if (!this.contexts[sessionId]) {
|
20 | yield this.createContext(sessionId);
|
21 | }
|
22 | return this.contexts[sessionId].context;
|
23 | });
|
24 | }
|
25 | createContext(sessionId) {
|
26 | return __awaiter(this, void 0, void 0, function* () {
|
27 | const data = {};
|
28 | const context = {
|
29 | get: (name) => {
|
30 | return data[name] && data[name].hasValue ? data[name].value : null;
|
31 | },
|
32 | has: (name) => {
|
33 | return data[name] && data[name].hasValue;
|
34 | },
|
35 | get$: (name) => {
|
36 | if (!data[name]) {
|
37 | data[name] = {
|
38 | stream: new rxjs_1.Subject(),
|
39 | value: undefined,
|
40 | hasValue: false,
|
41 | };
|
42 | }
|
43 | return data[name].stream;
|
44 | },
|
45 | set: (name, value) => __awaiter(this, void 0, void 0, function* () {
|
46 | if (!data[name]) {
|
47 | data[name] = {
|
48 | stream: new rxjs_1.Subject(),
|
49 | value: undefined,
|
50 | hasValue: false,
|
51 | };
|
52 | }
|
53 | data[name].value = value;
|
54 | data[name].hasValue = true;
|
55 | data[name].stream.next(value);
|
56 | yield this.save(sessionId);
|
57 | }),
|
58 | };
|
59 | this.contexts[sessionId] = { data, context };
|
60 | const saved = yield this.get(sessionId);
|
61 | if (saved) {
|
62 | Object.keys(saved)
|
63 | .map((key) => {
|
64 | data[key] = {
|
65 | value: saved[key],
|
66 | hasValue: true,
|
67 | stream: new rxjs_1.Subject(),
|
68 | };
|
69 | if (key !== "id" && key !== "cookie" && key !== "__lastAccess") {
|
70 | data[key].stream.next(saved[key]);
|
71 | }
|
72 | });
|
73 | }
|
74 | });
|
75 | }
|
76 | get(sid) {
|
77 | return __awaiter(this, void 0, void 0, function* () {
|
78 | return new Promise((resolve, reject) => {
|
79 | this.config.sessionsStorage.get(sid, (err, session) => {
|
80 | if (err) {
|
81 | if (err.code === "ENOENT") {
|
82 | resolve(null);
|
83 | return;
|
84 | }
|
85 | reject(err);
|
86 | return;
|
87 | }
|
88 | resolve(session);
|
89 | });
|
90 | });
|
91 | });
|
92 | }
|
93 | save(id) {
|
94 | return __awaiter(this, void 0, void 0, function* () {
|
95 | const data = {};
|
96 | Object.keys(this.contexts[id].data).map((d) => {
|
97 | data[d] = this.contexts[id].data[d].value;
|
98 | });
|
99 | return new Promise((resolve, reject) => {
|
100 | this.config.sessionsStorage.set(id, data, (err) => {
|
101 | if (err) {
|
102 | reject(err);
|
103 | return;
|
104 | }
|
105 | resolve();
|
106 | });
|
107 | });
|
108 | });
|
109 | }
|
110 | }
|
111 | exports.default = SessionsManager;
|