Skip to content

Commit 2c66a70

Browse files
Merge pull request #407 from splitio/redis_require
Update Redis require
2 parents 1209a36 + ffbd576 commit 2c66a70

File tree

6 files changed

+29
-11
lines changed

6 files changed

+29
-11
lines changed

CHANGES.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2.2.1 (May XX, 2025)
2+
- Updated the Redis storage to:
3+
- Avoid lazy require of the `ioredis` dependency when the SDK is initialized, and
4+
- 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).
5+
16
2.2.0 (March 28, 2025)
27
- 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.
38
- Added two new configuration options for the SDK storage in browsers when using storage type `LOCALSTORAGE`:

package-lock.json

Lines changed: 2 additions & 2 deletions
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": "2.2.0",
3+
"version": "2.2.1-rc.3",
44
"description": "Split JavaScript SDK common components",
55
"main": "cjs/index.js",
66
"module": "esm/index.js",

src/sdkFactory/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import SplitIO from '../../types/splitio';
77
import { validateAndTrackApiKey } from '../utils/inputValidation/apiKey';
88
import { createLoggerAPI } from '../logger/sdkLogger';
99
import { NEW_FACTORY, RETRIEVE_MANAGER } from '../logger/constants';
10-
import { SDK_SPLITS_ARRIVED, SDK_SEGMENTS_ARRIVED } from '../readiness/constants';
10+
import { SDK_SPLITS_ARRIVED, SDK_SEGMENTS_ARRIVED, SDK_SPLITS_CACHE_LOADED } from '../readiness/constants';
1111
import { objectAssign } from '../utils/lang/objectAssign';
1212
import { strategyDebugFactory } from '../trackers/strategy/strategyDebug';
1313
import { strategyOptimizedFactory } from '../trackers/strategy/strategyOptimized';
@@ -52,6 +52,9 @@ export function sdkFactory(params: ISdkFactoryParams): SplitIO.ISDK | SplitIO.IA
5252
readiness.splits.emit(SDK_SPLITS_ARRIVED);
5353
readiness.segments.emit(SDK_SEGMENTS_ARRIVED);
5454
},
55+
onReadyFromCacheCb: () => {
56+
readiness.splits.emit(SDK_SPLITS_CACHE_LOADED);
57+
}
5558
});
5659
// @TODO add support for dataloader: `if (params.dataLoader) params.dataLoader(storage);`
5760
const clients: Record<string, SplitIO.IBasicClient> = {};

src/storages/inRedis/index.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,37 @@ export interface InRedisStorageOptions {
1717
options?: Record<string, any>
1818
}
1919

20+
let RD: typeof RedisAdapter | undefined;
21+
22+
try {
23+
// Using `require` to prevent error when bundling or importing the SDK in a .mjs file, since ioredis is a CommonJS module.
24+
// Redis storage is not supported with .mjs files.
25+
RD = require('./RedisAdapter').RedisAdapter;
26+
} catch (error) { /* empty */ }
27+
2028
/**
2129
* InRedis storage factory for consumer server-side SplitFactory, that uses `Ioredis` Redis client for Node.js
2230
* @see {@link https://www.npmjs.com/package/ioredis}
2331
*/
2432
export function InRedisStorage(options: InRedisStorageOptions = {}): IStorageAsyncFactory {
2533

26-
// Lazy loading to prevent error when bundling or importing the SDK in a .mjs file, since ioredis is a CommonJS module.
27-
// Redis storage is not supported with .mjs files.
28-
const RD = require('./RedisAdapter').RedisAdapter;
29-
3034
const prefix = validatePrefix(options.prefix);
3135

3236
function InRedisStorageFactory(params: IStorageFactoryParams): IStorageAsync {
33-
const { onReadyCb, settings, settings: { log } } = params;
37+
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.');
38+
39+
const { onReadyFromCacheCb, onReadyCb, settings, settings: { log } } = params;
3440
const metadata = metadataBuilder(settings);
3541
const keys = new KeyBuilderSS(prefix, metadata);
36-
const redisClient: RedisAdapter = new RD(log, options.options || {});
42+
const redisClient = new RD(log, options.options || {});
3743
const telemetry = new TelemetryCacheInRedis(log, keys, redisClient);
3844
const impressionCountsCache = new ImpressionCountsCacheInRedis(log, keys.buildImpressionsCountKey(), redisClient);
3945
const uniqueKeysCache = new UniqueKeysCacheInRedis(log, keys.buildUniqueKeysKey(), redisClient);
4046

41-
// subscription to Redis connect event in order to emit SDK_READY event on consumer mode
47+
// RedisAdapter lets queue operations before connected
48+
onReadyFromCacheCb();
49+
50+
// Subscription to Redis connect event in order to emit SDK_READY event on consumer mode
4251
redisClient.on('connect', () => {
4352
onReadyCb();
4453
impressionCountsCache.start();

src/storages/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ export interface IStorageFactoryParams {
472472
* It is meant for emitting SDK_READY event in consumer mode, and waiting before using the storage in the synchronizer.
473473
*/
474474
onReadyCb: (error?: any) => void,
475+
onReadyFromCacheCb: () => void,
475476
}
476477

477478

0 commit comments

Comments
 (0)