11import crypto from "crypto"
22import os from "os"
33
4+ import type { Socket } from "socket.io-client"
5+
46import {
57 type TaskProviderLike ,
68 type CloudUserInfo ,
@@ -91,7 +93,7 @@ export class BridgeOrchestrator {
9193 await options . provider . getTelemetryProperties ( )
9294
9395 BridgeOrchestrator . instance = new BridgeOrchestrator ( options )
94- await BridgeOrchestrator . instance . connect ( )
96+ await BridgeOrchestrator . instance . socketTransport . connect ( )
9597 } catch ( error ) {
9698 console . error (
9799 `[BridgeOrchestrator#connectOrDisconnect] connect() failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
@@ -103,7 +105,7 @@ export class BridgeOrchestrator {
103105 if ( connectionState === ConnectionState . FAILED || connectionState === ConnectionState . DISCONNECTED ) {
104106 console . log ( `[BridgeOrchestrator#connectOrDisconnect] Re-connecting... (state: ${ connectionState } )` )
105107
106- instance . reconnect ( ) . catch ( ( error ) => {
108+ instance . socketTransport . reconnect ( ) . catch ( ( error ) => {
107109 console . error (
108110 `[BridgeOrchestrator#connectOrDisconnect] reconnect() failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
109111 )
@@ -124,7 +126,7 @@ export class BridgeOrchestrator {
124126
125127 try {
126128 console . log ( `[BridgeOrchestrator#connectOrDisconnect] Disconnecting... (state: ${ connectionState } )` )
127- await instance . disconnect ( )
129+ await instance . socketTransport . disconnect ( )
128130 } catch ( error ) {
129131 console . error (
130132 `[BridgeOrchestrator#connectOrDisconnect] disconnect() failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
@@ -161,9 +163,18 @@ export class BridgeOrchestrator {
161163 reconnectionDelay : this . RECONNECT_DELAY ,
162164 reconnectionDelayMax : this . RECONNECT_DELAY_MAX ,
163165 } ,
164- onConnect : ( ) => this . handleConnect ( ) ,
165- onDisconnect : ( ) => this . handleDisconnect ( ) ,
166- onReconnect : ( ) => this . handleReconnect ( ) ,
166+ onConnect : async ( socket : Socket ) => {
167+ this . setupSocketListeners ( )
168+ await this . extensionChannel . onConnect ( socket )
169+ } ,
170+ onDisconnect : async ( ) => {
171+ await this . extensionChannel . onDisconnect ( )
172+ await this . extensionChannel . cleanup ( this . socketTransport . getSocket ( ) )
173+ } ,
174+ onReconnect : async ( socket : Socket ) => {
175+ this . setupSocketListeners ( )
176+ await this . extensionChannel . onReconnect ( socket )
177+ } ,
167178 } )
168179
169180 this . extensionChannel = new ExtensionChannel ( {
@@ -193,54 +204,7 @@ export class BridgeOrchestrator {
193204 `[BridgeOrchestrator] on(${ ExtensionSocketEvents . RELAYED_COMMAND } ) -> ${ message . type } for ${ message . instanceId } ` ,
194205 )
195206
196- this . extensionChannel ? .handleCommand ( message )
207+ this . extensionChannel . handleCommand ( message )
197208 } )
198209 }
199-
200- private async handleConnect ( ) {
201- const socket = this . socketTransport . getSocket ( )
202-
203- if ( ! socket ) {
204- console . error ( "[BridgeOrchestrator#handleConnect] Socket not available" )
205- return
206- }
207-
208- await this . extensionChannel . onConnect ( socket )
209- }
210-
211- private handleDisconnect ( ) {
212- this . extensionChannel . onDisconnect ( )
213- }
214-
215- private async handleReconnect ( ) {
216- const socket = this . socketTransport . getSocket ( )
217-
218- if ( ! socket ) {
219- console . error ( "[BridgeOrchestrator] Socket not available after reconnect" )
220- return
221- }
222-
223- // Re-setup socket listeners to ensure they're properly configured
224- // after automatic reconnection (Socket.IO's built-in reconnection)
225- // The socket.off() calls in setupSocketListeners prevent duplicates
226- this . setupSocketListeners ( )
227-
228- await this . extensionChannel . onReconnect ( socket )
229- }
230-
231- private async connect ( ) : Promise < void > {
232- await this . socketTransport . connect ( )
233- this . setupSocketListeners ( )
234- }
235-
236- private async disconnect ( ) : Promise < void > {
237- await this . extensionChannel . cleanup ( this . socketTransport . getSocket ( ) )
238- await this . socketTransport . disconnect ( )
239- BridgeOrchestrator . instance = null
240- }
241-
242- private async reconnect ( ) : Promise < void > {
243- await this . socketTransport . reconnect ( )
244- this . setupSocketListeners ( )
245- }
246210}
0 commit comments