@@ -227,15 +227,20 @@ function TLSSocket(socket, options) {
227
227
this . authorizationError = null ;
228
228
229
229
// Wrap plain JS Stream into StreamWrap
230
+ var wrap ;
230
231
if ( ! ( socket instanceof net . Socket ) && socket instanceof Duplex )
231
- socket = new StreamWrap ( socket ) ;
232
+ wrap = new StreamWrap ( socket ) ;
233
+ else if ( ( socket instanceof net . Socket ) && ! socket . _handle )
234
+ wrap = new StreamWrap ( socket ) ;
235
+ else
236
+ wrap = socket ;
232
237
233
238
// Just a documented property to make secure sockets
234
239
// distinguishable from regular ones.
235
240
this . encrypted = true ;
236
241
237
242
net . Socket . call ( this , {
238
- handle : this . _wrapHandle ( socket && socket . _handle ) ,
243
+ handle : this . _wrapHandle ( wrap && wrap . _handle ) ,
239
244
allowHalfOpen : socket && socket . allowHalfOpen ,
240
245
readable : false ,
241
246
writable : false
@@ -246,7 +251,7 @@ function TLSSocket(socket, options) {
246
251
247
252
this . on ( 'error' , this . _tlsError ) ;
248
253
249
- this . _init ( socket ) ;
254
+ this . _init ( socket , wrap ) ;
250
255
251
256
// Make sure to setup all required properties like: `_connecting` before
252
257
// starting the flow of the data
@@ -302,7 +307,7 @@ TLSSocket.prototype._wrapHandle = function(handle) {
302
307
return res ;
303
308
} ;
304
309
305
- TLSSocket . prototype . _init = function ( socket ) {
310
+ TLSSocket . prototype . _init = function ( socket , wrap ) {
306
311
var self = this ;
307
312
var options = this . _tlsOptions ;
308
313
var ssl = this . _handle ;
@@ -394,24 +399,26 @@ TLSSocket.prototype._init = function(socket) {
394
399
ssl . receive ( buf ) ;
395
400
}
396
401
397
- if ( socket ) {
402
+ if ( socket instanceof net . Socket ) {
398
403
this . _parent = socket ;
399
404
400
405
// To prevent assertion in afterConnect() and properly kick off readStart
401
- this . _connecting = socket . _connecting ;
406
+ this . _connecting = socket . _connecting || ! socket . _handle ;
402
407
socket . once ( 'connect' , function ( ) {
403
408
self . _connecting = false ;
404
409
self . emit ( 'connect' ) ;
405
410
} ) ;
406
-
407
- socket . on ( 'error' , function ( err ) {
408
- self . _tlsError ( err ) ;
409
- } ) ;
410
411
}
411
412
412
413
// Assume `tls.connect()`
413
- if ( ! socket )
414
+ if ( wrap ) {
415
+ wrap . on ( 'error' , function ( err ) {
416
+ self . _tlsError ( err ) ;
417
+ } ) ;
418
+ } else {
419
+ assert ( ! socket ) ;
414
420
this . _connecting = true ;
421
+ }
415
422
} ;
416
423
417
424
TLSSocket . prototype . renegotiate = function ( options , callback ) {
@@ -506,6 +513,7 @@ TLSSocket.prototype._start = function() {
506
513
return ;
507
514
}
508
515
516
+ debug ( 'start' ) ;
509
517
if ( this . _tlsOptions . requestOCSP )
510
518
this . _handle . requestOCSP ( ) ;
511
519
this . _handle . start ( ) ;
0 commit comments