@cloudflare/sandbox 0.0.0-ee8c772 → 0.0.0-eec5bb6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +199 -0
- package/Dockerfile +96 -10
- package/README.md +806 -23
- package/container_src/bun.lock +76 -0
- package/container_src/circuit-breaker.ts +121 -0
- package/container_src/control-process.ts +784 -0
- package/container_src/handler/exec.ts +185 -0
- package/container_src/handler/file.ts +406 -0
- package/container_src/handler/git.ts +130 -0
- package/container_src/handler/ports.ts +314 -0
- package/container_src/handler/process.ts +568 -0
- package/container_src/handler/session.ts +92 -0
- package/container_src/index.ts +432 -2740
- package/container_src/interpreter-service.ts +276 -0
- package/container_src/isolation.ts +1038 -0
- package/container_src/mime-processor.ts +255 -0
- package/container_src/package.json +9 -0
- package/container_src/runtime/executors/javascript/node_executor.ts +123 -0
- package/container_src/runtime/executors/python/ipython_executor.py +338 -0
- package/container_src/runtime/executors/typescript/ts_executor.ts +138 -0
- package/container_src/runtime/process-pool.ts +464 -0
- package/container_src/shell-escape.ts +42 -0
- package/container_src/startup.sh +11 -0
- package/container_src/types.ts +131 -0
- package/package.json +6 -8
- package/src/client.ts +442 -1362
- package/src/errors.ts +219 -0
- package/src/index.ts +72 -126
- package/src/interpreter-client.ts +352 -0
- package/src/interpreter-types.ts +390 -0
- package/src/interpreter.ts +150 -0
- package/src/request-handler.ts +144 -0
- package/src/sandbox.ts +747 -0
- package/src/security.ts +113 -0
- package/src/sse-parser.ts +147 -0
- package/src/types.ts +502 -0
- package/tsconfig.json +1 -1
- package/tests/client.example.ts +0 -308
- package/tests/connection-test.ts +0 -81
- package/tests/simple-test.ts +0 -81
- package/tests/test1.ts +0 -281
- package/tests/test2.ts +0 -929
package/container_src/index.ts
CHANGED
|
@@ -1,89 +1,97 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
|
-
import { mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
|
|
3
|
-
import { dirname } from "node:path";
|
|
4
1
|
import { serve } from "bun";
|
|
2
|
+
import {
|
|
3
|
+
handleExecuteRequest,
|
|
4
|
+
handleStreamingExecuteRequest,
|
|
5
|
+
} from "./handler/exec";
|
|
6
|
+
import {
|
|
7
|
+
handleDeleteFileRequest,
|
|
8
|
+
handleListFilesRequest,
|
|
9
|
+
handleMkdirRequest,
|
|
10
|
+
handleMoveFileRequest,
|
|
11
|
+
handleReadFileRequest,
|
|
12
|
+
handleRenameFileRequest,
|
|
13
|
+
handleWriteFileRequest,
|
|
14
|
+
} from "./handler/file";
|
|
15
|
+
import { handleGitCheckoutRequest } from "./handler/git";
|
|
16
|
+
import {
|
|
17
|
+
handleExposePortRequest,
|
|
18
|
+
handleGetExposedPortsRequest,
|
|
19
|
+
handleProxyRequest,
|
|
20
|
+
handleUnexposePortRequest,
|
|
21
|
+
} from "./handler/ports";
|
|
22
|
+
import {
|
|
23
|
+
handleGetProcessLogsRequest,
|
|
24
|
+
handleGetProcessRequest,
|
|
25
|
+
handleKillAllProcessesRequest,
|
|
26
|
+
handleKillProcessRequest,
|
|
27
|
+
handleListProcessesRequest,
|
|
28
|
+
handleStartProcessRequest,
|
|
29
|
+
handleStreamProcessLogsRequest,
|
|
30
|
+
} from "./handler/process";
|
|
31
|
+
import { handleCreateSession, handleListSessions } from "./handler/session";
|
|
32
|
+
import type { CreateContextRequest } from "./interpreter-service";
|
|
33
|
+
import {
|
|
34
|
+
InterpreterNotReadyError,
|
|
35
|
+
InterpreterService,
|
|
36
|
+
} from "./interpreter-service";
|
|
37
|
+
import { hasNamespaceSupport, SessionManager } from "./isolation";
|
|
38
|
+
|
|
39
|
+
// In-memory storage for exposed ports
|
|
40
|
+
const exposedPorts = new Map<number, { name?: string; exposedAt: Date }>();
|
|
41
|
+
|
|
42
|
+
// Check isolation capabilities on startup
|
|
43
|
+
const isolationAvailable = hasNamespaceSupport();
|
|
44
|
+
console.log(
|
|
45
|
+
`[Container] Process isolation: ${
|
|
46
|
+
isolationAvailable
|
|
47
|
+
? "ENABLED (production mode)"
|
|
48
|
+
: "DISABLED (development mode)"
|
|
49
|
+
}`
|
|
50
|
+
);
|
|
5
51
|
|
|
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
|
-
interface RenameFileRequest {
|
|
43
|
-
oldPath: string;
|
|
44
|
-
newPath: string;
|
|
45
|
-
sessionId?: string;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
interface MoveFileRequest {
|
|
49
|
-
sourcePath: string;
|
|
50
|
-
destinationPath: string;
|
|
51
|
-
sessionId?: string;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
interface SessionData {
|
|
55
|
-
sessionId: string;
|
|
56
|
-
activeProcess: any | null;
|
|
57
|
-
createdAt: Date;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
// In-memory session storage (in production, you'd want to use a proper database)
|
|
61
|
-
const sessions = new Map<string, SessionData>();
|
|
62
|
-
|
|
63
|
-
// Generate a unique session ID
|
|
64
|
-
function generateSessionId(): string {
|
|
65
|
-
return `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// Clean up old sessions (older than 1 hour)
|
|
69
|
-
function cleanupOldSessions() {
|
|
70
|
-
const oneHourAgo = new Date(Date.now() - 60 * 60 * 1000);
|
|
71
|
-
for (const [sessionId, session] of sessions.entries()) {
|
|
72
|
-
if (session.createdAt < oneHourAgo && !session.activeProcess) {
|
|
73
|
-
sessions.delete(sessionId);
|
|
74
|
-
console.log(`[Server] Cleaned up old session: ${sessionId}`);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Run cleanup every 10 minutes
|
|
80
|
-
setInterval(cleanupOldSessions, 10 * 60 * 1000);
|
|
52
|
+
// Session manager for secure execution with isolation
|
|
53
|
+
const sessionManager = new SessionManager();
|
|
54
|
+
|
|
55
|
+
// Graceful shutdown handler
|
|
56
|
+
const SHUTDOWN_GRACE_PERIOD_MS = 5000; // Grace period for cleanup (5 seconds for proper async cleanup)
|
|
57
|
+
|
|
58
|
+
process.on("SIGTERM", async () => {
|
|
59
|
+
console.log("[Container] SIGTERM received, cleaning up sessions...");
|
|
60
|
+
await sessionManager.destroyAll();
|
|
61
|
+
setTimeout(() => {
|
|
62
|
+
process.exit(0);
|
|
63
|
+
}, SHUTDOWN_GRACE_PERIOD_MS);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
process.on("SIGINT", async () => {
|
|
67
|
+
console.log("[Container] SIGINT received, cleaning up sessions...");
|
|
68
|
+
await sessionManager.destroyAll();
|
|
69
|
+
setTimeout(() => {
|
|
70
|
+
process.exit(0);
|
|
71
|
+
}, SHUTDOWN_GRACE_PERIOD_MS);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Cleanup on uncaught exceptions (log but still exit)
|
|
75
|
+
process.on("uncaughtException", async (error) => {
|
|
76
|
+
console.error("[Container] Uncaught exception:", error);
|
|
77
|
+
await sessionManager.destroyAll();
|
|
78
|
+
process.exit(1);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Initialize interpreter service
|
|
82
|
+
const interpreterService = new InterpreterService();
|
|
83
|
+
|
|
84
|
+
// No initialization needed - service is ready immediately!
|
|
85
|
+
console.log("[Container] Interpreter service ready - no cold start!");
|
|
86
|
+
console.log("[Container] All API endpoints available immediately");
|
|
81
87
|
|
|
82
88
|
const server = serve({
|
|
83
|
-
fetch(req: Request) {
|
|
89
|
+
async fetch(req: Request) {
|
|
84
90
|
const url = new URL(req.url);
|
|
85
91
|
const pathname = url.pathname;
|
|
86
92
|
|
|
93
|
+
console.log(`[Container] Incoming ${req.method} request to ${pathname}`);
|
|
94
|
+
|
|
87
95
|
// Handle CORS
|
|
88
96
|
const corsHeaders = {
|
|
89
97
|
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
|
@@ -93,11 +101,13 @@ const server = serve({
|
|
|
93
101
|
|
|
94
102
|
// Handle preflight requests
|
|
95
103
|
if (req.method === "OPTIONS") {
|
|
104
|
+
console.log(`[Container] Handling CORS preflight for ${pathname}`);
|
|
96
105
|
return new Response(null, { headers: corsHeaders, status: 200 });
|
|
97
106
|
}
|
|
98
107
|
|
|
99
108
|
try {
|
|
100
109
|
// Handle different routes
|
|
110
|
+
console.log(`[Container] Processing ${req.method} ${pathname}`);
|
|
101
111
|
switch (pathname) {
|
|
102
112
|
case "/":
|
|
103
113
|
return new Response("Hello from Bun server! 🚀", {
|
|
@@ -107,123 +117,44 @@ const server = serve({
|
|
|
107
117
|
},
|
|
108
118
|
});
|
|
109
119
|
|
|
110
|
-
case "/api/hello":
|
|
111
|
-
return new Response(
|
|
112
|
-
JSON.stringify({
|
|
113
|
-
message: "Hello from API!",
|
|
114
|
-
timestamp: new Date().toISOString(),
|
|
115
|
-
}),
|
|
116
|
-
{
|
|
117
|
-
headers: {
|
|
118
|
-
"Content-Type": "application/json",
|
|
119
|
-
...corsHeaders,
|
|
120
|
-
},
|
|
121
|
-
}
|
|
122
|
-
);
|
|
123
|
-
|
|
124
|
-
case "/api/users":
|
|
125
|
-
if (req.method === "GET") {
|
|
126
|
-
return new Response(
|
|
127
|
-
JSON.stringify([
|
|
128
|
-
{ id: 1, name: "Alice" },
|
|
129
|
-
{ id: 2, name: "Bob" },
|
|
130
|
-
{ id: 3, name: "Charlie" },
|
|
131
|
-
]),
|
|
132
|
-
{
|
|
133
|
-
headers: {
|
|
134
|
-
"Content-Type": "application/json",
|
|
135
|
-
...corsHeaders,
|
|
136
|
-
},
|
|
137
|
-
}
|
|
138
|
-
);
|
|
139
|
-
} else if (req.method === "POST") {
|
|
140
|
-
return new Response(
|
|
141
|
-
JSON.stringify({
|
|
142
|
-
message: "User created successfully",
|
|
143
|
-
method: "POST",
|
|
144
|
-
}),
|
|
145
|
-
{
|
|
146
|
-
headers: {
|
|
147
|
-
"Content-Type": "application/json",
|
|
148
|
-
...corsHeaders,
|
|
149
|
-
},
|
|
150
|
-
}
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
break;
|
|
154
|
-
|
|
155
120
|
case "/api/session/create":
|
|
156
121
|
if (req.method === "POST") {
|
|
157
|
-
|
|
158
|
-
const sessionData: SessionData = {
|
|
159
|
-
activeProcess: null,
|
|
160
|
-
createdAt: new Date(),
|
|
161
|
-
sessionId,
|
|
162
|
-
};
|
|
163
|
-
sessions.set(sessionId, sessionData);
|
|
164
|
-
|
|
165
|
-
console.log(`[Server] Created new session: ${sessionId}`);
|
|
166
|
-
|
|
167
|
-
return new Response(
|
|
168
|
-
JSON.stringify({
|
|
169
|
-
message: "Session created successfully",
|
|
170
|
-
sessionId,
|
|
171
|
-
timestamp: new Date().toISOString(),
|
|
172
|
-
}),
|
|
173
|
-
{
|
|
174
|
-
headers: {
|
|
175
|
-
"Content-Type": "application/json",
|
|
176
|
-
...corsHeaders,
|
|
177
|
-
},
|
|
178
|
-
}
|
|
179
|
-
);
|
|
122
|
+
return handleCreateSession(req, corsHeaders, sessionManager);
|
|
180
123
|
}
|
|
181
124
|
break;
|
|
182
125
|
|
|
183
126
|
case "/api/session/list":
|
|
184
127
|
if (req.method === "GET") {
|
|
185
|
-
|
|
186
|
-
(session) => ({
|
|
187
|
-
createdAt: session.createdAt.toISOString(),
|
|
188
|
-
hasActiveProcess: !!session.activeProcess,
|
|
189
|
-
sessionId: session.sessionId,
|
|
190
|
-
})
|
|
191
|
-
);
|
|
192
|
-
|
|
193
|
-
return new Response(
|
|
194
|
-
JSON.stringify({
|
|
195
|
-
count: sessionList.length,
|
|
196
|
-
sessions: sessionList,
|
|
197
|
-
timestamp: new Date().toISOString(),
|
|
198
|
-
}),
|
|
199
|
-
{
|
|
200
|
-
headers: {
|
|
201
|
-
"Content-Type": "application/json",
|
|
202
|
-
...corsHeaders,
|
|
203
|
-
},
|
|
204
|
-
}
|
|
205
|
-
);
|
|
128
|
+
return handleListSessions(corsHeaders, sessionManager);
|
|
206
129
|
}
|
|
207
130
|
break;
|
|
208
131
|
|
|
209
132
|
case "/api/execute":
|
|
210
133
|
if (req.method === "POST") {
|
|
211
|
-
return handleExecuteRequest(req, corsHeaders);
|
|
134
|
+
return handleExecuteRequest(req, corsHeaders, sessionManager);
|
|
212
135
|
}
|
|
213
136
|
break;
|
|
214
137
|
|
|
215
138
|
case "/api/execute/stream":
|
|
216
139
|
if (req.method === "POST") {
|
|
217
|
-
return handleStreamingExecuteRequest(
|
|
140
|
+
return handleStreamingExecuteRequest(
|
|
141
|
+
req,
|
|
142
|
+
sessionManager,
|
|
143
|
+
corsHeaders
|
|
144
|
+
);
|
|
218
145
|
}
|
|
219
146
|
break;
|
|
220
147
|
|
|
221
148
|
case "/api/ping":
|
|
222
149
|
if (req.method === "GET") {
|
|
150
|
+
const health = await interpreterService.getHealthStatus();
|
|
223
151
|
return new Response(
|
|
224
152
|
JSON.stringify({
|
|
225
153
|
message: "pong",
|
|
226
154
|
timestamp: new Date().toISOString(),
|
|
155
|
+
system: "interpreter (70x faster)",
|
|
156
|
+
status: health.ready ? "ready" : "initializing",
|
|
157
|
+
progress: health.progress,
|
|
227
158
|
}),
|
|
228
159
|
{
|
|
229
160
|
headers: {
|
|
@@ -235,130 +166,378 @@ const server = serve({
|
|
|
235
166
|
}
|
|
236
167
|
break;
|
|
237
168
|
|
|
238
|
-
case "/api/
|
|
239
|
-
if (req.method === "
|
|
240
|
-
return
|
|
241
|
-
JSON.stringify({
|
|
242
|
-
availableCommands: [
|
|
243
|
-
"ls",
|
|
244
|
-
"pwd",
|
|
245
|
-
"echo",
|
|
246
|
-
"cat",
|
|
247
|
-
"grep",
|
|
248
|
-
"find",
|
|
249
|
-
"whoami",
|
|
250
|
-
"date",
|
|
251
|
-
"uptime",
|
|
252
|
-
"ps",
|
|
253
|
-
"top",
|
|
254
|
-
"df",
|
|
255
|
-
"du",
|
|
256
|
-
"free",
|
|
257
|
-
],
|
|
258
|
-
timestamp: new Date().toISOString(),
|
|
259
|
-
}),
|
|
260
|
-
{
|
|
261
|
-
headers: {
|
|
262
|
-
"Content-Type": "application/json",
|
|
263
|
-
...corsHeaders,
|
|
264
|
-
},
|
|
265
|
-
}
|
|
266
|
-
);
|
|
169
|
+
case "/api/git/checkout":
|
|
170
|
+
if (req.method === "POST") {
|
|
171
|
+
return handleGitCheckoutRequest(req, corsHeaders, sessionManager);
|
|
267
172
|
}
|
|
268
173
|
break;
|
|
269
174
|
|
|
270
|
-
case "/api/
|
|
175
|
+
case "/api/mkdir":
|
|
271
176
|
if (req.method === "POST") {
|
|
272
|
-
return
|
|
177
|
+
return handleMkdirRequest(req, corsHeaders, sessionManager);
|
|
273
178
|
}
|
|
274
179
|
break;
|
|
275
180
|
|
|
276
|
-
case "/api/
|
|
181
|
+
case "/api/write":
|
|
277
182
|
if (req.method === "POST") {
|
|
278
|
-
return
|
|
183
|
+
return handleWriteFileRequest(req, corsHeaders, sessionManager);
|
|
279
184
|
}
|
|
280
185
|
break;
|
|
281
186
|
|
|
282
|
-
case "/api/
|
|
187
|
+
case "/api/read":
|
|
283
188
|
if (req.method === "POST") {
|
|
284
|
-
return
|
|
189
|
+
return handleReadFileRequest(req, corsHeaders, sessionManager);
|
|
285
190
|
}
|
|
286
191
|
break;
|
|
287
192
|
|
|
288
|
-
case "/api/
|
|
193
|
+
case "/api/delete":
|
|
289
194
|
if (req.method === "POST") {
|
|
290
|
-
return
|
|
195
|
+
return handleDeleteFileRequest(req, corsHeaders, sessionManager);
|
|
291
196
|
}
|
|
292
197
|
break;
|
|
293
198
|
|
|
294
|
-
case "/api/
|
|
199
|
+
case "/api/rename":
|
|
295
200
|
if (req.method === "POST") {
|
|
296
|
-
return
|
|
201
|
+
return handleRenameFileRequest(req, corsHeaders, sessionManager);
|
|
297
202
|
}
|
|
298
203
|
break;
|
|
299
204
|
|
|
300
|
-
case "/api/
|
|
205
|
+
case "/api/move":
|
|
301
206
|
if (req.method === "POST") {
|
|
302
|
-
return
|
|
207
|
+
return handleMoveFileRequest(req, corsHeaders, sessionManager);
|
|
303
208
|
}
|
|
304
209
|
break;
|
|
305
210
|
|
|
306
|
-
case "/api/
|
|
211
|
+
case "/api/list-files":
|
|
307
212
|
if (req.method === "POST") {
|
|
308
|
-
return
|
|
213
|
+
return handleListFilesRequest(req, corsHeaders, sessionManager);
|
|
309
214
|
}
|
|
310
215
|
break;
|
|
311
216
|
|
|
312
|
-
case "/api/
|
|
217
|
+
case "/api/expose-port":
|
|
313
218
|
if (req.method === "POST") {
|
|
314
|
-
return
|
|
219
|
+
return handleExposePortRequest(exposedPorts, req, corsHeaders);
|
|
315
220
|
}
|
|
316
221
|
break;
|
|
317
222
|
|
|
318
|
-
case "/api/
|
|
319
|
-
if (req.method === "
|
|
320
|
-
return
|
|
223
|
+
case "/api/unexpose-port":
|
|
224
|
+
if (req.method === "DELETE") {
|
|
225
|
+
return handleUnexposePortRequest(exposedPorts, req, corsHeaders);
|
|
321
226
|
}
|
|
322
227
|
break;
|
|
323
228
|
|
|
324
|
-
case "/api/
|
|
325
|
-
if (req.method === "
|
|
326
|
-
return
|
|
229
|
+
case "/api/exposed-ports":
|
|
230
|
+
if (req.method === "GET") {
|
|
231
|
+
return handleGetExposedPortsRequest(exposedPorts, req, corsHeaders);
|
|
327
232
|
}
|
|
328
233
|
break;
|
|
329
234
|
|
|
330
|
-
case "/api/
|
|
235
|
+
case "/api/process/start":
|
|
331
236
|
if (req.method === "POST") {
|
|
332
|
-
return
|
|
237
|
+
return handleStartProcessRequest(req, corsHeaders, sessionManager);
|
|
333
238
|
}
|
|
334
239
|
break;
|
|
335
240
|
|
|
336
|
-
case "/api/
|
|
337
|
-
if (req.method === "
|
|
338
|
-
return
|
|
241
|
+
case "/api/process/list":
|
|
242
|
+
if (req.method === "GET") {
|
|
243
|
+
return handleListProcessesRequest(req, corsHeaders, sessionManager);
|
|
339
244
|
}
|
|
340
245
|
break;
|
|
341
246
|
|
|
342
|
-
case "/api/
|
|
247
|
+
case "/api/process/kill-all":
|
|
248
|
+
if (req.method === "DELETE") {
|
|
249
|
+
return handleKillAllProcessesRequest(
|
|
250
|
+
req,
|
|
251
|
+
corsHeaders,
|
|
252
|
+
sessionManager
|
|
253
|
+
);
|
|
254
|
+
}
|
|
255
|
+
break;
|
|
256
|
+
|
|
257
|
+
case "/api/contexts":
|
|
343
258
|
if (req.method === "POST") {
|
|
344
|
-
|
|
259
|
+
try {
|
|
260
|
+
const body = (await req.json()) as CreateContextRequest;
|
|
261
|
+
const context = await interpreterService.createContext(body);
|
|
262
|
+
return new Response(
|
|
263
|
+
JSON.stringify({
|
|
264
|
+
id: context.id,
|
|
265
|
+
language: context.language,
|
|
266
|
+
cwd: context.cwd,
|
|
267
|
+
createdAt: context.createdAt,
|
|
268
|
+
lastUsed: context.lastUsed,
|
|
269
|
+
}),
|
|
270
|
+
{
|
|
271
|
+
headers: {
|
|
272
|
+
"Content-Type": "application/json",
|
|
273
|
+
...corsHeaders,
|
|
274
|
+
},
|
|
275
|
+
}
|
|
276
|
+
);
|
|
277
|
+
} catch (error) {
|
|
278
|
+
if (error instanceof InterpreterNotReadyError) {
|
|
279
|
+
console.log(
|
|
280
|
+
`[Container] Request timed out waiting for interpreter (${error.progress}% complete)`
|
|
281
|
+
);
|
|
282
|
+
return new Response(
|
|
283
|
+
JSON.stringify({
|
|
284
|
+
error: error.message,
|
|
285
|
+
status: "initializing",
|
|
286
|
+
progress: error.progress,
|
|
287
|
+
}),
|
|
288
|
+
{
|
|
289
|
+
status: 503,
|
|
290
|
+
headers: {
|
|
291
|
+
"Content-Type": "application/json",
|
|
292
|
+
"Retry-After": String(error.retryAfter),
|
|
293
|
+
...corsHeaders,
|
|
294
|
+
},
|
|
295
|
+
}
|
|
296
|
+
);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Check if it's a circuit breaker error
|
|
300
|
+
if (
|
|
301
|
+
error instanceof Error &&
|
|
302
|
+
error.message.includes("Circuit breaker is open")
|
|
303
|
+
) {
|
|
304
|
+
console.log(
|
|
305
|
+
"[Container] Circuit breaker is open:",
|
|
306
|
+
error.message
|
|
307
|
+
);
|
|
308
|
+
return new Response(
|
|
309
|
+
JSON.stringify({
|
|
310
|
+
error:
|
|
311
|
+
"Service temporarily unavailable due to high error rate. Please try again later.",
|
|
312
|
+
status: "circuit_open",
|
|
313
|
+
details: error.message,
|
|
314
|
+
}),
|
|
315
|
+
{
|
|
316
|
+
status: 503,
|
|
317
|
+
headers: {
|
|
318
|
+
"Content-Type": "application/json",
|
|
319
|
+
"Retry-After": "60",
|
|
320
|
+
...corsHeaders,
|
|
321
|
+
},
|
|
322
|
+
}
|
|
323
|
+
);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Only log actual errors with stack traces
|
|
327
|
+
console.error("[Container] Error creating context:", error);
|
|
328
|
+
return new Response(
|
|
329
|
+
JSON.stringify({
|
|
330
|
+
error:
|
|
331
|
+
error instanceof Error
|
|
332
|
+
? error.message
|
|
333
|
+
: "Failed to create context",
|
|
334
|
+
}),
|
|
335
|
+
{
|
|
336
|
+
status: 500,
|
|
337
|
+
headers: {
|
|
338
|
+
"Content-Type": "application/json",
|
|
339
|
+
...corsHeaders,
|
|
340
|
+
},
|
|
341
|
+
}
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
} else if (req.method === "GET") {
|
|
345
|
+
const contexts = await interpreterService.listContexts();
|
|
346
|
+
return new Response(JSON.stringify({ contexts }), {
|
|
347
|
+
headers: {
|
|
348
|
+
"Content-Type": "application/json",
|
|
349
|
+
...corsHeaders,
|
|
350
|
+
},
|
|
351
|
+
});
|
|
345
352
|
}
|
|
346
353
|
break;
|
|
347
354
|
|
|
348
|
-
case "/api/
|
|
355
|
+
case "/api/execute/code":
|
|
349
356
|
if (req.method === "POST") {
|
|
350
|
-
|
|
357
|
+
try {
|
|
358
|
+
const body = (await req.json()) as {
|
|
359
|
+
context_id: string;
|
|
360
|
+
code: string;
|
|
361
|
+
language?: string;
|
|
362
|
+
};
|
|
363
|
+
return await interpreterService.executeCode(
|
|
364
|
+
body.context_id,
|
|
365
|
+
body.code,
|
|
366
|
+
body.language
|
|
367
|
+
);
|
|
368
|
+
} catch (error) {
|
|
369
|
+
// Check if it's a circuit breaker error
|
|
370
|
+
if (
|
|
371
|
+
error instanceof Error &&
|
|
372
|
+
error.message.includes("Circuit breaker is open")
|
|
373
|
+
) {
|
|
374
|
+
console.log(
|
|
375
|
+
"[Container] Circuit breaker is open for code execution:",
|
|
376
|
+
error.message
|
|
377
|
+
);
|
|
378
|
+
return new Response(
|
|
379
|
+
JSON.stringify({
|
|
380
|
+
error:
|
|
381
|
+
"Service temporarily unavailable due to high error rate. Please try again later.",
|
|
382
|
+
status: "circuit_open",
|
|
383
|
+
details: error.message,
|
|
384
|
+
}),
|
|
385
|
+
{
|
|
386
|
+
status: 503,
|
|
387
|
+
headers: {
|
|
388
|
+
"Content-Type": "application/json",
|
|
389
|
+
"Retry-After": "30",
|
|
390
|
+
...corsHeaders,
|
|
391
|
+
},
|
|
392
|
+
}
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
// Don't log stack traces for expected initialization state
|
|
397
|
+
if (
|
|
398
|
+
error instanceof Error &&
|
|
399
|
+
error.message.includes("initializing")
|
|
400
|
+
) {
|
|
401
|
+
console.log(
|
|
402
|
+
"[Container] Code execution deferred - service still initializing"
|
|
403
|
+
);
|
|
404
|
+
} else {
|
|
405
|
+
console.error("[Container] Error executing code:", error);
|
|
406
|
+
}
|
|
407
|
+
// Error response is already handled by service.executeCode for not ready state
|
|
408
|
+
return new Response(
|
|
409
|
+
JSON.stringify({
|
|
410
|
+
error:
|
|
411
|
+
error instanceof Error
|
|
412
|
+
? error.message
|
|
413
|
+
: "Failed to execute code",
|
|
414
|
+
}),
|
|
415
|
+
{
|
|
416
|
+
status: 500,
|
|
417
|
+
headers: {
|
|
418
|
+
"Content-Type": "application/json",
|
|
419
|
+
...corsHeaders,
|
|
420
|
+
},
|
|
421
|
+
}
|
|
422
|
+
);
|
|
423
|
+
}
|
|
351
424
|
}
|
|
352
425
|
break;
|
|
353
426
|
|
|
354
427
|
default:
|
|
428
|
+
// Handle dynamic routes for contexts
|
|
429
|
+
if (
|
|
430
|
+
pathname.startsWith("/api/contexts/") &&
|
|
431
|
+
pathname.split("/").length === 4
|
|
432
|
+
) {
|
|
433
|
+
const contextId = pathname.split("/")[3];
|
|
434
|
+
if (req.method === "DELETE") {
|
|
435
|
+
try {
|
|
436
|
+
await interpreterService.deleteContext(contextId);
|
|
437
|
+
return new Response(JSON.stringify({ success: true }), {
|
|
438
|
+
headers: {
|
|
439
|
+
"Content-Type": "application/json",
|
|
440
|
+
...corsHeaders,
|
|
441
|
+
},
|
|
442
|
+
});
|
|
443
|
+
} catch (error) {
|
|
444
|
+
if (error instanceof InterpreterNotReadyError) {
|
|
445
|
+
console.log(
|
|
446
|
+
`[Container] Request timed out waiting for interpreter (${error.progress}% complete)`
|
|
447
|
+
);
|
|
448
|
+
return new Response(
|
|
449
|
+
JSON.stringify({
|
|
450
|
+
error: error.message,
|
|
451
|
+
status: "initializing",
|
|
452
|
+
progress: error.progress,
|
|
453
|
+
}),
|
|
454
|
+
{
|
|
455
|
+
status: 503,
|
|
456
|
+
headers: {
|
|
457
|
+
"Content-Type": "application/json",
|
|
458
|
+
"Retry-After": "5",
|
|
459
|
+
...corsHeaders,
|
|
460
|
+
},
|
|
461
|
+
}
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
return new Response(
|
|
465
|
+
JSON.stringify({
|
|
466
|
+
error:
|
|
467
|
+
error instanceof Error
|
|
468
|
+
? error.message
|
|
469
|
+
: "Failed to delete context",
|
|
470
|
+
}),
|
|
471
|
+
{
|
|
472
|
+
status:
|
|
473
|
+
error instanceof Error &&
|
|
474
|
+
error.message.includes("not found")
|
|
475
|
+
? 404
|
|
476
|
+
: 500,
|
|
477
|
+
headers: {
|
|
478
|
+
"Content-Type": "application/json",
|
|
479
|
+
...corsHeaders,
|
|
480
|
+
},
|
|
481
|
+
}
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// Handle dynamic routes for individual processes
|
|
488
|
+
if (pathname.startsWith("/api/process/")) {
|
|
489
|
+
const segments = pathname.split("/");
|
|
490
|
+
if (segments.length >= 4) {
|
|
491
|
+
const processId = segments[3];
|
|
492
|
+
const action = segments[4]; // Optional: logs, stream, etc.
|
|
493
|
+
|
|
494
|
+
if (!action && req.method === "GET") {
|
|
495
|
+
return handleGetProcessRequest(
|
|
496
|
+
req,
|
|
497
|
+
corsHeaders,
|
|
498
|
+
processId,
|
|
499
|
+
sessionManager
|
|
500
|
+
);
|
|
501
|
+
} else if (!action && req.method === "DELETE") {
|
|
502
|
+
return handleKillProcessRequest(
|
|
503
|
+
req,
|
|
504
|
+
corsHeaders,
|
|
505
|
+
processId,
|
|
506
|
+
sessionManager
|
|
507
|
+
);
|
|
508
|
+
} else if (action === "logs" && req.method === "GET") {
|
|
509
|
+
return handleGetProcessLogsRequest(
|
|
510
|
+
req,
|
|
511
|
+
corsHeaders,
|
|
512
|
+
processId,
|
|
513
|
+
sessionManager
|
|
514
|
+
);
|
|
515
|
+
} else if (action === "stream" && req.method === "GET") {
|
|
516
|
+
return handleStreamProcessLogsRequest(
|
|
517
|
+
req,
|
|
518
|
+
corsHeaders,
|
|
519
|
+
processId,
|
|
520
|
+
sessionManager
|
|
521
|
+
);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
// Check if this is a proxy request for an exposed port
|
|
526
|
+
if (pathname.startsWith("/proxy/")) {
|
|
527
|
+
return handleProxyRequest(exposedPorts, req, corsHeaders);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
console.log(`[Container] Route not found: ${pathname}`);
|
|
355
531
|
return new Response("Not Found", {
|
|
356
532
|
headers: corsHeaders,
|
|
357
533
|
status: 404,
|
|
358
534
|
});
|
|
359
535
|
}
|
|
360
536
|
} catch (error) {
|
|
361
|
-
console.error(
|
|
537
|
+
console.error(
|
|
538
|
+
`[Container] Error handling ${req.method} ${pathname}:`,
|
|
539
|
+
error
|
|
540
|
+
);
|
|
362
541
|
return new Response(
|
|
363
542
|
JSON.stringify({
|
|
364
543
|
error: "Internal server error",
|
|
@@ -374,2527 +553,40 @@ const server = serve({
|
|
|
374
553
|
);
|
|
375
554
|
}
|
|
376
555
|
},
|
|
556
|
+
hostname: "0.0.0.0",
|
|
377
557
|
port: 3000,
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
req: Request,
|
|
382
|
-
corsHeaders: Record<string, string>
|
|
383
|
-
): Promise<Response> {
|
|
384
|
-
try {
|
|
385
|
-
const body = (await req.json()) as ExecuteRequest & { sessionId?: string };
|
|
386
|
-
const { command, args = [], sessionId } = body;
|
|
387
|
-
|
|
388
|
-
if (!command || typeof command !== "string") {
|
|
389
|
-
return new Response(
|
|
390
|
-
JSON.stringify({
|
|
391
|
-
error: "Command is required and must be a string",
|
|
392
|
-
}),
|
|
393
|
-
{
|
|
394
|
-
headers: {
|
|
395
|
-
"Content-Type": "application/json",
|
|
396
|
-
...corsHeaders,
|
|
397
|
-
},
|
|
398
|
-
status: 400,
|
|
399
|
-
}
|
|
400
|
-
);
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
// Basic safety check - prevent dangerous commands
|
|
404
|
-
const dangerousCommands = [
|
|
405
|
-
"rm",
|
|
406
|
-
"rmdir",
|
|
407
|
-
"del",
|
|
408
|
-
"format",
|
|
409
|
-
"shutdown",
|
|
410
|
-
"reboot",
|
|
411
|
-
];
|
|
412
|
-
const lowerCommand = command.toLowerCase();
|
|
413
|
-
|
|
414
|
-
if (
|
|
415
|
-
dangerousCommands.some((dangerous) => lowerCommand.includes(dangerous))
|
|
416
|
-
) {
|
|
417
|
-
return new Response(
|
|
418
|
-
JSON.stringify({
|
|
419
|
-
error: "Dangerous command not allowed",
|
|
420
|
-
}),
|
|
421
|
-
{
|
|
422
|
-
headers: {
|
|
423
|
-
"Content-Type": "application/json",
|
|
424
|
-
...corsHeaders,
|
|
425
|
-
},
|
|
426
|
-
status: 400,
|
|
427
|
-
}
|
|
428
|
-
);
|
|
429
|
-
}
|
|
430
|
-
|
|
431
|
-
console.log(`[Server] Executing command: ${command} ${args.join(" ")}`);
|
|
432
|
-
|
|
433
|
-
const result = await executeCommand(command, args, sessionId);
|
|
434
|
-
|
|
435
|
-
return new Response(
|
|
436
|
-
JSON.stringify({
|
|
437
|
-
args,
|
|
438
|
-
command,
|
|
439
|
-
exitCode: result.exitCode,
|
|
440
|
-
stderr: result.stderr,
|
|
441
|
-
stdout: result.stdout,
|
|
442
|
-
success: result.success,
|
|
443
|
-
timestamp: new Date().toISOString(),
|
|
444
|
-
}),
|
|
445
|
-
{
|
|
446
|
-
headers: {
|
|
447
|
-
"Content-Type": "application/json",
|
|
448
|
-
...corsHeaders,
|
|
449
|
-
},
|
|
450
|
-
}
|
|
451
|
-
);
|
|
452
|
-
} catch (error) {
|
|
453
|
-
console.error("[Server] Error in handleExecuteRequest:", error);
|
|
454
|
-
return new Response(
|
|
455
|
-
JSON.stringify({
|
|
456
|
-
error: "Failed to execute command",
|
|
457
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
458
|
-
}),
|
|
459
|
-
{
|
|
460
|
-
headers: {
|
|
461
|
-
"Content-Type": "application/json",
|
|
462
|
-
...corsHeaders,
|
|
463
|
-
},
|
|
464
|
-
status: 500,
|
|
465
|
-
}
|
|
466
|
-
);
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
async function handleStreamingExecuteRequest(
|
|
471
|
-
req: Request,
|
|
472
|
-
corsHeaders: Record<string, string>
|
|
473
|
-
): Promise<Response> {
|
|
474
|
-
try {
|
|
475
|
-
const body = (await req.json()) as ExecuteRequest & { sessionId?: string };
|
|
476
|
-
const { command, args = [], sessionId } = body;
|
|
477
|
-
|
|
478
|
-
if (!command || typeof command !== "string") {
|
|
479
|
-
return new Response(
|
|
480
|
-
JSON.stringify({
|
|
481
|
-
error: "Command is required and must be a string",
|
|
482
|
-
}),
|
|
483
|
-
{
|
|
484
|
-
headers: {
|
|
485
|
-
"Content-Type": "application/json",
|
|
486
|
-
...corsHeaders,
|
|
487
|
-
},
|
|
488
|
-
status: 400,
|
|
489
|
-
}
|
|
490
|
-
);
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
// Basic safety check - prevent dangerous commands
|
|
494
|
-
const dangerousCommands = [
|
|
495
|
-
"rm",
|
|
496
|
-
"rmdir",
|
|
497
|
-
"del",
|
|
498
|
-
"format",
|
|
499
|
-
"shutdown",
|
|
500
|
-
"reboot",
|
|
501
|
-
];
|
|
502
|
-
const lowerCommand = command.toLowerCase();
|
|
503
|
-
|
|
504
|
-
if (
|
|
505
|
-
dangerousCommands.some((dangerous) => lowerCommand.includes(dangerous))
|
|
506
|
-
) {
|
|
507
|
-
return new Response(
|
|
508
|
-
JSON.stringify({
|
|
509
|
-
error: "Dangerous command not allowed",
|
|
510
|
-
}),
|
|
511
|
-
{
|
|
512
|
-
headers: {
|
|
513
|
-
"Content-Type": "application/json",
|
|
514
|
-
...corsHeaders,
|
|
515
|
-
},
|
|
516
|
-
status: 400,
|
|
517
|
-
}
|
|
518
|
-
);
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
console.log(
|
|
522
|
-
`[Server] Executing streaming command: ${command} ${args.join(" ")}`
|
|
523
|
-
);
|
|
524
|
-
|
|
525
|
-
const stream = new ReadableStream({
|
|
526
|
-
start(controller) {
|
|
527
|
-
const child = spawn(command, args, {
|
|
528
|
-
shell: true,
|
|
529
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
530
|
-
});
|
|
531
|
-
|
|
532
|
-
// Store the process reference for cleanup if sessionId is provided
|
|
533
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
534
|
-
const session = sessions.get(sessionId)!;
|
|
535
|
-
session.activeProcess = child;
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
let stdout = "";
|
|
539
|
-
let stderr = "";
|
|
540
|
-
|
|
541
|
-
// Send command start event
|
|
542
|
-
controller.enqueue(
|
|
543
|
-
new TextEncoder().encode(
|
|
544
|
-
`data: ${JSON.stringify({
|
|
545
|
-
args,
|
|
546
|
-
command,
|
|
547
|
-
timestamp: new Date().toISOString(),
|
|
548
|
-
type: "command_start",
|
|
549
|
-
})}\n\n`
|
|
550
|
-
)
|
|
551
|
-
);
|
|
552
|
-
|
|
553
|
-
child.stdout?.on("data", (data) => {
|
|
554
|
-
const output = data.toString();
|
|
555
|
-
stdout += output;
|
|
556
|
-
|
|
557
|
-
// Send real-time output
|
|
558
|
-
controller.enqueue(
|
|
559
|
-
new TextEncoder().encode(
|
|
560
|
-
`data: ${JSON.stringify({
|
|
561
|
-
command,
|
|
562
|
-
data: output,
|
|
563
|
-
stream: "stdout",
|
|
564
|
-
type: "output",
|
|
565
|
-
})}\n\n`
|
|
566
|
-
)
|
|
567
|
-
);
|
|
568
|
-
});
|
|
569
|
-
|
|
570
|
-
child.stderr?.on("data", (data) => {
|
|
571
|
-
const output = data.toString();
|
|
572
|
-
stderr += output;
|
|
573
|
-
|
|
574
|
-
// Send real-time error output
|
|
575
|
-
controller.enqueue(
|
|
576
|
-
new TextEncoder().encode(
|
|
577
|
-
`data: ${JSON.stringify({
|
|
578
|
-
command,
|
|
579
|
-
data: output,
|
|
580
|
-
stream: "stderr",
|
|
581
|
-
type: "output",
|
|
582
|
-
})}\n\n`
|
|
583
|
-
)
|
|
584
|
-
);
|
|
585
|
-
});
|
|
586
|
-
|
|
587
|
-
child.on("close", (code) => {
|
|
588
|
-
// Clear the active process reference
|
|
589
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
590
|
-
const session = sessions.get(sessionId)!;
|
|
591
|
-
session.activeProcess = null;
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
console.log(
|
|
595
|
-
`[Server] Command completed: ${command}, Exit code: ${code}`
|
|
596
|
-
);
|
|
597
|
-
|
|
598
|
-
// Send command completion event
|
|
599
|
-
controller.enqueue(
|
|
600
|
-
new TextEncoder().encode(
|
|
601
|
-
`data: ${JSON.stringify({
|
|
602
|
-
args,
|
|
603
|
-
command,
|
|
604
|
-
exitCode: code,
|
|
605
|
-
stderr,
|
|
606
|
-
stdout,
|
|
607
|
-
success: code === 0,
|
|
608
|
-
timestamp: new Date().toISOString(),
|
|
609
|
-
type: "command_complete",
|
|
610
|
-
})}\n\n`
|
|
611
|
-
)
|
|
612
|
-
);
|
|
613
|
-
|
|
614
|
-
controller.close();
|
|
615
|
-
});
|
|
616
|
-
|
|
617
|
-
child.on("error", (error) => {
|
|
618
|
-
// Clear the active process reference
|
|
619
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
620
|
-
const session = sessions.get(sessionId)!;
|
|
621
|
-
session.activeProcess = null;
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
controller.enqueue(
|
|
625
|
-
new TextEncoder().encode(
|
|
626
|
-
`data: ${JSON.stringify({
|
|
627
|
-
args,
|
|
628
|
-
command,
|
|
629
|
-
error: error.message,
|
|
630
|
-
type: "error",
|
|
631
|
-
})}\n\n`
|
|
632
|
-
)
|
|
633
|
-
);
|
|
634
|
-
|
|
635
|
-
controller.close();
|
|
636
|
-
});
|
|
637
|
-
},
|
|
638
|
-
});
|
|
639
|
-
|
|
640
|
-
return new Response(stream, {
|
|
641
|
-
headers: {
|
|
642
|
-
"Cache-Control": "no-cache",
|
|
643
|
-
Connection: "keep-alive",
|
|
644
|
-
"Content-Type": "text/event-stream",
|
|
645
|
-
...corsHeaders,
|
|
646
|
-
},
|
|
647
|
-
});
|
|
648
|
-
} catch (error) {
|
|
649
|
-
console.error("[Server] Error in handleStreamingExecuteRequest:", error);
|
|
650
|
-
return new Response(
|
|
651
|
-
JSON.stringify({
|
|
652
|
-
error: "Failed to execute streaming command",
|
|
653
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
654
|
-
}),
|
|
655
|
-
{
|
|
656
|
-
headers: {
|
|
657
|
-
"Content-Type": "application/json",
|
|
658
|
-
...corsHeaders,
|
|
659
|
-
},
|
|
660
|
-
status: 500,
|
|
661
|
-
}
|
|
662
|
-
);
|
|
663
|
-
}
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
async function handleGitCheckoutRequest(
|
|
667
|
-
req: Request,
|
|
668
|
-
corsHeaders: Record<string, string>
|
|
669
|
-
): Promise<Response> {
|
|
670
|
-
try {
|
|
671
|
-
const body = (await req.json()) as GitCheckoutRequest;
|
|
672
|
-
const { repoUrl, branch = "main", targetDir, sessionId } = body;
|
|
673
|
-
|
|
674
|
-
if (!repoUrl || typeof repoUrl !== "string") {
|
|
675
|
-
return new Response(
|
|
676
|
-
JSON.stringify({
|
|
677
|
-
error: "Repository URL is required and must be a string",
|
|
678
|
-
}),
|
|
679
|
-
{
|
|
680
|
-
headers: {
|
|
681
|
-
"Content-Type": "application/json",
|
|
682
|
-
...corsHeaders,
|
|
683
|
-
},
|
|
684
|
-
status: 400,
|
|
685
|
-
}
|
|
686
|
-
);
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
// Validate repository URL format
|
|
690
|
-
const urlPattern =
|
|
691
|
-
/^(https?:\/\/|git@|ssh:\/\/).*\.git$|^https?:\/\/.*\/.*$/;
|
|
692
|
-
if (!urlPattern.test(repoUrl)) {
|
|
693
|
-
return new Response(
|
|
694
|
-
JSON.stringify({
|
|
695
|
-
error: "Invalid repository URL format",
|
|
696
|
-
}),
|
|
697
|
-
{
|
|
698
|
-
headers: {
|
|
699
|
-
"Content-Type": "application/json",
|
|
700
|
-
...corsHeaders,
|
|
701
|
-
},
|
|
702
|
-
status: 400,
|
|
703
|
-
}
|
|
704
|
-
);
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
// Generate target directory if not provided
|
|
708
|
-
const checkoutDir =
|
|
709
|
-
targetDir ||
|
|
710
|
-
`repo_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
711
|
-
|
|
712
|
-
console.log(
|
|
713
|
-
`[Server] Checking out repository: ${repoUrl} to ${checkoutDir}`
|
|
714
|
-
);
|
|
715
|
-
|
|
716
|
-
const result = await executeGitCheckout(
|
|
717
|
-
repoUrl,
|
|
718
|
-
branch,
|
|
719
|
-
checkoutDir,
|
|
720
|
-
sessionId
|
|
721
|
-
);
|
|
722
|
-
|
|
723
|
-
return new Response(
|
|
724
|
-
JSON.stringify({
|
|
725
|
-
branch,
|
|
726
|
-
exitCode: result.exitCode,
|
|
727
|
-
repoUrl,
|
|
728
|
-
stderr: result.stderr,
|
|
729
|
-
stdout: result.stdout,
|
|
730
|
-
success: result.success,
|
|
731
|
-
targetDir: checkoutDir,
|
|
732
|
-
timestamp: new Date().toISOString(),
|
|
733
|
-
}),
|
|
734
|
-
{
|
|
735
|
-
headers: {
|
|
736
|
-
"Content-Type": "application/json",
|
|
737
|
-
...corsHeaders,
|
|
738
|
-
},
|
|
739
|
-
}
|
|
740
|
-
);
|
|
741
|
-
} catch (error) {
|
|
742
|
-
console.error("[Server] Error in handleGitCheckoutRequest:", error);
|
|
743
|
-
return new Response(
|
|
744
|
-
JSON.stringify({
|
|
745
|
-
error: "Failed to checkout repository",
|
|
746
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
747
|
-
}),
|
|
748
|
-
{
|
|
749
|
-
headers: {
|
|
750
|
-
"Content-Type": "application/json",
|
|
751
|
-
...corsHeaders,
|
|
752
|
-
},
|
|
753
|
-
status: 500,
|
|
754
|
-
}
|
|
755
|
-
);
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
|
|
759
|
-
async function handleStreamingGitCheckoutRequest(
|
|
760
|
-
req: Request,
|
|
761
|
-
corsHeaders: Record<string, string>
|
|
762
|
-
): Promise<Response> {
|
|
763
|
-
try {
|
|
764
|
-
const body = (await req.json()) as GitCheckoutRequest;
|
|
765
|
-
const { repoUrl, branch = "main", targetDir, sessionId } = body;
|
|
558
|
+
// We don't need this, but typescript complains
|
|
559
|
+
websocket: { async message() {} },
|
|
560
|
+
});
|
|
766
561
|
|
|
767
|
-
|
|
768
|
-
return new Response(
|
|
769
|
-
JSON.stringify({
|
|
770
|
-
error: "Repository URL is required and must be a string",
|
|
771
|
-
}),
|
|
772
|
-
{
|
|
773
|
-
headers: {
|
|
774
|
-
"Content-Type": "application/json",
|
|
775
|
-
...corsHeaders,
|
|
776
|
-
},
|
|
777
|
-
status: 400,
|
|
778
|
-
}
|
|
779
|
-
);
|
|
780
|
-
}
|
|
781
|
-
|
|
782
|
-
// Validate repository URL format
|
|
783
|
-
const urlPattern =
|
|
784
|
-
/^(https?:\/\/|git@|ssh:\/\/).*\.git$|^https?:\/\/.*\/.*$/;
|
|
785
|
-
if (!urlPattern.test(repoUrl)) {
|
|
786
|
-
return new Response(
|
|
787
|
-
JSON.stringify({
|
|
788
|
-
error: "Invalid repository URL format",
|
|
789
|
-
}),
|
|
790
|
-
{
|
|
791
|
-
headers: {
|
|
792
|
-
"Content-Type": "application/json",
|
|
793
|
-
...corsHeaders,
|
|
794
|
-
},
|
|
795
|
-
status: 400,
|
|
796
|
-
}
|
|
797
|
-
);
|
|
798
|
-
}
|
|
799
|
-
|
|
800
|
-
// Generate target directory if not provided
|
|
801
|
-
const checkoutDir =
|
|
802
|
-
targetDir ||
|
|
803
|
-
`repo_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
804
|
-
|
|
805
|
-
console.log(
|
|
806
|
-
`[Server] Checking out repository: ${repoUrl} to ${checkoutDir}`
|
|
807
|
-
);
|
|
808
|
-
|
|
809
|
-
const stream = new ReadableStream({
|
|
810
|
-
start(controller) {
|
|
811
|
-
const child = spawn(
|
|
812
|
-
"git",
|
|
813
|
-
["clone", "-b", branch, repoUrl, checkoutDir],
|
|
814
|
-
{
|
|
815
|
-
shell: true,
|
|
816
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
817
|
-
}
|
|
818
|
-
);
|
|
819
|
-
|
|
820
|
-
// Store the process reference for cleanup if sessionId is provided
|
|
821
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
822
|
-
const session = sessions.get(sessionId)!;
|
|
823
|
-
session.activeProcess = child;
|
|
824
|
-
}
|
|
825
|
-
|
|
826
|
-
let stdout = "";
|
|
827
|
-
let stderr = "";
|
|
828
|
-
|
|
829
|
-
// Send command start event
|
|
830
|
-
controller.enqueue(
|
|
831
|
-
new TextEncoder().encode(
|
|
832
|
-
`data: ${JSON.stringify({
|
|
833
|
-
args: [branch, repoUrl, checkoutDir],
|
|
834
|
-
command: "git clone",
|
|
835
|
-
timestamp: new Date().toISOString(),
|
|
836
|
-
type: "command_start",
|
|
837
|
-
})}\n\n`
|
|
838
|
-
)
|
|
839
|
-
);
|
|
840
|
-
|
|
841
|
-
child.stdout?.on("data", (data) => {
|
|
842
|
-
const output = data.toString();
|
|
843
|
-
stdout += output;
|
|
844
|
-
|
|
845
|
-
// Send real-time output
|
|
846
|
-
controller.enqueue(
|
|
847
|
-
new TextEncoder().encode(
|
|
848
|
-
`data: ${JSON.stringify({
|
|
849
|
-
command: "git clone",
|
|
850
|
-
data: output,
|
|
851
|
-
stream: "stdout",
|
|
852
|
-
type: "output",
|
|
853
|
-
})}\n\n`
|
|
854
|
-
)
|
|
855
|
-
);
|
|
856
|
-
});
|
|
857
|
-
|
|
858
|
-
child.stderr?.on("data", (data) => {
|
|
859
|
-
const output = data.toString();
|
|
860
|
-
stderr += output;
|
|
861
|
-
|
|
862
|
-
// Send real-time error output
|
|
863
|
-
controller.enqueue(
|
|
864
|
-
new TextEncoder().encode(
|
|
865
|
-
`data: ${JSON.stringify({
|
|
866
|
-
command: "git clone",
|
|
867
|
-
data: output,
|
|
868
|
-
stream: "stderr",
|
|
869
|
-
type: "output",
|
|
870
|
-
})}\n\n`
|
|
871
|
-
)
|
|
872
|
-
);
|
|
873
|
-
});
|
|
874
|
-
|
|
875
|
-
child.on("close", (code) => {
|
|
876
|
-
// Clear the active process reference
|
|
877
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
878
|
-
const session = sessions.get(sessionId)!;
|
|
879
|
-
session.activeProcess = null;
|
|
880
|
-
}
|
|
881
|
-
|
|
882
|
-
console.log(
|
|
883
|
-
`[Server] Command completed: git clone, Exit code: ${code}`
|
|
884
|
-
);
|
|
885
|
-
|
|
886
|
-
// Send command completion event
|
|
887
|
-
controller.enqueue(
|
|
888
|
-
new TextEncoder().encode(
|
|
889
|
-
`data: ${JSON.stringify({
|
|
890
|
-
args: [branch, repoUrl, checkoutDir],
|
|
891
|
-
command: "git clone",
|
|
892
|
-
exitCode: code,
|
|
893
|
-
stderr,
|
|
894
|
-
stdout,
|
|
895
|
-
success: code === 0,
|
|
896
|
-
timestamp: new Date().toISOString(),
|
|
897
|
-
type: "command_complete",
|
|
898
|
-
})}\n\n`
|
|
899
|
-
)
|
|
900
|
-
);
|
|
901
|
-
|
|
902
|
-
controller.close();
|
|
903
|
-
});
|
|
904
|
-
|
|
905
|
-
child.on("error", (error) => {
|
|
906
|
-
// Clear the active process reference
|
|
907
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
908
|
-
const session = sessions.get(sessionId)!;
|
|
909
|
-
session.activeProcess = null;
|
|
910
|
-
}
|
|
911
|
-
|
|
912
|
-
controller.enqueue(
|
|
913
|
-
new TextEncoder().encode(
|
|
914
|
-
`data: ${JSON.stringify({
|
|
915
|
-
args: [branch, repoUrl, checkoutDir],
|
|
916
|
-
command: "git clone",
|
|
917
|
-
error: error.message,
|
|
918
|
-
type: "error",
|
|
919
|
-
})}\n\n`
|
|
920
|
-
)
|
|
921
|
-
);
|
|
922
|
-
|
|
923
|
-
controller.close();
|
|
924
|
-
});
|
|
925
|
-
},
|
|
926
|
-
});
|
|
927
|
-
|
|
928
|
-
return new Response(stream, {
|
|
929
|
-
headers: {
|
|
930
|
-
"Cache-Control": "no-cache",
|
|
931
|
-
Connection: "keep-alive",
|
|
932
|
-
"Content-Type": "text/event-stream",
|
|
933
|
-
...corsHeaders,
|
|
934
|
-
},
|
|
935
|
-
});
|
|
936
|
-
} catch (error) {
|
|
937
|
-
console.error(
|
|
938
|
-
"[Server] Error in handleStreamingGitCheckoutRequest:",
|
|
939
|
-
error
|
|
940
|
-
);
|
|
941
|
-
return new Response(
|
|
942
|
-
JSON.stringify({
|
|
943
|
-
error: "Failed to checkout repository",
|
|
944
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
945
|
-
}),
|
|
946
|
-
{
|
|
947
|
-
headers: {
|
|
948
|
-
"Content-Type": "application/json",
|
|
949
|
-
...corsHeaders,
|
|
950
|
-
},
|
|
951
|
-
status: 500,
|
|
952
|
-
}
|
|
953
|
-
);
|
|
954
|
-
}
|
|
955
|
-
}
|
|
956
|
-
|
|
957
|
-
async function handleMkdirRequest(
|
|
958
|
-
req: Request,
|
|
959
|
-
corsHeaders: Record<string, string>
|
|
960
|
-
): Promise<Response> {
|
|
961
|
-
try {
|
|
962
|
-
const body = (await req.json()) as MkdirRequest;
|
|
963
|
-
const { path, recursive = false, sessionId } = body;
|
|
964
|
-
|
|
965
|
-
if (!path || typeof path !== "string") {
|
|
966
|
-
return new Response(
|
|
967
|
-
JSON.stringify({
|
|
968
|
-
error: "Path is required and must be a string",
|
|
969
|
-
}),
|
|
970
|
-
{
|
|
971
|
-
headers: {
|
|
972
|
-
"Content-Type": "application/json",
|
|
973
|
-
...corsHeaders,
|
|
974
|
-
},
|
|
975
|
-
status: 400,
|
|
976
|
-
}
|
|
977
|
-
);
|
|
978
|
-
}
|
|
979
|
-
|
|
980
|
-
// Basic safety check - prevent dangerous paths
|
|
981
|
-
const dangerousPatterns = [
|
|
982
|
-
/^\/$/, // Root directory
|
|
983
|
-
/^\/etc/, // System directories
|
|
984
|
-
/^\/var/, // System directories
|
|
985
|
-
/^\/usr/, // System directories
|
|
986
|
-
/^\/bin/, // System directories
|
|
987
|
-
/^\/sbin/, // System directories
|
|
988
|
-
/^\/boot/, // System directories
|
|
989
|
-
/^\/dev/, // System directories
|
|
990
|
-
/^\/proc/, // System directories
|
|
991
|
-
/^\/sys/, // System directories
|
|
992
|
-
/^\/tmp\/\.\./, // Path traversal attempts
|
|
993
|
-
/\.\./, // Path traversal attempts
|
|
994
|
-
];
|
|
995
|
-
|
|
996
|
-
if (dangerousPatterns.some((pattern) => pattern.test(path))) {
|
|
997
|
-
return new Response(
|
|
998
|
-
JSON.stringify({
|
|
999
|
-
error: "Dangerous path not allowed",
|
|
1000
|
-
}),
|
|
1001
|
-
{
|
|
1002
|
-
headers: {
|
|
1003
|
-
"Content-Type": "application/json",
|
|
1004
|
-
...corsHeaders,
|
|
1005
|
-
},
|
|
1006
|
-
status: 400,
|
|
1007
|
-
}
|
|
1008
|
-
);
|
|
1009
|
-
}
|
|
1010
|
-
|
|
1011
|
-
console.log(
|
|
1012
|
-
`[Server] Creating directory: ${path} (recursive: ${recursive})`
|
|
1013
|
-
);
|
|
1014
|
-
|
|
1015
|
-
const result = await executeMkdir(path, recursive, sessionId);
|
|
1016
|
-
|
|
1017
|
-
return new Response(
|
|
1018
|
-
JSON.stringify({
|
|
1019
|
-
exitCode: result.exitCode,
|
|
1020
|
-
path,
|
|
1021
|
-
recursive,
|
|
1022
|
-
stderr: result.stderr,
|
|
1023
|
-
stdout: result.stdout,
|
|
1024
|
-
success: result.success,
|
|
1025
|
-
timestamp: new Date().toISOString(),
|
|
1026
|
-
}),
|
|
1027
|
-
{
|
|
1028
|
-
headers: {
|
|
1029
|
-
"Content-Type": "application/json",
|
|
1030
|
-
...corsHeaders,
|
|
1031
|
-
},
|
|
1032
|
-
}
|
|
1033
|
-
);
|
|
1034
|
-
} catch (error) {
|
|
1035
|
-
console.error("[Server] Error in handleMkdirRequest:", error);
|
|
1036
|
-
return new Response(
|
|
1037
|
-
JSON.stringify({
|
|
1038
|
-
error: "Failed to create directory",
|
|
1039
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
1040
|
-
}),
|
|
1041
|
-
{
|
|
1042
|
-
headers: {
|
|
1043
|
-
"Content-Type": "application/json",
|
|
1044
|
-
...corsHeaders,
|
|
1045
|
-
},
|
|
1046
|
-
status: 500,
|
|
1047
|
-
}
|
|
1048
|
-
);
|
|
1049
|
-
}
|
|
1050
|
-
}
|
|
1051
|
-
|
|
1052
|
-
async function handleStreamingMkdirRequest(
|
|
1053
|
-
req: Request,
|
|
1054
|
-
corsHeaders: Record<string, string>
|
|
1055
|
-
): Promise<Response> {
|
|
1056
|
-
try {
|
|
1057
|
-
const body = (await req.json()) as MkdirRequest;
|
|
1058
|
-
const { path, recursive = false, sessionId } = body;
|
|
1059
|
-
|
|
1060
|
-
if (!path || typeof path !== "string") {
|
|
1061
|
-
return new Response(
|
|
1062
|
-
JSON.stringify({
|
|
1063
|
-
error: "Path is required and must be a string",
|
|
1064
|
-
}),
|
|
1065
|
-
{
|
|
1066
|
-
headers: {
|
|
1067
|
-
"Content-Type": "application/json",
|
|
1068
|
-
...corsHeaders,
|
|
1069
|
-
},
|
|
1070
|
-
status: 400,
|
|
1071
|
-
}
|
|
1072
|
-
);
|
|
1073
|
-
}
|
|
1074
|
-
|
|
1075
|
-
// Basic safety check - prevent dangerous paths
|
|
1076
|
-
const dangerousPatterns = [
|
|
1077
|
-
/^\/$/, // Root directory
|
|
1078
|
-
/^\/etc/, // System directories
|
|
1079
|
-
/^\/var/, // System directories
|
|
1080
|
-
/^\/usr/, // System directories
|
|
1081
|
-
/^\/bin/, // System directories
|
|
1082
|
-
/^\/sbin/, // System directories
|
|
1083
|
-
/^\/boot/, // System directories
|
|
1084
|
-
/^\/dev/, // System directories
|
|
1085
|
-
/^\/proc/, // System directories
|
|
1086
|
-
/^\/sys/, // System directories
|
|
1087
|
-
/^\/tmp\/\.\./, // Path traversal attempts
|
|
1088
|
-
/\.\./, // Path traversal attempts
|
|
1089
|
-
];
|
|
1090
|
-
|
|
1091
|
-
if (dangerousPatterns.some((pattern) => pattern.test(path))) {
|
|
1092
|
-
return new Response(
|
|
1093
|
-
JSON.stringify({
|
|
1094
|
-
error: "Dangerous path not allowed",
|
|
1095
|
-
}),
|
|
1096
|
-
{
|
|
1097
|
-
headers: {
|
|
1098
|
-
"Content-Type": "application/json",
|
|
1099
|
-
...corsHeaders,
|
|
1100
|
-
},
|
|
1101
|
-
status: 400,
|
|
1102
|
-
}
|
|
1103
|
-
);
|
|
1104
|
-
}
|
|
1105
|
-
|
|
1106
|
-
console.log(
|
|
1107
|
-
`[Server] Creating directory: ${path} (recursive: ${recursive})`
|
|
1108
|
-
);
|
|
1109
|
-
|
|
1110
|
-
const stream = new ReadableStream({
|
|
1111
|
-
start(controller) {
|
|
1112
|
-
const args = recursive ? ["-p", path] : [path];
|
|
1113
|
-
const child = spawn("mkdir", args, {
|
|
1114
|
-
shell: true,
|
|
1115
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
1116
|
-
});
|
|
1117
|
-
|
|
1118
|
-
// Store the process reference for cleanup if sessionId is provided
|
|
1119
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
1120
|
-
const session = sessions.get(sessionId)!;
|
|
1121
|
-
session.activeProcess = child;
|
|
1122
|
-
}
|
|
1123
|
-
|
|
1124
|
-
let stdout = "";
|
|
1125
|
-
let stderr = "";
|
|
1126
|
-
|
|
1127
|
-
// Send command start event
|
|
1128
|
-
controller.enqueue(
|
|
1129
|
-
new TextEncoder().encode(
|
|
1130
|
-
`data: ${JSON.stringify({
|
|
1131
|
-
args,
|
|
1132
|
-
command: "mkdir",
|
|
1133
|
-
timestamp: new Date().toISOString(),
|
|
1134
|
-
type: "command_start",
|
|
1135
|
-
})}\n\n`
|
|
1136
|
-
)
|
|
1137
|
-
);
|
|
1138
|
-
|
|
1139
|
-
child.stdout?.on("data", (data) => {
|
|
1140
|
-
const output = data.toString();
|
|
1141
|
-
stdout += output;
|
|
1142
|
-
|
|
1143
|
-
// Send real-time output
|
|
1144
|
-
controller.enqueue(
|
|
1145
|
-
new TextEncoder().encode(
|
|
1146
|
-
`data: ${JSON.stringify({
|
|
1147
|
-
command: "mkdir",
|
|
1148
|
-
data: output,
|
|
1149
|
-
stream: "stdout",
|
|
1150
|
-
type: "output",
|
|
1151
|
-
})}\n\n`
|
|
1152
|
-
)
|
|
1153
|
-
);
|
|
1154
|
-
});
|
|
1155
|
-
|
|
1156
|
-
child.stderr?.on("data", (data) => {
|
|
1157
|
-
const output = data.toString();
|
|
1158
|
-
stderr += output;
|
|
1159
|
-
|
|
1160
|
-
// Send real-time error output
|
|
1161
|
-
controller.enqueue(
|
|
1162
|
-
new TextEncoder().encode(
|
|
1163
|
-
`data: ${JSON.stringify({
|
|
1164
|
-
command: "mkdir",
|
|
1165
|
-
data: output,
|
|
1166
|
-
stream: "stderr",
|
|
1167
|
-
type: "output",
|
|
1168
|
-
})}\n\n`
|
|
1169
|
-
)
|
|
1170
|
-
);
|
|
1171
|
-
});
|
|
1172
|
-
|
|
1173
|
-
child.on("close", (code) => {
|
|
1174
|
-
// Clear the active process reference
|
|
1175
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
1176
|
-
const session = sessions.get(sessionId)!;
|
|
1177
|
-
session.activeProcess = null;
|
|
1178
|
-
}
|
|
1179
|
-
|
|
1180
|
-
console.log(`[Server] Command completed: mkdir, Exit code: ${code}`);
|
|
1181
|
-
|
|
1182
|
-
// Send command completion event
|
|
1183
|
-
controller.enqueue(
|
|
1184
|
-
new TextEncoder().encode(
|
|
1185
|
-
`data: ${JSON.stringify({
|
|
1186
|
-
args,
|
|
1187
|
-
command: "mkdir",
|
|
1188
|
-
exitCode: code,
|
|
1189
|
-
stderr,
|
|
1190
|
-
stdout,
|
|
1191
|
-
success: code === 0,
|
|
1192
|
-
timestamp: new Date().toISOString(),
|
|
1193
|
-
type: "command_complete",
|
|
1194
|
-
})}\n\n`
|
|
1195
|
-
)
|
|
1196
|
-
);
|
|
1197
|
-
|
|
1198
|
-
controller.close();
|
|
1199
|
-
});
|
|
1200
|
-
|
|
1201
|
-
child.on("error", (error) => {
|
|
1202
|
-
// Clear the active process reference
|
|
1203
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
1204
|
-
const session = sessions.get(sessionId)!;
|
|
1205
|
-
session.activeProcess = null;
|
|
1206
|
-
}
|
|
1207
|
-
|
|
1208
|
-
controller.enqueue(
|
|
1209
|
-
new TextEncoder().encode(
|
|
1210
|
-
`data: ${JSON.stringify({
|
|
1211
|
-
args,
|
|
1212
|
-
command: "mkdir",
|
|
1213
|
-
error: error.message,
|
|
1214
|
-
type: "error",
|
|
1215
|
-
})}\n\n`
|
|
1216
|
-
)
|
|
1217
|
-
);
|
|
1218
|
-
|
|
1219
|
-
controller.close();
|
|
1220
|
-
});
|
|
1221
|
-
},
|
|
1222
|
-
});
|
|
1223
|
-
|
|
1224
|
-
return new Response(stream, {
|
|
1225
|
-
headers: {
|
|
1226
|
-
"Cache-Control": "no-cache",
|
|
1227
|
-
Connection: "keep-alive",
|
|
1228
|
-
"Content-Type": "text/event-stream",
|
|
1229
|
-
...corsHeaders,
|
|
1230
|
-
},
|
|
1231
|
-
});
|
|
1232
|
-
} catch (error) {
|
|
1233
|
-
console.error("[Server] Error in handleStreamingMkdirRequest:", error);
|
|
1234
|
-
return new Response(
|
|
1235
|
-
JSON.stringify({
|
|
1236
|
-
error: "Failed to create directory",
|
|
1237
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
1238
|
-
}),
|
|
1239
|
-
{
|
|
1240
|
-
headers: {
|
|
1241
|
-
"Content-Type": "application/json",
|
|
1242
|
-
...corsHeaders,
|
|
1243
|
-
},
|
|
1244
|
-
status: 500,
|
|
1245
|
-
}
|
|
1246
|
-
);
|
|
1247
|
-
}
|
|
1248
|
-
}
|
|
1249
|
-
|
|
1250
|
-
async function handleWriteFileRequest(
|
|
1251
|
-
req: Request,
|
|
1252
|
-
corsHeaders: Record<string, string>
|
|
1253
|
-
): Promise<Response> {
|
|
1254
|
-
try {
|
|
1255
|
-
const body = (await req.json()) as WriteFileRequest;
|
|
1256
|
-
const { path, content, encoding = "utf-8", sessionId } = body;
|
|
1257
|
-
|
|
1258
|
-
if (!path || typeof path !== "string") {
|
|
1259
|
-
return new Response(
|
|
1260
|
-
JSON.stringify({
|
|
1261
|
-
error: "Path is required and must be a string",
|
|
1262
|
-
}),
|
|
1263
|
-
{
|
|
1264
|
-
headers: {
|
|
1265
|
-
"Content-Type": "application/json",
|
|
1266
|
-
...corsHeaders,
|
|
1267
|
-
},
|
|
1268
|
-
status: 400,
|
|
1269
|
-
}
|
|
1270
|
-
);
|
|
1271
|
-
}
|
|
1272
|
-
|
|
1273
|
-
// Basic safety check - prevent dangerous paths
|
|
1274
|
-
const dangerousPatterns = [
|
|
1275
|
-
/^\/$/, // Root directory
|
|
1276
|
-
/^\/etc/, // System directories
|
|
1277
|
-
/^\/var/, // System directories
|
|
1278
|
-
/^\/usr/, // System directories
|
|
1279
|
-
/^\/bin/, // System directories
|
|
1280
|
-
/^\/sbin/, // System directories
|
|
1281
|
-
/^\/boot/, // System directories
|
|
1282
|
-
/^\/dev/, // System directories
|
|
1283
|
-
/^\/proc/, // System directories
|
|
1284
|
-
/^\/sys/, // System directories
|
|
1285
|
-
/^\/tmp\/\.\./, // Path traversal attempts
|
|
1286
|
-
/\.\./, // Path traversal attempts
|
|
1287
|
-
];
|
|
1288
|
-
|
|
1289
|
-
if (dangerousPatterns.some((pattern) => pattern.test(path))) {
|
|
1290
|
-
return new Response(
|
|
1291
|
-
JSON.stringify({
|
|
1292
|
-
error: "Dangerous path not allowed",
|
|
1293
|
-
}),
|
|
1294
|
-
{
|
|
1295
|
-
headers: {
|
|
1296
|
-
"Content-Type": "application/json",
|
|
1297
|
-
...corsHeaders,
|
|
1298
|
-
},
|
|
1299
|
-
status: 400,
|
|
1300
|
-
}
|
|
1301
|
-
);
|
|
1302
|
-
}
|
|
1303
|
-
|
|
1304
|
-
console.log(
|
|
1305
|
-
`[Server] Writing file: ${path} (content length: ${content.length})`
|
|
1306
|
-
);
|
|
1307
|
-
|
|
1308
|
-
const result = await executeWriteFile(path, content, encoding, sessionId);
|
|
1309
|
-
|
|
1310
|
-
return new Response(
|
|
1311
|
-
JSON.stringify({
|
|
1312
|
-
exitCode: result.exitCode,
|
|
1313
|
-
path,
|
|
1314
|
-
success: result.success,
|
|
1315
|
-
timestamp: new Date().toISOString(),
|
|
1316
|
-
}),
|
|
1317
|
-
{
|
|
1318
|
-
headers: {
|
|
1319
|
-
"Content-Type": "application/json",
|
|
1320
|
-
...corsHeaders,
|
|
1321
|
-
},
|
|
1322
|
-
}
|
|
1323
|
-
);
|
|
1324
|
-
} catch (error) {
|
|
1325
|
-
console.error("[Server] Error in handleWriteFileRequest:", error);
|
|
1326
|
-
return new Response(
|
|
1327
|
-
JSON.stringify({
|
|
1328
|
-
error: "Failed to write file",
|
|
1329
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
1330
|
-
}),
|
|
1331
|
-
{
|
|
1332
|
-
headers: {
|
|
1333
|
-
"Content-Type": "application/json",
|
|
1334
|
-
...corsHeaders,
|
|
1335
|
-
},
|
|
1336
|
-
status: 500,
|
|
1337
|
-
}
|
|
1338
|
-
);
|
|
1339
|
-
}
|
|
1340
|
-
}
|
|
1341
|
-
|
|
1342
|
-
async function handleStreamingWriteFileRequest(
|
|
1343
|
-
req: Request,
|
|
1344
|
-
corsHeaders: Record<string, string>
|
|
1345
|
-
): Promise<Response> {
|
|
1346
|
-
try {
|
|
1347
|
-
const body = (await req.json()) as WriteFileRequest;
|
|
1348
|
-
const { path, content, encoding = "utf-8", sessionId } = body;
|
|
1349
|
-
|
|
1350
|
-
if (!path || typeof path !== "string") {
|
|
1351
|
-
return new Response(
|
|
1352
|
-
JSON.stringify({
|
|
1353
|
-
error: "Path is required and must be a string",
|
|
1354
|
-
}),
|
|
1355
|
-
{
|
|
1356
|
-
headers: {
|
|
1357
|
-
"Content-Type": "application/json",
|
|
1358
|
-
...corsHeaders,
|
|
1359
|
-
},
|
|
1360
|
-
status: 400,
|
|
1361
|
-
}
|
|
1362
|
-
);
|
|
1363
|
-
}
|
|
1364
|
-
|
|
1365
|
-
// Basic safety check - prevent dangerous paths
|
|
1366
|
-
const dangerousPatterns = [
|
|
1367
|
-
/^\/$/, // Root directory
|
|
1368
|
-
/^\/etc/, // System directories
|
|
1369
|
-
/^\/var/, // System directories
|
|
1370
|
-
/^\/usr/, // System directories
|
|
1371
|
-
/^\/bin/, // System directories
|
|
1372
|
-
/^\/sbin/, // System directories
|
|
1373
|
-
/^\/boot/, // System directories
|
|
1374
|
-
/^\/dev/, // System directories
|
|
1375
|
-
/^\/proc/, // System directories
|
|
1376
|
-
/^\/sys/, // System directories
|
|
1377
|
-
/^\/tmp\/\.\./, // Path traversal attempts
|
|
1378
|
-
/\.\./, // Path traversal attempts
|
|
1379
|
-
];
|
|
1380
|
-
|
|
1381
|
-
if (dangerousPatterns.some((pattern) => pattern.test(path))) {
|
|
1382
|
-
return new Response(
|
|
1383
|
-
JSON.stringify({
|
|
1384
|
-
error: "Dangerous path not allowed",
|
|
1385
|
-
}),
|
|
1386
|
-
{
|
|
1387
|
-
headers: {
|
|
1388
|
-
"Content-Type": "application/json",
|
|
1389
|
-
...corsHeaders,
|
|
1390
|
-
},
|
|
1391
|
-
status: 400,
|
|
1392
|
-
}
|
|
1393
|
-
);
|
|
1394
|
-
}
|
|
1395
|
-
|
|
1396
|
-
console.log(
|
|
1397
|
-
`[Server] Writing file (streaming): ${path} (content length: ${content.length})`
|
|
1398
|
-
);
|
|
1399
|
-
|
|
1400
|
-
const stream = new ReadableStream({
|
|
1401
|
-
start(controller) {
|
|
1402
|
-
(async () => {
|
|
1403
|
-
try {
|
|
1404
|
-
// Send command start event
|
|
1405
|
-
controller.enqueue(
|
|
1406
|
-
new TextEncoder().encode(
|
|
1407
|
-
`data: ${JSON.stringify({
|
|
1408
|
-
path,
|
|
1409
|
-
timestamp: new Date().toISOString(),
|
|
1410
|
-
type: "command_start",
|
|
1411
|
-
})}\n\n`
|
|
1412
|
-
)
|
|
1413
|
-
);
|
|
1414
|
-
|
|
1415
|
-
// Ensure the directory exists
|
|
1416
|
-
const dir = dirname(path);
|
|
1417
|
-
if (dir !== ".") {
|
|
1418
|
-
await mkdir(dir, { recursive: true });
|
|
1419
|
-
|
|
1420
|
-
// Send directory creation event
|
|
1421
|
-
controller.enqueue(
|
|
1422
|
-
new TextEncoder().encode(
|
|
1423
|
-
`data: ${JSON.stringify({
|
|
1424
|
-
message: `Created directory: ${dir}`,
|
|
1425
|
-
type: "output",
|
|
1426
|
-
})}\n\n`
|
|
1427
|
-
)
|
|
1428
|
-
);
|
|
1429
|
-
}
|
|
1430
|
-
|
|
1431
|
-
// Write the file
|
|
1432
|
-
await writeFile(path, content, {
|
|
1433
|
-
encoding: encoding as BufferEncoding,
|
|
1434
|
-
});
|
|
1435
|
-
|
|
1436
|
-
console.log(`[Server] File written successfully: ${path}`);
|
|
1437
|
-
|
|
1438
|
-
// Send command completion event
|
|
1439
|
-
controller.enqueue(
|
|
1440
|
-
new TextEncoder().encode(
|
|
1441
|
-
`data: ${JSON.stringify({
|
|
1442
|
-
path,
|
|
1443
|
-
success: true,
|
|
1444
|
-
timestamp: new Date().toISOString(),
|
|
1445
|
-
type: "command_complete",
|
|
1446
|
-
})}\n\n`
|
|
1447
|
-
)
|
|
1448
|
-
);
|
|
1449
|
-
|
|
1450
|
-
controller.close();
|
|
1451
|
-
} catch (error) {
|
|
1452
|
-
console.error(`[Server] Error writing file: ${path}`, error);
|
|
1453
|
-
|
|
1454
|
-
controller.enqueue(
|
|
1455
|
-
new TextEncoder().encode(
|
|
1456
|
-
`data: ${JSON.stringify({
|
|
1457
|
-
error:
|
|
1458
|
-
error instanceof Error ? error.message : "Unknown error",
|
|
1459
|
-
path,
|
|
1460
|
-
type: "error",
|
|
1461
|
-
})}\n\n`
|
|
1462
|
-
)
|
|
1463
|
-
);
|
|
1464
|
-
|
|
1465
|
-
controller.close();
|
|
1466
|
-
}
|
|
1467
|
-
})();
|
|
1468
|
-
},
|
|
1469
|
-
});
|
|
1470
|
-
|
|
1471
|
-
return new Response(stream, {
|
|
1472
|
-
headers: {
|
|
1473
|
-
"Cache-Control": "no-cache",
|
|
1474
|
-
Connection: "keep-alive",
|
|
1475
|
-
"Content-Type": "text/event-stream",
|
|
1476
|
-
...corsHeaders,
|
|
1477
|
-
},
|
|
1478
|
-
});
|
|
1479
|
-
} catch (error) {
|
|
1480
|
-
console.error("[Server] Error in handleStreamingWriteFileRequest:", error);
|
|
1481
|
-
return new Response(
|
|
1482
|
-
JSON.stringify({
|
|
1483
|
-
error: "Failed to write file",
|
|
1484
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
1485
|
-
}),
|
|
1486
|
-
{
|
|
1487
|
-
headers: {
|
|
1488
|
-
"Content-Type": "application/json",
|
|
1489
|
-
...corsHeaders,
|
|
1490
|
-
},
|
|
1491
|
-
status: 500,
|
|
1492
|
-
}
|
|
1493
|
-
);
|
|
1494
|
-
}
|
|
1495
|
-
}
|
|
1496
|
-
|
|
1497
|
-
async function handleReadFileRequest(
|
|
1498
|
-
req: Request,
|
|
1499
|
-
corsHeaders: Record<string, string>
|
|
1500
|
-
): Promise<Response> {
|
|
1501
|
-
try {
|
|
1502
|
-
const body = (await req.json()) as ReadFileRequest;
|
|
1503
|
-
const { path, encoding = "utf-8", sessionId } = body;
|
|
1504
|
-
|
|
1505
|
-
if (!path || typeof path !== "string") {
|
|
1506
|
-
return new Response(
|
|
1507
|
-
JSON.stringify({
|
|
1508
|
-
error: "Path is required and must be a string",
|
|
1509
|
-
}),
|
|
1510
|
-
{
|
|
1511
|
-
headers: {
|
|
1512
|
-
"Content-Type": "application/json",
|
|
1513
|
-
...corsHeaders,
|
|
1514
|
-
},
|
|
1515
|
-
status: 400,
|
|
1516
|
-
}
|
|
1517
|
-
);
|
|
1518
|
-
}
|
|
1519
|
-
|
|
1520
|
-
// Basic safety check - prevent dangerous paths
|
|
1521
|
-
const dangerousPatterns = [
|
|
1522
|
-
/^\/$/, // Root directory
|
|
1523
|
-
/^\/etc/, // System directories
|
|
1524
|
-
/^\/var/, // System directories
|
|
1525
|
-
/^\/usr/, // System directories
|
|
1526
|
-
/^\/bin/, // System directories
|
|
1527
|
-
/^\/sbin/, // System directories
|
|
1528
|
-
/^\/boot/, // System directories
|
|
1529
|
-
/^\/dev/, // System directories
|
|
1530
|
-
/^\/proc/, // System directories
|
|
1531
|
-
/^\/sys/, // System directories
|
|
1532
|
-
/^\/tmp\/\.\./, // Path traversal attempts
|
|
1533
|
-
/\.\./, // Path traversal attempts
|
|
1534
|
-
];
|
|
1535
|
-
|
|
1536
|
-
if (dangerousPatterns.some((pattern) => pattern.test(path))) {
|
|
1537
|
-
return new Response(
|
|
1538
|
-
JSON.stringify({
|
|
1539
|
-
error: "Dangerous path not allowed",
|
|
1540
|
-
}),
|
|
1541
|
-
{
|
|
1542
|
-
headers: {
|
|
1543
|
-
"Content-Type": "application/json",
|
|
1544
|
-
...corsHeaders,
|
|
1545
|
-
},
|
|
1546
|
-
status: 400,
|
|
1547
|
-
}
|
|
1548
|
-
);
|
|
1549
|
-
}
|
|
1550
|
-
|
|
1551
|
-
console.log(`[Server] Reading file: ${path}`);
|
|
1552
|
-
|
|
1553
|
-
const result = await executeReadFile(path, encoding, sessionId);
|
|
1554
|
-
|
|
1555
|
-
return new Response(
|
|
1556
|
-
JSON.stringify({
|
|
1557
|
-
content: result.content,
|
|
1558
|
-
exitCode: result.exitCode,
|
|
1559
|
-
path,
|
|
1560
|
-
success: result.success,
|
|
1561
|
-
timestamp: new Date().toISOString(),
|
|
1562
|
-
}),
|
|
1563
|
-
{
|
|
1564
|
-
headers: {
|
|
1565
|
-
"Content-Type": "application/json",
|
|
1566
|
-
...corsHeaders,
|
|
1567
|
-
},
|
|
1568
|
-
}
|
|
1569
|
-
);
|
|
1570
|
-
} catch (error) {
|
|
1571
|
-
console.error("[Server] Error in handleReadFileRequest:", error);
|
|
1572
|
-
return new Response(
|
|
1573
|
-
JSON.stringify({
|
|
1574
|
-
error: "Failed to read file",
|
|
1575
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
1576
|
-
}),
|
|
1577
|
-
{
|
|
1578
|
-
headers: {
|
|
1579
|
-
"Content-Type": "application/json",
|
|
1580
|
-
...corsHeaders,
|
|
1581
|
-
},
|
|
1582
|
-
status: 500,
|
|
1583
|
-
}
|
|
1584
|
-
);
|
|
1585
|
-
}
|
|
1586
|
-
}
|
|
1587
|
-
|
|
1588
|
-
async function handleStreamingReadFileRequest(
|
|
1589
|
-
req: Request,
|
|
1590
|
-
corsHeaders: Record<string, string>
|
|
1591
|
-
): Promise<Response> {
|
|
1592
|
-
try {
|
|
1593
|
-
const body = (await req.json()) as ReadFileRequest;
|
|
1594
|
-
const { path, encoding = "utf-8", sessionId } = body;
|
|
1595
|
-
|
|
1596
|
-
if (!path || typeof path !== "string") {
|
|
1597
|
-
return new Response(
|
|
1598
|
-
JSON.stringify({
|
|
1599
|
-
error: "Path is required and must be a string",
|
|
1600
|
-
}),
|
|
1601
|
-
{
|
|
1602
|
-
headers: {
|
|
1603
|
-
"Content-Type": "application/json",
|
|
1604
|
-
...corsHeaders,
|
|
1605
|
-
},
|
|
1606
|
-
status: 400,
|
|
1607
|
-
}
|
|
1608
|
-
);
|
|
1609
|
-
}
|
|
1610
|
-
|
|
1611
|
-
// Basic safety check - prevent dangerous paths
|
|
1612
|
-
const dangerousPatterns = [
|
|
1613
|
-
/^\/$/, // Root directory
|
|
1614
|
-
/^\/etc/, // System directories
|
|
1615
|
-
/^\/var/, // System directories
|
|
1616
|
-
/^\/usr/, // System directories
|
|
1617
|
-
/^\/bin/, // System directories
|
|
1618
|
-
/^\/sbin/, // System directories
|
|
1619
|
-
/^\/boot/, // System directories
|
|
1620
|
-
/^\/dev/, // System directories
|
|
1621
|
-
/^\/proc/, // System directories
|
|
1622
|
-
/^\/sys/, // System directories
|
|
1623
|
-
/^\/tmp\/\.\./, // Path traversal attempts
|
|
1624
|
-
/\.\./, // Path traversal attempts
|
|
1625
|
-
];
|
|
1626
|
-
|
|
1627
|
-
if (dangerousPatterns.some((pattern) => pattern.test(path))) {
|
|
1628
|
-
return new Response(
|
|
1629
|
-
JSON.stringify({
|
|
1630
|
-
error: "Dangerous path not allowed",
|
|
1631
|
-
}),
|
|
1632
|
-
{
|
|
1633
|
-
headers: {
|
|
1634
|
-
"Content-Type": "application/json",
|
|
1635
|
-
...corsHeaders,
|
|
1636
|
-
},
|
|
1637
|
-
status: 400,
|
|
1638
|
-
}
|
|
1639
|
-
);
|
|
1640
|
-
}
|
|
1641
|
-
|
|
1642
|
-
console.log(`[Server] Reading file (streaming): ${path}`);
|
|
1643
|
-
|
|
1644
|
-
const stream = new ReadableStream({
|
|
1645
|
-
start(controller) {
|
|
1646
|
-
(async () => {
|
|
1647
|
-
try {
|
|
1648
|
-
// Send command start event
|
|
1649
|
-
controller.enqueue(
|
|
1650
|
-
new TextEncoder().encode(
|
|
1651
|
-
`data: ${JSON.stringify({
|
|
1652
|
-
path,
|
|
1653
|
-
timestamp: new Date().toISOString(),
|
|
1654
|
-
type: "command_start",
|
|
1655
|
-
})}\n\n`
|
|
1656
|
-
)
|
|
1657
|
-
);
|
|
1658
|
-
|
|
1659
|
-
// Read the file
|
|
1660
|
-
const content = await readFile(path, {
|
|
1661
|
-
encoding: encoding as BufferEncoding,
|
|
1662
|
-
});
|
|
1663
|
-
|
|
1664
|
-
console.log(`[Server] File read successfully: ${path}`);
|
|
1665
|
-
|
|
1666
|
-
// Send command completion event
|
|
1667
|
-
controller.enqueue(
|
|
1668
|
-
new TextEncoder().encode(
|
|
1669
|
-
`data: ${JSON.stringify({
|
|
1670
|
-
content,
|
|
1671
|
-
path,
|
|
1672
|
-
success: true,
|
|
1673
|
-
timestamp: new Date().toISOString(),
|
|
1674
|
-
type: "command_complete",
|
|
1675
|
-
})}\n\n`
|
|
1676
|
-
)
|
|
1677
|
-
);
|
|
1678
|
-
|
|
1679
|
-
controller.close();
|
|
1680
|
-
} catch (error) {
|
|
1681
|
-
console.error(`[Server] Error reading file: ${path}`, error);
|
|
1682
|
-
|
|
1683
|
-
controller.enqueue(
|
|
1684
|
-
new TextEncoder().encode(
|
|
1685
|
-
`data: ${JSON.stringify({
|
|
1686
|
-
error:
|
|
1687
|
-
error instanceof Error ? error.message : "Unknown error",
|
|
1688
|
-
path,
|
|
1689
|
-
type: "error",
|
|
1690
|
-
})}\n\n`
|
|
1691
|
-
)
|
|
1692
|
-
);
|
|
1693
|
-
|
|
1694
|
-
controller.close();
|
|
1695
|
-
}
|
|
1696
|
-
})();
|
|
1697
|
-
},
|
|
1698
|
-
});
|
|
1699
|
-
|
|
1700
|
-
return new Response(stream, {
|
|
1701
|
-
headers: {
|
|
1702
|
-
"Cache-Control": "no-cache",
|
|
1703
|
-
Connection: "keep-alive",
|
|
1704
|
-
"Content-Type": "text/event-stream",
|
|
1705
|
-
...corsHeaders,
|
|
1706
|
-
},
|
|
1707
|
-
});
|
|
1708
|
-
} catch (error) {
|
|
1709
|
-
console.error("[Server] Error in handleStreamingReadFileRequest:", error);
|
|
1710
|
-
return new Response(
|
|
1711
|
-
JSON.stringify({
|
|
1712
|
-
error: "Failed to read file",
|
|
1713
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
1714
|
-
}),
|
|
1715
|
-
{
|
|
1716
|
-
headers: {
|
|
1717
|
-
"Content-Type": "application/json",
|
|
1718
|
-
...corsHeaders,
|
|
1719
|
-
},
|
|
1720
|
-
status: 500,
|
|
1721
|
-
}
|
|
1722
|
-
);
|
|
1723
|
-
}
|
|
1724
|
-
}
|
|
1725
|
-
|
|
1726
|
-
async function handleDeleteFileRequest(
|
|
1727
|
-
req: Request,
|
|
1728
|
-
corsHeaders: Record<string, string>
|
|
1729
|
-
): Promise<Response> {
|
|
1730
|
-
try {
|
|
1731
|
-
const body = (await req.json()) as DeleteFileRequest;
|
|
1732
|
-
const { path, sessionId } = body;
|
|
1733
|
-
|
|
1734
|
-
if (!path || typeof path !== "string") {
|
|
1735
|
-
return new Response(
|
|
1736
|
-
JSON.stringify({
|
|
1737
|
-
error: "Path is required and must be a string",
|
|
1738
|
-
}),
|
|
1739
|
-
{
|
|
1740
|
-
headers: {
|
|
1741
|
-
"Content-Type": "application/json",
|
|
1742
|
-
...corsHeaders,
|
|
1743
|
-
},
|
|
1744
|
-
status: 400,
|
|
1745
|
-
}
|
|
1746
|
-
);
|
|
1747
|
-
}
|
|
1748
|
-
|
|
1749
|
-
// Basic safety check - prevent dangerous paths
|
|
1750
|
-
const dangerousPatterns = [
|
|
1751
|
-
/^\/$/, // Root directory
|
|
1752
|
-
/^\/etc/, // System directories
|
|
1753
|
-
/^\/var/, // System directories
|
|
1754
|
-
/^\/usr/, // System directories
|
|
1755
|
-
/^\/bin/, // System directories
|
|
1756
|
-
/^\/sbin/, // System directories
|
|
1757
|
-
/^\/boot/, // System directories
|
|
1758
|
-
/^\/dev/, // System directories
|
|
1759
|
-
/^\/proc/, // System directories
|
|
1760
|
-
/^\/sys/, // System directories
|
|
1761
|
-
/^\/tmp\/\.\./, // Path traversal attempts
|
|
1762
|
-
/\.\./, // Path traversal attempts
|
|
1763
|
-
];
|
|
1764
|
-
|
|
1765
|
-
if (dangerousPatterns.some((pattern) => pattern.test(path))) {
|
|
1766
|
-
return new Response(
|
|
1767
|
-
JSON.stringify({
|
|
1768
|
-
error: "Dangerous path not allowed",
|
|
1769
|
-
}),
|
|
1770
|
-
{
|
|
1771
|
-
headers: {
|
|
1772
|
-
"Content-Type": "application/json",
|
|
1773
|
-
...corsHeaders,
|
|
1774
|
-
},
|
|
1775
|
-
status: 400,
|
|
1776
|
-
}
|
|
1777
|
-
);
|
|
1778
|
-
}
|
|
1779
|
-
|
|
1780
|
-
console.log(`[Server] Deleting file: ${path}`);
|
|
1781
|
-
|
|
1782
|
-
const result = await executeDeleteFile(path, sessionId);
|
|
1783
|
-
|
|
1784
|
-
return new Response(
|
|
1785
|
-
JSON.stringify({
|
|
1786
|
-
exitCode: result.exitCode,
|
|
1787
|
-
path,
|
|
1788
|
-
success: result.success,
|
|
1789
|
-
timestamp: new Date().toISOString(),
|
|
1790
|
-
}),
|
|
1791
|
-
{
|
|
1792
|
-
headers: {
|
|
1793
|
-
"Content-Type": "application/json",
|
|
1794
|
-
...corsHeaders,
|
|
1795
|
-
},
|
|
1796
|
-
}
|
|
1797
|
-
);
|
|
1798
|
-
} catch (error) {
|
|
1799
|
-
console.error("[Server] Error in handleDeleteFileRequest:", error);
|
|
1800
|
-
return new Response(
|
|
1801
|
-
JSON.stringify({
|
|
1802
|
-
error: "Failed to delete file",
|
|
1803
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
1804
|
-
}),
|
|
1805
|
-
{
|
|
1806
|
-
headers: {
|
|
1807
|
-
"Content-Type": "application/json",
|
|
1808
|
-
...corsHeaders,
|
|
1809
|
-
},
|
|
1810
|
-
status: 500,
|
|
1811
|
-
}
|
|
1812
|
-
);
|
|
1813
|
-
}
|
|
1814
|
-
}
|
|
1815
|
-
|
|
1816
|
-
async function handleStreamingDeleteFileRequest(
|
|
1817
|
-
req: Request,
|
|
1818
|
-
corsHeaders: Record<string, string>
|
|
1819
|
-
): Promise<Response> {
|
|
1820
|
-
try {
|
|
1821
|
-
const body = (await req.json()) as DeleteFileRequest;
|
|
1822
|
-
const { path, sessionId } = body;
|
|
1823
|
-
|
|
1824
|
-
if (!path || typeof path !== "string") {
|
|
1825
|
-
return new Response(
|
|
1826
|
-
JSON.stringify({
|
|
1827
|
-
error: "Path is required and must be a string",
|
|
1828
|
-
}),
|
|
1829
|
-
{
|
|
1830
|
-
headers: {
|
|
1831
|
-
"Content-Type": "application/json",
|
|
1832
|
-
...corsHeaders,
|
|
1833
|
-
},
|
|
1834
|
-
status: 400,
|
|
1835
|
-
}
|
|
1836
|
-
);
|
|
1837
|
-
}
|
|
1838
|
-
|
|
1839
|
-
// Basic safety check - prevent dangerous paths
|
|
1840
|
-
const dangerousPatterns = [
|
|
1841
|
-
/^\/$/, // Root directory
|
|
1842
|
-
/^\/etc/, // System directories
|
|
1843
|
-
/^\/var/, // System directories
|
|
1844
|
-
/^\/usr/, // System directories
|
|
1845
|
-
/^\/bin/, // System directories
|
|
1846
|
-
/^\/sbin/, // System directories
|
|
1847
|
-
/^\/boot/, // System directories
|
|
1848
|
-
/^\/dev/, // System directories
|
|
1849
|
-
/^\/proc/, // System directories
|
|
1850
|
-
/^\/sys/, // System directories
|
|
1851
|
-
/^\/tmp\/\.\./, // Path traversal attempts
|
|
1852
|
-
/\.\./, // Path traversal attempts
|
|
1853
|
-
];
|
|
1854
|
-
|
|
1855
|
-
if (dangerousPatterns.some((pattern) => pattern.test(path))) {
|
|
1856
|
-
return new Response(
|
|
1857
|
-
JSON.stringify({
|
|
1858
|
-
error: "Dangerous path not allowed",
|
|
1859
|
-
}),
|
|
1860
|
-
{
|
|
1861
|
-
headers: {
|
|
1862
|
-
"Content-Type": "application/json",
|
|
1863
|
-
...corsHeaders,
|
|
1864
|
-
},
|
|
1865
|
-
status: 400,
|
|
1866
|
-
}
|
|
1867
|
-
);
|
|
1868
|
-
}
|
|
1869
|
-
|
|
1870
|
-
console.log(`[Server] Deleting file (streaming): ${path}`);
|
|
1871
|
-
|
|
1872
|
-
const stream = new ReadableStream({
|
|
1873
|
-
start(controller) {
|
|
1874
|
-
(async () => {
|
|
1875
|
-
try {
|
|
1876
|
-
// Send command start event
|
|
1877
|
-
controller.enqueue(
|
|
1878
|
-
new TextEncoder().encode(
|
|
1879
|
-
`data: ${JSON.stringify({
|
|
1880
|
-
path,
|
|
1881
|
-
timestamp: new Date().toISOString(),
|
|
1882
|
-
type: "command_start",
|
|
1883
|
-
})}\n\n`
|
|
1884
|
-
)
|
|
1885
|
-
);
|
|
1886
|
-
|
|
1887
|
-
// Delete the file
|
|
1888
|
-
await executeDeleteFile(path, sessionId);
|
|
1889
|
-
|
|
1890
|
-
console.log(`[Server] File deleted successfully: ${path}`);
|
|
1891
|
-
|
|
1892
|
-
// Send command completion event
|
|
1893
|
-
controller.enqueue(
|
|
1894
|
-
new TextEncoder().encode(
|
|
1895
|
-
`data: ${JSON.stringify({
|
|
1896
|
-
path,
|
|
1897
|
-
success: true,
|
|
1898
|
-
timestamp: new Date().toISOString(),
|
|
1899
|
-
type: "command_complete",
|
|
1900
|
-
})}\n\n`
|
|
1901
|
-
)
|
|
1902
|
-
);
|
|
1903
|
-
|
|
1904
|
-
controller.close();
|
|
1905
|
-
} catch (error) {
|
|
1906
|
-
console.error(`[Server] Error deleting file: ${path}`, error);
|
|
1907
|
-
|
|
1908
|
-
controller.enqueue(
|
|
1909
|
-
new TextEncoder().encode(
|
|
1910
|
-
`data: ${JSON.stringify({
|
|
1911
|
-
error:
|
|
1912
|
-
error instanceof Error ? error.message : "Unknown error",
|
|
1913
|
-
path,
|
|
1914
|
-
type: "error",
|
|
1915
|
-
})}\n\n`
|
|
1916
|
-
)
|
|
1917
|
-
);
|
|
1918
|
-
|
|
1919
|
-
controller.close();
|
|
1920
|
-
}
|
|
1921
|
-
})();
|
|
1922
|
-
},
|
|
1923
|
-
});
|
|
1924
|
-
|
|
1925
|
-
return new Response(stream, {
|
|
1926
|
-
headers: {
|
|
1927
|
-
"Cache-Control": "no-cache",
|
|
1928
|
-
Connection: "keep-alive",
|
|
1929
|
-
"Content-Type": "text/event-stream",
|
|
1930
|
-
...corsHeaders,
|
|
1931
|
-
},
|
|
1932
|
-
});
|
|
1933
|
-
} catch (error) {
|
|
1934
|
-
console.error("[Server] Error in handleStreamingDeleteFileRequest:", error);
|
|
1935
|
-
return new Response(
|
|
1936
|
-
JSON.stringify({
|
|
1937
|
-
error: "Failed to delete file",
|
|
1938
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
1939
|
-
}),
|
|
1940
|
-
{
|
|
1941
|
-
headers: {
|
|
1942
|
-
"Content-Type": "application/json",
|
|
1943
|
-
...corsHeaders,
|
|
1944
|
-
},
|
|
1945
|
-
status: 500,
|
|
1946
|
-
}
|
|
1947
|
-
);
|
|
1948
|
-
}
|
|
1949
|
-
}
|
|
1950
|
-
|
|
1951
|
-
async function handleRenameFileRequest(
|
|
1952
|
-
req: Request,
|
|
1953
|
-
corsHeaders: Record<string, string>
|
|
1954
|
-
): Promise<Response> {
|
|
1955
|
-
try {
|
|
1956
|
-
const body = (await req.json()) as RenameFileRequest;
|
|
1957
|
-
const { oldPath, newPath, sessionId } = body;
|
|
1958
|
-
|
|
1959
|
-
if (!oldPath || typeof oldPath !== "string") {
|
|
1960
|
-
return new Response(
|
|
1961
|
-
JSON.stringify({
|
|
1962
|
-
error: "Old path is required and must be a string",
|
|
1963
|
-
}),
|
|
1964
|
-
{
|
|
1965
|
-
headers: {
|
|
1966
|
-
"Content-Type": "application/json",
|
|
1967
|
-
...corsHeaders,
|
|
1968
|
-
},
|
|
1969
|
-
status: 400,
|
|
1970
|
-
}
|
|
1971
|
-
);
|
|
1972
|
-
}
|
|
1973
|
-
|
|
1974
|
-
if (!newPath || typeof newPath !== "string") {
|
|
1975
|
-
return new Response(
|
|
1976
|
-
JSON.stringify({
|
|
1977
|
-
error: "New path is required and must be a string",
|
|
1978
|
-
}),
|
|
1979
|
-
{
|
|
1980
|
-
headers: {
|
|
1981
|
-
"Content-Type": "application/json",
|
|
1982
|
-
...corsHeaders,
|
|
1983
|
-
},
|
|
1984
|
-
status: 400,
|
|
1985
|
-
}
|
|
1986
|
-
);
|
|
1987
|
-
}
|
|
1988
|
-
|
|
1989
|
-
// Basic safety check - prevent dangerous paths
|
|
1990
|
-
const dangerousPatterns = [
|
|
1991
|
-
/^\/$/, // Root directory
|
|
1992
|
-
/^\/etc/, // System directories
|
|
1993
|
-
/^\/var/, // System directories
|
|
1994
|
-
/^\/usr/, // System directories
|
|
1995
|
-
/^\/bin/, // System directories
|
|
1996
|
-
/^\/sbin/, // System directories
|
|
1997
|
-
/^\/boot/, // System directories
|
|
1998
|
-
/^\/dev/, // System directories
|
|
1999
|
-
/^\/proc/, // System directories
|
|
2000
|
-
/^\/sys/, // System directories
|
|
2001
|
-
/^\/tmp\/\.\./, // Path traversal attempts
|
|
2002
|
-
/\.\./, // Path traversal attempts
|
|
2003
|
-
];
|
|
2004
|
-
|
|
2005
|
-
if (
|
|
2006
|
-
dangerousPatterns.some(
|
|
2007
|
-
(pattern) => pattern.test(oldPath) || pattern.test(newPath)
|
|
2008
|
-
)
|
|
2009
|
-
) {
|
|
2010
|
-
return new Response(
|
|
2011
|
-
JSON.stringify({
|
|
2012
|
-
error: "Dangerous path not allowed",
|
|
2013
|
-
}),
|
|
2014
|
-
{
|
|
2015
|
-
headers: {
|
|
2016
|
-
"Content-Type": "application/json",
|
|
2017
|
-
...corsHeaders,
|
|
2018
|
-
},
|
|
2019
|
-
status: 400,
|
|
2020
|
-
}
|
|
2021
|
-
);
|
|
2022
|
-
}
|
|
2023
|
-
|
|
2024
|
-
console.log(`[Server] Renaming file: ${oldPath} -> ${newPath}`);
|
|
2025
|
-
|
|
2026
|
-
const result = await executeRenameFile(oldPath, newPath, sessionId);
|
|
2027
|
-
|
|
2028
|
-
return new Response(
|
|
2029
|
-
JSON.stringify({
|
|
2030
|
-
exitCode: result.exitCode,
|
|
2031
|
-
newPath,
|
|
2032
|
-
oldPath,
|
|
2033
|
-
success: result.success,
|
|
2034
|
-
timestamp: new Date().toISOString(),
|
|
2035
|
-
}),
|
|
2036
|
-
{
|
|
2037
|
-
headers: {
|
|
2038
|
-
"Content-Type": "application/json",
|
|
2039
|
-
...corsHeaders,
|
|
2040
|
-
},
|
|
2041
|
-
}
|
|
2042
|
-
);
|
|
2043
|
-
} catch (error) {
|
|
2044
|
-
console.error("[Server] Error in handleRenameFileRequest:", error);
|
|
2045
|
-
return new Response(
|
|
2046
|
-
JSON.stringify({
|
|
2047
|
-
error: "Failed to rename file",
|
|
2048
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
2049
|
-
}),
|
|
2050
|
-
{
|
|
2051
|
-
headers: {
|
|
2052
|
-
"Content-Type": "application/json",
|
|
2053
|
-
...corsHeaders,
|
|
2054
|
-
},
|
|
2055
|
-
status: 500,
|
|
2056
|
-
}
|
|
2057
|
-
);
|
|
2058
|
-
}
|
|
2059
|
-
}
|
|
2060
|
-
|
|
2061
|
-
async function handleStreamingRenameFileRequest(
|
|
2062
|
-
req: Request,
|
|
2063
|
-
corsHeaders: Record<string, string>
|
|
2064
|
-
): Promise<Response> {
|
|
2065
|
-
try {
|
|
2066
|
-
const body = (await req.json()) as RenameFileRequest;
|
|
2067
|
-
const { oldPath, newPath, sessionId } = body;
|
|
2068
|
-
|
|
2069
|
-
if (!oldPath || typeof oldPath !== "string") {
|
|
2070
|
-
return new Response(
|
|
2071
|
-
JSON.stringify({
|
|
2072
|
-
error: "Old path is required and must be a string",
|
|
2073
|
-
}),
|
|
2074
|
-
{
|
|
2075
|
-
headers: {
|
|
2076
|
-
"Content-Type": "application/json",
|
|
2077
|
-
...corsHeaders,
|
|
2078
|
-
},
|
|
2079
|
-
status: 400,
|
|
2080
|
-
}
|
|
2081
|
-
);
|
|
2082
|
-
}
|
|
2083
|
-
|
|
2084
|
-
if (!newPath || typeof newPath !== "string") {
|
|
2085
|
-
return new Response(
|
|
2086
|
-
JSON.stringify({
|
|
2087
|
-
error: "New path is required and must be a string",
|
|
2088
|
-
}),
|
|
2089
|
-
{
|
|
2090
|
-
headers: {
|
|
2091
|
-
"Content-Type": "application/json",
|
|
2092
|
-
...corsHeaders,
|
|
2093
|
-
},
|
|
2094
|
-
status: 400,
|
|
2095
|
-
}
|
|
2096
|
-
);
|
|
2097
|
-
}
|
|
2098
|
-
|
|
2099
|
-
// Basic safety check - prevent dangerous paths
|
|
2100
|
-
const dangerousPatterns = [
|
|
2101
|
-
/^\/$/, // Root directory
|
|
2102
|
-
/^\/etc/, // System directories
|
|
2103
|
-
/^\/var/, // System directories
|
|
2104
|
-
/^\/usr/, // System directories
|
|
2105
|
-
/^\/bin/, // System directories
|
|
2106
|
-
/^\/sbin/, // System directories
|
|
2107
|
-
/^\/boot/, // System directories
|
|
2108
|
-
/^\/dev/, // System directories
|
|
2109
|
-
/^\/proc/, // System directories
|
|
2110
|
-
/^\/sys/, // System directories
|
|
2111
|
-
/^\/tmp\/\.\./, // Path traversal attempts
|
|
2112
|
-
/\.\./, // Path traversal attempts
|
|
2113
|
-
];
|
|
2114
|
-
|
|
2115
|
-
if (
|
|
2116
|
-
dangerousPatterns.some(
|
|
2117
|
-
(pattern) => pattern.test(oldPath) || pattern.test(newPath)
|
|
2118
|
-
)
|
|
2119
|
-
) {
|
|
2120
|
-
return new Response(
|
|
2121
|
-
JSON.stringify({
|
|
2122
|
-
error: "Dangerous path not allowed",
|
|
2123
|
-
}),
|
|
2124
|
-
{
|
|
2125
|
-
headers: {
|
|
2126
|
-
"Content-Type": "application/json",
|
|
2127
|
-
...corsHeaders,
|
|
2128
|
-
},
|
|
2129
|
-
status: 400,
|
|
2130
|
-
}
|
|
2131
|
-
);
|
|
2132
|
-
}
|
|
2133
|
-
|
|
2134
|
-
console.log(`[Server] Renaming file (streaming): ${oldPath} -> ${newPath}`);
|
|
2135
|
-
|
|
2136
|
-
const stream = new ReadableStream({
|
|
2137
|
-
start(controller) {
|
|
2138
|
-
(async () => {
|
|
2139
|
-
try {
|
|
2140
|
-
// Send command start event
|
|
2141
|
-
controller.enqueue(
|
|
2142
|
-
new TextEncoder().encode(
|
|
2143
|
-
`data: ${JSON.stringify({
|
|
2144
|
-
newPath,
|
|
2145
|
-
oldPath,
|
|
2146
|
-
timestamp: new Date().toISOString(),
|
|
2147
|
-
type: "command_start",
|
|
2148
|
-
})}\n\n`
|
|
2149
|
-
)
|
|
2150
|
-
);
|
|
2151
|
-
|
|
2152
|
-
// Rename the file
|
|
2153
|
-
await executeRenameFile(oldPath, newPath, sessionId);
|
|
2154
|
-
|
|
2155
|
-
console.log(
|
|
2156
|
-
`[Server] File renamed successfully: ${oldPath} -> ${newPath}`
|
|
2157
|
-
);
|
|
2158
|
-
|
|
2159
|
-
// Send command completion event
|
|
2160
|
-
controller.enqueue(
|
|
2161
|
-
new TextEncoder().encode(
|
|
2162
|
-
`data: ${JSON.stringify({
|
|
2163
|
-
newPath,
|
|
2164
|
-
oldPath,
|
|
2165
|
-
success: true,
|
|
2166
|
-
timestamp: new Date().toISOString(),
|
|
2167
|
-
type: "command_complete",
|
|
2168
|
-
})}\n\n`
|
|
2169
|
-
)
|
|
2170
|
-
);
|
|
2171
|
-
|
|
2172
|
-
controller.close();
|
|
2173
|
-
} catch (error) {
|
|
2174
|
-
console.error(
|
|
2175
|
-
`[Server] Error renaming file: ${oldPath} -> ${newPath}`,
|
|
2176
|
-
error
|
|
2177
|
-
);
|
|
2178
|
-
|
|
2179
|
-
controller.enqueue(
|
|
2180
|
-
new TextEncoder().encode(
|
|
2181
|
-
`data: ${JSON.stringify({
|
|
2182
|
-
error:
|
|
2183
|
-
error instanceof Error ? error.message : "Unknown error",
|
|
2184
|
-
newPath,
|
|
2185
|
-
oldPath,
|
|
2186
|
-
type: "error",
|
|
2187
|
-
})}\n\n`
|
|
2188
|
-
)
|
|
2189
|
-
);
|
|
2190
|
-
|
|
2191
|
-
controller.close();
|
|
2192
|
-
}
|
|
2193
|
-
})();
|
|
2194
|
-
},
|
|
2195
|
-
});
|
|
2196
|
-
|
|
2197
|
-
return new Response(stream, {
|
|
2198
|
-
headers: {
|
|
2199
|
-
"Cache-Control": "no-cache",
|
|
2200
|
-
Connection: "keep-alive",
|
|
2201
|
-
"Content-Type": "text/event-stream",
|
|
2202
|
-
...corsHeaders,
|
|
2203
|
-
},
|
|
2204
|
-
});
|
|
2205
|
-
} catch (error) {
|
|
2206
|
-
console.error("[Server] Error in handleStreamingRenameFileRequest:", error);
|
|
2207
|
-
return new Response(
|
|
2208
|
-
JSON.stringify({
|
|
2209
|
-
error: "Failed to rename file",
|
|
2210
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
2211
|
-
}),
|
|
2212
|
-
{
|
|
2213
|
-
headers: {
|
|
2214
|
-
"Content-Type": "application/json",
|
|
2215
|
-
...corsHeaders,
|
|
2216
|
-
},
|
|
2217
|
-
status: 500,
|
|
2218
|
-
}
|
|
2219
|
-
);
|
|
2220
|
-
}
|
|
2221
|
-
}
|
|
2222
|
-
|
|
2223
|
-
async function handleMoveFileRequest(
|
|
2224
|
-
req: Request,
|
|
2225
|
-
corsHeaders: Record<string, string>
|
|
2226
|
-
): Promise<Response> {
|
|
2227
|
-
try {
|
|
2228
|
-
const body = (await req.json()) as MoveFileRequest;
|
|
2229
|
-
const { sourcePath, destinationPath, sessionId } = body;
|
|
2230
|
-
|
|
2231
|
-
if (!sourcePath || typeof sourcePath !== "string") {
|
|
2232
|
-
return new Response(
|
|
2233
|
-
JSON.stringify({
|
|
2234
|
-
error: "Source path is required and must be a string",
|
|
2235
|
-
}),
|
|
2236
|
-
{
|
|
2237
|
-
headers: {
|
|
2238
|
-
"Content-Type": "application/json",
|
|
2239
|
-
...corsHeaders,
|
|
2240
|
-
},
|
|
2241
|
-
status: 400,
|
|
2242
|
-
}
|
|
2243
|
-
);
|
|
2244
|
-
}
|
|
2245
|
-
|
|
2246
|
-
if (!destinationPath || typeof destinationPath !== "string") {
|
|
2247
|
-
return new Response(
|
|
2248
|
-
JSON.stringify({
|
|
2249
|
-
error: "Destination path is required and must be a string",
|
|
2250
|
-
}),
|
|
2251
|
-
{
|
|
2252
|
-
headers: {
|
|
2253
|
-
"Content-Type": "application/json",
|
|
2254
|
-
...corsHeaders,
|
|
2255
|
-
},
|
|
2256
|
-
status: 400,
|
|
2257
|
-
}
|
|
2258
|
-
);
|
|
2259
|
-
}
|
|
2260
|
-
|
|
2261
|
-
// Basic safety check - prevent dangerous paths
|
|
2262
|
-
const dangerousPatterns = [
|
|
2263
|
-
/^\/$/, // Root directory
|
|
2264
|
-
/^\/etc/, // System directories
|
|
2265
|
-
/^\/var/, // System directories
|
|
2266
|
-
/^\/usr/, // System directories
|
|
2267
|
-
/^\/bin/, // System directories
|
|
2268
|
-
/^\/sbin/, // System directories
|
|
2269
|
-
/^\/boot/, // System directories
|
|
2270
|
-
/^\/dev/, // System directories
|
|
2271
|
-
/^\/proc/, // System directories
|
|
2272
|
-
/^\/sys/, // System directories
|
|
2273
|
-
/^\/tmp\/\.\./, // Path traversal attempts
|
|
2274
|
-
/\.\./, // Path traversal attempts
|
|
2275
|
-
];
|
|
2276
|
-
|
|
2277
|
-
if (
|
|
2278
|
-
dangerousPatterns.some(
|
|
2279
|
-
(pattern) => pattern.test(sourcePath) || pattern.test(destinationPath)
|
|
2280
|
-
)
|
|
2281
|
-
) {
|
|
2282
|
-
return new Response(
|
|
2283
|
-
JSON.stringify({
|
|
2284
|
-
error: "Dangerous path not allowed",
|
|
2285
|
-
}),
|
|
2286
|
-
{
|
|
2287
|
-
headers: {
|
|
2288
|
-
"Content-Type": "application/json",
|
|
2289
|
-
...corsHeaders,
|
|
2290
|
-
},
|
|
2291
|
-
status: 400,
|
|
2292
|
-
}
|
|
2293
|
-
);
|
|
2294
|
-
}
|
|
2295
|
-
|
|
2296
|
-
console.log(`[Server] Moving file: ${sourcePath} -> ${destinationPath}`);
|
|
2297
|
-
|
|
2298
|
-
const result = await executeMoveFile(
|
|
2299
|
-
sourcePath,
|
|
2300
|
-
destinationPath,
|
|
2301
|
-
sessionId
|
|
2302
|
-
);
|
|
2303
|
-
|
|
2304
|
-
return new Response(
|
|
2305
|
-
JSON.stringify({
|
|
2306
|
-
destinationPath,
|
|
2307
|
-
exitCode: result.exitCode,
|
|
2308
|
-
sourcePath,
|
|
2309
|
-
success: result.success,
|
|
2310
|
-
timestamp: new Date().toISOString(),
|
|
2311
|
-
}),
|
|
2312
|
-
{
|
|
2313
|
-
headers: {
|
|
2314
|
-
"Content-Type": "application/json",
|
|
2315
|
-
...corsHeaders,
|
|
2316
|
-
},
|
|
2317
|
-
}
|
|
2318
|
-
);
|
|
2319
|
-
} catch (error) {
|
|
2320
|
-
console.error("[Server] Error in handleMoveFileRequest:", error);
|
|
2321
|
-
return new Response(
|
|
2322
|
-
JSON.stringify({
|
|
2323
|
-
error: "Failed to move file",
|
|
2324
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
2325
|
-
}),
|
|
2326
|
-
{
|
|
2327
|
-
headers: {
|
|
2328
|
-
"Content-Type": "application/json",
|
|
2329
|
-
...corsHeaders,
|
|
2330
|
-
},
|
|
2331
|
-
status: 500,
|
|
2332
|
-
}
|
|
2333
|
-
);
|
|
2334
|
-
}
|
|
2335
|
-
}
|
|
2336
|
-
|
|
2337
|
-
async function handleStreamingMoveFileRequest(
|
|
2338
|
-
req: Request,
|
|
2339
|
-
corsHeaders: Record<string, string>
|
|
2340
|
-
): Promise<Response> {
|
|
2341
|
-
try {
|
|
2342
|
-
const body = (await req.json()) as MoveFileRequest;
|
|
2343
|
-
const { sourcePath, destinationPath, sessionId } = body;
|
|
2344
|
-
|
|
2345
|
-
if (!sourcePath || typeof sourcePath !== "string") {
|
|
2346
|
-
return new Response(
|
|
2347
|
-
JSON.stringify({
|
|
2348
|
-
error: "Source path is required and must be a string",
|
|
2349
|
-
}),
|
|
2350
|
-
{
|
|
2351
|
-
headers: {
|
|
2352
|
-
"Content-Type": "application/json",
|
|
2353
|
-
...corsHeaders,
|
|
2354
|
-
},
|
|
2355
|
-
status: 400,
|
|
2356
|
-
}
|
|
2357
|
-
);
|
|
2358
|
-
}
|
|
2359
|
-
|
|
2360
|
-
if (!destinationPath || typeof destinationPath !== "string") {
|
|
2361
|
-
return new Response(
|
|
2362
|
-
JSON.stringify({
|
|
2363
|
-
error: "Destination path is required and must be a string",
|
|
2364
|
-
}),
|
|
2365
|
-
{
|
|
2366
|
-
headers: {
|
|
2367
|
-
"Content-Type": "application/json",
|
|
2368
|
-
...corsHeaders,
|
|
2369
|
-
},
|
|
2370
|
-
status: 400,
|
|
2371
|
-
}
|
|
2372
|
-
);
|
|
2373
|
-
}
|
|
2374
|
-
|
|
2375
|
-
// Basic safety check - prevent dangerous paths
|
|
2376
|
-
const dangerousPatterns = [
|
|
2377
|
-
/^\/$/, // Root directory
|
|
2378
|
-
/^\/etc/, // System directories
|
|
2379
|
-
/^\/var/, // System directories
|
|
2380
|
-
/^\/usr/, // System directories
|
|
2381
|
-
/^\/bin/, // System directories
|
|
2382
|
-
/^\/sbin/, // System directories
|
|
2383
|
-
/^\/boot/, // System directories
|
|
2384
|
-
/^\/dev/, // System directories
|
|
2385
|
-
/^\/proc/, // System directories
|
|
2386
|
-
/^\/sys/, // System directories
|
|
2387
|
-
/^\/tmp\/\.\./, // Path traversal attempts
|
|
2388
|
-
/\.\./, // Path traversal attempts
|
|
2389
|
-
];
|
|
2390
|
-
|
|
2391
|
-
if (
|
|
2392
|
-
dangerousPatterns.some(
|
|
2393
|
-
(pattern) => pattern.test(sourcePath) || pattern.test(destinationPath)
|
|
2394
|
-
)
|
|
2395
|
-
) {
|
|
2396
|
-
return new Response(
|
|
2397
|
-
JSON.stringify({
|
|
2398
|
-
error: "Dangerous path not allowed",
|
|
2399
|
-
}),
|
|
2400
|
-
{
|
|
2401
|
-
headers: {
|
|
2402
|
-
"Content-Type": "application/json",
|
|
2403
|
-
...corsHeaders,
|
|
2404
|
-
},
|
|
2405
|
-
status: 400,
|
|
2406
|
-
}
|
|
2407
|
-
);
|
|
2408
|
-
}
|
|
2409
|
-
|
|
2410
|
-
console.log(
|
|
2411
|
-
`[Server] Moving file (streaming): ${sourcePath} -> ${destinationPath}`
|
|
2412
|
-
);
|
|
2413
|
-
|
|
2414
|
-
const stream = new ReadableStream({
|
|
2415
|
-
start(controller) {
|
|
2416
|
-
(async () => {
|
|
2417
|
-
try {
|
|
2418
|
-
// Send command start event
|
|
2419
|
-
controller.enqueue(
|
|
2420
|
-
new TextEncoder().encode(
|
|
2421
|
-
`data: ${JSON.stringify({
|
|
2422
|
-
destinationPath,
|
|
2423
|
-
sourcePath,
|
|
2424
|
-
timestamp: new Date().toISOString(),
|
|
2425
|
-
type: "command_start",
|
|
2426
|
-
})}\n\n`
|
|
2427
|
-
)
|
|
2428
|
-
);
|
|
2429
|
-
|
|
2430
|
-
// Move the file
|
|
2431
|
-
await executeMoveFile(sourcePath, destinationPath, sessionId);
|
|
2432
|
-
|
|
2433
|
-
console.log(
|
|
2434
|
-
`[Server] File moved successfully: ${sourcePath} -> ${destinationPath}`
|
|
2435
|
-
);
|
|
2436
|
-
|
|
2437
|
-
// Send command completion event
|
|
2438
|
-
controller.enqueue(
|
|
2439
|
-
new TextEncoder().encode(
|
|
2440
|
-
`data: ${JSON.stringify({
|
|
2441
|
-
destinationPath,
|
|
2442
|
-
sourcePath,
|
|
2443
|
-
success: true,
|
|
2444
|
-
timestamp: new Date().toISOString(),
|
|
2445
|
-
type: "command_complete",
|
|
2446
|
-
})}\n\n`
|
|
2447
|
-
)
|
|
2448
|
-
);
|
|
2449
|
-
|
|
2450
|
-
controller.close();
|
|
2451
|
-
} catch (error) {
|
|
2452
|
-
console.error(
|
|
2453
|
-
`[Server] Error moving file: ${sourcePath} -> ${destinationPath}`,
|
|
2454
|
-
error
|
|
2455
|
-
);
|
|
2456
|
-
|
|
2457
|
-
controller.enqueue(
|
|
2458
|
-
new TextEncoder().encode(
|
|
2459
|
-
`data: ${JSON.stringify({
|
|
2460
|
-
destinationPath,
|
|
2461
|
-
error:
|
|
2462
|
-
error instanceof Error ? error.message : "Unknown error",
|
|
2463
|
-
sourcePath,
|
|
2464
|
-
type: "error",
|
|
2465
|
-
})}\n\n`
|
|
2466
|
-
)
|
|
2467
|
-
);
|
|
2468
|
-
|
|
2469
|
-
controller.close();
|
|
2470
|
-
}
|
|
2471
|
-
})();
|
|
2472
|
-
},
|
|
2473
|
-
});
|
|
2474
|
-
|
|
2475
|
-
return new Response(stream, {
|
|
2476
|
-
headers: {
|
|
2477
|
-
"Cache-Control": "no-cache",
|
|
2478
|
-
Connection: "keep-alive",
|
|
2479
|
-
"Content-Type": "text/event-stream",
|
|
2480
|
-
...corsHeaders,
|
|
2481
|
-
},
|
|
2482
|
-
});
|
|
2483
|
-
} catch (error) {
|
|
2484
|
-
console.error("[Server] Error in handleStreamingMoveFileRequest:", error);
|
|
2485
|
-
return new Response(
|
|
2486
|
-
JSON.stringify({
|
|
2487
|
-
error: "Failed to move file",
|
|
2488
|
-
message: error instanceof Error ? error.message : "Unknown error",
|
|
2489
|
-
}),
|
|
2490
|
-
{
|
|
2491
|
-
headers: {
|
|
2492
|
-
"Content-Type": "application/json",
|
|
2493
|
-
...corsHeaders,
|
|
2494
|
-
},
|
|
2495
|
-
status: 500,
|
|
2496
|
-
}
|
|
2497
|
-
);
|
|
2498
|
-
}
|
|
2499
|
-
}
|
|
2500
|
-
|
|
2501
|
-
function executeCommand(
|
|
2502
|
-
command: string,
|
|
2503
|
-
args: string[],
|
|
2504
|
-
sessionId?: string
|
|
2505
|
-
): Promise<{
|
|
2506
|
-
success: boolean;
|
|
2507
|
-
stdout: string;
|
|
2508
|
-
stderr: string;
|
|
2509
|
-
exitCode: number;
|
|
2510
|
-
}> {
|
|
2511
|
-
return new Promise((resolve, reject) => {
|
|
2512
|
-
const child = spawn(command, args, {
|
|
2513
|
-
shell: true,
|
|
2514
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
2515
|
-
});
|
|
2516
|
-
|
|
2517
|
-
// Store the process reference for cleanup if sessionId is provided
|
|
2518
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
2519
|
-
const session = sessions.get(sessionId)!;
|
|
2520
|
-
session.activeProcess = child;
|
|
2521
|
-
}
|
|
2522
|
-
|
|
2523
|
-
let stdout = "";
|
|
2524
|
-
let stderr = "";
|
|
2525
|
-
|
|
2526
|
-
child.stdout?.on("data", (data) => {
|
|
2527
|
-
stdout += data.toString();
|
|
2528
|
-
});
|
|
2529
|
-
|
|
2530
|
-
child.stderr?.on("data", (data) => {
|
|
2531
|
-
stderr += data.toString();
|
|
2532
|
-
});
|
|
2533
|
-
|
|
2534
|
-
child.on("close", (code) => {
|
|
2535
|
-
// Clear the active process reference
|
|
2536
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
2537
|
-
const session = sessions.get(sessionId)!;
|
|
2538
|
-
session.activeProcess = null;
|
|
2539
|
-
}
|
|
2540
|
-
|
|
2541
|
-
console.log(`[Server] Command completed: ${command}, Exit code: ${code}`);
|
|
2542
|
-
|
|
2543
|
-
resolve({
|
|
2544
|
-
exitCode: code || 0,
|
|
2545
|
-
stderr,
|
|
2546
|
-
stdout,
|
|
2547
|
-
success: code === 0,
|
|
2548
|
-
});
|
|
2549
|
-
});
|
|
2550
|
-
|
|
2551
|
-
child.on("error", (error) => {
|
|
2552
|
-
// Clear the active process reference
|
|
2553
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
2554
|
-
const session = sessions.get(sessionId)!;
|
|
2555
|
-
session.activeProcess = null;
|
|
2556
|
-
}
|
|
2557
|
-
|
|
2558
|
-
reject(error);
|
|
2559
|
-
});
|
|
2560
|
-
});
|
|
2561
|
-
}
|
|
2562
|
-
|
|
2563
|
-
function executeGitCheckout(
|
|
2564
|
-
repoUrl: string,
|
|
2565
|
-
branch: string,
|
|
2566
|
-
targetDir: string,
|
|
2567
|
-
sessionId?: string
|
|
2568
|
-
): Promise<{
|
|
2569
|
-
success: boolean;
|
|
2570
|
-
stdout: string;
|
|
2571
|
-
stderr: string;
|
|
2572
|
-
exitCode: number;
|
|
2573
|
-
}> {
|
|
2574
|
-
return new Promise((resolve, reject) => {
|
|
2575
|
-
// First, clone the repository
|
|
2576
|
-
const cloneChild = spawn(
|
|
2577
|
-
"git",
|
|
2578
|
-
["clone", "-b", branch, repoUrl, targetDir],
|
|
2579
|
-
{
|
|
2580
|
-
shell: true,
|
|
2581
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
2582
|
-
}
|
|
2583
|
-
);
|
|
2584
|
-
|
|
2585
|
-
// Store the process reference for cleanup if sessionId is provided
|
|
2586
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
2587
|
-
const session = sessions.get(sessionId)!;
|
|
2588
|
-
session.activeProcess = cloneChild;
|
|
2589
|
-
}
|
|
2590
|
-
|
|
2591
|
-
let stdout = "";
|
|
2592
|
-
let stderr = "";
|
|
2593
|
-
|
|
2594
|
-
cloneChild.stdout?.on("data", (data) => {
|
|
2595
|
-
stdout += data.toString();
|
|
2596
|
-
});
|
|
2597
|
-
|
|
2598
|
-
cloneChild.stderr?.on("data", (data) => {
|
|
2599
|
-
stderr += data.toString();
|
|
2600
|
-
});
|
|
2601
|
-
|
|
2602
|
-
cloneChild.on("close", (code) => {
|
|
2603
|
-
// Clear the active process reference
|
|
2604
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
2605
|
-
const session = sessions.get(sessionId)!;
|
|
2606
|
-
session.activeProcess = null;
|
|
2607
|
-
}
|
|
2608
|
-
|
|
2609
|
-
if (code === 0) {
|
|
2610
|
-
console.log(
|
|
2611
|
-
`[Server] Repository cloned successfully: ${repoUrl} to ${targetDir}`
|
|
2612
|
-
);
|
|
2613
|
-
resolve({
|
|
2614
|
-
exitCode: code || 0,
|
|
2615
|
-
stderr,
|
|
2616
|
-
stdout,
|
|
2617
|
-
success: true,
|
|
2618
|
-
});
|
|
2619
|
-
} else {
|
|
2620
|
-
console.error(
|
|
2621
|
-
`[Server] Failed to clone repository: ${repoUrl}, Exit code: ${code}`
|
|
2622
|
-
);
|
|
2623
|
-
resolve({
|
|
2624
|
-
exitCode: code || 1,
|
|
2625
|
-
stderr,
|
|
2626
|
-
stdout,
|
|
2627
|
-
success: false,
|
|
2628
|
-
});
|
|
2629
|
-
}
|
|
2630
|
-
});
|
|
2631
|
-
|
|
2632
|
-
cloneChild.on("error", (error) => {
|
|
2633
|
-
// Clear the active process reference
|
|
2634
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
2635
|
-
const session = sessions.get(sessionId)!;
|
|
2636
|
-
session.activeProcess = null;
|
|
2637
|
-
}
|
|
2638
|
-
|
|
2639
|
-
console.error(`[Server] Error cloning repository: ${repoUrl}`, error);
|
|
2640
|
-
reject(error);
|
|
2641
|
-
});
|
|
2642
|
-
});
|
|
2643
|
-
}
|
|
2644
|
-
|
|
2645
|
-
function executeMkdir(
|
|
2646
|
-
path: string,
|
|
2647
|
-
recursive: boolean,
|
|
2648
|
-
sessionId?: string
|
|
2649
|
-
): Promise<{
|
|
2650
|
-
success: boolean;
|
|
2651
|
-
stdout: string;
|
|
2652
|
-
stderr: string;
|
|
2653
|
-
exitCode: number;
|
|
2654
|
-
}> {
|
|
2655
|
-
return new Promise((resolve, reject) => {
|
|
2656
|
-
const args = recursive ? ["-p", path] : [path];
|
|
2657
|
-
const mkdirChild = spawn("mkdir", args, {
|
|
2658
|
-
shell: true,
|
|
2659
|
-
stdio: ["pipe", "pipe", "pipe"],
|
|
2660
|
-
});
|
|
2661
|
-
|
|
2662
|
-
// Store the process reference for cleanup if sessionId is provided
|
|
2663
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
2664
|
-
const session = sessions.get(sessionId)!;
|
|
2665
|
-
session.activeProcess = mkdirChild;
|
|
2666
|
-
}
|
|
2667
|
-
|
|
2668
|
-
let stdout = "";
|
|
2669
|
-
let stderr = "";
|
|
2670
|
-
|
|
2671
|
-
mkdirChild.stdout?.on("data", (data) => {
|
|
2672
|
-
stdout += data.toString();
|
|
2673
|
-
});
|
|
2674
|
-
|
|
2675
|
-
mkdirChild.stderr?.on("data", (data) => {
|
|
2676
|
-
stderr += data.toString();
|
|
2677
|
-
});
|
|
2678
|
-
|
|
2679
|
-
mkdirChild.on("close", (code) => {
|
|
2680
|
-
// Clear the active process reference
|
|
2681
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
2682
|
-
const session = sessions.get(sessionId)!;
|
|
2683
|
-
session.activeProcess = null;
|
|
2684
|
-
}
|
|
2685
|
-
|
|
2686
|
-
if (code === 0) {
|
|
2687
|
-
console.log(`[Server] Directory created successfully: ${path}`);
|
|
2688
|
-
resolve({
|
|
2689
|
-
exitCode: code || 0,
|
|
2690
|
-
stderr,
|
|
2691
|
-
stdout,
|
|
2692
|
-
success: true,
|
|
2693
|
-
});
|
|
2694
|
-
} else {
|
|
2695
|
-
console.error(
|
|
2696
|
-
`[Server] Failed to create directory: ${path}, Exit code: ${code}`
|
|
2697
|
-
);
|
|
2698
|
-
resolve({
|
|
2699
|
-
exitCode: code || 1,
|
|
2700
|
-
stderr,
|
|
2701
|
-
stdout,
|
|
2702
|
-
success: false,
|
|
2703
|
-
});
|
|
2704
|
-
}
|
|
2705
|
-
});
|
|
2706
|
-
|
|
2707
|
-
mkdirChild.on("error", (error) => {
|
|
2708
|
-
// Clear the active process reference
|
|
2709
|
-
if (sessionId && sessions.has(sessionId)) {
|
|
2710
|
-
const session = sessions.get(sessionId)!;
|
|
2711
|
-
session.activeProcess = null;
|
|
2712
|
-
}
|
|
2713
|
-
|
|
2714
|
-
console.error(`[Server] Error creating directory: ${path}`, error);
|
|
2715
|
-
reject(error);
|
|
2716
|
-
});
|
|
2717
|
-
});
|
|
2718
|
-
}
|
|
2719
|
-
|
|
2720
|
-
function executeWriteFile(
|
|
2721
|
-
path: string,
|
|
2722
|
-
content: string,
|
|
2723
|
-
encoding: string,
|
|
2724
|
-
sessionId?: string
|
|
2725
|
-
): Promise<{
|
|
2726
|
-
success: boolean;
|
|
2727
|
-
exitCode: number;
|
|
2728
|
-
}> {
|
|
2729
|
-
return new Promise((resolve, reject) => {
|
|
2730
|
-
(async () => {
|
|
2731
|
-
try {
|
|
2732
|
-
// Ensure the directory exists
|
|
2733
|
-
const dir = dirname(path);
|
|
2734
|
-
if (dir !== ".") {
|
|
2735
|
-
await mkdir(dir, { recursive: true });
|
|
2736
|
-
}
|
|
2737
|
-
|
|
2738
|
-
// Write the file
|
|
2739
|
-
await writeFile(path, content, {
|
|
2740
|
-
encoding: encoding as BufferEncoding,
|
|
2741
|
-
});
|
|
2742
|
-
|
|
2743
|
-
console.log(`[Server] File written successfully: ${path}`);
|
|
2744
|
-
resolve({
|
|
2745
|
-
exitCode: 0,
|
|
2746
|
-
success: true,
|
|
2747
|
-
});
|
|
2748
|
-
} catch (error) {
|
|
2749
|
-
console.error(`[Server] Error writing file: ${path}`, error);
|
|
2750
|
-
reject(error);
|
|
2751
|
-
}
|
|
2752
|
-
})();
|
|
2753
|
-
});
|
|
2754
|
-
}
|
|
2755
|
-
|
|
2756
|
-
function executeReadFile(
|
|
2757
|
-
path: string,
|
|
2758
|
-
encoding: string,
|
|
2759
|
-
sessionId?: string
|
|
2760
|
-
): Promise<{
|
|
2761
|
-
success: boolean;
|
|
2762
|
-
exitCode: number;
|
|
2763
|
-
content: string;
|
|
2764
|
-
}> {
|
|
2765
|
-
return new Promise((resolve, reject) => {
|
|
2766
|
-
(async () => {
|
|
2767
|
-
try {
|
|
2768
|
-
// Read the file
|
|
2769
|
-
const content = await readFile(path, {
|
|
2770
|
-
encoding: encoding as BufferEncoding,
|
|
2771
|
-
});
|
|
2772
|
-
|
|
2773
|
-
console.log(`[Server] File read successfully: ${path}`);
|
|
2774
|
-
resolve({
|
|
2775
|
-
content,
|
|
2776
|
-
exitCode: 0,
|
|
2777
|
-
success: true,
|
|
2778
|
-
});
|
|
2779
|
-
} catch (error) {
|
|
2780
|
-
console.error(`[Server] Error reading file: ${path}`, error);
|
|
2781
|
-
reject(error);
|
|
2782
|
-
}
|
|
2783
|
-
})();
|
|
2784
|
-
});
|
|
2785
|
-
}
|
|
2786
|
-
|
|
2787
|
-
function executeDeleteFile(
|
|
2788
|
-
path: string,
|
|
2789
|
-
sessionId?: string
|
|
2790
|
-
): Promise<{
|
|
2791
|
-
success: boolean;
|
|
2792
|
-
exitCode: number;
|
|
2793
|
-
}> {
|
|
2794
|
-
return new Promise((resolve, reject) => {
|
|
2795
|
-
(async () => {
|
|
2796
|
-
try {
|
|
2797
|
-
// Delete the file
|
|
2798
|
-
await unlink(path);
|
|
2799
|
-
|
|
2800
|
-
console.log(`[Server] File deleted successfully: ${path}`);
|
|
2801
|
-
resolve({
|
|
2802
|
-
exitCode: 0,
|
|
2803
|
-
success: true,
|
|
2804
|
-
});
|
|
2805
|
-
} catch (error) {
|
|
2806
|
-
console.error(`[Server] Error deleting file: ${path}`, error);
|
|
2807
|
-
reject(error);
|
|
2808
|
-
}
|
|
2809
|
-
})();
|
|
2810
|
-
});
|
|
2811
|
-
}
|
|
2812
|
-
|
|
2813
|
-
function executeRenameFile(
|
|
2814
|
-
oldPath: string,
|
|
2815
|
-
newPath: string,
|
|
2816
|
-
sessionId?: string
|
|
2817
|
-
): Promise<{
|
|
2818
|
-
success: boolean;
|
|
2819
|
-
exitCode: number;
|
|
2820
|
-
}> {
|
|
2821
|
-
return new Promise((resolve, reject) => {
|
|
2822
|
-
(async () => {
|
|
2823
|
-
try {
|
|
2824
|
-
// Rename the file
|
|
2825
|
-
await rename(oldPath, newPath);
|
|
2826
|
-
|
|
2827
|
-
console.log(
|
|
2828
|
-
`[Server] File renamed successfully: ${oldPath} -> ${newPath}`
|
|
2829
|
-
);
|
|
2830
|
-
resolve({
|
|
2831
|
-
exitCode: 0,
|
|
2832
|
-
success: true,
|
|
2833
|
-
});
|
|
2834
|
-
} catch (error) {
|
|
2835
|
-
console.error(
|
|
2836
|
-
`[Server] Error renaming file: ${oldPath} -> ${newPath}`,
|
|
2837
|
-
error
|
|
2838
|
-
);
|
|
2839
|
-
reject(error);
|
|
2840
|
-
}
|
|
2841
|
-
})();
|
|
2842
|
-
});
|
|
2843
|
-
}
|
|
2844
|
-
|
|
2845
|
-
function executeMoveFile(
|
|
2846
|
-
sourcePath: string,
|
|
2847
|
-
destinationPath: string,
|
|
2848
|
-
sessionId?: string
|
|
2849
|
-
): Promise<{
|
|
2850
|
-
success: boolean;
|
|
2851
|
-
exitCode: number;
|
|
2852
|
-
}> {
|
|
2853
|
-
return new Promise((resolve, reject) => {
|
|
2854
|
-
(async () => {
|
|
2855
|
-
try {
|
|
2856
|
-
// Move the file
|
|
2857
|
-
await rename(sourcePath, destinationPath);
|
|
2858
|
-
|
|
2859
|
-
console.log(
|
|
2860
|
-
`[Server] File moved successfully: ${sourcePath} -> ${destinationPath}`
|
|
2861
|
-
);
|
|
2862
|
-
resolve({
|
|
2863
|
-
exitCode: 0,
|
|
2864
|
-
success: true,
|
|
2865
|
-
});
|
|
2866
|
-
} catch (error) {
|
|
2867
|
-
console.error(
|
|
2868
|
-
`[Server] Error moving file: ${sourcePath} -> ${destinationPath}`,
|
|
2869
|
-
error
|
|
2870
|
-
);
|
|
2871
|
-
reject(error);
|
|
2872
|
-
}
|
|
2873
|
-
})();
|
|
2874
|
-
});
|
|
2875
|
-
}
|
|
2876
|
-
|
|
2877
|
-
console.log(`🚀 Bun server running on http://localhost:${server.port}`);
|
|
562
|
+
console.log(`🚀 Bun server running on http://0.0.0.0:${server.port}`);
|
|
2878
563
|
console.log(`📡 HTTP API endpoints available:`);
|
|
2879
564
|
console.log(` POST /api/session/create - Create a new session`);
|
|
2880
565
|
console.log(` GET /api/session/list - List all sessions`);
|
|
2881
566
|
console.log(` POST /api/execute - Execute a command (non-streaming)`);
|
|
2882
567
|
console.log(` POST /api/execute/stream - Execute a command (streaming)`);
|
|
2883
568
|
console.log(` POST /api/git/checkout - Checkout a git repository`);
|
|
2884
|
-
console.log(
|
|
2885
|
-
` POST /api/git/checkout/stream - Checkout a git repository (streaming)`
|
|
2886
|
-
);
|
|
2887
569
|
console.log(` POST /api/mkdir - Create a directory`);
|
|
2888
|
-
console.log(` POST /api/mkdir/stream - Create a directory (streaming)`);
|
|
2889
570
|
console.log(` POST /api/write - Write a file`);
|
|
2890
|
-
console.log(` POST /api/write/stream - Write a file (streaming)`);
|
|
2891
571
|
console.log(` POST /api/read - Read a file`);
|
|
2892
|
-
console.log(` POST /api/read/stream - Read a file (streaming)`);
|
|
2893
572
|
console.log(` POST /api/delete - Delete a file`);
|
|
2894
|
-
console.log(` POST /api/delete/stream - Delete a file (streaming)`);
|
|
2895
573
|
console.log(` POST /api/rename - Rename a file`);
|
|
2896
|
-
console.log(` POST /api/rename/stream - Rename a file (streaming)`);
|
|
2897
574
|
console.log(` POST /api/move - Move a file`);
|
|
2898
|
-
console.log(` POST /api/
|
|
575
|
+
console.log(` POST /api/expose-port - Expose a port for external access`);
|
|
576
|
+
console.log(` DELETE /api/unexpose-port - Unexpose a port`);
|
|
577
|
+
console.log(` GET /api/exposed-ports - List exposed ports`);
|
|
578
|
+
console.log(` POST /api/process/start - Start a background process`);
|
|
579
|
+
console.log(` GET /api/process/list - List all processes`);
|
|
580
|
+
console.log(` GET /api/process/{id} - Get process status`);
|
|
581
|
+
console.log(` DELETE /api/process/{id} - Kill a process`);
|
|
582
|
+
console.log(` GET /api/process/{id}/logs - Get process logs`);
|
|
583
|
+
console.log(` GET /api/process/{id}/stream - Stream process logs (SSE)`);
|
|
584
|
+
console.log(` DELETE /api/process/kill-all - Kill all processes`);
|
|
585
|
+
console.log(` GET /proxy/{port}/* - Proxy requests to exposed ports`);
|
|
586
|
+
console.log(` POST /api/contexts - Create a code execution context`);
|
|
587
|
+
console.log(` GET /api/contexts - List all contexts`);
|
|
588
|
+
console.log(` DELETE /api/contexts/{id} - Delete a context`);
|
|
589
|
+
console.log(
|
|
590
|
+
` POST /api/execute/code - Execute code in a context (streaming)`
|
|
591
|
+
);
|
|
2899
592
|
console.log(` GET /api/ping - Health check`);
|
|
2900
|
-
console.log(` GET /api/commands - List available commands`);
|