@kwiz/common 1.0.129 → 1.0.132

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (100) hide show
  1. package/.github/workflows/npm-publish.yml +24 -24
  2. package/LICENSE +21 -21
  3. package/lib/cjs/config.js +2 -0
  4. package/lib/cjs/config.js.map +1 -1
  5. package/lib/cjs/types/libs/msal.types.js +26 -26
  6. package/lib/cjs/utils/sharepoint.rest/user.js +11 -11
  7. package/lib/esm/config.js +2 -0
  8. package/lib/esm/config.js.map +1 -1
  9. package/lib/esm/types/libs/msal.types.js +26 -26
  10. package/lib/esm/utils/sharepoint.rest/user.js +11 -11
  11. package/lib/types/config.d.ts +2 -0
  12. package/package.json +81 -81
  13. package/readme.md +17 -17
  14. package/src/_dependencies.ts +12 -12
  15. package/src/config.ts +19 -17
  16. package/src/helpers/Guid.ts +181 -181
  17. package/src/helpers/base64.ts +173 -173
  18. package/src/helpers/browser.test.js +13 -13
  19. package/src/helpers/browser.ts +1504 -1504
  20. package/src/helpers/browserinfo.ts +292 -292
  21. package/src/helpers/collections.base.test.js +25 -25
  22. package/src/helpers/collections.base.ts +437 -437
  23. package/src/helpers/collections.ts +107 -107
  24. package/src/helpers/color.ts +54 -54
  25. package/src/helpers/cookies.ts +59 -59
  26. package/src/helpers/date.test.js +119 -119
  27. package/src/helpers/date.ts +188 -188
  28. package/src/helpers/debug.ts +186 -186
  29. package/src/helpers/diagrams.ts +43 -43
  30. package/src/helpers/emails.ts +6 -6
  31. package/src/helpers/eval.ts +5 -5
  32. package/src/helpers/file.test.js +50 -50
  33. package/src/helpers/file.ts +63 -63
  34. package/src/helpers/flatted.ts +149 -149
  35. package/src/helpers/functions.ts +16 -16
  36. package/src/helpers/graph/calendar.types.ts +10 -10
  37. package/src/helpers/http.ts +69 -69
  38. package/src/helpers/images.ts +22 -22
  39. package/src/helpers/json.ts +44 -44
  40. package/src/helpers/md5.ts +189 -189
  41. package/src/helpers/objects.test.js +33 -33
  42. package/src/helpers/objects.ts +274 -274
  43. package/src/helpers/promises.test.js +37 -37
  44. package/src/helpers/promises.ts +165 -165
  45. package/src/helpers/random.ts +27 -27
  46. package/src/helpers/scheduler/scheduler.test.js +103 -103
  47. package/src/helpers/scheduler/scheduler.ts +131 -131
  48. package/src/helpers/sharepoint.ts +796 -796
  49. package/src/helpers/strings.test.js +122 -122
  50. package/src/helpers/strings.ts +337 -337
  51. package/src/helpers/typecheckers.test.js +34 -34
  52. package/src/helpers/typecheckers.ts +266 -266
  53. package/src/helpers/url.test.js +43 -43
  54. package/src/helpers/url.ts +207 -207
  55. package/src/helpers/urlhelper.ts +111 -111
  56. package/src/index.ts +6 -6
  57. package/src/types/auth.ts +62 -62
  58. package/src/types/common.types.ts +15 -15
  59. package/src/types/flatted.types.ts +59 -59
  60. package/src/types/globals.types.ts +6 -6
  61. package/src/types/graph/calendar.types.ts +80 -80
  62. package/src/types/knownscript.types.ts +18 -18
  63. package/src/types/libs/datajs.types.ts +28 -28
  64. package/src/types/libs/ics.types.ts +30 -30
  65. package/src/types/libs/msal.types.ts +57 -57
  66. package/src/types/locales.ts +125 -125
  67. package/src/types/localstoragecache.types.ts +8 -8
  68. package/src/types/location.types.ts +27 -27
  69. package/src/types/moment.ts +11 -11
  70. package/src/types/regex.types.ts +16 -16
  71. package/src/types/rest.types.ts +95 -95
  72. package/src/types/sharepoint.types.ts +1466 -1466
  73. package/src/types/sharepoint.utils.types.ts +306 -306
  74. package/src/utils/auth/common.ts +118 -118
  75. package/src/utils/auth/discovery.test.js +12 -12
  76. package/src/utils/auth/discovery.ts +132 -132
  77. package/src/utils/base64.ts +27 -27
  78. package/src/utils/consolelogger.ts +333 -333
  79. package/src/utils/date.ts +172 -172
  80. package/src/utils/emails.ts +24 -24
  81. package/src/utils/knownscript.ts +286 -286
  82. package/src/utils/localstoragecache.ts +446 -446
  83. package/src/utils/rest.ts +501 -501
  84. package/src/utils/script.ts +170 -170
  85. package/src/utils/sharepoint.rest/common.ts +159 -159
  86. package/src/utils/sharepoint.rest/date.ts +62 -62
  87. package/src/utils/sharepoint.rest/file.folder.ts +685 -685
  88. package/src/utils/sharepoint.rest/item.ts +547 -547
  89. package/src/utils/sharepoint.rest/list.ts +1572 -1572
  90. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +774 -774
  91. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +275 -275
  92. package/src/utils/sharepoint.rest/listutils/common.ts +206 -206
  93. package/src/utils/sharepoint.rest/location.ts +141 -141
  94. package/src/utils/sharepoint.rest/navigation-links.ts +86 -86
  95. package/src/utils/sharepoint.rest/user-search.ts +252 -252
  96. package/src/utils/sharepoint.rest/user.ts +558 -558
  97. package/src/utils/sharepoint.rest/web.ts +1384 -1384
  98. package/src/utils/sod.ts +194 -194
  99. package/.madgerc +0 -3
  100. package/fix-folder-imports.js +0 -27
