@splitsoftware/splitio-commons 2.1.0-rc.2 → 2.1.0

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 (53) hide show
  1. package/CHANGES.txt +2 -7
  2. package/cjs/readiness/readinessManager.js +0 -6
  3. package/cjs/storages/AbstractSplitsCacheAsync.js +7 -0
  4. package/cjs/storages/AbstractSplitsCacheSync.js +7 -0
  5. package/cjs/storages/KeyBuilderCS.js +0 -3
  6. package/cjs/storages/dataLoader.js +2 -3
  7. package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +57 -1
  8. package/cjs/storages/inLocalStorage/index.js +3 -5
  9. package/cjs/storages/inRedis/constants.js +1 -1
  10. package/cjs/storages/pluggable/index.js +1 -2
  11. package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +2 -3
  12. package/cjs/sync/polling/updaters/splitChangesUpdater.js +10 -1
  13. package/cjs/sync/syncManagerOnline.js +3 -8
  14. package/cjs/trackers/uniqueKeysTracker.js +1 -1
  15. package/cjs/utils/constants/browser.js +5 -0
  16. package/cjs/utils/settingsValidation/storage/storageCS.js +1 -1
  17. package/esm/readiness/readinessManager.js +0 -6
  18. package/esm/storages/AbstractSplitsCacheAsync.js +7 -0
  19. package/esm/storages/AbstractSplitsCacheSync.js +7 -0
  20. package/esm/storages/KeyBuilderCS.js +0 -3
  21. package/esm/storages/dataLoader.js +1 -2
  22. package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +57 -1
  23. package/esm/storages/inLocalStorage/index.js +3 -5
  24. package/esm/storages/inRedis/constants.js +1 -1
  25. package/esm/storages/pluggable/index.js +1 -2
  26. package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +2 -3
  27. package/esm/sync/polling/updaters/splitChangesUpdater.js +11 -2
  28. package/esm/sync/syncManagerOnline.js +3 -8
  29. package/esm/trackers/uniqueKeysTracker.js +1 -1
  30. package/esm/utils/constants/browser.js +2 -0
  31. package/esm/utils/settingsValidation/storage/storageCS.js +1 -1
  32. package/package.json +1 -1
  33. package/src/readiness/readinessManager.ts +0 -5
  34. package/src/storages/AbstractSplitsCacheAsync.ts +8 -0
  35. package/src/storages/AbstractSplitsCacheSync.ts +8 -0
  36. package/src/storages/KeyBuilderCS.ts +0 -4
  37. package/src/storages/dataLoader.ts +1 -3
  38. package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +66 -1
  39. package/src/storages/inLocalStorage/index.ts +8 -8
  40. package/src/storages/inRedis/constants.ts +1 -1
  41. package/src/storages/pluggable/index.ts +1 -2
  42. package/src/storages/types.ts +4 -1
  43. package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +5 -6
  44. package/src/sync/polling/updaters/splitChangesUpdater.ts +11 -2
  45. package/src/sync/syncManagerOnline.ts +3 -9
  46. package/src/trackers/uniqueKeysTracker.ts +1 -1
  47. package/src/utils/constants/browser.ts +2 -0
  48. package/src/utils/lang/index.ts +1 -1
  49. package/src/utils/settingsValidation/storage/storageCS.ts +1 -1
  50. package/types/splitio.d.ts +1 -25
  51. package/cjs/storages/inLocalStorage/validateCache.js +0 -79
  52. package/esm/storages/inLocalStorage/validateCache.js +0 -75
  53. package/src/storages/inLocalStorage/validateCache.ts +0 -91
