@splitsoftware/splitio-commons 3.1.1-rc.10 → 3.1.1-rc.12

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 (237) hide show
  1. package/CHANGES.txt +3 -0
  2. package/cjs/evaluator/condition/index.js +2 -2
  3. package/cjs/evaluator/fallbackTreatmentsCalculator/fallbackSanitizer/index.js +5 -8
  4. package/cjs/evaluator/index.js +81 -71
  5. package/cjs/listeners/browser.js +2 -1
  6. package/cjs/listeners/node.js +4 -4
  7. package/cjs/logger/constants.js +15 -20
  8. package/cjs/logger/messages/debug.js +5 -6
  9. package/cjs/logger/messages/error.js +3 -3
  10. package/cjs/logger/messages/info.js +3 -4
  11. package/cjs/logger/messages/warn.js +6 -7
  12. package/cjs/platform/browser.js +15 -0
  13. package/cjs/platform/getEventSource/browser.js +8 -0
  14. package/cjs/platform/getEventSource/eventsource.js +480 -0
  15. package/cjs/platform/getEventSource/node.js +26 -0
  16. package/cjs/platform/getFetch/browser.js +9 -0
  17. package/cjs/platform/getFetch/node.js +27 -0
  18. package/cjs/platform/getOptions/node.js +25 -0
  19. package/cjs/platform/node.js +17 -0
  20. package/cjs/presets/serverSide.js +2 -2
  21. package/cjs/readiness/constants.js +5 -4
  22. package/cjs/readiness/readinessManager.js +23 -23
  23. package/cjs/readiness/sdkReadinessManager.js +1 -17
  24. package/cjs/sdkClient/client.js +24 -50
  25. package/cjs/sdkClient/clientInputValidation.js +6 -17
  26. package/cjs/sdkClient/sdkClient.js +3 -60
  27. package/cjs/sdkClient/sdkLifecycle.js +74 -0
  28. package/cjs/sdkClient/trackMethod.js +56 -0
  29. package/cjs/sdkFactory/index.js +11 -15
  30. package/cjs/sdkManager/index.js +5 -5
  31. package/cjs/services/authProvider.js +80 -0
  32. package/cjs/services/decorateHeaders.js +2 -1
  33. package/cjs/{sync/streaming/AuthClient/index.js → services/fetchAuth.js} +15 -16
  34. package/cjs/services/secureSplitHttpClient.js +36 -0
  35. package/cjs/services/serviceApi.js +147 -0
  36. package/cjs/services/splitApi.js +24 -10
  37. package/cjs/services/splitHttpClient.js +3 -5
  38. package/cjs/storages/AbstractDefinitionsCacheAsync.js +51 -0
  39. package/cjs/storages/AbstractDefinitionsCacheSync.js +76 -0
  40. package/cjs/storages/KeyBuilder.js +6 -6
  41. package/cjs/storages/KeyBuilderCS.js +5 -5
  42. package/cjs/storages/KeyBuilderSS.js +2 -2
  43. package/cjs/storages/getRolloutPlan.js +3 -3
  44. package/cjs/storages/inLocalStorage/DefinitionsCacheInLocal.js +195 -0
  45. package/cjs/storages/inLocalStorage/MySegmentsCacheInLocal.js +1 -2
  46. package/cjs/storages/inLocalStorage/RBSegmentsCacheInLocal.js +12 -11
  47. package/cjs/storages/inLocalStorage/index.js +6 -6
  48. package/cjs/storages/inLocalStorage/validateCache.js +3 -3
  49. package/cjs/storages/inMemory/DefinitionsCacheInMemory.js +122 -0
  50. package/cjs/storages/inMemory/InMemoryStorage.js +4 -4
  51. package/cjs/storages/inMemory/InMemoryStorageCS.js +5 -5
  52. package/cjs/storages/inMemory/RBSegmentsCacheInMemory.js +7 -6
  53. package/cjs/storages/inMemory/SegmentsCacheInMemory.js +1 -1
  54. package/cjs/storages/inMemory/TelemetryCacheInMemory.js +5 -5
  55. package/cjs/storages/inRedis/{SplitsCacheInRedis.js → DefinitionsCacheInRedis.js} +79 -77
  56. package/cjs/storages/inRedis/RBSegmentsCacheInRedis.js +4 -2
  57. package/cjs/storages/inRedis/index.js +2 -2
  58. package/cjs/storages/pluggable/DefinitionsCachePluggable.js +214 -0
  59. package/cjs/storages/pluggable/RBSegmentsCachePluggable.js +4 -2
  60. package/cjs/storages/pluggable/index.js +2 -2
  61. package/cjs/storages/setRolloutPlan.js +4 -4
  62. package/cjs/storages/utils.js +1 -0
  63. package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +5 -5
  64. package/cjs/sync/polling/fetchers/splitChangesFetcher.js +24 -15
  65. package/cjs/sync/polling/pollingManagerCS.js +86 -81
  66. package/cjs/sync/polling/pollingManagerSS.js +44 -36
  67. package/cjs/sync/polling/syncTasks/definitionsSyncTask.js +13 -0
  68. package/cjs/sync/polling/syncTasks/segmentsSyncTask.js +2 -3
  69. package/cjs/sync/polling/updaters/definitionChangesUpdater.js +207 -0
  70. package/cjs/sync/polling/updaters/mySegmentsUpdater.js +3 -3
  71. package/cjs/sync/polling/updaters/segmentChangesUpdater.js +6 -6
  72. package/cjs/sync/streaming/SSEHandler/NotificationKeeper.js +1 -1
  73. package/cjs/sync/streaming/SSEHandler/index.js +2 -1
  74. package/cjs/sync/streaming/UpdateWorkers/{SplitsUpdateWorker.js → DefinitionsUpdateWorker.js} +19 -20
  75. package/cjs/sync/streaming/constants.js +4 -3
  76. package/cjs/sync/streaming/parseUtils.js +14 -14
  77. package/cjs/sync/streaming/pushManager.js +22 -26
  78. package/cjs/sync/submitters/eventsSubmitter.js +1 -1
  79. package/cjs/sync/submitters/impressionCountsSubmitter.js +1 -1
  80. package/cjs/sync/submitters/impressionsSubmitter.js +5 -4
  81. package/cjs/sync/submitters/telemetrySubmitter.js +3 -4
  82. package/cjs/sync/submitters/uniqueKeysSubmitter.js +1 -1
  83. package/cjs/sync/syncManagerOnline.js +5 -5
  84. package/cjs/utils/Backoff.js +35 -3
  85. package/cjs/utils/EventEmitter.js +391 -0
  86. package/cjs/utils/filter/bloomFilter.js +37 -0
  87. package/cjs/utils/inputValidation/{split.js → definition.js} +10 -10
  88. package/cjs/utils/inputValidation/{splitExistence.js → definitionExistence.js} +6 -6
  89. package/cjs/utils/inputValidation/definitions.js +25 -0
  90. package/cjs/utils/inputValidation/index.js +7 -7
  91. package/cjs/utils/inputValidation/isOperational.js +4 -4
  92. package/cjs/utils/inputValidation/target.js +22 -0
  93. package/cjs/utils/labels/index.js +3 -3
  94. package/cjs/utils/settingsValidation/index.js +2 -0
  95. package/cjs/utils/settingsValidation/splitFilters.js +3 -3
  96. package/cjs/utils/settingsValidation/url.js +6 -2
  97. package/esm/evaluator/condition/index.js +2 -2
  98. package/esm/evaluator/fallbackTreatmentsCalculator/fallbackSanitizer/index.js +5 -8
  99. package/esm/evaluator/index.js +80 -71
  100. package/esm/listeners/browser.js +2 -1
  101. package/esm/listeners/node.js +4 -4
  102. package/esm/logger/constants.js +12 -17
  103. package/esm/logger/messages/debug.js +5 -6
  104. package/esm/logger/messages/error.js +3 -3
  105. package/esm/logger/messages/info.js +3 -4
  106. package/esm/logger/messages/warn.js +6 -7
  107. package/esm/platform/browser.js +12 -0
  108. package/esm/platform/getEventSource/browser.js +4 -0
  109. package/esm/platform/getEventSource/eventsource.js +480 -0
  110. package/esm/platform/getEventSource/node.js +20 -0
  111. package/esm/platform/getFetch/browser.js +5 -0
  112. package/esm/platform/getFetch/node.js +22 -0
  113. package/esm/platform/getOptions/node.js +20 -0
  114. package/esm/platform/node.js +14 -0
  115. package/esm/presets/serverSide.js +2 -2
  116. package/esm/readiness/constants.js +4 -3
  117. package/esm/readiness/readinessManager.js +24 -24
  118. package/esm/readiness/sdkReadinessManager.js +2 -18
  119. package/esm/sdkClient/client.js +23 -50
  120. package/esm/sdkClient/clientInputValidation.js +8 -19
  121. package/esm/sdkClient/sdkClient.js +3 -60
  122. package/esm/sdkClient/sdkLifecycle.js +70 -0
  123. package/esm/sdkClient/trackMethod.js +52 -0
  124. package/esm/sdkFactory/index.js +12 -16
  125. package/esm/sdkManager/index.js +6 -6
  126. package/esm/services/authProvider.js +76 -0
  127. package/esm/services/decorateHeaders.js +2 -1
  128. package/esm/{sync/streaming/AuthClient/index.js → services/fetchAuth.js} +13 -13
  129. package/esm/services/secureSplitHttpClient.js +32 -0
  130. package/esm/services/serviceApi.js +143 -0
  131. package/esm/services/splitApi.js +24 -10
  132. package/esm/services/splitHttpClient.js +3 -5
  133. package/esm/storages/AbstractDefinitionsCacheAsync.js +48 -0
  134. package/esm/storages/AbstractDefinitionsCacheSync.js +71 -0
  135. package/esm/storages/KeyBuilder.js +6 -6
  136. package/esm/storages/KeyBuilderCS.js +5 -5
  137. package/esm/storages/KeyBuilderSS.js +2 -2
  138. package/esm/storages/getRolloutPlan.js +3 -3
  139. package/esm/storages/inLocalStorage/DefinitionsCacheInLocal.js +192 -0
  140. package/esm/storages/inLocalStorage/MySegmentsCacheInLocal.js +1 -2
  141. package/esm/storages/inLocalStorage/RBSegmentsCacheInLocal.js +9 -8
  142. package/esm/storages/inLocalStorage/index.js +6 -6
  143. package/esm/storages/inLocalStorage/validateCache.js +3 -3
  144. package/esm/storages/inMemory/DefinitionsCacheInMemory.js +119 -0
  145. package/esm/storages/inMemory/InMemoryStorage.js +4 -4
  146. package/esm/storages/inMemory/InMemoryStorageCS.js +5 -5
  147. package/esm/storages/inMemory/RBSegmentsCacheInMemory.js +4 -3
  148. package/esm/storages/inMemory/SegmentsCacheInMemory.js +1 -1
  149. package/esm/storages/inMemory/TelemetryCacheInMemory.js +5 -5
  150. package/esm/storages/inRedis/{SplitsCacheInRedis.js → DefinitionsCacheInRedis.js} +78 -76
  151. package/esm/storages/inRedis/RBSegmentsCacheInRedis.js +4 -2
  152. package/esm/storages/inRedis/index.js +2 -2
  153. package/esm/storages/pluggable/DefinitionsCachePluggable.js +211 -0
  154. package/esm/storages/pluggable/RBSegmentsCachePluggable.js +4 -2
  155. package/esm/storages/pluggable/index.js +2 -2
  156. package/esm/storages/setRolloutPlan.js +4 -4
  157. package/esm/storages/utils.js +1 -0
  158. package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +6 -6
  159. package/esm/sync/polling/fetchers/splitChangesFetcher.js +25 -16
  160. package/esm/sync/polling/pollingManagerCS.js +87 -82
  161. package/esm/sync/polling/pollingManagerSS.js +44 -36
  162. package/esm/sync/polling/syncTasks/definitionsSyncTask.js +9 -0
  163. package/esm/sync/polling/syncTasks/segmentsSyncTask.js +2 -3
  164. package/esm/sync/polling/updaters/definitionChangesUpdater.js +201 -0
  165. package/esm/sync/polling/updaters/mySegmentsUpdater.js +3 -3
  166. package/esm/sync/polling/updaters/segmentChangesUpdater.js +7 -7
  167. package/esm/sync/streaming/SSEHandler/NotificationKeeper.js +2 -2
  168. package/esm/sync/streaming/SSEHandler/index.js +3 -2
  169. package/esm/sync/streaming/UpdateWorkers/{SplitsUpdateWorker.js → DefinitionsUpdateWorker.js} +19 -20
  170. package/esm/sync/streaming/constants.js +3 -2
  171. package/esm/sync/streaming/parseUtils.js +11 -11
  172. package/esm/sync/streaming/pushManager.js +24 -28
  173. package/esm/sync/submitters/eventsSubmitter.js +1 -1
  174. package/esm/sync/submitters/impressionCountsSubmitter.js +1 -1
  175. package/esm/sync/submitters/impressionsSubmitter.js +5 -4
  176. package/esm/sync/submitters/telemetrySubmitter.js +3 -4
  177. package/esm/sync/submitters/uniqueKeysSubmitter.js +1 -1
  178. package/esm/sync/syncManagerOnline.js +4 -4
  179. package/esm/utils/Backoff.js +35 -3
  180. package/esm/utils/EventEmitter.js +387 -0
  181. package/esm/utils/filter/bloomFilter.js +33 -0
  182. package/esm/utils/inputValidation/{split.js → definition.js} +8 -8
  183. package/esm/utils/inputValidation/definitionExistence.js +15 -0
  184. package/esm/utils/inputValidation/definitions.js +21 -0
  185. package/esm/utils/inputValidation/index.js +3 -3
  186. package/esm/utils/inputValidation/isOperational.js +4 -4
  187. package/esm/utils/inputValidation/target.js +18 -0
  188. package/esm/utils/labels/index.js +2 -2
  189. package/esm/utils/settingsValidation/index.js +2 -0
  190. package/esm/utils/settingsValidation/splitFilters.js +3 -3
  191. package/esm/utils/settingsValidation/url.js +6 -2
  192. package/package.json +1 -1
  193. package/src/dtos/types.ts +4 -4
  194. package/src/services/authProvider.ts +2 -2
  195. package/src/{sync/streaming/AuthClient/index.ts → services/fetchAuth.ts} +3 -4
  196. package/src/services/splitApi.ts +1 -1
  197. package/src/services/types.ts +21 -4
  198. package/src/storages/AbstractDefinitionsCacheAsync.ts +3 -3
  199. package/src/storages/AbstractDefinitionsCacheSync.ts +2 -2
  200. package/src/storages/inLocalStorage/DefinitionsCacheInLocal.ts +2 -2
  201. package/src/storages/inLocalStorage/RBSegmentsCacheInLocal.ts +3 -3
  202. package/src/storages/inMemory/DefinitionsCacheInMemory.ts +2 -2
  203. package/src/storages/inMemory/RBSegmentsCacheInMemory.ts +2 -2
  204. package/src/storages/inRedis/DefinitionsCacheInRedis.ts +6 -4
  205. package/src/storages/inRedis/RBSegmentsCacheInRedis.ts +7 -5
  206. package/src/storages/pluggable/DefinitionsCachePluggable.ts +4 -2
  207. package/src/storages/pluggable/RBSegmentsCachePluggable.ts +5 -3
  208. package/src/storages/types.ts +6 -6
  209. package/src/sync/polling/fetchers/splitChangesFetcher.ts +5 -5
  210. package/src/sync/polling/types.ts +22 -1
  211. package/src/sync/polling/updaters/definitionChangesUpdater.ts +26 -20
  212. package/src/sync/streaming/SSEClient/index.ts +1 -1
  213. package/src/sync/streaming/SSEClient/types.ts +1 -1
  214. package/src/sync/streaming/UpdateWorkers/DefinitionsUpdateWorker.ts +15 -11
  215. package/src/sync/streaming/UpdateWorkers/types.ts +6 -0
  216. package/src/sync/streaming/parseUtils.ts +3 -4
  217. package/src/sync/streaming/pushManager.ts +4 -3
  218. package/cjs/storages/AbstractSplitsCacheAsync.js +0 -51
  219. package/cjs/storages/AbstractSplitsCacheSync.js +0 -76
  220. package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +0 -194
  221. package/cjs/storages/inMemory/SplitsCacheInMemory.js +0 -121
  222. package/cjs/storages/pluggable/SplitsCachePluggable.js +0 -212
  223. package/cjs/sync/polling/syncTasks/splitsSyncTask.js +0 -13
  224. package/cjs/sync/polling/updaters/splitChangesUpdater.js +0 -200
  225. package/cjs/sync/streaming/AuthClient/types.js +0 -2
  226. package/cjs/utils/inputValidation/splits.js +0 -25
  227. package/esm/storages/AbstractSplitsCacheAsync.js +0 -48
  228. package/esm/storages/AbstractSplitsCacheSync.js +0 -71
  229. package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +0 -191
  230. package/esm/storages/inMemory/SplitsCacheInMemory.js +0 -118
  231. package/esm/storages/pluggable/SplitsCachePluggable.js +0 -209
  232. package/esm/sync/polling/syncTasks/splitsSyncTask.js +0 -9
  233. package/esm/sync/polling/updaters/splitChangesUpdater.js +0 -194
  234. package/esm/sync/streaming/AuthClient/types.js +0 -1
  235. package/esm/utils/inputValidation/splitExistence.js +0 -15
  236. package/esm/utils/inputValidation/splits.js +0 -21
  237. package/src/sync/streaming/AuthClient/types.ts +0 -19
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RBSegmentsCacheInMemory = void 0;
4
4
  var sets_1 = require("../../utils/lang/sets");
