Repository navigation
Expand file tree
/
Copy pathNodePtyAdapter.ts
More file actions
285 lines (251 loc) · 9.12 KB
/
Copy pathNodePtyAdapter.ts
File metadata and controls
285 lines (251 loc) · 9.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import * as NodeModule from "node:module";
import * as NodeNet from "node:net";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as PtyAdapter from "@t3tools/shared/PtyAdapter";
export class NodePtyModuleLoadError extends Schema.TaggedError<NodePtyModuleLoadError>()(
"NodePtyModuleLoadError",
{
platform: Schema.String,
architecture: Schema.String,
cause: Schema.Defect(),
},
) {
override get message(): string {
return `Failed to load node-pty for ${this.platform}-${this.architecture}.`;
}
}
type NodePtyModuleLoader = () => Promise<typeof import("node-pty")>;
// node-pty's native addon stays external; single-executables need require to resolve disk modules and spawn helpers.
const requireForNodePty = NodeModule.createRequire(import.meta.url);
const loadNodePty: NodePtyModuleLoader = () =>
Promise.resolve().then(() => requireForNodePty("node-pty") as typeof import("node-pty"));
export const NodePtyModuleLoaderRef = Context.Reference<NodePtyModuleLoader>(
"server/terminal/NodePtyModuleLoader",
{ defaultValue: () => loadNodePty },
);
let didEnsureSpawnHelperExecutable = false;
const resolveNodePtySpawnHelperPath = Effect.gen(function* () {
const path = yield* Path.Path;
const fs = yield* FileSystem.FileSystem;
const platform = yield* HostProcess.Platform;
const architecture = yield* HostProcess.Architecture;
const packageJsonPath = requireForNodePty.resolve("node-pty/package.json");
const packageDir = path.dirname(packageJsonPath);
const candidates = [
path.join(packageDir, "build", "Release", "spawn-helper"),
path.join(packageDir, "build", "Debug", "spawn-helper"),
path.join(packageDir, "prebuilds", `${platform}-${architecture}`, "spawn-helper"),
];
for (const candidate of candidates) {
if (yield* fs.exists(candidate)) {
return candidate;
}
}
return null;
}).pipe(Effect.orElseSucceed(() => null));
const ensureNodePtySpawnHelperExecutable = Effect.fn(function* () {
const fs = yield* FileSystem.FileSystem;
const platform = yield* HostProcess.Platform;
if (platform === "win32") return;
if (didEnsureSpawnHelperExecutable) return;
const helperPath = yield* resolveNodePtySpawnHelperPath;
if (!helperPath) return;
didEnsureSpawnHelperExecutable = true;
if (!(yield* fs.exists(helperPath))) {
return;
}
yield* fs.chmod(helperPath, 0o755).pipe(Effect.orElseSucceed(() => undefined));
});
// node-pty has no public readiness event; ready_datapipe sets pid before our listener.
// Deferred pipe creation: https://github.com/microsoft/node-pty/pull/885
const waitForWindowsPid = (
process: import("node-pty").IPty,
trackedProcess: NodePtyProcess,
shell: string,
) =>
Effect.callback<void, PtyAdapter.PtySpawnError>((resume) => {
const hasPid = () => Number.isInteger(process.pid) && process.pid > 0;
const failure = (cause: unknown) =>
Effect.fail(new PtyAdapter.PtySpawnError({ adapter: "node-pty", shell, cause }));
if (hasPid()) {
resume(Effect.void);
return;
}
if (!("_socket" in process) || !(process._socket instanceof NodeNet.Socket)) {
resume(failure(new Error("Windows PTY readiness socket is unavailable.")));
return;
}
const socket = process._socket;
const onReady = () => {
cleanup();
resume(
hasPid()
? Effect.void
: failure(new Error("Windows PTY became ready without a valid PID.")),
);
};
const onError = (cause: Error) => {
cleanup();
resume(failure(cause));
};
const onClose = () => onError(new Error("Windows PTY closed before its PID was available."));
let stopExit = () => {};
const cleanup = () => {
socket.off("ready_datapipe", onReady);
socket.off("error", onError);
socket.off("close", onClose);
stopExit();
};
socket.once("ready_datapipe", onReady);
socket.once("error", onError);
socket.once("close", onClose);
stopExit = trackedProcess.onExit(({ exitCode }) =>
onError(
new Error(`Windows PTY exited before its PID was available (exit code ${exitCode}).`),
),
);
return Effect.sync(cleanup);
});
// The private agent cancels pending connections before a child exists; public kill waits for first output.
const killStartingWindowsPty = (process: import("node-pty").IPty) =>
Effect.try(() => {
if (
"_agent" in process &&
typeof process._agent === "object" &&
process._agent !== null &&
"kill" in process._agent &&
typeof process._agent.kill === "function"
) {
process._agent.kill();
} else {
process.kill();
}
}).pipe(
Effect.catch((error) =>
Effect.logWarning("failed to cancel Windows terminal startup", {
terminalPid: process.pid,
cause: error,
}),
),
);
class NodePtyProcess implements PtyAdapter.PtyProcess {
private readonly process: import("node-pty").IPty;
private readonly platform: NodeJS.Platform;
private exitEvent: PtyAdapter.PtyExitEvent | undefined;
private readonly exitListeners = new Set<(event: PtyAdapter.PtyExitEvent) => void>();
private readonly exitSubscription: import("node-pty").IDisposable;
constructor(process: import("node-pty").IPty, platform: NodeJS.Platform) {
this.process = process;
this.platform = platform;
this.exitSubscription = process.onExit((event) => {
if (this.exitEvent) return;
this.exitEvent = { exitCode: event.exitCode, signal: event.signal ?? null };
this.exitSubscription.dispose();
for (const listener of this.exitListeners) listener(this.exitEvent);
this.exitListeners.clear();
});
}
get pid(): number {
return this.process.pid;
}
write(data: string): void {
this.process.write(data);
}
resize(cols: number, rows: number): void {
this.process.resize(cols, rows);
}
kill(signal?: string): void {
// node-pty terminates the Windows process tree without a POSIX signal.
this.process.kill(this.platform === "win32" ? undefined : signal);
}
onData(callback: (data: string) => void): () => void {
const disposable = this.process.onData(callback);
return () => {
disposable.dispose();
};
}
onExit(callback: (event: PtyAdapter.PtyExitEvent) => void): () => void {
if (this.exitEvent) {
callback(this.exitEvent);
return () => {};
}
this.exitListeners.add(callback);
return () => {
this.exitListeners.delete(callback);
};
}
disposeExitSubscription(): void {
this.exitSubscription.dispose();
this.exitListeners.clear();
}
}
const make = Effect.fn("NodePtyAdapter.make")(function* () {
const loadNodePtyModule = yield* NodePtyModuleLoaderRef;
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const platform = yield* HostProcess.Platform;
const architecture = yield* HostProcess.Architecture;
const nodePty = yield* Effect.tryPromise({
try: loadNodePtyModule,
catch: (cause) =>
new NodePtyModuleLoadError({
platform,
architecture,
cause,
}),
}).pipe(Effect.orDie);
const ensureNodePtySpawnHelperExecutableCached = yield* Effect.cached(
ensureNodePtySpawnHelperExecutable().pipe(
Effect.provideService(FileSystem.FileSystem, fs),
Effect.provideService(Path.Path, path),
Effect.provideService(HostProcess.Platform, platform),
Effect.provideService(HostProcess.Architecture, architecture),
Effect.orElseSucceed(() => undefined),
),
);
return PtyAdapter.PtyAdapter.of({
spawn: Effect.fn("NodePtyAdapter.spawn")(function* (input) {
yield* ensureNodePtySpawnHelperExecutableCached;
// ConPTY ignores node-pty's name option for TERM; set it explicitly to avoid missing/16-color values.
const env =
platform === "win32" && input.env["TERM"] === undefined
? { ...input.env, TERM: "xterm-256color" }
: input.env;
const ptyProcess = yield* Effect.try({
try: () => {
const nativeProcess = nodePty.spawn(input.shell, input.args ?? [], {
cwd: input.cwd,
cols: input.cols,
rows: input.rows,
env,
name: "xterm-256color",
});
return { nativeProcess, process: new NodePtyProcess(nativeProcess, platform) };
},
catch: (cause) =>
new PtyAdapter.PtySpawnError({
adapter: "node-pty",
shell: input.shell,
cause,
}),
});
if (platform === "win32") {
yield* waitForWindowsPid(ptyProcess.nativeProcess, ptyProcess.process, input.shell).pipe(
Effect.onError(() =>
Effect.sync(() => ptyProcess.process.disposeExitSubscription()).pipe(
Effect.andThen(killStartingWindowsPty(ptyProcess.nativeProcess)),
),
),
);
}
return ptyProcess.process;
}),
});
});
export const layer = Layer.effect(PtyAdapter.PtyAdapter, make());