Repository navigation
Expand file tree
/
Copy pathattach-client.ts
More file actions
275 lines (238 loc) · 6.77 KB
/
Copy pathattach-client.ts
File metadata and controls
275 lines (238 loc) · 6.77 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
import * as net from "node:net";
import * as os from "node:os";
import * as path from "node:path";
import { type AttachResult, createRequest, type ServerMessage, type ServerResponse } from "./messages.js";
export class AttachClient {
private socket?: net.Socket;
private isRawMode = false;
private stdin = process.stdin;
private stdout = process.stdout;
private attachedSession?: string;
/**
* Attach to a session
*/
async attach(sessionId: string): Promise<void> {
console.error(`Attaching to session ${sessionId}...`);
console.error("Press Ctrl+B to detach");
console.error("");
const socket = await this.connect();
this.socket = socket;
this.attachedSession = sessionId;
// Request attachment
const attachResponse = (await this.requestAttach(sessionId)) as AttachResult;
// Set up terminal
this.setupTerminal();
// Reset terminal completely before showing session
this.resetTerminal();
// Display initial output
if (attachResponse.rawOutput) {
if (!this.stdout.write(attachResponse.rawOutput)) {
// Handle backpressure - wait for drain before continuing
await new Promise((resolve) => this.stdout.once("drain", resolve));
}
}
// Handle incoming messages
let buffer = "";
socket.on("data", (data) => {
buffer += data.toString();
const lines = buffer.split("\n");
buffer = lines.pop() || "";
for (const line of lines) {
if (line.trim()) {
try {
const message: ServerMessage = JSON.parse(line);
this.handleMessage(message);
} catch (_err) {
// Ignore parse errors
}
}
}
});
socket.on("close", () => {
console.error("\n[Session closed]");
this.cleanup();
});
socket.on("error", (err) => {
console.error("\n[Connection error]", err.message);
this.cleanup();
});
}
/**
* Connect to the server
*/
private connect(): Promise<net.Socket> {
return new Promise((resolve, reject) => {
const socket = net.createConnection(path.join(os.homedir(), ".terminalcp", "server.sock"));
socket.once("connect", () => {
resolve(socket);
});
socket.once("error", (err) => {
reject(new Error(`Cannot connect to terminal server: ${err.message}`));
});
});
}
/**
* Send a request and wait for response
*/
private requestAttach(sessionId: string): Promise<AttachResult> {
return new Promise((resolve, reject) => {
const request = createRequest({ action: "attach", id: sessionId });
const requestId = request.id;
// Set up one-time response handler
let buffer = "";
const handleData = (data: Buffer) => {
buffer += data.toString();
const lines = buffer.split("\n");
buffer = lines.pop() || ""; // Keep incomplete line in buffer
for (const line of lines) {
if (line.trim()) {
try {
const response: ServerMessage = JSON.parse(line);
if (response.type === "response" && response.id === requestId) {
this.socket?.removeListener("data", handleData);
const res = response as ServerResponse;
if (res.error) {
reject(new Error(res.error));
} else {
if (!res.result || typeof res.result !== "object") {
reject(new Error("Invalid response format"));
return;
}
resolve(res.result as AttachResult);
}
return;
}
} catch (_err) {
// Continue looking
}
}
}
};
this.socket?.on("data", handleData);
const requestStr = `${JSON.stringify(request)}\n`;
this.socket?.write(requestStr, (err) => {
if (err) {
reject(new Error(`Failed to send request: ${err.message}`));
}
});
// Timeout after 5 seconds
setTimeout(() => {
this.socket?.removeListener("data", handleData);
reject(new Error(`Request timeout - no response for request ${requestId}`));
}, 5000);
});
}
/**
* Handle incoming server messages
*/
private handleMessage(message: ServerMessage): void {
if (message.type === "event") {
if (message.event === "output" && message.sessionId === this.attachedSession) {
if (!this.stdout.write(message.data)) {
// Handle backpressure - pause reading from socket until stdout drains
this.socket?.pause();
this.stdout.once("drain", () => {
this.socket?.resume();
});
}
}
}
}
/**
* Set up terminal for raw mode
*/
private setupTerminal(): void {
if (!this.stdin.isTTY) {
console.error("Not running in a TTY");
process.exit(1);
}
// Save current terminal state and enter raw mode
this.stdin.setRawMode(true);
this.isRawMode = true;
this.stdin.resume();
// Send initial terminal size
this.sendResize();
// Handle input
this.stdin.on("data", (data) => {
// Check for Ctrl+B to detach
if (data[0] === 0x02) {
// Ctrl+B
this.detach();
return;
}
// Forward input to server
if (this.socket && !this.socket.destroyed && this.attachedSession) {
const message = createRequest({
action: "stdin",
id: this.attachedSession,
data: data.toString(),
});
if (!this.socket.write(`${JSON.stringify(message)}\n`)) {
// Handle backpressure - pause stdin until socket drains
this.stdin.pause();
this.socket.once("drain", () => {
this.stdin.resume();
});
}
}
});
// Handle terminal resize
this.stdout.on("resize", () => {
this.sendResize();
});
}
/**
* Send terminal resize event
*/
private sendResize(): void {
if (!this.socket || this.socket.destroyed || !this.attachedSession) return;
const cols = this.stdout.columns || 80;
const rows = this.stdout.rows || 24;
const message = createRequest({
action: "resize",
id: this.attachedSession,
cols,
rows,
});
this.socket.write(`${JSON.stringify(message)}\n`);
}
/**
* Detach from session
*/
private detach(): void {
console.error("\n[Detaching...]");
if (this.socket && !this.socket.destroyed && this.attachedSession) {
const message = createRequest({
action: "detach",
id: this.attachedSession,
});
this.socket.write(`${JSON.stringify(message)}\n`);
this.socket.end();
}
this.cleanup();
}
/**
* Reset terminal to default state
*/
private resetTerminal(): void {
// Reset terminal to clear any lingering state:
// - \x1b[?1000l, \x1b[?1002l, \x1b[?1003l, \x1b[?1006l: Disable mouse tracking modes
// - \x1b[?47l, \x1b[?1049l: Exit alternate screen buffer
// - \x1b[?25h: Show cursor
// - \x1b[0m: Reset all colors and text attributes
// - \x1bc: Full terminal reset
this.stdout.write("\x1b[?1000l\x1b[?1002l\x1b[?1003l\x1b[?1006l\x1b[?47l\x1b[?1049l\x1b[?25h\x1b[0m\x1bc");
}
/**
* Clean up and restore terminal
*/
private cleanup(): void {
if (this.isRawMode && this.stdin.isTTY) {
this.stdin.setRawMode(false);
this.isRawMode = false;
}
this.stdin.pause();
this.resetTerminal();
process.exit(0);
}
}