@splitsoftware/splitio-commons 2.1.1-rc.0 → 2.1.1-rc.2

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 (187) hide show
  1. package/CHANGES.txt +7 -0
  2. package/README.md +0 -1
  3. package/cjs/evaluator/combiners/and.js +6 -2
  4. package/cjs/evaluator/combiners/ifelseif.js +6 -6
  5. package/cjs/evaluator/condition/index.js +5 -6
  6. package/cjs/evaluator/index.js +7 -7
  7. package/cjs/evaluator/matchers/index.js +1 -3
  8. package/cjs/evaluator/matchers/matcherTypes.js +0 -1
  9. package/cjs/evaluator/matchersTransform/index.js +0 -4
  10. package/cjs/evaluator/parser/index.js +2 -2
  11. package/cjs/evaluator/value/sanitize.js +0 -1
  12. package/cjs/logger/constants.js +3 -4
  13. package/cjs/logger/messages/debug.js +2 -3
  14. package/cjs/logger/messages/error.js +1 -1
  15. package/cjs/logger/messages/warn.js +2 -2
  16. package/cjs/readiness/readinessManager.js +6 -0
  17. package/cjs/sdkClient/client.js +29 -19
  18. package/cjs/sdkClient/clientAttributesDecoration.js +19 -25
  19. package/cjs/sdkClient/clientInputValidation.js +28 -26
  20. package/cjs/services/splitApi.js +2 -2
  21. package/cjs/storages/AbstractSplitsCacheAsync.js +0 -7
  22. package/cjs/storages/AbstractSplitsCacheSync.js +2 -12
  23. package/cjs/storages/KeyBuilder.js +0 -9
  24. package/cjs/storages/KeyBuilderCS.js +4 -4
  25. package/cjs/storages/KeyBuilderSS.js +0 -3
  26. package/cjs/storages/dataLoader.js +3 -2
  27. package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +23 -76
  28. package/cjs/storages/inLocalStorage/index.js +5 -7
  29. package/cjs/storages/inLocalStorage/validateCache.js +79 -0
  30. package/cjs/storages/inMemory/InMemoryStorage.js +0 -3
  31. package/cjs/storages/inMemory/InMemoryStorageCS.js +0 -4
  32. package/cjs/storages/inRedis/index.js +0 -2
  33. package/cjs/storages/pluggable/index.js +2 -3
  34. package/cjs/storages/utils.js +1 -0
  35. package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +3 -2
  36. package/cjs/sync/polling/fetchers/splitChangesFetcher.js +2 -2
  37. package/cjs/sync/polling/pollingManagerCS.js +7 -7
  38. package/cjs/sync/polling/updaters/mySegmentsUpdater.js +2 -2
  39. package/cjs/sync/polling/updaters/segmentChangesUpdater.js +1 -1
  40. package/cjs/sync/polling/updaters/splitChangesUpdater.js +33 -60
  41. package/cjs/sync/streaming/SSEHandler/index.js +0 -1
  42. package/cjs/sync/streaming/UpdateWorkers/SplitsUpdateWorker.js +77 -106
  43. package/cjs/sync/streaming/constants.js +1 -2
  44. package/cjs/sync/streaming/pushManager.js +16 -3
  45. package/cjs/sync/submitters/impressionsSubmitter.js +3 -2
  46. package/cjs/sync/syncManagerOnline.js +10 -5
  47. package/cjs/trackers/strategy/strategyDebug.js +2 -0
  48. package/cjs/trackers/strategy/strategyOptimized.js +3 -0
  49. package/cjs/utils/constants/index.js +2 -3
  50. package/cjs/utils/inputValidation/eventProperties.js +12 -1
  51. package/cjs/utils/inputValidation/index.js +3 -1
  52. package/cjs/utils/settingsValidation/storage/storageCS.js +1 -1
  53. package/esm/evaluator/combiners/and.js +6 -2
  54. package/esm/evaluator/combiners/ifelseif.js +7 -7
  55. package/esm/evaluator/condition/index.js +5 -6
  56. package/esm/evaluator/index.js +7 -7
  57. package/esm/evaluator/matchers/index.js +1 -3
  58. package/esm/evaluator/matchers/matcherTypes.js +0 -1
  59. package/esm/evaluator/matchersTransform/index.js +0 -4
  60. package/esm/evaluator/parser/index.js +2 -2
  61. package/esm/evaluator/value/sanitize.js +0 -1
  62. package/esm/logger/constants.js +0 -1
  63. package/esm/logger/messages/debug.js +2 -3
  64. package/esm/logger/messages/error.js +1 -1
  65. package/esm/logger/messages/warn.js +2 -2
  66. package/esm/readiness/readinessManager.js +6 -0
  67. package/esm/sdkClient/client.js +29 -19
  68. package/esm/sdkClient/clientAttributesDecoration.js +19 -25
  69. package/esm/sdkClient/clientInputValidation.js +29 -27
  70. package/esm/services/splitApi.js +2 -2
  71. package/esm/storages/AbstractSplitsCacheAsync.js +0 -7
  72. package/esm/storages/AbstractSplitsCacheSync.js +2 -12
  73. package/esm/storages/KeyBuilder.js +0 -9
  74. package/esm/storages/KeyBuilderCS.js +4 -4
  75. package/esm/storages/KeyBuilderSS.js +0 -3
  76. package/esm/storages/dataLoader.js +2 -1
  77. package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +23 -76
  78. package/esm/storages/inLocalStorage/index.js +5 -7
  79. package/esm/storages/inLocalStorage/validateCache.js +75 -0
  80. package/esm/storages/inMemory/InMemoryStorage.js +0 -3
  81. package/esm/storages/inMemory/InMemoryStorageCS.js +0 -4
  82. package/esm/storages/inRedis/index.js +0 -2
  83. package/esm/storages/pluggable/index.js +2 -3
  84. package/esm/storages/utils.js +1 -0
  85. package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +3 -2
  86. package/esm/sync/polling/fetchers/splitChangesFetcher.js +2 -2
  87. package/esm/sync/polling/pollingManagerCS.js +7 -7
  88. package/esm/sync/polling/updaters/mySegmentsUpdater.js +2 -2
  89. package/esm/sync/polling/updaters/segmentChangesUpdater.js +1 -1
  90. package/esm/sync/polling/updaters/splitChangesUpdater.js +34 -61
  91. package/esm/sync/streaming/SSEHandler/index.js +1 -2
  92. package/esm/sync/streaming/UpdateWorkers/SplitsUpdateWorker.js +73 -102
  93. package/esm/sync/streaming/constants.js +0 -1
  94. package/esm/sync/streaming/pushManager.js +19 -6
  95. package/esm/sync/submitters/impressionsSubmitter.js +3 -2
  96. package/esm/sync/syncManagerOnline.js +10 -5
  97. package/esm/trackers/strategy/strategyDebug.js +2 -0
  98. package/esm/trackers/strategy/strategyOptimized.js +3 -0
  99. package/esm/utils/constants/index.js +1 -2
  100. package/esm/utils/inputValidation/eventProperties.js +10 -0
  101. package/esm/utils/inputValidation/index.js +1 -0
  102. package/esm/utils/settingsValidation/storage/storageCS.js +1 -1
  103. package/package.json +1 -1
  104. package/src/dtos/types.ts +8 -32
  105. package/src/evaluator/Engine.ts +1 -1
  106. package/src/evaluator/combiners/and.ts +4 -5
  107. package/src/evaluator/combiners/ifelseif.ts +9 -7
  108. package/src/evaluator/condition/engineUtils.ts +1 -1
  109. package/src/evaluator/condition/index.ts +12 -12
  110. package/src/evaluator/index.ts +7 -7
  111. package/src/evaluator/matchers/index.ts +1 -3
  112. package/src/evaluator/matchers/matcherTypes.ts +0 -1
  113. package/src/evaluator/matchersTransform/index.ts +0 -3
  114. package/src/evaluator/parser/index.ts +3 -3
  115. package/src/evaluator/types.ts +2 -2
  116. package/src/evaluator/value/index.ts +2 -2
  117. package/src/evaluator/value/sanitize.ts +4 -5
  118. package/src/logger/constants.ts +0 -1
  119. package/src/logger/messages/debug.ts +2 -3
  120. package/src/logger/messages/error.ts +1 -1
  121. package/src/logger/messages/warn.ts +2 -2
  122. package/src/readiness/readinessManager.ts +5 -0
  123. package/src/sdkClient/client.ts +31 -21
  124. package/src/sdkClient/clientAttributesDecoration.ts +20 -27
  125. package/src/sdkClient/clientInputValidation.ts +30 -27
  126. package/src/sdkManager/index.ts +1 -1
  127. package/src/services/splitApi.ts +2 -2
  128. package/src/services/types.ts +1 -1
  129. package/src/storages/AbstractSplitsCacheAsync.ts +0 -8
  130. package/src/storages/AbstractSplitsCacheSync.ts +3 -14
  131. package/src/storages/KeyBuilder.ts +0 -12
  132. package/src/storages/KeyBuilderCS.ts +5 -5
  133. package/src/storages/KeyBuilderSS.ts +0 -4
  134. package/src/storages/dataLoader.ts +3 -1
  135. package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +26 -87
  136. package/src/storages/inLocalStorage/index.ts +8 -12
  137. package/src/storages/inLocalStorage/validateCache.ts +91 -0
  138. package/src/storages/inMemory/InMemoryStorage.ts +0 -3
  139. package/src/storages/inMemory/InMemoryStorageCS.ts +0 -4
  140. package/src/storages/inRedis/index.ts +0 -2
  141. package/src/storages/pluggable/index.ts +2 -3
  142. package/src/storages/types.ts +2 -37
  143. package/src/storages/utils.ts +1 -0
  144. package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +6 -5
  145. package/src/sync/polling/fetchers/splitChangesFetcher.ts +1 -2
  146. package/src/sync/polling/fetchers/types.ts +0 -1
  147. package/src/sync/polling/pollingManagerCS.ts +7 -7
  148. package/src/sync/polling/types.ts +2 -2
  149. package/src/sync/polling/updaters/mySegmentsUpdater.ts +2 -2
  150. package/src/sync/polling/updaters/segmentChangesUpdater.ts +1 -1
  151. package/src/sync/polling/updaters/splitChangesUpdater.ts +43 -71
  152. package/src/sync/streaming/SSEHandler/index.ts +1 -2
  153. package/src/sync/streaming/SSEHandler/types.ts +2 -2
  154. package/src/sync/streaming/UpdateWorkers/SplitsUpdateWorker.ts +68 -98
  155. package/src/sync/streaming/constants.ts +0 -1
  156. package/src/sync/streaming/parseUtils.ts +2 -2
  157. package/src/sync/streaming/pushManager.ts +18 -6
  158. package/src/sync/streaming/types.ts +2 -3
  159. package/src/sync/submitters/impressionsSubmitter.ts +3 -2
  160. package/src/sync/submitters/types.ts +23 -33
  161. package/src/sync/syncManagerOnline.ts +11 -5
  162. package/src/trackers/strategy/strategyDebug.ts +2 -0
  163. package/src/trackers/strategy/strategyOptimized.ts +3 -0
  164. package/src/utils/constants/index.ts +1 -2
  165. package/src/utils/inputValidation/eventProperties.ts +10 -0
  166. package/src/utils/inputValidation/index.ts +1 -0
  167. package/src/utils/lang/index.ts +2 -2
  168. package/src/utils/settingsValidation/storage/storageCS.ts +1 -1
  169. package/types/splitio.d.ts +128 -36
  170. package/cjs/evaluator/matchers/rbsegment.js +0 -43
  171. package/cjs/storages/inLocalStorage/RBSegmentsCacheInLocal.js +0 -117
  172. package/cjs/storages/inMemory/RBSegmentsCacheInMemory.js +0 -61
  173. package/cjs/storages/inRedis/RBSegmentsCacheInRedis.js +0 -64
  174. package/cjs/storages/pluggable/RBSegmentsCachePluggable.js +0 -64
  175. package/cjs/utils/constants/browser.js +0 -5
  176. package/esm/evaluator/matchers/rbsegment.js +0 -39
  177. package/esm/storages/inLocalStorage/RBSegmentsCacheInLocal.js +0 -114
  178. package/esm/storages/inMemory/RBSegmentsCacheInMemory.js +0 -58
  179. package/esm/storages/inRedis/RBSegmentsCacheInRedis.js +0 -61
  180. package/esm/storages/pluggable/RBSegmentsCachePluggable.js +0 -61
  181. package/esm/utils/constants/browser.js +0 -2
  182. package/src/evaluator/matchers/rbsegment.ts +0 -61
  183. package/src/storages/inLocalStorage/RBSegmentsCacheInLocal.ts +0 -136
  184. package/src/storages/inMemory/RBSegmentsCacheInMemory.ts +0 -68
  185. package/src/storages/inRedis/RBSegmentsCacheInRedis.ts +0 -79
  186. package/src/storages/pluggable/RBSegmentsCachePluggable.ts +0 -76
  187. package/src/utils/constants/browser.ts +0 -2
