UNPKG

9.3 kBJavaScriptView Raw
1import { __awaiter, __generator } from "tslib";
2import { dateAdd, getCtxCallback, jsS, objectDefinedNotNull } from "./util.js";
3import { DefaultRuntime } from "./libconfig.js";
4/**
5 * A wrapper class to provide a consistent interface to browser based storage
6 *
7 */
8var PnPClientStorageWrapper = /** @class */ (function () {
9 /**
10 * Creates a new instance of the PnPClientStorageWrapper class
11 *
12 * @constructor
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 // if the cache timeout is enabled call the handler
20 // this will clear any expired items and set the timeout function
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 * Get a value from storage, or null if that value does not exist
30 *
31 * @param key The key whose value we want to retrieve
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 * Adds a value to the underlying storage
52 *
53 * @param key The key to use when storing the provided value
54 * @param o The value to store
55 * @param expire Optional, if provided the expiration of the item, otherwise the default is used
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 * Deletes a value from the underlying storage
64 *
65 * @param key The key of the pair we want to remove from storage
66 */
67 PnPClientStorageWrapper.prototype.delete = function (key) {
68 if (this.enabled) {
69 this.store.removeItem(key);
70 }
71 };
72 /**
73 * Gets an item from the underlying storage, or adds it if it does not exist using the supplied getter function
74 *
75 * @param key The key to use when storing the provided value
76 * @param getter A function which will upon execution provide the desired value
77 * @param expire Optional, if provided the expiration of the item, otherwise the default is used
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 /*return*/, getter()];
87 }
88 o = this.get(key);
89 if (!(o === null)) return [3 /*break*/, 2];
90 return [4 /*yield*/, getter()];
91 case 1:
92 o = _a.sent();
93 this.put(key, o, expire);
94 _a.label = 2;
95 case 2: return [2 /*return*/, o];
96 }
97 });
98 });
99 };
100 /**
101 * Deletes any expired items placed in the store by the pnp library, leaves other items untouched
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 /*return*/];
111 }
112 i = 0;
113 _a.label = 1;
114 case 1:
115 if (!(i < this.store.length)) return [3 /*break*/, 4];
116 key = this.store.key(i);
117 if (!(key !== null)) return [3 /*break*/, 3];
118 if (!/["|']?pnp["|']? ?: ?1/i.test(this.store.getItem(key))) return [3 /*break*/, 3];
119 // get those items as get will delete from cache if they are expired
120 return [4 /*yield*/, this.get(key)];
121 case 2:
122 // get those items as get will delete from cache if they are expired
123 _a.sent();
124 _a.label = 3;
125 case 3:
126 i++;
127 return [3 /*break*/, 1];
128 case 4: return [2 /*return*/];
129 }
130 });
131 });
132 };
133 /**
134 * Used to determine if the wrapped storage is available currently
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 * Creates the persistable to store
149 */
150 PnPClientStorageWrapper.prototype.createPersistable = function (o, expire) {
151 if (expire === undefined) {
152 // ensure we are by default inline with the global library setting
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 * Deletes expired items added by this library in this.store and sets a timeout to call itself
163 */
164 PnPClientStorageWrapper.prototype.cacheExpirationHandler = function () {
165 var _this = this;
166 if (!this.enabled) {
167 return;
168 }
169 this.deleteExpired().then(function () {
170 // call ourself in the future
171 setTimeout(getCtxCallback(_this, _this.cacheExpirationHandler), DefaultRuntime.get("cacheExpirationIntervalMilliseconds"));
172 }).catch(console.error);
173 };
174 return PnPClientStorageWrapper;
175}());
176export { PnPClientStorageWrapper };
177/**
178 * A thin implementation of in-memory storage for use in nodejs
179 */
180var MemoryStorage = /** @class */ (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 * A class that will establish wrappers for both local and session storage
211 */
212var PnPClientStorage = /** @class */ (function () {
213 /**
214 * Creates a new instance of the PnPClientStorage class
215 *
216 * @constructor
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 * Provides access to the local storage of the browser
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 * Provides access to the session storage of the browser
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}());
252export { PnPClientStorage };
253//# sourceMappingURL=storage.js.map
\No newline at end of file