@cloudflare/sandbox 0.0.0-444d2da → 0.0.0-4aceb32
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 +94 -0
- package/Dockerfile +97 -11
- package/README.md +743 -24
- package/container_src/bun.lock +122 -0
- package/container_src/circuit-breaker.ts +121 -0
- package/container_src/handler/exec.ts +340 -0
- package/container_src/handler/file.ts +844 -0
- package/container_src/handler/git.ts +182 -0
- package/container_src/handler/ports.ts +314 -0
- package/container_src/handler/process.ts +640 -0
- package/container_src/index.ts +402 -2646
- package/container_src/jupyter-server.ts +579 -0
- package/container_src/jupyter-service.ts +448 -0
- package/container_src/mime-processor.ts +255 -0
- package/container_src/package.json +9 -0
- package/container_src/startup.sh +83 -0
- package/container_src/types.ts +108 -0
- package/package.json +6 -8
- package/src/client.ts +353 -1261
- package/src/errors.ts +218 -0
- package/src/index.ts +54 -129
- package/src/interpreter-types.ts +383 -0
- package/src/interpreter.ts +150 -0
- package/src/jupyter-client.ts +349 -0
- package/src/request-handler.ts +144 -0
- package/src/sandbox.ts +778 -0
- package/src/security.ts +113 -0
- package/src/sse-parser.ts +147 -0
- package/src/types.ts +401 -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,68 +1,49 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
|
|
3
|
-
import { dirname } from "node:path";
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
4
2
|
import { serve } from "bun";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
interface DeleteFileRequest {
|
|
38
|
-
path: string;
|
|
39
|
-
sessionId?: string;
|
|
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
|
-
}
|
|
3
|
+
import {
|
|
4
|
+
handleExecuteRequest,
|
|
5
|
+
handleStreamingExecuteRequest,
|
|
6
|
+
} from "./handler/exec";
|
|
7
|
+
import {
|
|
8
|
+
handleDeleteFileRequest,
|
|
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 type { CreateContextRequest } from "./jupyter-server";
|
|
32
|
+
import { JupyterNotReadyError, JupyterService } from "./jupyter-service";
|
|
33
|
+
import type { ProcessRecord, SessionData } from "./types";
|
|
59
34
|
|
|
60
35
|
// In-memory session storage (in production, you'd want to use a proper database)
|
|
61
36
|
const sessions = new Map<string, SessionData>();
|
|
62
37
|
|
|
63
|
-
//
|
|
38
|
+
// In-memory storage for exposed ports
|
|
39
|
+
const exposedPorts = new Map<number, { name?: string; exposedAt: Date }>();
|
|
40
|
+
|
|
41
|
+
// In-memory process storage - cleared on container restart
|
|
42
|
+
const processes = new Map<string, ProcessRecord>();
|
|
43
|
+
|
|
44
|
+
// Generate a unique session ID using cryptographically secure randomness
|
|
64
45
|
function generateSessionId(): string {
|
|
65
|
-
return `session_${Date.now()}_${
|
|
46
|
+
return `session_${Date.now()}_${randomBytes(6).toString("hex")}`;
|
|
66
47
|
}
|
|
67
48
|
|
|
68
49
|
// Clean up old sessions (older than 1 hour)
|
|
@@ -79,11 +60,36 @@ function cleanupOldSessions() {
|
|
|
79
60
|
// Run cleanup every 10 minutes
|
|
80
61
|
setInterval(cleanupOldSessions, 10 * 60 * 1000);
|
|
81
62
|
|
|
63
|
+
// Initialize Jupyter service with graceful degradation
|
|
64
|
+
const jupyterService = new JupyterService();
|
|
65
|
+
|
|
66
|
+
// Start Jupyter initialization in background (non-blocking)
|
|
67
|
+
console.log("[Container] Starting Jupyter initialization in background...");
|
|
68
|
+
console.log(
|
|
69
|
+
"[Container] API endpoints are available immediately. Jupyter-dependent features will be available shortly."
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
jupyterService
|
|
73
|
+
.initialize()
|
|
74
|
+
.then(() => {
|
|
75
|
+
console.log(
|
|
76
|
+
"[Container] Jupyter fully initialized - all features available"
|
|
77
|
+
);
|
|
78
|
+
})
|
|
79
|
+
.catch((error) => {
|
|
80
|
+
console.error("[Container] Jupyter initialization failed:", error.message);
|
|
81
|
+
console.error(
|
|
82
|
+
"[Container] The API will continue in degraded mode without code execution capabilities"
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
|
|
82
86
|
const server = serve({
|
|
83
|
-
fetch(req: Request) {
|
|
87
|
+
async fetch(req: Request) {
|
|
84
88
|
const url = new URL(req.url);
|
|
85
89
|
const pathname = url.pathname;
|
|
86
90
|
|
|
91
|
+
console.log(`[Container] Incoming ${req.method} request to ${pathname}`);
|
|
92
|
+
|
|
87
93
|
// Handle CORS
|
|
88
94
|
const corsHeaders = {
|
|
89
95
|
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
|
@@ -93,11 +99,13 @@ const server = serve({
|
|
|
93
99
|
|
|
94
100
|
// Handle preflight requests
|
|
95
101
|
if (req.method === "OPTIONS") {
|
|
102
|
+
console.log(`[Container] Handling CORS preflight for ${pathname}`);
|
|
96
103
|
return new Response(null, { headers: corsHeaders, status: 200 });
|
|
97
104
|
}
|
|
98
105
|
|
|
99
106
|
try {
|
|
100
107
|
// Handle different routes
|
|
108
|
+
console.log(`[Container] Processing ${req.method} ${pathname}`);
|
|
101
109
|
switch (pathname) {
|
|
102
110
|
case "/":
|
|
103
111
|
return new Response("Hello from Bun server! 🚀", {
|
|
@@ -107,51 +115,6 @@ const server = serve({
|
|
|
107
115
|
},
|
|
108
116
|
});
|
|
109
117
|
|
|
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
118
|
case "/api/session/create":
|
|
156
119
|
if (req.method === "POST") {
|
|
157
120
|
const sessionId = generateSessionId();
|
|
@@ -208,22 +171,29 @@ const server = serve({
|
|
|
208
171
|
|
|
209
172
|
case "/api/execute":
|
|
210
173
|
if (req.method === "POST") {
|
|
211
|
-
return handleExecuteRequest(req, corsHeaders);
|
|
174
|
+
return handleExecuteRequest(sessions, req, corsHeaders);
|
|
212
175
|
}
|
|
213
176
|
break;
|
|
214
177
|
|
|
215
178
|
case "/api/execute/stream":
|
|
216
179
|
if (req.method === "POST") {
|
|
217
|
-
return handleStreamingExecuteRequest(req, corsHeaders);
|
|
180
|
+
return handleStreamingExecuteRequest(sessions, req, corsHeaders);
|
|
218
181
|
}
|
|
219
182
|
break;
|
|
220
183
|
|
|
221
184
|
case "/api/ping":
|
|
222
185
|
if (req.method === "GET") {
|
|
186
|
+
const health = await jupyterService.getHealthStatus();
|
|
223
187
|
return new Response(
|
|
224
188
|
JSON.stringify({
|
|
225
189
|
message: "pong",
|
|
226
190
|
timestamp: new Date().toISOString(),
|
|
191
|
+
jupyter: health.ready
|
|
192
|
+
? "ready"
|
|
193
|
+
: health.initializing
|
|
194
|
+
? "initializing"
|
|
195
|
+
: "not ready",
|
|
196
|
+
jupyterHealth: health,
|
|
227
197
|
}),
|
|
228
198
|
{
|
|
229
199
|
headers: {
|
|
@@ -269,96 +239,368 @@ const server = serve({
|
|
|
269
239
|
|
|
270
240
|
case "/api/git/checkout":
|
|
271
241
|
if (req.method === "POST") {
|
|
272
|
-
return handleGitCheckoutRequest(req, corsHeaders);
|
|
242
|
+
return handleGitCheckoutRequest(sessions, req, corsHeaders);
|
|
273
243
|
}
|
|
274
244
|
break;
|
|
275
245
|
|
|
276
|
-
case "/api/
|
|
246
|
+
case "/api/mkdir":
|
|
277
247
|
if (req.method === "POST") {
|
|
278
|
-
return
|
|
248
|
+
return handleMkdirRequest(sessions, req, corsHeaders);
|
|
279
249
|
}
|
|
280
250
|
break;
|
|
281
251
|
|
|
282
|
-
case "/api/
|
|
252
|
+
case "/api/write":
|
|
283
253
|
if (req.method === "POST") {
|
|
284
|
-
return
|
|
254
|
+
return handleWriteFileRequest(req, corsHeaders);
|
|
285
255
|
}
|
|
286
256
|
break;
|
|
287
257
|
|
|
288
|
-
case "/api/
|
|
258
|
+
case "/api/read":
|
|
289
259
|
if (req.method === "POST") {
|
|
290
|
-
return
|
|
260
|
+
return handleReadFileRequest(req, corsHeaders);
|
|
291
261
|
}
|
|
292
262
|
break;
|
|
293
263
|
|
|
294
|
-
case "/api/
|
|
264
|
+
case "/api/delete":
|
|
295
265
|
if (req.method === "POST") {
|
|
296
|
-
return
|
|
266
|
+
return handleDeleteFileRequest(req, corsHeaders);
|
|
297
267
|
}
|
|
298
268
|
break;
|
|
299
269
|
|
|
300
|
-
case "/api/
|
|
270
|
+
case "/api/rename":
|
|
301
271
|
if (req.method === "POST") {
|
|
302
|
-
return
|
|
272
|
+
return handleRenameFileRequest(req, corsHeaders);
|
|
303
273
|
}
|
|
304
274
|
break;
|
|
305
275
|
|
|
306
|
-
case "/api/
|
|
276
|
+
case "/api/move":
|
|
307
277
|
if (req.method === "POST") {
|
|
308
|
-
return
|
|
278
|
+
return handleMoveFileRequest(req, corsHeaders);
|
|
309
279
|
}
|
|
310
280
|
break;
|
|
311
281
|
|
|
312
|
-
case "/api/
|
|
282
|
+
case "/api/expose-port":
|
|
313
283
|
if (req.method === "POST") {
|
|
314
|
-
return
|
|
284
|
+
return handleExposePortRequest(exposedPorts, req, corsHeaders);
|
|
315
285
|
}
|
|
316
286
|
break;
|
|
317
287
|
|
|
318
|
-
case "/api/
|
|
319
|
-
if (req.method === "
|
|
320
|
-
return
|
|
288
|
+
case "/api/unexpose-port":
|
|
289
|
+
if (req.method === "DELETE") {
|
|
290
|
+
return handleUnexposePortRequest(exposedPorts, req, corsHeaders);
|
|
321
291
|
}
|
|
322
292
|
break;
|
|
323
293
|
|
|
324
|
-
case "/api/
|
|
325
|
-
if (req.method === "
|
|
326
|
-
return
|
|
294
|
+
case "/api/exposed-ports":
|
|
295
|
+
if (req.method === "GET") {
|
|
296
|
+
return handleGetExposedPortsRequest(exposedPorts, req, corsHeaders);
|
|
327
297
|
}
|
|
328
298
|
break;
|
|
329
299
|
|
|
330
|
-
case "/api/
|
|
300
|
+
case "/api/process/start":
|
|
331
301
|
if (req.method === "POST") {
|
|
332
|
-
return
|
|
302
|
+
return handleStartProcessRequest(processes, req, corsHeaders);
|
|
333
303
|
}
|
|
334
304
|
break;
|
|
335
305
|
|
|
336
|
-
case "/api/
|
|
337
|
-
if (req.method === "
|
|
338
|
-
return
|
|
306
|
+
case "/api/process/list":
|
|
307
|
+
if (req.method === "GET") {
|
|
308
|
+
return handleListProcessesRequest(processes, req, corsHeaders);
|
|
339
309
|
}
|
|
340
310
|
break;
|
|
341
311
|
|
|
342
|
-
case "/api/
|
|
312
|
+
case "/api/process/kill-all":
|
|
313
|
+
if (req.method === "DELETE") {
|
|
314
|
+
return handleKillAllProcessesRequest(processes, req, corsHeaders);
|
|
315
|
+
}
|
|
316
|
+
break;
|
|
317
|
+
|
|
318
|
+
// Code interpreter endpoints
|
|
319
|
+
case "/api/contexts":
|
|
343
320
|
if (req.method === "POST") {
|
|
344
|
-
|
|
321
|
+
try {
|
|
322
|
+
const body = (await req.json()) as CreateContextRequest;
|
|
323
|
+
const context = await jupyterService.createContext(body);
|
|
324
|
+
return new Response(
|
|
325
|
+
JSON.stringify({
|
|
326
|
+
id: context.id,
|
|
327
|
+
language: context.language,
|
|
328
|
+
cwd: context.cwd,
|
|
329
|
+
createdAt: context.createdAt,
|
|
330
|
+
lastUsed: context.lastUsed,
|
|
331
|
+
}),
|
|
332
|
+
{
|
|
333
|
+
headers: {
|
|
334
|
+
"Content-Type": "application/json",
|
|
335
|
+
...corsHeaders,
|
|
336
|
+
},
|
|
337
|
+
}
|
|
338
|
+
);
|
|
339
|
+
} catch (error) {
|
|
340
|
+
if (error instanceof JupyterNotReadyError) {
|
|
341
|
+
// This happens when request times out waiting for Jupyter
|
|
342
|
+
console.log(
|
|
343
|
+
`[Container] Request timed out waiting for Jupyter (${error.progress}% complete)`
|
|
344
|
+
);
|
|
345
|
+
return new Response(
|
|
346
|
+
JSON.stringify({
|
|
347
|
+
error: error.message,
|
|
348
|
+
status: "initializing",
|
|
349
|
+
progress: error.progress,
|
|
350
|
+
}),
|
|
351
|
+
{
|
|
352
|
+
status: 503,
|
|
353
|
+
headers: {
|
|
354
|
+
"Content-Type": "application/json",
|
|
355
|
+
"Retry-After": String(error.retryAfter),
|
|
356
|
+
...corsHeaders,
|
|
357
|
+
},
|
|
358
|
+
}
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Check if it's a circuit breaker error
|
|
363
|
+
if (
|
|
364
|
+
error instanceof Error &&
|
|
365
|
+
error.message.includes("Circuit breaker is open")
|
|
366
|
+
) {
|
|
367
|
+
console.log(
|
|
368
|
+
"[Container] Circuit breaker is open:",
|
|
369
|
+
error.message
|
|
370
|
+
);
|
|
371
|
+
return new Response(
|
|
372
|
+
JSON.stringify({
|
|
373
|
+
error:
|
|
374
|
+
"Service temporarily unavailable due to high error rate. Please try again later.",
|
|
375
|
+
status: "circuit_open",
|
|
376
|
+
details: error.message,
|
|
377
|
+
}),
|
|
378
|
+
{
|
|
379
|
+
status: 503,
|
|
380
|
+
headers: {
|
|
381
|
+
"Content-Type": "application/json",
|
|
382
|
+
"Retry-After": "60",
|
|
383
|
+
...corsHeaders,
|
|
384
|
+
},
|
|
385
|
+
}
|
|
386
|
+
);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// Only log actual errors with stack traces
|
|
390
|
+
console.error("[Container] Error creating context:", error);
|
|
391
|
+
return new Response(
|
|
392
|
+
JSON.stringify({
|
|
393
|
+
error:
|
|
394
|
+
error instanceof Error
|
|
395
|
+
? error.message
|
|
396
|
+
: "Failed to create context",
|
|
397
|
+
}),
|
|
398
|
+
{
|
|
399
|
+
status: 500,
|
|
400
|
+
headers: {
|
|
401
|
+
"Content-Type": "application/json",
|
|
402
|
+
...corsHeaders,
|
|
403
|
+
},
|
|
404
|
+
}
|
|
405
|
+
);
|
|
406
|
+
}
|
|
407
|
+
} else if (req.method === "GET") {
|
|
408
|
+
const contexts = await jupyterService.listContexts();
|
|
409
|
+
return new Response(JSON.stringify({ contexts }), {
|
|
410
|
+
headers: {
|
|
411
|
+
"Content-Type": "application/json",
|
|
412
|
+
...corsHeaders,
|
|
413
|
+
},
|
|
414
|
+
});
|
|
345
415
|
}
|
|
346
416
|
break;
|
|
347
417
|
|
|
348
|
-
case "/api/
|
|
418
|
+
case "/api/execute/code":
|
|
349
419
|
if (req.method === "POST") {
|
|
350
|
-
|
|
420
|
+
try {
|
|
421
|
+
const body = (await req.json()) as {
|
|
422
|
+
context_id: string;
|
|
423
|
+
code: string;
|
|
424
|
+
language?: string;
|
|
425
|
+
};
|
|
426
|
+
return await jupyterService.executeCode(
|
|
427
|
+
body.context_id,
|
|
428
|
+
body.code,
|
|
429
|
+
body.language
|
|
430
|
+
);
|
|
431
|
+
} catch (error) {
|
|
432
|
+
// Check if it's a circuit breaker error
|
|
433
|
+
if (
|
|
434
|
+
error instanceof Error &&
|
|
435
|
+
error.message.includes("Circuit breaker is open")
|
|
436
|
+
) {
|
|
437
|
+
console.log(
|
|
438
|
+
"[Container] Circuit breaker is open for code execution:",
|
|
439
|
+
error.message
|
|
440
|
+
);
|
|
441
|
+
return new Response(
|
|
442
|
+
JSON.stringify({
|
|
443
|
+
error:
|
|
444
|
+
"Service temporarily unavailable due to high error rate. Please try again later.",
|
|
445
|
+
status: "circuit_open",
|
|
446
|
+
details: error.message,
|
|
447
|
+
}),
|
|
448
|
+
{
|
|
449
|
+
status: 503,
|
|
450
|
+
headers: {
|
|
451
|
+
"Content-Type": "application/json",
|
|
452
|
+
"Retry-After": "30",
|
|
453
|
+
...corsHeaders,
|
|
454
|
+
},
|
|
455
|
+
}
|
|
456
|
+
);
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// Don't log stack traces for expected initialization state
|
|
460
|
+
if (
|
|
461
|
+
error instanceof Error &&
|
|
462
|
+
error.message.includes("initializing")
|
|
463
|
+
) {
|
|
464
|
+
console.log(
|
|
465
|
+
"[Container] Code execution deferred - Jupyter still initializing"
|
|
466
|
+
);
|
|
467
|
+
} else {
|
|
468
|
+
console.error("[Container] Error executing code:", error);
|
|
469
|
+
}
|
|
470
|
+
// Error response is already handled by jupyterService.executeCode for not ready state
|
|
471
|
+
return new Response(
|
|
472
|
+
JSON.stringify({
|
|
473
|
+
error:
|
|
474
|
+
error instanceof Error
|
|
475
|
+
? error.message
|
|
476
|
+
: "Failed to execute code",
|
|
477
|
+
}),
|
|
478
|
+
{
|
|
479
|
+
status: 500,
|
|
480
|
+
headers: {
|
|
481
|
+
"Content-Type": "application/json",
|
|
482
|
+
...corsHeaders,
|
|
483
|
+
},
|
|
484
|
+
}
|
|
485
|
+
);
|
|
486
|
+
}
|
|
351
487
|
}
|
|
352
488
|
break;
|
|
353
489
|
|
|
354
490
|
default:
|
|
491
|
+
// Handle dynamic routes for contexts
|
|
492
|
+
if (
|
|
493
|
+
pathname.startsWith("/api/contexts/") &&
|
|
494
|
+
pathname.split("/").length === 4
|
|
495
|
+
) {
|
|
496
|
+
const contextId = pathname.split("/")[3];
|
|
497
|
+
if (req.method === "DELETE") {
|
|
498
|
+
try {
|
|
499
|
+
await jupyterService.deleteContext(contextId);
|
|
500
|
+
return new Response(JSON.stringify({ success: true }), {
|
|
501
|
+
headers: {
|
|
502
|
+
"Content-Type": "application/json",
|
|
503
|
+
...corsHeaders,
|
|
504
|
+
},
|
|
505
|
+
});
|
|
506
|
+
} catch (error) {
|
|
507
|
+
if (error instanceof JupyterNotReadyError) {
|
|
508
|
+
console.log(
|
|
509
|
+
`[Container] Request timed out waiting for Jupyter (${error.progress}% complete)`
|
|
510
|
+
);
|
|
511
|
+
return new Response(
|
|
512
|
+
JSON.stringify({
|
|
513
|
+
error: error.message,
|
|
514
|
+
status: "initializing",
|
|
515
|
+
progress: error.progress,
|
|
516
|
+
}),
|
|
517
|
+
{
|
|
518
|
+
status: 503,
|
|
519
|
+
headers: {
|
|
520
|
+
"Content-Type": "application/json",
|
|
521
|
+
"Retry-After": "5",
|
|
522
|
+
...corsHeaders,
|
|
523
|
+
},
|
|
524
|
+
}
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
return new Response(
|
|
528
|
+
JSON.stringify({
|
|
529
|
+
error:
|
|
530
|
+
error instanceof Error
|
|
531
|
+
? error.message
|
|
532
|
+
: "Failed to delete context",
|
|
533
|
+
}),
|
|
534
|
+
{
|
|
535
|
+
status:
|
|
536
|
+
error instanceof Error &&
|
|
537
|
+
error.message.includes("not found")
|
|
538
|
+
? 404
|
|
539
|
+
: 500,
|
|
540
|
+
headers: {
|
|
541
|
+
"Content-Type": "application/json",
|
|
542
|
+
...corsHeaders,
|
|
543
|
+
},
|
|
544
|
+
}
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// Handle dynamic routes for individual processes
|
|
551
|
+
if (pathname.startsWith("/api/process/")) {
|
|
552
|
+
const segments = pathname.split("/");
|
|
553
|
+
if (segments.length >= 4) {
|
|
554
|
+
const processId = segments[3];
|
|
555
|
+
const action = segments[4]; // Optional: logs, stream, etc.
|
|
556
|
+
|
|
557
|
+
if (!action && req.method === "GET") {
|
|
558
|
+
return handleGetProcessRequest(
|
|
559
|
+
processes,
|
|
560
|
+
req,
|
|
561
|
+
corsHeaders,
|
|
562
|
+
processId
|
|
563
|
+
);
|
|
564
|
+
} else if (!action && req.method === "DELETE") {
|
|
565
|
+
return handleKillProcessRequest(
|
|
566
|
+
processes,
|
|
567
|
+
req,
|
|
568
|
+
corsHeaders,
|
|
569
|
+
processId
|
|
570
|
+
);
|
|
571
|
+
} else if (action === "logs" && req.method === "GET") {
|
|
572
|
+
return handleGetProcessLogsRequest(
|
|
573
|
+
processes,
|
|
574
|
+
req,
|
|
575
|
+
corsHeaders,
|
|
576
|
+
processId
|
|
577
|
+
);
|
|
578
|
+
} else if (action === "stream" && req.method === "GET") {
|
|
579
|
+
return handleStreamProcessLogsRequest(
|
|
580
|
+
processes,
|
|
581
|
+
req,
|
|
582
|
+
corsHeaders,
|
|
583
|
+
processId
|
|
584
|
+
);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
// Check if this is a proxy request for an exposed port
|
|
589
|
+
if (pathname.startsWith("/proxy/")) {
|
|
590
|
+
return handleProxyRequest(exposedPorts, req, corsHeaders);
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
console.log(`[Container] Route not found: ${pathname}`);
|
|
355
594
|
return new Response("Not Found", {
|
|
356
595
|
headers: corsHeaders,
|
|
357
596
|
status: 404,
|
|
358
597
|
});
|
|
359
598
|
}
|
|
360
599
|
} catch (error) {
|
|
361
|
-
console.error(
|
|
600
|
+
console.error(
|
|
601
|
+
`[Container] Error handling ${req.method} ${pathname}:`,
|
|
602
|
+
error
|
|
603
|
+
);
|
|
362
604
|
return new Response(
|
|
363
605
|
JSON.stringify({
|
|
364
606
|
error: "Internal server error",
|
|
@@ -374,2527 +616,41 @@ const server = serve({
|
|
|
374
616
|
);
|
|
375
617
|
}
|
|
376
618
|
},
|
|
619
|
+
hostname: "0.0.0.0",
|
|
377
620
|
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
|
-
});
|
|
621
|
+
// We don't need this, but typescript complains
|
|
622
|
+
websocket: { async message() {} },
|
|
623
|
+
});
|
|
586
624
|
|
|
587
|
-
|
|
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;
|
|
766
|
-
|
|
767
|
-
if (!repoUrl || typeof repoUrl !== "string") {
|
|
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}`);
|
|
625
|
+
console.log(`🚀 Bun server running on http://0.0.0.0:${server.port}`);
|
|
2878
626
|
console.log(`📡 HTTP API endpoints available:`);
|
|
2879
627
|
console.log(` POST /api/session/create - Create a new session`);
|
|
2880
628
|
console.log(` GET /api/session/list - List all sessions`);
|
|
2881
629
|
console.log(` POST /api/execute - Execute a command (non-streaming)`);
|
|
2882
630
|
console.log(` POST /api/execute/stream - Execute a command (streaming)`);
|
|
2883
631
|
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
632
|
console.log(` POST /api/mkdir - Create a directory`);
|
|
2888
|
-
console.log(` POST /api/mkdir/stream - Create a directory (streaming)`);
|
|
2889
633
|
console.log(` POST /api/write - Write a file`);
|
|
2890
|
-
console.log(` POST /api/write/stream - Write a file (streaming)`);
|
|
2891
634
|
console.log(` POST /api/read - Read a file`);
|
|
2892
|
-
console.log(` POST /api/read/stream - Read a file (streaming)`);
|
|
2893
635
|
console.log(` POST /api/delete - Delete a file`);
|
|
2894
|
-
console.log(` POST /api/delete/stream - Delete a file (streaming)`);
|
|
2895
636
|
console.log(` POST /api/rename - Rename a file`);
|
|
2896
|
-
console.log(` POST /api/rename/stream - Rename a file (streaming)`);
|
|
2897
637
|
console.log(` POST /api/move - Move a file`);
|
|
2898
|
-
console.log(` POST /api/
|
|
638
|
+
console.log(` POST /api/expose-port - Expose a port for external access`);
|
|
639
|
+
console.log(` DELETE /api/unexpose-port - Unexpose a port`);
|
|
640
|
+
console.log(` GET /api/exposed-ports - List exposed ports`);
|
|
641
|
+
console.log(` POST /api/process/start - Start a background process`);
|
|
642
|
+
console.log(` GET /api/process/list - List all processes`);
|
|
643
|
+
console.log(` GET /api/process/{id} - Get process status`);
|
|
644
|
+
console.log(` DELETE /api/process/{id} - Kill a process`);
|
|
645
|
+
console.log(` GET /api/process/{id}/logs - Get process logs`);
|
|
646
|
+
console.log(` GET /api/process/{id}/stream - Stream process logs (SSE)`);
|
|
647
|
+
console.log(` DELETE /api/process/kill-all - Kill all processes`);
|
|
648
|
+
console.log(` GET /proxy/{port}/* - Proxy requests to exposed ports`);
|
|
649
|
+
console.log(` POST /api/contexts - Create a code execution context`);
|
|
650
|
+
console.log(` GET /api/contexts - List all contexts`);
|
|
651
|
+
console.log(` DELETE /api/contexts/{id} - Delete a context`);
|
|
652
|
+
console.log(
|
|
653
|
+
` POST /api/execute/code - Execute code in a context (streaming)`
|
|
654
|
+
);
|
|
2899
655
|
console.log(` GET /api/ping - Health check`);
|
|
2900
656
|
console.log(` GET /api/commands - List available commands`);
|