1
1
import crypto from "crypto"
2
+ import os from "os"
2
3
3
4
import {
4
5
type TaskProviderLike ,
5
6
type TaskLike ,
6
7
type CloudUserInfo ,
7
8
type ExtensionBridgeCommand ,
8
9
type TaskBridgeCommand ,
10
+ type StaticAppProperties ,
11
+ type GitProperties ,
9
12
ConnectionState ,
10
13
ExtensionSocketEvents ,
11
14
TaskSocketEvents ,
@@ -39,6 +42,8 @@ export class BridgeOrchestrator {
39
42
private readonly token : string
40
43
private readonly provider : TaskProviderLike
41
44
private readonly instanceId : string
45
+ private readonly appProperties : StaticAppProperties
46
+ private readonly gitProperties ?: GitProperties
42
47
43
48
// Components
44
49
private socketTransport : SocketTransport
@@ -61,66 +66,72 @@ export class BridgeOrchestrator {
61
66
public static async connectOrDisconnect (
62
67
userInfo : CloudUserInfo | null ,
63
68
remoteControlEnabled : boolean | undefined ,
64
- options ? : BridgeOrchestratorOptions ,
69
+ options : BridgeOrchestratorOptions ,
65
70
) : 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 ) {
67
79
const instance = BridgeOrchestrator . instance
68
80
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 ( )
94
86
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
+ )
105
93
}
106
94
} 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
+ )
112
102
113
- await instance . disconnect ( )
114
- } catch ( error ) {
103
+ instance . reconnect ( ) . catch ( ( error ) => {
115
104
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 ) } ` ,
117
106
)
118
- } finally {
119
- BridgeOrchestrator . instance = null
120
- }
107
+ } )
121
108
} 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
123
132
}
133
+ } else {
134
+ console . log ( `[BridgeOrchestrator#connectOrDisconnect] Already disconnected` )
124
135
}
125
136
}
126
137
@@ -146,6 +157,8 @@ export class BridgeOrchestrator {
146
157
this . token = options . token
147
158
this . provider = options . provider
148
159
this . instanceId = options . sessionId || crypto . randomUUID ( )
160
+ this . appProperties = { ...options . provider . appProperties , hostname : os . hostname ( ) }
161
+ this . gitProperties = options . provider . gitProperties
149
162
150
163
this . socketTransport = new SocketTransport ( {
151
164
url : this . socketBridgeUrl ,
@@ -166,8 +179,19 @@ export class BridgeOrchestrator {
166
179
onReconnect : ( ) => this . handleReconnect ( ) ,
167
180
} )
168
181
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
+ } )
171
195
}
172
196
173
197
private setupSocketListeners ( ) {
@@ -288,9 +312,6 @@ export class BridgeOrchestrator {
288
312
}
289
313
290
314
private async connect ( ) : Promise < void > {
291
- // Populate the app and git properties before registering the instance.
292
- await this . provider . getTelemetryProperties ( )
293
-
294
315
await this . socketTransport . connect ( )
295
316
this . setupSocketListeners ( )
296
317
}
0 commit comments