@splitsoftware/splitio-commons 3.0.0-rc.0 → 3.0.0-rc.1

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 (180) hide show
  1. package/CHANGES.txt +8 -3
  2. package/cjs/evaluator/index.js +11 -11
  3. package/cjs/logger/constants.js +4 -4
  4. package/cjs/logger/messages/warn.js +0 -1
  5. package/cjs/readiness/constants.js +4 -4
  6. package/cjs/readiness/readinessManager.js +22 -22
  7. package/cjs/readiness/sdkReadinessManager.js +0 -16
  8. package/cjs/sdkClient/sdkLifecycle.js +2 -1
  9. package/cjs/sdkClient/trackMethod.js +2 -2
  10. package/cjs/sdkFactory/index.js +5 -5
  11. package/cjs/sdkManager/index.js +2 -2
  12. package/cjs/services/authProvider.js +67 -0
  13. package/cjs/services/secureSplitHttpClient.js +39 -0
  14. package/cjs/services/splitApi.js +7 -2
  15. package/cjs/storages/AbstractDefinitionsCacheAsync.js +51 -0
  16. package/cjs/storages/AbstractDefinitionsCacheSync.js +76 -0
  17. package/cjs/storages/KeyBuilder.js +6 -6
  18. package/cjs/storages/KeyBuilderCS.js +5 -5
  19. package/cjs/storages/KeyBuilderSS.js +2 -2
  20. package/cjs/storages/getRolloutPlan.js +3 -3
  21. package/cjs/storages/inLocalStorage/DefinitionsCacheInLocal.js +194 -0
  22. package/cjs/storages/inLocalStorage/RBSegmentsCacheInLocal.js +9 -9
  23. package/cjs/storages/inLocalStorage/index.js +6 -6
  24. package/cjs/storages/inLocalStorage/validateCache.js +3 -3
  25. package/cjs/storages/inMemory/DefinitionsCacheInMemory.js +121 -0
  26. package/cjs/storages/inMemory/InMemoryStorage.js +4 -4
  27. package/cjs/storages/inMemory/InMemoryStorageCS.js +5 -5
  28. package/cjs/storages/inMemory/RBSegmentsCacheInMemory.js +4 -4
  29. package/cjs/storages/inMemory/SegmentsCacheInMemory.js +1 -1
  30. package/cjs/storages/inMemory/TelemetryCacheInMemory.js +3 -3
  31. package/cjs/storages/inRedis/{SplitsCacheInRedis.js → DefinitionsCacheInRedis.js} +77 -77
  32. package/cjs/storages/inRedis/index.js +2 -2
  33. package/cjs/storages/pluggable/DefinitionsCachePluggable.js +212 -0
  34. package/cjs/storages/pluggable/index.js +2 -2
  35. package/cjs/storages/setRolloutPlan.js +5 -5
  36. package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +5 -5
  37. package/cjs/sync/polling/fetchers/splitChangesFetcher.js +1 -1
  38. package/cjs/sync/polling/pollingManagerCS.js +7 -7
  39. package/cjs/sync/polling/syncTasks/definitionsSyncTask.js +1 -1
  40. package/cjs/sync/polling/updaters/definitionChangesUpdater.js +8 -8
  41. package/cjs/sync/polling/updaters/mySegmentsUpdater.js +2 -2
  42. package/cjs/sync/streaming/SSEHandler/NotificationKeeper.js +1 -1
  43. package/cjs/sync/streaming/SSEHandler/index.js +1 -1
  44. package/cjs/sync/streaming/UpdateWorkers/{SplitsUpdateWorker.js → DefinitionsUpdateWorker.js} +15 -15
  45. package/cjs/sync/streaming/constants.js +3 -3
  46. package/cjs/sync/streaming/pushManager.js +13 -13
  47. package/cjs/sync/submitters/telemetrySubmitter.js +1 -2
  48. package/cjs/sync/syncManagerOnline.js +4 -4
  49. package/cjs/utils/Backoff.js +20 -0
  50. package/cjs/utils/lang/sets.js +4 -4
  51. package/cjs/utils/settingsValidation/url.js +1 -1
  52. package/esm/evaluator/index.js +11 -11
  53. package/esm/logger/constants.js +1 -1
  54. package/esm/logger/messages/warn.js +0 -1
  55. package/esm/readiness/constants.js +3 -3
  56. package/esm/readiness/readinessManager.js +23 -23
  57. package/esm/readiness/sdkReadinessManager.js +1 -17
  58. package/esm/sdkClient/sdkLifecycle.js +2 -1
  59. package/esm/sdkClient/trackMethod.js +2 -2
  60. package/esm/sdkFactory/index.js +6 -6
  61. package/esm/sdkManager/index.js +2 -2
  62. package/esm/services/authProvider.js +63 -0
  63. package/esm/services/secureSplitHttpClient.js +35 -0
  64. package/esm/services/splitApi.js +7 -2
  65. package/esm/storages/AbstractDefinitionsCacheAsync.js +48 -0
  66. package/esm/storages/AbstractDefinitionsCacheSync.js +71 -0
  67. package/esm/storages/KeyBuilder.js +6 -6
  68. package/esm/storages/KeyBuilderCS.js +5 -5
  69. package/esm/storages/KeyBuilderSS.js +2 -2
  70. package/esm/storages/getRolloutPlan.js +3 -3
  71. package/esm/storages/inLocalStorage/DefinitionsCacheInLocal.js +191 -0
  72. package/esm/storages/inLocalStorage/RBSegmentsCacheInLocal.js +6 -6
  73. package/esm/storages/inLocalStorage/index.js +6 -6
  74. package/esm/storages/inLocalStorage/validateCache.js +3 -3
  75. package/esm/storages/inMemory/DefinitionsCacheInMemory.js +118 -0
  76. package/esm/storages/inMemory/InMemoryStorage.js +4 -4
  77. package/esm/storages/inMemory/InMemoryStorageCS.js +5 -5
  78. package/esm/storages/inMemory/RBSegmentsCacheInMemory.js +1 -1
  79. package/esm/storages/inMemory/SegmentsCacheInMemory.js +1 -1
  80. package/esm/storages/inMemory/TelemetryCacheInMemory.js +3 -3
  81. package/esm/storages/inRedis/{SplitsCacheInRedis.js → DefinitionsCacheInRedis.js} +76 -76
  82. package/esm/storages/inRedis/index.js +2 -2
  83. package/esm/storages/pluggable/DefinitionsCachePluggable.js +209 -0
  84. package/esm/storages/pluggable/index.js +2 -2
  85. package/esm/storages/setRolloutPlan.js +5 -5
  86. package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +6 -6
  87. package/esm/sync/polling/fetchers/splitChangesFetcher.js +1 -1
  88. package/esm/sync/polling/pollingManagerCS.js +4 -4
  89. package/esm/sync/polling/syncTasks/definitionsSyncTask.js +1 -1
  90. package/esm/sync/polling/updaters/definitionChangesUpdater.js +9 -9
  91. package/esm/sync/polling/updaters/mySegmentsUpdater.js +1 -1
  92. package/esm/sync/streaming/SSEHandler/NotificationKeeper.js +2 -2
  93. package/esm/sync/streaming/SSEHandler/index.js +2 -2
  94. package/esm/sync/streaming/UpdateWorkers/{SplitsUpdateWorker.js → DefinitionsUpdateWorker.js} +14 -14
  95. package/esm/sync/streaming/constants.js +2 -2
  96. package/esm/sync/streaming/pushManager.js +14 -14
  97. package/esm/sync/submitters/telemetrySubmitter.js +1 -2
  98. package/esm/sync/syncManagerOnline.js +3 -3
  99. package/esm/utils/Backoff.js +20 -0
  100. package/esm/utils/lang/sets.js +4 -4
  101. package/esm/utils/settingsValidation/url.js +1 -1
  102. package/package.json +1 -1
  103. package/src/dtos/types.ts +2 -2
  104. package/src/evaluator/index.ts +11 -11
  105. package/src/logger/constants.ts +1 -1
  106. package/src/logger/messages/warn.ts +0 -1
  107. package/src/readiness/constants.ts +3 -3
  108. package/src/readiness/readinessManager.ts +22 -22
  109. package/src/readiness/sdkReadinessManager.ts +1 -16
  110. package/src/readiness/types.ts +10 -16
  111. package/src/sdkClient/sdkLifecycle.ts +2 -1
  112. package/src/sdkClient/trackMethod.ts +2 -2
  113. package/src/sdkFactory/index.ts +5 -5
  114. package/src/sdkManager/index.ts +6 -6
  115. package/src/services/authProvider.ts +86 -0
  116. package/src/services/secureSplitHttpClient.ts +44 -0
  117. package/src/services/splitApi.ts +9 -3
  118. package/src/services/types.ts +4 -0
  119. package/src/storages/AbstractDefinitionsCacheAsync.ts +63 -0
  120. package/src/storages/AbstractDefinitionsCacheSync.ts +95 -0
  121. package/src/storages/KeyBuilder.ts +6 -6
  122. package/src/storages/KeyBuilderCS.ts +6 -6
  123. package/src/storages/KeyBuilderSS.ts +2 -2
  124. package/src/storages/getRolloutPlan.ts +3 -3
  125. package/src/storages/inLocalStorage/DefinitionsCacheInLocal.ts +234 -0
  126. package/src/storages/inLocalStorage/RBSegmentsCacheInLocal.ts +6 -6
  127. package/src/storages/inLocalStorage/index.ts +6 -6
  128. package/src/storages/inLocalStorage/validateCache.ts +4 -4
  129. package/src/storages/inMemory/DefinitionsCacheInMemory.ts +130 -0
  130. package/src/storages/inMemory/InMemoryStorage.ts +4 -4
  131. package/src/storages/inMemory/InMemoryStorageCS.ts +5 -5
  132. package/src/storages/inMemory/RBSegmentsCacheInMemory.ts +1 -1
  133. package/src/storages/inMemory/SegmentsCacheInMemory.ts +1 -1
  134. package/src/storages/inMemory/TelemetryCacheInMemory.ts +3 -3
  135. package/src/storages/inRedis/{SplitsCacheInRedis.ts → DefinitionsCacheInRedis.ts} +67 -67
  136. package/src/storages/inRedis/index.ts +2 -2
  137. package/src/storages/pluggable/DefinitionsCachePluggable.ts +230 -0
  138. package/src/storages/pluggable/index.ts +2 -2
  139. package/src/storages/setRolloutPlan.ts +7 -7
  140. package/src/storages/types.ts +19 -19
  141. package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +8 -8
  142. package/src/sync/polling/fetchers/splitChangesFetcher.ts +3 -3
  143. package/src/sync/polling/pollingManagerCS.ts +4 -4
  144. package/src/sync/polling/syncTasks/definitionsSyncTask.ts +1 -1
  145. package/src/sync/polling/types.ts +1 -1
  146. package/src/sync/polling/updaters/definitionChangesUpdater.ts +11 -11
  147. package/src/sync/polling/updaters/mySegmentsUpdater.ts +1 -1
  148. package/src/sync/streaming/AuthClient/index.ts +2 -2
  149. package/src/sync/streaming/AuthClient/types.ts +14 -11
  150. package/src/sync/streaming/SSEClient/index.ts +2 -2
  151. package/src/sync/streaming/SSEClient/types.ts +2 -2
  152. package/src/sync/streaming/SSEHandler/NotificationKeeper.ts +2 -2
  153. package/src/sync/streaming/SSEHandler/index.ts +2 -2
  154. package/src/sync/streaming/UpdateWorkers/{SplitsUpdateWorker.ts → DefinitionsUpdateWorker.ts} +16 -16
  155. package/src/sync/streaming/constants.ts +2 -2
  156. package/src/sync/streaming/pushManager.ts +16 -16
  157. package/src/sync/streaming/types.ts +2 -2
  158. package/src/sync/submitters/telemetrySubmitter.ts +1 -2
  159. package/src/sync/syncManagerOnline.ts +3 -3
  160. package/src/utils/Backoff.ts +21 -0
  161. package/src/utils/inputValidation/trafficTypeExistence.ts +2 -2
  162. package/src/utils/jwt/types.ts +1 -1
  163. package/src/utils/lang/sets.ts +4 -2
  164. package/src/utils/settingsValidation/url.ts +1 -1
  165. package/types/splitio.d.ts +23 -5
  166. package/cjs/storages/AbstractSplitsCacheAsync.js +0 -51
  167. package/cjs/storages/AbstractSplitsCacheSync.js +0 -76
  168. package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +0 -194
  169. package/cjs/storages/inMemory/SplitsCacheInMemory.js +0 -121
  170. package/cjs/storages/pluggable/SplitsCachePluggable.js +0 -212
  171. package/esm/storages/AbstractSplitsCacheAsync.js +0 -48
  172. package/esm/storages/AbstractSplitsCacheSync.js +0 -71
  173. package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +0 -191
  174. package/esm/storages/inMemory/SplitsCacheInMemory.js +0 -118
  175. package/esm/storages/pluggable/SplitsCachePluggable.js +0 -209
  176. package/src/storages/AbstractSplitsCacheAsync.ts +0 -63
  177. package/src/storages/AbstractSplitsCacheSync.ts +0 -95
  178. package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +0 -234
  179. package/src/storages/inMemory/SplitsCacheInMemory.ts +0 -130
  180. package/src/storages/pluggable/SplitsCachePluggable.ts +0 -230
