@splitsoftware/splitio-commons 0.1.1-canary.6 → 0.1.1-rc.18

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 (269) hide show
  1. package/cjs/evaluator/matchers/matcherTypes.js +4 -4
  2. package/cjs/evaluator/matchersTransform/index.js +11 -11
  3. package/cjs/evaluator/value/sanitize.js +6 -6
  4. package/cjs/listeners/browser.js +5 -2
  5. package/cjs/listeners/node.js +9 -2
  6. package/cjs/logger/constants.js +3 -1
  7. package/cjs/logger/messages/error.js +3 -2
  8. package/cjs/logger/messages/info.js +2 -2
  9. package/cjs/logger/messages/warn.js +2 -1
  10. package/cjs/readiness/readinessManager.js +10 -7
  11. package/cjs/sdkFactory/index.js +1 -1
  12. package/cjs/services/splitApi.js +9 -1
  13. package/cjs/services/splitHttpClient.js +5 -4
  14. package/cjs/storages/AbstractSplitsCacheSync.js +1 -1
  15. package/cjs/storages/inLocalStorage/index.js +5 -2
  16. package/cjs/storages/inMemory/InMemoryStorage.js +2 -0
  17. package/cjs/storages/inMemory/InMemoryStorageCS.js +2 -0
  18. package/cjs/storages/inRedis/SplitsCacheInRedis.js +6 -2
  19. package/cjs/storages/inRedis/index.js +5 -2
  20. package/cjs/storages/pluggable/SplitsCachePluggable.js +6 -2
  21. package/cjs/storages/pluggable/inMemoryWrapper.js +6 -7
  22. package/cjs/storages/pluggable/index.js +5 -2
  23. package/cjs/storages/pluggable/wrapperAdapter.js +0 -1
  24. package/cjs/sync/offline/splitsParser/splitsParserFromFile.js +92 -89
  25. package/cjs/sync/offline/splitsParser/splitsParserFromSettings.js +45 -42
  26. package/cjs/sync/offline/syncTasks/fromObjectSyncTask.js +14 -4
  27. package/cjs/sync/polling/fetchers/segmentChangesFetcher.js +0 -8
  28. package/cjs/sync/polling/updaters/mySegmentsUpdater.js +30 -10
  29. package/cjs/sync/polling/updaters/segmentChangesUpdater.js +2 -4
  30. package/cjs/sync/streaming/SSEClient/index.js +38 -20
  31. package/cjs/sync/streaming/SSEHandler/NotificationKeeper.js +7 -0
  32. package/cjs/sync/streaming/SSEHandler/NotificationParser.js +4 -1
  33. package/cjs/sync/streaming/SSEHandler/index.js +8 -9
  34. package/cjs/sync/streaming/SSEHandler/types.js +14 -0
  35. package/cjs/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.js +5 -5
  36. package/cjs/sync/streaming/UpdateWorkers/SegmentsUpdateWorker.js +2 -1
  37. package/cjs/sync/streaming/UpdateWorkers/SplitsUpdateWorker.js +5 -3
  38. package/cjs/sync/streaming/constants.js +3 -1
  39. package/cjs/sync/streaming/mySegmentsV2utils.js +75 -0
  40. package/cjs/sync/streaming/pushManager.js +148 -40
  41. package/cjs/sync/submitters/metricsSyncTask.js +1 -1
  42. package/cjs/sync/submitters/submitterSyncTask.js +2 -2
  43. package/cjs/sync/syncManagerFromFile.js +15 -0
  44. package/cjs/sync/syncManagerFromObject.js +14 -0
  45. package/cjs/sync/syncManagerOffline.js +3 -3
  46. package/cjs/sync/syncManagerOnline.js +18 -5
  47. package/cjs/sync/syncTask.js +1 -1
  48. package/cjs/trackers/impressionObserver/ImpressionObserver.js +0 -2
  49. package/cjs/trackers/impressionObserver/buildKey.js +3 -9
  50. package/cjs/trackers/impressionObserver/impressionObserverCS.js +2 -2
  51. package/cjs/trackers/impressionObserver/impressionObserverSS.js +3 -3
  52. package/cjs/utils/constants/index.js +4 -1
  53. package/cjs/utils/decompress/index.js +427 -0
  54. package/cjs/utils/murmur3/{commons.js → common.js} +2 -6
  55. package/cjs/utils/murmur3/murmur3.js +11 -12
  56. package/cjs/utils/murmur3/murmur3_128.js +7 -142
  57. package/cjs/utils/murmur3/murmur3_128_x86.js +154 -0
  58. package/cjs/utils/murmur3/murmur3_64.js +36 -0
  59. package/cjs/utils/murmur3/utfx.js +100 -106
  60. package/cjs/utils/promise/wrapper.js +14 -11
  61. package/cjs/utils/settingsValidation/index.js +5 -2
  62. package/cjs/utils/settingsValidation/localhost/index.js +20 -0
  63. package/cjs/utils/settingsValidation/splitFilters.js +0 -1
  64. package/cjs/utils/settingsValidation/storage/storageCS.js +18 -8
  65. package/cjs/utils/settingsValidation/url.js +1 -1
  66. package/esm/evaluator/matchers/matcherTypes.js +2 -2
  67. package/esm/evaluator/matchersTransform/index.js +12 -12
  68. package/esm/evaluator/value/sanitize.js +7 -7
  69. package/esm/listeners/browser.js +5 -2
  70. package/esm/listeners/node.js +9 -2
  71. package/esm/logger/constants.js +2 -0
  72. package/esm/logger/messages/error.js +3 -2
  73. package/esm/logger/messages/info.js +2 -2
  74. package/esm/logger/messages/warn.js +2 -1
  75. package/esm/readiness/readinessManager.js +10 -7
  76. package/esm/sdkFactory/index.js +1 -1
  77. package/esm/services/splitApi.js +9 -1
  78. package/esm/services/splitHttpClient.js +5 -4
  79. package/esm/storages/AbstractSplitsCacheSync.js +1 -1
  80. package/esm/storages/inLocalStorage/index.js +5 -2
  81. package/esm/storages/inMemory/InMemoryStorage.js +2 -0
  82. package/esm/storages/inMemory/InMemoryStorageCS.js +2 -0
  83. package/esm/storages/inRedis/SplitsCacheInRedis.js +6 -2
  84. package/esm/storages/inRedis/index.js +5 -2
  85. package/esm/storages/pluggable/SplitsCachePluggable.js +6 -2
  86. package/esm/storages/pluggable/inMemoryWrapper.js +6 -7
  87. package/esm/storages/pluggable/index.js +5 -2
  88. package/esm/storages/pluggable/wrapperAdapter.js +0 -1
  89. package/esm/sync/offline/splitsParser/splitsParserFromFile.js +90 -88
  90. package/esm/sync/offline/splitsParser/splitsParserFromSettings.js +43 -41
  91. package/esm/sync/offline/syncTasks/fromObjectSyncTask.js +15 -5
  92. package/esm/sync/polling/fetchers/segmentChangesFetcher.js +0 -8
  93. package/esm/sync/polling/updaters/mySegmentsUpdater.js +30 -10
  94. package/esm/sync/polling/updaters/segmentChangesUpdater.js +2 -4
  95. package/esm/sync/streaming/SSEClient/index.js +38 -20
  96. package/esm/sync/streaming/SSEHandler/NotificationKeeper.js +7 -0
  97. package/esm/sync/streaming/SSEHandler/NotificationParser.js +4 -1
  98. package/esm/sync/streaming/SSEHandler/index.js +9 -10
  99. package/esm/sync/streaming/SSEHandler/types.js +13 -1
  100. package/esm/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.js +5 -5
  101. package/esm/sync/streaming/UpdateWorkers/SegmentsUpdateWorker.js +2 -1
  102. package/esm/sync/streaming/UpdateWorkers/SplitsUpdateWorker.js +5 -3
  103. package/esm/sync/streaming/constants.js +2 -0
  104. package/esm/sync/streaming/mySegmentsV2utils.js +69 -0
  105. package/esm/sync/streaming/pushManager.js +150 -42
  106. package/esm/sync/submitters/metricsSyncTask.js +1 -1
  107. package/esm/sync/submitters/submitterSyncTask.js +2 -2
  108. package/esm/sync/syncManagerFromFile.js +11 -0
  109. package/esm/sync/syncManagerFromObject.js +10 -0
  110. package/esm/sync/syncManagerOffline.js +3 -3
  111. package/esm/sync/syncManagerOnline.js +18 -5
  112. package/esm/sync/syncTask.js +1 -1
  113. package/esm/trackers/impressionObserver/ImpressionObserver.js +0 -2
  114. package/esm/trackers/impressionObserver/buildKey.js +2 -9
  115. package/esm/trackers/impressionObserver/impressionObserverCS.js +2 -2
  116. package/esm/trackers/impressionObserver/impressionObserverSS.js +3 -3
  117. package/esm/utils/constants/index.js +3 -0
  118. package/esm/utils/decompress/index.js +424 -0
  119. package/esm/utils/murmur3/{commons.js → common.js} +1 -4
  120. package/esm/utils/murmur3/murmur3.js +1 -2
  121. package/esm/utils/murmur3/murmur3_128.js +7 -142
  122. package/esm/utils/murmur3/murmur3_128_x86.js +150 -0
  123. package/esm/utils/murmur3/murmur3_64.js +32 -0
  124. package/esm/utils/murmur3/utfx.js +96 -106
  125. package/esm/utils/promise/wrapper.js +14 -11
  126. package/esm/utils/settingsValidation/index.js +5 -2
  127. package/esm/utils/settingsValidation/localhost/index.js +16 -0
  128. package/esm/utils/settingsValidation/splitFilters.js +0 -1
  129. package/esm/utils/settingsValidation/storage/storageCS.js +16 -7
  130. package/esm/utils/settingsValidation/url.js +1 -1
  131. package/package.json +5 -5
  132. package/src/evaluator/matchers/matcherTypes.ts +2 -2
  133. package/src/evaluator/matchersTransform/index.ts +12 -12
  134. package/src/evaluator/value/sanitize.ts +7 -7
  135. package/src/listeners/browser.ts +5 -2
  136. package/src/listeners/node.ts +14 -2
  137. package/src/logger/constants.ts +2 -0
  138. package/src/logger/messages/error.ts +3 -2
  139. package/src/logger/messages/info.ts +2 -2
  140. package/src/logger/messages/warn.ts +3 -1
  141. package/src/readiness/readinessManager.ts +9 -7
  142. package/src/sdkFactory/index.ts +1 -1
  143. package/src/sdkFactory/types.ts +4 -5
  144. package/src/services/splitApi.ts +12 -3
  145. package/src/services/splitHttpClient.ts +6 -5
  146. package/src/services/types.ts +7 -3
  147. package/src/storages/AbstractSplitsCacheSync.ts +1 -1
  148. package/src/storages/inLocalStorage/index.ts +8 -4
  149. package/src/storages/inMemory/InMemoryStorage.ts +3 -0
  150. package/src/storages/inMemory/InMemoryStorageCS.ts +3 -0
  151. package/src/storages/inRedis/SplitsCacheInRedis.ts +3 -1
  152. package/src/storages/inRedis/index.ts +8 -4
  153. package/src/storages/pluggable/SegmentsCachePluggable.ts +1 -1
  154. package/src/storages/pluggable/SplitsCachePluggable.ts +3 -1
  155. package/src/storages/pluggable/inMemoryWrapper.ts +6 -7
  156. package/src/storages/pluggable/index.ts +8 -4
  157. package/src/storages/pluggable/wrapperAdapter.ts +0 -1
  158. package/src/storages/types.ts +18 -15
  159. package/src/sync/offline/splitsParser/splitsParserFromFile.ts +110 -105
  160. package/src/sync/offline/splitsParser/splitsParserFromSettings.ts +45 -41
  161. package/src/sync/offline/syncTasks/fromObjectSyncTask.ts +15 -5
  162. package/src/sync/polling/fetchers/segmentChangesFetcher.ts +0 -7
  163. package/src/sync/polling/types.ts +2 -1
  164. package/src/sync/polling/updaters/mySegmentsUpdater.ts +28 -10
  165. package/src/sync/polling/updaters/segmentChangesUpdater.ts +2 -3
  166. package/src/sync/streaming/AuthClient/types.ts +3 -0
  167. package/src/sync/streaming/SSEClient/index.ts +43 -23
  168. package/src/sync/streaming/SSEClient/types.ts +0 -1
  169. package/src/sync/streaming/SSEHandler/NotificationKeeper.ts +8 -0
  170. package/src/sync/streaming/SSEHandler/NotificationParser.ts +4 -2
  171. package/src/sync/streaming/SSEHandler/index.ts +11 -20
  172. package/src/sync/streaming/SSEHandler/types.ts +37 -3
  173. package/src/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.ts +7 -6
  174. package/src/sync/streaming/UpdateWorkers/SegmentsUpdateWorker.ts +2 -1
  175. package/src/sync/streaming/UpdateWorkers/SplitsUpdateWorker.ts +4 -3
  176. package/src/sync/streaming/UpdateWorkers/types.ts +1 -1
  177. package/src/sync/streaming/constants.ts +2 -0
  178. package/src/sync/streaming/mySegmentsV2utils.ts +77 -0
  179. package/src/sync/streaming/pushManager.ts +145 -42
  180. package/src/sync/streaming/types.ts +14 -22
  181. package/src/sync/submitters/metricsSyncTask.ts +1 -1
  182. package/src/sync/submitters/submitterSyncTask.ts +2 -1
  183. package/src/sync/syncManagerFromFile.ts +13 -0
  184. package/src/sync/syncManagerFromObject.ts +12 -0
  185. package/src/sync/syncManagerOffline.ts +3 -3
  186. package/src/sync/syncManagerOnline.ts +19 -5
  187. package/src/sync/syncTask.ts +1 -1
  188. package/src/sync/types.ts +3 -1
  189. package/src/trackers/impressionObserver/ImpressionObserver.ts +4 -6
  190. package/src/trackers/impressionObserver/buildKey.ts +2 -16
  191. package/src/trackers/impressionObserver/impressionObserverCS.ts +2 -2
  192. package/src/trackers/impressionObserver/impressionObserverSS.ts +3 -3
  193. package/src/types.ts +16 -2
  194. package/src/utils/constants/index.ts +6 -1
  195. package/src/utils/decompress/index.ts +429 -0
  196. package/src/utils/lang/index.ts +1 -1
  197. package/src/utils/murmur3/{commons.ts → common.ts} +1 -5
  198. package/src/utils/murmur3/murmur3.ts +5 -5
  199. package/src/utils/murmur3/murmur3_128.ts +7 -180
  200. package/src/utils/murmur3/murmur3_128_x86.ts +188 -0
  201. package/src/utils/murmur3/murmur3_64.ts +36 -0
  202. package/src/utils/murmur3/utfx.ts +92 -110
  203. package/src/utils/promise/wrapper.ts +12 -9
  204. package/src/utils/settingsValidation/index.ts +8 -4
  205. package/src/utils/settingsValidation/localhost/index.ts +19 -0
  206. package/src/utils/settingsValidation/splitFilters.ts +0 -1
  207. package/src/utils/settingsValidation/storage/storageCS.ts +21 -8
  208. package/src/utils/settingsValidation/types.ts +2 -11
  209. package/src/utils/settingsValidation/url.ts +1 -1
  210. package/types/evaluator/matchers/matcherTypes.d.ts +2 -2
  211. package/types/listeners/browser.d.ts +3 -3
  212. package/types/listeners/node.d.ts +3 -2
  213. package/types/logger/constants.d.ts +2 -0
  214. package/types/sdkFactory/types.d.ts +4 -5
  215. package/types/services/types.d.ts +4 -0
  216. package/types/storages/inLocalStorage/index.d.ts +2 -2
  217. package/types/storages/inMemory/InMemoryStorage.d.ts +3 -0
  218. package/types/storages/inMemory/InMemoryStorageCS.d.ts +3 -0
  219. package/types/storages/inRedis/index.d.ts +2 -2
  220. package/types/storages/pluggable/index.d.ts +2 -2
  221. package/types/storages/types.d.ts +15 -15
  222. package/types/sync/offline/splitsParser/splitsParserFromFile.d.ts +2 -7
  223. package/types/sync/offline/splitsParser/splitsParserFromSettings.d.ts +1 -5
  224. package/types/sync/polling/types.d.ts +2 -1
  225. package/types/sync/streaming/AuthClient/indexV1.d.ts +12 -0
  226. package/types/sync/streaming/AuthClient/indexV2.d.ts +8 -0
  227. package/types/sync/streaming/AuthClient/types.d.ts +2 -0
  228. package/types/sync/streaming/SSEClient/index.d.ts +9 -12
  229. package/types/sync/streaming/SSEClient/types.d.ts +0 -1
  230. package/types/sync/streaming/SSEHandler/NotificationParser.d.ts +3 -2
  231. package/types/sync/streaming/SSEHandler/types.d.ts +30 -2
  232. package/types/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.d.ts +4 -3
  233. package/types/sync/streaming/UpdateWorkers/SegmentsUpdateWorker.d.ts +2 -1
  234. package/types/sync/streaming/UpdateWorkers/SplitsUpdateWorker.d.ts +3 -2
  235. package/types/sync/streaming/UpdateWorkers/types.d.ts +1 -1
  236. package/types/sync/streaming/constants.d.ts +3 -1
  237. package/types/sync/streaming/mySegmentsV2utils.d.ts +27 -0
  238. package/types/sync/streaming/pushManagerNoUsers.d.ts +13 -0
  239. package/types/sync/streaming/types.d.ts +9 -5
  240. package/types/sync/submitters/submitterSyncTask.d.ts +1 -1
  241. package/types/sync/syncManagerFromFile.d.ts +2 -0
  242. package/types/sync/syncManagerFromObject.d.ts +2 -0
  243. package/types/sync/syncManagerOffline.d.ts +1 -1
  244. package/types/sync/syncTask.d.ts +1 -1
  245. package/types/sync/types.d.ts +2 -0
  246. package/types/trackers/impressionObserver/ImpressionObserver.d.ts +2 -2
  247. package/types/trackers/impressionObserver/buildKey.d.ts +1 -1
  248. package/types/trackers/impressionObserver/impressionObserverCS.d.ts +2 -2
  249. package/types/trackers/impressionObserver/impressionObserverSS.d.ts +2 -2
  250. package/types/types.d.ts +16 -2
  251. package/types/utils/constants/index.d.ts +5 -1
  252. package/types/utils/decompress/index.d.ts +16 -0
  253. package/types/utils/lang/index.d.ts +1 -1
  254. package/types/utils/murmur3/common.d.ts +12 -0
  255. package/types/utils/murmur3/murmur3.d.ts +2 -2
  256. package/types/utils/murmur3/murmur3_128.d.ts +5 -0
  257. package/types/utils/murmur3/murmur3_128_x86.d.ts +7 -0
  258. package/types/utils/murmur3/murmur3_64.d.ts +10 -0
  259. package/types/utils/murmur3/utfx.d.ts +24 -6
  260. package/types/utils/settingsValidation/index.d.ts +3 -2
  261. package/types/utils/settingsValidation/localhost/index.d.ts +9 -0
  262. package/types/utils/settingsValidation/storage/storageCS.d.ts +7 -1
  263. package/types/utils/settingsValidation/types.d.ts +2 -10
  264. package/cjs/sync/streaming/pushManagerCS.js +0 -178
  265. package/cjs/sync/streaming/pushManagerSS.js +0 -128
  266. package/esm/sync/streaming/pushManagerCS.js +0 -174
  267. package/esm/sync/streaming/pushManagerSS.js +0 -124
  268. package/src/sync/streaming/pushManagerCS.ts +0 -237
  269. package/src/sync/streaming/pushManagerSS.ts +0 -177
