@splitsoftware/splitio-commons 3.1.1-rc.4 → 3.1.1-rc.5

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 (37) hide show
  1. package/cjs/presets/serverSide.js +2 -2
  2. package/cjs/services/authProvider.js +3 -4
  3. package/cjs/services/secureSplitHttpClient.js +3 -8
  4. package/cjs/services/serviceApi.js +17 -15
  5. package/cjs/services/splitApi.js +10 -9
  6. package/cjs/sync/polling/updaters/definitionChangesUpdater.js +2 -2
  7. package/cjs/sync/streaming/AuthClient/index.js +13 -14
  8. package/cjs/sync/streaming/SSEHandler/index.js +1 -0
  9. package/cjs/sync/streaming/constants.js +2 -1
  10. package/cjs/sync/streaming/pushManager.js +5 -10
  11. package/esm/presets/serverSide.js +2 -2
  12. package/esm/services/authProvider.js +4 -5
  13. package/esm/services/secureSplitHttpClient.js +3 -8
  14. package/esm/services/serviceApi.js +18 -16
  15. package/esm/services/splitApi.js +10 -9
  16. package/esm/sync/polling/updaters/definitionChangesUpdater.js +3 -3
  17. package/esm/sync/streaming/AuthClient/index.js +11 -11
  18. package/esm/sync/streaming/SSEHandler/index.js +2 -1
  19. package/esm/sync/streaming/constants.js +1 -0
  20. package/esm/sync/streaming/pushManager.js +6 -11
  21. package/package.json +1 -1
  22. package/src/presets/serverSide.ts +2 -2
  23. package/src/services/authProvider.ts +19 -13
  24. package/src/services/secureSplitHttpClient.ts +5 -13
  25. package/src/services/serviceApi.ts +21 -18
  26. package/src/services/splitApi.ts +10 -9
  27. package/src/services/types.ts +2 -2
  28. package/src/sync/polling/updaters/definitionChangesUpdater.ts +3 -3
  29. package/src/sync/streaming/AuthClient/index.ts +14 -15
  30. package/src/sync/streaming/AuthClient/types.ts +9 -13
  31. package/src/sync/streaming/SSEClient/index.ts +2 -2
  32. package/src/sync/streaming/SSEClient/types.ts +2 -2
  33. package/src/sync/streaming/SSEHandler/index.ts +2 -1
  34. package/src/sync/streaming/SSEHandler/types.ts +2 -2
  35. package/src/sync/streaming/constants.ts +1 -0
  36. package/src/sync/streaming/pushManager.ts +32 -42
  37. package/src/sync/streaming/types.ts +3 -2
@@ -7,16 +7,15 @@ import { SSEHandlerFactory } from './SSEHandler';
7
7
  import { MySegmentsUpdateWorker } from './UpdateWorkers/MySegmentsUpdateWorker';
8
8
  import { SegmentsUpdateWorker } from './UpdateWorkers/SegmentsUpdateWorker';
9
9
  import { DefinitionsUpdateWorker } from './UpdateWorkers/DefinitionsUpdateWorker';
10
- import { authenticateFactory, hashUserKey } from './AuthClient';
11
10
  import { forOwn } from '../../utils/lang';
12
11
  import { SSEClient } from './SSEClient';
13
12
  import { checkIfServerSide, getMatching } from '../../utils/key';
14
- import { MEMBERSHIPS_MS_UPDATE, MEMBERSHIPS_LS_UPDATE, PUSH_NON_RETRYABLE_ERROR, PUSH_SUBSYSTEM_DOWN, SECONDS_BEFORE_EXPIRATION, SEGMENT_UPDATE, SPLIT_KILL, SPLIT_UPDATE, RB_SEGMENT_UPDATE, PUSH_RETRYABLE_ERROR, PUSH_SUBSYSTEM_UP, ControlType } from './constants';
13
+ import { MEMBERSHIPS_MS_UPDATE, MEMBERSHIPS_LS_UPDATE, PUSH_NON_RETRYABLE_ERROR, PUSH_SUBSYSTEM_DOWN, SECONDS_BEFORE_EXPIRATION, SEGMENT_UPDATE, SPLIT_KILL, SPLIT_UPDATE, CONFIG_UPDATE, RB_SEGMENT_UPDATE, PUSH_RETRYABLE_ERROR, PUSH_SUBSYSTEM_UP, ControlType } from './constants';
15
14
  import { STREAMING_FALLBACK, STREAMING_REFRESH_TOKEN, STREAMING_CONNECTING, STREAMING_DISABLED, ERROR_STREAMING_AUTH, STREAMING_DISCONNECTING, STREAMING_RECONNECT, STREAMING_PARSING_MEMBERSHIPS_UPDATE } from '../../logger/constants';
16
15
  import { IMembershipMSUpdateData, IMembershipLSUpdateData, KeyList, UpdateStrategy } from './SSEHandler/types';
17
16
  import { getDelay, isInBitmap, parseBitmap, parseCompressedData } from './parseUtils';
18
17
  import { Hash64, hash64 } from '../../utils/murmur3/murmur3_64';
