@splitsoftware/splitio-commons 1.16.1-rc.10 → 1.16.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 (77) hide show
  1. package/cjs/logger/constants.js +2 -2
  2. package/cjs/logger/messages/warn.js +1 -1
  3. package/cjs/services/splitApi.js +3 -3
  4. package/cjs/storages/AbstractSegmentsCacheSync.js +41 -7
  5. package/cjs/storages/dataLoader.js +1 -1
  6. package/cjs/storages/inLocalStorage/MySegmentsCacheInLocal.js +19 -63
  7. package/cjs/storages/inMemory/MySegmentsCacheInMemory.js +5 -40
  8. package/cjs/storages/inMemory/TelemetryCacheInMemory.js +1 -1
  9. package/cjs/sync/polling/fetchers/mySegmentsFetcher.js +2 -2
  10. package/cjs/sync/polling/updaters/mySegmentsUpdater.js +12 -21
  11. package/cjs/sync/streaming/AuthClient/index.js +1 -1
  12. package/cjs/sync/streaming/SSEHandler/index.js +5 -7
  13. package/cjs/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.js +106 -63
  14. package/cjs/sync/streaming/constants.js +3 -3
  15. package/cjs/sync/streaming/pushManager.js +25 -31
  16. package/cjs/utils/constants/index.js +3 -4
  17. package/esm/logger/constants.js +1 -1
  18. package/esm/logger/messages/warn.js +1 -1
  19. package/esm/services/splitApi.js +4 -4
  20. package/esm/storages/AbstractSegmentsCacheSync.js +41 -7
  21. package/esm/storages/dataLoader.js +1 -1
  22. package/esm/storages/inLocalStorage/MySegmentsCacheInLocal.js +19 -63
  23. package/esm/storages/inMemory/MySegmentsCacheInMemory.js +5 -40
  24. package/esm/storages/inMemory/TelemetryCacheInMemory.js +1 -1
  25. package/esm/sync/polling/fetchers/mySegmentsFetcher.js +2 -2
  26. package/esm/sync/polling/updaters/mySegmentsUpdater.js +12 -21
  27. package/esm/sync/streaming/AuthClient/index.js +1 -1
  28. package/esm/sync/streaming/SSEHandler/index.js +6 -8
  29. package/esm/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.js +106 -63
  30. package/esm/sync/streaming/constants.js +2 -2
  31. package/esm/sync/streaming/pushManager.js +28 -34
  32. package/esm/utils/constants/index.js +1 -2
  33. package/package.json +1 -1
  34. package/src/dtos/types.ts +9 -12
  35. package/src/logger/constants.ts +1 -1
  36. package/src/logger/messages/warn.ts +1 -1
  37. package/src/services/splitApi.ts +4 -4
  38. package/src/services/types.ts +1 -1
  39. package/src/storages/AbstractSegmentsCacheSync.ts +52 -7
  40. package/src/storages/AbstractSplitsCacheSync.ts +1 -1
  41. package/src/storages/dataLoader.ts +1 -1
  42. package/src/storages/inLocalStorage/MySegmentsCacheInLocal.ts +15 -69
  43. package/src/storages/inMemory/MySegmentsCacheInMemory.ts +6 -46
  44. package/src/storages/inMemory/TelemetryCacheInMemory.ts +1 -1
  45. package/src/storages/types.ts +6 -5
  46. package/src/sync/polling/fetchers/mySegmentsFetcher.ts +2 -1
  47. package/src/sync/polling/fetchers/types.ts +1 -0
  48. package/src/sync/polling/types.ts +9 -10
  49. package/src/sync/polling/updaters/mySegmentsUpdater.ts +15 -19
  50. package/src/sync/streaming/AuthClient/index.ts +1 -1
  51. package/src/sync/streaming/SSEHandler/index.ts +9 -11
  52. package/src/sync/streaming/SSEHandler/types.ts +6 -6
  53. package/src/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.ts +114 -65
  54. package/src/sync/streaming/constants.ts +2 -2
  55. package/src/sync/streaming/parseUtils.ts +2 -2
  56. package/src/sync/streaming/pushManager.ts +30 -39
  57. package/src/sync/streaming/types.ts +6 -6
  58. package/src/sync/submitters/types.ts +4 -5
  59. package/src/utils/constants/index.ts +1 -2
  60. package/types/dtos/types.d.ts +8 -12
  61. package/types/logger/constants.d.ts +1 -1
  62. package/types/services/types.d.ts +1 -1
  63. package/types/storages/AbstractSegmentsCacheSync.d.ts +8 -6
  64. package/types/storages/AbstractSplitsCacheSync.d.ts +1 -1
  65. package/types/storages/inLocalStorage/MySegmentsCacheInLocal.d.ts +1 -12
  66. package/types/storages/inMemory/MySegmentsCacheInMemory.d.ts +1 -9
  67. package/types/storages/types.d.ts +6 -5
  68. package/types/sync/polling/fetchers/types.d.ts +1 -1
  69. package/types/sync/polling/types.d.ts +9 -7
  70. package/types/sync/polling/updaters/mySegmentsUpdater.d.ts +1 -1
  71. package/types/sync/streaming/SSEHandler/types.d.ts +6 -6
  72. package/types/sync/streaming/UpdateWorkers/MySegmentsUpdateWorker.d.ts +3 -2
  73. package/types/sync/streaming/constants.d.ts +2 -2
  74. package/types/sync/streaming/parseUtils.d.ts +2 -2
  75. package/types/sync/streaming/types.d.ts +5 -5
  76. package/types/sync/submitters/types.d.ts +4 -5
  77. package/types/utils/constants/index.d.ts +1 -2