@@ -24,15 +24,6 @@ var KeyBuilder = /** @class */ (function () {
24
24
  KeyBuilder.prototype.buildSplitKeyPrefix = function () {
25
25
  return this.prefix + ".split.";
26
26
  };
27
- KeyBuilder.prototype.buildRBSegmentKey = function (rbsegmentName) {
28
- return this.prefix + ".rbsegment." + rbsegmentName;
29
- };
30
- KeyBuilder.prototype.buildRBSegmentsTillKey = function () {
31
- return this.prefix + ".rbsegments.till";
32
- };
33
- KeyBuilder.prototype.buildRBSegmentKeyPrefix = function () {
34
- return this.prefix + ".rbsegment.";
35
- };
36
27
  KeyBuilder.prototype.buildSegmentNameKey = function (segmentName) {
37
28
  return this.prefix + ".segment." + segmentName;
38
29
  };
@@ -6,7 +6,7 @@ var KeyBuilderCS = /** @class */ (function (_super) {
6
6
  function KeyBuilderCS(prefix, matchingKey) {
7
7
  var _this = _super.call(this, prefix) || this;
8
8
  _this.matchingKey = matchingKey;
9
- _this.regexSplitsCacheKey = new RegExp("^" + prefix + "\\.(splits?|trafficType|flagSet|rbsegment)\\.");
9
+ _this.regexSplitsCacheKey = new RegExp("^" + prefix + "\\.(splits?|trafficType|flagSet)\\.");
10
10
  return _this;
11
11
  }
12
12
  /**
@@ -32,12 +32,12 @@ var KeyBuilderCS = /** @class */ (function (_super) {
32
32
  KeyBuilderCS.prototype.isSplitKey = function (key) {
33
33
  return startsWith(key, this.prefix + ".split.");
34
34
  };
35
- KeyBuilderCS.prototype.isRBSegmentKey = function (key) {
36
- return startsWith(key, this.prefix + ".rbsegment.");
37
- };
38
35
  KeyBuilderCS.prototype.buildSplitsWithSegmentCountKey = function () {
39
36
  return this.prefix + ".splits.usingSegments";
40
37
  };
38
+ KeyBuilderCS.prototype.buildLastClear = function () {
39
+ return this.prefix + ".lastClear";
40
+ };
41
41
  return KeyBuilderCS;
42
42
  }(KeyBuilder));
43
43
  export { KeyBuilderCS };
@@ -39,9 +39,6 @@ var KeyBuilderSS = /** @class */ (function (_super) {
39
39
  KeyBuilderSS.prototype.searchPatternForSplitKeys = function () {
40
40
  return this.buildSplitKeyPrefix() + "*";
41
41
  };
42
- KeyBuilderSS.prototype.searchPatternForRBSegmentKeys = function () {
43
- return this.buildRBSegmentKeyPrefix() + "*";
44
- };
45
42
  /* Telemetry keys */
46
43
  KeyBuilderSS.prototype.buildLatencyKey = function (method, bucket) {
47
44
  return this.latencyPrefix + "::" + this.versionablePrefix + "/" + METHOD_NAMES[method] + "/" + bucket;
@@ -1,4 +1,5 @@
1
- import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../utils/constants/browser';
1
+ // This value might be eventually set via a config parameter
2
+ var DEFAULT_CACHE_EXPIRATION_IN_MILLIS = 864000000; // 10 days
2
3
  /**
3
4
  * Factory of client-side storage loader
4
5
  *
@@ -2,21 +2,17 @@ import { __extends } from "tslib";
2
2
  import { AbstractSplitsCacheSync, usesSegments } from '../AbstractSplitsCacheSync';
3
3
  import { isFiniteNumber, toNumber, isNaNNumber } from '../../utils/lang';
4
4
  import { LOG_PREFIX } from './constants';
5
- import { getStorageHash } from '../KeyBuilder';
6
5
  import { setToArray } from '../../utils/lang/sets';
7
6
  /**
8
7
  * ISplitsCacheSync implementation that stores split definitions in browser LocalStorage.
9
8
  */
10
9
  var SplitsCacheInLocal = /** @class */ (function (_super) {
11
10
  __extends(SplitsCacheInLocal, _super);
12
- function SplitsCacheInLocal(settings, keys, expirationTimestamp) {
11
+ function SplitsCacheInLocal(settings, keys) {
13
12
  var _this = _super.call(this) || this;
14
13
  _this.keys = keys;
15
14
  _this.log = settings.log;
16
- _this.storageHash = getStorageHash(settings);
17
15
  _this.flagSetsFilter = settings.sync.__splitFiltersValidation.groupedFilters.bySet;
18
- _this._checkExpiration(expirationTimestamp);
19
- _this._checkFilterQuery();
20
16
  return _this;
21
17
  }
22
18
  SplitsCacheInLocal.prototype._decrementCount = function (key) {
@@ -29,13 +25,11 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
29
25
  };
30
26
  SplitsCacheInLocal.prototype._decrementCounts = function (split) {
31
27
  try {
32
- if (split) {
33
- var ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
34
- this._decrementCount(ttKey);
35
- if (usesSegments(split)) {
36
- var segmentsCountKey = this.keys.buildSplitsWithSegmentCountKey();
37
- this._decrementCount(segmentsCountKey);
38
- }
28
+ var ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
29
+ this._decrementCount(ttKey);
30
+ if (usesSegments(split)) {
31
+ var segmentsCountKey = this.keys.buildSplitsWithSegmentCountKey();
32
+ this._decrementCount(segmentsCountKey);
39
33
  }
40
34
  }
41
35
  catch (e) {
@@ -44,13 +38,15 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
44
38
  };
45
39
  SplitsCacheInLocal.prototype._incrementCounts = function (split) {
46
40
  try {
47
- var ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
48
- // @ts-expect-error
49
- localStorage.setItem(ttKey, toNumber(localStorage.getItem(ttKey)) + 1);
50
- if (usesSegments(split)) {
51
- var segmentsCountKey = this.keys.buildSplitsWithSegmentCountKey();
41
+ if (split) {
42
+ var ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
52
43
  // @ts-expect-error
53
- localStorage.setItem(segmentsCountKey, toNumber(localStorage.getItem(segmentsCountKey)) + 1);
44
+ localStorage.setItem(ttKey, toNumber(localStorage.getItem(ttKey)) + 1);
45
+ if (usesSegments(split)) {
46
+ var segmentsCountKey = this.keys.buildSplitsWithSegmentCountKey();
47
+ // @ts-expect-error
48
+ localStorage.setItem(segmentsCountKey, toNumber(localStorage.getItem(segmentsCountKey)) + 1);
49
+ }
54
50
  }
55
51
  }
56
52
  catch (e) {
@@ -62,7 +58,6 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
62
58
  * We cannot simply call `localStorage.clear()` since that implies removing user items from the storage.
63
59
  */
64
60
  SplitsCacheInLocal.prototype.clear = function () {
65
- this.log.info(LOG_PREFIX + 'Flushing Splits data from localStorage');
66
61
  // collect item keys
67
62
  var len = localStorage.length;
68
63
  var accum = [];
@@ -83,11 +78,12 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
83
78
  var splitKey = this.keys.buildSplitKey(name_1);
84
79
  var splitFromLocalStorage = localStorage.getItem(splitKey);
85
80
  var previousSplit = splitFromLocalStorage ? JSON.parse(splitFromLocalStorage) : null;
81
+ if (previousSplit) {
82
+ this._decrementCounts(previousSplit);
83
+ this.removeFromFlagSets(previousSplit.name, previousSplit.sets);
84
+ }
86
85
  localStorage.setItem(splitKey, JSON.stringify(split));
87
86
  this._incrementCounts(split);
88
- this._decrementCounts(previousSplit);
89
- if (previousSplit)
90
- this.removeFromFlagSets(previousSplit.name, previousSplit.sets);
91
87
  this.addToFlagSets(split);
92
88
  return true;
93
89
  }
@@ -103,8 +99,7 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
103
99
  return false;
104
100
  localStorage.removeItem(this.keys.buildSplitKey(name));
105
101
  this._decrementCounts(split);
106
- if (split)
107
- this.removeFromFlagSets(split.name, split.sets);
102
+ this.removeFromFlagSets(split.name, split.sets);
108
103
  return true;
109
104
  }
110
105
  catch (e) {
@@ -117,18 +112,6 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
117
112
  return item && JSON.parse(item);
118
113
  };
119
114
  SplitsCacheInLocal.prototype.setChangeNumber = function (changeNumber) {
120
- // when using a new split query, we must update it at the store
121
- if (this.updateNewFilter) {
122
- this.log.info(LOG_PREFIX + 'SDK key, flags filter criteria or flags spec version was modified. Updating cache');
123
- var storageHashKey = this.keys.buildHashKey();
124
- try {
125
- localStorage.setItem(storageHashKey, this.storageHash);
126
- }
127
- catch (e) {
128
- this.log.error(LOG_PREFIX + e);
129
- }
130
- this.updateNewFilter = false;
131
- }
132
115
  try {
133
116
  localStorage.setItem(this.keys.buildSplitsTillKey(), changeNumber + '');
134
117
  // update "last updated" timestamp with current time
@@ -172,48 +155,12 @@ var SplitsCacheInLocal = /** @class */ (function (_super) {
172
155
  return true;
173
156
  var storedCount = localStorage.getItem(this.keys.buildSplitsWithSegmentCountKey());
174
157
  var splitsWithSegmentsCount = storedCount === null ? 0 : toNumber(storedCount);
175
- return isFiniteNumber(splitsWithSegmentsCount) ?
176
- splitsWithSegmentsCount > 0 :
177
- true;
178
- };
179
- /**
180
- * Check if the splits information is already stored in browser LocalStorage.
181
- * In this function we could add more code to check if the data is valid.
182
- * @override
183
- */
184
- SplitsCacheInLocal.prototype.checkCache = function () {
185
- return this.getChangeNumber() > -1;
186
- };
187
- /**
188
- * Clean Splits cache if its `lastUpdated` timestamp is older than the given `expirationTimestamp`,
189
- *
190
- * @param expirationTimestamp - if the value is not a number, data will not be cleaned
191
- */
192
- SplitsCacheInLocal.prototype._checkExpiration = function (expirationTimestamp) {
193
- var value = localStorage.getItem(this.keys.buildLastUpdatedKey());
194
- if (value !== null) {
195
- value = parseInt(value, 10);
196
- if (!isNaNNumber(value) && expirationTimestamp && value < expirationTimestamp)
197
- this.clear();
158
+ if (isFiniteNumber(splitsWithSegmentsCount)) {
159
+ return splitsWithSegmentsCount > 0;
198
160
  }
199
- };
200
- // @TODO eventually remove `_checkFilterQuery`. Cache should be cleared at the storage level, reusing same logic than PluggableStorage
201
- SplitsCacheInLocal.prototype._checkFilterQuery = function () {
202
- var storageHashKey = this.keys.buildHashKey();
203
- var storageHash = localStorage.getItem(storageHashKey);
204
- if (storageHash !== this.storageHash) {
205
- try {
206
- // mark cache to update the new query filter on first successful splits fetch
207
- this.updateNewFilter = true;
208
- // if there is cache, clear it
209
- if (this.checkCache())
210
- this.clear();
211
- }
212
- catch (e) {
213
- this.log.error(LOG_PREFIX + e);
214
- }
161
+ else {
162
+ return true;
215
163
  }
216
- // if the filter didn't change, nothing is done
217
164
  };
218
165
  SplitsCacheInLocal.prototype.getNamesByFlagSets = function (flagSets) {
219
166
  var _this = this;
@@ -6,14 +6,13 @@ import { KeyBuilderCS, myLargeSegmentsKeyBuilder } from '../KeyBuilderCS';
6
6
  import { isLocalStorageAvailable } from '../../utils/env/isLocalStorageAvailable';
7
7
  import { SplitsCacheInLocal } from './SplitsCacheInLocal';
8
8
  import { MySegmentsCacheInLocal } from './MySegmentsCacheInLocal';
9
- import { DEFAULT_CACHE_EXPIRATION_IN_MILLIS } from '../../utils/constants/browser';
10
9
  import { InMemoryStorageCSFactory } from '../inMemory/InMemoryStorageCS';
11
10
  import { LOG_PREFIX } from './constants';
12
11
  import { STORAGE_LOCALSTORAGE } from '../../utils/constants';
13
12
  import { shouldRecordTelemetry, TelemetryCacheInMemory } from '../inMemory/TelemetryCacheInMemory';
14
13
  import { UniqueKeysCacheInMemoryCS } from '../inMemory/UniqueKeysCacheInMemoryCS';
15
14
  import { getMatching } from '../../utils/key';
16
- import { RBSegmentsCacheInLocal } from './RBSegmentsCacheInLocal';
15
+ import { validateCache } from './validateCache';
17
16
  /**
18
17
  * InLocal storage factory for standalone client-side SplitFactory
19
18
  */
@@ -29,14 +28,11 @@ export function InLocalStorage(options) {
29
28
  var settings = params.settings, _a = params.settings, log = _a.log, _b = _a.scheduler, impressionsQueueSize = _b.impressionsQueueSize, eventsQueueSize = _b.eventsQueueSize;
30
29
  var matchingKey = getMatching(settings.core.key);
31
30
  var keys = new KeyBuilderCS(prefix, matchingKey);
32
- var expirationTimestamp = Date.now() - DEFAULT_CACHE_EXPIRATION_IN_MILLIS;
33
- var splits = new SplitsCacheInLocal(settings, keys, expirationTimestamp);
34
- var rbSegments = new RBSegmentsCacheInLocal(settings, keys);
31
+ var splits = new SplitsCacheInLocal(settings, keys);
35
32
  var segments = new MySegmentsCacheInLocal(log, keys);
36
33
  var largeSegments = new MySegmentsCacheInLocal(log, myLargeSegmentsKeyBuilder(prefix, matchingKey));
37
34
  return {
38
35
  splits: splits,
39
- rbSegments: rbSegments,
40
36
  segments: segments,
41
37
  largeSegments: largeSegments,
42
38
  impressions: new ImpressionsCacheInMemory(impressionsQueueSize),
@@ -44,12 +40,14 @@ export function InLocalStorage(options) {
44
40
  events: new EventsCacheInMemory(eventsQueueSize),
45
41
  telemetry: shouldRecordTelemetry(params) ? new TelemetryCacheInMemory(splits, segments) : undefined,
46
42
  uniqueKeys: new UniqueKeysCacheInMemoryCS(),
43
+ validateCache: function () {
44
+ return validateCache(options, settings, keys, splits, segments, largeSegments);
45
+ },
47
46
  destroy: function () { },
48
47
  // When using shared instantiation with MEMORY we reuse everything but segments (they are customer per key).
49
48
  shared: function (matchingKey) {
50
49
  return {
51
50
  splits: this.splits,
52
- rbSegments: this.rbSegments,
53
51
  segments: new MySegmentsCacheInLocal(log, new KeyBuilderCS(prefix, matchingKey)),
54
52
  largeSegments: new MySegmentsCacheInLocal(log, myLargeSegmentsKeyBuilder(prefix, matchingKey)),
55
53
  impressions: this.impressions,
@@ -0,0 +1,75 @@
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
+ }
@@ -6,7 +6,6 @@ import { ImpressionCountsCacheInMemory } from './ImpressionCountsCacheInMemory';
6
6
  import { LOCALHOST_MODE, STORAGE_MEMORY } from '../../utils/constants';
7
7
  import { shouldRecordTelemetry, TelemetryCacheInMemory } from './TelemetryCacheInMemory';
8
8
  import { UniqueKeysCacheInMemory } from './UniqueKeysCacheInMemory';
9
- import { RBSegmentsCacheInMemory } from './RBSegmentsCacheInMemory';
10
9
  /**
11
10
  * InMemory storage factory for standalone server-side SplitFactory
12
11
  *
@@ -15,11 +14,9 @@ import { RBSegmentsCacheInMemory } from './RBSegmentsCacheInMemory';
15
14
  export function InMemoryStorageFactory(params) {
16
15
  var _a = params.settings, _b = _a.scheduler, impressionsQueueSize = _b.impressionsQueueSize, eventsQueueSize = _b.eventsQueueSize, __splitFiltersValidation = _a.sync.__splitFiltersValidation;
17
16
  var splits = new SplitsCacheInMemory(__splitFiltersValidation);
18
- var rbSegments = new RBSegmentsCacheInMemory();
19
17
  var segments = new SegmentsCacheInMemory();
20
18
  var storage = {
21
19
  splits: splits,
22
- rbSegments: rbSegments,
23
20
  segments: segments,
24
21
  impressions: new ImpressionsCacheInMemory(impressionsQueueSize),
25
22
  impressionCounts: new ImpressionCountsCacheInMemory(),
@@ -6,7 +6,6 @@ import { ImpressionCountsCacheInMemory } from './ImpressionCountsCacheInMemory';
6
6
  import { LOCALHOST_MODE, STORAGE_MEMORY } from '../../utils/constants';
7
7
  import { shouldRecordTelemetry, TelemetryCacheInMemory } from './TelemetryCacheInMemory';
8
8
  import { UniqueKeysCacheInMemoryCS } from './UniqueKeysCacheInMemoryCS';
9
- import { RBSegmentsCacheInMemory } from './RBSegmentsCacheInMemory';
10
9
  /**
11
10
  * InMemory storage factory for standalone client-side SplitFactory
12
11
  *
@@ -15,12 +14,10 @@ import { RBSegmentsCacheInMemory } from './RBSegmentsCacheInMemory';
15
14
  export function InMemoryStorageCSFactory(params) {
16
15
  var _a = params.settings, _b = _a.scheduler, impressionsQueueSize = _b.impressionsQueueSize, eventsQueueSize = _b.eventsQueueSize, __splitFiltersValidation = _a.sync.__splitFiltersValidation;
17
16
  var splits = new SplitsCacheInMemory(__splitFiltersValidation);
18
- var rbSegments = new RBSegmentsCacheInMemory();
19
17
  var segments = new MySegmentsCacheInMemory();
20
18
  var largeSegments = new MySegmentsCacheInMemory();
21
19
  var storage = {
22
20
  splits: splits,
23
- rbSegments: rbSegments,
24
21
  segments: segments,
25
22
  largeSegments: largeSegments,
26
23
  impressions: new ImpressionsCacheInMemory(impressionsQueueSize),
@@ -33,7 +30,6 @@ export function InMemoryStorageCSFactory(params) {
33
30
  shared: function () {
34
31
  return {
35
32
  splits: this.splits,
36
- rbSegments: this.rbSegments,
37
33
  segments: new MySegmentsCacheInMemory(),
38
34
  largeSegments: new MySegmentsCacheInMemory(),
39
35
  impressions: this.impressions,
@@ -9,7 +9,6 @@ import { TelemetryCacheInRedis } from './TelemetryCacheInRedis';
9
9
  import { UniqueKeysCacheInRedis } from './UniqueKeysCacheInRedis';
10
10
  import { ImpressionCountsCacheInRedis } from './ImpressionCountsCacheInRedis';
11
11
  import { metadataBuilder } from '../utils';
12
- import { RBSegmentsCacheInRedis } from './RBSegmentsCacheInRedis';
13
12
  /**
14
13
  * InRedis storage factory for consumer server-side SplitFactory, that uses `Ioredis` Redis client for Node.js
15
14
  * @see {@link https://www.npmjs.com/package/ioredis}
@@ -38,7 +37,6 @@ export function InRedisStorage(options) {
38
37
  });
39
38
  return {
40
39
  splits: new SplitsCacheInRedis(log, keys, redisClient, settings.sync.__splitFiltersValidation),
41
- rbSegments: new RBSegmentsCacheInRedis(log, keys, redisClient),
42
40
  segments: new SegmentsCacheInRedis(log, keys, redisClient),
43
41
  impressions: new ImpressionsCacheInRedis(log, keys.buildImpressionsKey(), redisClient, metadata),
44
42
  impressionCounts: impressionCountsCache,
@@ -19,7 +19,6 @@ import { UniqueKeysCacheInMemory } from '../inMemory/UniqueKeysCacheInMemory';
19
19
  import { UniqueKeysCacheInMemoryCS } from '../inMemory/UniqueKeysCacheInMemoryCS';
20
20
  import { metadataBuilder } from '../utils';
21
21
  import { LOG_PREFIX } from '../pluggable/constants';
22
- import { RBSegmentsCachePluggable } from './RBSegmentsCachePluggable';
23
22
  var NO_VALID_WRAPPER = 'Expecting pluggable storage `wrapper` in options, but no valid wrapper instance was provided.';
24
23
  var NO_VALID_WRAPPER_INTERFACE = 'The provided wrapper instance doesn’t follow the expected interface. Check our docs.';
25
24
  /**
@@ -72,7 +71,8 @@ export function PluggableStorage(options) {
72
71
  // Connects to wrapper and emits SDK_READY event on main client
73
72
  var connectPromise = wrapper.connect().then(function () {
74
73
  if (isSynchronizer) {
75
- // In standalone or producer mode, clear storage if SDK key or feature flag filter has changed
74
+ // @TODO reuse InLocalStorage::validateCache logic
75
+ // In standalone or producer mode, clear storage if SDK key, flags filter criteria or flags spec version was modified
76
76
  return wrapper.get(keys.buildHashKey()).then(function (hash) {
77
77
  var currentHash = getStorageHash(settings);
78
78
  if (hash !== currentHash) {
@@ -102,7 +102,6 @@ export function PluggableStorage(options) {
102
102
  });
103
103
  return {
104
104
  splits: new SplitsCachePluggable(log, keys, wrapper, settings.sync.__splitFiltersValidation),
105
- rbSegments: new RBSegmentsCachePluggable(log, keys, wrapper),
106
105
  segments: new SegmentsCachePluggable(log, keys, wrapper),
107
106
  impressions: isPartialConsumer ? new ImpressionsCacheInMemory(impressionsQueueSize) : new ImpressionsCachePluggable(log, keys.buildImpressionsKey(), wrapper, metadata),
108
107
  impressionCounts: impressionCountsCache,
@@ -23,6 +23,7 @@ export function impressionsToJSON(impressions, metadata) {
23
23
  c: impression.changeNumber,
24
24
  m: impression.time,
25
25
  pt: impression.pt,
26
+ properties: impression.properties
26
27
  }
27
28
  };
28
29
  return JSON.stringify(impressionWithMetadata);
@@ -42,9 +42,10 @@ export function fromObjectUpdaterFactory(splitsParser, storage, readiness, setti
42
42
  readiness.splits.emit(SDK_SPLITS_ARRIVED);
43
43
  if (startingUp) {
44
44
  startingUp = false;
45
- Promise.resolve(splitsCache.checkCache()).then(function (cacheReady) {
45
+ var isCacheLoaded_1 = storage.validateCache ? storage.validateCache() : false;
46
+ Promise.resolve().then(function () {
46
47
  // Emits SDK_READY_FROM_CACHE
47
- if (cacheReady)
48
+ if (isCacheLoaded_1)
48
49
  readiness.splits.emit(SDK_SPLITS_CACHE_LOADED);
49
50
  // Emits SDK_READY
50
51
  readiness.segments.emit(SDK_SEGMENTS_ARRIVED);
@@ -3,10 +3,10 @@
3
3
  * SplitChanges fetcher is a wrapper around `splitChanges` API service that parses the response and handle errors.
4
4
  */
5
5
  export function splitChangesFetcherFactory(fetchSplitChanges) {
6
- return function splitChangesFetcher(since, noCache, till, rbSince,
6
+ return function splitChangesFetcher(since, noCache, till,
7
7
  // Optional decorator for `fetchSplitChanges` promise, such as timeout or time tracker
8
8
  decorator) {
9
- var splitsPromise = fetchSplitChanges(since, noCache, till, rbSince);
9
+ var splitsPromise = fetchSplitChanges(since, noCache, till);
10
10
  if (decorator)
11
11
  splitsPromise = decorator(splitsPromise);
12
12
  return splitsPromise.then(function (resp) { return resp.json(); });
@@ -31,10 +31,10 @@ export function pollingManagerCSFactory(params) {
31
31
  readiness.splits.on(SDK_SPLITS_ARRIVED, function () {
32
32
  if (!splitsSyncTask.isRunning())
33
33
  return; // noop if not doing polling
34
- var usingSegments = storage.splits.usesSegments() || storage.rbSegments.usesSegments();
35
- if (usingSegments !== mySegmentsSyncTask.isRunning()) {
36
- log.info(POLLING_SMART_PAUSING, [usingSegments ? 'ON' : 'OFF']);
37
- if (usingSegments) {
34
+ var splitsHaveSegments = storage.splits.usesSegments();
35
+ if (splitsHaveSegments !== mySegmentsSyncTask.isRunning()) {
36
+ log.info(POLLING_SMART_PAUSING, [splitsHaveSegments ? 'ON' : 'OFF']);
37
+ if (splitsHaveSegments) {
38
38
  startMySegmentsSyncTasks();
39
39
  }
40
40
  else {
@@ -46,10 +46,10 @@ export function pollingManagerCSFactory(params) {
46
46
  var mySegmentsSyncTask = mySegmentsSyncTaskFactory(splitApi.fetchMemberships, storage, readiness, settings, matchingKey);
47
47
  // smart ready
48
48
  function smartReady() {
49
- if (!readiness.isReady() && !storage.splits.usesSegments() && !storage.rbSegments.usesSegments())
49
+ if (!readiness.isReady() && !storage.splits.usesSegments())
50
50
  readiness.segments.emit(SDK_SEGMENTS_ARRIVED);
51
51
  }
52
- if (!storage.splits.usesSegments() && !storage.rbSegments.usesSegments())
52
+ if (!storage.splits.usesSegments())
53
53
  setTimeout(smartReady, 0);
54
54
  else
55
55
  readiness.splits.once(SDK_SPLITS_ARRIVED, smartReady);
@@ -63,7 +63,7 @@ export function pollingManagerCSFactory(params) {
63
63
  start: function () {
64
64
  log.info(POLLING_START);
65
65
  splitsSyncTask.start();
66
- if (storage.splits.usesSegments() || storage.rbSegments.usesSegments())
66
+ if (storage.splits.usesSegments())
67
67
  startMySegmentsSyncTasks();
68
68
  },
69
69
  // Stop periodic fetching (polling)
@@ -9,7 +9,7 @@ import { MEMBERSHIPS_LS_UPDATE } from '../../streaming/constants';
9
9
  * - uses `segmentsEventEmitter` to emit events related to segments data updates
10
10
  */
11
11
  export function mySegmentsUpdaterFactory(log, mySegmentsFetcher, storage, segmentsEventEmitter, requestTimeoutBeforeReady, retriesOnFailureBeforeReady, matchingKey) {
12
- var splits = storage.splits, rbSegments = storage.rbSegments, segments = storage.segments, largeSegments = storage.largeSegments;
12
+ var splits = storage.splits, segments = storage.segments, largeSegments = storage.largeSegments;
13
13
  var readyOnAlreadyExistentState = true;
14
14
  var startingUp = true;
15
15
  /** timeout and telemetry decorator for `splitChangesFetcher` promise */
@@ -31,7 +31,7 @@ export function mySegmentsUpdaterFactory(log, mySegmentsFetcher, storage, segmen
31
31
  shouldNotifyUpdate = largeSegments.resetSegments(segmentsData.ls || {}) || shouldNotifyUpdate;
32
32
  }
33
33
  // Notify update if required
34
- if ((splits.usesSegments() || rbSegments.usesSegments()) && (shouldNotifyUpdate || readyOnAlreadyExistentState)) {
34
+ if (splits.usesSegments() && (shouldNotifyUpdate || readyOnAlreadyExistentState)) {
35
35
  readyOnAlreadyExistentState = false;
36
36
  segmentsEventEmitter.emit(SDK_SEGMENTS_ARRIVED);
37
37
  }
@@ -36,7 +36,7 @@ export function segmentChangesUpdaterFactory(log, segmentChangesFetcher, segment
36
36
  * Returned promise will not be rejected.
37
37
  *
38
38
  * @param fetchOnlyNew - if true, only fetch the segments that not exists, i.e., which `changeNumber` is equal to -1.
39
- * This param is used by SplitUpdateWorker on server-side SDK, to fetch new registered segments on SPLIT_UPDATE or RB_SEGMENT_UPDATE notifications.
39
+ * This param is used by SplitUpdateWorker on server-side SDK, to fetch new registered segments on SPLIT_UPDATE notifications.
40
40
  * @param segmentName - segment name to fetch. By passing `undefined` it fetches the list of segments registered at the storage
41
41
  * @param noCache - true to revalidate data to fetch on a SEGMENT_UPDATE notifications.
42
42
  * @param till - till target for the provided segmentName, for CDN bypass.