@splitsoftware/splitio-commons 2.12.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 (395) hide show
  1. package/CHANGES.txt +19 -1
  2. package/cjs/evaluator/condition/engineUtils.js +2 -2
  3. package/cjs/evaluator/fallbackConfigsCalculator/fallbackSanitizer/index.js +70 -0
  4. package/cjs/evaluator/fallbackConfigsCalculator/index.js +25 -0
  5. package/cjs/evaluator/fallbackTreatmentsCalculator/fallbackSanitizer/index.js +5 -8
  6. package/cjs/evaluator/fallbackTreatmentsCalculator/index.js +21 -32
  7. package/cjs/evaluator/index.js +73 -48
  8. package/cjs/evaluator/matchers/whitelist.js +1 -1
  9. package/cjs/integrations/pluggable.js +1 -1
  10. package/cjs/listeners/browser.js +4 -3
  11. package/cjs/listeners/node.js +4 -4
  12. package/cjs/logger/constants.js +15 -21
  13. package/cjs/logger/index.js +1 -1
  14. package/cjs/logger/messages/debug.js +6 -7
  15. package/cjs/logger/messages/error.js +4 -4
  16. package/cjs/logger/messages/info.js +2 -4
  17. package/cjs/logger/messages/warn.js +9 -10
  18. package/cjs/platform/browser.js +15 -0
  19. package/cjs/platform/getEventSource/browser.js +8 -0
  20. package/cjs/platform/getEventSource/eventsource.js +480 -0
  21. package/cjs/platform/getEventSource/node.js +26 -0
  22. package/cjs/platform/getFetch/browser.js +9 -0
  23. package/cjs/platform/getFetch/node.js +27 -0
  24. package/cjs/platform/getOptions/node.js +25 -0
  25. package/cjs/platform/node.js +17 -0
  26. package/cjs/readiness/constants.js +5 -4
  27. package/cjs/readiness/readinessManager.js +22 -22
  28. package/cjs/readiness/sdkReadinessManager.js +0 -16
  29. package/cjs/sdkClient/client.js +9 -35
  30. package/cjs/sdkClient/clientAttributesDecoration.js +3 -3
  31. package/cjs/sdkClient/clientInputValidation.js +38 -81
  32. package/cjs/sdkClient/identity.js +1 -1
  33. package/cjs/sdkClient/sdkClient.js +4 -49
  34. package/cjs/sdkClient/sdkLifecycle.js +71 -0
  35. package/cjs/sdkClient/trackMethod.js +56 -0
  36. package/cjs/sdkFactory/index.js +13 -50
  37. package/cjs/sdkManager/index.js +5 -5
  38. package/cjs/services/authProvider.js +67 -0
  39. package/cjs/services/secureSplitHttpClient.js +39 -0
  40. package/cjs/services/splitApi.js +24 -14
  41. package/cjs/services/splitHttpClient.js +1 -1
  42. package/cjs/storages/AbstractDefinitionsCacheAsync.js +51 -0
  43. package/cjs/storages/AbstractDefinitionsCacheSync.js +76 -0
  44. package/cjs/storages/KeyBuilder.js +16 -16
  45. package/cjs/storages/KeyBuilderCS.js +17 -17
  46. package/cjs/storages/KeyBuilderSS.js +16 -16
  47. package/cjs/storages/getRolloutPlan.js +4 -4
  48. package/cjs/storages/inLocalStorage/DefinitionsCacheInLocal.js +194 -0
  49. package/cjs/storages/inLocalStorage/MySegmentsCacheInLocal.js +1 -1
  50. package/cjs/storages/inLocalStorage/RBSegmentsCacheInLocal.js +10 -10
  51. package/cjs/storages/inLocalStorage/index.js +6 -6
  52. package/cjs/storages/inLocalStorage/validateCache.js +3 -3
  53. package/cjs/storages/inMemory/DefinitionsCacheInMemory.js +121 -0
  54. package/cjs/storages/inMemory/ImpressionCountsCacheInMemory.js +1 -1
  55. package/cjs/storages/inMemory/InMemoryStorage.js +4 -4
  56. package/cjs/storages/inMemory/InMemoryStorageCS.js +5 -5
  57. package/cjs/storages/inMemory/MySegmentsCacheInMemory.js +1 -1
  58. package/cjs/storages/inMemory/RBSegmentsCacheInMemory.js +5 -5
  59. package/cjs/storages/inMemory/SegmentsCacheInMemory.js +1 -1
  60. package/cjs/storages/inMemory/TelemetryCacheInMemory.js +5 -5
  61. package/cjs/storages/inRedis/DefinitionsCacheInRedis.js +243 -0
  62. package/cjs/storages/inRedis/EventsCacheInRedis.js +1 -1
  63. package/cjs/storages/inRedis/ImpressionCountsCacheInRedis.js +5 -5
  64. package/cjs/storages/inRedis/ImpressionsCacheInRedis.js +1 -1
  65. package/cjs/storages/inRedis/RBSegmentsCacheInRedis.js +1 -1
  66. package/cjs/storages/inRedis/RedisAdapter.js +8 -8
  67. package/cjs/storages/inRedis/TelemetryCacheInRedis.js +7 -7
  68. package/cjs/storages/inRedis/UniqueKeysCacheInRedis.js +3 -3
  69. package/cjs/storages/inRedis/index.js +2 -2
  70. package/cjs/storages/pluggable/DefinitionsCachePluggable.js +212 -0
  71. package/cjs/storages/pluggable/EventsCachePluggable.js +1 -1
  72. package/cjs/storages/pluggable/ImpressionCountsCachePluggable.js +6 -6
  73. package/cjs/storages/pluggable/RBSegmentsCachePluggable.js +1 -1
  74. package/cjs/storages/pluggable/TelemetryCachePluggable.js +7 -7
  75. package/cjs/storages/pluggable/UniqueKeysCachePluggable.js +2 -2
  76. package/cjs/storages/pluggable/index.js +5 -5
  77. package/cjs/storages/pluggable/wrapperAdapter.js +1 -1
  78. package/cjs/storages/setRolloutPlan.js +6 -6
  79. package/cjs/storages/utils.js +6 -6
  80. package/cjs/sync/offline/splitsParser/parseCondition.js +1 -1
  81. package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +5 -5
  82. package/cjs/sync/polling/fetchers/segmentChangesFetcher.js +1 -1
  83. package/cjs/sync/polling/fetchers/splitChangesFetcher.js +24 -15
  84. package/cjs/sync/polling/pollingManagerCS.js +18 -18
  85. package/cjs/sync/polling/pollingManagerSS.js +13 -13
  86. package/cjs/sync/polling/syncTasks/definitionsSyncTask.js +12 -0
  87. package/cjs/sync/polling/updaters/{splitChangesUpdater.js → definitionChangesUpdater.js} +72 -64
  88. package/cjs/sync/polling/updaters/mySegmentsUpdater.js +3 -3
  89. package/cjs/sync/polling/updaters/segmentChangesUpdater.js +6 -6
  90. package/cjs/sync/streaming/SSEClient/index.js +2 -2
  91. package/cjs/sync/streaming/SSEHandler/NotificationKeeper.js +1 -1
  92. package/cjs/sync/streaming/SSEHandler/index.js +1 -1
  93. package/cjs/sync/streaming/UpdateWorkers/{SplitsUpdateWorker.js → DefinitionsUpdateWorker.js} +18 -18
  94. package/cjs/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.js +2 -2
  95. package/cjs/sync/streaming/UpdateWorkers/SegmentsUpdateWorker.js +2 -2
  96. package/cjs/sync/streaming/constants.js +3 -3
  97. package/cjs/sync/streaming/parseUtils.js +12 -12
  98. package/cjs/sync/streaming/pushManager.js +14 -14
  99. package/cjs/sync/submitters/impressionsSubmitter.js +6 -6
  100. package/cjs/sync/submitters/submitter.js +1 -1
  101. package/cjs/sync/submitters/telemetrySubmitter.js +1 -2
  102. package/cjs/sync/syncManagerOnline.js +9 -6
  103. package/cjs/trackers/eventTracker.js +10 -12
  104. package/cjs/trackers/impressionObserver/buildKey.js +1 -1
  105. package/cjs/trackers/impressionsTracker.js +37 -20
  106. package/cjs/trackers/uniqueKeysTracker.js +1 -1
  107. package/cjs/utils/Backoff.js +20 -0
  108. package/cjs/utils/EventEmitter.js +391 -0
  109. package/cjs/utils/filter/bloomFilter.js +37 -0
  110. package/cjs/utils/inputValidation/apiKey.js +1 -1
  111. package/cjs/utils/inputValidation/attribute.js +2 -2
  112. package/cjs/utils/inputValidation/{split.js → definition.js} +10 -10
  113. package/cjs/utils/inputValidation/{splitExistence.js → definitionExistence.js} +6 -6
  114. package/cjs/utils/inputValidation/definitions.js +25 -0
  115. package/cjs/utils/inputValidation/eventProperties.js +1 -1
  116. package/cjs/utils/inputValidation/index.js +7 -7
  117. package/cjs/utils/inputValidation/isOperational.js +4 -4
  118. package/cjs/utils/inputValidation/target.js +22 -0
  119. package/cjs/utils/labels/index.js +3 -3
  120. package/cjs/utils/lang/index.js +1 -1
  121. package/cjs/utils/lang/sets.js +4 -4
  122. package/cjs/utils/promise/timeout.js +1 -1
  123. package/cjs/utils/settingsValidation/splitFilters.js +4 -4
  124. package/cjs/utils/settingsValidation/url.js +6 -6
  125. package/esm/evaluator/condition/engineUtils.js +2 -2
  126. package/esm/evaluator/fallbackConfigsCalculator/fallbackSanitizer/index.js +64 -0
  127. package/esm/evaluator/fallbackConfigsCalculator/index.js +21 -0
  128. package/esm/evaluator/fallbackTreatmentsCalculator/fallbackSanitizer/index.js +5 -8
  129. package/esm/evaluator/fallbackTreatmentsCalculator/index.js +21 -33
  130. package/esm/evaluator/index.js +72 -48
  131. package/esm/evaluator/matchers/whitelist.js +1 -1
  132. package/esm/integrations/pluggable.js +1 -1
  133. package/esm/listeners/browser.js +4 -3
  134. package/esm/listeners/node.js +4 -4
  135. package/esm/logger/constants.js +12 -18
  136. package/esm/logger/index.js +1 -1
  137. package/esm/logger/messages/debug.js +5 -6
  138. package/esm/logger/messages/error.js +3 -3
  139. package/esm/logger/messages/info.js +1 -3
  140. package/esm/logger/messages/warn.js +8 -9
  141. package/esm/platform/browser.js +12 -0
  142. package/esm/platform/getEventSource/browser.js +4 -0
  143. package/esm/platform/getEventSource/eventsource.js +480 -0
  144. package/esm/platform/getEventSource/node.js +20 -0
  145. package/esm/platform/getFetch/browser.js +5 -0
  146. package/esm/platform/getFetch/node.js +22 -0
  147. package/esm/platform/getOptions/node.js +20 -0
  148. package/esm/platform/node.js +14 -0
  149. package/esm/readiness/constants.js +4 -3
  150. package/esm/readiness/readinessManager.js +23 -23
  151. package/esm/readiness/sdkReadinessManager.js +1 -17
  152. package/esm/sdkClient/client.js +10 -37
  153. package/esm/sdkClient/clientAttributesDecoration.js +3 -3
  154. package/esm/sdkClient/clientInputValidation.js +40 -83
  155. package/esm/sdkClient/identity.js +1 -1
  156. package/esm/sdkClient/sdkClient.js +4 -49
  157. package/esm/sdkClient/sdkLifecycle.js +67 -0
  158. package/esm/sdkClient/trackMethod.js +52 -0
  159. package/esm/sdkFactory/index.js +14 -51
  160. package/esm/sdkManager/index.js +6 -6
  161. package/esm/services/authProvider.js +63 -0
  162. package/esm/services/secureSplitHttpClient.js +35 -0
  163. package/esm/services/splitApi.js +24 -14
  164. package/esm/services/splitHttpClient.js +1 -1
  165. package/esm/storages/AbstractDefinitionsCacheAsync.js +48 -0
  166. package/esm/storages/AbstractDefinitionsCacheSync.js +71 -0
  167. package/esm/storages/KeyBuilder.js +16 -16
  168. package/esm/storages/KeyBuilderCS.js +16 -16
  169. package/esm/storages/KeyBuilderSS.js +15 -15
  170. package/esm/storages/getRolloutPlan.js +4 -4
  171. package/esm/storages/inLocalStorage/DefinitionsCacheInLocal.js +191 -0
  172. package/esm/storages/inLocalStorage/RBSegmentsCacheInLocal.js +7 -7
  173. package/esm/storages/inLocalStorage/index.js +6 -6
  174. package/esm/storages/inLocalStorage/validateCache.js +3 -3
  175. package/esm/storages/inMemory/DefinitionsCacheInMemory.js +118 -0
  176. package/esm/storages/inMemory/ImpressionCountsCacheInMemory.js +1 -1
  177. package/esm/storages/inMemory/InMemoryStorage.js +4 -4
  178. package/esm/storages/inMemory/InMemoryStorageCS.js +5 -5
  179. package/esm/storages/inMemory/RBSegmentsCacheInMemory.js +2 -2
  180. package/esm/storages/inMemory/SegmentsCacheInMemory.js +1 -1
  181. package/esm/storages/inMemory/TelemetryCacheInMemory.js +5 -5
  182. package/esm/storages/inRedis/DefinitionsCacheInRedis.js +240 -0
  183. package/esm/storages/inRedis/EventsCacheInRedis.js +1 -1
  184. package/esm/storages/inRedis/ImpressionCountsCacheInRedis.js +4 -4
  185. package/esm/storages/inRedis/RBSegmentsCacheInRedis.js +1 -1
  186. package/esm/storages/inRedis/RedisAdapter.js +7 -7
  187. package/esm/storages/inRedis/TelemetryCacheInRedis.js +7 -7
  188. package/esm/storages/inRedis/UniqueKeysCacheInRedis.js +1 -1
  189. package/esm/storages/inRedis/index.js +2 -2
  190. package/esm/storages/pluggable/DefinitionsCachePluggable.js +209 -0
  191. package/esm/storages/pluggable/EventsCachePluggable.js +1 -1
  192. package/esm/storages/pluggable/ImpressionCountsCachePluggable.js +5 -5
  193. package/esm/storages/pluggable/RBSegmentsCachePluggable.js +1 -1
  194. package/esm/storages/pluggable/TelemetryCachePluggable.js +7 -7
  195. package/esm/storages/pluggable/UniqueKeysCachePluggable.js +1 -1
  196. package/esm/storages/pluggable/index.js +4 -4
  197. package/esm/storages/pluggable/wrapperAdapter.js +1 -1
  198. package/esm/storages/setRolloutPlan.js +6 -6
  199. package/esm/storages/utils.js +6 -6
  200. package/esm/sync/offline/splitsParser/parseCondition.js +1 -1
  201. package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +6 -6
  202. package/esm/sync/polling/fetchers/splitChangesFetcher.js +25 -16
  203. package/esm/sync/polling/pollingManagerCS.js +15 -15
  204. package/esm/sync/polling/pollingManagerSS.js +13 -13
  205. package/esm/sync/polling/syncTasks/definitionsSyncTask.js +8 -0
  206. package/esm/sync/polling/updaters/definitionChangesUpdater.js +200 -0
  207. package/esm/sync/polling/updaters/mySegmentsUpdater.js +3 -3
  208. package/esm/sync/polling/updaters/segmentChangesUpdater.js +7 -7
  209. package/esm/sync/streaming/SSEClient/index.js +2 -2
  210. package/esm/sync/streaming/SSEHandler/NotificationKeeper.js +2 -2
  211. package/esm/sync/streaming/SSEHandler/index.js +2 -2
  212. package/esm/sync/streaming/UpdateWorkers/{SplitsUpdateWorker.js → DefinitionsUpdateWorker.js} +17 -17
  213. package/esm/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.js +2 -2
  214. package/esm/sync/streaming/UpdateWorkers/SegmentsUpdateWorker.js +2 -2
  215. package/esm/sync/streaming/constants.js +2 -2
  216. package/esm/sync/streaming/parseUtils.js +10 -10
  217. package/esm/sync/streaming/pushManager.js +16 -16
  218. package/esm/sync/submitters/impressionsSubmitter.js +6 -6
  219. package/esm/sync/submitters/submitter.js +1 -1
  220. package/esm/sync/submitters/telemetrySubmitter.js +1 -2
  221. package/esm/sync/syncManagerOnline.js +8 -5
  222. package/esm/trackers/eventTracker.js +10 -12
  223. package/esm/trackers/impressionObserver/buildKey.js +1 -1
  224. package/esm/trackers/impressionsTracker.js +38 -21
  225. package/esm/trackers/uniqueKeysTracker.js +1 -1
  226. package/esm/utils/Backoff.js +20 -0
  227. package/esm/utils/EventEmitter.js +387 -0
  228. package/esm/utils/filter/bloomFilter.js +33 -0
  229. package/esm/utils/inputValidation/apiKey.js +1 -1
  230. package/esm/utils/inputValidation/attribute.js +2 -2
  231. package/esm/utils/inputValidation/{split.js → definition.js} +8 -8
  232. package/esm/utils/inputValidation/definitionExistence.js +15 -0
  233. package/esm/utils/inputValidation/definitions.js +21 -0
  234. package/esm/utils/inputValidation/index.js +3 -3
  235. package/esm/utils/inputValidation/isOperational.js +4 -4
  236. package/esm/utils/inputValidation/target.js +18 -0
  237. package/esm/utils/labels/index.js +2 -2
  238. package/esm/utils/lang/sets.js +4 -4
  239. package/esm/utils/promise/timeout.js +1 -1
  240. package/esm/utils/settingsValidation/splitFilters.js +4 -4
  241. package/esm/utils/settingsValidation/url.js +6 -6
  242. package/package.json +15 -6
  243. package/src/dtos/types.ts +65 -70
  244. package/src/evaluator/Engine.ts +4 -4
  245. package/src/evaluator/combiners/and.ts +3 -3
  246. package/src/evaluator/combiners/ifelseif.ts +2 -2
  247. package/src/evaluator/condition/engineUtils.ts +2 -2
  248. package/src/evaluator/condition/index.ts +3 -3
  249. package/src/evaluator/fallbackConfigsCalculator/fallbackSanitizer/index.ts +76 -0
  250. package/src/evaluator/fallbackConfigsCalculator/index.ts +24 -0
  251. package/src/evaluator/fallbackTreatmentsCalculator/fallbackSanitizer/index.ts +5 -7
  252. package/src/evaluator/fallbackTreatmentsCalculator/index.ts +27 -41
  253. package/src/evaluator/index.ts +86 -51
  254. package/src/evaluator/matchers/dependency.ts +2 -2
  255. package/src/evaluator/matchers/matcherTypes.ts +2 -2
  256. package/src/evaluator/matchers/prerequisites.ts +4 -4
  257. package/src/evaluator/matchers/rbsegment.ts +2 -2
  258. package/src/evaluator/matchers/whitelist.ts +2 -2
  259. package/src/evaluator/matchersTransform/index.ts +2 -2
  260. package/src/evaluator/matchersTransform/whitelist.ts +2 -2
  261. package/src/evaluator/parser/index.ts +4 -4
  262. package/src/evaluator/treatments/index.ts +2 -2
  263. package/src/evaluator/types.ts +6 -6
  264. package/src/evaluator/value/index.ts +1 -1
  265. package/src/integrations/pluggable.ts +1 -1
  266. package/src/listeners/browser.ts +11 -8
  267. package/src/listeners/node.ts +2 -5
  268. package/src/logger/constants.ts +12 -18
  269. package/src/logger/messages/debug.ts +5 -6
  270. package/src/logger/messages/error.ts +3 -3
  271. package/src/logger/messages/info.ts +1 -3
  272. package/src/logger/messages/warn.ts +8 -9
  273. package/src/platform/browser.ts +14 -0
  274. package/src/platform/getEventSource/browser.ts +4 -0
  275. package/src/platform/getEventSource/eventsource.js +519 -0
  276. package/src/platform/getEventSource/node.ts +23 -0
  277. package/src/platform/getFetch/browser.ts +5 -0
  278. package/src/platform/getFetch/node.ts +27 -0
  279. package/src/platform/getOptions/node.ts +24 -0
  280. package/src/platform/node.ts +16 -0
  281. package/src/readiness/constants.ts +4 -3
  282. package/src/readiness/readinessManager.ts +22 -22
  283. package/src/readiness/sdkReadinessManager.ts +1 -16
  284. package/src/readiness/types.ts +10 -16
  285. package/src/sdkClient/client.ts +11 -41
  286. package/src/sdkClient/clientInputValidation.ts +44 -84
  287. package/src/sdkClient/sdkClient.ts +5 -54
  288. package/src/sdkClient/sdkLifecycle.ts +79 -0
  289. package/src/sdkClient/trackMethod.ts +59 -0
  290. package/src/sdkFactory/index.ts +15 -59
  291. package/src/sdkFactory/types.ts +19 -16
  292. package/src/sdkManager/index.ts +15 -15
  293. package/src/services/authProvider.ts +86 -0
  294. package/src/services/secureSplitHttpClient.ts +44 -0
  295. package/src/services/splitApi.ts +14 -2
  296. package/src/services/types.ts +7 -2
  297. package/src/storages/AbstractDefinitionsCacheAsync.ts +63 -0
  298. package/src/storages/AbstractDefinitionsCacheSync.ts +95 -0
  299. package/src/storages/KeyBuilder.ts +6 -6
  300. package/src/storages/KeyBuilderCS.ts +6 -6
  301. package/src/storages/KeyBuilderSS.ts +2 -2
  302. package/src/storages/getRolloutPlan.ts +3 -3
  303. package/src/storages/inLocalStorage/DefinitionsCacheInLocal.ts +234 -0
  304. package/src/storages/inLocalStorage/RBSegmentsCacheInLocal.ts +8 -8
  305. package/src/storages/inLocalStorage/index.ts +6 -6
  306. package/src/storages/inLocalStorage/validateCache.ts +4 -4
  307. package/src/storages/inMemory/DefinitionsCacheInMemory.ts +130 -0
  308. package/src/storages/inMemory/InMemoryStorage.ts +4 -4
  309. package/src/storages/inMemory/InMemoryStorageCS.ts +5 -5
  310. package/src/storages/inMemory/RBSegmentsCacheInMemory.ts +3 -3
  311. package/src/storages/inMemory/SegmentsCacheInMemory.ts +1 -1
  312. package/src/storages/inMemory/TelemetryCacheInMemory.ts +5 -5
  313. package/src/storages/inRedis/{SplitsCacheInRedis.ts → DefinitionsCacheInRedis.ts} +69 -69
  314. package/src/storages/inRedis/RBSegmentsCacheInRedis.ts +2 -2
  315. package/src/storages/inRedis/index.ts +2 -2
  316. package/src/storages/pluggable/DefinitionsCachePluggable.ts +230 -0
  317. package/src/storages/pluggable/RBSegmentsCachePluggable.ts +2 -2
  318. package/src/storages/pluggable/index.ts +2 -2
  319. package/src/storages/setRolloutPlan.ts +7 -7
  320. package/src/storages/types.ts +34 -29
  321. package/src/sync/offline/splitsParser/parseCondition.ts +2 -2
  322. package/src/sync/offline/splitsParser/splitsParserFromSettings.ts +3 -3
  323. package/src/sync/offline/splitsParser/types.ts +6 -3
  324. package/src/sync/offline/syncManagerOffline.ts +2 -2
  325. package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +14 -14
  326. package/src/sync/polling/fetchers/splitChangesFetcher.ts +58 -23
  327. package/src/sync/polling/fetchers/types.ts +5 -3
  328. package/src/sync/polling/pollingManagerCS.ts +16 -14
  329. package/src/sync/polling/pollingManagerSS.ts +15 -13
  330. package/src/sync/polling/syncTasks/{splitsSyncTask.ts → definitionsSyncTask.ts} +11 -12
  331. package/src/sync/polling/types.ts +3 -3
  332. package/src/sync/polling/updaters/definitionChangesUpdater.ts +239 -0
  333. package/src/sync/polling/updaters/mySegmentsUpdater.ts +3 -3
  334. package/src/sync/polling/updaters/segmentChangesUpdater.ts +7 -7
  335. package/src/sync/streaming/AuthClient/index.ts +2 -2
  336. package/src/sync/streaming/AuthClient/types.ts +14 -11
  337. package/src/sync/streaming/SSEClient/index.ts +2 -2
  338. package/src/sync/streaming/SSEClient/types.ts +2 -2
  339. package/src/sync/streaming/SSEHandler/NotificationKeeper.ts +2 -2
  340. package/src/sync/streaming/SSEHandler/index.ts +2 -2
  341. package/src/sync/streaming/UpdateWorkers/{SplitsUpdateWorker.ts → DefinitionsUpdateWorker.ts} +21 -21
  342. package/src/sync/streaming/constants.ts +2 -2
  343. package/src/sync/streaming/parseUtils.ts +12 -12
  344. package/src/sync/streaming/pushManager.ts +18 -18
  345. package/src/sync/streaming/types.ts +2 -2
  346. package/src/sync/submitters/impressionsSubmitter.ts +9 -9
  347. package/src/sync/submitters/telemetrySubmitter.ts +1 -2
  348. package/src/sync/submitters/types.ts +3 -0
  349. package/src/sync/syncManagerOnline.ts +9 -5
  350. package/src/trackers/eventTracker.ts +7 -10
  351. package/src/trackers/impressionsTracker.ts +42 -26
  352. package/src/trackers/types.ts +3 -1
  353. package/src/utils/Backoff.ts +21 -0
  354. package/src/utils/EventEmitter.js +444 -0
  355. package/src/utils/filter/bloomFilter.ts +38 -0
  356. package/src/utils/inputValidation/definition.ts +27 -0
  357. package/src/utils/inputValidation/definitionExistence.ts +19 -0
  358. package/src/utils/inputValidation/definitions.ts +21 -0
  359. package/src/utils/inputValidation/index.ts +3 -3
  360. package/src/utils/inputValidation/isOperational.ts +4 -4
  361. package/src/utils/inputValidation/target.ts +21 -0
  362. package/src/utils/inputValidation/trafficTypeExistence.ts +2 -2
  363. package/src/utils/jwt/types.ts +1 -1
  364. package/src/utils/labels/index.ts +2 -2
  365. package/src/utils/lang/sets.ts +4 -2
  366. package/src/utils/settingsValidation/splitFilters.ts +3 -3
  367. package/src/utils/settingsValidation/url.ts +1 -1
  368. package/types/splitio.d.ts +222 -11
  369. package/cjs/storages/AbstractSplitsCacheAsync.js +0 -51
  370. package/cjs/storages/AbstractSplitsCacheSync.js +0 -76
  371. package/cjs/storages/inLocalStorage/SplitsCacheInLocal.js +0 -194
  372. package/cjs/storages/inMemory/SplitsCacheInMemory.js +0 -121
  373. package/cjs/storages/inRedis/SplitsCacheInRedis.js +0 -243
  374. package/cjs/storages/pluggable/SplitsCachePluggable.js +0 -212
  375. package/cjs/sync/polling/syncTasks/splitsSyncTask.js +0 -13
  376. package/cjs/utils/inputValidation/splits.js +0 -25
  377. package/esm/storages/AbstractSplitsCacheAsync.js +0 -48
  378. package/esm/storages/AbstractSplitsCacheSync.js +0 -71
  379. package/esm/storages/inLocalStorage/SplitsCacheInLocal.js +0 -191
  380. package/esm/storages/inMemory/SplitsCacheInMemory.js +0 -118
  381. package/esm/storages/inRedis/SplitsCacheInRedis.js +0 -240
  382. package/esm/storages/pluggable/SplitsCachePluggable.js +0 -209
  383. package/esm/sync/polling/syncTasks/splitsSyncTask.js +0 -9
  384. package/esm/sync/polling/updaters/splitChangesUpdater.js +0 -192
  385. package/esm/utils/inputValidation/splitExistence.js +0 -15
  386. package/esm/utils/inputValidation/splits.js +0 -21
  387. package/src/storages/AbstractSplitsCacheAsync.ts +0 -63
  388. package/src/storages/AbstractSplitsCacheSync.ts +0 -95
  389. package/src/storages/inLocalStorage/SplitsCacheInLocal.ts +0 -234
  390. package/src/storages/inMemory/SplitsCacheInMemory.ts +0 -130
  391. package/src/storages/pluggable/SplitsCachePluggable.ts +0 -230
  392. package/src/sync/polling/updaters/splitChangesUpdater.ts +0 -230
  393. package/src/utils/inputValidation/split.ts +0 -27
  394. package/src/utils/inputValidation/splitExistence.ts +0 -19
  395. package/src/utils/inputValidation/splits.ts +0 -21
