@cloudflare/sandbox 0.0.9 → 0.1.0

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 (56) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/Dockerfile +1 -14
  3. package/container_src/handler/exec.ts +337 -0
  4. package/container_src/handler/file.ts +844 -0
  5. package/container_src/handler/git.ts +182 -0
  6. package/container_src/handler/ports.ts +314 -0
  7. package/container_src/handler/process.ts +640 -0
  8. package/container_src/index.ts +82 -2973
  9. package/container_src/types.ts +103 -0
  10. package/dist/chunk-6THNBO4S.js +46 -0
  11. package/dist/chunk-6THNBO4S.js.map +1 -0
  12. package/dist/chunk-6UAWTJ5S.js +85 -0
  13. package/dist/chunk-6UAWTJ5S.js.map +1 -0
  14. package/dist/chunk-G4XT4SP7.js +638 -0
  15. package/dist/chunk-G4XT4SP7.js.map +1 -0
  16. package/dist/chunk-ISFOIYQC.js +585 -0
  17. package/dist/chunk-ISFOIYQC.js.map +1 -0
  18. package/dist/chunk-NNGBXDMY.js +89 -0
  19. package/dist/chunk-NNGBXDMY.js.map +1 -0
  20. package/dist/client-Da-mLX4p.d.ts +210 -0
  21. package/dist/client.d.ts +2 -1
  22. package/dist/client.js +3 -37
  23. package/dist/index.d.ts +3 -1
  24. package/dist/index.js +13 -3
  25. package/dist/request-handler.d.ts +2 -1
  26. package/dist/request-handler.js +4 -2
  27. package/dist/sandbox.d.ts +2 -1
  28. package/dist/sandbox.js +4 -2
  29. package/dist/security.d.ts +30 -0
  30. package/dist/security.js +13 -0
  31. package/dist/security.js.map +1 -0
  32. package/dist/sse-parser.d.ts +28 -0
  33. package/dist/sse-parser.js +11 -0
  34. package/dist/sse-parser.js.map +1 -0
  35. package/dist/types.d.ts +284 -0
  36. package/dist/types.js +19 -0
  37. package/dist/types.js.map +1 -0
  38. package/package.json +2 -7
  39. package/src/client.ts +235 -1286
  40. package/src/index.ts +6 -0
  41. package/src/request-handler.ts +69 -20
  42. package/src/sandbox.ts +463 -70
  43. package/src/security.ts +113 -0
  44. package/src/sse-parser.ts +147 -0
  45. package/src/types.ts +386 -0
  46. package/README.md +0 -65
  47. package/dist/chunk-4J5LQCCN.js +0 -1446
  48. package/dist/chunk-4J5LQCCN.js.map +0 -1
  49. package/dist/chunk-5SZ3RVJZ.js +0 -250
  50. package/dist/chunk-5SZ3RVJZ.js.map +0 -1
  51. package/dist/client-BuVjqV00.d.ts +0 -247
  52. package/tests/client.example.ts +0 -308
  53. package/tests/connection-test.ts +0 -81
  54. package/tests/simple-test.ts +0 -81
  55. package/tests/test1.ts +0 -281
  56. package/tests/test2.ts +0 -929
