Skip to content

Commit 43b97ab

Browse files
Updated the Redis storage to avoid lazy require of the dependency when the SDK is initialized
1 parent 2fc6e11 commit 43b97ab

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
1.17.1 (July 25, 2025)
2+
- Updated the Redis storage to avoid lazy require of the `ioredis` dependency when the SDK is initialized.
23
- Bugfix - Enhanced HTTP client module to implement timeouts for failing requests that might otherwise remain pending indefinitely on some Fetch API implementations, pausing the SDK synchronization process.
34

45
1.17.0 (September 6, 2024)

src/consent/sdkUserConsent.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const ConsentStatus = {
1515
* The public user consent API exposed via SplitFactory, used to control if the SDK tracks and sends impressions and events or not.
1616
*/
1717
export function createUserConsentAPI(params: ISdkFactoryContext) {
18-
const { settings, settings: { log }, syncManager, storage: { events, impressions, impressionCounts } } = params;
18+
const { settings, settings: { log }, syncManager, storage: { events, impressions, impressionCounts, uniqueKeys } } = params;
1919

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

@@ -41,7 +41,8 @@ export function createUserConsentAPI(params: ISdkFactoryContext) {
4141
// @ts-ignore, clear method is present in storage for standalone and partial consumer mode
4242
if (events.clear) events.clear(); // @ts-ignore
4343
if (impressions.clear) impressions.clear(); // @ts-ignore
44-
if (impressionCounts && impressionCounts.clear) impressionCounts.clear();
44+
if (impressionCounts && impressionCounts.clear) impressionCounts.clear(); // @ts-ignore
45+
if (uniqueKeys && uniqueKeys.clear) uniqueKeys.clear();
4546
}
4647
} else {
4748
log.info(USER_CONSENT_NOT_UPDATED, [newConsentStatus]);

src/storages/inRedis/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@ 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
/**
21-
* InRedis storage factory for consumer server-side SplitFactory, that uses `Ioredis` Redis client for Node.
29+
* 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 {
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+
3339
const { onReadyCb, settings, settings: { log, sync: { impressionsMode } } } = params;
3440
const metadata = metadataBuilder(settings);
3541
const keys = new KeyBuilderSS(prefix, metadata);
@@ -38,7 +44,7 @@ export function InRedisStorage(options: InRedisStorageOptions = {}): IStorageAsy
3844
const impressionCountsCache = impressionsMode !== DEBUG ? new ImpressionCountsCacheInRedis(log, keys.buildImpressionsCountKey(), redisClient) : undefined;
3945
const uniqueKeysCache = impressionsMode === NONE ? new UniqueKeysCacheInRedis(log, keys.buildUniqueKeysKey(), redisClient) : undefined;
4046

41-
// subscription to Redis connect event in order to emit SDK_READY event on consumer mode
47+
// Subscription to Redis connect event in order to emit SDK_READY event on consumer mode
4248
redisClient.on('connect', () => {
4349
onReadyCb();
4450
if (impressionCountsCache) impressionCountsCache.start();

0 commit comments

Comments
 (0)