Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Schema from "effect/Schema";
4
5export class PtySpawnError extends Schema.TaggedError<PtySpawnError>()("PtySpawnError", {
6 adapter: Schema.String,
7 shell: Schema.optional(Schema.String),
8 attemptedShells: Schema.optional(Schema.Array(Schema.String)),
9 cause: Schema.optional(Schema.Defect()),
10}) {
11 override get message(): string {
12 const shell = this.shell === undefined ? "" : ` '${this.shell}'`;
13 const attemptedShells =
14 this.attemptedShells === undefined || this.attemptedShells.length === 0
15 ? ""
16 : ` Tried shells: ${this.attemptedShells.join(", ")}.`;
17 return `Failed to spawn PTY process${shell} with ${this.adapter}.${attemptedShells}`;
18 }
19}
20
21export interface PtyExitEvent {
22 exitCode: number;
23 signal: number | null;
24}
25
26/** What a PTY receives for client terminal input, whose `binary` data is one byte per code unit. */
27export function ptyInput(data: string, binary: boolean | undefined): string | Buffer {
28 return binary ? Buffer.from(data, "latin1") : data;
29}
30
31export interface PtyProcess {
32 readonly pid: number;
33 write(data: string | Buffer): void;
34 resize(cols: number, rows: number): void;
35 kill(signal?: string): void;
36 onData(callback: (data: string) => void): () => void;
37 onExit(callback: (event: PtyExitEvent) => void): () => void;
38}
39
40export interface PtySpawnInput {
41 shell: string;
42 args?: string[];
43 cwd: string;
44 cols: number;
45 rows: number;
46 env: NodeJS.ProcessEnv;
47}
48
49export class PtyAdapter extends Context.Service<
50 PtyAdapter,
51 {
52 readonly spawn: (input: PtySpawnInput) => Effect.Effect<PtyProcess, PtySpawnError>;
53 }
54>()("@t3tools/shared/PtyAdapter") {}