@@ -1,447 +1,447 @@
1
- import { sizeOf } from "../helpers/collections.base";
2
- import { isDebug } from "../helpers/debug";
3
- import { flatted } from "../helpers/flatted";
4
- import { jsonParse } from "../helpers/json";
5
- import { getGlobal } from "../helpers/objects";
6
- import { isDate, isNullOrEmptyString, isNullOrUndefined, isNumber } from "../helpers/typecheckers";
7
- import { ILocalStorageCacheLifetime } from "../types/localstoragecache.types";
8
- import { ConsoleLogger } from "./consolelogger";
9
-
10
- /**key with prefix, value is a date string */
11
- interface IExpirationsDictionary {
12
- [keyWithPRefix: string]: string;
13
- build: string;
14
- }
15
-
16
- let logger = ConsoleLogger.get("localstoragecache");
17
-
18
- export const keyPrefix = "kw$_";
19
- export const LOCAL_STORAGE_PREFIX = "kwizcom-localstorage-cache";
20
- export const LOCAL_STORGAGE_EXPIRATIONS_KEY = LOCAL_STORAGE_PREFIX + "-expirations";
21
- export const DEFAULT_EXPIRATION = 20 * 60 * 1000; // 20 minutes;
22
- /** When caching logic changes (serialization methods, format, schema), the MODULE_REVISION should be incremented
23
- * and all client side apps will need to be rebuilt */
24
- export const MODULE_REVISION = "1";
25
- /** key (no prefix) is kept in lower case. not case sensitive */
26
-
27
- function _getCache() {
28
- let _cache = getGlobal<{
29
- purgeCalled: boolean;
30
- expirations: IExpirationsDictionary;
31
- [keyNoPrefixToLower: string]: any;
32
- }>("common_utils_localstoragecache_module_cache");
33
-
34
- if (!_cache.purgeCalled) {
35
- //issue 7081 - purge all orphans/expired items
36
- _cache.purgeCalled = true;
37
- //clear expired cache items.
38
- (globalThis || window).setTimeout(() => {
39
- purgeCache();
40
- if (isDebug()) {
41
- let size = _getStoredSize();
42
- logger.debug(`Size of items in local storage: ${size}KB`);
43
- }
44
- }, 5000);
45
- }
46
-
47
- return _cache;
48
- }
49
-
50
- var _supportsLocalStorage: boolean = null;
51
-
52
- function _parseExpiration(exp: number | ILocalStorageCacheLifetime | Date): Date {
53
- var expirationDate: Date;
54
-
55
- if (isNumber(exp) && exp > 0) {
56
- expirationDate = new Date();
57
- expirationDate.setMilliseconds(expirationDate.getMilliseconds() + exp);
58
- } else if (exp instanceof Date) {
59
- expirationDate = exp;
60
- } else if (exp) {
61
- var tempexp = exp as ILocalStorageCacheLifetime;
62
- var seconds = typeof (tempexp.seconds) === "number" ? tempexp.seconds : undefined;
63
- var minutes = typeof (tempexp.minutes) === "number" ? tempexp.minutes : undefined;
64
- var hours = typeof (tempexp.hours) === "number" ? tempexp.hours : undefined;
65
- var days = typeof (tempexp.days) === "number" ? tempexp.days : undefined;
66
- var months = typeof (tempexp.months) === "number" ? tempexp.months : undefined;
67
- var years = typeof (tempexp.years) === "number" ? tempexp.years : undefined;
68
-
69
- if (seconds || minutes || hours || days || months || years) {
70
- expirationDate = new Date();
71
-
72
- if (seconds) {
73
- expirationDate.setMilliseconds(expirationDate.getMilliseconds() + (seconds * 1000));
74
- }
75
-
76
- if (minutes) {
77
- expirationDate.setMilliseconds(expirationDate.getMilliseconds() + (minutes * 60 * 1000));
78
- }
79
-
80
- if (hours) {
81
- expirationDate.setMilliseconds(expirationDate.getMilliseconds() + (hours * 60 * 60 * 1000));
82
- }
83
-
84
- if (days) {
85
- expirationDate.setMilliseconds(expirationDate.getMilliseconds() + (days * 24 * 60 * 60 * 1000));
86
- }
87
-
88
- if (months) {
89
- expirationDate.setMonth(expirationDate.getMonth() + months);
90
- }
91
-
92
- if (years) {
93
- expirationDate.setFullYear(expirationDate.getFullYear() + years);
94
- }
95
- }
96
- }
97
-
98
- if (!expirationDate) {
99
- expirationDate = new Date();
100
- expirationDate.setMilliseconds(expirationDate.getMilliseconds() + DEFAULT_EXPIRATION);
101
- }
102
-
103
- return expirationDate;
104
- }
105
-
106
- function _getCacheExpirations(): IExpirationsDictionary {
107
- let _cache = _getCache();
108
- if (isNullOrUndefined(_cache.expirations)) {
109
- _cache.expirations = jsonParse<IExpirationsDictionary>(_getItem(LOCAL_STORGAGE_EXPIRATIONS_KEY));
110
-
111
- //ISSUE: 1525 - expire the cache if it was built with a different version number so that the cache
112
- //is compatible with the current build
113
- if (!isNullOrUndefined(_cache.expirations) && _cache.expirations.build !== MODULE_REVISION) {
114
- logger.log(`Purging cache because of build number change`)
115
- purgeCache(true);
116
- _cache.expirations = null;
117
- }
118
-
119
- if (isNullOrUndefined(_cache.expirations)) {
120
- _cache.expirations = {
121
- build: MODULE_REVISION
122
- };
123
- }
124
- }
125
-
126
- return _cache.expirations;
127
- }
128
-
129
- function _saveCacheExpirations() {
130
- let _cache = _getCache();
131
- if (!isNullOrUndefined(_cache.expirations) && sizeOf(_cache.expirations) > 0) {
132
- _setItem(LOCAL_STORGAGE_EXPIRATIONS_KEY, JSON.stringify(_cache.expirations));
133
- }
134
- else {
135
- _removeItem(LOCAL_STORGAGE_EXPIRATIONS_KEY);
136
- }
137
- }
138
-
139
- function _setCacheExpiration(keyWithPrefix: string, expireDate: Date) {
140
- var expirations = _getCacheExpirations();
141
- expirations[keyWithPrefix] = expireDate.toISOString();
142
- _saveCacheExpirations();
143
- }
144
-
145
- function _isKeyExpired(keyWithPrefix: string) {
146
- var expirations = _getCacheExpirations();
147
- if (expirations && expirations[keyWithPrefix]) {
148
- var now = new Date();
149
- var eDate = new Date(expirations[keyWithPrefix]);
150
- if (now > eDate) {
151
- try {
152
- delete expirations[keyWithPrefix];
153
- } catch (ex) {
154
- expirations[keyWithPrefix] = undefined; // undefined variables are removed when passed to JSON.stringify
155
- }
156
-
157
- _saveCacheExpirations();
158
- //has a date, it is expired.
159
- return true;
160
- }
161
- //has a date, it is not expired yet.
162
- return false;
163
- }
164
- //has no date or not in expirations at all - say it is expired...
165
- return true;
166
- }
167
-
168
- function _getItem(key: string) {
169
- try {
170
- return localStorage.getItem(key);
171
- } catch {
172
- }
173
- return null;
174
- }
175
-
176
- function _setItem(key: string, value: string) {
177
- try {
178
- localStorage.setItem(key, value);
179
- return true;
180
- } catch {
181
- }
182
- return false;
183
- }
184
-
185
- function _removeItem(key: string) {
186
- try {
187
- localStorage.removeItem(key);
188
- return true;
189
- } catch {
190
- }
191
- return false;
192
- }
193
-
194
- /**Get the size (KB) of all entries in local storage. Only returns the size for entries with kwizcom key prefix. */
195
- function _getStoredSize() {
196
- let keys = getCacheKeys();
197
- let total = 0;
198
- let length = 0;
199
- let useBlob = 'Blob' in (globalThis || window);
200
-
201
- keys.forEach((key) => {
202
- let v = _getItem(`${keyPrefix}${key}`);
203
- if (!isNullOrEmptyString(v)) {
204
- if (useBlob) {
205
- length = (new Blob([v + key])).size;
206
- } else {
207
- length = ((v.length + key.length) * 2);
208
- }
209
- }
210
- total += length;
211
- });
212
-
213
- return Number((total / 1024).toFixed(2));
214
- }
215
-
216
- export function isLocalStorageSupported() {
217
- if (_supportsLocalStorage !== null) {
218
- return _supportsLocalStorage;
219
- }
220
-
221
- var result;
222
- try {
223
- _setItem(LOCAL_STORAGE_PREFIX, LOCAL_STORAGE_PREFIX);
224
- result = _getItem(LOCAL_STORAGE_PREFIX) === LOCAL_STORAGE_PREFIX;
225
- _removeItem(LOCAL_STORAGE_PREFIX);
226
- _supportsLocalStorage = result;
227
- } catch (ex) {
228
- _supportsLocalStorage = false;
229
- }
230
-
231
- return _supportsLocalStorage;
232
- }
233
-
234
- //#region exported methods
235
- export function getCacheItem<T>(key: string, options?: {
236
- /** Flatted allows serizlising circular dependency objects */
237
- useFlatted?: boolean;
238
- }): T {
239
- key = key.toLowerCase();
240
- let keyWithPrefix = keyPrefix + key;
241
-
242
- let _cache = _getCache();
243
-
244
- if (typeof (_cache[key]) !== "undefined"
245
- && _cache[key] !== null) {
246
- let isExpired = _isKeyExpired(keyWithPrefix);
247
- if (!isExpired) {
248
- return _cache[key] as T;
249
- }
250
- //else remove it from cache
251
- removeCacheItem(key);
252
- }
253
-
254
- if (isLocalStorageSupported()) {
255
- var value: string = _getItem(keyWithPrefix);
256
- if (isNullOrUndefined(value)) {
257
- return null;
258
- }
259
-
260
- let isExpired = _isKeyExpired(keyWithPrefix);
261
-
262
- if (!isExpired) {
263
- let valueAsT: T = options && options.useFlatted ? flatted.parse(value) as T : jsonParse<T>(value);
264
- if (valueAsT !== null) {
265
- _cache[key] = valueAsT;
266
- return valueAsT;
267
- } else {
268
- _cache[key] = value;
269
- return value as any as T;
270
- }
271
- }
272
- //else remove it from cache
273
- removeCacheItem(key);
274
- }
275
-
276
- return null;
277
- }
278
-
279
- export function setCacheItem(key: string, value: any, expiration: number | ILocalStorageCacheLifetime | Date, options?: {
280
- /** Flatted allows serizlising circular dependency objects */
281
- useFlatted?: boolean;
282
- }): void {
283
- if (isLocalStorageSupported()) {
284
- key = key.toLowerCase();
285
- removeCacheItem(key);
286
-
287
- var val = null;
288
- try {
289
- if (options && options.useFlatted)
290
- val = flatted.stringify(value);
291
- else
292
- val = JSON.stringify(value);
293
- } catch (ex) {
294
- logger.debug(`Object cannot be stored in local storage: ${ex && ex.message || ex} ${key}`);
295
- return;//this put [object] in cache for me if object can't be stringified!
296
- }
297
-
298
- let keyWithPrefix = keyPrefix + key;
299
-
300
- var expireDate = _parseExpiration(expiration);
301
-
302
- let saved = _setItem(keyWithPrefix, val);
303
- if (saved) {
304
- _setCacheExpiration(keyWithPrefix, expireDate);
305
- }
306
-
307
- let _cache = _getCache();
308
-
309
- _cache[key] = value;
310
- }
311
- }
312
-
313
- export function removeCacheItem(keyNoPrefix: string) {
314
- keyNoPrefix = keyNoPrefix.toLowerCase();
315
- let _cache = _getCache();
316
- delete _cache[keyNoPrefix];
317
- let keyWithPrefix = keyPrefix + keyNoPrefix;
318
-
319
- if (isLocalStorageSupported()) {
320
- _removeItem(keyNoPrefix);//in case we have an old one
321
- _removeItem(keyWithPrefix);
322
- }
323
- }
324
-
325
- export function removeCacheItems(keys: string[]) {
326
- keys.forEach((key) => {
327
- removeCacheItem(key);
328
- });
329
- }
330
-
331
- export function getCacheKeys() {
332
- let keys: string[] = [];
333
-
334
- if (isLocalStorageSupported()) {
335
- keys = Object.keys(localStorage).filter((key) => {
336
- return key.startsWith(keyPrefix);
337
- }).map((key) => {
338
- return key.substring(keyPrefix.length);
339
- });
340
- }
341
-
342
- return keys;
343
- }
344
-
345
- /** remove expired cache keys created by this utility.
346
- * to remove all keys (non-expired too) send removeAll=true
347
- */
348
- function purgeCache(removeAll?: boolean) {
349
- if (!isLocalStorageSupported()) return;
350
-
351
- var cacheExpirationsKeys = [
352
- LOCAL_STORGAGE_EXPIRATIONS_KEY,
353
- "kwizcom-aplfe-caching-expirations", // old clean up
354
- "localStorageExpirations" // old clean up
355
- ];
356
-
357
- let now = new Date();
358
-
359
- let nonExpiredKeys: string[] = [];
360
-
361
- //get all expiration keys (key/expiration date/time)
362
- for (let j = 0; j < cacheExpirationsKeys.length; j++) {
363
- try {
364
- let expirations = null;
365
- let cacheExpirationsKey = cacheExpirationsKeys[j];
366
-
367
- let removeAllForKey = removeAll || cacheExpirationsKey !== LOCAL_STORGAGE_EXPIRATIONS_KEY;
368
-
369
- if (cacheExpirationsKey === "localStorageExpirations") {
370
- //old format - load expirations from this one as well
371
- expirations = _getItem(cacheExpirationsKey); // "key1^11/18/2011 5pm|key2^3/10/2012 3pm"
372
- if (expirations) {
373
- let arr = expirations.split("|"); // ["key1^11/18/2011 5pm","key2^3/10/2012 3pm"]
374
- for (let i = 0; i < arr.length; i++) {
375
- try {
376
- let key_expiration_format = arr[i]; // "key1^11/18/2011 5pm"
377
- let key = key_expiration_format.split("^")[0];
378
- //old keys - remove all, all the time
379
- _removeItem(key);//remove key from cache
380
- } catch (e) { }
381
- }
382
- }
383
- } else {
384
- //new format
385
- expirations = cacheExpirationsKey === LOCAL_STORGAGE_EXPIRATIONS_KEY ? _getCacheExpirations() : jsonParse(_getItem(cacheExpirationsKey));
386
- if (expirations) {
387
- let expirationKeys = Object.keys(expirations);
388
- logger.group(() => {
389
- expirationKeys.forEach(keyWithPrefix => {
390
- try {
391
- let shouldRemoveKey = removeAllForKey || !keyWithPrefix.startsWith(keyPrefix);
392
- if (!shouldRemoveKey) {
393
- //check specific key expiration
394
- let expirationDate = new Date(expirations[keyWithPrefix]);
395
- if (!isDate(expirationDate) || expirationDate < now) {
396
- shouldRemoveKey = true;
397
- delete expirations[keyWithPrefix];
398
- logger.info(`purging key ${keyWithPrefix}`);
399
- }
400
- else {
401
- nonExpiredKeys.push(keyWithPrefix);
402
- }
403
- }
404
- if (shouldRemoveKey) _removeItem(keyWithPrefix);
405
- } catch (e) {
406
- logger.warn(`failed to remove key ${keyWithPrefix}`);
407
- }
408
- });
409
- }, "Checking expired items", true);
410
- }
411
- }
412
-
413
- if (cacheExpirationsKey === LOCAL_STORGAGE_EXPIRATIONS_KEY)
414
- _saveCacheExpirations();
415
- else//older keys - just remove them.
416
- _removeItem(cacheExpirationsKey);
417
- } catch (e) {
418
- logger.warn(`something went terribly wrong ${e}`);
419
- }
420
- }
421
-
422
- logger.group(() => {
423
- logger.table(nonExpiredKeys);
424
- //cleanup orphans
425
- //loop on all keys
426
- //if stats with: jsr_, kwfs| or keyPrefix - and not in nonExpiredKeys, it is an orphan. Remove it.
427
- let localStorageKeys = Object.keys(localStorage);
428
- for (let keyIdx = 0; keyIdx < localStorageKeys.length; keyIdx++) {
429
- let key = localStorageKeys[keyIdx];
430
- if (key.startsWith("jsr_") || key.startsWith("kwfs|")) {
431
- logger.log(`removing old key ${key}`);
432
- _removeItem(key);//old key
433
- }
434
- else if (key.startsWith(keyPrefix) && !nonExpiredKeys.includes(key))//orphan!
435
- {
436
- logger.log(`removing orphan key ${key}`);
437
- _removeItem(key);
438
- }
439
- }
440
- }, "Expired keys", true);
441
- }
442
-
443
- /** cleanup - remove all local storage keys created by this utility */
444
- export function clearCache(): void {
445
- return purgeCache(true);
446
- }
1
+ import { sizeOf } from "../helpers/collections.base";
2
+ import { isDebug } from "../helpers/debug";
3
+ import { flatted } from "../helpers/flatted";
4
+ import { jsonParse } from "../helpers/json";
5
+ import { getGlobal } from "../helpers/objects";
6
+ import { isDate, isNullOrEmptyString, isNullOrUndefined, isNumber } from "../helpers/typecheckers";
7
+ import { ILocalStorageCacheLifetime } from "../types/localstoragecache.types";
8
+ import { ConsoleLogger } from "./consolelogger";
9
+
10
+ /**key with prefix, value is a date string */
11
+ interface IExpirationsDictionary {
12
+ [keyWithPRefix: string]: string;
13
+ build: string;
14
+ }
15
+
16
+ let logger = ConsoleLogger.get("localstoragecache");
17
+
18
+ export const keyPrefix = "kw$_";
19
+ export const LOCAL_STORAGE_PREFIX = "kwizcom-localstorage-cache";
20
+ export const LOCAL_STORGAGE_EXPIRATIONS_KEY = LOCAL_STORAGE_PREFIX + "-expirations";
21
+ export const DEFAULT_EXPIRATION = 20 * 60 * 1000; // 20 minutes;
22
+ /** When caching logic changes (serialization methods, format, schema), the MODULE_REVISION should be incremented
23
+ * and all client side apps will need to be rebuilt */
24
+ export const MODULE_REVISION = "1";
25
+ /** key (no prefix) is kept in lower case. not case sensitive */
26
+
27
+ function _getCache() {
28
+ let _cache = getGlobal<{
29
+ purgeCalled: boolean;
30
+ expirations: IExpirationsDictionary;
31
+ [keyNoPrefixToLower: string]: any;
32
+ }>("common_utils_localstoragecache_module_cache");
33
+
34
+ if (!_cache.purgeCalled) {
35
+ //issue 7081 - purge all orphans/expired items
36
+ _cache.purgeCalled = true;
37
+ //clear expired cache items.
38
+ (globalThis || window).setTimeout(() => {
39
+ purgeCache();
40
+ if (isDebug()) {
41
+ let size = _getStoredSize();
42
+ logger.debug(`Size of items in local storage: ${size}KB`);
43
+ }
44
+ }, 5000);
45
+ }
46
+
47
+ return _cache;
48
+ }
49
+
50
+ var _supportsLocalStorage: boolean = null;
51
+
52
+ function _parseExpiration(exp: number | ILocalStorageCacheLifetime | Date): Date {
53
+ var expirationDate: Date;
54
+
55
+ if (isNumber(exp) && exp > 0) {
56
+ expirationDate = new Date();
57
+ expirationDate.setMilliseconds(expirationDate.getMilliseconds() + exp);
58
+ } else if (exp instanceof Date) {
59
+ expirationDate = exp;
60
+ } else if (exp) {
61
+ var tempexp = exp as ILocalStorageCacheLifetime;
62
+ var seconds = typeof (tempexp.seconds) === "number" ? tempexp.seconds : undefined;
63
+ var minutes = typeof (tempexp.minutes) === "number" ? tempexp.minutes : undefined;
64
+ var hours = typeof (tempexp.hours) === "number" ? tempexp.hours : undefined;
65
+ var days = typeof (tempexp.days) === "number" ? tempexp.days : undefined;
66
+ var months = typeof (tempexp.months) === "number" ? tempexp.months : undefined;
67
+ var years = typeof (tempexp.years) === "number" ? tempexp.years : undefined;
68
+
69
+ if (seconds || minutes || hours || days || months || years) {
70
+ expirationDate = new Date();
71
+
72
+ if (seconds) {
73
+ expirationDate.setMilliseconds(expirationDate.getMilliseconds() + (seconds * 1000));
74
+ }
75
+
76
+ if (minutes) {
77
+ expirationDate.setMilliseconds(expirationDate.getMilliseconds() + (minutes * 60 * 1000));
78
+ }
79
+
80
+ if (hours) {
81
+ expirationDate.setMilliseconds(expirationDate.getMilliseconds() + (hours * 60 * 60 * 1000));
82
+ }
83
+
84
+ if (days) {
85
+ expirationDate.setMilliseconds(expirationDate.getMilliseconds() + (days * 24 * 60 * 60 * 1000));
86
+ }
87
+
88
+ if (months) {
89
+ expirationDate.setMonth(expirationDate.getMonth() + months);
90
+ }
91
+
92
+ if (years) {
93
+ expirationDate.setFullYear(expirationDate.getFullYear() + years);
94
+ }
95
+ }
96
+ }
97
+
98
+ if (!expirationDate) {
99
+ expirationDate = new Date();
100
+ expirationDate.setMilliseconds(expirationDate.getMilliseconds() + DEFAULT_EXPIRATION);
101
+ }
102
+
103
+ return expirationDate;
104
+ }
105
+
106
+ function _getCacheExpirations(): IExpirationsDictionary {
107
+ let _cache = _getCache();
108
+ if (isNullOrUndefined(_cache.expirations)) {
109
+ _cache.expirations = jsonParse<IExpirationsDictionary>(_getItem(LOCAL_STORGAGE_EXPIRATIONS_KEY));
110
+
111
+ //ISSUE: 1525 - expire the cache if it was built with a different version number so that the cache
112
+ //is compatible with the current build
113
+ if (!isNullOrUndefined(_cache.expirations) && _cache.expirations.build !== MODULE_REVISION) {
114
+ logger.log(`Purging cache because of build number change`)
115
+ purgeCache(true);
116
+ _cache.expirations = null;
117
+ }
118
+
119
+ if (isNullOrUndefined(_cache.expirations)) {
120
+ _cache.expirations = {
121
+ build: MODULE_REVISION
122
+ };
123
+ }
124
+ }
125
+
126
+ return _cache.expirations;
127
+ }
128
+
129
+ function _saveCacheExpirations() {
130
+ let _cache = _getCache();
131
+ if (!isNullOrUndefined(_cache.expirations) && sizeOf(_cache.expirations) > 0) {
132
+ _setItem(LOCAL_STORGAGE_EXPIRATIONS_KEY, JSON.stringify(_cache.expirations));
133
+ }
134
+ else {
135
+ _removeItem(LOCAL_STORGAGE_EXPIRATIONS_KEY);
136
+ }
137
+ }
138
+
139
+ function _setCacheExpiration(keyWithPrefix: string, expireDate: Date) {
140
+ var expirations = _getCacheExpirations();
141
+ expirations[keyWithPrefix] = expireDate.toISOString();
142
+ _saveCacheExpirations();
143
+ }
144
+
145
+ function _isKeyExpired(keyWithPrefix: string) {
146
+ var expirations = _getCacheExpirations();
147
+ if (expirations && expirations[keyWithPrefix]) {
148
+ var now = new Date();
149
+ var eDate = new Date(expirations[keyWithPrefix]);
150
+ if (now > eDate) {
151
+ try {
152
+ delete expirations[keyWithPrefix];
153
+ } catch (ex) {
154
+ expirations[keyWithPrefix] = undefined; // undefined variables are removed when passed to JSON.stringify
155
+ }
156
+
157
+ _saveCacheExpirations();
158
+ //has a date, it is expired.
159
+ return true;
160
+ }
161
+ //has a date, it is not expired yet.
162
+ return false;
163
+ }
164
+ //has no date or not in expirations at all - say it is expired...
165
+ return true;
166
+ }
167
+
168
+ function _getItem(key: string) {
169
+ try {
170
+ return localStorage.getItem(key);
171
+ } catch {
172
+ }
173
+ return null;
174
+ }
175
+
176
+ function _setItem(key: string, value: string) {
177
+ try {
178
+ localStorage.setItem(key, value);
179
+ return true;
180
+ } catch {
181
+ }
182
+ return false;
183
+ }
184
+
185
+ function _removeItem(key: string) {
186
+ try {
187
+ localStorage.removeItem(key);
188
+ return true;
189
+ } catch {
190
+ }
191
+ return false;
192
+ }
193
+
194
+ /**Get the size (KB) of all entries in local storage. Only returns the size for entries with kwizcom key prefix. */
195
+ function _getStoredSize() {
196
+ let keys = getCacheKeys();
197
+ let total = 0;
198
+ let length = 0;
199
+ let useBlob = 'Blob' in (globalThis || window);
200
+
201
+ keys.forEach((key) => {
202
+ let v = _getItem(`${keyPrefix}${key}`);
203
+ if (!isNullOrEmptyString(v)) {
204
+ if (useBlob) {
205
+ length = (new Blob([v + key])).size;
206
+ } else {
207
+ length = ((v.length + key.length) * 2);
208
+ }
209
+ }
210
+ total += length;
211
+ });
212
+
213
+ return Number((total / 1024).toFixed(2));
214
+ }
215
+
216
+ export function isLocalStorageSupported() {
217
+ if (_supportsLocalStorage !== null) {
218
+ return _supportsLocalStorage;
219
+ }
220
+
221
+ var result;
222
+ try {
223
+ _setItem(LOCAL_STORAGE_PREFIX, LOCAL_STORAGE_PREFIX);
224
+ result = _getItem(LOCAL_STORAGE_PREFIX) === LOCAL_STORAGE_PREFIX;
225
+ _removeItem(LOCAL_STORAGE_PREFIX);
226
+ _supportsLocalStorage = result;
227
+ } catch (ex) {
228
+ _supportsLocalStorage = false;
229
+ }
230
+
231
+ return _supportsLocalStorage;
232
+ }
233
+
234
+ //#region exported methods
235
+ export function getCacheItem<T>(key: string, options?: {
236
+ /** Flatted allows serizlising circular dependency objects */
237
+ useFlatted?: boolean;
238
+ }): T {
239
+ key = key.toLowerCase();
240
+ let keyWithPrefix = keyPrefix + key;
241
+
242
+ let _cache = _getCache();
243
+
244
+ if (typeof (_cache[key]) !== "undefined"
245
+ && _cache[key] !== null) {
246
+ let isExpired = _isKeyExpired(keyWithPrefix);
247
+ if (!isExpired) {
248
+ return _cache[key] as T;
249
+ }
250
+ //else remove it from cache
251
+ removeCacheItem(key);
252
+ }
253
+
254
+ if (isLocalStorageSupported()) {
255
+ var value: string = _getItem(keyWithPrefix);
256
+ if (isNullOrUndefined(value)) {
257
+ return null;
258
+ }
259
+
260
+ let isExpired = _isKeyExpired(keyWithPrefix);
261
+
262
+ if (!isExpired) {
263
+ let valueAsT: T = options && options.useFlatted ? flatted.parse(value) as T : jsonParse<T>(value);
264
+ if (valueAsT !== null) {
265
+ _cache[key] = valueAsT;
266
+ return valueAsT;
267
+ } else {
268
+ _cache[key] = value;
269
+ return value as any as T;
270
+ }
271
+ }
272
+ //else remove it from cache
273
+ removeCacheItem(key);
274
+ }
275
+
276
+ return null;
277
+ }
278
+
279
+ export function setCacheItem(key: string, value: any, expiration: number | ILocalStorageCacheLifetime | Date, options?: {
280
+ /** Flatted allows serizlising circular dependency objects */
281
+ useFlatted?: boolean;
282
+ }): void {
283
+ if (isLocalStorageSupported()) {
284
+ key = key.toLowerCase();
285
+ removeCacheItem(key);
286
+
287
+ var val = null;
288
+ try {
289
+ if (options && options.useFlatted)
290
+ val = flatted.stringify(value);
291
+ else
292
+ val = JSON.stringify(value);
293
+ } catch (ex) {
294
+ logger.debug(`Object cannot be stored in local storage: ${ex && ex.message || ex} ${key}`);
295
+ return;//this put [object] in cache for me if object can't be stringified!
296
+ }
297
+
298
+ let keyWithPrefix = keyPrefix + key;
299
+
300
+ var expireDate = _parseExpiration(expiration);
301
+
302
+ let saved = _setItem(keyWithPrefix, val);
303
+ if (saved) {
304
+ _setCacheExpiration(keyWithPrefix, expireDate);
305
+ }
306
+
307
+ let _cache = _getCache();
308
+
309
+ _cache[key] = value;
310
+ }
311
+ }
312
+
313
+ export function removeCacheItem(keyNoPrefix: string) {
314
+ keyNoPrefix = keyNoPrefix.toLowerCase();
315
+ let _cache = _getCache();
316
+ delete _cache[keyNoPrefix];
317
+ let keyWithPrefix = keyPrefix + keyNoPrefix;
318
+
319
+ if (isLocalStorageSupported()) {
320
+ _removeItem(keyNoPrefix);//in case we have an old one
321
+ _removeItem(keyWithPrefix);
322
+ }
323
+ }
324
+
325
+ export function removeCacheItems(keys: string[]) {
326
+ keys.forEach((key) => {
327
+ removeCacheItem(key);
328
+ });
329
+ }
330
+
331
+ export function getCacheKeys() {
332
+ let keys: string[] = [];
333
+
334
+ if (isLocalStorageSupported()) {
335
+ keys = Object.keys(localStorage).filter((key) => {
336
+ return key.startsWith(keyPrefix);
337
+ }).map((key) => {
338
+ return key.substring(keyPrefix.length);
339
+ });
340
+ }
341
+
342
+ return keys;
343
+ }
344
+
345
+ /** remove expired cache keys created by this utility.
346
+ * to remove all keys (non-expired too) send removeAll=true
347
+ */
348
+ function purgeCache(removeAll?: boolean) {
349
+ if (!isLocalStorageSupported()) return;
350
+
351
+ var cacheExpirationsKeys = [
352
+ LOCAL_STORGAGE_EXPIRATIONS_KEY,
353
+ "kwizcom-aplfe-caching-expirations", // old clean up
354
+ "localStorageExpirations" // old clean up
355
+ ];
356
+
357
+ let now = new Date();
358
+
359
+ let nonExpiredKeys: string[] = [];
360
+
361
+ //get all expiration keys (key/expiration date/time)
362
+ for (let j = 0; j < cacheExpirationsKeys.length; j++) {
363
+ try {
364
+ let expirations = null;
365
+ let cacheExpirationsKey = cacheExpirationsKeys[j];
366
+
367
+ let removeAllForKey = removeAll || cacheExpirationsKey !== LOCAL_STORGAGE_EXPIRATIONS_KEY;
368
+
369
+ if (cacheExpirationsKey === "localStorageExpirations") {
370
+ //old format - load expirations from this one as well
371
+ expirations = _getItem(cacheExpirationsKey); // "key1^11/18/2011 5pm|key2^3/10/2012 3pm"
372
+ if (expirations) {
373
+ let arr = expirations.split("|"); // ["key1^11/18/2011 5pm","key2^3/10/2012 3pm"]
374
+ for (let i = 0; i < arr.length; i++) {
375
+ try {
376
+ let key_expiration_format = arr[i]; // "key1^11/18/2011 5pm"
377
+ let key = key_expiration_format.split("^")[0];
378
+ //old keys - remove all, all the time
379
+ _removeItem(key);//remove key from cache
380
+ } catch (e) { }
381
+ }
382
+ }
383
+ } else {
384
+ //new format
385
+ expirations = cacheExpirationsKey === LOCAL_STORGAGE_EXPIRATIONS_KEY ? _getCacheExpirations() : jsonParse(_getItem(cacheExpirationsKey));
386
+ if (expirations) {
387
+ let expirationKeys = Object.keys(expirations);
388
+ logger.group(() => {
389
+ expirationKeys.forEach(keyWithPrefix => {
390
+ try {
391
+ let shouldRemoveKey = removeAllForKey || !keyWithPrefix.startsWith(keyPrefix);
392
+ if (!shouldRemoveKey) {
393
+ //check specific key expiration
394
+ let expirationDate = new Date(expirations[keyWithPrefix]);
395
+ if (!isDate(expirationDate) || expirationDate < now) {
396
+ shouldRemoveKey = true;
397
+ delete expirations[keyWithPrefix];
398
+ logger.info(`purging key ${keyWithPrefix}`);
399
+ }
400
+ else {
401
+ nonExpiredKeys.push(keyWithPrefix);
402
+ }
403
+ }
404
+ if (shouldRemoveKey) _removeItem(keyWithPrefix);
405
+ } catch (e) {
406
+ logger.warn(`failed to remove key ${keyWithPrefix}`);
407
+ }
408
+ });
409
+ }, "Checking expired items", true);
410
+ }
411
+ }
412
+
413
+ if (cacheExpirationsKey === LOCAL_STORGAGE_EXPIRATIONS_KEY)
414
+ _saveCacheExpirations();
415
+ else//older keys - just remove them.
416
+ _removeItem(cacheExpirationsKey);
417
+ } catch (e) {
418
+ logger.warn(`something went terribly wrong ${e}`);
419
+ }
420
+ }
421
+
422
+ logger.group(() => {
423
+ logger.table(nonExpiredKeys);
424
+ //cleanup orphans
425
+ //loop on all keys
426
+ //if stats with: jsr_, kwfs| or keyPrefix - and not in nonExpiredKeys, it is an orphan. Remove it.
427
+ let localStorageKeys = Object.keys(localStorage);
428
+ for (let keyIdx = 0; keyIdx < localStorageKeys.length; keyIdx++) {
429
+ let key = localStorageKeys[keyIdx];
430
+ if (key.startsWith("jsr_") || key.startsWith("kwfs|")) {
431
+ logger.log(`removing old key ${key}`);
432
+ _removeItem(key);//old key
433
+ }
434
+ else if (key.startsWith(keyPrefix) && !nonExpiredKeys.includes(key))//orphan!
435
+ {
436
+ logger.log(`removing orphan key ${key}`);
437
+ _removeItem(key);
438
+ }
439
+ }
440
+ }, "Expired keys", true);
441
+ }
442
+
443
+ /** cleanup - remove all local storage keys created by this utility */
444
+ export function clearCache(): void {
445
+ return purgeCache(true);
446
+ }
447
447
  //#endregion