@splitsoftware/splitio-commons 1.0.1-rc.6 → 1.2.1-rc.0

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 (72) hide show
  1. package/CHANGES.txt +13 -3
  2. package/LICENSE +1 -1
  3. package/README.md +1 -1
  4. package/cjs/logger/messages/info.js +3 -3
  5. package/cjs/sdkClient/client.js +2 -1
  6. package/cjs/sdkClient/clientAttributesDecoration.js +108 -0
  7. package/cjs/sdkClient/clientCS.js +10 -7
  8. package/cjs/sdkClient/sdkClientMethodCS.js +2 -2
  9. package/cjs/sdkClient/sdkClientMethodCSWithTT.js +2 -2
  10. package/cjs/sdkFactory/index.js +2 -2
  11. package/cjs/services/splitHttpClient.js +1 -1
  12. package/cjs/storages/inMemory/AttributesCacheInMemory.js +70 -0
  13. package/cjs/sync/polling/updaters/splitChangesUpdater.js +6 -3
  14. package/cjs/sync/streaming/AuthClient/index.js +1 -2
  15. package/cjs/sync/streaming/pushManager.js +26 -22
  16. package/cjs/sync/syncManagerOnline.js +12 -7
  17. package/cjs/utils/inputValidation/attribute.js +20 -0
  18. package/cjs/utils/inputValidation/attributes.js +13 -1
  19. package/cjs/utils/murmur3/legacy.js +44 -0
  20. package/cjs/utils/promise/timeout.js +1 -1
  21. package/cjs/utils/settingsValidation/index.js +1 -1
  22. package/esm/logger/messages/info.js +3 -3
  23. package/esm/sdkClient/client.js +2 -1
  24. package/esm/sdkClient/clientAttributesDecoration.js +104 -0
  25. package/esm/sdkClient/clientCS.js +10 -7
  26. package/esm/sdkClient/sdkClientMethodCS.js +2 -2
  27. package/esm/sdkClient/sdkClientMethodCSWithTT.js +2 -2
  28. package/esm/sdkFactory/index.js +2 -2
  29. package/esm/services/splitHttpClient.js +1 -1
  30. package/esm/storages/inMemory/AttributesCacheInMemory.js +67 -0
  31. package/esm/sync/polling/updaters/splitChangesUpdater.js +6 -3
  32. package/esm/sync/streaming/AuthClient/index.js +1 -2
  33. package/esm/sync/streaming/pushManager.js +26 -22
  34. package/esm/sync/syncManagerOnline.js +12 -7
  35. package/esm/utils/inputValidation/attribute.js +16 -0
  36. package/esm/utils/inputValidation/attributes.js +11 -0
  37. package/esm/utils/murmur3/legacy.js +39 -0
  38. package/esm/utils/promise/timeout.js +1 -1
  39. package/esm/utils/settingsValidation/index.js +1 -1
  40. package/package.json +2 -2
  41. package/src/logger/messages/info.ts +3 -3
  42. package/src/sdkClient/client.ts +2 -1
  43. package/src/sdkClient/clientAttributesDecoration.ts +122 -0
  44. package/src/sdkClient/clientCS.ts +14 -7
  45. package/src/sdkClient/sdkClientMethodCS.ts +2 -0
  46. package/src/sdkClient/sdkClientMethodCSWithTT.ts +2 -0
  47. package/src/sdkFactory/index.ts +2 -2
  48. package/src/services/splitHttpClient.ts +1 -1
  49. package/src/storages/inMemory/AttributesCacheInMemory.ts +73 -0
  50. package/src/sync/polling/updaters/splitChangesUpdater.ts +5 -2
  51. package/src/sync/streaming/AuthClient/index.ts +1 -2
  52. package/src/sync/streaming/pushManager.ts +26 -26
  53. package/src/sync/syncManagerOnline.ts +10 -6
  54. package/src/types.ts +43 -0
  55. package/src/utils/inputValidation/attribute.ts +21 -0
  56. package/src/utils/inputValidation/attributes.ts +14 -0
  57. package/src/utils/murmur3/legacy.ts +48 -0
  58. package/src/utils/promise/timeout.ts +1 -1
  59. package/src/utils/settingsValidation/index.ts +1 -1
  60. package/types/sdkClient/clientAttributesDecoration.d.ts +51 -0
  61. package/types/sdkClient/clientCS.d.ts +2 -1
  62. package/types/storages/inMemory/AttributesCacheInMemory.d.ts +43 -0
  63. package/types/storages/inMemory/TelemetryCacheInMemory.d.ts +51 -0
  64. package/types/storages/inRedis/TelemetryCacheInRedis.d.ts +0 -0
  65. package/types/storages/pluggable/TelemetryCachePluggable.d.ts +2 -0
  66. package/types/sync/submitters/telemetrySyncTask.d.ts +17 -0
  67. package/types/trackers/telemetryRecorder.d.ts +0 -0
  68. package/types/types.d.ts +40 -0
  69. package/types/utils/EventEmitter.d.ts +4 -0
  70. package/types/utils/inputValidation/attribute.d.ts +2 -0
  71. package/types/utils/inputValidation/attributes.d.ts +1 -0
  72. package/types/utils/murmur3/legacy.d.ts +2 -0
