1
1
'use strict' ;
2
2
3
3
var net = require ( 'net' ) ;
4
+ var tls = require ( 'tls' ) ;
4
5
var URL = require ( 'url' ) ;
5
6
var util = require ( 'util' ) ;
6
7
var utils = require ( './lib/utils' ) ;
@@ -46,7 +47,10 @@ function RedisClient (options) {
46
47
cnx_options . family = ( ! options . family && net . isIP ( cnx_options . host ) ) || ( options . family === 'IPv6' ? 6 : 4 ) ;
47
48
this . address = cnx_options . host + ':' + cnx_options . port ;
48
49
}
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 ;
50
54
this . connection_id = ++ connection_id ;
51
55
this . connected = false ;
52
56
this . ready = false ;
@@ -95,7 +99,18 @@ util.inherits(RedisClient, events.EventEmitter);
95
99
// Attention: the function name "create_stream" should not be changed, as other libraries need this to mock the stream (e.g. fakeredis)
96
100
RedisClient . prototype . create_stream = function ( ) {
97
101
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
+ }
99
114
100
115
if ( this . options . connect_timeout ) {
101
116
this . stream . setTimeout ( this . connect_timeout , function ( ) {
@@ -104,7 +119,8 @@ RedisClient.prototype.create_stream = function () {
104
119
} ) ;
105
120
}
106
121
107
- this . stream . once ( 'connect' , function ( ) {
122
+ var connect_event = this . options . tls ? "secureConnect" : "connect" ;
123
+ this . stream . on ( connect_event , function ( ) {
108
124
this . removeAllListeners ( "timeout" ) ;
109
125
self . on_connect ( ) ;
110
126
} ) ;
@@ -119,6 +135,10 @@ RedisClient.prototype.create_stream = function () {
119
135
self . on_error ( err ) ;
120
136
} ) ;
121
137
138
+ this . stream . on ( 'clientError' , function ( err ) {
139
+ self . on_error ( err ) ;
140
+ } ) ;
141
+
122
142
this . stream . once ( 'close' , function ( ) {
123
143
self . connection_gone ( 'close' ) ;
124
144
} ) ;
0 commit comments