@@ -17,23 +17,29 @@ export interface InRedisStorageOptions {
17
17
options ?: Record < string , any >
18
18
}
19
19
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
+
20
28
/**
21
29
* InRedis storage factory for consumer server-side SplitFactory, that uses `Ioredis` Redis client for Node.js
22
30
* @see {@link https://www.npmjs.com/package/ioredis }
23
31
*/
24
32
export function InRedisStorage ( options : InRedisStorageOptions = { } ) : IStorageAsyncFactory {
25
33
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
-
30
34
const prefix = validatePrefix ( options . prefix ) ;
31
35
32
36
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
+
33
39
const { onReadyCb, settings, settings : { log } } = params ;
34
40
const metadata = metadataBuilder ( settings ) ;
35
41
const keys = new KeyBuilderSS ( prefix , metadata ) ;
36
- const redisClient : RedisAdapter = new RD ( log , options . options || { } ) ;
42
+ const redisClient = new RD ( log , options . options || { } ) ;
37
43
const telemetry = new TelemetryCacheInRedis ( log , keys , redisClient ) ;
38
44
const impressionCountsCache = new ImpressionCountsCacheInRedis ( log , keys . buildImpressionsCountKey ( ) , redisClient ) ;
39
45
const uniqueKeysCache = new UniqueKeysCacheInRedis ( log , keys . buildUniqueKeysKey ( ) , redisClient ) ;
0 commit comments