@@ -1,1446 +0,0 @@
1
- // src/client.ts
2
- var HttpClient = class {
3
- baseUrl;
4
- options;
5
- sessionId = null;
6
- constructor(options = {}) {
7
- this.options = {
8
- ...options
9
- };
10
- this.baseUrl = this.options.baseUrl;
11
- }
12
- async doFetch(path, options) {
13
- const url = this.options.stub ? `http://localhost:${this.options.port}${path}` : `${this.baseUrl}${path}`;
14
- const method = options?.method || "GET";
15
- console.log(`[HTTP Client] Making ${method} request to ${url}`);
16
- try {
17
- let response;
18
- if (this.options.stub) {
19
- response = await this.options.stub.containerFetch(
20
- url,
21
- options,
22
- this.options.port
23
- );
24
- } else {
25
- response = await fetch(url, options);
26
- }
27
- console.log(
28
- `[HTTP Client] Response: ${response.status} ${response.statusText}`
29
- );
30
- if (!response.ok) {
31
- console.error(
32
- `[HTTP Client] Request failed: ${method} ${url} - ${response.status} ${response.statusText}`
33
- );
34
- }
35
- return response;
36
- } catch (error) {
37
- console.error(`[HTTP Client] Request error: ${method} ${url}`, error);
38
- throw error;
39
- }
40
- }
41
- // Public methods to set event handlers
42
- setOnOutput(handler) {
43
- this.options.onOutput = handler;
44
- }
45
- setOnCommandComplete(handler) {
46
- this.options.onCommandComplete = handler;
47
- }
48
- setOnStreamEvent(handler) {
49
- this.options.onStreamEvent = handler;
50
- }
51
- // Public getter methods
52
- getOnOutput() {
53
- return this.options.onOutput;
54
- }
55
- getOnCommandComplete() {
56
- return this.options.onCommandComplete;
57
- }
58
- getOnStreamEvent() {
59
- return this.options.onStreamEvent;
60
- }
61
- async createSession() {
62
- try {
63
- const response = await this.doFetch(`/api/session/create`, {
64
- headers: {
65
- "Content-Type": "application/json"
66
- },
67
- method: "POST"
68
- });
69
- if (!response.ok) {
70
- throw new Error(`HTTP error! status: ${response.status}`);
71
- }
72
- const data = await response.json();
73
- this.sessionId = data.sessionId;
74
- console.log(`[HTTP Client] Created session: ${this.sessionId}`);
75
- return this.sessionId;
76
- } catch (error) {
77
- console.error("[HTTP Client] Error creating session:", error);
78
- throw error;
79
- }
80
- }
81
- async listSessions() {
82
- try {
83
- const response = await this.doFetch(`/api/session/list`, {
84
- headers: {
85
- "Content-Type": "application/json"
86
- },
87
- method: "GET"
88
- });
89
- if (!response.ok) {
90
- throw new Error(`HTTP error! status: ${response.status}`);
91
- }
92
- const data = await response.json();
93
- console.log(`[HTTP Client] Listed ${data.count} sessions`);
94
- return data;
95
- } catch (error) {
96
- console.error("[HTTP Client] Error listing sessions:", error);
97
- throw error;
98
- }
99
- }
100
- async execute(command, args = [], sessionId, background = false) {
101
- try {
102
- const targetSessionId = sessionId || this.sessionId;
103
- const response = await this.doFetch(`/api/execute`, {
104
- body: JSON.stringify({
105
- args,
106
- command,
107
- background,
108
- sessionId: targetSessionId
109
- }),
110
- headers: {
111
- "Content-Type": "application/json"
112
- },
113
- method: "POST"
114
- });
115
- if (!response.ok) {
116
- const errorData = await response.json().catch(() => ({}));
117
- throw new Error(
118
- errorData.error || `HTTP error! status: ${response.status}`
119
- );
120
- }
121
- const data = await response.json();
122
- console.log(
123
- `[HTTP Client] Command executed: ${command}, Success: ${data.success}`
124
- );
125
- this.options.onCommandComplete?.(
126
- data.success,
127
- data.exitCode,
128
- data.stdout,
129
- data.stderr,
130
- data.command,
131
- data.args
132
- );
133
- return data;
134
- } catch (error) {
135
- console.error("[HTTP Client] Error executing command:", error);
136
- this.options.onError?.(
137
- error instanceof Error ? error.message : "Unknown error",
138
- command,
139
- args
140
- );
141
- throw error;
142
- }
143
- }
144
- async executeStream(command, args = [], sessionId, background = false) {
145
- try {
146
- const targetSessionId = sessionId || this.sessionId;
147
- const response = await this.doFetch(`/api/execute/stream`, {
148
- body: JSON.stringify({
149
- args,
150
- command,
151
- background,
152
- sessionId: targetSessionId
153
- }),
154
- headers: {
155
- "Content-Type": "application/json"
156
- },
157
- method: "POST"
158
- });
159
- if (!response.ok) {
160
- const errorData = await response.json().catch(() => ({}));
161
- throw new Error(
162
- errorData.error || `HTTP error! status: ${response.status}`
163
- );
164
- }
165
- if (!response.body) {
166
- throw new Error("No response body for streaming request");
167
- }
168
- const reader = response.body.getReader();
169
- const decoder = new TextDecoder();
170
- try {
171
- while (true) {
172
- const { done, value } = await reader.read();
173
- if (done) {
174
- break;
175
- }
176
- const chunk = decoder.decode(value, { stream: true });
177
- const lines = chunk.split("\n");
178
- for (const line of lines) {
179
- if (line.startsWith("data: ")) {
180
- try {
181
- const eventData = line.slice(6);
182
- const event = JSON.parse(eventData);
183
- console.log(`[HTTP Client] Stream event: ${event.type}`);
184
- this.options.onStreamEvent?.(event);
185
- switch (event.type) {
186
- case "command_start":
187
- console.log(
188
- `[HTTP Client] Command started: ${event.command} ${event.args?.join(" ")}`
189
- );
190
- this.options.onCommandStart?.(
191
- event.command,
192
- event.args || []
193
- );
194
- break;
195
- case "output":
196
- console.log(`[${event.stream}] ${event.data}`);
197
- this.options.onOutput?.(
198
- event.stream,
199
- event.data,
200
- event.command
201
- );
202
- break;
203
- case "command_complete":
204
- console.log(
205
- `[HTTP Client] Command completed: ${event.command}, Success: ${event.success}, Exit code: ${event.exitCode}`
206
- );
207
- this.options.onCommandComplete?.(
208
- event.success,
209
- event.exitCode,
210
- event.stdout,
211
- event.stderr,
212
- event.command,
213
- event.args || []
214
- );
215
- break;
216
- case "error":
217
- console.error(
218
- `[HTTP Client] Command error: ${event.error}`
219
- );
220
- this.options.onError?.(
221
- event.error,
222
- event.command,
223
- event.args
224
- );
225
- break;
226
- }
227
- } catch (parseError) {
228
- console.warn(
229
- "[HTTP Client] Failed to parse stream event:",
230
- parseError
231
- );
232
- }
233
- }
234
- }
235
- }
236
- } finally {
237
- reader.releaseLock();
238
- }
239
- } catch (error) {
240
- console.error("[HTTP Client] Error in streaming execution:", error);
241
- this.options.onError?.(
242
- error instanceof Error ? error.message : "Unknown error",
243
- command,
244
- args
245
- );
246
- throw error;
247
- }
248
- }
249
- async gitCheckout(repoUrl, branch = "main", targetDir, sessionId) {
250
- try {
251
- const targetSessionId = sessionId || this.sessionId;
252
- const response = await this.doFetch(`/api/git/checkout`, {
253
- body: JSON.stringify({
254
- branch,
255
- repoUrl,
256
- sessionId: targetSessionId,
257
- targetDir
258
- }),
259
- headers: {
260
- "Content-Type": "application/json"
261
- },
262
- method: "POST"
263
- });
264
- if (!response.ok) {
265
- const errorData = await response.json().catch(() => ({}));
266
- throw new Error(
267
- errorData.error || `HTTP error! status: ${response.status}`
268
- );
269
- }
270
- const data = await response.json();
271
- console.log(
272
- `[HTTP Client] Git checkout completed: ${repoUrl}, Success: ${data.success}, Target: ${data.targetDir}`
273
- );
274
- return data;
275
- } catch (error) {
276
- console.error("[HTTP Client] Error in git checkout:", error);
277
- throw error;
278
- }
279
- }
280
- async gitCheckoutStream(repoUrl, branch = "main", targetDir, sessionId) {
281
- try {
282
- const targetSessionId = sessionId || this.sessionId;
283
- const response = await this.doFetch(`/api/git/checkout/stream`, {
284
- body: JSON.stringify({
285
- branch,
286
- repoUrl,
287
- sessionId: targetSessionId,
288
- targetDir
289
- }),
290
- headers: {
291
- "Content-Type": "application/json"
292
- },
293
- method: "POST"
294
- });
295
- if (!response.ok) {
296
- const errorData = await response.json().catch(() => ({}));
297
- throw new Error(
298
- errorData.error || `HTTP error! status: ${response.status}`
299
- );
300
- }
301
- if (!response.body) {
302
- throw new Error("No response body for streaming request");
303
- }
304
- const reader = response.body.getReader();
305
- const decoder = new TextDecoder();
306
- try {
307
- while (true) {
308
- const { done, value } = await reader.read();
309
- if (done) {
310
- break;
311
- }
312
- const chunk = decoder.decode(value, { stream: true });
313
- const lines = chunk.split("\n");
314
- for (const line of lines) {
315
- if (line.startsWith("data: ")) {
316
- try {
317
- const eventData = line.slice(6);
318
- const event = JSON.parse(eventData);
319
- console.log(
320
- `[HTTP Client] Git checkout stream event: ${event.type}`
321
- );
322
- this.options.onStreamEvent?.(event);
323
- switch (event.type) {
324
- case "command_start":
325
- console.log(
326
- `[HTTP Client] Git checkout started: ${event.command} ${event.args?.join(" ")}`
327
- );
328
- this.options.onCommandStart?.(
329
- event.command,
330
- event.args || []
331
- );
332
- break;
333
- case "output":
334
- console.log(`[${event.stream}] ${event.data}`);
335
- this.options.onOutput?.(
336
- event.stream,
337
- event.data,
338
- event.command
339
- );
340
- break;
341
- case "command_complete":
342
- console.log(
343
- `[HTTP Client] Git checkout completed: ${event.command}, Success: ${event.success}, Exit code: ${event.exitCode}`
344
- );
345
- this.options.onCommandComplete?.(
346
- event.success,
347
- event.exitCode,
348
- event.stdout,
349
- event.stderr,
350
- event.command,
351
- event.args || []
352
- );
353
- break;
354
- case "error":
355
- console.error(
356
- `[HTTP Client] Git checkout error: ${event.error}`
357
- );
358
- this.options.onError?.(
359
- event.error,
360
- event.command,
361
- event.args
362
- );
363
- break;
364
- }
365
- } catch (parseError) {
366
- console.warn(
367
- "[HTTP Client] Failed to parse git checkout stream event:",
368
- parseError
369
- );
370
- }
371
- }
372
- }
373
- }
374
- } finally {
375
- reader.releaseLock();
376
- }
377
- } catch (error) {
378
- console.error("[HTTP Client] Error in streaming git checkout:", error);
379
- this.options.onError?.(
380
- error instanceof Error ? error.message : "Unknown error",
381
- "git clone",
382
- [branch, repoUrl, targetDir || ""]
383
- );
384
- throw error;
385
- }
386
- }
387
- async mkdir(path, recursive = false, sessionId) {
388
- try {
389
- const targetSessionId = sessionId || this.sessionId;
390
- const response = await this.doFetch(`/api/mkdir`, {
391
- body: JSON.stringify({
392
- path,
393
- recursive,
394
- sessionId: targetSessionId
395
- }),
396
- headers: {
397
- "Content-Type": "application/json"
398
- },
399
- method: "POST"
400
- });
401
- if (!response.ok) {
402
- const errorData = await response.json().catch(() => ({}));
403
- throw new Error(
404
- errorData.error || `HTTP error! status: ${response.status}`
405
- );
406
- }
407
- const data = await response.json();
408
- console.log(
409
- `[HTTP Client] Directory created: ${path}, Success: ${data.success}, Recursive: ${data.recursive}`
410
- );
411
- return data;
412
- } catch (error) {
413
- console.error("[HTTP Client] Error creating directory:", error);
414
- throw error;
415
- }
416
- }
417
- async mkdirStream(path, recursive = false, sessionId) {
418
- try {
419
- const targetSessionId = sessionId || this.sessionId;
420
- const response = await this.doFetch(`/api/mkdir/stream`, {
421
- body: JSON.stringify({
422
- path,
423
- recursive,
424
- sessionId: targetSessionId
425
- }),
426
- headers: {
427
- "Content-Type": "application/json"
428
- },
429
- method: "POST"
430
- });
431
- if (!response.ok) {
432
- const errorData = await response.json().catch(() => ({}));
433
- throw new Error(
434
- errorData.error || `HTTP error! status: ${response.status}`
435
- );
436
- }
437
- if (!response.body) {
438
- throw new Error("No response body for streaming request");
439
- }
440
- const reader = response.body.getReader();
441
- const decoder = new TextDecoder();
442
- try {
443
- while (true) {
444
- const { done, value } = await reader.read();
445
- if (done) {
446
- break;
447
- }
448
- const chunk = decoder.decode(value, { stream: true });
449
- const lines = chunk.split("\n");
450
- for (const line of lines) {
451
- if (line.startsWith("data: ")) {
452
- try {
453
- const eventData = line.slice(6);
454
- const event = JSON.parse(eventData);
455
- console.log(`[HTTP Client] Mkdir stream event: ${event.type}`);
456
- this.options.onStreamEvent?.(event);
457
- switch (event.type) {
458
- case "command_start":
459
- console.log(
460
- `[HTTP Client] Mkdir started: ${event.command} ${event.args?.join(" ")}`
461
- );
462
- this.options.onCommandStart?.(
463
- event.command,
464
- event.args || []
465
- );
466
- break;
467
- case "output":
468
- console.log(`[${event.stream}] ${event.data}`);
469
- this.options.onOutput?.(
470
- event.stream,
471
- event.data,
472
- event.command
473
- );
474
- break;
475
- case "command_complete":
476
- console.log(
477
- `[HTTP Client] Mkdir completed: ${event.command}, Success: ${event.success}, Exit code: ${event.exitCode}`
478
- );
479
- this.options.onCommandComplete?.(
480
- event.success,
481
- event.exitCode,
482
- event.stdout,
483
- event.stderr,
484
- event.command,
485
- event.args || []
486
- );
487
- break;
488
- case "error":
489
- console.error(`[HTTP Client] Mkdir error: ${event.error}`);
490
- this.options.onError?.(
491
- event.error,
492
- event.command,
493
- event.args
494
- );
495
- break;
496
- }
497
- } catch (parseError) {
498
- console.warn(
499
- "[HTTP Client] Failed to parse mkdir stream event:",
500
- parseError
501
- );
502
- }
503
- }
504
- }
505
- }
506
- } finally {
507
- reader.releaseLock();
508
- }
509
- } catch (error) {
510
- console.error("[HTTP Client] Error in streaming mkdir:", error);
511
- this.options.onError?.(
512
- error instanceof Error ? error.message : "Unknown error",
513
- "mkdir",
514
- recursive ? ["-p", path] : [path]
515
- );
516
- throw error;
517
- }
518
- }
519
- async writeFile(path, content, encoding = "utf-8", sessionId) {
520
- try {
521
- const targetSessionId = sessionId || this.sessionId;
522
- const response = await this.doFetch(`/api/write`, {
523
- body: JSON.stringify({
524
- content,
525
- encoding,
526
- path,
527
- sessionId: targetSessionId
528
- }),
529
- headers: {
530
- "Content-Type": "application/json"
531
- },
532
- method: "POST"
533
- });
534
- if (!response.ok) {
535
- const errorData = await response.json().catch(() => ({}));
536
- throw new Error(
537
- errorData.error || `HTTP error! status: ${response.status}`
538
- );
539
- }
540
- const data = await response.json();
541
- console.log(
542
- `[HTTP Client] File written: ${path}, Success: ${data.success}`
543
- );
544
- return data;
545
- } catch (error) {
546
- console.error("[HTTP Client] Error writing file:", error);
547
- throw error;
548
- }
549
- }
550
- async writeFileStream(path, content, encoding = "utf-8", sessionId) {
551
- try {
552
- const targetSessionId = sessionId || this.sessionId;
553
- const response = await this.doFetch(`/api/write/stream`, {
554
- body: JSON.stringify({
555
- content,
556
- encoding,
557
- path,
558
- sessionId: targetSessionId
559
- }),
560
- headers: {
561
- "Content-Type": "application/json"
562
- },
563
- method: "POST"
564
- });
565
- if (!response.ok) {
566
- const errorData = await response.json().catch(() => ({}));
567
- throw new Error(
568
- errorData.error || `HTTP error! status: ${response.status}`
569
- );
570
- }
571
- if (!response.body) {
572
- throw new Error("No response body for streaming request");
573
- }
574
- const reader = response.body.getReader();
575
- const decoder = new TextDecoder();
576
- try {
577
- while (true) {
578
- const { done, value } = await reader.read();
579
- if (done) {
580
- break;
581
- }
582
- const chunk = decoder.decode(value, { stream: true });
583
- const lines = chunk.split("\n");
584
- for (const line of lines) {
585
- if (line.startsWith("data: ")) {
586
- try {
587
- const eventData = line.slice(6);
588
- const event = JSON.parse(eventData);
589
- console.log(
590
- `[HTTP Client] Write file stream event: ${event.type}`
591
- );
592
- this.options.onStreamEvent?.(event);
593
- switch (event.type) {
594
- case "command_start":
595
- console.log(
596
- `[HTTP Client] Write file started: ${event.path}`
597
- );
598
- this.options.onCommandStart?.("write", [
599
- path,
600
- content,
601
- encoding
602
- ]);
603
- break;
604
- case "output":
605
- console.log(`[output] ${event.message}`);
606
- this.options.onOutput?.("stdout", event.message, "write");
607
- break;
608
- case "command_complete":
609
- console.log(
610
- `[HTTP Client] Write file completed: ${event.path}, Success: ${event.success}`
611
- );
612
- this.options.onCommandComplete?.(
613
- event.success,
614
- 0,
615
- "",
616
- "",
617
- "write",
618
- [path, content, encoding]
619
- );
620
- break;
621
- case "error":
622
- console.error(
623
- `[HTTP Client] Write file error: ${event.error}`
624
- );
625
- this.options.onError?.(event.error, "write", [
626
- path,
627
- content,
628
- encoding
629
- ]);
630
- break;
631
- }
632
- } catch (parseError) {
633
- console.warn(
634
- "[HTTP Client] Failed to parse write file stream event:",
635
- parseError
636
- );
637
- }
638
- }
639
- }
640
- }
641
- } finally {
642
- reader.releaseLock();
643
- }
644
- } catch (error) {
645
- console.error("[HTTP Client] Error in streaming write file:", error);
646
- this.options.onError?.(
647
- error instanceof Error ? error.message : "Unknown error",
648
- "write",
649
- [path, content, encoding]
650
- );
651
- throw error;
652
- }
653
- }
654
- async readFile(path, encoding = "utf-8", sessionId) {
655
- try {
656
- const targetSessionId = sessionId || this.sessionId;
657
- const response = await this.doFetch(`/api/read`, {
658
- body: JSON.stringify({
659
- encoding,
660
- path,
661
- sessionId: targetSessionId
662
- }),
663
- headers: {
664
- "Content-Type": "application/json"
665
- },
666
- method: "POST"
667
- });
668
- if (!response.ok) {
669
- const errorData = await response.json().catch(() => ({}));
670
- throw new Error(
671
- errorData.error || `HTTP error! status: ${response.status}`
672
- );
673
- }
674
- const data = await response.json();
675
- console.log(
676
- `[HTTP Client] File read: ${path}, Success: ${data.success}, Content length: ${data.content.length}`
677
- );
678
- return data;
679
- } catch (error) {
680
- console.error("[HTTP Client] Error reading file:", error);
681
- throw error;
682
- }
683
- }
684
- async readFileStream(path, encoding = "utf-8", sessionId) {
685
- try {
686
- const targetSessionId = sessionId || this.sessionId;
687
- const response = await this.doFetch(`/api/read/stream`, {
688
- body: JSON.stringify({
689
- encoding,
690
- path,
691
- sessionId: targetSessionId
692
- }),
693
- headers: {
694
- "Content-Type": "application/json"
695
- },
696
- method: "POST"
697
- });
698
- if (!response.ok) {
699
- const errorData = await response.json().catch(() => ({}));
700
- throw new Error(
701
- errorData.error || `HTTP error! status: ${response.status}`
702
- );
703
- }
704
- if (!response.body) {
705
- throw new Error("No response body for streaming request");
706
- }
707
- const reader = response.body.getReader();
708
- const decoder = new TextDecoder();
709
- try {
710
- while (true) {
711
- const { done, value } = await reader.read();
712
- if (done) {
713
- break;
714
- }
715
- const chunk = decoder.decode(value, { stream: true });
716
- const lines = chunk.split("\n");
717
- for (const line of lines) {
718
- if (line.startsWith("data: ")) {
719
- try {
720
- const eventData = line.slice(6);
721
- const event = JSON.parse(eventData);
722
- console.log(
723
- `[HTTP Client] Read file stream event: ${event.type}`
724
- );
725
- this.options.onStreamEvent?.(event);
726
- switch (event.type) {
727
- case "command_start":
728
- console.log(
729
- `[HTTP Client] Read file started: ${event.path}`
730
- );
731
- this.options.onCommandStart?.("read", [path, encoding]);
732
- break;
733
- case "command_complete":
734
- console.log(
735
- `[HTTP Client] Read file completed: ${event.path}, Success: ${event.success}, Content length: ${event.content?.length || 0}`
736
- );
737
- this.options.onCommandComplete?.(
738
- event.success,
739
- 0,
740
- event.content || "",
741
- "",
742
- "read",
743
- [path, encoding]
744
- );
745
- break;
746
- case "error":
747
- console.error(
748
- `[HTTP Client] Read file error: ${event.error}`
749
- );
750
- this.options.onError?.(event.error, "read", [
751
- path,
752
- encoding
753
- ]);
754
- break;
755
- }
756
- } catch (parseError) {
757
- console.warn(
758
- "[HTTP Client] Failed to parse read file stream event:",
759
- parseError
760
- );
761
- }
762
- }
763
- }
764
- }
765
- } finally {
766
- reader.releaseLock();
767
- }
768
- } catch (error) {
769
- console.error("[HTTP Client] Error in streaming read file:", error);
770
- this.options.onError?.(
771
- error instanceof Error ? error.message : "Unknown error",
772
- "read",
773
- [path, encoding]
774
- );
775
- throw error;
776
- }
777
- }
778
- async deleteFile(path, sessionId) {
779
- try {
780
- const targetSessionId = sessionId || this.sessionId;
781
- const response = await this.doFetch(`/api/delete`, {
782
- body: JSON.stringify({
783
- path,
784
- sessionId: targetSessionId
785
- }),
786
- headers: {
787
- "Content-Type": "application/json"
788
- },
789
- method: "POST"
790
- });
791
- if (!response.ok) {
792
- const errorData = await response.json().catch(() => ({}));
793
- throw new Error(
794
- errorData.error || `HTTP error! status: ${response.status}`
795
- );
796
- }
797
- const data = await response.json();
798
- console.log(
799
- `[HTTP Client] File deleted: ${path}, Success: ${data.success}`
800
- );
801
- return data;
802
- } catch (error) {
803
- console.error("[HTTP Client] Error deleting file:", error);
804
- throw error;
805
- }
806
- }
807
- async deleteFileStream(path, sessionId) {
808
- try {
809
- const targetSessionId = sessionId || this.sessionId;
810
- const response = await this.doFetch(`/api/delete/stream`, {
811
- body: JSON.stringify({
812
- path,
813
- sessionId: targetSessionId
814
- }),
815
- headers: {
816
- "Content-Type": "application/json"
817
- },
818
- method: "POST"
819
- });
820
- if (!response.ok) {
821
- const errorData = await response.json().catch(() => ({}));
822
- throw new Error(
823
- errorData.error || `HTTP error! status: ${response.status}`
824
- );
825
- }
826
- if (!response.body) {
827
- throw new Error("No response body for streaming request");
828
- }
829
- const reader = response.body.getReader();
830
- const decoder = new TextDecoder();
831
- try {
832
- while (true) {
833
- const { done, value } = await reader.read();
834
- if (done) {
835
- break;
836
- }
837
- const chunk = decoder.decode(value, { stream: true });
838
- const lines = chunk.split("\n");
839
- for (const line of lines) {
840
- if (line.startsWith("data: ")) {
841
- try {
842
- const eventData = line.slice(6);
843
- const event = JSON.parse(eventData);
844
- console.log(
845
- `[HTTP Client] Delete file stream event: ${event.type}`
846
- );
847
- this.options.onStreamEvent?.(event);
848
- switch (event.type) {
849
- case "command_start":
850
- console.log(
851
- `[HTTP Client] Delete file started: ${event.path}`
852
- );
853
- this.options.onCommandStart?.("delete", [path]);
854
- break;
855
- case "command_complete":
856
- console.log(
857
- `[HTTP Client] Delete file completed: ${event.path}, Success: ${event.success}`
858
- );
859
- this.options.onCommandComplete?.(
860
- event.success,
861
- 0,
862
- "",
863
- "",
864
- "delete",
865
- [path]
866
- );
867
- break;
868
- case "error":
869
- console.error(
870
- `[HTTP Client] Delete file error: ${event.error}`
871
- );
872
- this.options.onError?.(event.error, "delete", [path]);
873
- break;
874
- }
875
- } catch (parseError) {
876
- console.warn(
877
- "[HTTP Client] Failed to parse delete file stream event:",
878
- parseError
879
- );
880
- }
881
- }
882
- }
883
- }
884
- } finally {
885
- reader.releaseLock();
886
- }
887
- } catch (error) {
888
- console.error("[HTTP Client] Error in streaming delete file:", error);
889
- this.options.onError?.(
890
- error instanceof Error ? error.message : "Unknown error",
891
- "delete",
892
- [path]
893
- );
894
- throw error;
895
- }
896
- }
897
- async renameFile(oldPath, newPath, sessionId) {
898
- try {
899
- const targetSessionId = sessionId || this.sessionId;
900
- const response = await this.doFetch(`/api/rename`, {
901
- body: JSON.stringify({
902
- newPath,
903
- oldPath,
904
- sessionId: targetSessionId
905
- }),
906
- headers: {
907
- "Content-Type": "application/json"
908
- },
909
- method: "POST"
910
- });
911
- if (!response.ok) {
912
- const errorData = await response.json().catch(() => ({}));
913
- throw new Error(
914
- errorData.error || `HTTP error! status: ${response.status}`
915
- );
916
- }
917
- const data = await response.json();
918
- console.log(
919
- `[HTTP Client] File renamed: ${oldPath} -> ${newPath}, Success: ${data.success}`
920
- );
921
- return data;
922
- } catch (error) {
923
- console.error("[HTTP Client] Error renaming file:", error);
924
- throw error;
925
- }
926
- }
927
- async renameFileStream(oldPath, newPath, sessionId) {
928
- try {
929
- const targetSessionId = sessionId || this.sessionId;
930
- const response = await this.doFetch(`/api/rename/stream`, {
931
- body: JSON.stringify({
932
- newPath,
933
- oldPath,
934
- sessionId: targetSessionId
935
- }),
936
- headers: {
937
- "Content-Type": "application/json"
938
- },
939
- method: "POST"
940
- });
941
- if (!response.ok) {
942
- const errorData = await response.json().catch(() => ({}));
943
- throw new Error(
944
- errorData.error || `HTTP error! status: ${response.status}`
945
- );
946
- }
947
- if (!response.body) {
948
- throw new Error("No response body for streaming request");
949
- }
950
- const reader = response.body.getReader();
951
- const decoder = new TextDecoder();
952
- try {
953
- while (true) {
954
- const { done, value } = await reader.read();
955
- if (done) {
956
- break;
957
- }
958
- const chunk = decoder.decode(value, { stream: true });
959
- const lines = chunk.split("\n");
960
- for (const line of lines) {
961
- if (line.startsWith("data: ")) {
962
- try {
963
- const eventData = line.slice(6);
964
- const event = JSON.parse(eventData);
965
- console.log(
966
- `[HTTP Client] Rename file stream event: ${event.type}`
967
- );
968
- this.options.onStreamEvent?.(event);
969
- switch (event.type) {
970
- case "command_start":
971
- console.log(
972
- `[HTTP Client] Rename file started: ${event.oldPath} -> ${event.newPath}`
973
- );
974
- this.options.onCommandStart?.("rename", [oldPath, newPath]);
975
- break;
976
- case "command_complete":
977
- console.log(
978
- `[HTTP Client] Rename file completed: ${event.oldPath} -> ${event.newPath}, Success: ${event.success}`
979
- );
980
- this.options.onCommandComplete?.(
981
- event.success,
982
- 0,
983
- "",
984
- "",
985
- "rename",
986
- [oldPath, newPath]
987
- );
988
- break;
989
- case "error":
990
- console.error(
991
- `[HTTP Client] Rename file error: ${event.error}`
992
- );
993
- this.options.onError?.(event.error, "rename", [
994
- oldPath,
995
- newPath
996
- ]);
997
- break;
998
- }
999
- } catch (parseError) {
1000
- console.warn(
1001
- "[HTTP Client] Failed to parse rename file stream event:",
1002
- parseError
1003
- );
1004
- }
1005
- }
1006
- }
1007
- }
1008
- } finally {
1009
- reader.releaseLock();
1010
- }
1011
- } catch (error) {
1012
- console.error("[HTTP Client] Error in streaming rename file:", error);
1013
- this.options.onError?.(
1014
- error instanceof Error ? error.message : "Unknown error",
1015
- "rename",
1016
- [oldPath, newPath]
1017
- );
1018
- throw error;
1019
- }
1020
- }
1021
- async moveFile(sourcePath, destinationPath, sessionId) {
1022
- try {
1023
- const targetSessionId = sessionId || this.sessionId;
1024
- const response = await this.doFetch(`/api/move`, {
1025
- body: JSON.stringify({
1026
- destinationPath,
1027
- sessionId: targetSessionId,
1028
- sourcePath
1029
- }),
1030
- headers: {
1031
- "Content-Type": "application/json"
1032
- },
1033
- method: "POST"
1034
- });
1035
- if (!response.ok) {
1036
- const errorData = await response.json().catch(() => ({}));
1037
- throw new Error(
1038
- errorData.error || `HTTP error! status: ${response.status}`
1039
- );
1040
- }
1041
- const data = await response.json();
1042
- console.log(
1043
- `[HTTP Client] File moved: ${sourcePath} -> ${destinationPath}, Success: ${data.success}`
1044
- );
1045
- return data;
1046
- } catch (error) {
1047
- console.error("[HTTP Client] Error moving file:", error);
1048
- throw error;
1049
- }
1050
- }
1051
- async moveFileStream(sourcePath, destinationPath, sessionId) {
1052
- try {
1053
- const targetSessionId = sessionId || this.sessionId;
1054
- const response = await this.doFetch(`/api/move/stream`, {
1055
- body: JSON.stringify({
1056
- destinationPath,
1057
- sessionId: targetSessionId,
1058
- sourcePath
1059
- }),
1060
- headers: {
1061
- "Content-Type": "application/json"
1062
- },
1063
- method: "POST"
1064
- });
1065
- if (!response.ok) {
1066
- const errorData = await response.json().catch(() => ({}));
1067
- throw new Error(
1068
- errorData.error || `HTTP error! status: ${response.status}`
1069
- );
1070
- }
1071
- if (!response.body) {
1072
- throw new Error("No response body for streaming request");
1073
- }
1074
- const reader = response.body.getReader();
1075
- const decoder = new TextDecoder();
1076
- try {
1077
- while (true) {
1078
- const { done, value } = await reader.read();
1079
- if (done) {
1080
- break;
1081
- }
1082
- const chunk = decoder.decode(value, { stream: true });
1083
- const lines = chunk.split("\n");
1084
- for (const line of lines) {
1085
- if (line.startsWith("data: ")) {
1086
- try {
1087
- const eventData = line.slice(6);
1088
- const event = JSON.parse(eventData);
1089
- console.log(
1090
- `[HTTP Client] Move file stream event: ${event.type}`
1091
- );
1092
- this.options.onStreamEvent?.(event);
1093
- switch (event.type) {
1094
- case "command_start":
1095
- console.log(
1096
- `[HTTP Client] Move file started: ${event.sourcePath} -> ${event.destinationPath}`
1097
- );
1098
- this.options.onCommandStart?.("move", [
1099
- sourcePath,
1100
- destinationPath
1101
- ]);
1102
- break;
1103
- case "command_complete":
1104
- console.log(
1105
- `[HTTP Client] Move file completed: ${event.sourcePath} -> ${event.destinationPath}, Success: ${event.success}`
1106
- );
1107
- this.options.onCommandComplete?.(
1108
- event.success,
1109
- 0,
1110
- "",
1111
- "",
1112
- "move",
1113
- [sourcePath, destinationPath]
1114
- );
1115
- break;
1116
- case "error":
1117
- console.error(
1118
- `[HTTP Client] Move file error: ${event.error}`
1119
- );
1120
- this.options.onError?.(event.error, "move", [
1121
- sourcePath,
1122
- destinationPath
1123
- ]);
1124
- break;
1125
- }
1126
- } catch (parseError) {
1127
- console.warn(
1128
- "[HTTP Client] Failed to parse move file stream event:",
1129
- parseError
1130
- );
1131
- }
1132
- }
1133
- }
1134
- }
1135
- } finally {
1136
- reader.releaseLock();
1137
- }
1138
- } catch (error) {
1139
- console.error("[HTTP Client] Error in streaming move file:", error);
1140
- this.options.onError?.(
1141
- error instanceof Error ? error.message : "Unknown error",
1142
- "move",
1143
- [sourcePath, destinationPath]
1144
- );
1145
- throw error;
1146
- }
1147
- }
1148
- async exposePort(port, name) {
1149
- try {
1150
- const response = await this.doFetch(`/api/expose-port`, {
1151
- body: JSON.stringify({
1152
- port,
1153
- name
1154
- }),
1155
- headers: {
1156
- "Content-Type": "application/json"
1157
- },
1158
- method: "POST"
1159
- });
1160
- if (!response.ok) {
1161
- const errorData = await response.json().catch(() => ({}));
1162
- console.log(errorData);
1163
- throw new Error(
1164
- errorData.error || `HTTP error! status: ${response.status}`
1165
- );
1166
- }
1167
- const data = await response.json();
1168
- console.log(
1169
- `[HTTP Client] Port exposed: ${port}${name ? ` (${name})` : ""}, Success: ${data.success}`
1170
- );
1171
- return data;
1172
- } catch (error) {
1173
- console.error("[HTTP Client] Error exposing port:", error);
1174
- throw error;
1175
- }
1176
- }
1177
- async unexposePort(port) {
1178
- try {
1179
- const response = await this.doFetch(`/api/unexpose-port`, {
1180
- body: JSON.stringify({
1181
- port
1182
- }),
1183
- headers: {
1184
- "Content-Type": "application/json"
1185
- },
1186
- method: "DELETE"
1187
- });
1188
- if (!response.ok) {
1189
- const errorData = await response.json().catch(() => ({}));
1190
- throw new Error(
1191
- errorData.error || `HTTP error! status: ${response.status}`
1192
- );
1193
- }
1194
- const data = await response.json();
1195
- console.log(
1196
- `[HTTP Client] Port unexposed: ${port}, Success: ${data.success}`
1197
- );
1198
- return data;
1199
- } catch (error) {
1200
- console.error("[HTTP Client] Error unexposing port:", error);
1201
- throw error;
1202
- }
1203
- }
1204
- async getExposedPorts() {
1205
- try {
1206
- const response = await this.doFetch(`/api/exposed-ports`, {
1207
- headers: {
1208
- "Content-Type": "application/json"
1209
- },
1210
- method: "GET"
1211
- });
1212
- if (!response.ok) {
1213
- const errorData = await response.json().catch(() => ({}));
1214
- throw new Error(
1215
- errorData.error || `HTTP error! status: ${response.status}`
1216
- );
1217
- }
1218
- const data = await response.json();
1219
- console.log(
1220
- `[HTTP Client] Got ${data.count} exposed ports`
1221
- );
1222
- return data;
1223
- } catch (error) {
1224
- console.error("[HTTP Client] Error getting exposed ports:", error);
1225
- throw error;
1226
- }
1227
- }
1228
- async ping() {
1229
- try {
1230
- const response = await this.doFetch(`/api/ping`, {
1231
- headers: {
1232
- "Content-Type": "application/json"
1233
- },
1234
- method: "GET"
1235
- });
1236
- if (!response.ok) {
1237
- throw new Error(`HTTP error! status: ${response.status}`);
1238
- }
1239
- const data = await response.json();
1240
- console.log(`[HTTP Client] Ping response: ${data.message}`);
1241
- return data.timestamp;
1242
- } catch (error) {
1243
- console.error("[HTTP Client] Error pinging server:", error);
1244
- throw error;
1245
- }
1246
- }
1247
- async getCommands() {
1248
- try {
1249
- const response = await fetch(`${this.baseUrl}/api/commands`, {
1250
- headers: {
1251
- "Content-Type": "application/json"
1252
- },
1253
- method: "GET"
1254
- });
1255
- if (!response.ok) {
1256
- throw new Error(`HTTP error! status: ${response.status}`);
1257
- }
1258
- const data = await response.json();
1259
- console.log(
1260
- `[HTTP Client] Available commands: ${data.availableCommands.length}`
1261
- );
1262
- return data.availableCommands;
1263
- } catch (error) {
1264
- console.error("[HTTP Client] Error getting commands:", error);
1265
- throw error;
1266
- }
1267
- }
1268
- getSessionId() {
1269
- return this.sessionId;
1270
- }
1271
- setSessionId(sessionId) {
1272
- this.sessionId = sessionId;
1273
- }
1274
- clearSession() {
1275
- this.sessionId = null;
1276
- }
1277
- };
1278
- function createClient(options) {
1279
- return new HttpClient(options);
1280
- }
1281
- async function quickExecute(command, args = [], options) {
1282
- const client = createClient(options);
1283
- await client.createSession();
1284
- try {
1285
- return await client.execute(command, args);
1286
- } finally {
1287
- client.clearSession();
1288
- }
1289
- }
1290
- async function quickExecuteStream(command, args = [], options) {
1291
- const client = createClient(options);
1292
- await client.createSession();
1293
- try {
1294
- await client.executeStream(command, args);
1295
- } finally {
1296
- client.clearSession();
1297
- }
1298
- }
1299
- async function quickGitCheckout(repoUrl, branch = "main", targetDir, options) {
1300
- const client = createClient(options);
1301
- await client.createSession();
1302
- try {
1303
- return await client.gitCheckout(repoUrl, branch, targetDir);
1304
- } finally {
1305
- client.clearSession();
1306
- }
1307
- }
1308
- async function quickMkdir(path, recursive = false, options) {
1309
- const client = createClient(options);
1310
- await client.createSession();
1311
- try {
1312
- return await client.mkdir(path, recursive);
1313
- } finally {
1314
- client.clearSession();
1315
- }
1316
- }
1317
- async function quickGitCheckoutStream(repoUrl, branch = "main", targetDir, options) {
1318
- const client = createClient(options);
1319
- await client.createSession();
1320
- try {
1321
- await client.gitCheckoutStream(repoUrl, branch, targetDir);
1322
- } finally {
1323
- client.clearSession();
1324
- }
1325
- }
1326
- async function quickMkdirStream(path, recursive = false, options) {
1327
- const client = createClient(options);
1328
- await client.createSession();
1329
- try {
1330
- await client.mkdirStream(path, recursive);
1331
- } finally {
1332
- client.clearSession();
1333
- }
1334
- }
1335
- async function quickWriteFile(path, content, encoding = "utf-8", options) {
1336
- const client = createClient(options);
1337
- await client.createSession();
1338
- try {
1339
- return await client.writeFile(path, content, encoding);
1340
- } finally {
1341
- client.clearSession();
1342
- }
1343
- }
1344
- async function quickWriteFileStream(path, content, encoding = "utf-8", options) {
1345
- const client = createClient(options);
1346
- await client.createSession();
1347
- try {
1348
- await client.writeFileStream(path, content, encoding);
1349
- } finally {
1350
- client.clearSession();
1351
- }
1352
- }
1353
- async function quickReadFile(path, encoding = "utf-8", options) {
1354
- const client = createClient(options);
1355
- await client.createSession();
1356
- try {
1357
- return await client.readFile(path, encoding);
1358
- } finally {
1359
- client.clearSession();
1360
- }
1361
- }
1362
- async function quickReadFileStream(path, encoding = "utf-8", options) {
1363
- const client = createClient(options);
1364
- await client.createSession();
1365
- try {
1366
- await client.readFileStream(path, encoding);
1367
- } finally {
1368
- client.clearSession();
1369
- }
1370
- }
1371
- async function quickDeleteFile(path, options) {
1372
- const client = createClient(options);
1373
- await client.createSession();
1374
- try {
1375
- return await client.deleteFile(path);
1376
- } finally {
1377
- client.clearSession();
1378
- }
1379
- }
1380
- async function quickDeleteFileStream(path, options) {
1381
- const client = createClient(options);
1382
- await client.createSession();
1383
- try {
1384
- await client.deleteFileStream(path);
1385
- } finally {
1386
- client.clearSession();
1387
- }
1388
- }
1389
- async function quickRenameFile(oldPath, newPath, options) {
1390
- const client = createClient(options);
1391
- await client.createSession();
1392
- try {
1393
- return await client.renameFile(oldPath, newPath);
1394
- } finally {
1395
- client.clearSession();
1396
- }
1397
- }
1398
- async function quickRenameFileStream(oldPath, newPath, options) {
1399
- const client = createClient(options);
1400
- await client.createSession();
1401
- try {
1402
- await client.renameFileStream(oldPath, newPath);
1403
- } finally {
1404
- client.clearSession();
1405
- }
1406
- }
1407
- async function quickMoveFile(sourcePath, destinationPath, options) {
1408
- const client = createClient(options);
1409
- await client.createSession();
1410
- try {
1411
- return await client.moveFile(sourcePath, destinationPath);
1412
- } finally {
1413
- client.clearSession();
1414
- }
1415
- }
1416
- async function quickMoveFileStream(sourcePath, destinationPath, options) {
1417
- const client = createClient(options);
1418
- await client.createSession();
1419
- try {
1420
- await client.moveFileStream(sourcePath, destinationPath);
1421
- } finally {
1422
- client.clearSession();
1423
- }
1424
- }
1425
-
1426
- export {
1427
- HttpClient,
1428
- createClient,
1429
- quickExecute,
1430
- quickExecuteStream,
1431
- quickGitCheckout,
1432
- quickMkdir,
1433
- quickGitCheckoutStream,
1434
- quickMkdirStream,
1435
- quickWriteFile,
1436
- quickWriteFileStream,
1437
- quickReadFile,
1438
- quickReadFileStream,
1439
- quickDeleteFile,
1440
- quickDeleteFileStream,
1441
- quickRenameFile,
1442
- quickRenameFileStream,
1443
- quickMoveFile,
1444
- quickMoveFileStream
1445
- };
1446
- //# sourceMappingURL=chunk-4J5LQCCN.js.map