Skip to content

Commit 42541fd

Browse files
committed
EventEmitter & Buffer don't exist in a browser
1 parent 617a12f commit 42541fd

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/gptscript.ts

+22-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as path from "path"
2-
import events from "events"
32
import child_process from "child_process"
43
import net from "node:net"
54
import http from "http"
@@ -43,26 +42,25 @@ export enum RunEventType {
4342
CallFinish = "callFinish",
4443
}
4544

46-
export class Run extends events.EventEmitter {
45+
export class Run {
4746
public readonly id: string
4847
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 = ''
5251
public readonly path: string
5352

5453
private promise?: Promise<string>
5554
private process?: child_process.ChildProcess
5655
private req?: http.ClientRequest
5756
private stdout?: string
5857
private stderr?: string
58+
private callbacks: Record<string, ((f: Frame) => void)[]> = {}
5959

6060
constructor(path: string, opts: RunOpts) {
61-
super()
6261
this.id = randomId("run-")
6362
this.opts = opts
6463
this.path = path
65-
this.state = RunState.Creating
6664
}
6765

6866
exec(command: string, args: string[], stdin: string = "", env: NodeJS.Dict<string> = process.env): void {
@@ -159,7 +157,7 @@ export class Run extends events.EventEmitter {
159157
this.err = "Run has been aborted"
160158
} else if (code !== 0) {
161159
this.state = RunState.Error
162-
this.err = this.stderr
160+
this.err = this.stderr || ''
163161
} else {
164162
this.state = RunState.Finished
165163
}
@@ -228,14 +226,14 @@ export class Run extends events.EventEmitter {
228226

229227
res.on("error", (error: Error) => {
230228
this.state = RunState.Error
231-
this.err = error.message
229+
this.err = error.message || ''
232230
reject(this.err)
233231
})
234232
})
235233

236234
this.req.on("error", (error: Error) => {
237235
this.state = RunState.Error
238-
this.err = error.message
236+
this.err = error.message || ''
239237
reject(this.err)
240238
})
241239

@@ -260,7 +258,7 @@ export class Run extends events.EventEmitter {
260258
method: method,
261259
headers: {
262260
"Content-Type": "application/json",
263-
"Content-Length": Buffer.byteLength(postData),
261+
"Content-Length": postData.length
264262
},
265263
}
266264
}
@@ -289,7 +287,7 @@ export class Run extends events.EventEmitter {
289287
} else if (f.type === RunEventType.RunFinish) {
290288
if (f.err) {
291289
this.state = RunState.Error
292-
this.err = f.err
290+
this.err = f.err || ''
293291
} else {
294292
this.state = RunState.Finished
295293
this.stdout = f.output || ""
@@ -345,6 +343,12 @@ export class Run extends events.EventEmitter {
345343
return ""
346344
}
347345

346+
private emit(event: RunEventType, data: any){
347+
for ( const cb of this.callbacks[event] || [] ){
348+
cb(data)
349+
}
350+
}
351+
348352
public on(event: RunEventType.RunStart, listener: (data: RunStartFrame) => void): this;
349353
public on(event: RunEventType.RunFinish, listener: (data: RunFinishFrame) => void): this;
350354
public on(event: RunEventType.CallStart, listener: (data: CallStartFrame) => void): this;
@@ -354,7 +358,12 @@ export class Run extends events.EventEmitter {
354358
public on(event: RunEventType.CallFinish, listener: (data: CallFinishFrame) => void): this;
355359
public on(event: RunEventType.Event, listener: (data: Frame) => void): this;
356360
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+
358367
return this
359368
}
360369

0 commit comments

Comments
 (0)