Skip to content

Release v2.3.0 #411

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 30 additions & 24 deletions CHANGES.txt

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@splitsoftware/splitio-commons",
"version": "2.2.0",
"version": "2.3.0",
"description": "Split JavaScript SDK common components",
"main": "cjs/index.js",
"module": "esm/index.js",
Expand Down
4 changes: 3 additions & 1 deletion src/consent/__tests__/sdkUserConsent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ test('createUserConsentAPI', () => {
const syncManager = { submitterManager: syncTaskFactory() };
const storage = {
events: { clear: jest.fn() },
impressions: { clear: jest.fn() }
impressions: { clear: jest.fn() },
impressionCounts: { clear: jest.fn() },
uniqueKeys: { clear: jest.fn() }
};

// @ts-ignore
Expand Down
5 changes: 3 additions & 2 deletions src/consent/sdkUserConsent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const ConsentStatus = {
* The public user consent API exposed via SplitFactory, used to control if the SDK tracks and sends impressions and events or not.
*/
export function createUserConsentAPI(params: ISdkFactoryContext) {
const { settings, settings: { log }, syncManager, storage: { events, impressions, impressionCounts } } = params;
const { settings, settings: { log }, syncManager, storage: { events, impressions, impressionCounts, uniqueKeys } } = params;

if (!isConsentGranted(settings)) log.info(USER_CONSENT_INITIAL, [settings.userConsent]);

Expand All @@ -41,7 +41,8 @@ export function createUserConsentAPI(params: ISdkFactoryContext) {
// @ts-ignore, clear method is present in storage for standalone and partial consumer mode
if (events.clear) events.clear(); // @ts-ignore
if (impressions.clear) impressions.clear(); // @ts-ignore
if (impressionCounts && impressionCounts.clear) impressionCounts.clear();
if (impressionCounts.clear) impressionCounts.clear(); // @ts-ignore
if (uniqueKeys.clear) uniqueKeys.clear();
}
} else {
log.info(USER_CONSENT_NOT_UPDATED, [newConsentStatus]);
Expand Down
32 changes: 19 additions & 13 deletions src/listeners/__tests__/browser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BrowserSignalListener } from '../browser';
import { IEventsCacheSync, IImpressionCountsCacheSync, IImpressionsCacheSync, IStorageSync, ITelemetryCacheSync, IUniqueKeysCacheBase } from '../../storages/types';
import { ISplitApi } from '../../services/types';
import { fullSettings } from '../../utils/settingsValidation/__tests__/settings.mocks';

Expand Down Expand Up @@ -30,42 +29,48 @@ const fakeUniqueKeys = {
};

// Storage with impressionsCount and telemetry cache
const fakeStorageOptimized = { // @ts-expect-error
const fakeStorageOptimized = {
impressions: {
isEmpty: jest.fn(),
pop() {
return [fakeImpression];
}
} as IImpressionsCacheSync, // @ts-expect-error
},
events: {
isEmpty: jest.fn(),
pop() {
return [fakeEvent];
}
} as IEventsCacheSync, // @ts-expect-error
},
impressionCounts: {
isEmpty: jest.fn(),
pop() {
return fakeImpressionCounts;
}
} as IImpressionCountsCacheSync, // @ts-expect-error
},
uniqueKeys: {
isEmpty: jest.fn(),
pop() {
return fakeUniqueKeys;
}
} as IUniqueKeysCacheBase, // @ts-expect-error
},
telemetry: {
isEmpty: jest.fn(),
pop() {
return 'fake telemetry';
}
} as ITelemetryCacheSync
}
};

const fakeStorageDebug = {
impressions: fakeStorageOptimized.impressions,
events: fakeStorageOptimized.events
events: fakeStorageOptimized.events,
impressionCounts: {
isEmpty: jest.fn(() => true)
},
uniqueKeys: {
isEmpty: jest.fn(() => true)
}
};