@@ -1,130 +0,0 @@
1
- import { ISplit, ISplitFiltersValidation } from '../../dtos/types';
2
- import { AbstractSplitsCacheSync, usesSegments } from '../AbstractSplitsCacheSync';
3
- import { isFiniteNumber } from '../../utils/lang';
4
-
5
- /**
6
- * Default ISplitsCacheSync implementation that stores split definitions in memory.
7
- */
8
- export class SplitsCacheInMemory extends AbstractSplitsCacheSync {
9
-
10
- private flagSetsFilter: string[];
11
- private splitsCache: Record<string, ISplit> = {};
12
- private ttCache: Record<string, number> = {};
13
- private changeNumber: number = -1;
14
- private segmentsCount: number = 0;
15
- private flagSetsCache: Record<string, Set<string>> = {};
16
-
17
- constructor(splitFiltersValidation?: ISplitFiltersValidation) {
18
- super();
19
- this.flagSetsFilter = splitFiltersValidation ? splitFiltersValidation.groupedFilters.bySet : [];
20
- }
21
-
22
- clear() {
23
- this.splitsCache = {};
24
- this.ttCache = {};
25
- this.changeNumber = -1;
26
- this.segmentsCount = 0;
27
- this.flagSetsCache = {};
28
- }
29
-
30
- addSplit(split: ISplit): boolean {
31
- const name = split.name;
32
- const previousSplit = this.getSplit(name);
33
- if (previousSplit) { // We had this Split already
34
-
35
- const previousTtName = previousSplit.trafficTypeName;
36
- this.ttCache[previousTtName]--;
37
- if (!this.ttCache[previousTtName]) delete this.ttCache[previousTtName];
38
-
39
- this.removeFromFlagSets(previousSplit.name, previousSplit.sets);
40
-
41
- // Subtract from segments count for the previous version of this Split
42
- if (usesSegments(previousSplit)) this.segmentsCount--;
43
- }
44
-
45
- // Store the Split.
46
- this.splitsCache[name] = split;
47
- // Update TT cache
48
- const ttName = split.trafficTypeName;
49
- this.ttCache[ttName] = (this.ttCache[ttName] || 0) + 1;
50
- this.addToFlagSets(split);
51
-
52
- // Add to segments count for the new version of the Split
53
- if (usesSegments(split)) this.segmentsCount++;
54
-
55
- return true;
56
- }
57
-
58
- removeSplit(name: string): boolean {
59
- const split = this.getSplit(name);
60
- if (!split) return false;
61
-
62
- // Delete the Split
63
- delete this.splitsCache[name];
64
-
65
- const ttName = split.trafficTypeName;
66
- this.ttCache[ttName]--; // Update tt cache
67
- if (!this.ttCache[ttName]) delete this.ttCache[ttName];
68
- this.removeFromFlagSets(split.name, split.sets);
69
-
70
- // Update the segments count.
71
- if (usesSegments(split)) this.segmentsCount--;
72
-
73
- return true;
74
- }
75
-
76
- getSplit(name: string): ISplit | null {
77
- return this.splitsCache[name] || null;
78
- }
79
-
80
- setChangeNumber(changeNumber: number): boolean {
81
- this.changeNumber = changeNumber;
82
- return true;
83
- }
84
-
85
- getChangeNumber(): number {
86
- return this.changeNumber;
87
- }
88
-
89
- getSplitNames(): string[] {
90
- return Object.keys(this.splitsCache);
91
- }
92
-
93
- trafficTypeExists(trafficType: string): boolean {
94
- return isFiniteNumber(this.ttCache[trafficType]) && this.ttCache[trafficType] > 0;
95
- }
96
-
97
- usesSegments(): boolean {
98
- return this.getChangeNumber() === -1 || this.segmentsCount > 0;
99
- }
100
-
101
- getNamesByFlagSets(flagSets: string[]): Set<string>[] {
102
- return flagSets.map(flagSet => this.flagSetsCache[flagSet] || new Set());
103
- }
104
-
105
- private addToFlagSets(featureFlag: ISplit) {
106
- if (!featureFlag.sets) return;
107
- featureFlag.sets.forEach(featureFlagSet => {
108
-
109
- if (this.flagSetsFilter.length > 0 && !this.flagSetsFilter.some(filterFlagSet => filterFlagSet === featureFlagSet)) return;
110
-
111
- if (!this.flagSetsCache[featureFlagSet]) this.flagSetsCache[featureFlagSet] = new Set([]);
112
-
113
- this.flagSetsCache[featureFlagSet].add(featureFlag.name);
114
- });
115
- }
116
-
117
- private removeFromFlagSets(featureFlagName: string, flagSets: string[] | undefined) {
118
- if (!flagSets) return;
119
- flagSets.forEach(flagSet => {
120
- this.removeNames(flagSet, featureFlagName);
121
- });
122
- }
123
-
124
- private removeNames(flagSetName: string, featureFlagName: string) {
125
- if (!this.flagSetsCache[flagSetName]) return;
126
- this.flagSetsCache[flagSetName].delete(featureFlagName);
127
- if (this.flagSetsCache[flagSetName].size === 0) delete this.flagSetsCache[flagSetName];
128
- }
129
-
130
- }
@@ -1,230 +0,0 @@
1
- import { isFiniteNumber, isNaNNumber } from '../../utils/lang';
2
- import { KeyBuilder } from '../KeyBuilder';
3
- import { IPluggableStorageWrapper } from '../types';
4
- import { ILogger } from '../../logger/types';
5
- import { ISplit, ISplitFiltersValidation } from '../../dtos/types';
6
- import { LOG_PREFIX } from './constants';
7
- import { AbstractSplitsCacheAsync } from '../AbstractSplitsCacheAsync';
8
- import { returnDifference } from '../../utils/lang/sets';
9
-
10
- /**
11
- * ISplitsCacheAsync implementation for pluggable storages.
12
- */
13
- export class SplitsCachePluggable extends AbstractSplitsCacheAsync {
14
-
15
- private readonly log: ILogger;
16
- private readonly keys: KeyBuilder;
17
- private readonly wrapper: IPluggableStorageWrapper;
18
- private readonly flagSetsFilter: string[];
19
-
20
- /**
21
- * Create a SplitsCache that uses a storage wrapper.
22
- * @param log - Logger instance.
23
- * @param keys - Key builder.
24
- * @param wrapper - Adapted wrapper storage.
25
- */
26
- constructor(log: ILogger, keys: KeyBuilder, wrapper: IPluggableStorageWrapper, splitFiltersValidation?: ISplitFiltersValidation) {
27
- super();
28
- this.log = log;
29
- this.keys = keys;
30
- this.wrapper = wrapper;
31
- this.flagSetsFilter = splitFiltersValidation ? splitFiltersValidation.groupedFilters.bySet : [];
32
- }
33
-
34
- private _decrementCounts(split: ISplit) {
35
- const ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
36
- return this.wrapper.decr(ttKey).then(count => {
37
- if (count === 0) return this.wrapper.del(ttKey);
38
- });
39
- }
40
-
41
- private _incrementCounts(split: ISplit) {
42
- const ttKey = this.keys.buildTrafficTypeKey(split.trafficTypeName);
43
- return this.wrapper.incr(ttKey);
44
- }
45
-
46
- private _updateFlagSets(featureFlagName: string, flagSetsOfRemovedFlag?: string[], flagSetsOfAddedFlag?: string[]) {
47
- const removeFromFlagSets = returnDifference(flagSetsOfRemovedFlag, flagSetsOfAddedFlag);
48
-
49
- let addToFlagSets = returnDifference(flagSetsOfAddedFlag, flagSetsOfRemovedFlag);
50
- if (this.flagSetsFilter.length > 0) {
51
- addToFlagSets = addToFlagSets.filter(flagSet => {
52
- return this.flagSetsFilter.some(filterFlagSet => filterFlagSet === flagSet);
53
- });
54
- }
55
-
56
- const items = [featureFlagName];
57
-
58
- return Promise.all([
59
- ...removeFromFlagSets.map(flagSetName => this.wrapper.removeItems(this.keys.buildFlagSetKey(flagSetName), items)),
60
- ...addToFlagSets.map(flagSetName => this.wrapper.addItems(this.keys.buildFlagSetKey(flagSetName), items))
61
- ]);
62
- }
63
-
64
- /**
65
- * Add a given split.
66
- * The returned promise is resolved when the operation success
67
- * or rejected if it fails (e.g., wrapper operation fails)
68
- */
69
- addSplit(split: ISplit): Promise<boolean> {
70
- const name = split.name;
71
- const splitKey = this.keys.buildSplitKey(name);
72
- return this.wrapper.get(splitKey).then(splitFromStorage => {
73
-
74
- // handling parsing error
75
- let parsedPreviousSplit: ISplit, stringifiedNewSplit;
76
- try {
77
- parsedPreviousSplit = splitFromStorage ? JSON.parse(splitFromStorage) : undefined;
78
- stringifiedNewSplit = JSON.stringify(split);
79
- } catch (e) {
80
- throw new Error('Error parsing feature flag definition: ' + e);
81
- }
82
-
83
- return this.wrapper.set(splitKey, stringifiedNewSplit).then(() => {
84
- // avoid unnecessary increment/decrement operations
85
- if (parsedPreviousSplit && parsedPreviousSplit.trafficTypeName === split.trafficTypeName) return;
86
-
87
- // update traffic type counts
88
- return this._incrementCounts(split).then(() => {
89
- if (parsedPreviousSplit) return this._decrementCounts(parsedPreviousSplit);
90
- });
91
- }).then(() => this._updateFlagSets(name, parsedPreviousSplit && parsedPreviousSplit.sets, split.sets));
92
- }).then(() => true);
93
- }
94
-
95
- /**
96
- * Remove a given split.
97
- * The returned promise is resolved when the operation success, with a boolean indicating if the split existed or not.
98
- * or rejected if it fails (e.g., wrapper operation fails).
99
- */
100
- removeSplit(name: string) {
101
- return this.getSplit(name).then((split) => {
102
- if (split) {
103
- return this._decrementCounts(split).then(() => this._updateFlagSets(name, split.sets));
104
- }
105
- }).then(() => {
106
- return this.wrapper.del(this.keys.buildSplitKey(name));
107
- });
108
- }
109
-
110
- /**
111
- * Get split.
112
- * The returned promise is resolved with the split definition or null if it's not defined,
113
- * or rejected if wrapper operation fails.
114
- */
115
- getSplit(name: string): Promise<ISplit | null> {
116
- return this.wrapper.get(this.keys.buildSplitKey(name))
117
- .then(maybeSplit => maybeSplit && JSON.parse(maybeSplit));
118
- }
119
-
120
- /**
121
- * Get list of splits.
122
- * The returned promise is resolved with a map of split names to their split definition or null if it's not defined,
123
- * or rejected if wrapper operation fails.
124
- */
125
- getSplits(names: string[]): Promise<Record<string, ISplit | null>> {
126
- const keys = names.map(name => this.keys.buildSplitKey(name));
127
-
128
- return this.wrapper.getMany(keys).then(splitDefinitions => {
129
- const splits: Record<string, ISplit | null> = {};
130
- names.forEach((name, idx) => {
131
- const split = splitDefinitions[idx];
132
- splits[name] = split && JSON.parse(split);
133
- });
134
- return Promise.resolve(splits);
135
- });
136
- }
137
-
138
- /**
139
- * Get list of all split definitions.
140
- * The returned promise is resolved with the list of split definitions,
141
- * or rejected if wrapper operation fails.
142
- */
143
- getAll(): Promise<ISplit[]> {
144
- return this.wrapper.getKeysByPrefix(this.keys.buildSplitKeyPrefix())
145
- .then((listOfKeys) => this.wrapper.getMany(listOfKeys))
146
- .then((splitDefinitions) => splitDefinitions.map((splitDefinition) => {
147
- return JSON.parse(splitDefinition as string);
148
- }));
149
- }
150
-
151
- /**
152
- * Get list of split names.
153
- * The returned promise is resolved with the list of split names,
154
- * or rejected if wrapper operation fails.
155
- */
156
- getSplitNames(): Promise<string[]> {
157
- return this.wrapper.getKeysByPrefix(this.keys.buildSplitKeyPrefix()).then(
158
- (listOfKeys) => listOfKeys.map(this.keys.extractKey)
159
- );
160
- }
161
-
162
- /**
163
- * Get list of feature flag names related to a given list of flag set names.
164
- * The returned promise is resolved with the list of feature flag names per flag set.
165
- * It never rejects (If there is a wrapper error for some flag set, an empty set is returned for it).
166
- */
167
- getNamesByFlagSets(flagSets: string[]): Promise<Set<string>[]> {
168
- return Promise.all(flagSets.map(flagSet => {
169
- const flagSetKey = this.keys.buildFlagSetKey(flagSet);
170
- return this.wrapper.getItems(flagSetKey).catch(() => []);
171
- })).then(namesByFlagSets => namesByFlagSets.map(namesByFlagSet => new Set(namesByFlagSet)));
172
- }
173
-
174
- /**
175
- * Check traffic type existence.
176
- * The returned promise is resolved with a boolean indicating whether the TT exist or not.
177
- * In case of wrapper operation failures, the promise resolves with a true value, assuming that the TT might exist.
178
- * It will never be rejected.
179
- */
180
- trafficTypeExists(trafficType: string): Promise<boolean> {
181
- // If there is a number there should be > 0, otherwise the TT is considered as not existent.
182
- return this.wrapper.get(this.keys.buildTrafficTypeKey(trafficType))
183
- .then((ttCount: string | null | number) => {
184
- if (ttCount === null) return false; // if entry doesn't exist, means that TT doesn't exist
185
-
186
- ttCount = parseInt(ttCount as string, 10);
187
- if (!isFiniteNumber(ttCount) || ttCount < 0) {
188
- this.log.info(LOG_PREFIX + `Could not validate traffic type existence of ${trafficType} due to data corruption of some sorts.`);
189
- return false;
190
- }
191
-
192
- return ttCount > 0;
193
- }).catch(e => {
194
- this.log.error(LOG_PREFIX + `Could not validate traffic type existence of ${trafficType} due to an error: ${e}.`);
195
- // If there is an error, bypass the validation so the event can get tracked.
196
- return true;
197
- });
198
- }
199
-
200
- /**
201
- * Set till number.
202
- * The returned promise is resolved when the operation success,
203
- * or rejected if it fails (e.g., wrapper operation fails).
204
- */
205
- setChangeNumber(changeNumber: number) {
206
- return this.wrapper.set(this.keys.buildSplitsTillKey(), changeNumber + '');
207
- }
208
-
209
- /**
210
- * Get till number or -1 if it's not defined.
211
- * The returned promise is resolved with the changeNumber or -1 if it doesn't exist or a wrapper operation fails.
212
- * The promise will never be rejected.
213
- */
214
- getChangeNumber(): Promise<number> {
215
- return this.wrapper.get(this.keys.buildSplitsTillKey()).then((value) => {
216
- const i = parseInt(value as string, 10);
217
-
218
- return isNaNNumber(i) ? -1 : i;
219
- }).catch((e) => {
220
- this.log.error(LOG_PREFIX + 'Could not retrieve changeNumber from storage. Error: ' + e);
221
- return -1;
222
- });
223
- }
224
-
225
- // @TODO implement if required by DataLoader or producer mode
226
- clear() {
227
- return Promise.resolve(true);
228
- }
229
-
230
- }
@@ -1,230 +0,0 @@
1
- import { ISegmentsCacheBase, IStorageBase } from '../../../storages/types';
2
- import { ISplitChangesFetcher } from '../fetchers/types';
3
- import { IRBSegment, ISplit, ISplitChangesResponse, ISplitFiltersValidation, MaybeThenable } from '../../../dtos/types';
4
- import { ISplitsEventEmitter } from '../../../readiness/types';
5
- import { timeout } from '../../../utils/promise/timeout';
6
- import { SDK_SPLITS_ARRIVED, FLAGS_UPDATE, SEGMENTS_UPDATE } from '../../../readiness/constants';
7
- import { ILogger } from '../../../logger/types';
8
- import { SYNC_SPLITS_FETCH, SYNC_SPLITS_UPDATE, SYNC_RBS_UPDATE, SYNC_SPLITS_FETCH_FAILS, SYNC_SPLITS_FETCH_RETRY } from '../../../logger/constants';
9
- import { startsWith } from '../../../utils/lang';
10
- import { IN_RULE_BASED_SEGMENT, IN_SEGMENT, RULE_BASED_SEGMENT, STANDARD_SEGMENT } from '../../../utils/constants';
11
- import { setToArray } from '../../../utils/lang/sets';
12
- import { SPLIT_UPDATE } from '../../streaming/constants';
13
- import { SdkUpdateMetadata } from '../../../../types/splitio';
14
-
15
- export type InstantUpdate = { payload: ISplit | IRBSegment, changeNumber: number, type: string };
16
- type SplitChangesUpdater = (noCache?: boolean, till?: number, instantUpdate?: InstantUpdate) => Promise<boolean>
17
-
18
- // Checks that all registered segments have been fetched (changeNumber !== -1 for every segment).
19
- // Returns a promise that could be rejected.
20
- // @TODO review together with Segments and MySegments storage APIs
21
- function checkAllSegmentsExist(segments: ISegmentsCacheBase): Promise<boolean> {
22
- let registeredSegments = Promise.resolve(segments.getRegisteredSegments());
23
- return registeredSegments.then(segmentNames => {
24
- return Promise.all(segmentNames.map(segmentName => segments.getChangeNumber(segmentName)))
25
- .then(changeNumbers => changeNumbers.every(changeNumber => changeNumber !== undefined));
26
- });
27
- }
28
-
29
- /**
30
- * Collect segments from a raw FF or RBS definition.
31
- * Exported for testing purposes.
32
- */
33
- export function parseSegments(ruleEntity: ISplit | IRBSegment, matcherType: typeof IN_SEGMENT | typeof IN_RULE_BASED_SEGMENT = IN_SEGMENT): Set<string> {
34
- const { conditions = [], excluded } = ruleEntity as IRBSegment;
35
-
36
- const segments = new Set<string>();
37
- if (excluded && excluded.segments) {
38
- excluded.segments.forEach(({ type, name }) => {
39
- if ((type === STANDARD_SEGMENT && matcherType === IN_SEGMENT) || (type === RULE_BASED_SEGMENT && matcherType === IN_RULE_BASED_SEGMENT)) {
40
- segments.add(name);
41
- }
42
- });
43
- }
44
-
45
- for (let i = 0; i < conditions.length; i++) {
46
- const matchers = conditions[i].matcherGroup.matchers;
47
-
48
- matchers.forEach(matcher => {
49
- if (matcher.matcherType === matcherType) segments.add(matcher.userDefinedSegmentMatcherData.segmentName);
50
- });
51
- }
52
-
53
- return segments;
54
- }
55
-
56
- interface ISplitMutations<T extends ISplit | IRBSegment> {
57
- added: T[],
58
- removed: T[],
59
- names: string[]
60
- }
61
-
62
- /**
63
- * If there are defined filters and one feature flag doesn't match with them, its status is changed to 'ARCHIVE' to avoid storing it
64
- * If there is `bySet` filter, `byName` and `byPrefix` filters are ignored
65
- *
66
- * @param featureFlag - feature flag to be evaluated
67
- * @param filters - splitFiltersValidation bySet | byName
68
- */
69
- function matchFilters(featureFlag: ISplit, filters: ISplitFiltersValidation) {
70
- const { bySet: setsFilter, byName: namesFilter, byPrefix: prefixFilter } = filters.groupedFilters;
71
- if (setsFilter.length > 0) return featureFlag.sets && featureFlag.sets.some((featureFlagSet: string) => setsFilter.indexOf(featureFlagSet) > -1);
72
-
73
- const namesFilterConfigured = namesFilter.length > 0;
74
- const prefixFilterConfigured = prefixFilter.length > 0;
75
-
76
- if (!namesFilterConfigured && !prefixFilterConfigured) return true;
77
-
78
- const matchNames = namesFilterConfigured && namesFilter.indexOf(featureFlag.name) > -1;
79
- const matchPrefix = prefixFilterConfigured && prefixFilter.some(prefix => startsWith(featureFlag.name, prefix));
80
- return matchNames || matchPrefix;
81
- }
82
-
83
- /**
84
- * Given the list of splits from /splitChanges endpoint, it returns the mutations,
85
- * i.e., an object with added splits, removed splits and used segments.
86
- * Exported for testing purposes.
87
- */
88
- export function computeMutation<T extends ISplit | IRBSegment>(rules: Array<T>, segments: Set<string>, filters?: ISplitFiltersValidation): ISplitMutations<T> {
89
-
90
- return rules.reduce((accum, ruleEntity) => {
91
- if (ruleEntity.status === 'ACTIVE' && (!filters || matchFilters(ruleEntity as ISplit, filters))) {
92
- accum.added.push(ruleEntity);
93
-
94
- parseSegments(ruleEntity).forEach((segmentName: string) => {
95
- segments.add(segmentName);
96
- });
97
- } else {
98
- accum.removed.push(ruleEntity);
99
- }
100
- accum.names.push(ruleEntity.name);
101
-
102
- return accum;
103
- }, { added: [], removed: [], names: [] } as ISplitMutations<T>);
104
- }
105
-
106
- /**
107
- * factory of SplitChanges updater, a task that:
108
- * - fetches split changes using `splitChangesFetcher`
109
- * - updates `splitsCache`
110
- * - uses `splitsEventEmitter` to emit events related to split data updates
111
- *
112
- * @param log - Logger instance
113
- * @param splitChangesFetcher - Fetcher of `/splitChanges`
114
- * @param splits - Splits storage, with sync or async methods
115
- * @param segments - Segments storage, with sync or async methods
116
- * @param splitsEventEmitter - Optional readiness manager. Not required for synchronizer or producer mode.
117
- * @param requestTimeoutBeforeReady - How long the updater will wait for the request to timeout. Default 0, i.e., never timeout.
118
- * @param retriesOnFailureBeforeReady - How many retries on `/splitChanges` we the updater do in case of failure or timeout. Default 0, i.e., no retries.
119
- */
120
- export function splitChangesUpdaterFactory(
121
- log: ILogger,
122
- splitChangesFetcher: ISplitChangesFetcher,
123
- storage: Pick<IStorageBase, 'splits' | 'rbSegments' | 'segments' | 'save'>,
124
- splitFiltersValidation: ISplitFiltersValidation,
125
- splitsEventEmitter?: ISplitsEventEmitter,
126
- requestTimeoutBeforeReady = 0,
127
- retriesOnFailureBeforeReady = 0,
128
- isClientSide?: boolean
129
- ): SplitChangesUpdater {
130
- const { splits, rbSegments, segments } = storage;
131
-
132
- let startingUp = true;
133
-
134
- /** timeout decorator for `splitChangesFetcher` promise */
135
- function _promiseDecorator<T>(promise: Promise<T>) {
136
- if (startingUp && requestTimeoutBeforeReady) promise = timeout(requestTimeoutBeforeReady, promise);
137
- return promise;
138
- }
139
-
140
- /**
141
- * SplitChanges updater returns a promise that resolves with a `false` boolean value if it fails to fetch splits or synchronize them with the storage.
142
- * Returned promise will not be rejected.
143
- *
144
- * @param noCache - true to revalidate data to fetch
145
- * @param till - query param to bypass CDN requests
146
- */
147
- return function splitChangesUpdater(noCache?: boolean, till?: number, instantUpdate?: InstantUpdate) {
148
-
149
- /**
150
- * @param since - current changeNumber at splitsCache
151
- * @param retry - current number of retry attempts
152
- */
153
- function _splitChangesUpdater(sinces: [number, number], retry = 0): Promise<boolean> {
154
- const [since, rbSince] = sinces;
155
- log.debug(SYNC_SPLITS_FETCH, sinces);
156
- return Promise.resolve(
157
- instantUpdate ?
158
- instantUpdate.type === SPLIT_UPDATE ?
159
- // IFFU edge case: a change to a flag that adds an IN_RULE_BASED_SEGMENT matcher that is not present yet
160
- Promise.resolve(rbSegments.contains(parseSegments(instantUpdate.payload, IN_RULE_BASED_SEGMENT))).then((contains) => {
161
- return contains ?
162
- { ff: { d: [instantUpdate.payload as ISplit], t: instantUpdate.changeNumber } } :
163
- splitChangesFetcher(since, noCache, till, rbSince, _promiseDecorator);
164
- }) :
165
- { rbs: { d: [instantUpdate.payload as IRBSegment], t: instantUpdate.changeNumber } } :
166
- splitChangesFetcher(since, noCache, till, rbSince, _promiseDecorator)
167
- )
168
- .then((splitChanges: ISplitChangesResponse) => {
169
- const usedSegments = new Set<string>();
170
-
171
- let updatedFlags: string[] = [];
172
- let ffUpdate: MaybeThenable<boolean> = false;
173
- if (splitChanges.ff) {
174
- const { added, removed, names } = computeMutation(splitChanges.ff.d, usedSegments, splitFiltersValidation);
175
- updatedFlags = names;
176
- log.debug(SYNC_SPLITS_UPDATE, [added.length, removed.length]);
177
- ffUpdate = splits.update(added, removed, splitChanges.ff.t);
178
- }
179
-
180
- let rbsUpdate: MaybeThenable<boolean> = false;
181
- if (splitChanges.rbs) {
182
- const { added, removed } = computeMutation(splitChanges.rbs.d, usedSegments);
183
- log.debug(SYNC_RBS_UPDATE, [added.length, removed.length]);
184
- rbsUpdate = rbSegments.update(added, removed, splitChanges.rbs.t);
185
- }
186
-
187
- return Promise.all([ffUpdate, rbsUpdate,
188
- // @TODO if at least 1 segment fetch fails due to 404 and other segments are updated in the storage, SDK_UPDATE is not emitted
189
- segments.registerSegments(setToArray(usedSegments))
190
- ]).then(([ffChanged, rbsChanged]) => {
191
- if (storage.save) storage.save();
192
-
193
- startingUp = false;
194
-
195
- if (splitsEventEmitter) {
196
- // To emit SDK_SPLITS_ARRIVED for server-side SDK, we must check that all registered segments have been fetched
197
- return Promise.resolve(!splitsEventEmitter.splitsArrived || ((ffChanged || rbsChanged) && (isClientSide || checkAllSegmentsExist(segments))))
198
- .catch(() => false /** noop. just to handle a possible `checkAllSegmentsExist` rejection, before emitting SDK event */)
199
- .then(emitSplitsArrivedEvent => {
200
- // emit SDK events
201
- if (emitSplitsArrivedEvent) {
202
- const metadata: SdkUpdateMetadata = {
203
- type: updatedFlags.length > 0 ? FLAGS_UPDATE : SEGMENTS_UPDATE,
204
- names: updatedFlags.length > 0 ? updatedFlags : []
205
- };
206
- splitsEventEmitter.emit(SDK_SPLITS_ARRIVED, metadata);
207
- }
208
- return true;
209
- });
210
- }
211
- return true;
212
- });
213
- })
214
- .catch(error => {
215
- if (startingUp && retriesOnFailureBeforeReady > retry) {
216
- retry += 1;
217
- log.warn(SYNC_SPLITS_FETCH_RETRY, [retry, error]);
218
- return _splitChangesUpdater(sinces, retry);
219
- } else {
220
- startingUp = false;
221
- log.warn(SYNC_SPLITS_FETCH_FAILS, [error]);
222
- }
223
- return false;
224
- });
225
- }
226
-
227
- // `getChangeNumber` never rejects or throws error
228
- return Promise.all([splits.getChangeNumber(), rbSegments.getChangeNumber()]).then(_splitChangesUpdater);
229
- };
230
- }
@@ -1,27 +0,0 @@
1
- import { ERROR_NULL, ERROR_INVALID, WARN_TRIMMING, ERROR_EMPTY } from '../../logger/constants';
2
- import { ILogger } from '../../logger/types';
3
- import { isString } from '../lang';
4
-
5
- // include BOM and nbsp
6
- const TRIMMABLE_SPACES_REGEX = /^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/;
7
-
8
- export function validateSplit(log: ILogger, maybeSplit: any, method: string, item = 'feature flag name'): string | false {
9
- if (maybeSplit == undefined) { // eslint-disable-line eqeqeq
10
- log.error(ERROR_NULL, [method, item]);
11
- } else if (!isString(maybeSplit)) {
12
- log.error(ERROR_INVALID, [method, item]);
13
- } else {
14
- if (TRIMMABLE_SPACES_REGEX.test(maybeSplit)) {
15
- log.warn(WARN_TRIMMING, [method, item, maybeSplit]);
16
- maybeSplit = maybeSplit.trim();
17
- }
18
-
19
- if (maybeSplit.length > 0) {
20
- return maybeSplit;
21
- } else {
22
- log.error(ERROR_EMPTY, [method, item]);
23
- }
24
- }
25
-
26
- return false;
27
- }
@@ -1,19 +0,0 @@
1
- import { FALLBACK_SPLIT_NOT_FOUND, SPLIT_NOT_FOUND } from '../labels';
2
- import { IReadinessManager } from '../../readiness/types';
3
- import { ILogger } from '../../logger/types';
4
- import { WARN_NOT_EXISTENT_SPLIT } from '../../logger/constants';
5
-
6
- /**
7
- * This is defined here and in this format mostly because of the logger and the fact that it's considered a validation at product level.
8
- * But it's not going to run on the input validation layer. In any case, the most compelling reason to use it as we do is to avoid going to Redis and get a split twice.
9
- */
10
- export function validateSplitExistence(log: ILogger, readinessManager: IReadinessManager, splitName: string, labelOrSplitObj: any, method: string): boolean {
11
- if (readinessManager.isReady()) { // Only if it's ready (synced with BE) we validate this, otherwise it may just be that the SDK is still syncing
12
- if (labelOrSplitObj === SPLIT_NOT_FOUND || labelOrSplitObj === FALLBACK_SPLIT_NOT_FOUND || labelOrSplitObj == null) {
13
- log.warn(WARN_NOT_EXISTENT_SPLIT, [method, splitName]);
14
- return false;
15
- }
16
- }
17
-
18
- return true;
19
- }
@@ -1,21 +0,0 @@
1
- import { ERROR_EMPTY_ARRAY } from '../../logger/constants';
2
- import { ILogger } from '../../logger/types';
3
- import { uniq } from '../lang';
4
- import { validateSplit } from './split';
5
-
6
- export function validateSplits(log: ILogger, maybeSplits: any, method: string, listName = 'feature flag names', item = 'feature flag name'): string[] | false {
7
- if (Array.isArray(maybeSplits) && maybeSplits.length > 0) {
8
- let validatedArray: string[] = [];
9
- // Remove invalid values
10
- maybeSplits.forEach(maybeSplit => {
11
- const splitName = validateSplit(log, maybeSplit, method, item);
12
- if (splitName) validatedArray.push(splitName);
13
- });
14
-
15
- // Strip off duplicated values if we have valid split names then return
16
- if (validatedArray.length) return uniq(validatedArray);
17
- }
18
-
19
- log.error(ERROR_EMPTY_ARRAY, [method, listName]);
20
- return false;
21
- }