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