11'use strict' ;
22
33var net = require ( 'net' ) ;
4+ var tls = require ( 'tls' ) ;
45var URL = require ( 'url' ) ;
56var util = require ( 'util' ) ;
67var utils = require ( './lib/utils' ) ;
@@ -46,7 +47,10 @@ function RedisClient (options) {
4647 cnx_options . family = ( ! options . family && net . isIP ( cnx_options . host ) ) || ( options . family === 'IPv6' ? 6 : 4 ) ;
4748 this . address = cnx_options . host + ':' + cnx_options . port ;
4849 }
49- this . connection_option = cnx_options ;
50+ for ( var tls_option in options . tls ) { // jshint ignore: line
51+ cnx_options [ tls_option ] = options . tls [ tls_option ] ;
52+ }
53+ this . connection_options = cnx_options ;
5054 this . connection_id = ++ connection_id ;
5155 this . connected = false ;
5256 this . ready = false ;
@@ -95,7 +99,18 @@ util.inherits(RedisClient, events.EventEmitter);
9599// Attention: the function name "create_stream" should not be changed, as other libraries need this to mock the stream (e.g. fakeredis)
96100RedisClient . prototype . create_stream = function ( ) {
97101 var self = this ;
98- this . stream = net . createConnection ( this . connection_option ) ;
102+
103+ // On a reconnect destroy the former stream and retry
104+ if ( this . stream ) {
105+ this . stream . removeAllListeners ( ) ;
106+ this . stream . destroy ( ) ;
107+ }
108+
109+ if ( this . options . tls ) {
110+ this . stream = tls . connect ( this . connection_options ) ;
111+ } else {
112+ this . stream = net . createConnection ( this . connection_options ) ;
113+ }
99114
100115 if ( this . options . connect_timeout ) {
101116 this . stream . setTimeout ( this . connect_timeout , function ( ) {
@@ -104,7 +119,8 @@ RedisClient.prototype.create_stream = function () {
104119 } ) ;
105120 }
106121
107- this . stream . once ( 'connect' , function ( ) {
122+ var connect_event = this . options . tls ? "secureConnect" : "connect" ;
123+ this . stream . on ( connect_event , function ( ) {
108124 this . removeAllListeners ( "timeout" ) ;
109125 self . on_connect ( ) ;
110126 } ) ;
@@ -119,6 +135,10 @@ RedisClient.prototype.create_stream = function () {
119135 self . on_error ( err ) ;
120136 } ) ;
121137
138+ this . stream . on ( 'clientError' , function ( err ) {
139+ self . on_error ( err ) ;
140+ } ) ;
141+
122142 this . stream . once ( 'close' , function ( ) {
123143 self . connection_gone ( 'close' ) ;
124144 } ) ;
0 commit comments