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