1
+ import { RedisClusterClientOptions } from '@redis/client/dist/lib/cluster' ;
1
2
import { createConnection } from 'node:net' ;
2
3
import { once } from 'node:events' ;
3
4
import { createClient } from '@redis/client/index' ;
@@ -102,7 +103,8 @@ async function spawnRedisClusterNodeDockers(
102
103
dockersConfig : RedisClusterDockersConfig ,
103
104
serverArguments : Array < string > ,
104
105
fromSlot : number ,
105
- toSlot : number
106
+ toSlot : number ,
107
+ clientConfig ?: Partial < RedisClusterClientOptions >
106
108
) {
107
109
const range : Array < number > = [ ] ;
108
110
for ( let i = fromSlot ; i < toSlot ; i ++ ) {
@@ -111,7 +113,8 @@ async function spawnRedisClusterNodeDockers(
111
113
112
114
const master = await spawnRedisClusterNodeDocker (
113
115
dockersConfig ,
114
- serverArguments
116
+ serverArguments ,
117
+ clientConfig
115
118
) ;
116
119
117
120
await master . client . clusterAddSlots ( range ) ;
@@ -127,7 +130,13 @@ async function spawnRedisClusterNodeDockers(
127
130
'yes' ,
128
131
'--cluster-node-timeout' ,
129
132
'5000'
130
- ] ) . then ( async replica => {
133
+ ] , clientConfig ) . then ( async replica => {
134
+
135
+ const requirePassIndex = serverArguments . findIndex ( ( x ) => x === '--requirepass' ) ;
136
+ if ( requirePassIndex !== - 1 ) {
137
+ const password = serverArguments [ requirePassIndex + 1 ] ;
138
+ await replica . client . configSet ( { 'masterauth' : password } )
139
+ }
131
140
await replica . client . clusterMeet ( '127.0.0.1' , master . docker . port ) ;
132
141
133
142
while ( ( await replica . client . clusterSlots ( ) ) . length === 0 ) {
@@ -151,7 +160,8 @@ async function spawnRedisClusterNodeDockers(
151
160
152
161
async function spawnRedisClusterNodeDocker (
153
162
dockersConfig : RedisClusterDockersConfig ,
154
- serverArguments : Array < string >
163
+ serverArguments : Array < string > ,
164
+ clientConfig ?: Partial < RedisClusterClientOptions >
155
165
) {
156
166
const docker = await spawnRedisServerDocker ( dockersConfig , [
157
167
...serverArguments ,
@@ -163,7 +173,8 @@ async function spawnRedisClusterNodeDocker(
163
173
client = createClient ( {
164
174
socket : {
165
175
port : docker . port
166
- }
176
+ } ,
177
+ ...clientConfig
167
178
} ) ;
168
179
169
180
await client . connect ( ) ;
@@ -178,7 +189,8 @@ const SLOTS = 16384;
178
189
179
190
async function spawnRedisClusterDockers (
180
191
dockersConfig : RedisClusterDockersConfig ,
181
- serverArguments : Array < string >
192
+ serverArguments : Array < string > ,
193
+ clientConfig ?: Partial < RedisClusterClientOptions >
182
194
) : Promise < Array < RedisServerDocker > > {
183
195
const numberOfMasters = dockersConfig . numberOfMasters ?? 2 ,
184
196
slotsPerNode = Math . floor ( SLOTS / numberOfMasters ) ,
@@ -191,7 +203,8 @@ async function spawnRedisClusterDockers(
191
203
dockersConfig ,
192
204
serverArguments ,
193
205
fromSlot ,
194
- toSlot
206
+ toSlot ,
207
+ clientConfig
195
208
)
196
209
) ;
197
210
}
@@ -234,13 +247,18 @@ function totalNodes(slots: any) {
234
247
235
248
const RUNNING_CLUSTERS = new Map < Array < string > , ReturnType < typeof spawnRedisClusterDockers > > ( ) ;
236
249
237
- export function spawnRedisCluster ( dockersConfig : RedisClusterDockersConfig , serverArguments : Array < string > ) : Promise < Array < RedisServerDocker > > {
250
+ export function spawnRedisCluster (
251
+ dockersConfig : RedisClusterDockersConfig ,
252
+ serverArguments : Array < string > ,
253
+ clientConfig ?: Partial < RedisClusterClientOptions > ) : Promise < Array < RedisServerDocker > > {
254
+
238
255
const runningCluster = RUNNING_CLUSTERS . get ( serverArguments ) ;
239
256
if ( runningCluster ) {
240
257
return runningCluster ;
241
258
}
242
259
243
- const dockersPromise = spawnRedisClusterDockers ( dockersConfig , serverArguments ) ;
260
+ const dockersPromise = spawnRedisClusterDockers ( dockersConfig , serverArguments , clientConfig ) ;
261
+
244
262
RUNNING_CLUSTERS . set ( serverArguments , dockersPromise ) ;
245
263
return dockersPromise ;
246
264
}
0 commit comments