Open History to see the last commit
1import { Effect, Layer } from "effect";
2import { PtyAdapter, PtyAdapterShape, PtyExitEvent, PtyProcess } from "../Services/PTY";
3
4class BunPtyProcess implements PtyProcess {
5 private readonly dataListeners = new Set<(data: string) => void>();
6 private readonly exitListeners = new Set<(event: PtyExitEvent) => void>();
7 private readonly decoder = new TextDecoder();
8 private didExit = false;
9
10 constructor(private readonly process: Bun.Subprocess) {
11 void this.process.exited
12 .then((exitCode) => {
13 this.emitExit({
14 exitCode: Number.isInteger(exitCode) ? exitCode : 0,
15 signal: typeof this.process.signalCode === "number" ? this.process.signalCode : null,
16 });
17 })
18 .catch(() => {
19 this.emitExit({ exitCode: 1, signal: null });
20 });
21 }
22
23 get pid(): number {
24 return this.process.pid;
25 }
26
27 write(data: string): void {
28 if (!this.process.terminal) {
29 throw new Error("Bun PTY terminal handle is unavailable");
30 }
31 this.process.terminal.write(data);
32 }
33
34 resize(cols: number, rows: number): void {
35 if (!this.process.terminal?.resize) {
36 throw new Error("Bun PTY resize is unavailable");
37 }
38 this.process.terminal.resize(cols, rows);
39 }
40
41 kill(signal?: string): void {
42 if (!signal) {
43 this.process.kill();
44 return;
45 }
46 this.process.kill(signal as NodeJS.Signals);
47 }
48
49 onData(callback: (data: string) => void): () => void {
50 this.dataListeners.add(callback);
51 return () => {
52 this.dataListeners.delete(callback);
53 };
54 }
55
56 onExit(callback: (event: PtyExitEvent) => void): () => void {
57 this.exitListeners.add(callback);
58 return () => {
59 this.exitListeners.delete(callback);
60 };
61 }
62
63 emitData(data: Uint8Array): void {
64 if (this.didExit) return;
65 const text = this.decoder.decode(data, { stream: true });
66 if (text.length === 0) return;
67 for (const listener of this.dataListeners) {
68 listener(text);
69 }
70 }
71
72 private emitExit(event: PtyExitEvent): void {
73 if (this.didExit) return;
74 this.didExit = true;
75
76 const remainder = this.decoder.decode();
77 if (remainder.length > 0) {
78 for (const listener of this.dataListeners) {
79 listener(remainder);
80 }
81 }
82
83 for (const listener of this.exitListeners) {
84 listener(event);
85 }
86 }
87}
88
89export const BunPtyAdapterLive = Layer.effect(
90 PtyAdapter,
91 Effect.gen(function* () {
92 if (process.platform === "win32") {
93 return yield* Effect.die("Bun PTY terminal support is unavailable on Windows.");
94 }
95 return {
96 spawn: (input) =>
97 Effect.sync(() => {
98 let processHandle: BunPtyProcess | null = null;
99 const command = [input.shell, ...(input.args ?? [])];
100 const subprocess = Bun.spawn(command, {
101 cwd: input.cwd,
102 env: input.env,
103 terminal: {
104 cols: input.cols,
105 rows: input.rows,
106 data: (_terminal, data) => {
107 processHandle?.emitData(data);
108 },
109 },
110 });
111 processHandle = new BunPtyProcess(subprocess);
112 return processHandle;
113 }),
114 } satisfies PtyAdapterShape;
115 }),
116);