@@ -3,6 +3,7 @@ import { EventEmitter } from "events";
33import asCallback from "standard-as-callback" ;
44import Cluster from "./cluster" ;
55import Command from "./Command" ;
6+ import { DataHandledable , FlushQueueOptions , Condition } from "./DataHandler" ;
67import { StandaloneConnector } from "./connectors" ;
78import AbstractConnector from "./connectors/AbstractConnector" ;
89import SentinelConnector from "./connectors/SentinelConnector" ;
@@ -60,7 +61,7 @@ type RedisStatus =
6061 * }
6162 * ```
6263 */
63- class Redis extends Commander {
64+ class Redis extends Commander implements DataHandledable {
6465 static Cluster = Cluster ;
6566 static Command = Command ;
6667 /**
@@ -89,14 +90,18 @@ class Redis extends Commander {
8990 */
9091 isCluster = false ;
9192
93+ /**
94+ * @ignore
95+ */
96+ condition : Condition | null ;
97+
98+ /**
99+ * @ignore
100+ */
101+ commandQueue : Deque < CommandItem > ;
102+
92103 private connector : AbstractConnector ;
93104 private reconnectTimeout : ReturnType < typeof setTimeout > | null = null ;
94- private condition : {
95- select : number ;
96- auth ?: string | [ string , string ] ;
97- subscriber : boolean ;
98- } ;
99- private commandQueue : Deque < CommandItem > ;
100105 private offlineQueue : Deque ;
101106 private connectionEpoch = 0 ;
102107 private retryAttempts = 0 ;
@@ -220,9 +225,11 @@ class Redis extends Commander {
220225
221226 // Node ignores setKeepAlive before connect, therefore we wait for the event:
222227 // https://github.com/nodejs/node/issues/31663
223- if ( typeof options . keepAlive === ' number' ) {
228+ if ( typeof options . keepAlive === " number" ) {
224229 if ( stream . connecting ) {
225- stream . once ( CONNECT_EVENT , ( ) => stream . setKeepAlive ( true , options . keepAlive ) ) ;
230+ stream . once ( CONNECT_EVENT , ( ) => {
231+ stream . setKeepAlive ( true , options . keepAlive ) ;
232+ } ) ;
226233 } else {
227234 stream . setKeepAlive ( true , options . keepAlive ) ;
228235 }
@@ -344,10 +351,10 @@ class Redis extends Commander {
344351 * One of `"normal"`, `"subscriber"`, or `"monitor"`. When the connection is
345352 * not in `"normal"` mode, certain commands are not allowed.
346353 */
347- get mode ( ) : "normal" | "subscriber" | "monitor" {
354+ get mode ( ) : "normal" | "subscriber" | "monitor" {
348355 return this . options . monitor
349356 ? "monitor"
350- : this . condition && this . condition . subscriber
357+ : this . condition ? .subscriber
351358 ? "subscriber"
352359 : "normal" ;
353360 }
@@ -421,7 +428,7 @@ class Redis extends Commander {
421428 return command . promise ;
422429 }
423430 if (
424- this . condition . subscriber &&
431+ this . condition ? .subscriber &&
425432 ! Command . checkFlag ( "VALID_IN_SUBSCRIBER_MODE" , command . name )
426433 ) {
427434 command . reject (
@@ -491,7 +498,7 @@ class Redis extends Commander {
491498 debug (
492499 "write command[%s]: %d -> %s(%o)" ,
493500 this . _getDescription ( ) ,
494- this . condition . select ,
501+ this . condition ? .select ,
495502 command . name ,
496503 command . args
497504 ) ;
@@ -600,45 +607,22 @@ class Redis extends Commander {
600607 }
601608
602609 /**
603- * Get description of the connection. Used for debugging.
610+ * @ignore
604611 */
605- private _getDescription ( ) {
606- let description ;
607- if ( "path" in this . options && this . options . path ) {
608- description = this . options . path ;
609- } else if (
610- this . stream &&
611- this . stream . remoteAddress &&
612- this . stream . remotePort
613- ) {
614- description = this . stream . remoteAddress + ":" + this . stream . remotePort ;
615- } else if ( "host" in this . options && this . options . host ) {
616- description = this . options . host + ":" + this . options . port ;
617- } else {
618- // Unexpected
619- description = "" ;
620- }
621- if ( this . options . connectionName ) {
622- description += ` (${ this . options . connectionName } )` ;
623- }
624- return description ;
625- }
626-
627- private resetCommandQueue ( ) {
628- this . commandQueue = new Deque ( ) ;
629- }
630-
631- private resetOfflineQueue ( ) {
632- this . offlineQueue = new Deque ( ) ;
633- }
634-
635- private recoverFromFatalError ( commandError , err : Error | null , options ) {
612+ recoverFromFatalError (
613+ _commandError : Error ,
614+ err : Error ,
615+ options : FlushQueueOptions
616+ ) {
636617 this . flushQueue ( err , options ) ;
637618 this . silentEmit ( "error" , err ) ;
638619 this . disconnect ( true ) ;
639620 }
640621
641- private handleReconnection ( err : Error , item : CommandItem ) {
622+ /**
623+ * @ignore
624+ */
625+ handleReconnection ( err : Error , item : CommandItem ) {
642626 let needReconnect : ReturnType < ReconnectOnError > = false ;
643627 if ( this . options . reconnectOnError ) {
644628 needReconnect = this . options . reconnectOnError ( err ) ;
@@ -657,7 +641,7 @@ class Redis extends Commander {
657641 this . disconnect ( true ) ;
658642 }
659643 if (
660- this . condition . select !== item . select &&
644+ this . condition ? .select !== item . select &&
661645 item . command . name !== "select"
662646 ) {
663647 this . select ( item . select ) ;
@@ -671,6 +655,39 @@ class Redis extends Commander {
671655 }
672656 }
673657
658+ /**
659+ * Get description of the connection. Used for debugging.
660+ */
661+ private _getDescription ( ) {
662+ let description ;
663+ if ( "path" in this . options && this . options . path ) {
664+ description = this . options . path ;
665+ } else if (
666+ this . stream &&
667+ this . stream . remoteAddress &&
668+ this . stream . remotePort
669+ ) {
670+ description = this . stream . remoteAddress + ":" + this . stream . remotePort ;
671+ } else if ( "host" in this . options && this . options . host ) {
672+ description = this . options . host + ":" + this . options . port ;
673+ } else {
674+ // Unexpected
675+ description = "" ;
676+ }
677+ if ( this . options . connectionName ) {
678+ description += ` (${ this . options . connectionName } )` ;
679+ }
680+ return description ;
681+ }
682+
683+ private resetCommandQueue ( ) {
684+ this . commandQueue = new Deque ( ) ;
685+ }
686+
687+ private resetOfflineQueue ( ) {
688+ this . offlineQueue = new Deque ( ) ;
689+ }
690+
674691 private parseOptions ( ...args : unknown [ ] ) {
675692 const options : Record < string , unknown > = { } ;
676693 let isTls = false ;
@@ -744,7 +761,7 @@ class Redis extends Commander {
744761 * @param error The error object to send to the commands
745762 * @param options options
746763 */
747- private flushQueue ( error : Error , options ?: RedisOptions ) {
764+ private flushQueue ( error : Error , options ?: FlushQueueOptions ) {
748765 options = defaults ( { } , options , {
749766 offlineQueue : true ,
750767 commandQueue : true ,
0 commit comments