@cloudflare/sandbox 0.0.0-12bbd12 → 0.0.0-153e416

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.
Files changed (99) hide show
  1. package/CHANGELOG.md +180 -0
  2. package/Dockerfile +107 -70
  3. package/README.md +88 -710
  4. package/dist/chunk-BFVUNTP4.js +104 -0
  5. package/dist/chunk-BFVUNTP4.js.map +1 -0
  6. package/dist/chunk-EKSWCBCA.js +86 -0
  7. package/dist/chunk-EKSWCBCA.js.map +1 -0
  8. package/dist/chunk-FE4PJSRB.js +7 -0
  9. package/dist/chunk-FE4PJSRB.js.map +1 -0
  10. package/dist/chunk-JXZMAU2C.js +559 -0
  11. package/dist/chunk-JXZMAU2C.js.map +1 -0
  12. package/dist/chunk-SVWLTRHD.js +2456 -0
  13. package/dist/chunk-SVWLTRHD.js.map +1 -0
  14. package/dist/chunk-Z532A7QC.js +78 -0
  15. package/dist/chunk-Z532A7QC.js.map +1 -0
  16. package/dist/file-stream.d.ts +43 -0
  17. package/dist/file-stream.js +9 -0
  18. package/dist/file-stream.js.map +1 -0
  19. package/dist/index.d.ts +9 -0
  20. package/dist/index.js +67 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/interpreter.d.ts +33 -0
  23. package/dist/interpreter.js +8 -0
  24. package/dist/interpreter.js.map +1 -0
  25. package/dist/request-handler.d.ts +18 -0
  26. package/dist/request-handler.js +13 -0
  27. package/dist/request-handler.js.map +1 -0
  28. package/dist/sandbox-DWQVgVTY.d.ts +603 -0
  29. package/dist/sandbox.d.ts +4 -0
  30. package/dist/sandbox.js +13 -0
  31. package/dist/sandbox.js.map +1 -0
  32. package/dist/security.d.ts +31 -0
  33. package/dist/security.js +13 -0
  34. package/dist/security.js.map +1 -0
  35. package/dist/sse-parser.d.ts +28 -0
  36. package/dist/sse-parser.js +11 -0
  37. package/dist/sse-parser.js.map +1 -0
  38. package/dist/version.d.ts +8 -0
  39. package/dist/version.js +7 -0
  40. package/dist/version.js.map +1 -0
  41. package/package.json +13 -5
  42. package/src/clients/base-client.ts +280 -0
  43. package/src/clients/command-client.ts +115 -0
  44. package/src/clients/file-client.ts +295 -0
  45. package/src/clients/git-client.ts +92 -0
  46. package/src/clients/index.ts +64 -0
  47. package/src/{jupyter-client.ts → clients/interpreter-client.ts} +148 -168
  48. package/src/clients/port-client.ts +105 -0
  49. package/src/clients/process-client.ts +177 -0
  50. package/src/clients/sandbox-client.ts +41 -0
  51. package/src/clients/types.ts +84 -0
  52. package/src/clients/utility-client.ts +119 -0
  53. package/src/errors/adapter.ts +180 -0
  54. package/src/errors/classes.ts +469 -0
  55. package/src/errors/index.ts +105 -0
  56. package/src/file-stream.ts +164 -0
  57. package/src/index.ts +87 -50
  58. package/src/interpreter.ts +22 -13
  59. package/src/request-handler.ts +80 -44
  60. package/src/sandbox.ts +671 -446
  61. package/src/security.ts +14 -23
  62. package/src/sse-parser.ts +4 -8
  63. package/src/version.ts +6 -0
  64. package/startup.sh +3 -0
  65. package/tests/base-client.test.ts +328 -0
  66. package/tests/command-client.test.ts +407 -0
  67. package/tests/file-client.test.ts +719 -0
  68. package/tests/file-stream.test.ts +306 -0
  69. package/tests/get-sandbox.test.ts +110 -0
  70. package/tests/git-client.test.ts +328 -0
  71. package/tests/port-client.test.ts +301 -0
  72. package/tests/process-client.test.ts +658 -0
  73. package/tests/request-handler.test.ts +240 -0
  74. package/tests/sandbox.test.ts +554 -0
  75. package/tests/sse-parser.test.ts +290 -0
  76. package/tests/utility-client.test.ts +332 -0
  77. package/tests/version.test.ts +16 -0
  78. package/tests/wrangler.jsonc +35 -0
  79. package/tsconfig.json +9 -1
  80. package/vitest.config.ts +31 -0
  81. package/container_src/bun.lock +0 -122
  82. package/container_src/circuit-breaker.ts +0 -121
  83. package/container_src/handler/exec.ts +0 -340
  84. package/container_src/handler/file.ts +0 -1064
  85. package/container_src/handler/git.ts +0 -182
  86. package/container_src/handler/ports.ts +0 -314
  87. package/container_src/handler/process.ts +0 -640
  88. package/container_src/index.ts +0 -663
  89. package/container_src/jupyter-server.ts +0 -579
  90. package/container_src/jupyter-service.ts +0 -461
  91. package/container_src/jupyter_config.py +0 -48
  92. package/container_src/mime-processor.ts +0 -255
  93. package/container_src/package.json +0 -18
  94. package/container_src/startup.sh +0 -84
  95. package/container_src/types.ts +0 -117
  96. package/src/client.ts +0 -1024
  97. package/src/errors.ts +0 -218
  98. package/src/interpreter-types.ts +0 -383
  99. package/src/types.ts +0 -511
