@cloudflare/sandbox 0.0.0-c0d9d33 → 0.0.0-c2e3384

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 +322 -0
  2. package/Dockerfile +183 -9
  3. package/README.md +154 -50
  4. package/dist/index.d.ts +1953 -0
  5. package/dist/index.d.ts.map +1 -0
  6. package/dist/index.js +3280 -0
  7. package/dist/index.js.map +1 -0
  8. package/package.json +16 -10
  9. package/src/clients/base-client.ts +295 -0
  10. package/src/clients/command-client.ts +115 -0
  11. package/src/clients/file-client.ts +300 -0
  12. package/src/clients/git-client.ts +98 -0
  13. package/src/clients/index.ts +64 -0
  14. package/src/clients/interpreter-client.ts +333 -0
  15. package/src/clients/port-client.ts +105 -0
  16. package/src/clients/process-client.ts +180 -0
  17. package/src/clients/sandbox-client.ts +39 -0
  18. package/src/clients/types.ts +88 -0
  19. package/src/clients/utility-client.ts +156 -0
  20. package/src/errors/adapter.ts +238 -0
  21. package/src/errors/classes.ts +594 -0
  22. package/src/errors/index.ts +109 -0
  23. package/src/file-stream.ts +169 -0
  24. package/src/index.ts +95 -120
  25. package/src/interpreter.ts +168 -0
  26. package/src/request-handler.ts +183 -0
  27. package/src/sandbox.ts +1268 -0
  28. package/src/security.ts +119 -0
  29. package/src/sse-parser.ts +144 -0
  30. package/src/version.ts +6 -0
  31. package/startup.sh +3 -0
  32. package/tests/base-client.test.ts +364 -0
  33. package/tests/command-client.test.ts +444 -0
  34. package/tests/file-client.test.ts +831 -0
  35. package/tests/file-stream.test.ts +310 -0
  36. package/tests/get-sandbox.test.ts +149 -0
  37. package/tests/git-client.test.ts +487 -0
  38. package/tests/port-client.test.ts +293 -0
  39. package/tests/process-client.test.ts +683 -0
  40. package/tests/request-handler.test.ts +292 -0
  41. package/tests/sandbox.test.ts +739 -0
  42. package/tests/sse-parser.test.ts +291 -0
  43. package/tests/utility-client.test.ts +339 -0
  44. package/tests/version.test.ts +16 -0
  45. package/tests/wrangler.jsonc +35 -0
  46. package/tsconfig.json +9 -1
  47. package/tsdown.config.ts +12 -0
  48. package/vitest.config.ts +31 -0
  49. package/container_src/index.ts +0 -2900
  50. package/container_src/package.json +0 -9
  51. package/src/client.ts +0 -1929
  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
package/README.md CHANGED
@@ -1,65 +1,169 @@
1
- ## @cloudflare/sandbox
1
+ <img width="1362" height="450" alt="Image" src="https://github.com/user-attachments/assets/6f770ae3-0a14-4d2b-9aed-a304ee5446c5" />
2
2
 
3
- > **⚠️ Experimental** - This library is currently experimental and we're actively seeking feedback. Please try it out and let us know what you think!
3
+ # Cloudflare Sandbox SDK
4
4
 
