Skip to content

Update Redis require #407

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 6 commits into from
May 9, 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
5 changes: 5 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2.2.1 (May XX, 2025)
- Updated the Redis storage to:
- Avoid lazy require of the `ioredis` dependency when the SDK is initialized, and
- Flag the SDK as ready from cache immediately to allow queueing feature flag evaluations before SDK_READY event is emitted (Reverted in v1.7.0).

2.2.0 (March 28, 2025)
- Added a new optional argument to the client `getTreatment` methods to allow passing additional evaluation options, such as a map of properties to append to the generated impressions sent to Split backend. Read more in our docs.
- Added two new configuration options for the SDK storage in browsers when using storage type `LOCALSTORAGE`:
Expand Down
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.2.1-rc.3",
"description": "Split JavaScript SDK common components",
"main": "cjs/index.js",
"module": "esm/index.js",
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
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