UNPKG

@pnp/common

Version:

pnp - provides shared functionality across all pnp libraries

253 lines 9.3 kB
import { __awaiter, __generator } from "tslib"; import { dateAdd, getCtxCallback, jsS, objectDefinedNotNull } from "./util.js"; import { DefaultRuntime } from "./libconfig.js"; /** * A wrapper class to provide a consistent interface to browser based storage * */ var PnPClientStorageWrapper = /** @class */ (function () { /** * Creates a new instance of the PnPClientStorageWrapper class * * @constructor */ function PnPClientStorageWrapper(store, defaultTimeoutMinutes) { if (defaultTimeoutMinutes === void 0) { defaultTimeoutMinutes = -1; } this.store = store; this.defaultTimeoutMinutes = defaultTimeoutMinutes; this.enabled = this.test(); // if the cache timeout is enabled call the handler // this will clear any expired items and set the timeout function if (DefaultRuntime.get("enableCacheExpiration")) { this.cacheExpirationHandler(); } } PnPClientStorageWrapper.bind = function (store) { return new PnPClientStorageWrapper(typeof (store) === "undefined" ? new MemoryStorage() : store); }; /** * Get a value from storage, or null if that value does not exist * * @param key The key whose value we want to retrieve */ PnPClientStorageWrapper.prototype.get = function (key) { if (!this.enabled) { return null; } var o = this.store.getItem(key); if (!objectDefinedNotNull(o)) { return null; } var persistable = JSON.parse(o); if (new Date(persistable.expiration) <= new Date()) { this.delete(key); return null; } else { return persistable.value; } }; /** * Adds a value to the underlying storage * * @param key The key to use when storing the provided value * @param o The value to store * @param expire Optional, if provided the expiration of the item, otherwise the default is used */ PnPClientStorageWrapper.prototype.put = function (key, o, expire) { if (this.enabled) { this.store.setItem(key, this.createPersistable(o, expire)); } }; /** * Deletes a value from the underlying storage * * @param key The key of the pair we want to remove from storage */ PnPClientStorageWrapper.prototype.delete = function (key) { if (this.enabled) { this.store.removeItem(key); } }; /** * Gets an item from the underlying storage, or adds it if it does not exist using the supplied getter function * * @param key The key to use when storing the provided value * @param getter A function which will upon execution provide the desired value * @param expire Optional, if provided the expiration of the item, otherwise the default is used */ PnPClientStorageWrapper.prototype.getOrPut = function (key, getter, expire) { return __awaiter(this, void 0, void 0, function () { var o; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.enabled) { return [2 /*return*/, getter()]; } o = this.get(key); if (!(o === null)) return [3 /*break*/, 2]; return [4 /*yield*/, getter()]; case 1: o = _a.sent(); this.put(key, o, expire); _a.label = 2; case 2: return [2 /*return*/, o]; } }); }); }; /** * Deletes any expired items placed in the store by the pnp library, leaves other items untouched */ PnPClientStorageWrapper.prototype.deleteExpired = function () { return __awaiter(this, void 0, void 0, function () { var i, key; return __generator(this, function (_a) { switch (_a.label) { case 0: if (!this.enabled) { return [2 /*return*/]; } i = 0; _a.label = 1; case 1: if (!(i < this.store.length)) return [3 /*break*/, 4]; key = this.store.key(i); if (!(key !== null)) return [3 /*break*/, 3]; if (!/["|']?pnp["|']? ?: ?1/i.test(this.store.getItem(key))) return [3 /*break*/, 3]; // get those items as get will delete from cache if they are expired return [4 /*yield*/, this.get(key)]; case 2: // get those items as get will delete from cache if they are expired _a.sent(); _a.label = 3; case 3: i++; return [3 /*break*/, 1]; case 4: return [2 /*return*/]; } }); }); }; /** * Used to determine if the wrapped storage is available currently */ PnPClientStorageWrapper.prototype.test = function () { var str = "t"; try { this.store.setItem(str, str); this.store.removeItem(str); return true; } catch (e) { return false; } }; /** * Creates the persistable to store */ PnPClientStorageWrapper.prototype.createPersistable = function (o, expire) { if (expire === undefined) { // ensure we are by default inline with the global library setting var defaultTimeout = DefaultRuntime.get("defaultCachingTimeoutSeconds"); if (this.defaultTimeoutMinutes > 0) { defaultTimeout = this.defaultTimeoutMinutes * 60; } expire = dateAdd(new Date(), "second", defaultTimeout); } return jsS({ pnp: 1, expiration: expire, value: o }); }; /** * Deletes expired items added by this library in this.store and sets a timeout to call itself */ PnPClientStorageWrapper.prototype.cacheExpirationHandler = function () { var _this = this; if (!this.enabled) { return; } this.deleteExpired().then(function () { // call ourself in the future setTimeout(getCtxCallback(_this, _this.cacheExpirationHandler), DefaultRuntime.get("cacheExpirationIntervalMilliseconds")); }).catch(console.error); }; return PnPClientStorageWrapper; }()); export { PnPClientStorageWrapper }; /** * A thin implementation of in-memory storage for use in nodejs */ var MemoryStorage = /** @class */ (function () { function MemoryStorage(_store) { if (_store === void 0) { _store = new Map(); } this._store = _store; } Object.defineProperty(MemoryStorage.prototype, "length", { get: function () { return this._store.size; }, enumerable: false, configurable: true }); MemoryStorage.prototype.clear = function () { this._store.clear(); }; MemoryStorage.prototype.getItem = function (key) { return this._store.get(key); }; MemoryStorage.prototype.key = function (index) { return Array.from(this._store)[index][0]; }; MemoryStorage.prototype.removeItem = function (key) { this._store.delete(key); }; MemoryStorage.prototype.setItem = function (key, data) { this._store.set(key, data); }; return MemoryStorage; }()); /** * A class that will establish wrappers for both local and session storage */ var PnPClientStorage = /** @class */ (function () { /** * Creates a new instance of the PnPClientStorage class * * @constructor */ function PnPClientStorage(_local, _session) { if (_local === void 0) { _local = null; } if (_session === void 0) { _session = null; } this._local = _local; this._session = _session; } Object.defineProperty(PnPClientStorage.prototype, "local", { /** * Provides access to the local storage of the browser */ get: function () { if (this._local === null) { this._local = new PnPClientStorageWrapper(typeof (localStorage) === "undefined" ? new MemoryStorage() : localStorage); } return this._local; }, enumerable: false, configurable: true }); Object.defineProperty(PnPClientStorage.prototype, "session", { /** * Provides access to the session storage of the browser */ get: function () { if (this._session === null) { this._session = new PnPClientStorageWrapper(typeof (sessionStorage) === "undefined" ? new MemoryStorage() : sessionStorage); } return this._session; }, enumerable: false, configurable: true }); return PnPClientStorage; }()); export { PnPClientStorage }; //# sourceMappingURL=storage.js.map