ide на основе vs code · VibeGit
ide на основе vs code Это ide на основе vs code(code oss), в ней будет встроенный умный апи роутинг, свой маркет с плагинами(агент харнесс, поддержка java/kotlin/мобильной разработки, командная разработка проектов, orca, оркестратор агентов
krutaya-ide-na-osnove-vs-code / src / vs / platform / remote / common / remoteAgentConnection.ts 840 строк · изменён 14 нояб., 04:52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
import { CancelablePromise, createCancelablePromise, promiseWithResolvers } from '../../../base/common/async.js' ;
import { VSBuffer } from '../../../base/common/buffer.js' ;
import { CancellationToken, CancellationTokenSource } from '../../../base/common/cancellation.js' ;
import { isCancellationError, onUnexpectedError } from '../../../base/common/errors.js' ;
import { Emitter } from '../../../base/common/event.js' ;
import { Disposable, DisposableStore, IDisposable, toDisposable } from '../../../base/common/lifecycle.js' ;
import RemoteAuthorities
performance
StopWatch
generateUuid
IIPCLogger
Client ISocket PersistentProtocol ProtocolConstants SocketCloseEventType
ILogService
RemoteAgentConnectionContext
RemoteAuthorityResolverError RemoteConnection
IRemoteSocketFactoryService
ISignService
ConnectionType
Management
ExtensionHost
Tunnel
connectionType ConnectionType
connectionType
ConnectionType Management
ConnectionType ExtensionHost
ConnectionType Tunnel
type
auth
data
type
data
signedData
type
commit
signedData
desiredConnectionType ConnectionType
args
type
reason
type
AuthRequest SignRequest ConnectionTypeRequest ErrorMessage OKMessage
commit
quality
connectTo
connectionToken
reconnectionToken
reconnectionProtocol PersistentProtocol
remoteSocketFactoryService IRemoteSocketFactoryService
signService ISignService
logService ILogService
millis CancellationToken
source
source millis
source token
a CancellationToken b CancellationToken CancellationToken
a isCancellationRequested b isCancellationRequested
CancellationToken Cancelled
source
a source
b source
source token
_state
_disposables DisposableStore
promise
value
err
_state
timeoutCancellationToken CancellationToken
_state
_disposables
promise promise resolve _resolvePromise reject _rejectPromise
timeoutCancellationToken isCancellationRequested
_disposables timeoutCancellationToken
disposable IDisposable
_state
_disposables disposable
disposable
_state
_disposables
_state
Error
err
err code
err syscall
err
value
_state
_disposables
_state
value
err
_state
_disposables
_state
err
protocol PersistentProtocol timeoutCancellationToken CancellationToken
result timeoutCancellationToken
result protocol raw
msg raw
error msg
error
result error
result msg
result promise
logService ILogService remoteSocketFactoryService IRemoteSocketFactoryService connectTo path query debugConnectionType debugLabel timeoutCancellationToken CancellationToken ISocket
result timeoutCancellationToken
sw StopWatch
logService
performance
remoteSocketFactoryService connectTo path query debugLabel socket
result didTimeout
performance
logService
socket
performance
logService
result socket
err
performance
logService
logService err
result err
result promise
promise timeoutCancellationToken CancellationToken
result timeoutCancellationToken
promise
res
result didTimeout
result res
err
result didTimeout
result err
result promise
options ISimpleConnectionOptions connectionType ConnectionType args timeoutCancellationToken CancellationToken protocol PersistentProtocol ownsProtocol
logPrefix options connectionType
options logService
socket ISocket
socket options logService options remoteSocketFactoryService options connectTo RemoteAuthorities connectionType timeoutCancellationToken
error
options logService
options logService error
error
options logService
protocol PersistentProtocol
ownsProtocol
options reconnectionProtocol
options reconnectionProtocol socket
protocol options reconnectionProtocol
ownsProtocol
protocol socket
ownsProtocol
options logService
message options signService timeoutCancellationToken
authRequest AuthRequest
type
auth options connectionToken
data message data
protocol VSBuffer authRequest
msg protocol timeoutCancellationToken
msg type msg data
error
error code
error
options logService
isValid options signService message msg signedData timeoutCancellationToken
isValid
error
error code
error
signed options signService msg data timeoutCancellationToken
connTypeRequest ConnectionTypeRequest
type
commit options commit
signedData signed
desiredConnectionType connectionType
args
connTypeRequest args args
options logService
protocol VSBuffer connTypeRequest
protocol ownsProtocol
error
error error code
options logService
options logService error
error error code
options logService
options logService error
ownsProtocol
protocol
error
protocol PersistentProtocol
options ISimpleConnectionOptions connectionType ConnectionType args timeoutCancellationToken CancellationToken protocol PersistentProtocol firstMessage
startTime Date
logPrefix options connectionType
protocol ownsProtocol options connectionType args timeoutCancellationToken
result timeoutCancellationToken
result protocol raw
msg raw
error msg
error
options logService
options logService error
ownsProtocol
protocol
result error
options reconnectionProtocol
options logService
result protocol firstMessage msg
result promise
options ISimpleConnectionOptions timeoutCancellationToken CancellationToken IManagementConnectionResult
protocol options ConnectionType Management timeoutCancellationToken
protocol
language
debugId
port
env key
protocol PersistentProtocol
debugPort
options ISimpleConnectionOptions startArguments IRemoteExtensionHostStartParams timeoutCancellationToken CancellationToken IExtensionHostConnectionResult
protocol firstMessage options ConnectionType ExtensionHost startArguments timeoutCancellationToken
debugPort firstMessage firstMessage debugPort
protocol debugPort
host
port
options ISimpleConnectionOptions startParams ITunnelConnectionStartParams timeoutCancellationToken CancellationToken PersistentProtocol
startTime Date
logPrefix options ConnectionType Tunnel
protocol options ConnectionType Tunnel startParams timeoutCancellationToken
options logService
protocol
commit
quality
addressProvider IAddressProvider
remoteSocketFactoryService IRemoteSocketFactoryService
signService ISignService
logService ILogService
ipcLogger IIPCLogger
options IConnectionOptions reconnectionToken reconnectionProtocol PersistentProtocol ISimpleConnectionOptions
connectTo connectionToken options addressProvider
commit options commit
quality options quality
connectTo
connectionToken connectionToken
reconnectionToken reconnectionToken
reconnectionProtocol reconnectionProtocol
remoteSocketFactoryService options remoteSocketFactoryService
signService options signService
logService options logService
connectTo
connectionToken
IAddress
options IConnectionOptions remoteAuthority clientId ManagementPersistentConnection
options
simpleOptions
protocol simpleOptions CancellationToken None
options remoteAuthority clientId simpleOptions reconnectionToken protocol
options IConnectionOptions startArguments IRemoteExtensionHostStartParams ExtensionHostPersistentConnection
options
simpleOptions
protocol debugPort simpleOptions startArguments CancellationToken None
options startArguments simpleOptions reconnectionToken protocol debugPort
options IConnectionOptions simpleOptions ISimpleConnectionOptions
attempt attempt
reconnectionToken
simpleOptions options reconnectionToken
result simpleOptions
result
err
attempt
options logService
options logService err
options logService
options logService err
PersistentConnection RemoteAuthorityResolverError err
err
options IConnectionOptions tunnelRemoteHost tunnelRemotePort PersistentProtocol
simpleOptions options
protocol simpleOptions host tunnelRemoteHost port tunnelRemotePort CancellationToken None
protocol
seconds CancelablePromise
token
resolve reject
timeout resolve seconds
token
timeout
PersistentConnectionEventType
ConnectionLost
ReconnectionWait
ReconnectionRunning
ReconnectionPermanentFailure
ConnectionGain
type PersistentConnectionEventType ConnectionLost
reconnectionToken
millisSinceLastIncomingData
type PersistentConnectionEventType ReconnectionWait
reconnectionToken
millisSinceLastIncomingData
durationSeconds
cancellableTimer CancelablePromise
cancellableTimer
type PersistentConnectionEventType ReconnectionRunning
reconnectionToken
millisSinceLastIncomingData
attempt
type PersistentConnectionEventType ConnectionGain
reconnectionToken
millisSinceLastIncomingData
attempt
type PersistentConnectionEventType ReconnectionPermanentFailure
reconnectionToken
millisSinceLastIncomingData
attempt
handled
ConnectionGainEvent ConnectionLostEvent ReconnectionWaitEvent ReconnectionRunningEvent ReconnectionPermanentFailureEvent
millisSinceLastIncomingData attempt handled
_permanentFailure
_permanentFailureMillisSinceLastIncomingData millisSinceLastIncomingData
_permanentFailureAttempt attempt
_permanentFailureHandled handled
_instances instance instance _permanentFailureMillisSinceLastIncomingData _permanentFailureAttempt _permanentFailureHandled
_instances instance instance
_instances instance instance
_permanentFailure
_permanentFailureMillisSinceLastIncomingData
_permanentFailureAttempt
_permanentFailureHandled
_instances PersistentConnection
_onDidStateChange
onDidStateChange _onDidStateChange event
_permanentFailure
_permanentFailure PersistentConnection _permanentFailure
_isReconnecting
_isDisposed
_reconnectionGraceTime ProtocolConstants ReconnectionGraceTime
_connectionType ConnectionType
_options IConnectionOptions
reconnectionToken
protocol PersistentProtocol
_reconnectionFailureIsFatal
_onDidStateChange reconnectionToken
protocol e
logPrefix _connectionType reconnectionToken
e
_options logService
e type SocketCloseEventType NodeSocketCloseEvent
_options logService
e error
_options logService e error
_options logService
e event
_options logService e event
protocol e
logPrefix _connectionType reconnectionToken
_options logService
PersistentConnection _instances
myIndex PersistentConnection _instances
myIndex
PersistentConnection _instances myIndex
_isPermanentFailure
PersistentConnection _permanentFailureMillisSinceLastIncomingData PersistentConnection _permanentFailureAttempt PersistentConnection _permanentFailureHandled
graceTime
sanitizedGrace graceTime ProtocolConstants ReconnectionGraceTime
logPrefix _connectionType reconnectionToken
_options logService
_reconnectionGraceTime sanitizedGrace
override
_isDisposed
_isReconnecting
_isReconnecting
_isReconnecting
_isPermanentFailure _isDisposed
logPrefix _connectionType reconnectionToken
_options logService
_onDidStateChange reconnectionToken protocol
graceTime _reconnectionGraceTime
_options logService
graceTime
_options logService
protocol
loopStartTime Date
attempt
attempt
waitTime attempt length attempt length
waitTime
sleepPromise waitTime
_onDidStateChange reconnectionToken protocol waitTime sleepPromise
_options logService
sleepPromise
_isPermanentFailure
_options logService
_onDidStateChange reconnectionToken protocol attempt
_options logService
simpleOptions _options reconnectionToken protocol
_options logService
simpleOptions
_options logService
_onDidStateChange reconnectionToken protocol attempt
err
err code
_options logService
_options logService err
protocol attempt
Date loopStartTime graceTime
graceSeconds Math graceTime
_options logService
_options logService err
protocol attempt
RemoteAuthorityResolverError err
_options logService
_options logService err
err code err code err code err code err syscall
_options logService
_options logService err
err
_options logService
_options logService err
err
_options logService
_options logService err
protocol attempt RemoteAuthorityResolverError err
_options logService
_options logService err
protocol attempt
_isPermanentFailure _isDisposed
millisSinceLastIncomingData attempt handled
_reconnectionFailureIsFatal
PersistentConnection millisSinceLastIncomingData attempt handled
millisSinceLastIncomingData attempt handled
millisSinceLastIncomingData attempt handled
_onDidStateChange reconnectionToken millisSinceLastIncomingData attempt handled
protocol
protocol
options ISimpleConnectionOptions timeoutCancellationToken CancellationToken
client Client RemoteAgentConnectionContext
options IConnectionOptions remoteAuthority clientId reconnectionToken protocol PersistentProtocol
ConnectionType Management options reconnectionToken protocol
client protocol
remoteAuthority remoteAuthority
clientId clientId
options ipcLogger
options ISimpleConnectionOptions timeoutCancellationToken CancellationToken
options timeoutCancellationToken
_startArguments IRemoteExtensionHostStartParams
debugPort
options IConnectionOptions startArguments IRemoteExtensionHostStartParams reconnectionToken protocol PersistentProtocol debugPort
ConnectionType ExtensionHost options reconnectionToken protocol
_startArguments startArguments
debugPort debugPort
options ISimpleConnectionOptions timeoutCancellationToken CancellationToken
options _startArguments timeoutCancellationToken
protocol PersistentProtocol
protocol
socket protocol
protocol
socket
err
err
msg Error
msg msg type
error
error code
error
candidate fallback
candidate candidate candidate
fallback
candidate Number
Number
Math candidate
str len
str length len
str
str
connectionType ConnectionType reconnectionToken
connectionType ConnectionType reconnectionToken isReconnect
options ISimpleConnectionOptions connectionType ConnectionType
startTime
{
}
from
'../../../base/common/network.js'
;
import
*
as
from
'../../../base/common/performance.js'
;
import
{
}
from
'../../../base/common/stopwatch.js'
;
import
{
}
from
'../../../base/common/uuid.js'
;
import
{
}
from
'../../../base/parts/ipc/common/ipc.js'
;
import
{
,
,
,
,
}
from
'../../../base/parts/ipc/common/ipc.net.js'
;
import
{
}
from
'../../log/common/log.js'
;
import
{
}
from
'./remoteAgentEnvironment.js'
;
import
{
,
}
from
'./remoteAuthorityResolver.js'
;
import
{
}
from
'./remoteSocketFactoryService.js'
;
import
{
}
from
'../../sign/common/sign.js'
;
const
RECONNECT_TIMEOUT
=
30
*
1000
;
export
const
enum
{
=
1
,
=
2
,
=
3
,
}
function
connectionTypeToString
(
:
)
:
string
{
switch
(
)
{
case
.
:
return
'Management'
;
case
.
:
return
'ExtensionHost'
;
case
.
:
return
'Tunnel'
;
}
}
export
interface
AuthRequest
{
:
'auth'
;
:
string
;
:
string
;
}
export
interface
SignRequest
{
:
'sign'
;
:
string
;
:
string
;
}
export
interface
ConnectionTypeRequest
{
:
'connectionType'
;
?
:
string
;
:
string
;
?
:
;
?
:
any
;
}
export
interface
ErrorMessage
{
:
'error'
;
:
string
;
}
export
interface
OKMessage
{
:
'ok'
;
}
export
type
HandshakeMessage
=
|
|
|
|
;
interface
ISimpleConnectionOptions< T extends RemoteConnection = RemoteConnection>
{
:
string
|
undefined
;
:
string
|
undefined
;
:
T
;
:
string
|
undefined
;
:
string
;
:
|
null
;
:
;
:
;
:
;
}
function
createTimeoutCancellation
(
:
number
)
:
{
const
=
new
CancellationTokenSource
(
)
;
setTimeout
(
(
)
=>
.
cancel
(
)
,
)
;
return
.
;
}
function
combineTimeoutCancellation
(
:
,
:
)
:
{
if
(
.
||
.
)
{
return
.
;
}
const
=
new
CancellationTokenSource
(
)
;
.
onCancellationRequested
(
(
)
=>
.
cancel
(
)
)
;
.
onCancellationRequested
(
(
)
=>
.
cancel
(
)
)
;
return
.
;
}
class
PromiseWithTimeout< T >
{
private
:
'pending'
|
'resolved'
|
'rejected'
|
'timedout'
;
private
readonly
:
;
public
readonly
:
Promise
<
T
>
;
private
readonly
_resolvePromise
:
(
:
T
)
=>
void
;
private
readonly
_rejectPromise
:
(
:
any
)
=>
void
;
public
get
didTimeout
(
)
:
boolean
{
return
(
this
.
===
'timedout'
)
;
}
constructor
(
:
)
{
this
.
=
'pending'
;
this
.
=
new
DisposableStore
(
)
;
(
{
:
this
.
,
:
this
.
,
:
this
.
}
=
promiseWithResolvers < T >
(
)
)
;
if
(
.
)
{
this
.
_timeout
(
)
;
}
else
{
this
.
.
add
(
.
onCancellationRequested
(
(
)
=>
this
.
_timeout
(
)
)
)
;
}
}
public
registerDisposable
(
:
)
:
void
{
if
(
this
.
===
'pending'
)
{
this
.
.
add
(
)
;
}
else
{
.
dispose
(
)
;
}
}
private
_timeout
(
)
:
void
{
if
(
this
.
!==
'pending'
)
{
return
;
}
this
.
.
dispose
(
)
;
this
.
=
'timedout'
;
this
.
_rejectPromise
(
this
.
_createTimeoutError
(
)
)
;
}
private
_createTimeoutError
(
)
:
{
const
:
any
=
new
Error
(
'Time limit reached'
)
;
.
=
'ETIMEDOUT'
;
.
=
'connect'
;
return
;
}
public
resolve
(
:
T
)
:
void
{
if
(
this
.
!==
'pending'
)
{
return
;
}
this
.
.
dispose
(
)
;
this
.
=
'resolved'
;
this
.
_resolvePromise
(
)
;
}
public
reject
(
:
any
)
:
void
{
if
(
this
.
!==
'pending'
)
{
return
;
}
this
.
.
dispose
(
)
;
this
.
=
'rejected'
;
this
.
_rejectPromise
(
)
;
}
}
function
readOneControlMessage < T >
(
:
,
:
)
:
Promise
<
T
>
{
const
=
new
PromiseWithTimeout< T >
(
)
;
.
registerDisposable
(
.
onControlMessage
(
=>
{
const
:
T
=
JSON
.
parse
(
.
toString
(
)
)
;
const
=
getErrorFromMessage
(
)
;
if
(
)
{
.
reject
(
)
;
}
else
{
.
resolve
(
)
;
}
}
)
)
;
return
.
;
}
function
createSocket < T extends RemoteConnection>
(
:
,
:
,
:
T
,
:
string
,
:
string
,
:
string
,
:
string
,
:
)
:
Promise
<
>
{
const
=
new
PromiseWithTimeout< ISocket>
(
)
;
const
=
.
create
(
false
)
;
.
info
(
` Creating a socket ( ${ debugLabel} )... `
)
;
.
mark
(
` code/willCreateSocket/ ${ debugConnectionType} `
)
;
.
connect
(
,
,
,
)
.
then
(
(
)
=>
{
if
(
.
)
{
.
mark
(
` code/didCreateSocketError/ ${ debugConnectionType} `
)
;
.
info
(
` Creating a socket ( ${ debugLabel} ) finished after ${ sw. elapsed ( ) } ms, but this is too late and has timed out already. `
)
;
?.
dispose
(
)
;
}
else
{
.
mark
(
` code/didCreateSocketOK/ ${ debugConnectionType} `
)
;
.
info
(
` Creating a socket ( ${ debugLabel} ) was successful after ${ sw. elapsed ( ) } ms. `
)
;
.
resolve
(
)
;
}
}
,
(
)
=>
{
.
mark
(
` code/didCreateSocketError/ ${ debugConnectionType} `
)
;
.
info
(
` Creating a socket ( ${ debugLabel} ) returned an error after ${ sw. elapsed ( ) } ms. `
)
;
.
error
(
)
;
.
reject
(
)
;
}
)
;
return
.
;
}
function
raceWithTimeoutCancellation < T >
(
:
Promise
<
T
>
,
:
)
:
Promise
<
T
>
{
const
=
new
PromiseWithTimeout< T >
(
)
;
.
then
(
(
)
=>
{
if
(
!
.
)
{
.
resolve
(
)
;
}
}
,
(
)
=>
{
if
(
!
.
)
{
.
reject
(
)
;
}
}
)
;
return
.
;
}
async
function
connectToRemoteExtensionHostAgent < T extends RemoteConnection>
(
:
<
T
>
,
:
,
:
any
|
undefined
,
:
)
:
Promise
<
{
:
;
:
boolean
}
>
{
const
=
connectLogPrefix
(
,
)
;
.
.
trace
(
` ${ logPrefix} 1/6. invoking socketFactory.connect(). `
)
;
let
:
;
try
{
=
await
createSocket
(
.
,
.
,
.
,
.
getServerRootPath
(
)
,
` reconnectionToken= ${ options. reconnectionToken} &reconnection= ${ options. reconnectionProtocol ? 'true' : 'false' } `
,
connectionTypeToString
(
)
,
` renderer- ${ connectionTypeToString ( connectionType) } - ${ options. reconnectionToken} `
,
)
;
}
catch
(
)
{
.
.
error
(
` ${ logPrefix} socketFactory.connect() failed or timed out. Error: `
)
;
.
.
error
(
)
;
throw
;
}
.
.
trace
(
` ${ logPrefix} 2/6. socketFactory.connect() was successful. `
)
;
let
:
;
let
:
boolean
;
if
(
.
)
{
.
.
beginAcceptReconnection
(
,
null
)
;
=
.
;
=
false
;
}
else
{
=
new
PersistentProtocol
(
{
}
)
;
=
true
;
}
.
.
trace
(
` ${ logPrefix} 3/6. sending AuthRequest control message. `
)
;
const
=
await
raceWithTimeoutCancellation
(
.
.
createNewMessage
(
generateUuid
(
)
)
,
)
;
const
:
=
{
:
'auth'
,
:
.
||
'00000000000000000000'
,
:
.
}
;
.
sendControl
(
.
fromString
(
JSON
.
stringify
(
)
)
)
;
try
{
const
=
await
readOneControlMessage < HandshakeMessage>
(
,
combineTimeoutCancellation
(
,
createTimeoutCancellation
(
10000
)
)
)
;
if
(
.
!==
'sign'
||
typeof
.
!==
'string'
)
{
const
:
any
=
new
Error
(
'Unexpected handshake message'
)
;
.
=
'VSCODE_CONNECTION_ERROR'
;
throw
;
}
.
.
trace
(
` ${ logPrefix} 4/6. received SignRequest control message. `
)
;
const
=
await
raceWithTimeoutCancellation
(
.
.
validate
(
,
.
)
,
)
;
if
(
!
)
{
const
:
any
=
new
Error
(
'Refused to connect to unsupported server'
)
;
.
=
'VSCODE_CONNECTION_ERROR'
;
throw
;
}
const
=
await
raceWithTimeoutCancellation
(
.
.
sign
(
.
)
,
)
;
const
:
=
{
:
'connectionType'
,
:
.
,
:
,
:
}
;
if
(
)
{
.
=
;
}
.
.
trace
(
` ${ logPrefix} 5/6. sending ConnectionTypeRequest control message. `
)
;
.
sendControl
(
.
fromString
(
JSON
.
stringify
(
)
)
)
;
return
{
,
}
;
}
catch
(
)
{
if
(
&&
.
===
'ETIMEDOUT'
)
{
.
.
error
(
` ${ logPrefix} the handshake timed out. Error: `
)
;
.
.
error
(
)
;
}
if
(
&&
.
===
'VSCODE_CONNECTION_ERROR'
)
{
.
.
error
(
` ${ logPrefix} received error control message when negotiating connection. Error: `
)
;
.
.
error
(
)
;
}
if
(
)
{
safeDisposeProtocolAndSocket
(
)
;
}
throw
;
}
}
interface
IManagementConnectionResult
{
:
;
}
async
function
connectToRemoteExtensionHostAgentAndReadOneMessage < T >
(
:
,
:
,
:
any
|
undefined
,
:
)
:
Promise
<
{
:
;
:
T
}
>
{
const
=
.
now
(
)
;
const
=
connectLogPrefix
(
,
)
;
const
{
,
}
=
await
connectToRemoteExtensionHostAgent
(
,
,
,
)
;
const
=
new
PromiseWithTimeout< { protocol: PersistentProtocol; firstMessage: T } >
(
)
;
.
registerDisposable
(
.
onControlMessage
(
=>
{
const
:
T
=
JSON
.
parse
(
.
toString
(
)
)
;
const
=
getErrorFromMessage
(
)
;
if
(
)
{
.
.
error
(
` ${ logPrefix} received error control message when negotiating connection. Error: `
)
;
.
.
error
(
)
;
if
(
)
{
safeDisposeProtocolAndSocket
(
)
;
}
.
reject
(
)
;
}
else
{
.
?.
endAcceptReconnection
(
)
;
.
.
trace
(
` ${ logPrefix} 6/6. handshake finished, connection is up and running after ${ logElapsed ( startTime) } ! `
)
;
.
resolve
(
{
,
:
}
)
;
}
}
)
)
;
return
.
;
}
async
function
doConnectRemoteAgentManagement
(
:
,
:
)
:
Promise
<
>
{
const
{
}
=
await
connectToRemoteExtensionHostAgentAndReadOneMessage
(
,
.
,
undefined
,
)
;
return
{
}
;
}
export
interface
IRemoteExtensionHostStartParams
{
:
string
;
?
:
string
;
break
?
:
boolean
;
?
:
number
|
null
;
?
:
{
[
:
string
]
:
string
|
null
}
;
}
interface
IExtensionHostConnectionResult
{
:
;
?
:
number
;
}
async
function
doConnectRemoteAgentExtensionHost
(
:
,
:
,
:
)
:
Promise
<
>
{
const
{
,
}
=
await
connectToRemoteExtensionHostAgentAndReadOneMessage < { debugPort? : number } >
(
,
.
,
,
)
;
const
=
&&
.
;
return
{
,
}
;
}
export
interface
ITunnelConnectionStartParams
{
:
string
;
:
number
;
}
async
function
doConnectRemoteAgentTunnel
(
:
,
:
,
:
)
:
Promise
<
>
{
const
=
.
now
(
)
;
const
=
connectLogPrefix
(
,
.
)
;
const
{
}
=
await
connectToRemoteExtensionHostAgentAndReadOneMessage
(
,
.
,
,
)
;
.
.
trace
(
` ${ logPrefix} 6/6. handshake finished, connection is up and running after ${ logElapsed ( startTime) } ! `
)
;
return
;
}
export
interface
IConnectionOptions< T extends RemoteConnection = RemoteConnection>
{
:
string
|
undefined
;
:
string
|
undefined
;
:
<
T
>
;
:
;
:
;
:
;
:
|
null
;
}
async
function
resolveConnectionOptions < T extends RemoteConnection>
(
:
<
T
>
,
:
string
,
:
|
null
)
:
Promise
<
<
T
>>
{
const
{
,
}
=
await
.
.
getAddress
(
)
;
return
{
:
.
,
:
.
,
,
:
,
:
,
:
,
:
.
,
:
.
,
:
.
}
;
}
export
interface
IAddress< T extends RemoteConnection = RemoteConnection>
{
:
T
;
:
string
|
undefined
;
}
export
interface
IAddressProvider< T extends RemoteConnection = RemoteConnection>
{
getAddress
(
)
:
Promise
<
<
T
>>
;
}
export
async
function
connectRemoteAgentManagement
(
:
,
:
string
,
:
string
)
:
Promise
<
>
{
return
createInitialConnection
(
,
async
(
)
=>
{
const
{
}
=
await
doConnectRemoteAgentManagement
(
,
.
)
;
return
new
ManagementPersistentConnection
(
,
,
,
.
,
)
;
}
)
;
}
export
async
function
connectRemoteAgentExtensionHost
(
:
,
:
)
:
Promise
<
>
{
return
createInitialConnection
(
,
async
(
)
=>
{
const
{
,
}
=
await
doConnectRemoteAgentExtensionHost
(
,
,
.
)
;
return
new
ExtensionHostPersistentConnection
(
,
,
.
,
,
)
;
}
)
;
}
async
function
createInitialConnection < T extends PersistentConnection, O extends RemoteConnection>
(
:
<
O
>
,
connectionFactory
:
(
:
<
O
>
)
=>
Promise
<
T
>
)
:
Promise
<
T
>
{
const
MAX_ATTEMPTS
=
5
;
for
(
let
=
1
;
;
++
)
{
try
{
const
=
generateUuid
(
)
;
const
=
await
resolveConnectionOptions
(
,
,
null
)
;
const
=
await
connectionFactory
(
)
;
return
;
}
catch
(
)
{
if
(
<
MAX_ATTEMPTS
)
{
.
.
error
(
` [remote-connection][attempt ${ attempt} ] An error occurred in initial connection! Will retry... Error: `
)
;
.
.
error
(
)
;
}
else
{
.
.
error
(
` [remote-connection][attempt ${ attempt} ] An error occurred in initial connection! It will be treated as a permanent error. Error: `
)
;
.
.
error
(
)
;
.
triggerPermanentFailure
(
0
,
0
,
.
isHandled
(
)
)
;
throw
;
}
}
}
}
export
async
function
connectRemoteAgentTunnel
(
:
,
:
string
,
:
number
)
:
Promise
<
>
{
const
=
await
resolveConnectionOptions
(
,
generateUuid
(
)
,
null
)
;
const
=
await
doConnectRemoteAgentTunnel
(
,
{
:
,
:
}
,
.
)
;
return
;
}
function
sleep
(
:
number
)
:
<
void
>
{
return
createCancelablePromise
(
=>
{
return
new
Promise
(
(
,
)
=>
{
const
=
setTimeout
(
,
*
1000
)
;
.
onCancellationRequested
(
(
)
=>
{
clearTimeout
(
)
;
resolve
(
)
;
}
)
;
}
)
;
}
)
;
}
export
const
enum
{
,
,
,
,
}
export
class
ConnectionLostEvent
{
public
readonly
=
.
;
constructor
(
public
readonly
:
string
,
public
readonly
:
number
)
{
}
}
export
class
ReconnectionWaitEvent
{
public
readonly
=
.
;
constructor
(
public
readonly
:
string
,
public
readonly
:
number
,
public
readonly
:
number
,
private
readonly
:
<
void
>
)
{
}
public
skipWait
(
)
:
void
{
this
.
.
cancel
(
)
;
}
}
export
class
ReconnectionRunningEvent
{
public
readonly
=
.
;
constructor
(
public
readonly
:
string
,
public
readonly
:
number
,
public
readonly
:
number
)
{
}
}
export
class
ConnectionGainEvent
{
public
readonly
=
.
;
constructor
(
public
readonly
:
string
,
public
readonly
:
number
,
public
readonly
:
number
)
{
}
}
export
class
ReconnectionPermanentFailureEvent
{
public
readonly
=
.
;
constructor
(
public
readonly
:
string
,
public
readonly
:
number
,
public
readonly
:
number
,
public
readonly
:
boolean
)
{
}
}
export
type
PersistentConnectionEvent
=
|
|
|
|
;
export
abstract
class
PersistentConnection
extends
Disposable
{
public
static
triggerPermanentFailure
(
:
number
,
:
number
,
:
boolean
)
:
void
{
this
.
=
true
;
this
.
=
;
this
.
=
;
this
.
=
;
this
.
.
forEach
(
=>
.
_gotoPermanentFailure
(
this
.
,
this
.
,
this
.
)
)
;
}
public
static
debugTriggerReconnection
(
)
{
this
.
.
forEach
(
=>
.
_beginReconnecting
(
)
)
;
}
public
static
debugPauseSocketWriting
(
)
{
this
.
.
forEach
(
=>
.
_pauseSocketWriting
(
)
)
;
}
private
static
:
boolean
=
false
;
private
static
:
number
=
0
;
private
static
:
number
=
0
;
private
static
:
boolean
=
false
;
private
static
:
[
]
=
[
]
;
private
readonly
=
this
.
_register
(
new
Emitter< PersistentConnectionEvent>
(
)
)
;
public
readonly
=
this
.
.
;
private
:
boolean
=
false
;
private
get
_isPermanentFailure
(
)
:
boolean
{
return
this
.
||
.
;
}
private
:
boolean
=
false
;
private
:
boolean
=
false
;
private
:
number
=
.
;
constructor
(
private
readonly
:
,
protected
readonly
:
,
public
readonly
:
string
,
public
readonly
:
,
private
readonly
:
boolean
)
{
super
(
)
;
this
.
.
fire
(
new
ConnectionGainEvent
(
this
.
,
0
,
0
)
)
;
this
.
_register
(
.
onSocketClose
(
(
)
=>
{
const
=
commonLogPrefix
(
this
.
,
this
.
,
true
)
;
if
(
!
)
{
this
.
.
.
info
(
` ${ logPrefix} received socket close event. `
)
;
}
else
if
(
.
===
.
)
{
this
.
.
.
info
(
` ${ logPrefix} received socket close event (hadError: ${ e. hadError} ). `
)
;
if
(
.
)
{
this
.
.
.
error
(
.
)
;
}
}
else
{
this
.
.
.
info
(
` ${ logPrefix} received socket close event (wasClean: ${ e. wasClean} , code: ${ e. code} , reason: ${ e. reason} ). `
)
;
if
(
.
)
{
this
.
.
.
error
(
.
)
;
}
}
this
.
_beginReconnecting
(
)
;
}
)
)
;
this
.
_register
(
.
onSocketTimeout
(
(
)
=>
{
const
=
commonLogPrefix
(
this
.
,
this
.
,
true
)
;
this
.
.
.
info
(
` ${ logPrefix} received socket timeout event (reason: ${ e. reason} , unacknowledgedMsgCount: ${ e. unacknowledgedMsgCount} , timeSinceOldestUnacknowledgedMsg: ${ e. timeSinceOldestUnacknowledgedMsg} , timeSinceLastReceivedSomeData: ${ e. timeSinceLastReceivedSomeData} ). `
)
;
this
.
_beginReconnecting
(
)
;
}
)
)
;
.
.
push
(
this
)
;
this
.
_register
(
toDisposable
(
(
)
=>
{
const
=
.
.
indexOf
(
this
)
;
if
(
>=
0
)
{
.
.
splice
(
,
1
)
;
}
}
)
)
;
if
(
this
.
)
{
this
.
_gotoPermanentFailure
(
.
,
.
,
.
)
;
}
}
public
updateGraceTime
(
:
number
)
:
void
{
const
=
sanitizeGraceTime
(
,
.
)
;
const
=
commonLogPrefix
(
this
.
,
this
.
,
false
)
;
this
.
.
.
trace
(
` ${ logPrefix} Applying reconnection grace time: ${ sanitizedGrace} ms ( ${ Math. floor ( sanitizedGrace / 1000 ) } s) `
)
;
this
.
=
;
}
public
dispose
(
)
:
void
{
super
.
dispose
(
)
;
this
.
=
true
;
}
private
async
_beginReconnecting
(
)
:
Promise
<
void
>
{
if
(
this
.
)
{
return
;
}
try
{
this
.
=
true
;
await
this
.
_runReconnectingLoop
(
)
;
}
finally
{
this
.
=
false
;
}
}
private
async
_runReconnectingLoop
(
)
:
Promise
<
void
>
{
if
(
this
.
||
this
.
)
{
return
;
}
const
=
commonLogPrefix
(
this
.
,
this
.
,
true
)
;
this
.
.
.
info
(
` ${ logPrefix} starting reconnecting loop. You can get more information with the trace log level. `
)
;
this
.
.
fire
(
new
ConnectionLostEvent
(
this
.
,
this
.
.
getMillisSinceLastIncomingData
(
)
)
)
;
const
TIMES
=
[
0
,
5
,
5
,
10
,
10
,
10
,
10
,
10
,
30
]
;
const
=
this
.
;
this
.
.
.
info
(
` ${ logPrefix} starting reconnection with grace time: ${ graceTime} ms ( ${ Math. floor ( graceTime / 1000 ) } s) `
)
;
if
(
<=
0
)
{
this
.
.
.
error
(
` ${ logPrefix} reconnection grace time is set to 0ms, will not attempt to reconnect. `
)
;
this
.
_onReconnectionPermanentFailure
(
this
.
.
getMillisSinceLastIncomingData
(
)
,
0
,
false
)
;
return
;
}
const
=
.
now
(
)
;
let
=
-
1
;
do
{
++
;
const
=
(
<
TIMES
.
?
TIMES
[
]
:
TIMES
[
TIMES
.
-
1
]
)
;
try
{
if
(
>
0
)
{
const
=
sleep
(
)
;
this
.
.
fire
(
new
ReconnectionWaitEvent
(
this
.
,
this
.
.
getMillisSinceLastIncomingData
(
)
,
,
)
)
;
this
.
.
.
info
(
` ${ logPrefix} waiting for ${ waitTime} seconds before reconnecting... `
)
;
try
{
await
;
}
catch
{
}
}
if
(
this
.
)
{
this
.
.
.
error
(
` ${ logPrefix} permanent failure occurred while running the reconnecting loop. `
)
;
break
;
}
this
.
.
fire
(
new
ReconnectionRunningEvent
(
this
.
,
this
.
.
getMillisSinceLastIncomingData
(
)
,
+
1
)
)
;
this
.
.
.
info
(
` ${ logPrefix} resolving connection... `
)
;
const
=
await
resolveConnectionOptions
(
this
.
,
this
.
,
this
.
)
;
this
.
.
.
info
(
` ${ logPrefix} connecting to ${ simpleOptions. connectTo} ... `
)
;
await
this
.
_reconnect
(
,
createTimeoutCancellation
(
RECONNECT_TIMEOUT
)
)
;
this
.
.
.
info
(
` ${ logPrefix} reconnected! `
)
;
this
.
.
fire
(
new
ConnectionGainEvent
(
this
.
,
this
.
.
getMillisSinceLastIncomingData
(
)
,
+
1
)
)
;
break
;
}
catch
(
)
{
if
(
.
===
'VSCODE_CONNECTION_ERROR'
)
{
this
.
.
.
error
(
` ${ logPrefix} A permanent error occurred in the reconnecting loop! Will give up now! Error: `
)
;
this
.
.
.
error
(
)
;
this
.
_onReconnectionPermanentFailure
(
this
.
.
getMillisSinceLastIncomingData
(
)
,
+
1
,
false
)
;
break
;
}
if
(
.
now
(
)
-
>=
)
{
const
=
.
round
(
/
1000
)
;
this
.
.
.
error
(
` ${ logPrefix} An error occurred while reconnecting, but it will be treated as a permanent error because the reconnection grace time ( ${ graceSeconds} s) has expired! Will give up now! Error: `
)
;
this
.
.
.
error
(
)
;
this
.
_onReconnectionPermanentFailure
(
this
.
.
getMillisSinceLastIncomingData
(
)
,
+
1
,
false
)
;
break
;
}
if
(
.
isTemporarilyNotAvailable
(
)
)
{
this
.
.
.
info
(
` ${ logPrefix} A temporarily not available error occurred while trying to reconnect, will try again... `
)
;
this
.
.
.
trace
(
)
;
continue
;
}
if
(
(
.
===
'ETIMEDOUT'
||
.
===
'ENETUNREACH'
||
.
===
'ECONNREFUSED'
||
.
===
'ECONNRESET'
)
&&
.
===
'connect'
)
{
this
.
.
.
info
(
` ${ logPrefix} A network error occurred while trying to reconnect, will try again... `
)
;
this
.
.
.
trace
(
)
;
continue
;
}
if
(
isCancellationError
(
)
)
{
this
.
.
.
info
(
` ${ logPrefix} A promise cancelation error occurred while trying to reconnect, will try again... `
)
;
this
.
.
.
trace
(
)
;
continue
;
}
if
(
instanceof
RemoteAuthorityResolverError
)
{
this
.
.
.
error
(
` ${ logPrefix} A RemoteAuthorityResolverError occurred while trying to reconnect. Will give up now! Error: `
)
;
this
.
.
.
error
(
)
;
this
.
_onReconnectionPermanentFailure
(
this
.
.
getMillisSinceLastIncomingData
(
)
,
+
1
,
.
isHandled
(
)
)
;
break
;
}
this
.
.
.
error
(
` ${ logPrefix} An unknown error occurred while trying to reconnect, since this is an unknown case, it will be treated as a permanent error! Will give up now! Error: `
)
;
this
.
.
.
error
(
)
;
this
.
_onReconnectionPermanentFailure
(
this
.
.
getMillisSinceLastIncomingData
(
)
,
+
1
,
false
)
;
break
;
}
}
while
(
!
this
.
&&
!
this
.
)
;
}
private
_onReconnectionPermanentFailure
(
:
number
,
:
number
,
:
boolean
)
:
void
{
if
(
this
.
)
{
.
triggerPermanentFailure
(
,
,
)
;
}
else
{
this
.
_gotoPermanentFailure
(
,
,
)
;
}
}
private
_gotoPermanentFailure
(
:
number
,
:
number
,
:
boolean
)
:
void
{
this
.
.
fire
(
new
ReconnectionPermanentFailureEvent
(
this
.
,
,
,
)
)
;
safeDisposeProtocolAndSocket
(
this
.
)
;
}
private
_pauseSocketWriting
(
)
:
void
{
this
.
.
pauseSocketWriting
(
)
;
}
protected
abstract
_reconnect
(
:
,
:
)
:
Promise
<
void
>
;
}
export
class
ManagementPersistentConnection
extends
PersistentConnection
{
public
readonly
:
<
>
;
constructor
(
:
,
:
string
,
:
string
,
:
string
,
:
)
{
super
(
.
,
,
,
,
true
)
;
this
.
=
this
.
_register
(
new
Client< RemoteAgentConnectionContext>
(
,
{
:
,
:
}
,
.
)
)
;
}
protected
async
_reconnect
(
:
,
:
)
:
Promise
<
void
>
{
await
doConnectRemoteAgentManagement
(
,
)
;
}
}
export
class
ExtensionHostPersistentConnection
extends
PersistentConnection
{
private
readonly
:
;
public
readonly
:
number
|
undefined
;
constructor
(
:
,
:
,
:
string
,
:
,
:
number
|
undefined
)
{
super
(
.
,
,
,
,
false
)
;
this
.
=
;
this
.
=
;
}
protected
async
_reconnect
(
:
,
:
)
:
Promise
<
void
>
{
await
doConnectRemoteAgentExtensionHost
(
,
this
.
,
)
;
}
}
function
safeDisposeProtocolAndSocket
(
:
)
:
void
{
try
{
.
acceptDisconnect
(
)
;
const
=
.
getSocket
(
)
;
.
dispose
(
)
;
.
dispose
(
)
;
}
catch
(
)
{
onUnexpectedError
(
)
;
}
}
function
getErrorFromMessage
(
:
any
)
:
|
null
{
if
(
&&
.
===
'error'
)
{
const
=
new
Error
(
` Connection error: ${ msg. reason} `
)
;
(
<
any
>
)
.
=
'VSCODE_CONNECTION_ERROR'
;
return
;
}
return
null
;
}
function
sanitizeGraceTime
(
:
number
,
:
number
)
:
number
{
if
(
typeof
!==
'number'
||
!
isFinite
(
)
||
<
0
)
{
return
;
}
if
(
>
.
MAX_SAFE_INTEGER
)
{
return
.
MAX_SAFE_INTEGER
;
}
return
.
floor
(
)
;
}
function
stringRightPad
(
:
string
,
:
number
)
:
string
{
while
(
.
<
)
{
+=
' '
;
}
return
;
}
function
_commonLogPrefix
(
:
,
:
string
)
:
string
{
return
` [remote-connection][ ${ stringRightPad ( connectionTypeToString ( connectionType) , 13 ) } ][ ${ reconnectionToken. substr ( 0 , 5 ) } …] `
;
}
function
commonLogPrefix
(
:
,
:
string
,
:
boolean
)
:
string
{
return
` ${ _commonLogPrefix ( connectionType, reconnectionToken) } [ ${ isReconnect ? 'reconnect' : 'initial' } ] `
;
}
function
connectLogPrefix
(
:
,
:
)
:
string
{
return
` ${ commonLogPrefix ( connectionType, options. reconnectionToken, ! ! options. reconnectionProtocol) } [ ${ options. connectTo} ] `
;
}
function
logElapsed
(
:
number
)
:
string
{
return
` ${ Date. now ( ) - startTime} ms `
;
}