@@ -1,75 +0,0 @@
1
- import { isFiniteNumber, isNaNNumber } from '../../utils/lang';
2
- import { getStorageHash } from '../KeyBuilder';
3
- import { LOG_PREFIX } from './constants';
4
- var DEFAULT_CACHE_EXPIRATION_IN_DAYS = 10;
5
- var MILLIS_IN_A_DAY = 86400000;
6
- /**
7
- * Validates if cache should be cleared and sets the cache `hash` if needed.
8
- *
9
- * @returns `true` if cache should be cleared, `false` otherwise
10
- */
11
- function validateExpiration(options, settings, keys, currentTimestamp, isThereCache) {
12
- var log = settings.log;
13
- // Check expiration
14
- var lastUpdatedTimestamp = parseInt(localStorage.getItem(keys.buildLastUpdatedKey()), 10);
15
- if (!isNaNNumber(lastUpdatedTimestamp)) {
16
- var cacheExpirationInDays = isFiniteNumber(options.expirationDays) && options.expirationDays >= 1 ? options.expirationDays : DEFAULT_CACHE_EXPIRATION_IN_DAYS;
17
- var expirationTimestamp = currentTimestamp - MILLIS_IN_A_DAY * cacheExpirationInDays;
18
- if (lastUpdatedTimestamp < expirationTimestamp) {
19
- log.info(LOG_PREFIX + 'Cache expired more than ' + cacheExpirationInDays + ' days ago. Cleaning up cache');
20
- return true;
21
- }
22
- }
23
- // Check hash
24
- var storageHashKey = keys.buildHashKey();
25
- var storageHash = localStorage.getItem(storageHashKey);
26
- var currentStorageHash = getStorageHash(settings);
27
- if (storageHash !== currentStorageHash) {
28
- try {
29
- localStorage.setItem(storageHashKey, currentStorageHash);
30
- }
31
- catch (e) {
32
- log.error(LOG_PREFIX + e);
33
- }
34
- if (isThereCache) {
35
- log.info(LOG_PREFIX + 'SDK key, flags filter criteria, or flags spec version has changed. Cleaning up cache');
36
- return true;
37
- }
38
- return false; // No cache to clear
39
- }
40
- // Clear on init
41
- if (options.clearOnInit) {
42
- var lastClearTimestamp = parseInt(localStorage.getItem(keys.buildLastClear()), 10);
43
- if (isNaNNumber(lastClearTimestamp) || lastClearTimestamp < currentTimestamp - MILLIS_IN_A_DAY) {
44
- log.info(LOG_PREFIX + 'clearOnInit was set and cache was not cleared in the last 24 hours. Cleaning up cache');
45
- return true;
46
- }
47
- }
48
- }
49
- /**
50
- * Clean cache if:
51
- * - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp`
52
- * - its hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified
53
- * - `clearOnInit` was set and cache was not cleared in the last 24 hours
54
- *
55
- * @returns `true` if cache is ready to be used, `false` otherwise (cache was cleared or there is no cache)
56
- */
57
- export function validateCache(options, settings, keys, splits, segments, largeSegments) {
58
- var currentTimestamp = Date.now();
59
- var isThereCache = splits.getChangeNumber() > -1;
60
- if (validateExpiration(options, settings, keys, currentTimestamp, isThereCache)) {
61
- splits.clear();
62
- segments.clear();
63
- largeSegments.clear();
64
- // Update last clear timestamp
65
- try {
66
- localStorage.setItem(keys.buildLastClear(), currentTimestamp + '');
67
- }
68
- catch (e) {
69
- settings.log.error(LOG_PREFIX + e);
70
- }
71
- return false;
72
- }
73
- // Check if ready from cache
74
- return isThereCache;
75
- }
@@ -1,91 +0,0 @@
1
- import { ISettings } from '../../types';
2
- import { isFiniteNumber, isNaNNumber } from '../../utils/lang';
3
- import { getStorageHash } from '../KeyBuilder';
4
- import { LOG_PREFIX } from './constants';
5
- import type { SplitsCacheInLocal } from './SplitsCacheInLocal';
6
- import type { MySegmentsCacheInLocal } from './MySegmentsCacheInLocal';
7
- import { KeyBuilderCS } from '../KeyBuilderCS';
8
- import SplitIO from '../../../types/splitio';
9
-
10
- const DEFAULT_CACHE_EXPIRATION_IN_DAYS = 10;
11
- const MILLIS_IN_A_DAY = 86400000;
12
-
13
- /**
14
- * Validates if cache should be cleared and sets the cache `hash` if needed.
15
- *
16
- * @returns `true` if cache should be cleared, `false` otherwise
17
- */
18
- function validateExpiration(options: SplitIO.InLocalStorageOptions, settings: ISettings, keys: KeyBuilderCS, currentTimestamp: number, isThereCache: boolean) {
19
- const { log } = settings;
20
-
21
- // Check expiration
22
- const lastUpdatedTimestamp = parseInt(localStorage.getItem(keys.buildLastUpdatedKey()) as string, 10);
23
- if (!isNaNNumber(lastUpdatedTimestamp)) {
24
- const cacheExpirationInDays = isFiniteNumber(options.expirationDays) && options.expirationDays >= 1 ? options.expirationDays : DEFAULT_CACHE_EXPIRATION_IN_DAYS;
25
- const expirationTimestamp = currentTimestamp - MILLIS_IN_A_DAY * cacheExpirationInDays;
26
- if (lastUpdatedTimestamp < expirationTimestamp) {
27
- log.info(LOG_PREFIX + 'Cache expired more than ' + cacheExpirationInDays + ' days ago. Cleaning up cache');
28
- return true;
29
- }
30
- }
31
-
32
- // Check hash
33
- const storageHashKey = keys.buildHashKey();
34
- const storageHash = localStorage.getItem(storageHashKey);
35
- const currentStorageHash = getStorageHash(settings);
36
-
37
- if (storageHash !== currentStorageHash) {
38
- try {
39
- localStorage.setItem(storageHashKey, currentStorageHash);
40
- } catch (e) {
41
- log.error(LOG_PREFIX + e);
42
- }
43
- if (isThereCache) {
44
- log.info(LOG_PREFIX + 'SDK key, flags filter criteria, or flags spec version has changed. Cleaning up cache');
45
- return true;
46
- }
47
- return false; // No cache to clear
48
- }
49
-
50
- // Clear on init
51
- if (options.clearOnInit) {
52
- const lastClearTimestamp = parseInt(localStorage.getItem(keys.buildLastClear()) as string, 10);
53
-
54
- if (isNaNNumber(lastClearTimestamp) || lastClearTimestamp < currentTimestamp - MILLIS_IN_A_DAY) {
55
- log.info(LOG_PREFIX + 'clearOnInit was set and cache was not cleared in the last 24 hours. Cleaning up cache');
56
- return true;
57
- }
58
- }
59
- }
60
-
61
- /**
62
- * Clean cache if:
63
- * - it has expired, i.e., its `lastUpdated` timestamp is older than the given `expirationTimestamp`
64
- * - its hash has changed, i.e., the SDK key, flags filter criteria or flags spec version was modified
65
- * - `clearOnInit` was set and cache was not cleared in the last 24 hours
66
- *
67
- * @returns `true` if cache is ready to be used, `false` otherwise (cache was cleared or there is no cache)
68
- */
69
- export function validateCache(options: SplitIO.InLocalStorageOptions, settings: ISettings, keys: KeyBuilderCS, splits: SplitsCacheInLocal, segments: MySegmentsCacheInLocal, largeSegments: MySegmentsCacheInLocal): boolean {
70
-
71
- const currentTimestamp = Date.now();
72
- const isThereCache = splits.getChangeNumber() > -1;
73
-
74
- if (validateExpiration(options, settings, keys, currentTimestamp, isThereCache)) {
75
- splits.clear();
76
- segments.clear();
77
- largeSegments.clear();
78
-
79
- // Update last clear timestamp
80
- try {
81
- localStorage.setItem(keys.buildLastClear(), currentTimestamp + '');
82
- } catch (e) {
83
- settings.log.error(LOG_PREFIX + e);
84
- }
85
-
86
- return false;
87
- }
88
-
89
- // Check if ready from cache
90
- return isThereCache;
91
- }