@@ -189,115 +189,122 @@ class Redis extends Commander implements DataHandledable {
189
189
190
190
const { options } = this ;
191
191
192
- this . condition = {
193
- select : options . db ,
194
- auth : options . username
195
- ? [ options . username , options . password ]
196
- : options . password ,
197
- subscriber : false ,
198
- } ;
199
-
200
- const _this = this ;
201
- asCallback (
202
- this . connector . connect ( function ( type , err ) {
203
- _this . silentEmit ( type , err ) ;
204
- } ) as Promise < NetStream > ,
205
- function ( err : Error | null , stream ?: NetStream ) {
206
- if ( err ) {
207
- _this . flushQueue ( err ) ;
208
- _this . silentEmit ( "error" , err ) ;
209
- reject ( err ) ;
210
- _this . setStatus ( "end" ) ;
211
- return ;
212
- }
213
- let CONNECT_EVENT = options . tls ? "secureConnect" : "connect" ;
214
- if (
215
- "sentinels" in options &&
216
- options . sentinels &&
217
- ! options . enableTLSForSentinelMode
218
- ) {
219
- CONNECT_EVENT = "connect" ;
220
- }
192
+ this . resolvePassword ( ) . then ( ( resolvedPassword ) => {
193
+ this . condition = {
194
+ select : options . db ,
195
+ auth : options . username
196
+ ? [ options . username , resolvedPassword ]
197
+ : resolvedPassword ,
198
+ subscriber : false ,
199
+ } ;
200
+
201
+ const _this = this ;
202
+ asCallback (
203
+ this . connector . connect ( function ( type , err ) {
204
+ _this . silentEmit ( type , err ) ;
205
+ } ) as Promise < NetStream > ,
206
+ function ( err : Error | null , stream ?: NetStream ) {
207
+ if ( err ) {
208
+ _this . flushQueue ( err ) ;
209
+ _this . silentEmit ( "error" , err ) ;
210
+ reject ( err ) ;
211
+ _this . setStatus ( "end" ) ;
212
+ return ;
213
+ }
214
+ let CONNECT_EVENT = options . tls ? "secureConnect" : "connect" ;
215
+ if (
216
+ "sentinels" in options &&
217
+ options . sentinels &&
218
+ ! options . enableTLSForSentinelMode
219
+ ) {
220
+ CONNECT_EVENT = "connect" ;
221
+ }
221
222
222
- _this . stream = stream ;
223
+ _this . stream = stream ;
223
224
224
- if ( options . noDelay ) {
225
- stream . setNoDelay ( true ) ;
226
- }
225
+ if ( options . noDelay ) {
226
+ stream . setNoDelay ( true ) ;
227
+ }
227
228
228
- // Node ignores setKeepAlive before connect, therefore we wait for the event:
229
- // https://github.com/nodejs/node/issues/31663
230
- if ( typeof options . keepAlive === "number" ) {
231
- if ( stream . connecting ) {
232
- stream . once ( CONNECT_EVENT , ( ) => {
229
+ // Node ignores setKeepAlive before connect, therefore we wait for the event:
230
+ // https://github.com/nodejs/node/issues/31663
231
+ if ( typeof options . keepAlive === "number" ) {
232
+ if ( stream . connecting ) {
233
+ stream . once ( CONNECT_EVENT , ( ) => {
234
+ stream . setKeepAlive ( true , options . keepAlive ) ;
235
+ } ) ;
236
+ } else {
233
237
stream . setKeepAlive ( true , options . keepAlive ) ;
234
- } ) ;
235
- } else {
236
- stream . setKeepAlive ( true , options . keepAlive ) ;
238
+ }
237
239
}
238
- }
239
240
240
- if ( stream . connecting ) {
241
- stream . once ( CONNECT_EVENT , eventHandler . connectHandler ( _this ) ) ;
242
-
243
- if ( options . connectTimeout ) {
244
- /*
245
- * Typically, Socket#setTimeout(0) will clear the timer
246
- * set before. However, in some platforms (Electron 3.x~4.x),
247
- * the timer will not be cleared. So we introduce a variable here.
248
- *
249
- * See https://github.com/electron/electron/issues/14915
250
- */
251
- let connectTimeoutCleared = false ;
252
- stream . setTimeout ( options . connectTimeout , function ( ) {
253
- if ( connectTimeoutCleared ) {
254
- return ;
255
- }
256
- stream . setTimeout ( 0 ) ;
257
- stream . destroy ( ) ;
258
-
259
- const err = new Error ( "connect ETIMEDOUT" ) ;
260
- // @ts -expect-error
261
- err . errorno = "ETIMEDOUT" ;
262
- // @ts -expect-error
263
- err . code = "ETIMEDOUT" ;
264
- // @ts -expect-error
265
- err . syscall = "connect" ;
266
- eventHandler . errorHandler ( _this ) ( err ) ;
267
- } ) ;
268
- stream . once ( CONNECT_EVENT , function ( ) {
269
- connectTimeoutCleared = true ;
270
- stream . setTimeout ( 0 ) ;
271
- } ) ;
241
+ if ( stream . connecting ) {
242
+ stream . once ( CONNECT_EVENT , eventHandler . connectHandler ( _this ) ) ;
243
+
244
+ if ( options . connectTimeout ) {
245
+ /*
246
+ * Typically, Socket#setTimeout(0) will clear the timer
247
+ * set before. However, in some platforms (Electron 3.x~4.x),
248
+ * the timer will not be cleared. So we introduce a variable here.
249
+ *
250
+ * See https://github.com/electron/electron/issues/14915
251
+ */
252
+ let connectTimeoutCleared = false ;
253
+ stream . setTimeout ( options . connectTimeout , function ( ) {
254
+ if ( connectTimeoutCleared ) {
255
+ return ;
256
+ }
257
+ stream . setTimeout ( 0 ) ;
258
+ stream . destroy ( ) ;
259
+
260
+ const err = new Error ( "connect ETIMEDOUT" ) ;
261
+ // @ts -expect-error
262
+ err . errorno = "ETIMEDOUT" ;
263
+ // @ts -expect-error
264
+ err . code = "ETIMEDOUT" ;
265
+ // @ts -expect-error
266
+ err . syscall = "connect" ;
267
+ eventHandler . errorHandler ( _this ) ( err ) ;
268
+ } ) ;
269
+ stream . once ( CONNECT_EVENT , function ( ) {
270
+ connectTimeoutCleared = true ;
271
+ stream . setTimeout ( 0 ) ;
272
+ } ) ;
273
+ }
274
+ } else if ( stream . destroyed ) {
275
+ const firstError = _this . connector . firstError ;
276
+ if ( firstError ) {
277
+ process . nextTick ( ( ) => {
278
+ eventHandler . errorHandler ( _this ) ( firstError ) ;
279
+ } ) ;
280
+ }
281
+ process . nextTick ( eventHandler . closeHandler ( _this ) ) ;
282
+ } else {
283
+ process . nextTick ( eventHandler . connectHandler ( _this ) ) ;
272
284
}
273
- } else if ( stream . destroyed ) {
274
- const firstError = _this . connector . firstError ;
275
- if ( firstError ) {
276
- process . nextTick ( ( ) => {
277
- eventHandler . errorHandler ( _this ) ( firstError ) ;
278
- } ) ;
285
+ if ( ! stream . destroyed ) {
286
+ stream . once ( "error" , eventHandler . errorHandler ( _this ) ) ;
287
+ stream . once ( "close" , eventHandler . closeHandler ( _this ) ) ;
279
288
}
280
- process . nextTick ( eventHandler . closeHandler ( _this ) ) ;
281
- } else {
282
- process . nextTick ( eventHandler . connectHandler ( _this ) ) ;
283
- }
284
- if ( ! stream . destroyed ) {
285
- stream . once ( "error" , eventHandler . errorHandler ( _this ) ) ;
286
- stream . once ( "close" , eventHandler . closeHandler ( _this ) ) ;
287
- }
288
289
289
- const connectionReadyHandler = function ( ) {
290
- _this . removeListener ( "close" , connectionCloseHandler ) ;
291
- resolve ( ) ;
292
- } ;
293
- var connectionCloseHandler = function ( ) {
294
- _this . removeListener ( "ready" , connectionReadyHandler ) ;
295
- reject ( new Error ( CONNECTION_CLOSED_ERROR_MSG ) ) ;
296
- } ;
297
- _this . once ( "ready" , connectionReadyHandler ) ;
298
- _this . once ( "close" , connectionCloseHandler ) ;
299
- }
300
- ) ;
290
+ const connectionReadyHandler = function ( ) {
291
+ _this . removeListener ( "close" , connectionCloseHandler ) ;
292
+ resolve ( ) ;
293
+ } ;
294
+ var connectionCloseHandler = function ( ) {
295
+ _this . removeListener ( "ready" , connectionReadyHandler ) ;
296
+ reject ( new Error ( CONNECTION_CLOSED_ERROR_MSG ) ) ;
297
+ } ;
298
+ _this . once ( "ready" , connectionReadyHandler ) ;
299
+ _this . once ( "close" , connectionCloseHandler ) ;
300
+ }
301
+ ) ;
302
+ } ) . catch ( ( err ) => {
303
+ this . flushQueue ( err ) ;
304
+ this . silentEmit ( "error" , err ) ;
305
+ reject ( err ) ;
306
+ this . setStatus ( "end" ) ;
307
+ } ) ;
301
308
} ) ;
302
309
303
310
return asCallback ( promise , callback ) ;
@@ -858,6 +865,17 @@ class Redis extends Commander implements DataHandledable {
858
865
}
859
866
} ) . catch ( noop ) ;
860
867
}
868
+
869
+ private async resolvePassword ( ) : Promise < string | null > {
870
+ const { password } = this . options ;
871
+ if ( ! password ) {
872
+ return null ;
873
+ }
874
+ if ( typeof password === "function" ) {
875
+ return await password ( ) ;
876
+ }
877
+ return password ;
878
+ }
861
879
}
862
880
863
881
interface Redis extends EventEmitter {
0 commit comments