11import crypto from "crypto"
2+ import os from "os"
23
34import {
45 type TaskProviderLike ,
56 type TaskLike ,
67 type CloudUserInfo ,
78 type ExtensionBridgeCommand ,
89 type TaskBridgeCommand ,
10+ type StaticAppProperties ,
11+ type GitProperties ,
912 ConnectionState ,
1013 ExtensionSocketEvents ,
1114 TaskSocketEvents ,
@@ -39,6 +42,8 @@ export class BridgeOrchestrator {
3942 private readonly token : string
4043 private readonly provider : TaskProviderLike
4144 private readonly instanceId : string
45+ private readonly appProperties : StaticAppProperties
46+ private readonly gitProperties ?: GitProperties
4247
4348 // Components
4449 private socketTransport : SocketTransport
@@ -61,66 +66,72 @@ export class BridgeOrchestrator {
6166 public static async connectOrDisconnect (
6267 userInfo : CloudUserInfo | null ,
6368 remoteControlEnabled : boolean | undefined ,
64- options ? : BridgeOrchestratorOptions ,
69+ options : BridgeOrchestratorOptions ,
6570 ) : Promise < void > {
66- const isEnabled = BridgeOrchestrator . isEnabled ( userInfo , remoteControlEnabled )
71+ if ( BridgeOrchestrator . isEnabled ( userInfo , remoteControlEnabled ) ) {
72+ await BridgeOrchestrator . connect ( options )
73+ } else {
74+ await BridgeOrchestrator . disconnect ( )
75+ }
76+ }
77+
78+ public static async connect ( options : BridgeOrchestratorOptions ) {
6779 const instance = BridgeOrchestrator . instance
6880
69- if ( isEnabled ) {
70- if ( ! instance ) {
71- if ( ! options ) {
72- console . error (
73- `[BridgeOrchestrator#connectOrDisconnect] Cannot connect: options are required for connection` ,
74- )
75- return
76- }
77- try {
78- console . log ( `[BridgeOrchestrator#connectOrDisconnect] Connecting...` )
79- BridgeOrchestrator . instance = new BridgeOrchestrator ( options )
80- await BridgeOrchestrator . instance . connect ( )
81- } catch ( error ) {
82- console . error (
83- `[BridgeOrchestrator#connectOrDisconnect] connect() failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
84- )
85- }
86- } else {
87- if (
88- instance . connectionState === ConnectionState . FAILED ||
89- instance . connectionState === ConnectionState . DISCONNECTED
90- ) {
91- console . log (
92- `[BridgeOrchestrator#connectOrDisconnect] Re-connecting... (state: ${ instance . connectionState } )` ,
93- )
81+ if ( ! instance ) {
82+ try {
83+ console . log ( `[BridgeOrchestrator#connectOrDisconnect] Connecting...` )
84+ // Populate telemetry properties before registering the instance.
85+ await options . provider . getTelemetryProperties ( )
9486
95- instance . reconnect ( ) . catch ( ( error ) => {
96- console . error (
97- `[BridgeOrchestrator#connectOrDisconnect] reconnect() failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
98- )
99- } )
100- } else {
101- console . log (
102- `[BridgeOrchestrator#connectOrDisconnect] Already connected or connecting (state: ${ instance . connectionState } )` ,
103- )
104- }
87+ BridgeOrchestrator . instance = new BridgeOrchestrator ( options )
88+ await BridgeOrchestrator . instance . connect ( )
89+ } catch ( error ) {
90+ console . error (
91+ `[BridgeOrchestrator#connectOrDisconnect] connect() failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
92+ )
10593 }
10694 } else {
107- if ( instance ) {
108- try {
109- console . log (
110- `[BridgeOrchestrator#connectOrDisconnect] Disconnecting... (state: ${ instance . connectionState } )` ,
111- )
95+ if (
96+ instance . connectionState === ConnectionState . FAILED ||
97+ instance . connectionState === ConnectionState . DISCONNECTED
98+ ) {
99+ console . log (
100+ `[BridgeOrchestrator#connectOrDisconnect] Re-connecting... (state: ${ instance . connectionState } )` ,
101+ )
112102
113- await instance . disconnect ( )
114- } catch ( error ) {
103+ instance . reconnect ( ) . catch ( ( error ) => {
115104 console . error (
116- `[BridgeOrchestrator#connectOrDisconnect] disconnect () failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
105+ `[BridgeOrchestrator#connectOrDisconnect] reconnect () failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
117106 )
118- } finally {
119- BridgeOrchestrator . instance = null
120- }
107+ } )
121108 } else {
122- console . log ( `[BridgeOrchestrator#connectOrDisconnect] Already disconnected` )
109+ console . log (
110+ `[BridgeOrchestrator#connectOrDisconnect] Already connected or connecting (state: ${ instance . connectionState } )` ,
111+ )
112+ }
113+ }
114+ }
115+
116+ public static async disconnect ( ) {
117+ const instance = BridgeOrchestrator . instance
118+
119+ if ( instance ) {
120+ try {
121+ console . log (
122+ `[BridgeOrchestrator#connectOrDisconnect] Disconnecting... (state: ${ instance . connectionState } )` ,
123+ )
124+
125+ await instance . disconnect ( )
126+ } catch ( error ) {
127+ console . error (
128+ `[BridgeOrchestrator#connectOrDisconnect] disconnect() failed: ${ error instanceof Error ? error . message : String ( error ) } ` ,
129+ )
130+ } finally {
131+ BridgeOrchestrator . instance = null
123132 }
133+ } else {
134+ console . log ( `[BridgeOrchestrator#connectOrDisconnect] Already disconnected` )
124135 }
125136 }
126137
@@ -146,6 +157,8 @@ export class BridgeOrchestrator {
146157 this . token = options . token
147158 this . provider = options . provider
148159 this . instanceId = options . sessionId || crypto . randomUUID ( )
160+ this . appProperties = { ...options . provider . appProperties , hostname : os . hostname ( ) }
161+ this . gitProperties = options . provider . gitProperties
149162
150163 this . socketTransport = new SocketTransport ( {
151164 url : this . socketBridgeUrl ,
@@ -166,8 +179,19 @@ export class BridgeOrchestrator {
166179 onReconnect : ( ) => this . handleReconnect ( ) ,
167180 } )
168181
169- this . extensionChannel = new ExtensionChannel ( this . instanceId , this . userId , this . provider )
170- this . taskChannel = new TaskChannel ( this . instanceId )
182+ this . extensionChannel = new ExtensionChannel ( {
183+ instanceId : this . instanceId ,
184+ appProperties : this . appProperties ,
185+ gitProperties : this . gitProperties ,
186+ userId : this . userId ,
187+ provider : this . provider ,
188+ } )
189+
190+ this . taskChannel = new TaskChannel ( {
191+ instanceId : this . instanceId ,
192+ appProperties : this . appProperties ,
193+ gitProperties : this . gitProperties ,
194+ } )
171195 }
172196
173197 private setupSocketListeners ( ) {
@@ -288,9 +312,6 @@ export class BridgeOrchestrator {
288312 }
289313
290314 private async connect ( ) : Promise < void > {
291- // Populate the app and git properties before registering the instance.
292- await this . provider . getTelemetryProperties ( )
293-
294315 await this . socketTransport . connect ( )
295316 this . setupSocketListeners ( )
296317 }
0 commit comments