@@ -1,237 +0,0 @@
1
- import { IPushEventEmitter, IPushManagerCS } from './types';
2
- import { ISSEClient } from './SSEClient/types';
3
- import { IStorageSync } from '../../storages/types';
4
- import { IReadinessManager } from '../../readiness/types';
5
- import { ISegmentsSyncTask, IPollingManager } from '../polling/types';
6
- import { IUpdateWorker } from './UpdateWorkers/types';
7
- import objectAssign from 'object-assign';
8
- import { MY_SEGMENTS_UPDATE, PUSH_NONRETRYABLE_ERROR, PUSH_SUBSYSTEM_DOWN, SECONDS_BEFORE_EXPIRATION, SPLIT_KILL, SPLIT_UPDATE, PUSH_RETRYABLE_ERROR, PUSH_SUBSYSTEM_UP } from './constants';
9
- import Backoff from '../../utils/Backoff';
10
- import SSEHandlerFactory from './SSEHandler';
11
- import MySegmentsUpdateWorker from './UpdateWorkers/MySegmentsUpdateWorker';
12
- import SplitsUpdateWorker from './UpdateWorkers/SplitsUpdateWorker';
13
- import { authenticateFactory, hashUserKey } from './AuthClient';
14
- import { forOwn } from '../../utils/lang';
15
- import SSEClient from './SSEClient';
16
- import { IFetchAuth } from '../../services/types';
17
- import { ISettings } from '../../types';
18
- import { getMatching } from '../../utils/key';
19
- import { IPlatform } from '../../sdkFactory/types';
20
- import { STREAMING_FALLBACK, STREAMING_REFRESH_TOKEN, STREAMING_CONNECTING, STREAMING_DISABLED, ERROR_STREAMING_AUTH, STREAMING_DISCONNECTING, STREAMING_RECONNECT } from '../../logger/constants';
21
-
22
- /**
23
- * PushManager factory for client-side, with support for multiple clients.
24
- * It assumes settings contains a key.
25
- */
26
- export default function pushManagerCSFactory(
27
- pollingManager: IPollingManager,
28
- storage: IStorageSync,
29
- readiness: IReadinessManager,
30
- fetchAuth: IFetchAuth,
31
- platform: IPlatform,
32
- settings: ISettings
33
- ): IPushManagerCS | undefined {
34
-
35
- const log = settings.log;
36
-
37
- let sseClient: ISSEClient;
38
- try {
39
- sseClient = new SSEClient(settings.urls.streaming, platform.getEventSource);
40
- } catch (e) {
41
- log.warn(STREAMING_FALLBACK, [e]);
42
- return;
43
- }
44
- const authenticate = authenticateFactory(fetchAuth);
45
-
46
- // init feedback loop
47
- const pushEmitter = new platform.EventEmitter() as IPushEventEmitter;
48
- const sseHandler = SSEHandlerFactory(log, pushEmitter);
49
- sseClient.setEventHandler(sseHandler);
50
-
51
- // [Only for client-side] map of hashes to user keys, to dispatch MY_SEGMENTS_UPDATE events to the corresponding MySegmentsUpdateWorker
52
- const userKeyHashes: Record<string, string> = {};
53
- const userKey = getMatching(settings.core.key); // matching key of main client
54
- const hash = hashUserKey(userKey);
55
- userKeyHashes[hash] = userKey;
56
-
57
- // [Only for client-side] map of user keys to their corresponding MySegmentsUpdateWorkers. It has a two-fold intention:
58
- // - stop workers all together when push is disconnected
59
- // - keep the current list of user keys to authenticate
60
- const workers: Record<string, IUpdateWorker> = {};
61
-
62
- // init workers
63
- const mySegmentsUpdateWorker = new MySegmentsUpdateWorker(pollingManager.segmentsSyncTask);
64
- workers[userKey] = mySegmentsUpdateWorker;
65
- const splitsUpdateWorker = new SplitsUpdateWorker(storage.splits, pollingManager.splitsSyncTask, readiness.splits);
66
-
67
- // [Only for client-side] variable to flag that a new client was added. It is needed to reconnect streaming.
68
- let connectForNewClient = false;
69
-
70
- // flag that indicates if `disconnectPush` was called, either by the SyncManager (when the client is destroyed) or by a PUSH_NONRETRYABLE_ERROR error.
71
- // It is used to halt the `connectPush` process if it was in progress.
72
- let disconnected: boolean | undefined;
73
-
74
- /** PushManager functions related to initialization */
75
-
76
- const connectPushRetryBackoff = new Backoff(connectPush, settings.scheduler.pushRetryBackoffBase);
77
-
78
- let timeoutId: ReturnType<typeof setTimeout>;
79
-
80
- function scheduleTokenRefresh(issuedAt: number, expirationTime: number) {
81
- // clear scheduled token refresh if exists (needed when resuming PUSH)
82
- if (timeoutId) clearTimeout(timeoutId);
83
-
84
- // Set token refresh 10 minutes before expirationTime
85
- const delayInSeconds = expirationTime - issuedAt - SECONDS_BEFORE_EXPIRATION;
86
-
87
- log.info(STREAMING_REFRESH_TOKEN, [delayInSeconds]);
88
-
89
- timeoutId = setTimeout(connectPush, delayInSeconds * 1000);
90
- }
91
-
92
- function connectPush() {
93
- disconnected = false;
94
- log.info(STREAMING_CONNECTING);
95
-
96
- const userKeys = Object.keys(workers); // [Only for client-side]
97
- authenticate(userKeys).then(
98
- function (authData) {
99
- if (disconnected) return;
100
-
101
- // 'pushEnabled: false' is handled as a PUSH_NONRETRYABLE_ERROR instead of PUSH_SUBSYSTEM_DOWN, in order to
102
- // close the sseClient in case the org has been bloqued while the instance was connected to streaming
103
- if (!authData.pushEnabled) {
104
- log.info(STREAMING_DISABLED);
105
- pushEmitter.emit(PUSH_NONRETRYABLE_ERROR);
106
- return;
107
- }
108
-
109
- // [Only for client-side] don't open SSE connection if a new shared client was added, since it means that a new authentication is taking place
110
- if (userKeys && userKeys.length < Object.keys(workers).length) return;
111
-
112
- // Connect to SSE and schedule refresh token
113
- const decodedToken = authData.decodedToken;
114
- sseClient.open(authData);
115
- scheduleTokenRefresh(decodedToken.iat, decodedToken.exp);
116
- }
117
- ).catch(
118
- function (error) {
119
- if (disconnected) return;
120
-
121
- log.error(ERROR_STREAMING_AUTH, [error.message]);
122
-
123
- // Handle 4XX HTTP errors: 401 (invalid API Key) or 400 (using incorrect API Key, i.e., client-side API Key on server-side)
124
- if (error.statusCode >= 400 && error.statusCode < 500) {
125
- pushEmitter.emit(PUSH_NONRETRYABLE_ERROR);
126
- return;
127
- }
128
-
129
- // Handle other HTTP and network errors as recoverable errors
130
- pushEmitter.emit(PUSH_RETRYABLE_ERROR);
131
- }
132
- );
133
- }
134
-
135
- // close SSE connection and cancel scheduled tasks
136
- function disconnectPush() {
137
- disconnected = true;
138
- log.info(STREAMING_DISCONNECTING);
139
- sseClient.close();
140
-
141
- if (timeoutId) clearTimeout(timeoutId);
142
- connectPushRetryBackoff.reset();
143
-
144
- stopWorkers();
145
- }
146
-
147
- // cancel scheduled fetch retries of Splits, Segments, and MySegments Update Workers
148
- function stopWorkers() {
149
- splitsUpdateWorker.backoff.reset();
150
- forOwn(workers, worker => worker.backoff.reset());
151
- }
152
-
153
- pushEmitter.on(PUSH_SUBSYSTEM_DOWN, stopWorkers);
154
-
155
- // restart backoff retry counter once push is connected
156
- pushEmitter.on(PUSH_SUBSYSTEM_UP, () => { connectPushRetryBackoff.reset(); });
157
-
158
- /** Fallbacking without retry due to: STREAMING_DISABLED control event, or 'pushEnabled: false', or non-recoverable SSE and Authentication errors */
159
-
160
- pushEmitter.on(PUSH_NONRETRYABLE_ERROR, function handleNonRetryableError() {
161
- // Note: `stopWorkers` is been called twice, but it is not harmful
162
- disconnectPush();
163
- pushEmitter.emit(PUSH_SUBSYSTEM_DOWN); // no harm if polling already
164
- });
165
-
166
- /** Fallbacking with retry due to recoverable SSE and Authentication errors */
167
-
168
- pushEmitter.on(PUSH_RETRYABLE_ERROR, function handleRetryableError() { // HTTP or network error in SSE connection
169
- // SSE connection is closed to avoid repeated errors due to retries
170
- sseClient.close();
171
-
172
- // retry streaming reconnect with backoff algorithm
173
- let delayInMillis = connectPushRetryBackoff.scheduleCall();
174
-
175
- log.info(STREAMING_RECONNECT, [delayInMillis / 1000]);
176
-
177
- pushEmitter.emit(PUSH_SUBSYSTEM_DOWN); // no harm if polling already
178
- });
179
-
180
- /** Functions related to synchronization (Queues and Workers in the spec) */
181
-
182
- pushEmitter.on(SPLIT_KILL, splitsUpdateWorker.killSplit);
183
- pushEmitter.on(SPLIT_UPDATE, splitsUpdateWorker.put);
184
- // [Only for client-side]
185
- pushEmitter.on(MY_SEGMENTS_UPDATE, function handleMySegmentsUpdate(parsedData, channel) {
186
- const userKeyHash = channel.split('_')[2];
187
- const userKey = userKeyHashes[userKeyHash];
188
- if (userKey && workers[userKey]) { // check context since it can be undefined if client has been destroyed
189
- const mySegmentsUpdateWorker = workers[userKey];
190
- mySegmentsUpdateWorker.put(
191
- parsedData.changeNumber,
192
- parsedData.includesPayload ? parsedData.segmentList ? parsedData.segmentList : [] : undefined);
193
- }
194
- });
195
-
196
- return objectAssign(
197
- // Expose Event Emitter functionality and Event constants
198
- Object.create(pushEmitter),
199
- {
200
- // Expose functionality for starting and stoping push mode:
201
- stop: disconnectPush, // `handleNonRetryableError` cannot be used as `stop`, because it emits PUSH_SUBSYSTEM_DOWN event, which start polling.
202
-
203
- start() {
204
- // Run in next event-loop cycle for optimization: if multiple clients are created in the same cycle than the factory, only one authentication is performed.
205
- setTimeout(connectPush);
206
- },
207
-
208
- // [Only for client-side]
209
- add(userKey: string, mySegmentsSyncTask: ISegmentsSyncTask) {
210
- const mySegmentsUpdateWorker = new MySegmentsUpdateWorker(mySegmentsSyncTask);
211
- workers[userKey] = mySegmentsUpdateWorker;
212
-
213
- const hash = hashUserKey(userKey);
214
-
215
- if (!userKeyHashes[hash]) {
216
- userKeyHashes[hash] = userKey;
217
- connectForNewClient = true; // we must reconnect on start, to listen the channel for the new user key
218
- }
219
-
220
- // Reconnects in case of a new client.
221
- // Run in next event-loop cycle to save authentication calls
222
- // in case the user is creating several clients in the current cycle.
223
- setTimeout(function checkForReconnect() {
224
- if (connectForNewClient) {
225
- connectForNewClient = false;
226
- connectPush();
227
- }
228
- }, 0);
229
- },
230
- // [Only for client-side]
231
- remove(userKey: string) {
232
- const hash = hashUserKey(userKey);
233
- delete userKeyHashes[hash];
234
- }
235
- }
236
- );
237
- }
@@ -1,177 +0,0 @@
1
- import { IPushEventEmitter, IPushManager } from './types';
2
- import { ISSEClient } from './SSEClient/types';
3
- import { IStorageSync } from '../../storages/types';
4
- import { IPollingManager } from '../polling/types';
5
- import { IReadinessManager } from '../../readiness/types';
6
- import objectAssign from 'object-assign';
7
- import { PUSH_NONRETRYABLE_ERROR, PUSH_SUBSYSTEM_DOWN, SECONDS_BEFORE_EXPIRATION, SEGMENT_UPDATE, SPLIT_KILL, SPLIT_UPDATE, PUSH_RETRYABLE_ERROR, PUSH_SUBSYSTEM_UP } from './constants';
8
- import Backoff from '../../utils/Backoff';
9
- import SSEHandlerFactory from './SSEHandler';
10
- import SegmentsUpdateWorker from './UpdateWorkers/SegmentsUpdateWorker';
11
- import SplitsUpdateWorker from './UpdateWorkers/SplitsUpdateWorker';
12
- import { IFetchAuth } from '../../services/types';
13
- import { authenticateFactory } from './AuthClient';
14
- import SSEClient from './SSEClient';
15
- import { ISettings } from '../../types';
16
- import { IPlatform } from '../../sdkFactory/types';
17
- import { STREAMING_FALLBACK, STREAMING_REFRESH_TOKEN, STREAMING_CONNECTING, STREAMING_DISABLED, ERROR_STREAMING_AUTH, STREAMING_DISCONNECTING, STREAMING_RECONNECT } from '../../logger/constants';
18
-
19
- /**
20
- * PushManager factory for server-side
21
- */
22
- export default function pushManagerSSFactory(
23
- pollingManager: IPollingManager,
24
- storage: IStorageSync,
25
- readiness: IReadinessManager,
26
- fetchAuth: IFetchAuth,
27
- platform: IPlatform,
28
- settings: ISettings
29
- ): IPushManager | undefined {
30
-
31
- const log = settings.log;
32
-
33
- let sseClient: ISSEClient;
34
- try {
35
- sseClient = new SSEClient(settings.urls.streaming, platform.getEventSource);
36
- } catch (e) {
37
- log.warn(STREAMING_FALLBACK, [e]);
38
- return;
39
- }
40
- const authenticate = authenticateFactory(fetchAuth);
41
-
42
- // init feedback loop (pushEmitter)
43
- const pushEmitter = new platform.EventEmitter() as IPushEventEmitter;
44
- const sseHandler = SSEHandlerFactory(log, pushEmitter);
45
- sseClient.setEventHandler(sseHandler);
46
-
47
- // init workers
48
- const splitsUpdateWorker = new SplitsUpdateWorker(storage.splits, pollingManager.splitsSyncTask, readiness.splits, pollingManager.segmentsSyncTask);
49
- const segmentsUpdateWorker = new SegmentsUpdateWorker(storage.segments, pollingManager.segmentsSyncTask);
50
-
51
- // flag that indicates if `disconnectPush` was called, either by the SyncManager (when the client is destroyed) or by a PUSH_NONRETRYABLE_ERROR error.
52
- // It is used to halt the `connectPush` process if it was in progress.
53
- let disconnected: boolean | undefined;
54
-
55
- /** PushManager functions related to initialization */
56
-
57
- const connectPushRetryBackoff = new Backoff(connectPush, settings.scheduler.pushRetryBackoffBase);
58
-
59
- let timeoutId: ReturnType<typeof setTimeout>;
60
-
61
- function scheduleTokenRefresh(issuedAt: number, expirationTime: number) {
62
- // clear scheduled token refresh if exists (needed when resuming PUSH)
63
- if (timeoutId) clearTimeout(timeoutId);
64
-
65
- // Set token refresh 10 minutes before expirationTime
66
- const delayInSeconds = expirationTime - issuedAt - SECONDS_BEFORE_EXPIRATION;
67
-
68
- log.info(STREAMING_REFRESH_TOKEN, [delayInSeconds]);
69
-
70
- timeoutId = setTimeout(connectPush, delayInSeconds * 1000);
71
- }
72
-
73
- function connectPush() {
74
- disconnected = false;
75
- log.info(STREAMING_CONNECTING);
76
-
77
- authenticate().then(
78
- function (authData) {
79
- if (disconnected) return;
80
-
81
- // 'pushEnabled: false' is handled as a PUSH_NONRETRYABLE_ERROR instead of PUSH_SUBSYSTEM_DOWN, in order to
82
- // close the sseClient in case the org has been bloqued while the instance was connected to streaming
83
- if (!authData.pushEnabled) {
84
- log.info(STREAMING_DISABLED);
85
- pushEmitter.emit(PUSH_NONRETRYABLE_ERROR);
86
- return;
87
- }
88
-
89
- // Connect to SSE and schedule refresh token
90
- const decodedToken = authData.decodedToken;
91
- sseClient.open(authData);
92
- scheduleTokenRefresh(decodedToken.iat, decodedToken.exp);
93
- }
94
- ).catch(
95
- function (error) {
96
- if (disconnected) return;
97
-
98
- log.error(ERROR_STREAMING_AUTH, [error.message]);
99
-
100
- // Handle 4XX HTTP errors: 401 (invalid API Key) or 400 (using incorrect API Key, i.e., client-side API Key on server-side)
101
- if (error.statusCode >= 400 && error.statusCode < 500) {
102
- pushEmitter.emit(PUSH_NONRETRYABLE_ERROR);
103
- return;
104
- }
105
-
106
- // Handle other HTTP and network errors as recoverable errors
107
- pushEmitter.emit(PUSH_RETRYABLE_ERROR);
108
- }
109
- );
110
- }
111
-
112
- // close SSE connection and cancel scheduled tasks
113
- function disconnectPush() {
114
- disconnected = true;
115
- log.info(STREAMING_DISCONNECTING);
116
- sseClient.close();
117
-
118
- if (timeoutId) clearTimeout(timeoutId);
119
- connectPushRetryBackoff.reset();
120
-
121
- stopWorkers();
122
- }
123
-
124
- // cancel scheduled fetch retries of Splits, Segments, and MySegments Update Workers
125
- function stopWorkers() {
126
- splitsUpdateWorker.backoff.reset();
127
- segmentsUpdateWorker.backoff.reset();
128
- }
129
-
130
- pushEmitter.on(PUSH_SUBSYSTEM_DOWN, stopWorkers);
131
-
132
- // restart backoff retry counter once push is connected
133
- pushEmitter.on(PUSH_SUBSYSTEM_UP, () => { connectPushRetryBackoff.reset(); });
134
-
135
- /** Fallbacking without retry due to: STREAMING_DISABLED control event, or 'pushEnabled: false', or non-recoverable SSE and Authentication errors */
136
-
137
- pushEmitter.on(PUSH_NONRETRYABLE_ERROR, function handleNonRetryableError() {
138
- // Note: `stopWorkers` is been called twice, but it is not harmful
139
- disconnectPush();
140
- pushEmitter.emit(PUSH_SUBSYSTEM_DOWN); // no harm if polling already
141
- });
142
-
143
- /** Fallbacking with retry due to recoverable SSE and Authentication errors */
144
-
145
- pushEmitter.on(PUSH_RETRYABLE_ERROR, function handleRetryableError() { // HTTP or network error in SSE connection
146
- // SSE connection is closed to avoid repeated errors due to retries
147
- sseClient.close();
148
-
149
- // retry streaming reconnect with backoff algorithm
150
- let delayInMillis = connectPushRetryBackoff.scheduleCall();
151
-
152
- log.info(STREAMING_RECONNECT, [delayInMillis / 1000]);
153
-
154
- pushEmitter.emit(PUSH_SUBSYSTEM_DOWN); // no harm if polling already
155
- });
156
-
157
- /** Functions related to synchronization (Queues and Workers in the spec) */
158
-
159
- pushEmitter.on(SPLIT_KILL, splitsUpdateWorker.killSplit);
160
- pushEmitter.on(SPLIT_UPDATE, splitsUpdateWorker.put);
161
- // [Only for server-side]
162
- pushEmitter.on(SEGMENT_UPDATE, segmentsUpdateWorker.put);
163
-
164
- return objectAssign(
165
- // Expose Event Emitter functionality and Event constants
166
- Object.create(pushEmitter),
167
- {
168
- // Expose functionality for starting and stoping push mode:
169
- stop: disconnectPush, // `handleNonRetryableError` cannot be used as `stop`, because it emits PUSH_SUBSYSTEM_DOWN event, which start polling.
170
-
171
- start() {
172
- // Run in next event-loop cycle as in browser
173
- setTimeout(connectPush);
174
- }
175
- }
176
- );
177
- }