Skip to content

Commit ac554e2

Browse files
Merge pull request #87 from splitio/user_consent_polishing
User consent polishing
2 parents 582b53a + 8255eae commit ac554e2

6 files changed

Lines changed: 75 additions & 5 deletions

File tree

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@splitsoftware/splitio-commons",
3-
"version": "1.2.1-rc.5",
3+
"version": "1.2.1-rc.7",
44
"description": "Split Javascript SDK common components",
55
"main": "cjs/index.js",
66
"module": "esm/index.js",

src/sync/streaming/SSEClient/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { IEventSourceConstructor } from '../../../services/types';
22
import { ISettings } from '../../../types';
3+
import { isString } from '../../../utils/lang';
34
import { IAuthTokenPushEnabled } from '../AuthClient/types';
45
import { ISSEClient, ISseEventHandler } from './types';
56

@@ -15,7 +16,7 @@ const CONTROL_CHANNEL_REGEX = /^control_/;
1516
*/
1617
function buildSSEHeaders(settings: ISettings) {
1718
const headers: Record<string, string> = {
18-
SplitSDKClientKey: settings.core.authorizationKey.slice(-4),
19+
SplitSDKClientKey: isString(settings.core.authorizationKey) ? settings.core.authorizationKey.slice(-4) : '',
1920
SplitSDKVersion: settings.version,
2021
};
2122

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { eventsSyncTaskFactory } from '../eventsSyncTask';
2+
import { loggerMock } from '../../../logger/__tests__/sdkLogger.mock';
3+
4+
5+
6+
describe('Events submitter (eventsSyncTask)', () => {
7+
8+
let __onFullQueueCb: () => void;
9+
const postEventsBulkMock = jest.fn();
10+
const eventsCacheMock = {
11+
isEmpty: jest.fn(() => true),
12+
setOnFullQueueCb: jest.fn(function (onFullQueueCb) { __onFullQueueCb = onFullQueueCb; })
13+
};
14+
15+
beforeEach(() => {
16+
eventsCacheMock.isEmpty.mockClear();
17+
});
18+
19+
test('with eventsFirstPushWindow', async () => {
20+
const eventsFirstPushWindow = 20; // @ts-ignore
21+
const eventsSubmitter = eventsSyncTaskFactory(loggerMock, postEventsBulkMock, eventsCacheMock, 30000, eventsFirstPushWindow);
22+
23+
eventsSubmitter.start();
24+
expect(eventsSubmitter.isRunning()).toEqual(true); // Submitter should be flagged as running
25+
expect(eventsSubmitter.isExecuting()).toEqual(false); // but not executed immediatelly if there is a push window
26+
expect(eventsCacheMock.isEmpty).not.toBeCalled();
27+
28+
// If queue is full, submitter should be executed
29+
__onFullQueueCb();
30+
expect(eventsSubmitter.isExecuting()).toEqual(true);
31+
expect(eventsCacheMock.isEmpty).toBeCalledTimes(1);
32+
33+
// Await first push window
34+
await new Promise(res => setTimeout(res, eventsFirstPushWindow + 10));
35+
expect(eventsCacheMock.isEmpty).toBeCalledTimes(2); // after the push window, submitter should have been executed
36+
37+
expect(eventsSubmitter.isRunning()).toEqual(true);
38+
eventsSubmitter.stop();
39+
expect(eventsSubmitter.isRunning()).toEqual(false);
40+
});
41+
42+
test('without eventsFirstPushWindow', async () => {
43+
// @ts-ignore
44+
const eventsSubmitter = eventsSyncTaskFactory(loggerMock, postEventsBulkMock, eventsCacheMock, 30000);
45+
46+
eventsSubmitter.start();
47+
expect(eventsSubmitter.isRunning()).toEqual(true); // Submitter should be flagged as running
48+
expect(eventsSubmitter.isExecuting()).toEqual(true); // and executes immediatelly if there isn't a push window
49+
expect(eventsCacheMock.isEmpty).toBeCalledTimes(1);
50+
51+
// If queue is full, submitter should be executed
52+
__onFullQueueCb();
53+
expect(eventsSubmitter.isExecuting()).toEqual(true);
54+
expect(eventsCacheMock.isEmpty).toBeCalledTimes(2);
55+
56+
expect(eventsSubmitter.isRunning()).toEqual(true);
57+
eventsSubmitter.stop();
58+
expect(eventsSubmitter.isRunning()).toEqual(false);
59+
});
60+
61+
});

src/sync/submitters/eventsSyncTask.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,25 @@ export function eventsSyncTaskFactory(
2222
// don't retry events.
2323
const syncTask = submitterSyncTaskFactory(log, postEventsBulk, eventsCache, eventsPushRate, DATA_NAME, latencyTracker);
2424

25-
// Set a timer for the first push of events,
25+
// Set a timer for the first push window of events.
26+
// Not implemented in the base submitter or sync task, since this feature is only used by the events submitter.
2627
if (eventsFirstPushWindow > 0) {
28+
let running = false;
2729
let stopEventPublisherTimeout: ReturnType<typeof setTimeout>;
2830
const originalStart = syncTask.start;
2931
syncTask.start = () => {
32+
running = true;
3033
stopEventPublisherTimeout = setTimeout(originalStart, eventsFirstPushWindow);
3134
};
3235
const originalStop = syncTask.stop;
3336
syncTask.stop = () => {
37+
running = false;
3438
clearTimeout(stopEventPublisherTimeout);
3539
originalStop();
3640
};
41+
syncTask.isRunning = () => {
42+
return running;
43+
};
3744
}
3845

3946
// register events submitter to be executed when events cache is full

src/utils/settingsValidation/consent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { ERROR_INVALID_CONFIG_PARAM } from '../../logger/constants';
22
import { ILogger } from '../../logger/types';
3+
import { ConsentStatus } from '../../types';
34
import { CONSENT_DECLINED, CONSENT_GRANTED, CONSENT_UNKNOWN } from '../constants';
45
import { stringToUpperCase } from '../lang';
56

67
const userConsentValues = [CONSENT_DECLINED, CONSENT_GRANTED, CONSENT_UNKNOWN];
78

8-
export function validateConsent({ userConsent, log }: { userConsent: any, log: ILogger }) {
9+
export function validateConsent({ userConsent, log }: { userConsent?: any, log: ILogger }): ConsentStatus {
910
userConsent = stringToUpperCase(userConsent);
1011

1112
if (userConsentValues.indexOf(userConsent) > -1) return userConsent;

0 commit comments

Comments
 (0)