@cloudflare/sandbox 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @cloudflare/sandbox
2
2
 
3
+ ## 0.0.3
4
+
5
+ ### Patch Changes
6
+
7
+ - [`2b087c4`](https://github.com/cloudflare/sandbox-sdk/commit/2b087c40a29697c20dad19b4e3b8512f5d404bd3) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Fix worker unable to find container port
8
+
9
+ ## 0.0.2
10
+
11
+ ### Patch Changes
12
+
13
+ - [`52f02f0`](https://github.com/cloudflare/sandbox-sdk/commit/52f02f0625ef9f8eac695e51f93fa79651c0206d) Thanks [@threepointone](https://github.com/threepointone)! - readFile
14
+
3
15
  ## 0.0.1
4
16
 
5
17
  ### Patch Changes
package/README.md CHANGED
@@ -56,6 +56,7 @@ export default {
56
56
  - `gitCheckout(repoUrl: string, options: { branch?: string; targetDir?: string; stream?: boolean })`: Checkout a git repository in the sandbox.
57
57
  - `mkdir(path: string, options: { recursive?: boolean; stream?: boolean })`: Create a directory in the sandbox.
58
58
  - `writeFile(path: string, content: string, options: { encoding?: string; stream?: boolean })`: Write content to a file in the sandbox.
59
+ - `readFile(path: string, options: { encoding?: string; stream?: boolean })`: Read content from a file in the sandbox.
59
60
  - `deleteFile(path: string, options?: { stream?: boolean })`: Delete a file from the sandbox.
60
61
  - `renameFile(oldPath: string, newPath: string, options?: { stream?: boolean })`: Rename a file in the sandbox.
61
62
  - `moveFile(sourcePath: string, destinationPath: string, options?: { stream?: boolean })`: Move a file from one location to another in the sandbox.
@@ -1,5 +1,5 @@
1
1
  import { spawn } from "node:child_process";
2
- import { mkdir, rename, unlink, writeFile } from "node:fs/promises";
2
+ import { mkdir, readFile, rename, unlink, writeFile } from "node:fs/promises";
3
3
  import { dirname } from "node:path";
4
4
  import { serve } from "bun";
5
5
 
@@ -28,6 +28,12 @@ interface WriteFileRequest {
28
28
  sessionId?: string;
29
29
  }
30
30
 
31
+ interface ReadFileRequest {
32
+ path: string;
33
+ encoding?: string;
34
+ sessionId?: string;
35
+ }
36
+
31
37
  interface DeleteFileRequest {
32
38
  path: string;
33
39
  sessionId?: string;
@@ -297,6 +303,18 @@ const server = serve({
297
303
  }
298
304
  break;
299
305
 
306
+ case "/api/read":
307
+ if (req.method === "POST") {
308
+ return handleReadFileRequest(req, corsHeaders);
309
+ }
310
+ break;
311
+
312
+ case "/api/read/stream":
313
+ if (req.method === "POST") {
314
+ return handleStreamingReadFileRequest(req, corsHeaders);
315
+ }
316
+ break;
317
+
300
318
  case "/api/delete":
301
319
  if (req.method === "POST") {
302
320
  return handleDeleteFileRequest(req, corsHeaders);
@@ -1476,6 +1494,235 @@ async function handleStreamingWriteFileRequest(
1476
1494
  }
1477
1495
  }
1478
1496
 
1497
+ async function handleReadFileRequest(
1498
+ req: Request,
1499
+ corsHeaders: Record<string, string>
1500
+ ): Promise<Response> {
1501
+ try {
1502
+ const body = (await req.json()) as ReadFileRequest;
1503
+ const { path, encoding = "utf-8", sessionId } = body;
1504
+
1505
+ if (!path || typeof path !== "string") {
1506
+ return new Response(
1507
+ JSON.stringify({
1508
+ error: "Path is required and must be a string",
1509
+ }),
1510
+ {
1511
+ headers: {
1512
+ "Content-Type": "application/json",
1513
+ ...corsHeaders,
1514
+ },
1515
+ status: 400,
1516
+ }
1517
+ );
1518
+ }
1519
+
1520
+ // Basic safety check - prevent dangerous paths
1521
+ const dangerousPatterns = [
1522
+ /^\/$/, // Root directory
1523
+ /^\/etc/, // System directories
1524
+ /^\/var/, // System directories
1525
+ /^\/usr/, // System directories
1526
+ /^\/bin/, // System directories
1527
+ /^\/sbin/, // System directories
1528
+ /^\/boot/, // System directories
1529
+ /^\/dev/, // System directories
1530
+ /^\/proc/, // System directories
1531
+ /^\/sys/, // System directories
1532
+ /^\/tmp\/\.\./, // Path traversal attempts
1533
+ /\.\./, // Path traversal attempts
1534
+ ];
1535
+
1536
+ if (dangerousPatterns.some((pattern) => pattern.test(path))) {
1537
+ return new Response(
1538
+ JSON.stringify({
1539
+ error: "Dangerous path not allowed",
1540
+ }),
1541
+ {
1542
+ headers: {
1543
+ "Content-Type": "application/json",
1544
+ ...corsHeaders,
1545
+ },
1546
+ status: 400,
1547
+ }
1548
+ );
1549
+ }
1550
+
1551
+ console.log(`[Server] Reading file: ${path}`);
1552
+
1553
+ const result = await executeReadFile(path, encoding, sessionId);
1554
+
1555
+ return new Response(
1556
+ JSON.stringify({
1557
+ content: result.content,
1558
+ exitCode: result.exitCode,
1559
+ path,
1560
+ success: result.success,
1561
+ timestamp: new Date().toISOString(),
1562
+ }),
1563
+ {
1564
+ headers: {
1565
+ "Content-Type": "application/json",
1566
+ ...corsHeaders,
1567
+ },
1568
+ }
1569
+ );
1570
+ } catch (error) {
1571
+ console.error("[Server] Error in handleReadFileRequest:", error);
1572
+ return new Response(
1573
+ JSON.stringify({
1574
+ error: "Failed to read file",
1575
+ message: error instanceof Error ? error.message : "Unknown error",
1576
+ }),
1577
+ {
1578
+ headers: {
1579
+ "Content-Type": "application/json",
1580
+ ...corsHeaders,
1581
+ },
1582
+ status: 500,
1583
+ }
1584
+ );
1585
+ }
1586
+ }
1587
+
1588
+ async function handleStreamingReadFileRequest(
1589
+ req: Request,
1590
+ corsHeaders: Record<string, string>
1591
+ ): Promise<Response> {
1592
+ try {
1593
+ const body = (await req.json()) as ReadFileRequest;
1594
+ const { path, encoding = "utf-8", sessionId } = body;
1595
+
1596
+ if (!path || typeof path !== "string") {
1597
+ return new Response(
1598
+ JSON.stringify({
1599
+ error: "Path is required and must be a string",
1600
+ }),
1601
+ {
1602
+ headers: {
1603
+ "Content-Type": "application/json",
1604
+ ...corsHeaders,
1605
+ },
1606
+ status: 400,
1607
+ }
1608
+ );
1609
+ }
1610
+
1611
+ // Basic safety check - prevent dangerous paths
1612
+ const dangerousPatterns = [
1613
+ /^\/$/, // Root directory
1614
+ /^\/etc/, // System directories
1615
+ /^\/var/, // System directories
1616
+ /^\/usr/, // System directories
1617
+ /^\/bin/, // System directories
1618
+ /^\/sbin/, // System directories
1619
+ /^\/boot/, // System directories
1620
+ /^\/dev/, // System directories
1621
+ /^\/proc/, // System directories
1622
+ /^\/sys/, // System directories
1623
+ /^\/tmp\/\.\./, // Path traversal attempts
1624
+ /\.\./, // Path traversal attempts
1625
+ ];
1626
+
1627
+ if (dangerousPatterns.some((pattern) => pattern.test(path))) {
1628
+ return new Response(
1629
+ JSON.stringify({
1630
+ error: "Dangerous path not allowed",
1631
+ }),
1632
+ {
1633
+ headers: {
1634
+ "Content-Type": "application/json",
1635
+ ...corsHeaders,
1636
+ },
1637
+ status: 400,
1638
+ }
1639
+ );
1640
+ }
1641
+
1642
+ console.log(`[Server] Reading file (streaming): ${path}`);
1643
+
1644
+ const stream = new ReadableStream({
1645
+ start(controller) {
1646
+ (async () => {
1647
+ try {
1648
+ // Send command start event
1649
+ controller.enqueue(
1650
+ new TextEncoder().encode(
1651
+ `data: ${JSON.stringify({
1652
+ path,
1653
+ timestamp: new Date().toISOString(),
1654
+ type: "command_start",
1655
+ })}\n\n`
1656
+ )
1657
+ );
1658
+
1659
+ // Read the file
1660
+ const content = await readFile(path, {
1661
+ encoding: encoding as BufferEncoding,
1662
+ });
1663
+
1664
+ console.log(`[Server] File read successfully: ${path}`);
1665
+
1666
+ // Send command completion event
1667
+ controller.enqueue(
1668
+ new TextEncoder().encode(
1669
+ `data: ${JSON.stringify({
1670
+ content,
1671
+ path,
1672
+ success: true,
1673
+ timestamp: new Date().toISOString(),
1674
+ type: "command_complete",
1675
+ })}\n\n`
1676
+ )
1677
+ );
1678
+
1679
+ controller.close();
1680
+ } catch (error) {
1681
+ console.error(`[Server] Error reading file: ${path}`, error);
1682
+
1683
+ controller.enqueue(
1684
+ new TextEncoder().encode(
1685
+ `data: ${JSON.stringify({
1686
+ error:
1687
+ error instanceof Error ? error.message : "Unknown error",
1688
+ path,
1689
+ type: "error",
1690
+ })}\n\n`
1691
+ )
1692
+ );
1693
+
1694
+ controller.close();
1695
+ }
1696
+ })();
1697
+ },
1698
+ });
1699
+
1700
+ return new Response(stream, {
1701
+ headers: {
1702
+ "Cache-Control": "no-cache",
1703
+ Connection: "keep-alive",
1704
+ "Content-Type": "text/event-stream",
1705
+ ...corsHeaders,
1706
+ },
1707
+ });
1708
+ } catch (error) {
1709
+ console.error("[Server] Error in handleStreamingReadFileRequest:", error);
1710
+ return new Response(
1711
+ JSON.stringify({
1712
+ error: "Failed to read file",
1713
+ message: error instanceof Error ? error.message : "Unknown error",
1714
+ }),
1715
+ {
1716
+ headers: {
1717
+ "Content-Type": "application/json",
1718
+ ...corsHeaders,
1719
+ },
1720
+ status: 500,
1721
+ }
1722
+ );
1723
+ }
1724
+ }
1725
+
1479
1726
  async function handleDeleteFileRequest(
1480
1727
  req: Request,
1481
1728
  corsHeaders: Record<string, string>
@@ -2506,6 +2753,37 @@ function executeWriteFile(
2506
2753
  });
2507
2754
  }
2508
2755
 
2756
+ function executeReadFile(
2757
+ path: string,
2758
+ encoding: string,
2759
+ sessionId?: string
2760
+ ): Promise<{
2761
+ success: boolean;
2762
+ exitCode: number;
2763
+ content: string;
2764
+ }> {
2765
+ return new Promise((resolve, reject) => {
2766
+ (async () => {
2767
+ try {
2768
+ // Read the file
2769
+ const content = await readFile(path, {
2770
+ encoding: encoding as BufferEncoding,
2771
+ });
2772
+
2773
+ console.log(`[Server] File read successfully: ${path}`);
2774
+ resolve({
2775
+ content,
2776
+ exitCode: 0,
2777
+ success: true,
2778
+ });
2779
+ } catch (error) {
2780
+ console.error(`[Server] Error reading file: ${path}`, error);
2781
+ reject(error);
2782
+ }
2783
+ })();
2784
+ });
2785
+ }
2786
+
2509
2787
  function executeDeleteFile(
2510
2788
  path: string,
2511
2789
  sessionId?: string
@@ -2610,6 +2888,8 @@ console.log(` POST /api/mkdir - Create a directory`);
2610
2888
  console.log(` POST /api/mkdir/stream - Create a directory (streaming)`);
2611
2889
  console.log(` POST /api/write - Write a file`);
2612
2890
  console.log(` POST /api/write/stream - Write a file (streaming)`);
2891
+ console.log(` POST /api/read - Read a file`);
2892
+ console.log(` POST /api/read/stream - Read a file (streaming)`);
2613
2893
  console.log(` POST /api/delete - Delete a file`);
2614
2894
  console.log(` POST /api/delete/stream - Delete a file (streaming)`);
2615
2895
  console.log(` POST /api/rename - Rename a file`);
@@ -626,6 +626,130 @@ var HttpClient = class {
626
626
  throw error;
627
627
  }
628
628
  }
629
+ async readFile(path, encoding = "utf-8", sessionId) {
630
+ try {
631
+ const targetSessionId = sessionId || this.sessionId;
632
+ const response = await this.doFetch(`/api/read`, {
633
+ body: JSON.stringify({
634
+ encoding,
635
+ path,
636
+ sessionId: targetSessionId
637
+ }),
638
+ headers: {
639
+ "Content-Type": "application/json"
640
+ },
641
+ method: "POST"
642
+ });
643
+ if (!response.ok) {
644
+ const errorData = await response.json().catch(() => ({}));
645
+ throw new Error(
646
+ errorData.error || `HTTP error! status: ${response.status}`
647
+ );
648
+ }
649
+ const data = await response.json();
650
+ console.log(
651
+ `[HTTP Client] File read: ${path}, Success: ${data.success}, Content length: ${data.content.length}`
652
+ );
653
+ return data;
654
+ } catch (error) {
655
+ console.error("[HTTP Client] Error reading file:", error);
656
+ throw error;
657
+ }
658
+ }
659
+ async readFileStream(path, encoding = "utf-8", sessionId) {
660
+ try {
661
+ const targetSessionId = sessionId || this.sessionId;
662
+ const response = await this.doFetch(`/api/read/stream`, {
663
+ body: JSON.stringify({
664
+ encoding,
665
+ path,
666
+ sessionId: targetSessionId
667
+ }),
668
+ headers: {
669
+ "Content-Type": "application/json"
670
+ },
671
+ method: "POST"
672
+ });
673
+ if (!response.ok) {
674
+ const errorData = await response.json().catch(() => ({}));
675
+ throw new Error(
676
+ errorData.error || `HTTP error! status: ${response.status}`
677
+ );
678
+ }
679
+ if (!response.body) {
680
+ throw new Error("No response body for streaming request");
681
+ }
682
+ const reader = response.body.getReader();
683
+ const decoder = new TextDecoder();
684
+ try {
685
+ while (true) {
686
+ const { done, value } = await reader.read();
687
+ if (done) {
688
+ break;
689
+ }
690
+ const chunk = decoder.decode(value, { stream: true });
691
+ const lines = chunk.split("\n");
692
+ for (const line of lines) {
693
+ if (line.startsWith("data: ")) {
694
+ try {
695
+ const eventData = line.slice(6);
696
+ const event = JSON.parse(eventData);
697
+ console.log(
698
+ `[HTTP Client] Read file stream event: ${event.type}`
699
+ );
700
+ this.options.onStreamEvent?.(event);
701
+ switch (event.type) {
702
+ case "command_start":
703
+ console.log(
704
+ `[HTTP Client] Read file started: ${event.path}`
705
+ );
706
+ this.options.onCommandStart?.("read", [path, encoding]);
707
+ break;
708
+ case "command_complete":
709
+ console.log(
710
+ `[HTTP Client] Read file completed: ${event.path}, Success: ${event.success}, Content length: ${event.content?.length || 0}`
711
+ );
712
+ this.options.onCommandComplete?.(
713
+ event.success,
714
+ 0,
715
+ event.content || "",
716
+ "",
717
+ "read",
718
+ [path, encoding]
719
+ );
720
+ break;
721
+ case "error":
722
+ console.error(
723
+ `[HTTP Client] Read file error: ${event.error}`
724
+ );
725
+ this.options.onError?.(event.error, "read", [
726
+ path,
727
+ encoding
728
+ ]);
729
+ break;
730
+ }
731
+ } catch (parseError) {
732
+ console.warn(
733
+ "[HTTP Client] Failed to parse read file stream event:",
734
+ parseError
735
+ );
736
+ }
737
+ }
738
+ }
739
+ }
740
+ } finally {
741
+ reader.releaseLock();
742
+ }
743
+ } catch (error) {
744
+ console.error("[HTTP Client] Error in streaming read file:", error);
745
+ this.options.onError?.(
746
+ error instanceof Error ? error.message : "Unknown error",
747
+ "read",
748
+ [path, encoding]
749
+ );
750
+ throw error;
751
+ }
752
+ }
629
753
  async deleteFile(path, sessionId) {
630
754
  try {
631
755
  const targetSessionId = sessionId || this.sessionId;
@@ -1121,6 +1245,24 @@ async function quickWriteFileStream(path, content, encoding = "utf-8", options)
1121
1245
  client.clearSession();
1122
1246
  }
1123
1247
  }
1248
+ async function quickReadFile(path, encoding = "utf-8", options) {
1249
+ const client = createClient(options);
1250
+ await client.createSession();
1251
+ try {
1252
+ return await client.readFile(path, encoding);
1253
+ } finally {
1254
+ client.clearSession();
1255
+ }
1256
+ }
1257
+ async function quickReadFileStream(path, encoding = "utf-8", options) {
1258
+ const client = createClient(options);
1259
+ await client.createSession();
1260
+ try {
1261
+ await client.readFileStream(path, encoding);
1262
+ } finally {
1263
+ client.clearSession();
1264
+ }
1265
+ }
1124
1266
  async function quickDeleteFile(path, options) {
1125
1267
  const client = createClient(options);
1126
1268
  await client.createSession();
@@ -1187,6 +1329,8 @@ export {
1187
1329
  quickMkdirStream,
1188
1330
  quickWriteFile,
1189
1331
  quickWriteFileStream,
1332
+ quickReadFile,
1333
+ quickReadFileStream,
1190
1334
  quickDeleteFile,
1191
1335
  quickDeleteFileStream,
1192
1336
  quickRenameFile,
@@ -1194,4 +1338,4 @@ export {
1194
1338
  quickMoveFile,
1195
1339
  quickMoveFileStream
1196
1340
  };
1197
- //# sourceMappingURL=chunk-GYEDSCAB.js.map
1341
+ //# sourceMappingURL=chunk-N6VMTCTA.js.map