@@ -30,17 +30,25 @@ export function hasBinary(obj: any, toJSON?: boolean): boolean {
3030} 
3131
3232/** 
33-  * Whether the client comes from the `redis` package 
33+  * Whether the client comes from version 5.x of  the `redis` package 
3434 * 
3535 * @param  redisClient 
3636 * 
37-  * @see  https://github.com/redis/node-redis 
37+  * @see  https://github.com/redis/node-redis/blob/master/docs/v5.md 
38+  */ 
39+ function  isRedisV5Client ( redisClient : any )  { 
40+   return  typeof  redisClient . createPool  ===  "function" ; 
41+ } 
42+ 
43+ /** 
44+  * Whether the client comes from version 4.x of the `redis` package 
45+  * 
46+  * @param  redisClient 
47+  * 
48+  * @see  https://github.com/redis/node-redis/blob/master/docs/v4-to-v5.md 
3849 */ 
3950function  isRedisV4Client ( redisClient : any )  { 
40-   return  ( 
41-     typeof  redisClient . sSubscribe  ===  "function"  || 
42-     typeof  redisClient . totalClients  ===  "number" 
43-   ) ; 
51+   return  typeof  redisClient . sSubscribe  ===  "function" ; 
4452} 
4553
4654/** 
@@ -60,6 +68,8 @@ function mapResult(result) {
6068  } ; 
6169} 
6270
71+ const  POOL  =  Symbol ( "redis_v5_pool" ) ; 
72+ 
6373/** 
6474 * @see  https://redis.io/commands/xread/ 
6575 */ 
@@ -69,29 +79,40 @@ export function XREAD(
6979  offset : string , 
7080  readCount : number 
7181)  { 
72-   if  ( isRedisV4Client ( redisClient ) )  { 
73-     return  import ( "redis" ) . then ( ( redisPackage )  =>  { 
74-       const  streams  =  [ 
82+   if  ( isRedisV5Client ( redisClient ) )  { 
83+     if  ( ! redisClient [ POOL ] )  { 
84+       redisClient [ POOL ]  =  redisClient . createPool ( ) ; 
85+     } 
86+ 
87+     return  redisClient [ POOL ] . xRead ( 
88+       [ 
7589        { 
7690          key : streamName , 
7791          id : offset , 
7892        } , 
79-       ] ; 
80-       const   options   =   { 
93+       ] , 
94+       { 
8195        COUNT : readCount , 
8296        BLOCK : 5000 , 
83-       } ; 
84-       if  ( redisPackage . commandOptions )  { 
85-         return  redisClient . xRead ( 
86-           redisPackage . commandOptions ( { 
87-             isolated : true , 
88-           } ) , 
89-           streams , 
90-           options 
91-         ) ; 
92-       }  else  { 
93-         return  redisClient . xRead ( streams ,  options ) ; 
9497      } 
98+     ) ; 
99+   }  else  if  ( isRedisV4Client ( redisClient ) )  { 
100+     return  import ( "redis" ) . then ( ( redisPackage )  =>  { 
101+       return  redisClient . xRead ( 
102+         redisPackage . commandOptions ( { 
103+           isolated : true , 
104+         } ) , 
105+         [ 
106+           { 
107+             key : streamName , 
108+             id : offset , 
109+           } , 
110+         ] , 
111+         { 
112+           COUNT : readCount , 
113+           BLOCK : 5000 , 
114+         } 
115+       ) ; 
95116    } ) ; 
96117  }  else  { 
97118    return  redisClient 
@@ -118,7 +139,7 @@ export function XADD(
118139  payload : any , 
119140  maxLenThreshold : number 
120141)  { 
121-   if  ( isRedisV4Client ( redisClient ) )  { 
142+   if  ( isRedisV4Client ( redisClient )   ||   isRedisV5Client ( redisClient ) )  { 
122143    return  redisClient . xAdd ( streamName ,  "*" ,  payload ,  { 
123144      TRIM : { 
124145        strategy : "MAXLEN" , 
@@ -145,7 +166,7 @@ export function XRANGE(
145166  start : string , 
146167  end : string 
147168)  { 
148-   if  ( isRedisV4Client ( redisClient ) )  { 
169+   if  ( isRedisV4Client ( redisClient )   ||   isRedisV5Client ( redisClient ) )  { 
149170    return  redisClient . xRange ( streamName ,  start ,  end ) ; 
150171  }  else  { 
151172    return  redisClient . xrange ( streamName ,  start ,  end ) . then ( ( res )  =>  { 
@@ -163,7 +184,7 @@ export function SET(
163184  value : string , 
164185  expiryInSeconds : number 
165186)  { 
166-   if  ( isRedisV4Client ( redisClient ) )  { 
187+   if  ( isRedisV4Client ( redisClient )   ||   isRedisV5Client ( redisClient ) )  { 
167188    return  redisClient . set ( key ,  value ,  { 
168189      PX : expiryInSeconds , 
169190    } ) ; 
@@ -176,7 +197,7 @@ export function SET(
176197 * @see  https://redis.io/commands/getdel/ 
177198 */ 
178199export  function  GETDEL ( redisClient : any ,  key : string )  { 
179-   if  ( isRedisV4Client ( redisClient ) )  { 
200+   if  ( isRedisV4Client ( redisClient )   ||   isRedisV5Client ( redisClient ) )  { 
180201    // note: GETDEL was added in Redis version 6.2 
181202    return  redisClient . multi ( ) . get ( key ) . del ( key ) . exec ( ) ; 
182203  }  else  { 
0 commit comments