package/src/client.ts DELETED
@@ -1,1024 +0,0 @@
1
- import type { ExecuteRequest } from "../container_src/types";
2
- import type { Sandbox } from "./index";
3
- import type {
4
- BaseExecOptions,
5
- DeleteFileResponse,
6
- ExecuteResponse,
7
- GetProcessLogsResponse,
8
- GetProcessResponse,
9
- GitCheckoutResponse,
10
- ListFilesResponse,
11
- ListProcessesResponse,
12
- MkdirResponse,
13
- MoveFileResponse,
14
- ReadFileResponse,
15
- RenameFileResponse,
16
- StartProcessRequest,
17
- StartProcessResponse,
18
- WriteFileResponse,
19
- } from "./types";
20
-
21
-
22
- interface CommandsResponse {
23
- availableCommands: string[];
24
- timestamp: string;
25
- }
26
-
27
- interface GitCheckoutRequest {
28
- repoUrl: string;
29
- branch?: string;
30
- targetDir?: string;
31
- sessionId?: string;
32
- }
33
-
34
-
35
- interface MkdirRequest {
36
- path: string;
37
- recursive?: boolean;
38
- sessionId?: string;
39
- }
40
-
41
-
42
- interface WriteFileRequest {
43
- path: string;
44
- content: string;
45
- encoding?: string;
46
- sessionId?: string;
47
- }
48
-
49
-
50
- interface ReadFileRequest {
51
- path: string;
52
- encoding?: string;
53
- sessionId?: string;
54
- }
55
-
56
-
57
- interface DeleteFileRequest {
58
- path: string;
59
- sessionId?: string;
60
- }
61
-
62
-
63
- interface RenameFileRequest {
64
- oldPath: string;
65
- newPath: string;
66
- sessionId?: string;
67
- }
68
-
69
-
70
- interface MoveFileRequest {
71
- sourcePath: string;
72
- destinationPath: string;
73
- sessionId?: string;
74
- }
75
-
76
-
77
- interface ListFilesRequest {
78
- path: string;
79
- options?: {
80
- recursive?: boolean;
81
- includeHidden?: boolean;
82
- };
83
- sessionId?: string;
84
- }
85
-
86
-
87
- interface PreviewInfo {
88
- url: string;
89
- port: number;
90
- name?: string;
91
- }
92
-
93
- interface ExposedPort extends PreviewInfo {
94
- exposedAt: string;
95
- timestamp: string;
96
- }
97
-
98
- interface ExposePortResponse {
99
- success: boolean;
100
- port: number;
101
- name?: string;
102
- exposedAt: string;
103
- timestamp: string;
104
- }
105
-
106
- interface UnexposePortResponse {
107
- success: boolean;
108
- port: number;
109
- timestamp: string;
110
- }
111
-
112
- interface GetExposedPortsResponse {
113
- ports: ExposedPort[];
114
- count: number;
115
- timestamp: string;
116
- }
117
-
118
- interface PingResponse {
119
- message: string;
120
- timestamp: string;
121
- }
122
-
123
- interface HttpClientOptions {
124
- stub?: Sandbox;
125
- baseUrl?: string;
126
- port?: number;
127
- onCommandStart?: (command: string) => void;
128
- onOutput?: (
129
- stream: "stdout" | "stderr",
130
- data: string,
131
- command: string
132
- ) => void;
133
- onCommandComplete?: (
134
- success: boolean,
135
- exitCode: number,
136
- stdout: string,
137
- stderr: string,
138
- command: string
139
- ) => void;
140
- onError?: (error: string, command?: string) => void;
141
- }
142
-
143
- export class HttpClient {
144
- private baseUrl: string;
145
- private options: HttpClientOptions;
146
- private sessionId: string | null = null;
147
-
148
- constructor(options: HttpClientOptions = {}) {
149
- this.options = {
150
- ...options,
151
- };
152
- this.baseUrl = this.options.baseUrl!;
153
- }
154
-
155
- protected async doFetch(
156
- path: string,
157
- options?: RequestInit
158
- ): Promise<Response> {
159
- const url = this.options.stub
160
- ? `http://localhost:${this.options.port}${path}`
161
- : `${this.baseUrl}${path}`;
162
- const method = options?.method || "GET";
163
-
164
- console.log(`[HTTP Client] Making ${method} request to ${url}`);
165
-
166
- try {
167
- let response: Response;
168
-
169
- if (this.options.stub) {
170
- response = await this.options.stub.containerFetch(
171
- url,
172
- options,
173
- this.options.port
174
- );
175
- } else {
176
- response = await fetch(url, options);
177
- }
178
-
179
- console.log(
180
- `[HTTP Client] Response: ${response.status} ${response.statusText}`
181
- );
182
-
183
- if (!response.ok) {
184
- console.error(
185
- `[HTTP Client] Request failed: ${method} ${url} - ${response.status} ${response.statusText}`
186
- );
187
- }
188
-
189
- return response;
190
- } catch (error) {
191
- console.error(`[HTTP Client] Request error: ${method} ${url}`, error);
192
- throw error;
193
- }
194
- }
195
-
196
- async execute(
197
- command: string,
198
- options: Pick<BaseExecOptions, "sessionId" | "cwd" | "env">
199
- ): Promise<ExecuteResponse> {
200
- try {
201
- const targetSessionId = options.sessionId || this.sessionId;
202
- const executeRequest = {
203
- command,
204
- sessionId: targetSessionId,
205
- cwd: options.cwd,
206
- env: options.env,
207
- } satisfies ExecuteRequest;
208
-
209
- const response = await this.doFetch(`/api/execute`, {
210
- body: JSON.stringify(executeRequest),
211
- headers: {
212
- "Content-Type": "application/json",
213
- },
214
- method: "POST",
215
- });
216
-
217
- if (!response.ok) {
218
- const errorData = (await response.json().catch(() => ({}))) as {
219
- error?: string;
220
- };
221
- throw new Error(
222
- errorData.error || `HTTP error! status: ${response.status}`
223
- );
224
- }
225
-
226
- const data: ExecuteResponse = await response.json();
227
- console.log(
228
- `[HTTP Client] Command executed: ${command}, Success: ${data.success}`
229
- );
230
-
231
- // Call the callback if provided
232
- this.options.onCommandComplete?.(
233
- data.success,
234
- data.exitCode,
235
- data.stdout,
236
- data.stderr,
237
- data.command
238
- );
239
-
240
- return data;
241
- } catch (error) {
242
- console.error("[HTTP Client] Error executing command:", error);
243
- this.options.onError?.(
244
- error instanceof Error ? error.message : "Unknown error",
245
- command
246
- );
247
- throw error;
248
- }
249
- }
250
-
251
- async executeCommandStream(
252
- command: string,
253
- sessionId?: string
254
- ): Promise<ReadableStream<Uint8Array>> {
255
- try {
256
- const targetSessionId = sessionId || this.sessionId;
257
-
258
- const response = await this.doFetch(`/api/execute/stream`, {
259
- body: JSON.stringify({
260
- command,
261
- sessionId: targetSessionId,
262
- }),
263
- headers: {
264
- "Content-Type": "application/json",
265
- Accept: "text/event-stream",
266
- },
267
- method: "POST",
268
- });
269
-
270
- if (!response.ok) {
271
- const errorData = (await response.json().catch(() => ({}))) as {
272
- error?: string;
273
- };
274
- throw new Error(
275
- errorData.error || `HTTP error! status: ${response.status}`
276
- );
277
- }
278
-
279
- if (!response.body) {
280
- throw new Error("No response body for streaming request");
281
- }
282
-
283
- console.log(`[HTTP Client] Started command stream: ${command}`);
284
-
285
- return response.body;
286
- } catch (error) {
287
- console.error("[HTTP Client] Error in command stream:", error);
288
- throw error;
289
- }
290
- }
291
-
292
- async gitCheckout(
293
- repoUrl: string,
294
- branch: string = "main",
295
- targetDir?: string,
296
- sessionId?: string
297
- ): Promise<GitCheckoutResponse> {
298
- try {
299
- const targetSessionId = sessionId || this.sessionId;
300
-
301
- const response = await this.doFetch(`/api/git/checkout`, {
302
- body: JSON.stringify({
303
- branch,
304
- repoUrl,
305
- sessionId: targetSessionId,
306
- targetDir,
307
- } as GitCheckoutRequest),
308
- headers: {
309
- "Content-Type": "application/json",
310
- },
311
- method: "POST",
312
- });
313
-
314
- if (!response.ok) {
315
- const errorData = (await response.json().catch(() => ({}))) as {
316
- error?: string;
317
- };
318
- throw new Error(
319
- errorData.error || `HTTP error! status: ${response.status}`
320
- );
321
- }
322
-
323
- const data: GitCheckoutResponse = await response.json();
324
- console.log(
325
- `[HTTP Client] Git checkout completed: ${repoUrl}, Success: ${data.success}, Target: ${data.targetDir}`
326
- );
327
-
328
- return data;
329
- } catch (error) {
330
- console.error("[HTTP Client] Error in git checkout:", error);
331
- throw error;
332
- }
333
- }
334
-
335
- async mkdir(
336
- path: string,
337
- recursive: boolean = false,
338
- sessionId?: string
339
- ): Promise<MkdirResponse> {
340
- try {
341
- const targetSessionId = sessionId || this.sessionId;
342
-
343
- const response = await this.doFetch(`/api/mkdir`, {
344
- body: JSON.stringify({
345
- path,
346
- recursive,
347
- sessionId: targetSessionId,
348
- } as MkdirRequest),
349
- headers: {
350
- "Content-Type": "application/json",
351
- },
352
- method: "POST",
353
- });
354
-
355
- if (!response.ok) {
356
- const errorData = (await response.json().catch(() => ({}))) as {
357
- error?: string;
358
- };
359
- throw new Error(
360
- errorData.error || `HTTP error! status: ${response.status}`
361
- );
362
- }
363
-
364
- const data: MkdirResponse = await response.json();
365
- console.log(
366
- `[HTTP Client] Directory created: ${path}, Success: ${data.success}, Recursive: ${data.recursive}`
367
- );
368
-
369
- return data;
370
- } catch (error) {
371
- console.error("[HTTP Client] Error creating directory:", error);
372
- throw error;
373
- }
374
- }
375
-
376
- async writeFile(
377
- path: string,
378
- content: string,
379
- encoding: string = "utf-8",
380
- sessionId?: string
381
- ): Promise<WriteFileResponse> {
382
- try {
383
- const targetSessionId = sessionId || this.sessionId;
384
-
385
- const response = await this.doFetch(`/api/write`, {
386
- body: JSON.stringify({
387
- content,
388
- encoding,
389
- path,
390
- sessionId: targetSessionId,
391
- } as WriteFileRequest),
392
- headers: {
393
- "Content-Type": "application/json",
394
- },
395
- method: "POST",
396
- });
397
-
398
- if (!response.ok) {
399
- const errorData = (await response.json().catch(() => ({}))) as {
400
- error?: string;
401
- };
402
- throw new Error(
403
- errorData.error || `HTTP error! status: ${response.status}`
404
- );
405
- }
406
-
407
- const data: WriteFileResponse = await response.json();
408
- console.log(
409
- `[HTTP Client] File written: ${path}, Success: ${data.success}`
410
- );
411
-
412
- return data;
413
- } catch (error) {
414
- console.error("[HTTP Client] Error writing file:", error);
415
- throw error;
416
- }
417
- }
418
-
419
- async readFile(
420
- path: string,
421
- encoding: string = "utf-8",
422
- sessionId?: string
423
- ): Promise<ReadFileResponse> {
424
- try {
425
- const targetSessionId = sessionId || this.sessionId;
426
-
427
- const response = await this.doFetch(`/api/read`, {
428
- body: JSON.stringify({
429
- encoding,
430
- path,
431
- sessionId: targetSessionId,
432
- } as ReadFileRequest),
433
- headers: {
434
- "Content-Type": "application/json",
435
- },
436
- method: "POST",
437
- });
438
-
439
- if (!response.ok) {
440
- const errorData = (await response.json().catch(() => ({}))) as {
441
- error?: string;
442
- };
443
- throw new Error(
444
- errorData.error || `HTTP error! status: ${response.status}`
445
- );
446
- }
447
-
448
- const data: ReadFileResponse = await response.json();
449
- console.log(
450
- `[HTTP Client] File read: ${path}, Success: ${data.success}, Content length: ${data.content.length}`
451
- );
452
-
453
- return data;
454
- } catch (error) {
455
- console.error("[HTTP Client] Error reading file:", error);
456
- throw error;
457
- }
458
- }
459
-
460
- async deleteFile(
461
- path: string,
462
- sessionId?: string
463
- ): Promise<DeleteFileResponse> {
464
- try {
465
- const targetSessionId = sessionId || this.sessionId;
466
-
467
- const response = await this.doFetch(`/api/delete`, {
468
- body: JSON.stringify({
469
- path,
470
- sessionId: targetSessionId,
471
- } as DeleteFileRequest),
472
- headers: {
473
- "Content-Type": "application/json",
474
- },
475
- method: "POST",
476
- });
477
-
478
- if (!response.ok) {
479
- const errorData = (await response.json().catch(() => ({}))) as {
480
- error?: string;
481
- };
482
- throw new Error(
483
- errorData.error || `HTTP error! status: ${response.status}`
484
- );
485
- }
486
-
487
- const data: DeleteFileResponse = await response.json();
488
- console.log(
489
- `[HTTP Client] File deleted: ${path}, Success: ${data.success}`
490
- );
491
-
492
- return data;
493
- } catch (error) {
494
- console.error("[HTTP Client] Error deleting file:", error);
495
- throw error;
496
- }
497
- }
498
-
499
- async renameFile(
500
- oldPath: string,
501
- newPath: string,
502
- sessionId?: string
503
- ): Promise<RenameFileResponse> {
504
- try {
505
- const targetSessionId = sessionId || this.sessionId;
506
-
507
- const response = await this.doFetch(`/api/rename`, {
508
- body: JSON.stringify({
509
- newPath,
510
- oldPath,
511
- sessionId: targetSessionId,
512
- } as RenameFileRequest),
513
- headers: {
514
- "Content-Type": "application/json",
515
- },
516
- method: "POST",
517
- });
518
-
519
- if (!response.ok) {
520
- const errorData = (await response.json().catch(() => ({}))) as {
521
- error?: string;
522
- };
523
- throw new Error(
524
- errorData.error || `HTTP error! status: ${response.status}`
525
- );
526
- }
527
-
528
- const data: RenameFileResponse = await response.json();
529
- console.log(
530
- `[HTTP Client] File renamed: ${oldPath} -> ${newPath}, Success: ${data.success}`
531
- );
532
-
533
- return data;
534
- } catch (error) {
535
- console.error("[HTTP Client] Error renaming file:", error);
536
- throw error;
537
- }
538
- }
539
-
540
- async moveFile(
541
- sourcePath: string,
542
- destinationPath: string,
543
- sessionId?: string
544
- ): Promise<MoveFileResponse> {
545
- try {
546
- const targetSessionId = sessionId || this.sessionId;
547
-
548
- const response = await this.doFetch(`/api/move`, {
549
- body: JSON.stringify({
550
- destinationPath,
551
- sessionId: targetSessionId,
552
- sourcePath,
553
- } as MoveFileRequest),
554
- headers: {
555
- "Content-Type": "application/json",
556
- },
557
- method: "POST",
558
- });
559
-
560
- if (!response.ok) {
561
- const errorData = (await response.json().catch(() => ({}))) as {
562
- error?: string;
563
- };
564
- throw new Error(
565
- errorData.error || `HTTP error! status: ${response.status}`
566
- );
567
- }
568
-
569
- const data: MoveFileResponse = await response.json();
570
- console.log(
571
- `[HTTP Client] File moved: ${sourcePath} -> ${destinationPath}, Success: ${data.success}`
572
- );
573
-
574
- return data;
575
- } catch (error) {
576
- console.error("[HTTP Client] Error moving file:", error);
577
- throw error;
578
- }
579
- }
580
-
581
- async listFiles(
582
- path: string,
583
- options?: {
584
- recursive?: boolean;
585
- includeHidden?: boolean;
586
- },
587
- sessionId?: string
588
- ): Promise<ListFilesResponse> {
589
- try {
590
- const targetSessionId = sessionId || this.sessionId;
591
-
592
- const response = await this.doFetch(`/api/list-files`, {
593
- body: JSON.stringify({
594
- path,
595
- options,
596
- sessionId: targetSessionId,
597
- } as ListFilesRequest),
598
- headers: {
599
- "Content-Type": "application/json",
600
- },
601
- method: "POST",
602
- });
603
-
604
- if (!response.ok) {
605
- const errorData = (await response.json().catch(() => ({}))) as {
606
- error?: string;
607
- };
608
- throw new Error(
609
- errorData.error || `HTTP error! status: ${response.status}`
610
- );
611
- }
612
-
613
- const data: ListFilesResponse = await response.json();
614
- console.log(
615
- `[HTTP Client] Listed ${data.files.length} files in: ${path}, Success: ${data.success}`
616
- );
617
-
618
- return data;
619
- } catch (error) {
620
- console.error("[HTTP Client] Error listing files:", error);
621
- throw error;
622
- }
623
- }
624
-
625
- async exposePort(port: number, name?: string): Promise<ExposePortResponse> {
626
- try {
627
- const response = await this.doFetch(`/api/expose-port`, {
628
- body: JSON.stringify({
629
- port,
630
- name,
631
- }),
632
- headers: {
633
- "Content-Type": "application/json",
634
- },
635
- method: "POST",
636
- });
637
-
638
- if (!response.ok) {
639
- const errorData = (await response.json().catch(() => ({}))) as {
640
- error?: string;
641
- };
642
- console.log(errorData);
643
- throw new Error(
644
- errorData.error || `HTTP error! status: ${response.status}`
645
- );
646
- }
647
-
648
- const data: ExposePortResponse = await response.json();
649
- console.log(
650
- `[HTTP Client] Port exposed: ${port}${
651
- name ? ` (${name})` : ""
652
- }, Success: ${data.success}`
653
- );
654
-
655
- return data;
656
- } catch (error) {
657
- console.error("[HTTP Client] Error exposing port:", error);
658
- throw error;
659
- }
660
- }
661
-
662
- async unexposePort(port: number): Promise<UnexposePortResponse> {
663
- try {
664
- const response = await this.doFetch(`/api/unexpose-port`, {
665
- body: JSON.stringify({
666
- port,
667
- }),
668
- headers: {
669
- "Content-Type": "application/json",
670
- },
671
- method: "DELETE",
672
- });
673
-
674
- if (!response.ok) {
675
- const errorData = (await response.json().catch(() => ({}))) as {
676
- error?: string;
677
- };
678
- throw new Error(
679
- errorData.error || `HTTP error! status: ${response.status}`
680
- );
681
- }
682
-
683
- const data: UnexposePortResponse = await response.json();
684
- console.log(
685
- `[HTTP Client] Port unexposed: ${port}, Success: ${data.success}`
686
- );
687
-
688
- return data;
689
- } catch (error) {
690
- console.error("[HTTP Client] Error unexposing port:", error);
691
- throw error;
692
- }
693
- }
694
-
695
- async getExposedPorts(): Promise<GetExposedPortsResponse> {
696
- try {
697
- const response = await this.doFetch(`/api/exposed-ports`, {
698
- headers: {
699
- "Content-Type": "application/json",
700
- },
701
- method: "GET",
702
- });
703
-
704
- if (!response.ok) {
705
- const errorData = (await response.json().catch(() => ({}))) as {
706
- error?: string;
707
- };
708
- throw new Error(
709
- errorData.error || `HTTP error! status: ${response.status}`
710
- );
711
- }
712
-
713
- const data: GetExposedPortsResponse = await response.json();
714
- console.log(`[HTTP Client] Got ${data.count} exposed ports`);
715
-
716
- return data;
717
- } catch (error) {
718
- console.error("[HTTP Client] Error getting exposed ports:", error);
719
- throw error;
720
- }
721
- }
722
-
723
- async ping(): Promise<string> {
724
- try {
725
- const response = await this.doFetch(`/api/ping`, {
726
- headers: {
727
- "Content-Type": "application/json",
728
- },
729
- method: "GET",
730
- });
731
-
732
- if (!response.ok) {
733
- throw new Error(`HTTP error! status: ${response.status}`);
734
- }
735
-
736
- const data: PingResponse = await response.json();
737
- console.log(`[HTTP Client] Ping response: ${data.message}`);
738
- return data.timestamp;
739
- } catch (error) {
740
- console.error("[HTTP Client] Error pinging server:", error);
741
- throw error;
742
- }
743
- }
744
-
745
- async getCommands(): Promise<string[]> {
746
- try {
747
- const response = await fetch(`${this.baseUrl}/api/commands`, {
748
- headers: {
749
- "Content-Type": "application/json",
750
- },
751
- method: "GET",
752
- });
753
-
754
- if (!response.ok) {
755
- throw new Error(`HTTP error! status: ${response.status}`);
756
- }
757
-
758
- const data: CommandsResponse = await response.json();
759
- console.log(
760
- `[HTTP Client] Available commands: ${data.availableCommands.length}`
761
- );
762
- return data.availableCommands;
763
- } catch (error) {
764
- console.error("[HTTP Client] Error getting commands:", error);
765
- throw error;
766
- }
767
- }
768
-
769
- getSessionId(): string | null {
770
- return this.sessionId;
771
- }
772
-
773
- setSessionId(sessionId: string): void {
774
- this.sessionId = sessionId;
775
- }
776
-
777
- clearSession(): void {
778
- this.sessionId = null;
779
- }
780
-
781
- // Process management methods
782
- async startProcess(
783
- command: string,
784
- options?: {
785
- processId?: string;
786
- sessionId?: string;
787
- timeout?: number;
788
- env?: Record<string, string>;
789
- cwd?: string;
790
- encoding?: string;
791
- autoCleanup?: boolean;
792
- }
793
- ): Promise<StartProcessResponse> {
794
- try {
795
- const targetSessionId = options?.sessionId || this.sessionId;
796
-
797
- const response = await this.doFetch("/api/process/start", {
798
- body: JSON.stringify({
799
- command,
800
- options: {
801
- ...options,
802
- sessionId: targetSessionId,
803
- },
804
- } as StartProcessRequest),
805
- headers: {
806
- "Content-Type": "application/json",
807
- },
808
- method: "POST",
809
- });
810
-
811
- if (!response.ok) {
812
- const errorData = (await response.json().catch(() => ({}))) as {
813
- error?: string;
814
- };
815
- throw new Error(
816
- errorData.error || `HTTP error! status: ${response.status}`
817
- );
818
- }
819
-
820
- const data: StartProcessResponse = await response.json();
821
- console.log(
822
- `[HTTP Client] Process started: ${command}, ID: ${data.process.id}`
823
- );
824
-
825
- return data;
826
- } catch (error) {
827
- console.error("[HTTP Client] Error starting process:", error);
828
- throw error;
829
- }
830
- }
831
-
832
- async listProcesses(): Promise<ListProcessesResponse> {
833
- try {
834
- const response = await this.doFetch("/api/process/list", {
835
- headers: {
836
- "Content-Type": "application/json",
837
- },
838
- method: "GET",
839
- });
840
-
841
- if (!response.ok) {
842
- const errorData = (await response.json().catch(() => ({}))) as {
843
- error?: string;
844
- };
845
- throw new Error(
846
- errorData.error || `HTTP error! status: ${response.status}`
847
- );
848
- }
849
-
850
- const data: ListProcessesResponse = await response.json();
851
- console.log(`[HTTP Client] Listed ${data.processes.length} processes`);
852
-
853
- return data;
854
- } catch (error) {
855
- console.error("[HTTP Client] Error listing processes:", error);
856
- throw error;
857
- }
858
- }
859
-
860
- async getProcess(processId: string): Promise<GetProcessResponse> {
861
- try {
862
- const response = await this.doFetch(`/api/process/${processId}`, {
863
- headers: {
864
- "Content-Type": "application/json",
865
- },
866
- method: "GET",
867
- });
868
-
869
- if (!response.ok) {
870
- const errorData = (await response.json().catch(() => ({}))) as {
871
- error?: string;
872
- };
873
- throw new Error(
874
- errorData.error || `HTTP error! status: ${response.status}`
875
- );
876
- }
877
-
878
- const data: GetProcessResponse = await response.json();
879
- console.log(
880
- `[HTTP Client] Got process ${processId}: ${
881
- data.process?.status || "not found"
882
- }`
883
- );
884
-
885
- return data;
886
- } catch (error) {
887
- console.error("[HTTP Client] Error getting process:", error);
888
- throw error;
889
- }
890
- }
891
-
892
- async killProcess(
893
- processId: string
894
- ): Promise<{ success: boolean; message: string }> {
895
- try {
896
- const response = await this.doFetch(`/api/process/${processId}`, {
897
- headers: {
898
- "Content-Type": "application/json",
899
- },
900
- method: "DELETE",
901
- });
902
-
903
- if (!response.ok) {
904
- const errorData = (await response.json().catch(() => ({}))) as {
905
- error?: string;
906
- };
907
- throw new Error(
908
- errorData.error || `HTTP error! status: ${response.status}`
909
- );
910
- }
911
-
912
- const data = (await response.json()) as {
913
- success: boolean;
914
- message: string;
915
- };
916
- console.log(`[HTTP Client] Killed process ${processId}`);
917
-
918
- return data;
919
- } catch (error) {
920
- console.error("[HTTP Client] Error killing process:", error);
921
- throw error;
922
- }
923
- }
924
-
925
- async killAllProcesses(): Promise<{
926
- success: boolean;
927
- killedCount: number;
928
- message: string;
929
- }> {
930
- try {
931
- const response = await this.doFetch("/api/process/kill-all", {
932
- headers: {
933
- "Content-Type": "application/json",
934
- },
935
- method: "DELETE",
936
- });
937
-
938
- if (!response.ok) {
939
- const errorData = (await response.json().catch(() => ({}))) as {
940
- error?: string;
941
- };
942
- throw new Error(
943
- errorData.error || `HTTP error! status: ${response.status}`
944
- );
945
- }
946
-
947
- const data = (await response.json()) as {
948
- success: boolean;
949
- killedCount: number;
950
- message: string;
951
- };
952
- console.log(`[HTTP Client] Killed ${data.killedCount} processes`);
953
-
954
- return data;
955
- } catch (error) {
956
- console.error("[HTTP Client] Error killing all processes:", error);
957
- throw error;
958
- }
959
- }
960
-
961
- async getProcessLogs(processId: string): Promise<GetProcessLogsResponse> {
962
- try {
963
- const response = await this.doFetch(`/api/process/${processId}/logs`, {
964
- headers: {
965
- "Content-Type": "application/json",
966
- },
967
- method: "GET",
968
- });
969
-
970
- if (!response.ok) {
971
- const errorData = (await response.json().catch(() => ({}))) as {
972
- error?: string;
973
- };
974
- throw new Error(
975
- errorData.error || `HTTP error! status: ${response.status}`
976
- );
977
- }
978
-
979
- const data: GetProcessLogsResponse = await response.json();
980
- console.log(`[HTTP Client] Got logs for process ${processId}`);
981
-
982
- return data;
983
- } catch (error) {
984
- console.error("[HTTP Client] Error getting process logs:", error);
985
- throw error;
986
- }
987
- }
988
-
989
- async streamProcessLogs(
990
- processId: string
991
- ): Promise<ReadableStream<Uint8Array>> {
992
- try {
993
- const response = await this.doFetch(`/api/process/${processId}/stream`, {
994
- headers: {
995
- Accept: "text/event-stream",
996
- "Cache-Control": "no-cache",
997
- },
998
- method: "GET",
999
- });
1000
-
1001
- if (!response.ok) {
1002
- const errorData = (await response.json().catch(() => ({}))) as {
1003
- error?: string;
1004
- };
1005
- throw new Error(
1006
- errorData.error || `HTTP error! status: ${response.status}`
1007
- );
1008
- }
1009
-
1010
- if (!response.body) {
1011
- throw new Error("No response body for streaming request");
1012
- }
1013
-
1014
- console.log(
1015
- `[HTTP Client] Started streaming logs for process ${processId}`
1016
- );
1017
-
1018
- return response.body;
1019
- } catch (error) {
1020
- console.error("[HTTP Client] Error streaming process logs:", error);
1021
- throw error;
1022
- }
1023
- }
1024
- }