5
- A library to spin up a sandboxed environment.
5
+ [![npm version](https://img.shields.io/npm/v/@cloudflare/sandbox.svg)](https://www.npmjs.com/package/@cloudflare/sandbox)
6
6
 
7
- First, setup your wrangler.json to use the sandbox:
7
+ **Build secure, isolated code execution environments on Cloudflare.**
8
8
 
9
- ```jsonc
10
- {
11
- // ...
12
- "containers": [
13
- {
14
- "class_name": "Sandbox",
15
- "image": "./node_modules/@cloudflare/sandbox/Dockerfile",
16
- "name": "sandbox"
17
- }
18
- ],
19
- "durable_objects": {
20
- "bindings": [
21
- {
22
- "class_name": "Sandbox",
23
- "name": "Sandbox"
24
- }
25
- ]
26
- },
27
- "migrations": [
28
- {
29
- "new_sqlite_classes": ["Sandbox"],
30
- "tag": "v1"
31
- }
32
- ]
33
- }
9
+ The Sandbox SDK lets you run untrusted code safely in isolated containers. Execute commands, manage files, run background processes, and expose services — all from your Workers applications.
10
+
11
+ Perfect for AI code execution, interactive development environments, data analysis platforms, CI/CD systems, and any application that needs secure code execution at the edge.
12
+
13
+ ## Getting Started
14
+
15
+ ### Prerequisites
16
+
17
+ 1. Install [Node.js](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) (version 16.17.0 or later)
18
+ 2. Ensure Docker is running locally
19
+ 3. For deploying to production, sign up for a [Cloudflare account](https://dash.cloudflare.com/sign-up/workers-and-pages)
20
+
21
+ ### 1. Create a new project
22
+
23
+ Create a new Sandbox SDK project using the minimal template:
24
+
25
+ ```bash
26
+ npm create cloudflare@latest -- my-sandbox --template=cloudflare/sandbox-sdk/examples/minimal
27
+ cd my-sandbox
34
28
  ```
35
29
 
36
- Then, export the Sandbox class in your worker:
30
+ ### 2. Test locally
37
31
 
38
- ```ts
39
- export { Sandbox } from "@cloudflare/sandbox";
32
+ Start the development server:
33
+
34
+ ```bash
35
+ npm run dev
40
36
  ```
41
37
 
42
- You can then use the Sandbox class in your worker:
38
+ > **Note:** First run builds the Docker container (2-3 minutes). Subsequent runs are much faster.
39
+
40
+ Test the endpoints:
41
+
42
+ ```bash
43
+ # Execute Python code
44
+ curl http://localhost:8787/run
45
+
46
+ # File operations
47
+ curl http://localhost:8787/file
48
+ ```
43
49
 
44
- ```ts
45
- import { getSandbox } from "@cloudflare/sandbox";
50
+ ### 3. Deploy to production
51
+
52
+ Deploy your Worker and container:
53
+
54
+ ```bash
55
+ npx wrangler deploy
56
+ ```
57
+
58
+ > **Wait for provisioning:** After first deployment, wait 2-3 minutes before making requests.
59
+
60
+ **📖 [View the complete getting started guide](https://developers.cloudflare.com/sandbox/get-started/)** for detailed instructions and explanations.
61
+
62
+ ## Quick API Example
63
+
64
+ ```typescript
65
+ import { getSandbox, proxyToSandbox, type Sandbox } from '@cloudflare/sandbox';
66
+
67
+ export { Sandbox } from '@cloudflare/sandbox';
68
+
69
+ type Env = {
70
+ Sandbox: DurableObjectNamespace<Sandbox>;
71
+ };
46
72
 
47
73
  export default {
48
- async fetch(request: Request, env: Env) {
49
- const sandbox = getSandbox(env.Sandbox, "my-sandbox");
50
- return sandbox.exec("ls", ["-la"]);
51
- },
74
+ async fetch(request: Request, env: Env): Promise<Response> {
75
+ // Required for preview URLs
76
+ const proxyResponse = await proxyToSandbox(request, env);
77
+ if (proxyResponse) return proxyResponse;
78
+
79
+ const url = new URL(request.url);
80
+ const sandbox = getSandbox(env.Sandbox, 'my-sandbox');
81
+
82
+ // Execute Python code
83
+ if (url.pathname === '/run') {
84
+ const result = await sandbox.exec('python3 -c "print(2 + 2)"');
85
+ return Response.json({ output: result.stdout, success: result.success });
86
+ }
87
+
88
+ // Work with files
89
+ if (url.pathname === '/file') {
90
+ await sandbox.writeFile('/workspace/hello.txt', 'Hello, Sandbox!');
91
+ const file = await sandbox.readFile('/workspace/hello.txt');
92
+ return Response.json({ content: file.content });
93
+ }
94
+
95
+ return new Response('Try /run or /file');
96
+ }
52
97
  };
53
98
  ```
54
99
 
55
- ### Methods:
56
-
57
- - `exec(command: string, args: string[], options?: { stream?: boolean })`: Execute a command in the sandbox.
58
- - `gitCheckout(repoUrl: string, options: { branch?: string; targetDir?: string; stream?: boolean })`: Checkout a git repository in the sandbox.
59
- - `mkdir(path: string, options: { recursive?: boolean; stream?: boolean })`: Create a directory in the sandbox.
60
- - `writeFile(path: string, content: string, options: { encoding?: string; stream?: boolean })`: Write content to a file in the sandbox.
61
- - `readFile(path: string, options: { encoding?: string; stream?: boolean })`: Read content from a file in the sandbox.
62
- - `deleteFile(path: string, options?: { stream?: boolean })`: Delete a file from the sandbox.
63
- - `renameFile(oldPath: string, newPath: string, options?: { stream?: boolean })`: Rename a file in the sandbox.
64
- - `moveFile(sourcePath: string, destinationPath: string, options?: { stream?: boolean })`: Move a file from one location to another in the sandbox.
65
- - `ping()`: Ping the sandbox.
100
+ ## Documentation
101
+
102
+ **📖 [Full Documentation](https://developers.cloudflare.com/sandbox/)**
103
+
104
+ - [Get Started Guide](https://developers.cloudflare.com/sandbox/get-started/) - Step-by-step tutorial
105
+ - [API Reference](https://developers.cloudflare.com/sandbox/api/) - Complete API docs
106
+ - [Guides](https://developers.cloudflare.com/sandbox/guides/) - Execute commands, manage files, expose services
107
+ - [Examples](https://developers.cloudflare.com/sandbox/tutorials/) - AI agents, data analysis, CI/CD pipelines
108
+
109
+ ## Key Features
110
+
111
+ - **Secure Isolation** - Each sandbox runs in its own container
112
+ - **Edge-Native** - Runs on Cloudflare's global network
113
+ - **Code Interpreter** - Execute Python and JavaScript with rich outputs
114
+ - **File System Access** - Read, write, and manage files
115
+ - **Command Execution** - Run any command with streaming support
116
+ - **Preview URLs** - Expose services with public URLs
117
+ - **Git Integration** - Clone repositories directly
118
+
119
+ ## Contributing
120
+
121
+ We welcome contributions from the community! See [CONTRIBUTING.md](./CONTRIBUTING.md) for guidelines on:
122
+
123
+ - Setting up your development environment
124
+ - Creating pull requests
125
+ - Code style and testing requirements
126
+
127
+ ## Development
128
+
129
+ This repository contains the SDK source code. Quick start:
130
+
131
+ ```bash
132
+ # Clone the repo
133
+ git clone https://github.com/cloudflare/sandbox-sdk
134
+ cd sandbox-sdk
135
+
136
+ # Install dependencies
137
+ npm install
138
+
139
+ # Run tests
140
+ npm test
141
+
142
+ # Build the project
143
+ npm run build
144
+
145
+ # Type checking and linting
146
+ npm run check
147
+ ```
148
+
149
+ ## Examples
150
+
151
+ See the [examples directory](./examples) for complete working examples:
152
+
153
+ - [Minimal](./examples/minimal) - Basic sandbox setup
154
+ - [Code Interpreter](./examples/code-interpreter) - Use sandbox as an interpreter tool with gpt-oss
155
+
156
+ ## Status
157
+
158
+ **Beta** - The SDK is in active development. APIs may change before v1.0.
159
+
160
+ ## License
161
+
162
+ [MIT License](LICENSE)
163
+
164
+ ## Links
165
+
166
+ - [Documentation](https://developers.cloudflare.com/sandbox/)
167
+ - [GitHub Issues](https://github.com/cloudflare/sandbox-sdk/issues)
168
+ - [Developer Discord](https://discord.cloudflare.com)
169
+ - [Cloudflare Developers](https://twitter.com/CloudflareDev)