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

NodePTY.ts

117 lines3.4 KB

1import { createRequire } from "node:module";
2​
3import { Effect, FileSystem, Layer, Path } from "effect";
4import { PtyAdapter, PtyAdapterShape, PtyExitEvent, PtyProcess } from "../Services/PTY";
5​
6let didEnsureSpawnHelperExecutable = false;
7​
8const resolveNodePtySpawnHelperPath = Effect.gen(function* () {
9 const requireForNodePty = createRequire(import.meta.url);
10 const path = yield* Path.Path;
11 const fs = yield* FileSystem.FileSystem;
12​
13 const packageJsonPath = requireForNodePty.resolve("node-pty/package.json");
14 const packageDir = path.dirname(packageJsonPath);
15 const candidates = [
16 path.join(packageDir, "build", "Release", "spawn-helper"),
17 path.join(packageDir, "build", "Debug", "spawn-helper"),
18 path.join(packageDir, "prebuilds", `${process.platform}-${process.arch}`, "spawn-helper"),
19 ];
20​
21 for (const candidate of candidates) {
22 if (yield* fs.exists(candidate)) {
23 return candidate;
24 }
25 }
26 return null;
27}).pipe(Effect.orElseSucceed(() => null));
28​
29export const ensureNodePtySpawnHelperExecutable = Effect.fn(function* (explicitPath?: string) {
30 const fs = yield* FileSystem.FileSystem;
31 if (process.platform === "win32") return;
32 if (!explicitPath && didEnsureSpawnHelperExecutable) return;
33​
34 const helperPath = explicitPath ?? (yield* resolveNodePtySpawnHelperPath);
35 if (!helperPath) return;
36 if (!explicitPath) {
37 didEnsureSpawnHelperExecutable = true;
38 }
39​
40 if (!(yield* fs.exists(helperPath))) {
41 return;
42 }
43​
44 // Best-effort: avoid FileSystem.stat in packaged mode where some fs metadata can be missing.
45 yield* fs.chmod(helperPath, 0o755).pipe(Effect.orElseSucceed(() => undefined));
46});
47​
48class NodePtyProcess implements PtyProcess {
49 constructor(private readonly process: import("node-pty").IPty) {}
50​
51 get pid(): number {
52 return this.process.pid;
53 }
54​
55 write(data: string): void {
56 this.process.write(data);
57 }
58​
59 resize(cols: number, rows: number): void {
60 this.process.resize(cols, rows);
61 }
62​
63 kill(signal?: string): void {
64 this.process.kill(signal);
65 }
66​
67 onData(callback: (data: string) => void): () => void {
68 const disposable = this.process.onData(callback);
69 return () => {
70 disposable.dispose();
71 };
72 }
73​
74 onExit(callback: (event: PtyExitEvent) => void): () => void {
75 const disposable = this.process.onExit((event) => {
76 callback({
77 exitCode: event.exitCode,
78 signal: event.signal ?? null,
79 });
80 });
81 return () => {
82 disposable.dispose();
83 };
84 }
86​
87export const layer = Layer.effect(
88 PtyAdapter,
89 Effect.gen(function* () {
90 const fs = yield* FileSystem.FileSystem;
91 const path = yield* Path.Path;
92​
93 const nodePty = yield* Effect.promise(() => import("node-pty"));
94​
95 const ensureNodePtySpawnHelperExecutableCached = yield* Effect.cached(
96 ensureNodePtySpawnHelperExecutable().pipe(
97 Effect.provideService(FileSystem.FileSystem, fs),
98 Effect.provideService(Path.Path, path),
99 Effect.orElseSucceed(() => undefined),
100 ),
101 );
102​
103 return {
104 spawn: Effect.fn(function* (input) {
105 yield* ensureNodePtySpawnHelperExecutableCached;
106 const ptyProcess = nodePty.spawn(input.shell, input.args ?? [], {
107 cwd: input.cwd,
108 cols: input.cols,
109 rows: input.rows,
110 env: input.env,
111 name: globalThis.process.platform === "win32" ? "xterm-color" : "xterm-256color",
112 });
113 return new NodePtyProcess(ptyProcess);
114 }),
115 } satisfies PtyAdapterShape;
116 }),
117);