@@ -66,16 +66,7 @@ class CFSocket extends EventEmitter {
66
66
const { connect } = await import ( 'cloudflare:sockets' )
67
67
this . _cfSocket = connect ( `${ host } :${ port } ` , options )
68
68
this . _cfWriter = this . _cfSocket . writable . getWriter ( )
69
-
70
- this . _cfSocket . closed . then ( ( ) => {
71
- if ( ! this . _upgrading ) {
72
- log ( 'CF socket closed' )
73
- this . _cfSocket = null
74
- this . emit ( 'close' )
75
- } else {
76
- this . _upgrading = false
77
- }
78
- } )
69
+ this . _addClosedHandler ( )
79
70
80
71
this . _cfReader = this . _cfSocket . readable . getReader ( )
81
72
if ( this . ssl ) {
@@ -149,14 +140,35 @@ class CFSocket extends EventEmitter {
149
140
}
150
141
151
142
startTls ( options ) {
143
+ if ( this . _upgraded ) {
144
+ // Don't try to upgrade again.
145
+ this . emit ( 'error' , 'Cannot call `startTls()` more than once on a socket' )
146
+ return
147
+ }
152
148
this . _cfWriter . releaseLock ( )
153
149
this . _cfReader . releaseLock ( )
154
150
this . _upgrading = true
155
151
this . _cfSocket = this . _cfSocket . startTls ( options )
156
152
this . _cfWriter = this . _cfSocket . writable . getWriter ( )
157
153
this . _cfReader = this . _cfSocket . readable . getReader ( )
154
+ this . _addClosedHandler ( )
158
155
this . _listen ( ) . catch ( ( e ) => this . emit ( 'error' , e ) )
159
156
}
157
+
158
+ _addClosedHandler ( ) {
159
+ this . _cfSocket . closed
160
+ . then ( ( ) => {
161
+ if ( ! this . _upgrading ) {
162
+ log ( 'CF socket closed' )
163
+ this . _cfSocket = null
164
+ this . emit ( 'close' )
165
+ } else {
166
+ this . _upgrading = false
167
+ this . _upgraded = true
168
+ }
169
+ } )
170
+ . catch ( ( e ) => this . emit ( 'error' , e ) )
171
+ }
160
172
}
161
173
162
174
const debug = false
0 commit comments