@@ -0,0 +1,51 @@
1
+ import { SplitIO } from '../types';
2
+ import { ILogger } from '../logger/types';
3
+ /**
4
+ * Add in memory attributes storage methods and combine them with any attribute received from the getTreatment/s call
5
+ */
6
+ export declare function clientAttributesDecoration<TClient extends SplitIO.IClient | SplitIO.IAsyncClient>(log: ILogger, client: TClient): TClient & {
7
+ getTreatment: (maybeKey: SplitIO.SplitKey, maybeSplit: string, maybeAttributes?: SplitIO.Attributes | undefined) => string | SplitIO.AsyncTreatment;
8
+ getTreatmentWithConfig: (maybeKey: SplitIO.SplitKey, maybeSplit: string, maybeAttributes?: SplitIO.Attributes | undefined) => SplitIO.TreatmentWithConfig | SplitIO.AsyncTreatmentWithConfig;
9
+ getTreatments: (maybeKey: SplitIO.SplitKey, maybeSplits: string[], maybeAttributes?: SplitIO.Attributes | undefined) => SplitIO.Treatments | SplitIO.AsyncTreatments;
10
+ getTreatmentsWithConfig: (maybeKey: SplitIO.SplitKey, maybeSplits: string[], maybeAttributes?: SplitIO.Attributes | undefined) => SplitIO.TreatmentsWithConfig | SplitIO.AsyncTreatmentsWithConfig;
11
+ track: (maybeKey: SplitIO.SplitKey, maybeTT: string, maybeEvent: string, maybeEventValue?: number | undefined, maybeProperties?: SplitIO.Properties | undefined) => import("../dtos/types").MaybeThenable<boolean>;
12
+ /**
13
+ * Add an attribute to client's in memory attributes storage
14
+ *
15
+ * @param {string} attributeName Attrinute name
16
+ * @param {string, number, boolean, list} attributeValue Attribute value
17
+ * @returns {boolean} true if the attribute was stored and false otherways
18
+ */
19
+ setAttribute(attributeName: string, attributeValue: Object): boolean;
20
+ /**
21
+ * Returns the attribute with the given key
22
+ *
23
+ * @param {string} attributeName Attribute name
24
+ * @returns {Object} Attribute with the given key
25
+ */
26
+ getAttribute(attributeName: string): Object;
27
+ /**
28
+ * Add to client's in memory attributes storage the attributes in 'attributes'
29
+ *
30
+ * @param {Object} attributes Object with attributes to store
31
+ * @returns true if attributes were stored an false otherways
32
+ */
33
+ setAttributes(attributes: Record<string, Object>): boolean;
34
+ /**
35
+ * Return all the attributes stored in client's in memory attributes storage
36
+ *
37
+ * @returns {Object} returns all the stored attributes
38
+ */
39
+ getAttributes(): Record<string, Object>;
40
+ /**
41
+ * Removes from client's in memory attributes storage the attribute with the given key
42
+ *
43
+ * @param {string} attributeName
44
+ * @returns {boolean} true if attribute was removed and false otherways
45
+ */
46
+ removeAttribute(attributeName: string): boolean;
47
+ /**
48
+ * Remove all the stored attributes in the client's in memory attribute storage
49
+ */
50
+ clearAttributes(): boolean;
51
+ };
@@ -1,3 +1,4 @@
1
+ import { ILogger } from '../logger/types';
1
2
  import { SplitIO } from '../types';
2
3
  /**
3
4
  * Decorator that binds a key and (optionally) a traffic type to client methods
@@ -6,4 +7,4 @@ import { SplitIO } from '../types';
6
7
  * @param key validated split key
7
8
  * @param trafficType validated traffic type
8
9
  */
