Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions apps/server/src/terminal/BunPtyAdapter.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect, it } from "@effect/vitest";

import { BunPtyOperationUnavailableError } from "./BunPtyAdapter.ts";

it("describes unavailable Bun PTY operations structurally", () => {
const error = new BunPtyOperationUnavailableError({
operation: "resize",
pid: 42,
});

expect(error).toMatchObject({
_tag: "BunPtyOperationUnavailableError",
operation: "resize",
pid: 42,
});
expect(error.message).toBe("Bun PTY resize is unavailable for process 42.");
});
17 changes: 15 additions & 2 deletions apps/server/src/terminal/BunPtyAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@

import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";

import * as PtyAdapter from "./PtyAdapter.ts";

export class BunPtyOperationUnavailableError extends Schema.TaggedErrorClass<BunPtyOperationUnavailableError>()(
"BunPtyOperationUnavailableError",
{
operation: Schema.Literals(["write", "resize"]),
pid: Schema.Number,
},
) {
override get message(): string {
return `Bun PTY ${this.operation} is unavailable for process ${this.pid}.`;
}
}

class BunPtyProcess implements PtyAdapter.PtyProcess {
private readonly dataListeners = new Set<(data: string) => void>();
private readonly exitListeners = new Set<(event: PtyAdapter.PtyExitEvent) => void>();
Expand Down Expand Up @@ -33,14 +46,14 @@ class BunPtyProcess implements PtyAdapter.PtyProcess {

write(data: string): void {
if (!this.process.terminal) {
throw new Error("Bun PTY terminal handle is unavailable");
throw new BunPtyOperationUnavailableError({ operation: "write", pid: this.pid });
}
this.process.terminal.write(data);
}

resize(cols: number, rows: number): void {
if (!this.process.terminal?.resize) {
throw new Error("Bun PTY resize is unavailable");
throw new BunPtyOperationUnavailableError({ operation: "resize", pid: this.pid });
}
this.process.terminal.resize(cols, rows);
}
Expand Down
Loading