@@ -17,23 +17,29 @@ 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 */
2432export 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 ( 'Redis storage is not available. Runtime environment must support CommonJS (`require`) to import the ioredis dependency.' ) ;
38+
3339 const { 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 ) ;
0 commit comments