Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

BunPTY.ts

118 lines3.2 KB

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 }
88​
89export const layer = Layer.effect(
90 PtyAdapter,
91 Effect.gen(function* () {
92 if (process.platform === "win32") {
93 return yield* Effect.die(
94 "Bun PTY terminal support is unavailable on Windows. Please use Node.js (e.g. by running `npx t3`) instead.",
95 );
96 }
97 return {
98 spawn: (input) =>
99 Effect.sync(() => {
100 let processHandle: BunPtyProcess | null = null;
101 const command = [input.shell, ...(input.args ?? [])];
102 const subprocess = Bun.spawn(command, {
103 cwd: input.cwd,
104 env: input.env,
105 terminal: {
106 cols: input.cols,
107 rows: input.rows,
108 data: (_terminal, data) => {
109 processHandle?.emitData(data);
110 },
111 },
112 });
113 processHandle = new BunPtyProcess(subprocess);
114 return processHandle;
115 }),
116 } satisfies PtyAdapterShape;
117 }),
118);