1
1
import * as path from "path"
2
- import events from "events"
3
2
import child_process from "child_process"
4
3
import net from "node:net"
5
4
import http from "http"
@@ -43,26 +42,25 @@ export enum RunEventType {
43
42
CallFinish = "callFinish" ,
44
43
}
45
44
46
- export class Run extends events . EventEmitter {
45
+ export class Run {
47
46
public readonly id : string
48
47
public readonly opts : RunOpts
49
- public state : RunState
50
- public calls ? : Call [ ]
51
- public err ?: string
48
+ public state : RunState = RunState . Creating
49
+ public calls : Call [ ] = [ ]
50
+ public err = ''
52
51
public readonly path : string
53
52
54
53
private promise ?: Promise < string >
55
54
private process ?: child_process . ChildProcess
56
55
private req ?: http . ClientRequest
57
56
private stdout ?: string
58
57
private stderr ?: string
58
+ private callbacks : Record < string , ( ( f : Frame ) => void ) [ ] > = { }
59
59
60
60
constructor ( path : string , opts : RunOpts ) {
61
- super ( )
62
61
this . id = randomId ( "run-" )
63
62
this . opts = opts
64
63
this . path = path
65
- this . state = RunState . Creating
66
64
}
67
65
68
66
exec ( command : string , args : string [ ] , stdin : string = "" , env : NodeJS . Dict < string > = process . env ) : void {
@@ -159,7 +157,7 @@ export class Run extends events.EventEmitter {
159
157
this . err = "Run has been aborted"
160
158
} else if ( code !== 0 ) {
161
159
this . state = RunState . Error
162
- this . err = this . stderr
160
+ this . err = this . stderr || ''
163
161
} else {
164
162
this . state = RunState . Finished
165
163
}
@@ -228,14 +226,14 @@ export class Run extends events.EventEmitter {
228
226
229
227
res . on ( "error" , ( error : Error ) => {
230
228
this . state = RunState . Error
231
- this . err = error . message
229
+ this . err = error . message || ''
232
230
reject ( this . err )
233
231
} )
234
232
} )
235
233
236
234
this . req . on ( "error" , ( error : Error ) => {
237
235
this . state = RunState . Error
238
- this . err = error . message
236
+ this . err = error . message || ''
239
237
reject ( this . err )
240
238
} )
241
239
@@ -260,7 +258,7 @@ export class Run extends events.EventEmitter {
260
258
method : method ,
261
259
headers : {
262
260
"Content-Type" : "application/json" ,
263
- "Content-Length" : Buffer . byteLength ( postData ) ,
261
+ "Content-Length" : postData . length
264
262
} ,
265
263
}
266
264
}
@@ -289,7 +287,7 @@ export class Run extends events.EventEmitter {
289
287
} else if ( f . type === RunEventType . RunFinish ) {
290
288
if ( f . err ) {
291
289
this . state = RunState . Error
292
- this . err = f . err
290
+ this . err = f . err || ''
293
291
} else {
294
292
this . state = RunState . Finished
295
293
this . stdout = f . output || ""
@@ -345,6 +343,12 @@ export class Run extends events.EventEmitter {
345
343
return ""
346
344
}
347
345
346
+ private emit ( event : RunEventType , data : any ) {
347
+ for ( const cb of this . callbacks [ event ] || [ ] ) {
348
+ cb ( data )
349
+ }
350
+ }
351
+
348
352
public on ( event : RunEventType . RunStart , listener : ( data : RunStartFrame ) => void ) : this;
349
353
public on ( event : RunEventType . RunFinish , listener : ( data : RunFinishFrame ) => void ) : this;
350
354
public on ( event : RunEventType . CallStart , listener : ( data : CallStartFrame ) => void ) : this;
@@ -354,7 +358,12 @@ export class Run extends events.EventEmitter {
354
358
public on ( event : RunEventType . CallFinish , listener : ( data : CallFinishFrame ) => void ) : this;
355
359
public on ( event : RunEventType . Event , listener : ( data : Frame ) => void ) : this;
356
360
public on ( event : RunEventType , listener : ( data : any ) => void ) : this {
357
- super . on ( event , listener )
361
+ if ( ! this . callbacks [ event ] ) {
362
+ this . callbacks [ event ] = [ ] ;
363
+ }
364
+
365
+ this . callbacks [ event ] . push ( listener )
366
+
358
367
return this
359
368
}
360
369
0 commit comments