1 | import { __awaiter, __generator } from "tslib";
|
2 | import { dateAdd, getCtxCallback, jsS, objectDefinedNotNull } from "./util.js";
|
3 | import { DefaultRuntime } from "./libconfig.js";
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | var PnPClientStorageWrapper = (function () {
|
9 | |
10 |
|
11 |
|
12 |
|
13 |
|
14 | function PnPClientStorageWrapper(store, defaultTimeoutMinutes) {
|
15 | if (defaultTimeoutMinutes === void 0) { defaultTimeoutMinutes = -1; }
|
16 | this.store = store;
|
17 | this.defaultTimeoutMinutes = defaultTimeoutMinutes;
|
18 | this.enabled = this.test();
|
19 |
|
20 |
|
21 | if (DefaultRuntime.get("enableCacheExpiration")) {
|
22 | this.cacheExpirationHandler();
|
23 | }
|
24 | }
|
25 | PnPClientStorageWrapper.bind = function (store) {
|
26 | return new PnPClientStorageWrapper(typeof (store) === "undefined" ? new MemoryStorage() : store);
|
27 | };
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 | PnPClientStorageWrapper.prototype.get = function (key) {
|
34 | if (!this.enabled) {
|
35 | return null;
|
36 | }
|
37 | var o = this.store.getItem(key);
|
38 | if (!objectDefinedNotNull(o)) {
|
39 | return null;
|
40 | }
|
41 | var persistable = JSON.parse(o);
|
42 | if (new Date(persistable.expiration) <= new Date()) {
|
43 | this.delete(key);
|
44 | return null;
|
45 | }
|
46 | else {
|
47 | return persistable.value;
|
48 | }
|
49 | };
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | PnPClientStorageWrapper.prototype.put = function (key, o, expire) {
|
58 | if (this.enabled) {
|
59 | this.store.setItem(key, this.createPersistable(o, expire));
|
60 | }
|
61 | };
|
62 | |
63 |
|
64 |
|
65 |
|
66 |
|
67 | PnPClientStorageWrapper.prototype.delete = function (key) {
|
68 | if (this.enabled) {
|
69 | this.store.removeItem(key);
|
70 | }
|
71 | };
|
72 | |
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | PnPClientStorageWrapper.prototype.getOrPut = function (key, getter, expire) {
|
80 | return __awaiter(this, void 0, void 0, function () {
|
81 | var o;
|
82 | return __generator(this, function (_a) {
|
83 | switch (_a.label) {
|
84 | case 0:
|
85 | if (!this.enabled) {
|
86 | return [2 , getter()];
|
87 | }
|
88 | o = this.get(key);
|
89 | if (!(o === null)) return [3 , 2];
|
90 | return [4 , getter()];
|
91 | case 1:
|
92 | o = _a.sent();
|
93 | this.put(key, o, expire);
|
94 | _a.label = 2;
|
95 | case 2: return [2 , o];
|
96 | }
|
97 | });
|
98 | });
|
99 | };
|
100 | |
101 |
|
102 |
|
103 | PnPClientStorageWrapper.prototype.deleteExpired = function () {
|
104 | return __awaiter(this, void 0, void 0, function () {
|
105 | var i, key;
|
106 | return __generator(this, function (_a) {
|
107 | switch (_a.label) {
|
108 | case 0:
|
109 | if (!this.enabled) {
|
110 | return [2 ];
|
111 | }
|
112 | i = 0;
|
113 | _a.label = 1;
|
114 | case 1:
|
115 | if (!(i < this.store.length)) return [3 , 4];
|
116 | key = this.store.key(i);
|
117 | if (!(key !== null)) return [3 , 3];
|
118 | if (!/["|']?pnp["|']? ?: ?1/i.test(this.store.getItem(key))) return [3 , 3];
|
119 |
|
120 | return [4 , this.get(key)];
|
121 | case 2:
|
122 |
|
123 | _a.sent();
|
124 | _a.label = 3;
|
125 | case 3:
|
126 | i++;
|
127 | return [3 , 1];
|
128 | case 4: return [2 ];
|
129 | }
|
130 | });
|
131 | });
|
132 | };
|
133 | |
134 |
|
135 |
|
136 | PnPClientStorageWrapper.prototype.test = function () {
|
137 | var str = "t";
|
138 | try {
|
139 | this.store.setItem(str, str);
|
140 | this.store.removeItem(str);
|
141 | return true;
|
142 | }
|
143 | catch (e) {
|
144 | return false;
|
145 | }
|
146 | };
|
147 | |
148 |
|
149 |
|
150 | PnPClientStorageWrapper.prototype.createPersistable = function (o, expire) {
|
151 | if (expire === undefined) {
|
152 |
|
153 | var defaultTimeout = DefaultRuntime.get("defaultCachingTimeoutSeconds");
|
154 | if (this.defaultTimeoutMinutes > 0) {
|
155 | defaultTimeout = this.defaultTimeoutMinutes * 60;
|
156 | }
|
157 | expire = dateAdd(new Date(), "second", defaultTimeout);
|
158 | }
|
159 | return jsS({ pnp: 1, expiration: expire, value: o });
|
160 | };
|
161 | |
162 |
|
163 |
|
164 | PnPClientStorageWrapper.prototype.cacheExpirationHandler = function () {
|
165 | var _this = this;
|
166 | if (!this.enabled) {
|
167 | return;
|
168 | }
|
169 | this.deleteExpired().then(function () {
|
170 |
|
171 | setTimeout(getCtxCallback(_this, _this.cacheExpirationHandler), DefaultRuntime.get("cacheExpirationIntervalMilliseconds"));
|
172 | }).catch(console.error);
|
173 | };
|
174 | return PnPClientStorageWrapper;
|
175 | }());
|
176 | export { PnPClientStorageWrapper };
|
177 |
|
178 |
|
179 |
|
180 | var MemoryStorage = (function () {
|
181 | function MemoryStorage(_store) {
|
182 | if (_store === void 0) { _store = new Map(); }
|
183 | this._store = _store;
|
184 | }
|
185 | Object.defineProperty(MemoryStorage.prototype, "length", {
|
186 | get: function () {
|
187 | return this._store.size;
|
188 | },
|
189 | enumerable: false,
|
190 | configurable: true
|
191 | });
|
192 | MemoryStorage.prototype.clear = function () {
|
193 | this._store.clear();
|
194 | };
|
195 | MemoryStorage.prototype.getItem = function (key) {
|
196 | return this._store.get(key);
|
197 | };
|
198 | MemoryStorage.prototype.key = function (index) {
|
199 | return Array.from(this._store)[index][0];
|
200 | };
|
201 | MemoryStorage.prototype.removeItem = function (key) {
|
202 | this._store.delete(key);
|
203 | };
|
204 | MemoryStorage.prototype.setItem = function (key, data) {
|
205 | this._store.set(key, data);
|
206 | };
|
207 | return MemoryStorage;
|
208 | }());
|
209 |
|
210 |
|
211 |
|
212 | var PnPClientStorage = (function () {
|
213 | |
214 |
|
215 |
|
216 |
|
217 |
|
218 | function PnPClientStorage(_local, _session) {
|
219 | if (_local === void 0) { _local = null; }
|
220 | if (_session === void 0) { _session = null; }
|
221 | this._local = _local;
|
222 | this._session = _session;
|
223 | }
|
224 | Object.defineProperty(PnPClientStorage.prototype, "local", {
|
225 | |
226 |
|
227 |
|
228 | get: function () {
|
229 | if (this._local === null) {
|
230 | this._local = new PnPClientStorageWrapper(typeof (localStorage) === "undefined" ? new MemoryStorage() : localStorage);
|
231 | }
|
232 | return this._local;
|
233 | },
|
234 | enumerable: false,
|
235 | configurable: true
|
236 | });
|
237 | Object.defineProperty(PnPClientStorage.prototype, "session", {
|
238 | |
239 |
|
240 |
|
241 | get: function () {
|
242 | if (this._session === null) {
|
243 | this._session = new PnPClientStorageWrapper(typeof (sessionStorage) === "undefined" ? new MemoryStorage() : sessionStorage);
|
244 | }
|
245 | return this._session;
|
246 | },
|
247 | enumerable: false,
|
248 | configurable: true
|
249 | });
|
250 | return PnPClientStorage;
|
251 | }());
|
252 | export { PnPClientStorage };
|
253 |
|
\ | No newline at end of file |