@@ -1,75 +1,118 @@
1
1
  import { Backoff } from '../../../utils/Backoff';
2
+ import { MEMBERSHIPS } from '../../../utils/constants';
3
+ import { FETCH_BACKOFF_MAX_RETRIES } from './constants';
4
+ import { MEMBERSHIPS_LS_UPDATE, MEMBERSHIPS_MS_UPDATE } from '../constants';
2
5
  /**
3
6
  * MySegmentsUpdateWorker factory
4
7
  */
5
- export function MySegmentsUpdateWorker(mySegmentsSyncTask, telemetryTracker, updateType) {
6
- var maxChangeNumber = 0; // keeps the maximum changeNumber among queued events
7
- var currentChangeNumber = -1;
8
- var handleNewEvent = false;
9
- var isHandlingEvent;
10
- var _segmentsData; // keeps the segmentsData (if included in notification payload) from the queued event with maximum changeNumber
11
- var _delay;
12
- var _delayTimeoutID;
13
- var backoff = new Backoff(__handleMySegmentsUpdateCall);
14
- function __handleMySegmentsUpdateCall() {
15
- isHandlingEvent = true;
16
- if (maxChangeNumber > currentChangeNumber) {
17
- handleNewEvent = false;
18
- var currentMaxChangeNumber_1 = maxChangeNumber;
19
- // fetch mySegments revalidating data if cached
20
- var syncTask = _delay ?
21
- new Promise(function (res) {
22
- _delayTimeoutID = setTimeout(function () {
23
- _delay = undefined;
24
- mySegmentsSyncTask.execute(_segmentsData, true).then(res);
25
- }, _delay);
26
- }) :
27
- mySegmentsSyncTask.execute(_segmentsData, true);
28
- syncTask.then(function (result) {
29
- if (!isHandlingEvent)
30
- return; // halt if `stop` has been called
31
- if (result !== false) { // Unlike `Splits|SegmentsUpdateWorker`, we cannot use `mySegmentsCache.getChangeNumber` since `/mySegments` endpoint doesn't provide this value.
32
- if (_segmentsData)
33
- telemetryTracker.trackUpdatesFromSSE(updateType);
34
- currentChangeNumber = Math.max(currentChangeNumber, currentMaxChangeNumber_1); // use `currentMaxChangeNumber`, in case that `maxChangeNumber` was updated during fetch.
35
- }
36
- if (handleNewEvent) {
37
- __handleMySegmentsUpdateCall();
38
- }
39
- else {
40
- backoff.scheduleCall();
41
- }
42
- });
43
- }
44
- else {
45
- isHandlingEvent = false;
8
+ export function MySegmentsUpdateWorker(log, storage, mySegmentsSyncTask, telemetryTracker) {
9
+ var _a;
10
+ function createUpdateWorker(mySegmentsCache) {
11
+ var maxChangeNumber = 0; // keeps the maximum changeNumber among queued events
12
+ var currentChangeNumber = -1;
13
+ var handleNewEvent = false;
14
+ var isHandlingEvent;
15
+ var cdnBypass;
16
+ var _segmentsData; // keeps the segmentsData (if included in notification payload) from the queued event with maximum changeNumber
17
+ var _delay;
18
+ var _delayTimeoutID;
19
+ var backoff = new Backoff(__handleMySegmentsUpdateCall);
20
+ function __handleMySegmentsUpdateCall() {
21
+ isHandlingEvent = true;
22
+ if (maxChangeNumber > Math.max(currentChangeNumber, mySegmentsCache.getChangeNumber())) {
23
+ handleNewEvent = false;
24
+ var currentMaxChangeNumber_1 = maxChangeNumber;
25
+ // fetch mySegments revalidating data if cached
26
+ var syncTask = _delay ?
27
+ new Promise(function (res) {
28
+ _delayTimeoutID = setTimeout(function () {
29
+ _delay = undefined;
30
+ mySegmentsSyncTask.execute(_segmentsData, true, cdnBypass ? maxChangeNumber : undefined).then(res);
31
+ }, _delay);
32
+ }) :
33
+ mySegmentsSyncTask.execute(_segmentsData, true, cdnBypass ? maxChangeNumber : undefined);
34
+ syncTask.then(function (result) {
35
+ if (!isHandlingEvent)
36
+ return; // halt if `stop` has been called
37
+ if (result !== false) { // Unlike `Splits|SegmentsUpdateWorker`, `mySegmentsCache.getChangeNumber` can be -1, since `/memberships` change number is optional
38
+ var storageChangeNumber = mySegmentsCache.getChangeNumber();
39
+ currentChangeNumber = storageChangeNumber > -1 ?
40
+ storageChangeNumber :
41
+ Math.max(currentChangeNumber, currentMaxChangeNumber_1); // use `currentMaxChangeNumber`, in case that `maxChangeNumber` was updated during fetch.
42
+ }
43
+ if (handleNewEvent) {
44
+ __handleMySegmentsUpdateCall();
45
+ }
46
+ else {
47
+ if (_segmentsData)
48
+ telemetryTracker.trackUpdatesFromSSE(MEMBERSHIPS);
49
+ var attempts = backoff.attempts + 1;
50
+ if (maxChangeNumber <= currentChangeNumber) {
51
+ log.debug("Refresh completed" + (cdnBypass ? ' bypassing the CDN' : '') + " in " + attempts + " attempts.");
52
+ isHandlingEvent = false;
53
+ return;
54
+ }
55
+ if (attempts < FETCH_BACKOFF_MAX_RETRIES) {
56
+ backoff.scheduleCall();
57
+ return;
58
+ }
59
+ if (cdnBypass) {
60
+ log.debug("No changes fetched after " + attempts + " attempts with CDN bypassed.");
61
+ isHandlingEvent = false;
62
+ }
63
+ else {
64
+ backoff.reset();
65
+ cdnBypass = true;
66
+ __handleMySegmentsUpdateCall();
67
+ }
68
+ }
69
+ });
70
+ }
71
+ else {
72
+ isHandlingEvent = false;
73
+ }
46
74
  }
75
+ return {
76
+ /**
77
+ * Invoked by NotificationProcessor on MY_(LARGE)_SEGMENTS_UPDATE notifications
78
+ *
79
+ * @param changeNumber change number of the notification
80
+ * @param segmentsData data for KeyList or SegmentRemoval instant updates
81
+ * @param delay optional time to wait for BoundedFetchRequest or BoundedFetchRequest updates
82
+ */
83
+ put: function (mySegmentsData, payload, delay) {
84
+ var type = mySegmentsData.type, cn = mySegmentsData.cn;
85
+ // Ignore event if it is outdated or if there is a pending fetch request (_delay is set)
86
+ if (cn <= Math.max(currentChangeNumber, mySegmentsCache.getChangeNumber()) || cn <= maxChangeNumber || _delay)
87
+ return;
88
+ maxChangeNumber = cn;
89
+ handleNewEvent = true;
90
+ cdnBypass = false;
91
+ _segmentsData = payload && { type: type, cn: cn, added: payload.added, removed: payload.removed };
92
+ _delay = delay;
93
+ if (backoff.timeoutID || !isHandlingEvent)
94
+ __handleMySegmentsUpdateCall();
95
+ backoff.reset();
96
+ },
97
+ stop: function () {
98
+ clearTimeout(_delayTimeoutID);
99
+ _delay = undefined;
100
+ isHandlingEvent = false;
101
+ backoff.reset();
102
+ }
103
+ };
47
104
  }
105
+ var updateWorkers = (_a = {},
106
+ _a[MEMBERSHIPS_MS_UPDATE] = createUpdateWorker(storage.segments),
107
+ _a[MEMBERSHIPS_LS_UPDATE] = createUpdateWorker(storage.largeSegments),
108
+ _a);
48
109
  return {
49
- /**
50
- * Invoked by NotificationProcessor on MY_(LARGE)_SEGMENTS_UPDATE notifications
51
- *
52
- * @param changeNumber change number of the notification
53
- * @param segmentsData data for KeyList or SegmentRemoval instant updates
54
- * @param delay optional time to wait for BoundedFetchRequest or BoundedFetchRequest updates
55
- */
56
- put: function (changeNumber, segmentsData, delay) {
57
- // Ignore event if it is outdated or if there is a pending fetch request (_delay is set)
58
- if (changeNumber <= currentChangeNumber || changeNumber <= maxChangeNumber || _delay)
59
- return;
60
- maxChangeNumber = changeNumber;
61
- handleNewEvent = true;
62
- _segmentsData = segmentsData;
63
- _delay = delay;
64
- if (backoff.timeoutID || !isHandlingEvent)
65
- __handleMySegmentsUpdateCall();
66
- backoff.reset();
110
+ put: function (mySegmentsData, payload, delay) {
111
+ updateWorkers[mySegmentsData.type].put(mySegmentsData, payload, delay);
67
112
  },
68
113
  stop: function () {
69
- clearTimeout(_delayTimeoutID);
70
- _delay = undefined;
71
- isHandlingEvent = false;
72
- backoff.reset();
114
+ updateWorkers[MEMBERSHIPS_MS_UPDATE].stop();
115
+ updateWorkers[MEMBERSHIPS_LS_UPDATE].stop();
73
116
  }
74
117
  };
75
118
  }
@@ -22,11 +22,11 @@ export var PUSH_SUBSYSTEM_UP = 'PUSH_SUBSYSTEM_UP';
22
22
  */
23
23
  export var PUSH_SUBSYSTEM_DOWN = 'PUSH_SUBSYSTEM_DOWN';
24
24
  // Update-type push notifications, handled by NotificationProcessor
25
- export var MY_SEGMENTS_UPDATE_V3 = 'MY_SEGMENTS_UPDATE_V3';
25
+ export var MEMBERSHIPS_MS_UPDATE = 'MEMBERSHIPS_MS_UPDATE';
26
+ export var MEMBERSHIPS_LS_UPDATE = 'MEMBERSHIPS_LS_UPDATE';
26
27
  export var SEGMENT_UPDATE = 'SEGMENT_UPDATE';
27
28
  export var SPLIT_KILL = 'SPLIT_KILL';
28
29
  export var SPLIT_UPDATE = 'SPLIT_UPDATE';
29
- export var MY_LARGE_SEGMENTS_UPDATE = 'MY_LARGE_SEGMENTS_UPDATE';
30
30
  // Control-type push notifications, handled by NotificationKeeper
31
31
  export var CONTROL = 'CONTROL';
32
32
  export var OCCUPANCY = 'OCCUPANCY';
@@ -8,13 +8,13 @@ import { authenticateFactory, hashUserKey } from './AuthClient';
8
8
  import { forOwn } from '../../utils/lang';
9
9
  import { SSEClient } from './SSEClient';
10
10
  import { getMatching } from '../../utils/key';
11
- import { MY_SEGMENTS_UPDATE_V3, PUSH_NONRETRYABLE_ERROR, PUSH_SUBSYSTEM_DOWN, SECONDS_BEFORE_EXPIRATION, SEGMENT_UPDATE, SPLIT_KILL, SPLIT_UPDATE, PUSH_RETRYABLE_ERROR, PUSH_SUBSYSTEM_UP, ControlType, MY_LARGE_SEGMENTS_UPDATE } from './constants';
12
- import { STREAMING_FALLBACK, STREAMING_REFRESH_TOKEN, STREAMING_CONNECTING, STREAMING_DISABLED, ERROR_STREAMING_AUTH, STREAMING_DISCONNECTING, STREAMING_RECONNECT, STREAMING_PARSING_MY_SEGMENTS_UPDATE, STREAMING_PARSING_SPLIT_UPDATE } from '../../logger/constants';
11
+ import { MEMBERSHIPS_MS_UPDATE, MEMBERSHIPS_LS_UPDATE, PUSH_NONRETRYABLE_ERROR, PUSH_SUBSYSTEM_DOWN, SECONDS_BEFORE_EXPIRATION, SEGMENT_UPDATE, SPLIT_KILL, SPLIT_UPDATE, PUSH_RETRYABLE_ERROR, PUSH_SUBSYSTEM_UP, ControlType } from './constants';
12
+ import { STREAMING_FALLBACK, STREAMING_REFRESH_TOKEN, STREAMING_CONNECTING, STREAMING_DISABLED, ERROR_STREAMING_AUTH, STREAMING_DISCONNECTING, STREAMING_RECONNECT, STREAMING_PARSING_MEMBERSHIPS_UPDATE, STREAMING_PARSING_SPLIT_UPDATE } from '../../logger/constants';
13
13
  import { UpdateStrategy } from './SSEHandler/types';
14
14
  import { getDelay, isInBitmap, parseBitmap, parseFFUpdatePayload, parseKeyList } from './parseUtils';
15
15
  import { _Set } from '../../utils/lang/sets';
16
16
  import { hash64 } from '../../utils/murmur3/murmur3_64';
17
- import { TOKEN_REFRESH, AUTH_REJECTION, MY_LARGE_SEGMENT, MY_SEGMENT } from '../../utils/constants';
17
+ import { TOKEN_REFRESH, AUTH_REJECTION } from '../../utils/constants';
18
18
  /**
19
19
  * PushManager factory:
20
20
  * - for server-side if key is not provided in settings.
@@ -45,10 +45,10 @@ export function pushManagerFactory(params, pollingManager) {
45
45
  var segmentsUpdateWorker = userKey ? undefined : SegmentsUpdateWorker(log, pollingManager.segmentsSyncTask, storage.segments);
46
46
  // For server-side we pass the segmentsSyncTask, used by SplitsUpdateWorker to fetch new segments
47
47
  var splitsUpdateWorker = SplitsUpdateWorker(log, storage.splits, pollingManager.splitsSyncTask, readiness.splits, telemetryTracker, userKey ? undefined : pollingManager.segmentsSyncTask);
48
- // [Only for client-side] map of hashes to user keys, to dispatch MY_SEGMENTS_UPDATE events to the corresponding MySegmentsUpdateWorker
48
+ // [Only for client-side] map of hashes to user keys, to dispatch membership update events to the corresponding MySegmentsUpdateWorker
49
49
  var userKeyHashes = {};
50
50
  // [Only for client-side] map of user keys to their corresponding hash64 and MySegmentsUpdateWorkers.
51
- // Hash64 is used to process MY_SEGMENTS_UPDATE events and dispatch actions to the corresponding MySegmentsUpdateWorker.
51
+ // Hash64 is used to process membership update events and dispatch actions to the corresponding MySegmentsUpdateWorker.
52
52
  var clients = {};
53
53
  // [Only for client-side] variable to flag that a new client was added. It is needed to reconnect streaming.
54
54
  var connectForNewClient = false;
@@ -140,9 +140,8 @@ export function pushManagerFactory(params, pollingManager) {
140
140
  splitsUpdateWorker.stop();
141
141
  if (userKey)
142
142
  forOwn(clients, function (_a) {
143
- var worker = _a.worker, workerLarge = _a.workerLarge;
144
- worker.stop();
145
- workerLarge.stop();
143
+ var worker = _a.worker;
144
+ return worker.stop();
146
145
  });
147
146
  else
148
147
  segmentsUpdateWorker.stop();
@@ -197,7 +196,6 @@ export function pushManagerFactory(params, pollingManager) {
197
196
  splitsUpdateWorker.put(parsedData);
198
197
  });
199
198
  function handleMySegmentsUpdate(parsedData) {
200
- var isLS = parsedData.t === MY_LARGE_SEGMENTS_UPDATE;
201
199
  switch (parsedData.u) {
202
200
  case UpdateStrategy.BoundedFetchRequest: {
203
201
  var bitmap_1;
@@ -205,13 +203,13 @@ export function pushManagerFactory(params, pollingManager) {
205
203
  bitmap_1 = parseBitmap(parsedData.d, parsedData.c);
206
204
  }
207
205
  catch (e) {
208
- log.warn(STREAMING_PARSING_MY_SEGMENTS_UPDATE, ['BoundedFetchRequest', e]);
206
+ log.warn(STREAMING_PARSING_MEMBERSHIPS_UPDATE, ['BoundedFetchRequest', e]);
209
207
  break;
210
208
  }
211
209
  forOwn(clients, function (_a, matchingKey) {
212
- var hash64 = _a.hash64, worker = _a.worker, workerLarge = _a.workerLarge;
210
+ var hash64 = _a.hash64, worker = _a.worker;
213
211
  if (isInBitmap(bitmap_1, hash64.hex)) {
214
- (isLS ? workerLarge : worker).put(parsedData.cn, undefined, getDelay(parsedData, matchingKey));
212
+ worker.put(parsedData, undefined, getDelay(parsedData, matchingKey));
215
213
  }
216
214
  });
217
215
  return;
@@ -224,51 +222,48 @@ export function pushManagerFactory(params, pollingManager) {
224
222
  removed_1 = new _Set(keyList.r);
225
223
  }
226
224
  catch (e) {
227
- log.warn(STREAMING_PARSING_MY_SEGMENTS_UPDATE, ['KeyList', e]);
225
+ log.warn(STREAMING_PARSING_MEMBERSHIPS_UPDATE, ['KeyList', e]);
228
226
  break;
229
227
  }
230
228
  if (!parsedData.n || !parsedData.n.length) {
231
- log.warn(STREAMING_PARSING_MY_SEGMENTS_UPDATE, ['KeyList', 'No segment name was provided']);
229
+ log.warn(STREAMING_PARSING_MEMBERSHIPS_UPDATE, ['KeyList', 'No segment name was provided']);
232
230
  break;
233
231
  }
234
232
  forOwn(clients, function (_a) {
235
- var hash64 = _a.hash64, worker = _a.worker, workerLarge = _a.workerLarge;
233
+ var hash64 = _a.hash64, worker = _a.worker;
236
234
  var add = added_1.has(hash64.dec) ? true : removed_1.has(hash64.dec) ? false : undefined;
237
235
  if (add !== undefined) {
238
- (isLS ? workerLarge : worker).put(parsedData.cn, [{
239
- isLS: isLS,
240
- name: parsedData.n[0],
241
- add: add,
242
- }]);
236
+ worker.put(parsedData, {
237
+ added: add ? [parsedData.n[0]] : [],
238
+ removed: add ? [] : [parsedData.n[0]]
239
+ });
243
240
  }
244
241
  });
245
242
  return;
246
243
  }
247
244
  case UpdateStrategy.SegmentRemoval:
248
245
  if (!parsedData.n || !parsedData.n.length) {
249
- log.warn(STREAMING_PARSING_MY_SEGMENTS_UPDATE, ['SegmentRemoval', 'No segment name was provided']);
246
+ log.warn(STREAMING_PARSING_MEMBERSHIPS_UPDATE, ['SegmentRemoval', 'No segment name was provided']);
250
247
  break;
251
248
  }
252
249
  forOwn(clients, function (_a) {
253
- var worker = _a.worker, workerLarge = _a.workerLarge;
254
- (isLS ? workerLarge : worker).put(parsedData.cn, parsedData.n.map(function (largeSegment) { return ({
255
- isLS: isLS,
256
- name: largeSegment,
257
- add: false,
258
- cn: parsedData.cn
259
- }); }));
250
+ var worker = _a.worker;
251
+ worker.put(parsedData, {
252
+ added: [],
253
+ removed: parsedData.n
254
+ });
260
255
  });
261
256
  return;
262
257
  }
263
258
  // `UpdateStrategy.UnboundedFetchRequest` and fallbacks of other cases
264
259
  forOwn(clients, function (_a, matchingKey) {
265
- var worker = _a.worker, workerLarge = _a.workerLarge;
266
- (isLS ? workerLarge : worker).put(parsedData.cn, undefined, getDelay(parsedData, matchingKey));
260
+ var worker = _a.worker;
261
+ worker.put(parsedData, undefined, getDelay(parsedData, matchingKey));
267
262
  });
268
263
  }
269
264
  if (userKey) {
270
- pushEmitter.on(MY_SEGMENTS_UPDATE_V3, handleMySegmentsUpdate);
271
- pushEmitter.on(MY_LARGE_SEGMENTS_UPDATE, handleMySegmentsUpdate);
265
+ pushEmitter.on(MEMBERSHIPS_MS_UPDATE, handleMySegmentsUpdate);
266
+ pushEmitter.on(MEMBERSHIPS_LS_UPDATE, handleMySegmentsUpdate);
272
267
  }
273
268
  else {
274
269
  pushEmitter.on(SEGMENT_UPDATE, segmentsUpdateWorker.put);
@@ -306,8 +301,7 @@ export function pushManagerFactory(params, pollingManager) {
306
301
  userKeyHashes[hash] = userKey;
307
302
  clients[userKey] = {
308
303
  hash64: hash64(userKey),
309
- worker: MySegmentsUpdateWorker(mySegmentsSyncTask, telemetryTracker, MY_SEGMENT),
310
- workerLarge: MySegmentsUpdateWorker(mySegmentsSyncTask, telemetryTracker, MY_LARGE_SEGMENT)
304
+ worker: MySegmentsUpdateWorker(log, storage, mySegmentsSyncTask, telemetryTracker)
311
305
  };
312
306
  connectForNewClient = true; // we must reconnect on start, to listen the channel for the new user key
313
307
  // Reconnects in case of a new client.
@@ -60,8 +60,7 @@ export var EVENTS = 'ev';
60
60
  export var TELEMETRY = 'te';
61
61
  export var TOKEN = 'to';
62
62
  export var SEGMENT = 'se';
63
- export var MY_SEGMENT = 'ms';
64
- export var MY_LARGE_SEGMENT = 'mls';
63
+ export var MEMBERSHIPS = 'ms';
65
64
  export var TREATMENT = 't';
66
65
  export var TREATMENTS = 'ts';
67
66
  export var TREATMENT_WITH_CONFIG = 'tc';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@splitsoftware/splitio-commons",
3
- "version": "1.16.1-rc.10",
3
+ "version": "1.16.1-rc.12",
4
4
  "description": "Split JavaScript SDK common components",
5
5
  "main": "cjs/index.js",
6
6
  "module": "esm/index.js",
package/src/dtos/types.ts CHANGED
@@ -224,20 +224,17 @@ export interface ISegmentChangesResponse {
224
224
  till: number
225
225
  }
226
226
 
227
+ export interface IMySegmentsResponse {
228
+ cn?: number,
229
+ k?: {
230
+ n: string
231
+ }[]
232
+ }
233
+
227
234
  /** Interface of the parsed JSON response of `/memberships/{userKey}` */
228
235
  export interface IMembershipsResponse {
229
- ms?: {
230
- cn?: number,
231
- k?: Array<{
232
- n: string
233
- }>
234
- },
235
- ls?: {
236
- cn?: number,
237
- k?: Array<{
238
- n: string
239
- }>
240
- }
236
+ ms?: IMySegmentsResponse,
237
+ ls?: IMySegmentsResponse
241
238
  }
242
239
 
243
240
  /** Metadata internal type for storages */
@@ -79,7 +79,7 @@ export const WARN_SPLITS_FILTER_IGNORED = 219;
79
79
  export const WARN_SPLITS_FILTER_INVALID = 220;
80
80
  export const WARN_SPLITS_FILTER_EMPTY = 221;
81
81
  export const WARN_SDK_KEY = 222;
82
- export const STREAMING_PARSING_MY_SEGMENTS_UPDATE = 223;
82
+ export const STREAMING_PARSING_MEMBERSHIPS_UPDATE = 223;
83
83
  export const STREAMING_PARSING_SPLIT_UPDATE = 224;
84
84
  export const WARN_INVALID_FLAGSET = 225;
85
85
  export const WARN_LOWERCASE_FLAGSET = 226;
@@ -32,7 +32,7 @@ export const codesWarn: [number, string][] = codesError.concat([
32
32
  [c.WARN_SPLITS_FILTER_EMPTY, c.LOG_PREFIX_SETTINGS + ': feature flag filter configuration must be a non-empty array of filter objects.'],
33
33
  [c.WARN_SDK_KEY, c.LOG_PREFIX_SETTINGS + ': You already have %s. We recommend keeping only one instance of the factory at all times (Singleton pattern) and reusing it throughout your application'],
34
34
 
35
- [c.STREAMING_PARSING_MY_SEGMENTS_UPDATE, c.LOG_PREFIX_SYNC_STREAMING + 'Fetching Memberships due to an error processing %s notification: %s'],
35
+ [c.STREAMING_PARSING_MEMBERSHIPS_UPDATE, c.LOG_PREFIX_SYNC_STREAMING + 'Fetching Memberships due to an error processing %s notification: %s'],
36
36
  [c.STREAMING_PARSING_SPLIT_UPDATE, c.LOG_PREFIX_SYNC_STREAMING + 'Fetching SplitChanges due to an error processing SPLIT_UPDATE notification: %s'],
37
37
  [c.WARN_INVALID_FLAGSET, '%s: you passed %s, flag set must adhere to the regular expressions %s. This means a flag set must start with a letter or number, be in lowercase, alphanumeric and have a max length of 50 characters. %s was discarded.'],
38
38
  [c.WARN_LOWERCASE_FLAGSET, '%s: flag set %s should be all lowercase - converting string to lowercase.'],
@@ -4,7 +4,7 @@ import { splitHttpClientFactory } from './splitHttpClient';
4
4
  import { ISplitApi } from './types';
5
5
  import { objectAssign } from '../utils/lang/objectAssign';
6
6
  import { ITelemetryTracker } from '../trackers/types';
7
- import { SPLITS, IMPRESSIONS, IMPRESSIONS_COUNT, EVENTS, TELEMETRY, TOKEN, SEGMENT, MY_SEGMENT } from '../utils/constants';
7
+ import { SPLITS, IMPRESSIONS, IMPRESSIONS_COUNT, EVENTS, TELEMETRY, TOKEN, SEGMENT, MEMBERSHIPS } from '../utils/constants';
8
8
  import { ERROR_TOO_MANY_SETS } from '../logger/constants';
9
9
 
10
10
  const noCacheHeaderOptions = { headers: { 'Cache-Control': 'no-cache' } };
@@ -67,15 +67,15 @@ export function splitApiFactory(
67
67
  return splitHttpClient(url, noCache ? noCacheHeaderOptions : undefined, telemetryTracker.trackHttp(SEGMENT));
68
68
  },
69
69
 
70
- fetchMemberships(userMatchingKey: string, noCache?: boolean) {
70
+ fetchMemberships(userMatchingKey: string, noCache?: boolean, till?: number) {
71
71
  /**
72
72
  * URI encoding of user keys in order to:
73
73
  * - avoid 400 responses (due to URI malformed). E.g.: '/api/memberships/%'
74
74
  * - avoid 404 responses. E.g.: '/api/memberships/foo/bar'
75
75
  * - match user keys with special characters. E.g.: 'foo%bar', 'foo/bar'
76
76
  */
77
- const url = `${urls.sdk}/memberships/${encodeURIComponent(userMatchingKey)}`;
78
- return splitHttpClient(url, noCache ? noCacheHeaderOptions : undefined, telemetryTracker.trackHttp(MY_SEGMENT));
77
+ const url = `${urls.sdk}/memberships/${encodeURIComponent(userMatchingKey)}${till ? '?till=' + till : ''}`;
78
+ return splitHttpClient(url, noCache ? noCacheHeaderOptions : undefined, telemetryTracker.trackHttp(MEMBERSHIPS));
79
79
  },
80
80
 
81
81
  /**
@@ -39,7 +39,7 @@ export type IFetchSplitChanges = (since: number, noCache?: boolean, till?: numbe
39
39
 
40
40
  export type IFetchSegmentChanges = (since: number, segmentName: string, noCache?: boolean, till?: number) => Promise<IResponse>
41
41
 
42
- export type IFetchMemberships = (userMatchingKey: string, noCache?: boolean) => Promise<IResponse>
42
+ export type IFetchMemberships = (userMatchingKey: string, noCache?: boolean, till?: number) => Promise<IResponse>
43
43
 
44
44
  export type IPostEventsBulk = (body: string, headers?: Record<string, string>) => Promise<IResponse>
45
45
 
@@ -1,5 +1,7 @@
1
1
  /* eslint-disable @typescript-eslint/no-unused-vars */
2
2
  /* eslint-disable no-unused-vars */
3
+ import { IMySegmentsResponse } from '../dtos/types';
4
+ import { MySegmentsData } from '../sync/polling/types';
3
5
  import { ISegmentsCacheSync } from './types';
4
6
 
5
7
  /**
@@ -28,7 +30,9 @@ export abstract class AbstractSegmentsCacheSync implements ISegmentsCacheSync {
28
30
  /**
29
31
  * clear the cache.
30
32
  */
31
- abstract clear(): void
33
+ clear() {
34
+ this.resetSegments({});
35
+ }
32
36
 
33
37
  /**
34
38
  * For server-side synchronizer: add the given list of segments to the cache, with an empty list of keys. The segments that already exist are not modified.
@@ -49,16 +53,57 @@ export abstract class AbstractSegmentsCacheSync implements ISegmentsCacheSync {
49
53
  abstract getKeysCount(): number
50
54
 
51
55
  /**
52
- * For server-side synchronizer: set the change number of `name` segment.
53
- * For client-side synchronizer: the method is not used.
56
+ * For server-side synchronizer: change number of `name` segment.
57
+ * For client-side synchronizer: change number of mySegments.
54
58
  */
55
- setChangeNumber(name: string, changeNumber: number): boolean { return true; }
56
-
59
+ abstract setChangeNumber(name?: string, changeNumber?: number): boolean | void
57
60
  abstract getChangeNumber(name: string): number
58
61
 
59
62
  /**
60
63
  * For server-side synchronizer: the method is not used.
61
- * For client-side synchronizer: reset the cache with the given list of segments.
64
+ * For client-side synchronizer: it resets or updates the cache.
62
65
  */
63
- resetSegments(names: string[], changeNumber?: number): boolean { return true; }
66
+ resetSegments(segmentsData: MySegmentsData | IMySegmentsResponse): boolean {
67
+ this.setChangeNumber(undefined, segmentsData.cn);
68
+
69
+ const { added, removed } = segmentsData as MySegmentsData;
70
+
71
+ if (added && removed) {
72
+ let isDiff = false;
73
+
74
+ added.forEach(segment => {
75
+ isDiff = this.addToSegment(segment) || isDiff;
76
+ });
77
+
78
+ removed.forEach(segment => {
79
+ isDiff = this.removeFromSegment(segment) || isDiff;
80
+ });
81
+
82
+ return isDiff;
83
+ }
84
+
85
+ const names = ((segmentsData as IMySegmentsResponse).k || []).map(s => s.n).sort();
86
+ const storedSegmentKeys = this.getRegisteredSegments().sort();
87
+
88
+ // Extreme fast => everything is empty
89
+ if (!names.length && !storedSegmentKeys.length) return false;
90
+
91
+ let index = 0;
92
+
93
+ while (index < names.length && index < storedSegmentKeys.length && names[index] === storedSegmentKeys[index]) index++;
94
+
95
+ // Quick path => no changes
96
+ if (index === names.length && index === storedSegmentKeys.length) return false;
97
+
98
+ // Slowest path => add and/or remove segments
99
+ for (let removeIndex = index; removeIndex < storedSegmentKeys.length; removeIndex++) {
100
+ this.removeFromSegment(storedSegmentKeys[removeIndex]);
101
+ }
102
+
103
+ for (let addIndex = index; addIndex < names.length; addIndex++) {
104
+ this.addToSegment(names[addIndex]);
105
+ }
106
+
107
+ return true;
108
+ }
64
109
  }
@@ -32,7 +32,7 @@ export abstract class AbstractSplitsCacheSync implements ISplitsCacheSync {
32
32
  return splits;
33
33
  }
34
34
 
35
- abstract setChangeNumber(changeNumber: number): boolean
35
+ abstract setChangeNumber(changeNumber: number): boolean | void
36
36
 
37
37
  abstract getChangeNumber(): number
38
38
 
@@ -50,6 +50,6 @@ export function dataLoaderFactory(preloadedData: SplitIO.PreloadedData): DataLoa
50
50
  return Array.isArray(userIds) && userIds.indexOf(userId) > -1;
51
51
  });
52
52
  }
53
- storage.segments.resetSegments(mySegmentsData);
53
+ storage.segments.resetSegments({ k: mySegmentsData.map(s => ({ n: s })) });
54
54
  };
55
55
  }