9
- export declare function clientCSDecorator(client: SplitIO.IClient, key: SplitIO.SplitKey, trafficType?: string): SplitIO.ICsClient;
10
+ export declare function clientCSDecorator(log: ILogger, client: SplitIO.IClient, key: SplitIO.SplitKey, trafficType?: string): SplitIO.ICsClient;
@@ -0,0 +1,43 @@
1
+ export declare class AttributesCacheInMemory {
2
+ private attributesCache;
3
+ /**
4
+ * Create or update the value for the given attribute
5
+ *
6
+ * @param {string} attributeName attribute name
7
+ * @param {Object} attributeValue attribute value
8
+ * @returns {boolean} the attribute was stored
9
+ */
10
+ setAttribute(attributeName: string, attributeValue: Object): boolean;
11
+ /**
12
+ * Retrieves the value of a given attribute
13
+ *
14
+ * @param {string} attributeName attribute name
15
+ * @returns {Object?} stored attribute value
16
+ */
17
+ getAttribute(attributeName: string): Object;
18
+ /**
19
+ * Create or update all the given attributes
20
+ *
21
+ * @param {[string, Object]} attributes attributes to create or update
22
+ * @returns {boolean} attributes were stored
23
+ */
24
+ setAttributes(attributes: Record<string, Object>): boolean;
25
+ /**
26
+ * Retrieve the full attributes map
27
+ *
28
+ * @returns {Map<string, Object>} stored attributes
29
+ */
30
+ getAll(): Record<string, Object>;
31
+ /**
32
+ * Removes a given attribute from the map
33
+ *
34
+ * @param {string} attributeName attribute to remove
35
+ * @returns {boolean} attribute removed
36
+ */
37
+ removeAttribute(attributeName: string): boolean;
38
+ /**
39
+ * Clears all attributes stored in the SDK
40
+ *
41
+ */
42
+ clear(): boolean;
43
+ }
@@ -0,0 +1,51 @@
1
+ import { ImpressionDataType, EventDataType, StreamingEvent, Method, OperationType } from '../../sync/submitters/types';
2
+ import { TelemetryCacheSync } from '../types';
3
+ export declare class TelemetryCacheInMemory implements TelemetryCacheSync {
4
+ private timeUntilReady?;
5
+ getTimeUntilReady(): number | undefined;
6
+ recordTimeUntilReady(ms: number): void;
7
+ private timeUntilReadyFromCache?;
8
+ getTimeUntilReadyFromCache(): number | undefined;
9
+ recordTimeUntilReadyFromCache(ms: number): void;
10
+ private notReadyUsage;
11
+ getNonReadyUsage(): number;
12
+ recordNonReadyUsage(): void;
13
+ private impressionStats;
14
+ getImpressionStats(type: ImpressionDataType): number;
15
+ recordImpressionStats(type: ImpressionDataType, count: number): void;
16
+ private eventStats;
17
+ getEventStats(type: EventDataType): number;
18
+ recordEventStats(type: EventDataType, count: number): void;
19
+ private lastSync;
20
+ getLastSynchronization(): Record<OperationType, number | undefined>;
21
+ recordSuccessfulSync(resource: OperationType, timeMs: number): void;
22
+ private httpErrors;
23
+ popHttpErrors(): Record<OperationType, {
24
+ [statusCode: string]: number;
25
+ }>;
26
+ recordSyncError(resource: OperationType, status: number): void;
27
+ private httpLatencies;
28
+ popHttpLatencies(): Record<OperationType, number[]>;
29
+ recordSyncLatency(resource: OperationType, latencyMs: number): void;
30
+ private authRejections;
31
+ popAuthRejections(): number;
32
+ recordAuthRejections(): void;
33
+ private tokenRefreshes;
34
+ popTokenRefreshes(): number;
35
+ recordTokenRefreshes(): void;
36
+ private streamingEvents;
37
+ popStreamingEvents(): StreamingEvent[];
38
+ recordStreamingEvents(streamingEvent: StreamingEvent): void;
39
+ private tags;
40
+ popTags(): string[];
41
+ addTag(tag: string): void;
42
+ private sessionLength?;
43
+ getSessionLength(): number | undefined;
44
+ recordSessionLength(ms: number): void;
45
+ private exceptions;
46
+ popExceptions(): Record<Method, number>;
47
+ recordException(method: Method): void;
48
+ private latencies;
49
+ popLatencies(): Record<Method, number[]>;
50
+ recordLatency(method: Method, latencyMs: number): void;
51
+ }
@@ -0,0 +1,2 @@
1
+ export declare class TelemetryCachePluggable {
2
+ }
@@ -0,0 +1,17 @@
1
+ import { ISyncTask, ITimeTracker } from '../types';
2
+ import { IPostMetricsUsage } from '../../services/types';
3
+ import { IStorageSync, TelemetryCacheSync } from '../../storages/types';
4
+ import { TelemetryStatsPayload } from './types';
5
+ import { ILogger } from '../../logger/types';
6
+ /**
7
+ * Converts `impressions` data from cache into request payload.
8
+ */
9
+ export declare function telemetryCacheAdapter(telemetryCache: TelemetryCacheSync, storage: IStorageSync): {
10
+ isEmpty(): boolean;
11
+ clear(): void;
12
+ state(): TelemetryStatsPayload;
13
+ };
14
+ /**
15
+ * Sync task that periodically posts impressions data
16
+ */
17
+ export declare function telemetrySyncTaskFactory(log: ILogger, postMetricsUsage: IPostMetricsUsage, telemetryCache: TelemetryCacheSync, telemetryRefreshRate: number, latencyTracker: ITimeTracker, storage: IStorageSync): ISyncTask;
File without changes
package/types/types.d.ts CHANGED
@@ -375,6 +375,7 @@ interface IBasicClient extends IStatusInterface {
375
375
  * @returns {Promise<void>}
376
376
  */
377
377
  destroy(): Promise<void>;
378
+ isBrowserClient: boolean;
378
379
  }