@@ -1,118 +0,0 @@
1
- import { __extends } from "tslib";
2
- import { AbstractSplitsCacheSync, usesSegments } from '../AbstractSplitsCacheSync';
3
- import { isFiniteNumber } from '../../utils/lang';
4
- /**
5
- * Default ISplitsCacheSync implementation that stores split definitions in memory.
6
- */
7
- var SplitsCacheInMemory = /** @class */ (function (_super) {
8
- __extends(SplitsCacheInMemory, _super);
9
- function SplitsCacheInMemory(splitFiltersValidation) {
10
- var _this = _super.call(this) || this;
11
- _this.splitsCache = {};
12
- _this.ttCache = {};
13
- _this.changeNumber = -1;
14
- _this.segmentsCount = 0;
15
- _this.flagSetsCache = {};
16
- _this.flagSetsFilter = splitFiltersValidation ? splitFiltersValidation.groupedFilters.bySet : [];
17
- return _this;
18
- }
19
- SplitsCacheInMemory.prototype.clear = function () {
20
- this.splitsCache = {};
21
- this.ttCache = {};
22
- this.changeNumber = -1;
23
- this.segmentsCount = 0;
24
- this.flagSetsCache = {};
25
- };
26
- SplitsCacheInMemory.prototype.addSplit = function (split) {
27
- var name = split.name;
28
- var previousSplit = this.getSplit(name);
29
- if (previousSplit) { // We had this Split already
30
- var previousTtName = previousSplit.trafficTypeName;
31
- this.ttCache[previousTtName]--;
32
- if (!this.ttCache[previousTtName])
33
- delete this.ttCache[previousTtName];
34
- this.removeFromFlagSets(previousSplit.name, previousSplit.sets);
35
- // Subtract from segments count for the previous version of this Split
36
- if (usesSegments(previousSplit))
37
- this.segmentsCount--;
38
- }
39
- // Store the Split.
40
- this.splitsCache[name] = split;
41
- // Update TT cache
42
- var ttName = split.trafficTypeName;
43
- this.ttCache[ttName] = (this.ttCache[ttName] || 0) + 1;
44
- this.addToFlagSets(split);
45
- // Add to segments count for the new version of the Split
46
- if (usesSegments(split))
47
- this.segmentsCount++;
48
- return true;
49
- };
50
- SplitsCacheInMemory.prototype.removeSplit = function (name) {
51
- var split = this.getSplit(name);
52
- if (!split)
53
- return false;
54
- // Delete the Split
55
- delete this.splitsCache[name];
56
- var ttName = split.trafficTypeName;
57
- this.ttCache[ttName]--; // Update tt cache
58
- if (!this.ttCache[ttName])
59
- delete this.ttCache[ttName];
60
- this.removeFromFlagSets(split.name, split.sets);
61
- // Update the segments count.
62
- if (usesSegments(split))
63
- this.segmentsCount--;
64
- return true;
65
- };
66
- SplitsCacheInMemory.prototype.getSplit = function (name) {
67
- return this.splitsCache[name] || null;
68
- };
69
- SplitsCacheInMemory.prototype.setChangeNumber = function (changeNumber) {
70
- this.changeNumber = changeNumber;
71
- return true;
72
- };
73
- SplitsCacheInMemory.prototype.getChangeNumber = function () {
74
- return this.changeNumber;
75
- };
76
- SplitsCacheInMemory.prototype.getSplitNames = function () {
77
- return Object.keys(this.splitsCache);
78
- };
79
- SplitsCacheInMemory.prototype.trafficTypeExists = function (trafficType) {
80
- return isFiniteNumber(this.ttCache[trafficType]) && this.ttCache[trafficType] > 0;
81
- };
82
- SplitsCacheInMemory.prototype.usesSegments = function () {
83
- return this.getChangeNumber() === -1 || this.segmentsCount > 0;
84
- };
85
- SplitsCacheInMemory.prototype.getNamesByFlagSets = function (flagSets) {
86
- var _this = this;
87
- return flagSets.map(function (flagSet) { return _this.flagSetsCache[flagSet] || new Set(); });
88
- };
89
- SplitsCacheInMemory.prototype.addToFlagSets = function (featureFlag) {
90
- var _this = this;
91
- if (!featureFlag.sets)
92
- return;
93
- featureFlag.sets.forEach(function (featureFlagSet) {
94
- if (_this.flagSetsFilter.length > 0 && !_this.flagSetsFilter.some(function (filterFlagSet) { return filterFlagSet === featureFlagSet; }))
95
- return;
96
- if (!_this.flagSetsCache[featureFlagSet])
97
- _this.flagSetsCache[featureFlagSet] = new Set([]);
98
- _this.flagSetsCache[featureFlagSet].add(featureFlag.name);
99
- });
100
- };
101
- SplitsCacheInMemory.prototype.removeFromFlagSets = function (featureFlagName, flagSets) {
102
- var _this = this;
103
- if (!flagSets)
104
- return;
105
- flagSets.forEach(function (flagSet) {
106
- _this.removeNames(flagSet, featureFlagName);
107
- });
108
- };
109
- SplitsCacheInMemory.prototype.removeNames = function (flagSetName, featureFlagName) {
110
- if (!this.flagSetsCache[flagSetName])
111
- return;
112
- this.flagSetsCache[flagSetName].delete(featureFlagName);
113
- if (this.flagSetsCache[flagSetName].size === 0)
114
- delete this.flagSetsCache[flagSetName];
115
- };
116
- return SplitsCacheInMemory;
117
- }(AbstractSplitsCacheSync));
118
- export { SplitsCacheInMemory };
@@ -1,209 +0,0 @@
1
- import { __extends, __spreadArray } from "tslib";
2
- import { isFiniteNumber, isNaNNumber } from '../../utils/lang';
3
- import { LOG_PREFIX } from './constants';
4
- import { AbstractSplitsCacheAsync } from '../AbstractSplitsCacheAsync';
5
- import { returnDifference } from '../../utils/lang/sets';
6
- /**
7
- * ISplitsCacheAsync implementation for pluggable storages.
8
- */
9
- var SplitsCachePluggable = /** @class */ (function (_super) {
10
- __extends(SplitsCachePluggable, _super);
11
- /**
12
- * Create a SplitsCache that uses a storage wrapper.
13
- * @param log - Logger instance.
14
- * @param keys - Key builder.
15
- * @param wrapper - Adapted wrapper storage.
16
- */
17
- function SplitsCachePluggable(log, keys, wrapper, splitFiltersValidation) {
18
- var _this = _super.call(this) || this;
19
- _this.log = log;
20
- _this.keys = keys;
21
- _this.wrapper = wrapper;
22
- _this.flagSetsFilter = splitFiltersValidation ? splitFiltersValidation.groupedFilters.bySet : [];
23
- return _this;
24
- }
25
- SplitsCachePluggable.prototype._decrementCounts = function (split) {
26
- var _this = this;
27
- var ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
28
- return this.wrapper.decr(ttKey).then(function (count) {
29
- if (count === 0)
30
- return _this.wrapper.del(ttKey);
31
- });
32
- };
33
- SplitsCachePluggable.prototype._incrementCounts = function (split) {
34
- var ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
35
- return this.wrapper.incr(ttKey);
36
- };
37
- SplitsCachePluggable.prototype._updateFlagSets = function (featureFlagName, flagSetsOfRemovedFlag, flagSetsOfAddedFlag) {
38
- var _this = this;
39
- var removeFromFlagSets = returnDifference(flagSetsOfRemovedFlag, flagSetsOfAddedFlag);
40
- var addToFlagSets = returnDifference(flagSetsOfAddedFlag, flagSetsOfRemovedFlag);
41
- if (this.flagSetsFilter.length > 0) {
42
- addToFlagSets = addToFlagSets.filter(function (flagSet) {
43
- return _this.flagSetsFilter.some(function (filterFlagSet) { return filterFlagSet === flagSet; });
44
- });
45
- }
46
- var items = [featureFlagName];
47
- return Promise.all(__spreadArray(__spreadArray([], removeFromFlagSets.map(function (flagSetName) { return _this.wrapper.removeItems(_this.keys.buildFlagSetKey(flagSetName), items); }), true), addToFlagSets.map(function (flagSetName) { return _this.wrapper.addItems(_this.keys.buildFlagSetKey(flagSetName), items); }), true));
48
- };
49
- /**
50
- * Add a given split.
51
- * The returned promise is resolved when the operation success
52
- * or rejected if it fails (e.g., wrapper operation fails)
53
- */
54
- SplitsCachePluggable.prototype.addSplit = function (split) {
55
- var _this = this;
56
- var name = split.name;
57
- var splitKey = this.keys.buildSplitKey(name);
58
- return this.wrapper.get(splitKey).then(function (splitFromStorage) {
59
- // handling parsing error
60
- var parsedPreviousSplit, stringifiedNewSplit;
61
- try {
62
- parsedPreviousSplit = splitFromStorage ? JSON.parse(splitFromStorage) : undefined;
63
- stringifiedNewSplit = JSON.stringify(split);
64
- }
65
- catch (e) {
66
- throw new Error('Error parsing feature flag definition: ' + e);
67
- }
68
- return _this.wrapper.set(splitKey, stringifiedNewSplit).then(function () {
69
- // avoid unnecessary increment/decrement operations
70
- if (parsedPreviousSplit && parsedPreviousSplit.trafficTypeName === split.trafficTypeName)
71
- return;
72
- // update traffic type counts
73
- return _this._incrementCounts(split).then(function () {
74
- if (parsedPreviousSplit)
75
- return _this._decrementCounts(parsedPreviousSplit);
76
- });
77
- }).then(function () { return _this._updateFlagSets(name, parsedPreviousSplit && parsedPreviousSplit.sets, split.sets); });
78
- }).then(function () { return true; });
79
- };
80
- /**
81
- * Remove a given split.
82
- * The returned promise is resolved when the operation success, with a boolean indicating if the split existed or not.
83
- * or rejected if it fails (e.g., wrapper operation fails).
84
- */
85
- SplitsCachePluggable.prototype.removeSplit = function (name) {
86
- var _this = this;
87
- return this.getSplit(name).then(function (split) {
88
- if (split) {
89
- return _this._decrementCounts(split).then(function () { return _this._updateFlagSets(name, split.sets); });
90
- }
91
- }).then(function () {
92
- return _this.wrapper.del(_this.keys.buildSplitKey(name));
93
- });
94
- };
95
- /**
96
- * Get split.
97
- * The returned promise is resolved with the split definition or null if it's not defined,
98
- * or rejected if wrapper operation fails.
99
- */
100
- SplitsCachePluggable.prototype.getSplit = function (name) {
101
- return this.wrapper.get(this.keys.buildSplitKey(name))
102
- .then(function (maybeSplit) { return maybeSplit && JSON.parse(maybeSplit); });
103
- };
104
- /**
105
- * Get list of splits.
106
- * The returned promise is resolved with a map of split names to their split definition or null if it's not defined,
107
- * or rejected if wrapper operation fails.
108
- */
109
- SplitsCachePluggable.prototype.getSplits = function (names) {
110
- var _this = this;
111
- var keys = names.map(function (name) { return _this.keys.buildSplitKey(name); });
112
- return this.wrapper.getMany(keys).then(function (splitDefinitions) {
113
- var splits = {};
114
- names.forEach(function (name, idx) {
115
- var split = splitDefinitions[idx];
116
- splits[name] = split && JSON.parse(split);
117
- });
118
- return Promise.resolve(splits);
119
- });
120
- };
121
- /**
122
- * Get list of all split definitions.
123
- * The returned promise is resolved with the list of split definitions,
124
- * or rejected if wrapper operation fails.
125
- */
126
- SplitsCachePluggable.prototype.getAll = function () {
127
- var _this = this;
128
- return this.wrapper.getKeysByPrefix(this.keys.buildSplitKeyPrefix())
129
- .then(function (listOfKeys) { return _this.wrapper.getMany(listOfKeys); })
130
- .then(function (splitDefinitions) { return splitDefinitions.map(function (splitDefinition) {
131
- return JSON.parse(splitDefinition);
132
- }); });
133
- };
134
- /**
135
- * Get list of split names.
136
- * The returned promise is resolved with the list of split names,
137
- * or rejected if wrapper operation fails.
138
- */
139
- SplitsCachePluggable.prototype.getSplitNames = function () {
140
- var _this = this;
141
- return this.wrapper.getKeysByPrefix(this.keys.buildSplitKeyPrefix()).then(function (listOfKeys) { return listOfKeys.map(_this.keys.extractKey); });
142
- };
143
- /**
144
- * Get list of feature flag names related to a given list of flag set names.
145
- * The returned promise is resolved with the list of feature flag names per flag set.
146
- * It never rejects (If there is a wrapper error for some flag set, an empty set is returned for it).
147
- */
148
- SplitsCachePluggable.prototype.getNamesByFlagSets = function (flagSets) {
149
- var _this = this;
150
- return Promise.all(flagSets.map(function (flagSet) {
151
- var flagSetKey = _this.keys.buildFlagSetKey(flagSet);
152
- return _this.wrapper.getItems(flagSetKey).catch(function () { return []; });
153
- })).then(function (namesByFlagSets) { return namesByFlagSets.map(function (namesByFlagSet) { return new Set(namesByFlagSet); }); });
154
- };
155
- /**
156
- * Check traffic type existence.
157
- * The returned promise is resolved with a boolean indicating whether the TT exist or not.
158
- * In case of wrapper operation failures, the promise resolves with a true value, assuming that the TT might exist.
159
- * It will never be rejected.
160
- */
161
- SplitsCachePluggable.prototype.trafficTypeExists = function (trafficType) {
162
- var _this = this;
163
- // If there is a number there should be > 0, otherwise the TT is considered as not existent.
164
- return this.wrapper.get(this.keys.buildTrafficTypeKey(trafficType))
165
- .then(function (ttCount) {
166
- if (ttCount === null)
167
- return false; // if entry doesn't exist, means that TT doesn't exist
168
- ttCount = parseInt(ttCount, 10);
169
- if (!isFiniteNumber(ttCount) || ttCount < 0) {
170
- _this.log.info(LOG_PREFIX + "Could not validate traffic type existence of ".concat(trafficType, " due to data corruption of some sorts."));
171
- return false;
172
- }
173
- return ttCount > 0;
174
- }).catch(function (e) {
175
- _this.log.error(LOG_PREFIX + "Could not validate traffic type existence of ".concat(trafficType, " due to an error: ").concat(e, "."));
176
- // If there is an error, bypass the validation so the event can get tracked.
177
- return true;
178
- });
179
- };
180
- /**
181
- * Set till number.
182
- * The returned promise is resolved when the operation success,
183
- * or rejected if it fails (e.g., wrapper operation fails).
184
- */
185
- SplitsCachePluggable.prototype.setChangeNumber = function (changeNumber) {
186
- return this.wrapper.set(this.keys.buildSplitsTillKey(), changeNumber + '');
187
- };
188
- /**
189
- * Get till number or -1 if it's not defined.
190
- * The returned promise is resolved with the changeNumber or -1 if it doesn't exist or a wrapper operation fails.
191
- * The promise will never be rejected.
192
- */
193
- SplitsCachePluggable.prototype.getChangeNumber = function () {
194
- var _this = this;
195
- return this.wrapper.get(this.keys.buildSplitsTillKey()).then(function (value) {
196
- var i = parseInt(value, 10);
197
- return isNaNNumber(i) ? -1 : i;
198
- }).catch(function (e) {
199
- _this.log.error(LOG_PREFIX + 'Could not retrieve changeNumber from storage. Error: ' + e);
200
- return -1;
201
- });
202
- };
203
- // @TODO implement if required by DataLoader or producer mode
204
- SplitsCachePluggable.prototype.clear = function () {
205
- return Promise.resolve(true);
206
- };
207
- return SplitsCachePluggable;
208
- }(AbstractSplitsCacheAsync));
209
- export { SplitsCachePluggable };
@@ -1,63 +0,0 @@
1
- import { ISplitsCacheAsync } from './types';
2
- import { IDefinition } from '../dtos/types';
3
- import { objectAssign } from '../utils/lang/objectAssign';
4
-
5
- /**
6
- * This class provides a skeletal implementation of the ISplitsCacheAsync interface
7
- * to minimize the effort required to implement this interface.
8
- */
9
- export abstract class AbstractSplitsCacheAsync implements ISplitsCacheAsync {
10
-
11
- protected abstract addSplit(split: IDefinition): Promise<boolean>
12
- protected abstract removeSplit(name: string): Promise<boolean>
13
- protected abstract setChangeNumber(changeNumber: number): Promise<boolean | void>
14
-
15
- update(toAdd: IDefinition[], toRemove: string[], changeNumber: number): Promise<boolean> {
16
- return Promise.all([
17
- this.setChangeNumber(changeNumber),
18
- Promise.all(toAdd.map(addedFF => this.addSplit(addedFF))),
19
- Promise.all(toRemove.map(removedFF => this.removeSplit(removedFF)))
20
- ]).then(([, added, removed]) => {
21
- return added.some(result => result) || removed.some(result => result);
22
- });
23
- }
24
-
25
- abstract getSplit(name: string): Promise<IDefinition | null>
26
- abstract getSplits(names: string[]): Promise<Record<string, IDefinition | null>>
27
- abstract getChangeNumber(): Promise<number>
28
- abstract getAll(): Promise<IDefinition[]>
29
- abstract getSplitNames(): Promise<string[]>
30
- abstract getNamesByFlagSets(flagSets: string[]): Promise<Set<string>[]>
31
- abstract trafficTypeExists(trafficType: string): Promise<boolean>
32
- abstract clear(): Promise<boolean | void>
33
-
34
- // @TODO revisit segment-related methods ('usesSegments', 'getRegisteredSegments', 'registerSegments')
35
- // noop, just keeping the interface. This is used by standalone client-side API only, and so only implemented by InMemory and InLocalStorage.
36
- usesSegments(): Promise<boolean> {
37
- return Promise.resolve(true);
38
- }
39
-
40
- /**
41
- * Kill `name` split and set `defaultTreatment` and `changeNumber`.
42
- * Used for SPLIT_KILL push notifications.
43
- *
44
- * @returns a promise that is resolved once the split kill operation is performed. The fulfillment value is a boolean: `true` if the operation successed updating the split or `false` if no split is updated,
45
- * for instance, if the `changeNumber` is old, or if the split is not found (e.g., `/splitchanges` hasn't been fetched yet), or if the storage fails to apply the update.
46
- * The promise will never be rejected.
47
- */
48
- killLocally(name: string, defaultTreatment: string, changeNumber: number): Promise<boolean> {
49
- return this.getSplit(name).then(split => {
50
-
51
- if (split && (!split.changeNumber || split.changeNumber < changeNumber)) {
52
- const newSplit = objectAssign({}, split);
53
- newSplit.killed = true;
54
- newSplit.defaultTreatment = defaultTreatment;
55
- newSplit.changeNumber = changeNumber;
56
-
57
- return this.addSplit(newSplit);
58
- }
59
- return false;
60
- }).catch(() => false);
61
- }
62
-
63
- }
@@ -1,95 +0,0 @@
1
- import { ISplitsCacheSync, IStorageSync } from './types';
2
- import { IRBSegment, IDefinition } from '../dtos/types';
3
- import { objectAssign } from '../utils/lang/objectAssign';
4
- import { IN_SEGMENT, IN_LARGE_SEGMENT } from '../utils/constants';
5
-
6
- /**
7
- * This class provides a skeletal implementation of the ISplitsCacheSync interface
8
- * to minimize the effort required to implement this interface.
9
- */
10
- export abstract class AbstractSplitsCacheSync implements ISplitsCacheSync {
11
-
12
- protected abstract addSplit(split: IDefinition): boolean
13
- protected abstract removeSplit(name: string): boolean
14
- protected abstract setChangeNumber(changeNumber: number): boolean | void
15
-
16
- update(toAdd: IDefinition[], toRemove: string[], changeNumber: number): boolean {
17
- let updated = toAdd.map(addedFF => this.addSplit(addedFF)).some(result => result);
18
- updated = toRemove.map(removedFF => this.removeSplit(removedFF)).some(result => result) || updated;
19
- this.setChangeNumber(changeNumber);
20
- return updated;
21
- }
22
-
23
- abstract getSplit(name: string): IDefinition | null
24
-
25
- getSplits(names: string[]): Record<string, IDefinition | null> {
26
- const splits: Record<string, IDefinition | null> = {};
27
- names.forEach(name => {
28
- splits[name] = this.getSplit(name);
29
- });
30
- return splits;
31
- }
32
-
33
- abstract getChangeNumber(): number
34
-
35
- getAll(): IDefinition[] {
36
- return this.getSplitNames().map(key => this.getSplit(key) as IDefinition);
37
- }
38
-
39
- abstract getSplitNames(): string[]
40
-
41
- abstract trafficTypeExists(trafficType: string): boolean
42
-
43
- abstract usesSegments(): boolean
44
-
45
- abstract clear(): void
46
-
47
- /**
48
- * Kill `name` split and set `defaultTreatment` and `changeNumber`.
49
- * Used for SPLIT_KILL push notifications.
50
- *
51
- * @returns `true` if the operation successed updating the split, or `false` if no split is updated,
52
- * for instance, if the `changeNumber` is old, or if the split is not found (e.g., `/splitchanges` hasn't been fetched yet), or if the storage fails to apply the update.
53
- */
54
- killLocally(name: string, defaultTreatment: string, changeNumber: number): boolean {
55
- const split = this.getSplit(name);
56
-
57
- if (split && (!split.changeNumber || split.changeNumber < changeNumber)) {
58
- const newSplit = objectAssign({}, split);
59
- newSplit.killed = true;
60
- newSplit.defaultTreatment = defaultTreatment;
61
- newSplit.changeNumber = changeNumber;
62
-
63
- return this.addSplit(newSplit);
64
- }
65
- return false;
66
- }
67
-
68
- abstract getNamesByFlagSets(flagSets: string[]): Set<string>[]
69
-
70
- }
71
-
72
- /**
73
- * Given a parsed split, it returns a boolean flagging if its conditions use segments matchers (rules & whitelists).
74
- * This util is intended to simplify the implementation of `splitsCache::usesSegments` method
75
- */
76
- export function usesSegments(ruleEntity: IDefinition | IRBSegment) {
77
- const conditions = ruleEntity.conditions || [];
78
- for (let i = 0; i < conditions.length; i++) {
79
- const matchers = conditions[i].matcherGroup.matchers;
80
-
81
- for (let j = 0; j < matchers.length; j++) {
82
- const matcher = matchers[j].matcherType;
83
- if (matcher === IN_SEGMENT || matcher === IN_LARGE_SEGMENT) return true;
84
- }
85
- }
86
-
87
- const excluded = (ruleEntity as IRBSegment).excluded;
88
- if (excluded && excluded.segments && excluded.segments.length > 0) return true;
89
-
90
- return false;
91
- }
92
-
93
- export function usesSegmentsSync(storage: Pick<IStorageSync, 'splits' | 'rbSegments'>) {
94
- return storage.splits.usesSegments() || storage.rbSegments.usesSegments();
95
- }