@cloudflare/sandbox 0.0.0-211d237 → 0.0.0-2450ebd

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