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