// @ts-expect-error
Expand Down Expand Up @@ -155,7 +160,8 @@ function assertStop(listener: BrowserSignalListener) {

test('Browser JS listener / consumer mode', () => {
// No SyncManager ==> consumer mode
const listener = new BrowserSignalListener(undefined, fullSettings, fakeStorageOptimized as IStorageSync, fakeSplitApi);
// @ts-expect-error
const listener = new BrowserSignalListener(undefined, fullSettings, fakeStorageOptimized, fakeSplitApi);

listener.start();
assertStart(listener);
Expand All @@ -180,7 +186,7 @@ test('Browser JS listener / standalone mode / Impressions optimized mode with te
const syncManagerMock = {};

// @ts-expect-error
const listener = new BrowserSignalListener(syncManagerMock, fullSettings, fakeStorageOptimized as IStorageSync, fakeSplitApi);
const listener = new BrowserSignalListener(syncManagerMock, fullSettings, fakeStorageOptimized, fakeSplitApi);

listener.start();
assertStart(listener);
Expand All @@ -205,7 +211,7 @@ test('Browser JS listener / standalone mode / Impressions debug mode', () => {
const syncManagerMock = {};

// @ts-expect-error
const listener = new BrowserSignalListener(syncManagerMock, fullSettings, fakeStorageDebug as IStorageSync, fakeSplitApi);
const listener = new BrowserSignalListener(syncManagerMock, fullSettings, fakeStorageDebug, fakeSplitApi);

listener.start();
assertStart(listener);
Expand Down Expand Up @@ -234,7 +240,7 @@ test('Browser JS listener / standalone mode / Impressions debug mode', () => {
test('Browser JS listener / standalone mode / Fallback to regular Fetch transport', () => {

function runBrowserListener() { // @ts-expect-error
const listener = new BrowserSignalListener({}, fullSettings, fakeStorageDebug as IStorageSync, fakeSplitApi);
const listener = new BrowserSignalListener({}, fullSettings, fakeStorageDebug, fakeSplitApi);
listener.start();
// Trigger data flush
triggerEvent(VISIBILITYCHANGE_EVENT, 'hidden');
Expand Down Expand Up @@ -270,7 +276,7 @@ test('Browser JS listener / standalone mode / user consent status', () => {
const settings = { ...fullSettings };

// @ts-expect-error
const listener = new BrowserSignalListener(syncManagerMock, settings, fakeStorageOptimized as IStorageSync, fakeSplitApi);
const listener = new BrowserSignalListener(syncManagerMock, settings, fakeStorageOptimized, fakeSplitApi);

listener.start();

Expand Down
5 changes: 2 additions & 3 deletions src/listeners/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,8 @@ export class BrowserSignalListener implements ISignalListener {

this._flushData(events + '/testImpressions/beacon', this.storage.impressions, this.serviceApi.postTestImpressionsBulk, this.fromImpressionsCollector, extraMetadata);
this._flushData(events + '/events/beacon', this.storage.events, this.serviceApi.postEventsBulk);
if (this.storage.impressionCounts) this._flushData(events + '/testImpressions/count/beacon', this.storage.impressionCounts, this.serviceApi.postTestImpressionsCount, fromImpressionCountsCollector);
// @ts-ignore
if (this.storage.uniqueKeys) this._flushData(telemetry + '/v1/keys/cs/beacon', this.storage.uniqueKeys, this.serviceApi.postUniqueKeysBulkCs);
this._flushData(events + '/testImpressions/count/beacon', this.storage.impressionCounts, this.serviceApi.postTestImpressionsCount, fromImpressionCountsCollector);
this._flushData(telemetry + '/v1/keys/cs/beacon', this.storage.uniqueKeys, this.serviceApi.postUniqueKeysBulkCs);
}

// Flush telemetry data
Expand Down
5 changes: 4 additions & 1 deletion src/sdkFactory/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import SplitIO from '../../types/splitio';
import { validateAndTrackApiKey } from '../utils/inputValidation/apiKey';
import { createLoggerAPI } from '../logger/sdkLogger';
import { NEW_FACTORY, RETRIEVE_MANAGER } from '../logger/constants';
import { SDK_SPLITS_ARRIVED, SDK_SEGMENTS_ARRIVED } from '../readiness/constants';
import { SDK_SPLITS_ARRIVED, SDK_SEGMENTS_ARRIVED, SDK_SPLITS_CACHE_LOADED } from '../readiness/constants';
import { objectAssign } from '../utils/lang/objectAssign';
import { strategyDebugFactory } from '../trackers/strategy/strategyDebug';
import { strategyOptimizedFactory } from '../trackers/strategy/strategyOptimized';
Expand Down Expand Up @@ -52,6 +52,9 @@ export function sdkFactory(params: ISdkFactoryParams): SplitIO.ISDK | SplitIO.IA
readiness.splits.emit(SDK_SPLITS_ARRIVED);
readiness.segments.emit(SDK_SEGMENTS_ARRIVED);
},
onReadyFromCacheCb: () => {
readiness.splits.emit(SDK_SPLITS_CACHE_LOADED);
}
});
// @TODO add support for dataloader: `if (params.dataLoader) params.dataLoader(storage);`
const clients: Record<string, SplitIO.IBasicClient> = {};
Expand Down
2 changes: 1 addition & 1 deletion src/sdkFactory/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export interface IPlatform {
/**
* If provided, it is used to pass additional options to fetch and eventsource calls.
*/
getOptions?: (settings: ISettings) => object
getOptions?: (settings: ISettings) => (object | undefined)
/**
* If provided, it is used to retrieve the EventSource constructor for streaming support.
*/
Expand Down
5 changes: 4 additions & 1 deletion src/services/splitHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { ERROR_HTTP, ERROR_CLIENT_CANNOT_GET_READY } from '../logger/constants';
import { ISettings } from '../types';
import { IPlatform } from '../sdkFactory/types';
import { decorateHeaders, removeNonISO88591 } from './decorateHeaders';
import { timeout } from '../utils/promise/timeout';

const PENDING_FETCH_ERROR_TIMEOUT = 100;
const messageNoFetch = 'Global fetch API is not available.';

/**
Expand Down Expand Up @@ -45,7 +47,8 @@ export function splitHttpClientFactory(settings: ISettings, { getOptions, getFet
// https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#Checking_that_the_fetch_was_successful
.then(response => {
if (!response.ok) {
return response.text().then(message => Promise.reject({ response, message }));
// `text()` promise might not settle in some fetch implementations and cases (e.g. no content)
return timeout(PENDING_FETCH_ERROR_TIMEOUT, response.text()).then(message => Promise.reject({ response, message }), () => Promise.reject({ response }));
}
latencyTracker();
return response;
Expand Down
23 changes: 16 additions & 7 deletions src/storages/inRedis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,37 @@ export interface InRedisStorageOptions {
options?: Record<string, any>
}

let RD: typeof RedisAdapter | undefined;

try {
// Using `require` to prevent error when bundling or importing the SDK in a .mjs file, since ioredis is a CommonJS module.
// Redis storage is not supported with .mjs files.
RD = require('./RedisAdapter').RedisAdapter;
} catch (error) { /* empty */ }

/**
* InRedis storage factory for consumer server-side SplitFactory, that uses `Ioredis` Redis client for Node.js
* @see {@link https://www.npmjs.com/package/ioredis}
*/
export function InRedisStorage(options: InRedisStorageOptions = {}): IStorageAsyncFactory {

// Lazy loading to prevent error when bundling or importing the SDK in a .mjs file, since ioredis is a CommonJS module.
// Redis storage is not supported with .mjs files.
const RD = require('./RedisAdapter').RedisAdapter;

const prefix = validatePrefix(options.prefix);

function InRedisStorageFactory(params: IStorageFactoryParams): IStorageAsync {
const { onReadyCb, settings, settings: { log } } = params;
if (!RD) throw new Error('The SDK Redis storage is unavailable. Make sure your runtime environment supports CommonJS (`require`) so the `ioredis` dependency can be imported.');

const { onReadyFromCacheCb, onReadyCb, settings, settings: { log } } = params;
const metadata = metadataBuilder(settings);
const keys = new KeyBuilderSS(prefix, metadata);
const redisClient: RedisAdapter = new RD(log, options.options || {});
const redisClient = new RD(log, options.options || {});
const telemetry = new TelemetryCacheInRedis(log, keys, redisClient);
const impressionCountsCache = new ImpressionCountsCacheInRedis(log, keys.buildImpressionsCountKey(), redisClient);
const uniqueKeysCache = new UniqueKeysCacheInRedis(log, keys.buildUniqueKeysKey(), redisClient);

// subscription to Redis connect event in order to emit SDK_READY event on consumer mode
// RedisAdapter lets queue operations before connected
onReadyFromCacheCb();

// Subscription to Redis connect event in order to emit SDK_READY event on consumer mode
redisClient.on('connect', () => {
onReadyCb();
impressionCountsCache.start();
Expand Down
1 change: 1 addition & 0 deletions src/storages/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ export interface IStorageFactoryParams {
* It is meant for emitting SDK_READY event in consumer mode, and waiting before using the storage in the synchronizer.
*/
onReadyCb: (error?: any) => void,
onReadyFromCacheCb: () => void,
}


Expand Down