1
1
import { ReadWriteConnection , InitData , OperatingSystem , ISharedProcessData } from "../common/connection" ;
2
- import { NewEvalMessage , ServerMessage , EvalDoneMessage , EvalFailedMessage , TypedValue , ClientMessage , NewSessionMessage , TTYDimensions , SessionOutputMessage , CloseSessionInputMessage , WorkingInitMessage , NewConnectionMessage , NewServerMessage } from "../proto" ;
2
+ import { NewEvalMessage , ServerMessage , EvalDoneMessage , EvalFailedMessage , TypedValue , ClientMessage , NewSessionMessage , TTYDimensions , SessionOutputMessage , CloseSessionInputMessage , WorkingInitMessage } from "../proto" ;
3
3
import { Emitter , Event } from "@coder/events" ;
4
4
import { logger , field } from "@coder/logger" ;
5
- import { ChildProcess , SpawnOptions , ServerProcess , ServerSocket , Socket , ServerListener , Server } from "./command" ;
5
+ import { ChildProcess , SpawnOptions , ForkOptions , ServerProcess , ServerSocket , Socket , ServerListener , Server } from "./command" ;
6
6
7
7
/**
8
8
* Client accepts an arbitrary connection intended to communicate with the Server.
9
9
*/
10
10
export class Client {
11
+
12
+ public Socket : typeof ServerSocket ;
13
+
11
14
private evalId : number = 0 ;
12
15
private evalDoneEmitter : Emitter < EvalDoneMessage > = new Emitter ( ) ;
13
16
private evalFailedEmitter : Emitter < EvalFailedMessage > = new Emitter ( ) ;
@@ -41,6 +44,15 @@ export class Client {
41
44
}
42
45
} ) ;
43
46
47
+ const that = this ;
48
+ this . Socket = class extends ServerSocket {
49
+
50
+ public constructor ( ) {
51
+ super ( that . connection , that . connectionId ++ , that . registerConnection ) ;
52
+ }
53
+
54
+ } ;
55
+
44
56
this . initDataPromise = new Promise ( ( resolve ) : void => {
45
57
this . initDataEmitter . event ( resolve ) ;
46
58
} ) ;
@@ -77,7 +89,7 @@ export class Client {
77
89
const newEval = new NewEvalMessage ( ) ;
78
90
const id = this . evalId ++ ;
79
91
newEval . setId ( id ) ;
80
- newEval . setArgsList ( [ a1 , a2 , a3 , a4 , a5 , a6 ] . filter ( a => a ) . map ( a => JSON . stringify ( a ) ) ) ;
92
+ newEval . setArgsList ( [ a1 , a2 , a3 , a4 , a5 , a6 ] . filter ( a => typeof a !== "undefined" ) . map ( a => JSON . stringify ( a ) ) ) ;
81
93
newEval . setFunction ( func . toString ( ) ) ;
82
94
83
95
const clientMsg = new ClientMessage ( ) ;
@@ -158,7 +170,7 @@ export class Client {
158
170
* @param args Args to add for the module
159
171
* @param options Options to execute
160
172
*/
161
- public fork ( modulePath : string , args : string [ ] = [ ] , options ?: SpawnOptions ) : ChildProcess {
173
+ public fork ( modulePath : string , args : string [ ] = [ ] , options ?: ForkOptions ) : ChildProcess {
162
174
return this . doSpawn ( modulePath , args , options , true ) ;
163
175
}
164
176
@@ -167,27 +179,17 @@ export class Client {
167
179
* Forks a module from bootstrap-fork
168
180
* @param modulePath Path of the module
169
181
*/
170
- public bootstrapFork ( modulePath : string ) : ChildProcess {
171
- return this . doSpawn ( modulePath , [ ] , undefined , true , true ) ;
182
+ public bootstrapFork ( modulePath : string , args : string [ ] = [ ] , options ?: ForkOptions ) : ChildProcess {
183
+ return this . doSpawn ( modulePath , args , options , true , true ) ;
172
184
}
173
185
174
- public createConnection ( path : string , callback ?: ( ) => void ) : Socket ;
175
- public createConnection ( port : number , callback ?: ( ) => void ) : Socket ;
176
- public createConnection ( target : string | number , callback ?: ( ) => void ) : Socket {
186
+ public createConnection ( path : string , callback ?: Function ) : Socket ;
187
+ public createConnection ( port : number , callback ?: Function ) : Socket ;
188
+ public createConnection ( target : string | number , callback ?: Function ) : Socket ;
189
+ public createConnection ( target : string | number , callback ?: Function ) : Socket {
177
190
const id = this . connectionId ++ ;
178
- const newCon = new NewConnectionMessage ( ) ;
179
- newCon . setId ( id ) ;
180
- if ( typeof target === "string" ) {
181
- newCon . setPath ( target ) ;
182
- } else {
183
- newCon . setPort ( target ) ;
184
- }
185
- const clientMsg = new ClientMessage ( ) ;
186
- clientMsg . setNewConnection ( newCon ) ;
187
- this . connection . send ( clientMsg . serializeBinary ( ) ) ;
188
-
189
- const socket = new ServerSocket ( this . connection , id , callback ) ;
190
- this . connections . set ( id , socket ) ;
191
+ const socket = new ServerSocket ( this . connection , id , this . registerConnection ) ;
192
+ socket . connect ( target , callback ) ;
191
193
192
194
return socket ;
193
195
}
@@ -214,7 +216,9 @@ export class Client {
214
216
}
215
217
if ( options . env ) {
216
218
Object . keys ( options . env ) . forEach ( ( envKey ) => {
217
- newSess . getEnvMap ( ) . set ( envKey , options . env ! [ envKey ] ) ;
219
+ if ( options . env ! [ envKey ] ) {
220
+ newSess . getEnvMap ( ) . set ( envKey , options . env ! [ envKey ] . toString ( ) ) ;
221
+ }
218
222
} ) ;
219
223
}
220
224
if ( options . tty ) {
@@ -356,9 +360,9 @@ export class Client {
356
360
return ;
357
361
}
358
362
const conId = message . getServerConnectionEstablished ( ) ! . getConnectionId ( ) ;
359
- const serverSocket = new ServerSocket ( this . connection , conId ) ;
363
+ const serverSocket = new ServerSocket ( this . connection , conId , this . registerConnection ) ;
364
+ this . registerConnection ( conId , serverSocket ) ;
360
365
serverSocket . emit ( "connect" ) ;
361
- this . connections . set ( conId , serverSocket ) ;
362
366
s . emit ( "connection" , serverSocket ) ;
363
367
} else if ( message . getServerFailure ( ) ) {
364
368
const s = this . servers . get ( message . getServerFailure ( ) ! . getId ( ) ) ;
@@ -376,4 +380,12 @@ export class Client {
376
380
this . servers . delete ( message . getServerClose ( ) ! . getId ( ) ) ;
377
381
}
378
382
}
383
+
384
+ private registerConnection = ( id : number , socket : ServerSocket ) : void => {
385
+ if ( this . connections . has ( id ) ) {
386
+ throw new Error ( `${ id } is already registered` ) ;
387
+ }
388
+ this . connections . set ( id , socket ) ;
389
+ }
390
+
379
391
}
0 commit comments