379
380
  /**
380
381
  * Common definitions between SDK instances for different environments interface.
@@ -1108,6 +1109,45 @@ export declare namespace SplitIO {
1108
1109
  * @returns {boolean} Whether the event was added to the queue succesfully or not.
1109
1110
  */
1110
1111
  track(...args: [trafficType: string, eventType: string, value?: number, properties?: Properties] | [eventType: string, value?: number, properties?: Properties]): boolean;
1112
+ /**
1113
+ * Add an attribute to client's in memory attributes storage
1114
+ * @function setAttribute
1115
+ * @param {string} attributeName Attrinute name
1116
+ * @param {string, number, boolean, list} attributeValue Attribute value
1117
+ * @returns {boolean} true if the attribute was stored and false otherways
1118
+ */
1119
+ setAttribute(attributeName: string, attributeValue: Object): boolean;
1120
+ /**
1121
+ * Returns the attribute with the given key
1122
+ * @function getAttribute
1123
+ * @param {string} attributeName Attribute name
1124
+ * @returns {Object} Attribute with the given key
1125
+ */
1126
+ getAttribute(attributeName: string): Object;
1127
+ /**
1128
+ * Add to client's in memory attributes storage the attributes in 'attributes'
1129
+ * @function setAttributes
1130
+ * @param {Object} attributes Object with attributes to store
1131
+ * @returns true if attributes were stored an false otherways
1132
+ */
1133
+ setAttributes(attributes: Record<string, Object>): boolean;
1134
+ /**
1135
+ * Return all the attributes stored in client's in memory attributes storage
1136
+ * @function getAttributes
1137
+ * @returns {Object} returns all the stored attributes
1138
+ */
1139
+ getAttributes(): Record<string, Object>;
1140
+ /**
1141
+ * Removes from client's in memory attributes storage the attribute with the given key
1142
+ * @function removeAttribute
1143
+ * @param {string} attributeName
1144
+ * @returns {boolean} true if attribute was removed and false otherways
1145
+ */
1146
+ removeAttribute(attributeName: string): boolean;
1147
+ /**
1148
+ * Remove all the stored attributes in the client's in memory attribute storage
1149
+ */
1150
+ clearAttributes(): boolean;
1111
1151
  }
1112
1152
  /**
1113
1153
  * Representation of a manager instance with synchronous storage of the SDK.
@@ -0,0 +1,4 @@
1
+ import { IEventEmitter } from '../types';
2
+ export declare const EventEmitter: {
3
+ new (): IEventEmitter;
4
+ };
@@ -0,0 +1,2 @@
1
+ import { ILogger } from '../../logger/types';
2
+ export declare function validateAttribute(log: ILogger, attributeKey: string, attributeValue: Object, method: string): boolean;
@@ -1,3 +1,4 @@
1
1
  import { SplitIO } from '../../types';
2
2
  import { ILogger } from '../../logger/types';
3
3
  export declare function validateAttributes(log: ILogger, maybeAttrs: any, method: string): SplitIO.Attributes | undefined | false;
4
+ export declare function validateAttributesDeep(log: ILogger, maybeAttributes: Record<string, Object>, method: string): boolean;
@@ -0,0 +1,2 @@
1
+ export declare function hash(str: string, seed: number): number;
2
+ export declare function bucket(str: string, seed: number): number;