19
- import { IJwtCredentialV2 } from './AuthClient/types';
18
+ import { IJwtCredential } from './AuthClient/types';
20
19
  import { TOKEN_REFRESH, AUTH_REJECTION } from '../../utils/constants';
21
20
  import { ISdkFactoryContextSync } from '../../sdkFactory/types';
22
21
 
@@ -45,7 +44,6 @@ export function pushManagerFactory(
45
44
  log.warn(STREAMING_FALLBACK, [e]);
46
45
  return;
47
46
  }
48
- const authenticate = authenticateFactory(serviceApi.fetchAuth);
49
47
 
50
48
  // init feedback loop
51
49
  const pushEmitter = new platform.EventEmitter() as IPushEventEmitter;
@@ -58,8 +56,6 @@ export function pushManagerFactory(
58
56
  // For server-side we pass the segmentsSyncTask, used by DefinitionsUpdateWorker to fetch new segments
59
57
  const definitionsUpdateWorker = DefinitionsUpdateWorker(log, storage, pollingManager.definitionsSyncTask, readiness.definitions, telemetryTracker);
60
58
 
61
- // [Only for client-side] map of hashes to user keys, to dispatch membership update events to the corresponding MySegmentsUpdateWorker
62
- const userKeyHashes: Record<string, string> = {};
63
59
  // [Only for client-side] map of user keys to their corresponding hash64 and MySegmentsUpdateWorkers.
64
60
  // Hash64 is used to process membership update events and dispatch actions to the corresponding MySegmentsUpdateWorker.
65
61
  const clients: Record<string, { hash64: Hash64, worker: ReturnType<typeof MySegmentsUpdateWorker> }> = {};
@@ -81,7 +77,7 @@ export function pushManagerFactory(
81
77
  let timeoutIdTokenRefresh: ReturnType<typeof setTimeout>;
82
78
  let timeoutIdSseOpen: ReturnType<typeof setTimeout>;
83
79
 
84
- function scheduleTokenRefreshAndSse(authData: IJwtCredentialV2) {
80
+ function scheduleTokenRefreshAndSse(authData: IJwtCredential) {
85
81
  // clear scheduled tasks if exist
86
82
  if (timeoutIdTokenRefresh) clearTimeout(timeoutIdTokenRefresh);
87
83
  if (timeoutIdSseOpen) clearTimeout(timeoutIdSseOpen);
@@ -113,41 +109,37 @@ export function pushManagerFactory(
113
109
  disconnected = false;
114
110
 
115
111
  const userKeys = userKey ? Object.keys(clients) : undefined;
116
- authenticate(userKeys).then(
117
- function (authData) {
118
- if (disconnected) return;
119
-
120
- // 'pushEnabled: false' is handled as a PUSH_NON_RETRYABLE_ERROR instead of PUSH_SUBSYSTEM_DOWN, in order to
121
- // close the sseClient in case the org has been bloqued while the instance was connected to streaming
122
- if (!authData.pushEnabled) {
123
- log.info(STREAMING_DISABLED);
124
- pushEmitter.emit(PUSH_NON_RETRYABLE_ERROR);
125
- return;
126
- }
127
-
128
- // [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
129
- if (userKeys && userKeys.length < Object.keys(clients).length) return;
112
+ serviceApi.fetchAuth(userKeys).then((authData) => {
113
+ if (disconnected) return;
130
114
 
131
- // Schedule SSE connection and refresh token
132
- scheduleTokenRefreshAndSse(authData);
115
+ // 'pushEnabled: false' is handled as a PUSH_NON_RETRYABLE_ERROR instead of PUSH_SUBSYSTEM_DOWN, in order to
116
+ // close the sseClient in case the org has been bloqued while the instance was connected to streaming
117
+ if (!authData.pushEnabled) {
118
+ log.info(STREAMING_DISABLED);
119
+ pushEmitter.emit(PUSH_NON_RETRYABLE_ERROR);
120
+ return;
133
121
  }
134
- ).catch(
135
- function (error) {
136
- if (disconnected) return;
137
122
 
138
- log.error(ERROR_STREAMING_AUTH, [error.message]);
123
+ // [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
124
+ if (userKeys && userKeys.length < Object.keys(clients).length) return;
139
125
 
140
- // Handle 4XX HTTP errors: 401 (invalid SDK Key) or 400 (using incorrect SDK Key, i.e., client-side SDK Key on server-side)
141
- if (error.statusCode >= 400 && error.statusCode < 500) {
142
- telemetryTracker.streamingEvent(AUTH_REJECTION);
143
- pushEmitter.emit(PUSH_NON_RETRYABLE_ERROR);
144
- return;
145
- }
126
+ // Schedule SSE connection and refresh token
127
+ scheduleTokenRefreshAndSse(authData);
128
+ }).catch((error) => {
129
+ if (disconnected) return;
146
130
 
147
- // Handle other HTTP and network errors as recoverable errors
148
- pushEmitter.emit(PUSH_RETRYABLE_ERROR);
131
+ log.error(ERROR_STREAMING_AUTH, [error.message]);
132
+
133
+ // Handle 4XX HTTP errors: 401 (invalid SDK Key) or 400 (using incorrect SDK Key, i.e., client-side SDK Key on server-side)
134
+ if (error.statusCode >= 400 && error.statusCode < 500) {
135
+ telemetryTracker.streamingEvent(AUTH_REJECTION);
136
+ pushEmitter.emit(PUSH_NON_RETRYABLE_ERROR);
137
+ return;
149
138
  }
150
- );
139
+
140
+ // Handle other HTTP and network errors as recoverable errors
141
+ pushEmitter.emit(PUSH_RETRYABLE_ERROR);
142
+ });
151
143
  }
152
144
 
153
145
  // close SSE connection and cancel scheduled tasks
@@ -218,8 +210,11 @@ export function pushManagerFactory(
218
210
 
219
211
  /** Functions related to synchronization (Queues and Workers in the spec) */
220
212
 
213
+ // Some notification types are specific to the Feature flags or Configs SDKs. Their handlers are
214
+ // registered unconditionally, since the SDK only subscribes to the channels of its own entities.
221
215
  pushEmitter.on(SPLIT_KILL, definitionsUpdateWorker.killDefinition);
222
216
  pushEmitter.on(SPLIT_UPDATE, definitionsUpdateWorker.put);
217
+ pushEmitter.on(CONFIG_UPDATE, definitionsUpdateWorker.put);
223
218
  pushEmitter.on(RB_SEGMENT_UPDATE, definitionsUpdateWorker.put);
224
219
 
225
220
  function handleMySegmentsUpdate(parsedData: IMembershipMSUpdateData | IMembershipLSUpdateData) {
@@ -324,10 +319,7 @@ export function pushManagerFactory(
324
319
 
325
320
  // [Only for client-side]
326
321
  add(userKey: string, mySegmentsSyncTask: IMySegmentsSyncTask) {
327
- const hash = hashUserKey(userKey);
328
-
329
- if (!userKeyHashes[hash]) {
330
- userKeyHashes[hash] = userKey;
322
+ if (!clients[userKey]) {
331
323
  clients[userKey] = {
332
324
  hash64: hash64(userKey),
333
325
  worker: MySegmentsUpdateWorker(log, storage, mySegmentsSyncTask, telemetryTracker)
@@ -349,8 +341,6 @@ export function pushManagerFactory(
349
341
  },
350
342
  // [Only for client-side]
351
343
  remove(userKey: string) {
352
- const hash = hashUserKey(userKey);
353
- delete userKeyHashes[hash];
354
344
  delete clients[userKey];
355
345
  }
356
346
  }
@@ -16,19 +16,20 @@ export type MEMBERSHIPS_LS_UPDATE = 'MEMBERSHIPS_LS_UPDATE';
16
16
  export type SEGMENT_UPDATE = 'SEGMENT_UPDATE';
17
17
  export type SPLIT_KILL = 'SPLIT_KILL';
18
18
  export type SPLIT_UPDATE = 'SPLIT_UPDATE';
19
+ export type CONFIG_UPDATE = 'CONFIG_UPDATE';
19
20
  export type RB_SEGMENT_UPDATE = 'RB_SEGMENT_UPDATE';
20
21
 
21
22
  // Control-type push notifications, handled by NotificationKeeper
22
23
  export type CONTROL = 'CONTROL';
23
24
  export type OCCUPANCY = 'OCCUPANCY';
24
25
 
25
- export type IPushEvent = PUSH_SUBSYSTEM_UP | PUSH_SUBSYSTEM_DOWN | PUSH_NON_RETRYABLE_ERROR | PUSH_RETRYABLE_ERROR | MEMBERSHIPS_MS_UPDATE | MEMBERSHIPS_LS_UPDATE | SEGMENT_UPDATE | SPLIT_UPDATE | SPLIT_KILL | RB_SEGMENT_UPDATE | ControlType.STREAMING_RESET
26
+ export type IPushEvent = PUSH_SUBSYSTEM_UP | PUSH_SUBSYSTEM_DOWN | PUSH_NON_RETRYABLE_ERROR | PUSH_RETRYABLE_ERROR | MEMBERSHIPS_MS_UPDATE | MEMBERSHIPS_LS_UPDATE | SEGMENT_UPDATE | SPLIT_UPDATE | CONFIG_UPDATE | SPLIT_KILL | RB_SEGMENT_UPDATE | ControlType.STREAMING_RESET
26
27
 
27
28
  type IParsedData<T extends IPushEvent> =
28
29
  T extends MEMBERSHIPS_MS_UPDATE ? IMembershipMSUpdateData :
29
30
  T extends MEMBERSHIPS_LS_UPDATE ? IMembershipLSUpdateData :
30
31
  T extends SEGMENT_UPDATE ? ISegmentUpdateData :
31
- T extends SPLIT_UPDATE | RB_SEGMENT_UPDATE ? ISplitUpdateData :
32
+ T extends SPLIT_UPDATE | CONFIG_UPDATE | RB_SEGMENT_UPDATE ? ISplitUpdateData :
32
33
  T extends SPLIT_KILL ? ISplitKillData : INotificationData;
33
34
 
34
35
  /**