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

ptyAdapter.ts

130 lines3.3 KB

1import fs from "node:fs";
2import { createRequire } from "node:module";
3import path from "node:path";
4​
5import * as nodePty from "node-pty";
6​
7export interface PtyExitEvent {
8 exitCode: number;
9 signal: number | null;
11​
12export interface PtyProcess {
13 readonly pid: number;
14 write(data: string): void;
15 resize(cols: number, rows: number): void;
16 kill(signal?: string): void;
17 onData(callback: (data: string) => void): () => void;
18 onExit(callback: (event: PtyExitEvent) => void): () => void;
20​
21export interface PtySpawnInput {
22 shell: string;
23 cwd: string;
24 cols: number;
25 rows: number;
26 env: NodeJS.ProcessEnv;
28​
29export interface PtyAdapter {
30 spawn(input: PtySpawnInput): PtyProcess;
32​
33const requireForNodePty = createRequire(import.meta.url);
34let didEnsureSpawnHelperExecutable = false;
35​
36function resolveNodePtySpawnHelperPath(): string | null {
37 try {
38 const packageJsonPath = requireForNodePty.resolve("node-pty/package.json");
39 const packageDir = path.dirname(packageJsonPath);
40 const candidates = [
41 path.join(packageDir, "build", "Release", "spawn-helper"),
42 path.join(packageDir, "build", "Debug", "spawn-helper"),
43 path.join(packageDir, "prebuilds", `${process.platform}-${process.arch}`, "spawn-helper"),
44 ];
45​
46 for (const candidate of candidates) {
47 if (fs.existsSync(candidate)) {
48 return candidate;
49 }
50 }
51​
52 return null;
53 } catch {
54 return null;
55 }
57​
58export function ensureNodePtySpawnHelperExecutable(explicitPath?: string): void {
59 if (process.platform === "win32") return;
60 if (!explicitPath && didEnsureSpawnHelperExecutable) return;
61​
62 const helperPath = explicitPath ?? resolveNodePtySpawnHelperPath();
63 if (!helperPath) return;
64 if (!explicitPath) {
65 didEnsureSpawnHelperExecutable = true;
66 }
67​
68 try {
69 const stat = fs.statSync(helperPath);
70 const mode = stat.mode & 0o777;
71 if ((mode & 0o111) === 0) {
72 fs.chmodSync(helperPath, mode | 0o111);
73 }
74 } catch {
75 // Best effort only. If chmod fails, node-pty spawn will surface the real error.
76 }
78​
79class NodePtyProcess implements PtyProcess {
80 constructor(private readonly process: nodePty.IPty) {}
81​
82 get pid(): number {
83 return this.process.pid;
84 }
85​
86 write(data: string): void {
87 this.process.write(data);
88 }
89​
90 resize(cols: number, rows: number): void {
91 this.process.resize(cols, rows);
92 }
93​
94 kill(signal?: string): void {
95 this.process.kill(signal);
96 }
97​
98 onData(callback: (data: string) => void): () => void {
99 const disposable = this.process.onData(callback);
100 return () => {
101 disposable.dispose();
102 };
103 }
104​
105 onExit(callback: (event: PtyExitEvent) => void): () => void {
106 const disposable = this.process.onExit((event) => {
107 callback({
108 exitCode: event.exitCode,
109 signal: event.signal ?? null,
110 });
111 });
112 return () => {
113 disposable.dispose();
114 };
115 }
117​
118export class NodePtyAdapter implements PtyAdapter {
119 spawn(input: PtySpawnInput): PtyProcess {
120 ensureNodePtySpawnHelperExecutable();
121 const ptyProcess = nodePty.spawn(input.shell, [], {
122 cwd: input.cwd,
123 cols: input.cols,
124 rows: input.rows,
125 env: input.env,
126 name: globalThis.process.platform === "win32" ? "xterm-color" : "xterm-256color",
127 });
128 return new NodePtyProcess(ptyProcess);
129 }