1
- var net = require ( 'net' ) ;
1
+ var Net = require ( 'net' ) ;
2
2
var util = require ( 'util' ) ;
3
- var tls = require ( 'tls' ) ;
3
+ var Tls = require ( 'tls' ) ;
4
+ var Timers = require ( 'timers' ) ;
4
5
var EventEmitter = require ( 'events' ) . EventEmitter ;
5
6
var Queue = require ( 'double-ended-queue' ) ;
6
7
@@ -30,9 +31,9 @@ function Connection (opts)
30
31
31
32
if ( ! opts . config . stream ) {
32
33
if ( opts . config . socketPath ) {
33
- this . stream = net . connect ( opts . config . socketPath ) ;
34
+ this . stream = Net . connect ( opts . config . socketPath ) ;
34
35
} else {
35
- this . stream = net . connect ( opts . config . port , opts . config . host ) ;
36
+ this . stream = Net . connect ( opts . config . port , opts . config . host ) ;
36
37
}
37
38
} else {
38
39
// if stream is a function, treat it as "stream agent / factory"
@@ -74,19 +75,22 @@ function Connection (opts)
74
75
this . threadId = null ;
75
76
this . _handshakePacket = null ;
76
77
this . _fatalError = null ;
77
-
78
+ this . _protocolError = null ;
78
79
this . _outOfOrderPackets = [ ] ;
79
80
80
- this . stream . once ( 'error' , function ( err ) {
81
- connection . _handleNetworkError ( err ) ;
82
- } ) ;
81
+ this . stream . once ( 'error' , connection . _handleNetworkError . bind ( this ) ) ;
83
82
84
83
// see https://gist.github.com/khoomeister/4985691#use-that-instead-of-bind
85
84
this . packetParser = new PacketParser ( function ( p ) { connection . handlePacket ( p ) ; } ) ;
85
+
86
86
this . stream . on ( 'data' , function ( data ) {
87
+ if ( connection . connectTimeout ) {
88
+ Timers . clearTimeout ( connection . connectTimeout ) ;
89
+ connection . connectTimeout = null ;
90
+ }
87
91
connection . packetParser . execute ( data ) ;
88
92
} ) ;
89
- this . _protocolError = null ;
93
+
90
94
this . stream . on ( 'end' , function ( ) {
91
95
// we need to set this flag everywhere where we want connection to close
92
96
if ( connection . _closing ) {
@@ -119,6 +123,11 @@ function Connection (opts)
119
123
// most common example: "Too many connections" error ( packet is sent immediately on connection attempt, we don't know server encoding yet)
120
124
// will be overwrittedn with actial encoding value as soon as server handshake packet is received
121
125
this . serverEncoding = 'utf8' ;
126
+
127
+ if ( this . config . connectTimeout ) {
128
+ var timeoutHandler = this . _handleTimeoutError . bind ( this ) ;
129
+ this . connectTimeout = Timers . setTimeout ( timeoutHandler , this . config . connectTimeout ) ;
130
+ }
122
131
}
123
132
util . inherits ( Connection , EventEmitter ) ;
124
133
@@ -139,6 +148,21 @@ Connection.prototype._handleNetworkError = function (err) {
139
148
connection . _fatalError = err ;
140
149
} ;
141
150
151
+ Connection . prototype . _handleTimeoutError = function ( ) {
152
+ if ( this . stream ) {
153
+ Timers . clearTimeout ( this . connectTimeout ) ;
154
+ this . connectTimeout = null ;
155
+ this . stream . destroy ( ) ;
156
+ }
157
+
158
+ var err = new Error ( 'connect ETIMEDOUT' ) ;
159
+ err . errorno = 'ETIMEDOUT' ;
160
+ err . code = 'ETIMEDOUT' ;
161
+ err . syscall = 'connect' ;
162
+
163
+ this . _handleNetworkError ( err ) ;
164
+ } ;
165
+
142
166
// notify all commands in the queue and bubble error as connection "error"
143
167
// called on stream error or unexpected termination
144
168
Connection . prototype . _notifyError = function ( err ) {
@@ -191,15 +215,15 @@ Connection.prototype.writePacket = function (packet) {
191
215
this . write ( packet . buffer ) ;
192
216
} ;
193
217
194
- if ( tls . TLSSocket ) {
218
+ if ( Tls . TLSSocket ) {
195
219
// 0.11+ environment
196
220
Connection . prototype . startTLS = function _startTLS ( onSecure ) {
197
221
if ( this . config . debug ) {
198
222
console . log ( 'Upgrading connection to TLS' ) ;
199
223
}
200
224
var connection = this ;
201
225
var stream = this . stream ;
202
- var secureContext = tls . createSecureContext ( {
226
+ var secureContext = Tls . createSecureContext ( {
203
227
ca : this . config . ssl . ca ,
204
228
cert : this . config . ssl . cert ,
205
229
ciphers : this . config . ssl . ciphers ,
@@ -209,7 +233,7 @@ if (tls.TLSSocket) {
209
233
210
234
var rejectUnauthorized = this . config . ssl . rejectUnauthorized ;
211
235
var secureEstablished = false ;
212
- var secureSocket = new tls . TLSSocket ( connection . stream , {
236
+ var secureSocket = new Tls . TLSSocket ( connection . stream , {
213
237
rejectUnauthorized : rejectUnauthorized ,
214
238
requestCert : true ,
215
239
secureContext : secureContext ,
@@ -244,7 +268,6 @@ if (tls.TLSSocket) {
244
268
console . log ( 'Upgrading connection to TLS' ) ;
245
269
}
246
270
var connection = this ;
247
- var tls = require ( 'tls' ) ;
248
271
var crypto = require ( 'crypto' ) ;
249
272
var config = this . config ;
250
273
var stream = this . stream ;
@@ -256,7 +279,7 @@ if (tls.TLSSocket) {
256
279
ca : config . ssl . ca ,
257
280
ciphers : config . ssl . ciphers
258
281
} ) ;
259
- var securePair = tls . createSecurePair ( credentials , false , true , rejectUnauthorized ) ;
282
+ var securePair = Tls . createSecurePair ( credentials , false , true , rejectUnauthorized ) ;
260
283
261
284
if ( stream . ondata ) {
262
285
stream . ondata = null ;
@@ -278,7 +301,7 @@ if (tls.TLSSocket) {
278
301
279
302
Connection . prototype . pipe = function ( ) {
280
303
var connection = this ;
281
- if ( this . stream instanceof net . Stream ) {
304
+ if ( this . stream instanceof Net . Stream ) {
282
305
this . stream . ondata = function ( data , start , end ) {
283
306
connection . packetParser . execute ( data , start , end ) ;
284
307
} ;
0 commit comments