5
- var AbstractSplitsCacheSync_1 = require("../AbstractSplitsCacheSync");
5
+ var AbstractDefinitionsCacheSync_1 = require("../AbstractDefinitionsCacheSync");
6
6
  var RBSegmentsCacheInMemory = /** @class */ (function () {
7
7
  function RBSegmentsCacheInMemory() {
8
8
  this.cache = {};
@@ -17,17 +17,18 @@ var RBSegmentsCacheInMemory = /** @class */ (function () {
17
17
  RBSegmentsCacheInMemory.prototype.update = function (toAdd, toRemove, changeNumber) {
18
18
  var _this = this;
19
19
  var updated = toAdd.map(function (toAdd) { return _this.add(toAdd); }).some(function (result) { return result; });
20
- updated = toRemove.map(function (toRemove) { return _this.remove(toRemove.name); }).some(function (result) { return result; }) || updated;
21
- this.changeNumber = changeNumber;
20
+ updated = toRemove.map(function (toRemove) { return _this.remove(toRemove); }).some(function (result) { return result; }) || updated;
21
+ if (changeNumber !== undefined)
22
+ this.changeNumber = changeNumber;
22
23
  return updated;
23
24
  };
24
25
  RBSegmentsCacheInMemory.prototype.add = function (rbSegment) {
25
26
  var name = rbSegment.name;
26
27
  var previous = this.get(name);
27
- if (previous && (0, AbstractSplitsCacheSync_1.usesSegments)(previous))
28
+ if (previous && (0, AbstractDefinitionsCacheSync_1.usesSegments)(previous))
28
29
  this.segmentsCount--;
29
30
  this.cache[name] = rbSegment;
30
- if ((0, AbstractSplitsCacheSync_1.usesSegments)(rbSegment))
31
+ if ((0, AbstractDefinitionsCacheSync_1.usesSegments)(rbSegment))
31
32
  this.segmentsCount++;
32
33
  return true;
33
34
  };
@@ -36,7 +37,7 @@ var RBSegmentsCacheInMemory = /** @class */ (function () {
36
37
  if (!rbSegment)
37
38
  return false;
38
39
  delete this.cache[name];
39
- if ((0, AbstractSplitsCacheSync_1.usesSegments)(rbSegment))
40
+ if ((0, AbstractDefinitionsCacheSync_1.usesSegments)(rbSegment))
40
41
  this.segmentsCount--;
41
42
  return true;
42
43
  };
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SegmentsCacheInMemory = void 0;
4
4
  var lang_1 = require("../../utils/lang");
5
5
  /**
6
- * Default ISplitsCacheSync implementation for server-side that stores segments definitions in memory.
6
+ * Default IDefinitionsCacheSync implementation for server-side that stores segments definitions in memory.
7
7
  */
8
8
  var SegmentsCacheInMemory = /** @class */ (function () {
9
9
  function SegmentsCacheInMemory() {
@@ -19,13 +19,13 @@ var ACCEPTANCE_RANGE = 0.001;
19
19
  * All factory instances track telemetry on server-side, and 0.1% on client-side.
20
20
  */
21
21
  function shouldRecordTelemetry(_a) {
22
- var settings = _a.settings;
23
- return settings.mode !== constants_1.LOCALHOST_MODE && ((0, key_1.checkIfServerSide)(settings) || Math.random() <= ACCEPTANCE_RANGE);
22
+ var settings = _a.settings, disableTelemetry = _a.disableTelemetry;
23
+ return !disableTelemetry && settings.mode !== constants_1.LOCALHOST_MODE && ((0, key_1.checkIfServerSide)(settings) || Math.random() <= ACCEPTANCE_RANGE);
24
24
  }
25
25
  exports.shouldRecordTelemetry = shouldRecordTelemetry;
26
26
  var TelemetryCacheInMemory = /** @class */ (function () {
27
- function TelemetryCacheInMemory(splits, segments, largeSegments) {
28
- this.splits = splits;
27
+ function TelemetryCacheInMemory(definitions, segments, largeSegments) {
28
+ this.definitions = definitions;
29
29
  this.segments = segments;
30
30
  this.largeSegments = largeSegments;
31
31
  this.name = 'telemetry stats';
@@ -61,7 +61,7 @@ var TelemetryCacheInMemory = /** @class */ (function () {
61
61
  iQ: this.getImpressionStats(constants_1.QUEUED),
62
62
  iDe: this.getImpressionStats(constants_1.DEDUPED),
63
63
  iDr: this.getImpressionStats(constants_1.DROPPED),
64
- spC: this.splits && this.splits.getSplitNames().length,
64
+ spC: this.definitions && this.definitions.getNames().length,
65
65
  seC: this.segments && this.segments.getRegisteredSegments().length,
66
66
  skC: this.segments && this.segments.getKeysCount(),
67
67
  lsC: this.largeSegments && this.largeSegments.getRegisteredSegments().length,
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SplitsCacheInRedis = void 0;
3
+ exports.DefinitionsCacheInRedis = void 0;
4
4
  var tslib_1 = require("tslib");
5
5
  var lang_1 = require("../../utils/lang");
6
6
  var constants_1 = require("./constants");
7
- var AbstractSplitsCacheAsync_1 = require("../AbstractSplitsCacheAsync");
7
+ var AbstractDefinitionsCacheAsync_1 = require("../AbstractDefinitionsCacheAsync");
8
8
  var sets_1 = require("../../utils/lang/sets");
9
9
  /**
10
10
  * Discard errors for an answer of multiple operations.
@@ -17,17 +17,17 @@ function processPipelineAnswer(results) {
17
17
  }, []) : [];
18
18
  }
19
19
  /**
20
- * ISplitsCacheAsync implementation that stores split definitions in Redis.
20
+ * IDefinitionsCacheAsync implementation that stores definitions in Redis.
21
21
  * Supported by Node.js
22
22
  */
23
- var SplitsCacheInRedis = /** @class */ (function (_super) {
24
- tslib_1.__extends(SplitsCacheInRedis, _super);
25
- function SplitsCacheInRedis(log, keys, redis, splitFiltersValidation) {
23
+ var DefinitionsCacheInRedis = /** @class */ (function (_super) {
24
+ tslib_1.__extends(DefinitionsCacheInRedis, _super);
25
+ function DefinitionsCacheInRedis(log, keys, redis, splitFiltersValidation) {
26
26
  var _this = _super.call(this) || this;
27
27
  _this.log = log;
28
28
  _this.redis = redis;
29
29
  _this.keys = keys;
30
- _this.flagSetsFilter = splitFiltersValidation ? splitFiltersValidation.groupedFilters.bySet : [];
30
+ _this.setsFilter = splitFiltersValidation ? splitFiltersValidation.groupedFilters.bySet : [];
31
31
  // There is no need to listen for redis 'error' event, because in that case ioredis calls will be rejected and handled by redis storage adapters.
32
32
  // But it is done just to avoid getting the ioredis message `Unhandled error event`.
33
33
  _this.redis.on('error', function (e) {
@@ -38,104 +38,106 @@ var SplitsCacheInRedis = /** @class */ (function (_super) {
38
38
  });
39
39
  return _this;
40
40
  }
41
- SplitsCacheInRedis.prototype._decrementCounts = function (split) {
41
+ DefinitionsCacheInRedis.prototype._decrementCounts = function (definition) {
42
42
  var _this = this;
43
- var ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
43
+ var ttKey = this.keys.buildTrafficTypeKey(definition.trafficTypeName);
44
44
  return this.redis.decr(ttKey).then(function (count) {
45
45
  if (count === 0)
46
46
  return _this.redis.del(ttKey);
47
47
  });
48
48
  };
49
- SplitsCacheInRedis.prototype._incrementCounts = function (split) {
50
- var ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
49
+ DefinitionsCacheInRedis.prototype._incrementCounts = function (definition) {
50
+ var ttKey = this.keys.buildTrafficTypeKey(definition.trafficTypeName);
51
51
  return this.redis.incr(ttKey);
52
52
  };
53
- SplitsCacheInRedis.prototype._updateFlagSets = function (featureFlagName, flagSetsOfRemovedFlag, flagSetsOfAddedFlag) {
53
+ DefinitionsCacheInRedis.prototype._updateSets = function (definitionName, setsOfRemovedDefinition, setsOfAddedDefinition) {
54
54
  var _this = this;
55
- var removeFromFlagSets = (0, sets_1.returnDifference)(flagSetsOfRemovedFlag, flagSetsOfAddedFlag);
56
- var addToFlagSets = (0, sets_1.returnDifference)(flagSetsOfAddedFlag, flagSetsOfRemovedFlag);
57
- if (this.flagSetsFilter.length > 0) {
58
- addToFlagSets = addToFlagSets.filter(function (flagSet) {
59
- return _this.flagSetsFilter.some(function (filterFlagSet) { return filterFlagSet === flagSet; });
55
+ var removeFromSets = (0, sets_1.returnDifference)(setsOfRemovedDefinition, setsOfAddedDefinition);
56
+ var addToSets = (0, sets_1.returnDifference)(setsOfAddedDefinition, setsOfRemovedDefinition);
57
+ if (this.setsFilter.length > 0) {
58
+ addToSets = addToSets.filter(function (set) {
59
+ return _this.setsFilter.some(function (filterSet) { return filterSet === set; });
60
60
  });
61
61
  }
62
- var items = [featureFlagName];
63
- return Promise.all(tslib_1.__spreadArray(tslib_1.__spreadArray([], removeFromFlagSets.map(function (flagSetName) { return _this.redis.srem(_this.keys.buildFlagSetKey(flagSetName), items); }), true), addToFlagSets.map(function (flagSetName) { return _this.redis.sadd(_this.keys.buildFlagSetKey(flagSetName), items); }), true));
62
+ var items = [definitionName];
63
+ return Promise.all(tslib_1.__spreadArray(tslib_1.__spreadArray([], removeFromSets.map(function (setName) { return _this.redis.srem(_this.keys.buildSetKey(setName), items); }), true), addToSets.map(function (setName) { return _this.redis.sadd(_this.keys.buildSetKey(setName), items); }), true));
64
64
  };
65
65
  /**
66
- * Add a given split.
66
+ * Add a given definition.
67
67
  * The returned promise is resolved when the operation success
68
68
  * or rejected if it fails (e.g., redis operation fails)
69
69
  */
70
- SplitsCacheInRedis.prototype.addSplit = function (split) {
70
+ DefinitionsCacheInRedis.prototype.add = function (definition) {
71
71
  var _this = this;
72
- var name = split.name;
73
- var splitKey = this.keys.buildSplitKey(name);
74
- return this.redis.get(splitKey).then(function (splitFromStorage) {
72
+ var name = definition.name;
73
+ var definitionKey = this.keys.buildDefinitionKey(name);
74
+ return this.redis.get(definitionKey).then(function (definitionFromStorage) {
75
75
  // handling parsing error
76
- var parsedPreviousSplit, stringifiedNewSplit;
76
+ var parsedPreviousDefinition, stringifiedNewDefinition;
77
77
  try {
78
- parsedPreviousSplit = splitFromStorage ? JSON.parse(splitFromStorage) : undefined;
79
- stringifiedNewSplit = JSON.stringify(split);
78
+ parsedPreviousDefinition = definitionFromStorage ? JSON.parse(definitionFromStorage) : undefined;
79
+ stringifiedNewDefinition = JSON.stringify(definition);
80
80
  }
81
81
  catch (e) {
82
82
  throw new Error('Error parsing feature flag definition: ' + e);
83
83
  }
84
- return _this.redis.set(splitKey, stringifiedNewSplit).then(function () {
84
+ return _this.redis.set(definitionKey, stringifiedNewDefinition).then(function () {
85
85
  // avoid unnecessary increment/decrement operations
86
- if (parsedPreviousSplit && parsedPreviousSplit.trafficTypeName === split.trafficTypeName)
86
+ if (parsedPreviousDefinition && parsedPreviousDefinition.trafficTypeName === definition.trafficTypeName)
87
87
  return;
88
88
  // update traffic type counts
89
- return _this._incrementCounts(split).then(function () {
90
- if (parsedPreviousSplit)
91
- return _this._decrementCounts(parsedPreviousSplit);
89
+ return _this._incrementCounts(definition).then(function () {
90
+ if (parsedPreviousDefinition)
91
+ return _this._decrementCounts(parsedPreviousDefinition);
92
92
  });
93
- }).then(function () { return _this._updateFlagSets(name, parsedPreviousSplit && parsedPreviousSplit.sets, split.sets); });
93
+ }).then(function () { return _this._updateSets(name, parsedPreviousDefinition && parsedPreviousDefinition.sets, definition.sets); });
94
94
  }).then(function () { return true; });
95
95
  };
96
96
  /**
97
- * Remove a given split.
98
- * The returned promise is resolved when the operation success, with true or false indicating if the split existed (and was removed) or not.
97
+ * Remove a given definition.
98
+ * The returned promise is resolved when the operation success, with true or false indicating if the definition existed (and was removed) or not.
99
99
  * or rejected if it fails (e.g., redis operation fails).
100
100
  */
101
- SplitsCacheInRedis.prototype.removeSplit = function (name) {
101
+ DefinitionsCacheInRedis.prototype.remove = function (name) {
102
102
  var _this = this;
103
- return this.getSplit(name).then(function (split) {
104
- if (split) {
105
- return _this._decrementCounts(split).then(function () { return _this._updateFlagSets(name, split.sets); });
103
+ return this.get(name).then(function (definition) {
104
+ if (definition) {
105
+ return _this._decrementCounts(definition).then(function () { return _this._updateSets(name, definition.sets); });
106
106
  }
107
107
  }).then(function () {
108
- return _this.redis.del(_this.keys.buildSplitKey(name)).then(function (status) { return status === 1; });
108
+ return _this.redis.del(_this.keys.buildDefinitionKey(name)).then(function (status) { return status === 1; });
109
109
  });
110
110
  };
111
111
  /**
112
- * Get split definition or null if it's not defined.
112
+ * Get definition or null if it's not defined.
113
113
  * Returned promise is rejected if redis operation fails.
114
114
  */
115
- SplitsCacheInRedis.prototype.getSplit = function (name) {
115
+ DefinitionsCacheInRedis.prototype.get = function (name) {
116
116
  if (this.redisError) {
117
117
  this.log.error(constants_1.LOG_PREFIX + this.redisError);
118
118
  return Promise.reject(this.redisError);
119
119
  }
120
- return this.redis.get(this.keys.buildSplitKey(name))
121
- .then(function (maybeSplit) { return maybeSplit && JSON.parse(maybeSplit); });
120
+ return this.redis.get(this.keys.buildDefinitionKey(name))
121
+ .then(function (maybeDefinition) { return maybeDefinition && JSON.parse(maybeDefinition); });
122
122
  };
123
123
  /**
124
124
  * Set till number.
125
125
  * The returned promise is resolved when the operation success,
126
126
  * or rejected if it fails.
127
127
  */
128
- SplitsCacheInRedis.prototype.setChangeNumber = function (changeNumber) {
129
- return this.redis.set(this.keys.buildSplitsTillKey(), changeNumber + '').then(function (status) { return status === 'OK'; });
128
+ DefinitionsCacheInRedis.prototype.setChangeNumber = function (changeNumber) {
129
+ if (changeNumber !== undefined) {
130
+ return this.redis.set(this.keys.buildDefinitionsTillKey(), changeNumber + '').then(function (status) { return status === 'OK'; });
131
+ }
130
132
  };
131
133
  /**
132
134
  * Get till number or -1 if it's not defined.
133
135
  * The returned promise is resolved with the changeNumber or -1 if it doesn't exist or a redis operation fails.
134
136
  * The promise will never be rejected.
135
137
  */
136
- SplitsCacheInRedis.prototype.getChangeNumber = function () {
138
+ DefinitionsCacheInRedis.prototype.getChangeNumber = function () {
137
139
  var _this = this;
138
- return this.redis.get(this.keys.buildSplitsTillKey()).then(function (value) {
140
+ return this.redis.get(this.keys.buildDefinitionsTillKey()).then(function (value) {
139
141
  var i = parseInt(value, 10);
140
142
  return (0, lang_1.isNaNNumber)(i) ? -1 : i;
141
143
  }).catch(function (e) {
@@ -144,44 +146,44 @@ var SplitsCacheInRedis = /** @class */ (function (_super) {
144
146
  });
145
147
  };
146
148
  /**
147
- * Get list of all split definitions.
148
- * The returned promise is resolved with the list of split definitions,
149
+ * Get list of all definitions.
150
+ * The returned promise is resolved with the list of definitions,
149
151
  * or rejected if redis operation fails.
150
152
  */
151
153
  // @TODO we need to benchmark which is the maximun number of commands we could pipeline without kill redis performance.
152
- SplitsCacheInRedis.prototype.getAll = function () {
154
+ DefinitionsCacheInRedis.prototype.getAll = function () {
153
155
  var _this = this;
154
- return this.redis.keys(this.keys.searchPatternForSplitKeys())
156
+ return this.redis.keys(this.keys.searchPatternForDefinitionKeys())
155
157
  .then(function (listOfKeys) { return _this.redis.pipeline(listOfKeys.map(function (k) { return ['get', k]; })).exec(); })
156
158
  .then(processPipelineAnswer)
157
- .then(function (splitDefinitions) { return splitDefinitions.map(function (splitDefinition) {
158
- return JSON.parse(splitDefinition);
159
+ .then(function (definitions) { return definitions.map(function (definition) {
160
+ return JSON.parse(definition);
159
161
  }); });
160
162
  };
161
163
  /**
162
- * Get list of split names.
163
- * The returned promise is resolved with the list of split names,
164
+ * Get list of definition names.
165
+ * The returned promise is resolved with the list of names,
164
166
  * or rejected if redis operation fails.
165
167
  */
166
- SplitsCacheInRedis.prototype.getSplitNames = function () {
168
+ DefinitionsCacheInRedis.prototype.getNames = function () {
167
169
  var _this = this;
168
- return this.redis.keys(this.keys.searchPatternForSplitKeys()).then(function (listOfKeys) { return listOfKeys.map(_this.keys.extractKey); });
170
+ return this.redis.keys(this.keys.searchPatternForDefinitionKeys()).then(function (listOfKeys) { return listOfKeys.map(_this.keys.extractKey); });
169
171
  };
170
172
  /**
171
- * Get list of feature flag names related to a given list of flag set names.
172
- * The returned promise is resolved with the list of feature flag names per flag set,
173
+ * Get list of definition names related to a given list of set names.
174
+ * The returned promise is resolved with the list of names per set,
173
175
  * or rejected if the pipelined redis operation fails (e.g., timeout).
174
176
  */
175
- SplitsCacheInRedis.prototype.getNamesByFlagSets = function (flagSets) {
177
+ DefinitionsCacheInRedis.prototype.getNamesBySets = function (sets) {
176
178
  var _this = this;
177
- return this.redis.pipeline(flagSets.map(function (flagSet) { return ['smembers', _this.keys.buildFlagSetKey(flagSet)]; })).exec()
179
+ return this.redis.pipeline(sets.map(function (set) { return ['smembers', _this.keys.buildSetKey(set)]; })).exec()
178
180
  .then(function (results) { return results ? results.map(function (_a, index) {
179
181
  var e = _a[0], value = _a[1];
180
182
  if (e === null)
181
183
  return value;
182
- _this.log.error(constants_1.LOG_PREFIX + "Could not read result from get members of flag set ".concat(flagSets[index], " due to an error: ").concat(e));
184
+ _this.log.error(constants_1.LOG_PREFIX + "Could not read result from get members of set ".concat(sets[index], " due to an error: ").concat(e));
183
185
  }) : []; })
184
- .then(function (namesByFlagSets) { return namesByFlagSets.map(function (namesByFlagSet) { return new Set(namesByFlagSet); }); });
186
+ .then(function (namesBySets) { return namesBySets.map(function (namesBySet) { return new Set(namesBySet); }); });
185
187
  };
186
188
  /**
187
189
  * Check traffic type existence.
@@ -189,7 +191,7 @@ var SplitsCacheInRedis = /** @class */ (function (_super) {
189
191
  * In case of redis operation failure, the promise resolves with a true value, assuming that the TT might exist.
190
192
  * It will never be rejected.
191
193
  */
192
- SplitsCacheInRedis.prototype.trafficTypeExists = function (trafficType) {
194
+ DefinitionsCacheInRedis.prototype.trafficTypeExists = function (trafficType) {
193
195
  var _this = this;
194
196
  // If there is a number there should be > 0, otherwise the TT is considered as not existent.
195
197
  return this.redis.get(this.keys.buildTrafficTypeKey(trafficType))
@@ -210,34 +212,34 @@ var SplitsCacheInRedis = /** @class */ (function (_super) {
210
212
  });
211
213
  };
212
214
  // @TODO remove or implement. It is not being used.
213
- SplitsCacheInRedis.prototype.clear = function () {
215
+ DefinitionsCacheInRedis.prototype.clear = function () {
214
216
  return Promise.resolve();
215
217
  };
216
218
  /**
217
- * Fetches multiple splits definitions.
219
+ * Fetches multiple definitions.
218
220
  * Returned promise is rejected if redis operation fails.
219
221
  */
220
- SplitsCacheInRedis.prototype.getSplits = function (names) {
222
+ DefinitionsCacheInRedis.prototype.getMany = function (names) {
221
223
  var _a;
222
224
  var _this = this;
223
225
  if (this.redisError) {
224
226
  this.log.error(constants_1.LOG_PREFIX + this.redisError);
225
227
  return Promise.reject(this.redisError);
226
228
  }
227
- var keys = names.map(function (name) { return _this.keys.buildSplitKey(name); });
228
- return (_a = this.redis).mget.apply(_a, keys).then(function (splitDefinitions) {
229
- var splits = {};
229
+ var keys = names.map(function (name) { return _this.keys.buildDefinitionKey(name); });
230
+ return (_a = this.redis).mget.apply(_a, keys).then(function (stringifiedDefinitions) {
231
+ var definitions = {};
230
232
  names.forEach(function (name, idx) {
231
- var split = splitDefinitions[idx];
232
- splits[name] = split && JSON.parse(split);
233
+ var definition = stringifiedDefinitions[idx];
234
+ definitions[name] = definition && JSON.parse(definition);
233
235
  });
234
- return Promise.resolve(splits);
236
+ return Promise.resolve(definitions);
235
237
  })
236
238
  .catch(function (e) {
237
239
  _this.log.error(constants_1.LOG_PREFIX + "Could not grab feature flags due to an error: ".concat(e, "."));
238
240
  return Promise.reject(e);
239
241
  });
240
242
  };
241
- return SplitsCacheInRedis;
242
- }(AbstractSplitsCacheAsync_1.AbstractSplitsCacheAsync));
243
- exports.SplitsCacheInRedis = SplitsCacheInRedis;
243
+ return DefinitionsCacheInRedis;
244
+ }(AbstractDefinitionsCacheAsync_1.AbstractDefinitionsCacheAsync));
245
+ exports.DefinitionsCacheInRedis = DefinitionsCacheInRedis;
@@ -34,7 +34,7 @@ var RBSegmentsCacheInRedis = /** @class */ (function () {
34
34
  return _this.redis.set(key, stringifiedNewRBSegment).then(function () { return true; });
35
35
  })),
36
36
  Promise.all(toRemove.map(function (toRemove) {
37
- var key = _this.keys.buildRBSegmentKey(toRemove.name);
37
+ var key = _this.keys.buildRBSegmentKey(toRemove);
38
38
  return _this.redis.del(key).then(function (status) { return status === 1; });
39
39
  }))
40
40
  ]).then(function (_a) {
@@ -43,7 +43,9 @@ var RBSegmentsCacheInRedis = /** @class */ (function () {
43
43
  });
44
44
  };
45
45
  RBSegmentsCacheInRedis.prototype.setChangeNumber = function (changeNumber) {
46
- return this.redis.set(this.keys.buildRBSegmentsTillKey(), changeNumber + '').then(function (status) { return status === 'OK'; });
46
+ if (changeNumber !== undefined) {
47
+ return this.redis.set(this.keys.buildRBSegmentsTillKey(), changeNumber + '').then(function (status) { return status === 'OK'; });
48
+ }
47
49
  };
48
50
  RBSegmentsCacheInRedis.prototype.getChangeNumber = function () {
49
51
  var _this = this;
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InRedisStorage = void 0;
4
4
  var KeyBuilder_1 = require("../KeyBuilder");
5
5
  var KeyBuilderSS_1 = require("../KeyBuilderSS");
6
- var SplitsCacheInRedis_1 = require("./SplitsCacheInRedis");
6
+ var DefinitionsCacheInRedis_1 = require("./DefinitionsCacheInRedis");
7
7
  var SegmentsCacheInRedis_1 = require("./SegmentsCacheInRedis");
8
8
  var ImpressionsCacheInRedis_1 = require("./ImpressionsCacheInRedis");
9
9
  var EventsCacheInRedis_1 = require("./EventsCacheInRedis");
@@ -48,7 +48,7 @@ function InRedisStorage(options) {
48
48
  telemetry.recordConfig();
49
49
  });
50
50
  return {
51
- splits: new SplitsCacheInRedis_1.SplitsCacheInRedis(log, keys, redisClient, settings.sync.__splitFiltersValidation),
51
+ definitions: new DefinitionsCacheInRedis_1.DefinitionsCacheInRedis(log, keys, redisClient, settings.sync.__splitFiltersValidation),
52
52
  rbSegments: new RBSegmentsCacheInRedis_1.RBSegmentsCacheInRedis(log, keys, redisClient),
53
53
  segments: new SegmentsCacheInRedis_1.SegmentsCacheInRedis(log, keys, redisClient),
54
54
  impressions: new ImpressionsCacheInRedis_1.ImpressionsCacheInRedis(log, keys.buildImpressionsKey(), redisClient, metadata),
@@ -0,0 +1,214 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DefinitionsCachePluggable = void 0;
4
+ var tslib_1 = require("tslib");
5
+ var lang_1 = require("../../utils/lang");
6
+ var constants_1 = require("./constants");
7
+ var AbstractDefinitionsCacheAsync_1 = require("../AbstractDefinitionsCacheAsync");
8
+ var sets_1 = require("../../utils/lang/sets");
9
+ /**
10
+ * IDefinitionsCacheAsync implementation for pluggable storages.
11
+ */
12
+ var DefinitionsCachePluggable = /** @class */ (function (_super) {
13
+ tslib_1.__extends(DefinitionsCachePluggable, _super);
14
+ /**
15
+ * Create a DefinitionsCache that uses a storage wrapper.
16
+ * @param log - Logger instance.
17
+ * @param keys - Key builder.
18
+ * @param wrapper - Adapted wrapper storage.
19
+ */
20
+ function DefinitionsCachePluggable(log, keys, wrapper, splitFiltersValidation) {
21
+ var _this = _super.call(this) || this;
22
+ _this.log = log;
23
+ _this.keys = keys;
24
+ _this.wrapper = wrapper;
25
+ _this.setsFilter = splitFiltersValidation ? splitFiltersValidation.groupedFilters.bySet : [];
26
+ return _this;
27
+ }
28
+ DefinitionsCachePluggable.prototype._decrementCounts = function (definition) {
29
+ var _this = this;
30
+ var ttKey = this.keys.buildTrafficTypeKey(definition.trafficTypeName);
31
+ return this.wrapper.decr(ttKey).then(function (count) {
32
+ if (count === 0)
33
+ return _this.wrapper.del(ttKey);
34
+ });
35
+ };
36
+ DefinitionsCachePluggable.prototype._incrementCounts = function (definition) {
37
+ var ttKey = this.keys.buildTrafficTypeKey(definition.trafficTypeName);
38
+ return this.wrapper.incr(ttKey);
39
+ };
40
+ DefinitionsCachePluggable.prototype._updateSets = function (definitionName, setsOfRemovedDefinition, setsOfAddedDefinition) {
41
+ var _this = this;
42
+ var removeFromSets = (0, sets_1.returnDifference)(setsOfRemovedDefinition, setsOfAddedDefinition);
43
+ var addToSets = (0, sets_1.returnDifference)(setsOfAddedDefinition, setsOfRemovedDefinition);
44
+ if (this.setsFilter.length > 0) {
45
+ addToSets = addToSets.filter(function (set) {
46
+ return _this.setsFilter.some(function (filterSet) { return filterSet === set; });
47
+ });
48
+ }
49
+ var items = [definitionName];
50
+ return Promise.all(tslib_1.__spreadArray(tslib_1.__spreadArray([], removeFromSets.map(function (setName) { return _this.wrapper.removeItems(_this.keys.buildSetKey(setName), items); }), true), addToSets.map(function (setName) { return _this.wrapper.addItems(_this.keys.buildSetKey(setName), items); }), true));
51
+ };
52
+ /**
53
+ * Add a given definition.
54
+ * The returned promise is resolved when the operation success
55
+ * or rejected if it fails (e.g., wrapper operation fails)
56
+ */
57
+ DefinitionsCachePluggable.prototype.add = function (definition) {
58
+ var _this = this;
59
+ var name = definition.name;
60
+ var definitionKey = this.keys.buildDefinitionKey(name);
61
+ return this.wrapper.get(definitionKey).then(function (definitionFromStorage) {
62
+ // handling parsing error
63
+ var parsedPreviousDefinition, stringifiedNewDefinition;
64
+ try {
65
+ parsedPreviousDefinition = definitionFromStorage ? JSON.parse(definitionFromStorage) : undefined;
66
+ stringifiedNewDefinition = JSON.stringify(definition);
67
+ }
68
+ catch (e) {
69
+ throw new Error('Error parsing feature flag definition: ' + e);
70
+ }
71
+ return _this.wrapper.set(definitionKey, stringifiedNewDefinition).then(function () {
72
+ // avoid unnecessary increment/decrement operations
73
+ if (parsedPreviousDefinition && parsedPreviousDefinition.trafficTypeName === definition.trafficTypeName)
74
+ return;
75
+ // update traffic type counts
76
+ return _this._incrementCounts(definition).then(function () {
77
+ if (parsedPreviousDefinition)
78
+ return _this._decrementCounts(parsedPreviousDefinition);
79
+ });
80
+ }).then(function () { return _this._updateSets(name, parsedPreviousDefinition && parsedPreviousDefinition.sets, definition.sets); });
81
+ }).then(function () { return true; });
82
+ };
83
+ /**
84
+ * Remove a given definition.
85
+ * The returned promise is resolved when the operation success, with a boolean indicating if the definition existed or not.
86
+ * or rejected if it fails (e.g., wrapper operation fails).
87
+ */
88
+ DefinitionsCachePluggable.prototype.remove = function (name) {
89
+ var _this = this;
90
+ return this.get(name).then(function (definition) {
91
+ if (definition) {
92
+ return _this._decrementCounts(definition).then(function () { return _this._updateSets(name, definition.sets); });
93
+ }
94
+ }).then(function () {
95
+ return _this.wrapper.del(_this.keys.buildDefinitionKey(name));
96
+ });
97
+ };
98
+ /**
99
+ * Get definition.
100
+ * The returned promise is resolved with the definition or null if it's not defined,
101
+ * or rejected if wrapper operation fails.
102
+ */
103
+ DefinitionsCachePluggable.prototype.get = function (name) {
104
+ return this.wrapper.get(this.keys.buildDefinitionKey(name))
105
+ .then(function (maybeDefinition) { return maybeDefinition && JSON.parse(maybeDefinition); });
106
+ };
107
+ /**
108
+ * Get list of definitions.
109
+ * The returned promise is resolved with a map of names to their definition or null if it's not defined,
110
+ * or rejected if wrapper operation fails.
111
+ */
112
+ DefinitionsCachePluggable.prototype.getMany = function (names) {
113
+ var _this = this;
114
+ var keys = names.map(function (name) { return _this.keys.buildDefinitionKey(name); });
115
+ return this.wrapper.getMany(keys).then(function (stringifiedDefinitions) {
116
+ var definitions = {};
117
+ names.forEach(function (name, idx) {
118
+ var definition = stringifiedDefinitions[idx];
119
+ definitions[name] = definition && JSON.parse(definition);
120
+ });
121
+ return Promise.resolve(definitions);
122
+ });
123
+ };
124
+ /**
125
+ * Get list of all definitions.
126
+ * The returned promise is resolved with the list of definitions,
127
+ * or rejected if wrapper operation fails.
128
+ */
129
+ DefinitionsCachePluggable.prototype.getAll = function () {
130
+ var _this = this;
131
+ return this.wrapper.getKeysByPrefix(this.keys.buildDefinitionKeyPrefix())
132
+ .then(function (listOfKeys) { return _this.wrapper.getMany(listOfKeys); })
133
+ .then(function (definitions) { return definitions.map(function (definition) {
134
+ return JSON.parse(definition);
135
+ }); });
136
+ };
137
+ /**
138
+ * Get list of definition names.
139
+ * The returned promise is resolved with the list of names,
140
+ * or rejected if wrapper operation fails.
141
+ */
142
+ DefinitionsCachePluggable.prototype.getNames = function () {
143
+ var _this = this;
144
+ return this.wrapper.getKeysByPrefix(this.keys.buildDefinitionKeyPrefix()).then(function (listOfKeys) { return listOfKeys.map(_this.keys.extractKey); });
145
+ };
146
+ /**
147
+ * Get list of definition names related to a given list of set names.
148
+ * The returned promise is resolved with the list of names per set.
149
+ * It never rejects (If there is a wrapper error for some set, an empty set is returned for it).
150
+ */
151
+ DefinitionsCachePluggable.prototype.getNamesBySets = function (sets) {
152
+ var _this = this;
153
+ return Promise.all(sets.map(function (set) {
154
+ var setKey = _this.keys.buildSetKey(set);
155
+ return _this.wrapper.getItems(setKey).catch(function () { return []; });
156
+ })).then(function (namesBySets) { return namesBySets.map(function (namesBySet) { return new Set(namesBySet); }); });
157
+ };
158
+ /**
159
+ * Check traffic type existence.
160
+ * The returned promise is resolved with a boolean indicating whether the TT exist or not.
161
+ * In case of wrapper operation failures, the promise resolves with a true value, assuming that the TT might exist.
162
+ * It will never be rejected.
163
+ */
164
+ DefinitionsCachePluggable.prototype.trafficTypeExists = function (trafficType) {
165
+ var _this = this;
166
+ // If there is a number there should be > 0, otherwise the TT is considered as not existent.
167
+ return this.wrapper.get(this.keys.buildTrafficTypeKey(trafficType))
168
+ .then(function (ttCount) {
169
+ if (ttCount === null)
170
+ return false; // if entry doesn't exist, means that TT doesn't exist
171
+ ttCount = parseInt(ttCount, 10);
172
+ if (!(0, lang_1.isFiniteNumber)(ttCount) || ttCount < 0) {
173
+ _this.log.info(constants_1.LOG_PREFIX + "Could not validate traffic type existence of ".concat(trafficType, " due to data corruption of some sorts."));
174
+ return false;
175
+ }
176
+ return ttCount > 0;
177
+ }).catch(function (e) {
178
+ _this.log.error(constants_1.LOG_PREFIX + "Could not validate traffic type existence of ".concat(trafficType, " due to an error: ").concat(e, "."));
179
+ // If there is an error, bypass the validation so the event can get tracked.
180
+ return true;
181
+ });
182
+ };
183
+ /**
184
+ * Set till number.
185
+ * The returned promise is resolved when the operation success,
186
+ * or rejected if it fails (e.g., wrapper operation fails).
187
+ */
188
+ DefinitionsCachePluggable.prototype.setChangeNumber = function (changeNumber) {
189
+ if (changeNumber !== undefined) {
190
+ return this.wrapper.set(this.keys.buildDefinitionsTillKey(), changeNumber + '');
191
+ }
192
+ };
193
+ /**
194
+ * Get till number or -1 if it's not defined.
195
+ * The returned promise is resolved with the changeNumber or -1 if it doesn't exist or a wrapper operation fails.
196
+ * The promise will never be rejected.
197
+ */
198
+ DefinitionsCachePluggable.prototype.getChangeNumber = function () {
199
+ var _this = this;
200
+ return this.wrapper.get(this.keys.buildDefinitionsTillKey()).then(function (value) {
201
+ var i = parseInt(value, 10);
202
+ return (0, lang_1.isNaNNumber)(i) ? -1 : i;
203
+ }).catch(function (e) {
204
+ _this.log.error(constants_1.LOG_PREFIX + 'Could not retrieve changeNumber from storage. Error: ' + e);
205
+ return -1;
206
+ });
207
+ };
208
+ // @TODO implement if required by DataLoader or producer mode
209
+ DefinitionsCachePluggable.prototype.clear = function () {
210
+ return Promise.resolve(true);
211
+ };
212
+ return DefinitionsCachePluggable;
213
+ }(AbstractDefinitionsCacheAsync_1.AbstractDefinitionsCacheAsync));
214
+ exports.DefinitionsCachePluggable = DefinitionsCachePluggable;