@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.
- package/CHANGELOG.md +322 -0
- package/Dockerfile +183 -9
- package/README.md +154 -50
- package/dist/index.d.ts +1953 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3280 -0
- package/dist/index.js.map +1 -0
- package/package.json +16 -10
- package/src/clients/base-client.ts +295 -0
- package/src/clients/command-client.ts +115 -0
- package/src/clients/file-client.ts +300 -0
- package/src/clients/git-client.ts +98 -0
- package/src/clients/index.ts +64 -0
- package/src/clients/interpreter-client.ts +333 -0
- package/src/clients/port-client.ts +105 -0
- package/src/clients/process-client.ts +180 -0
- package/src/clients/sandbox-client.ts +39 -0
- package/src/clients/types.ts +88 -0
- package/src/clients/utility-client.ts +156 -0
- package/src/errors/adapter.ts +238 -0
- package/src/errors/classes.ts +594 -0
- package/src/errors/index.ts +109 -0
- package/src/file-stream.ts +169 -0
- package/src/index.ts +95 -120
- package/src/interpreter.ts +168 -0
- package/src/request-handler.ts +183 -0
- package/src/sandbox.ts +1268 -0
- package/src/security.ts +119 -0
- package/src/sse-parser.ts +144 -0
- package/src/version.ts +6 -0
- package/startup.sh +3 -0
- package/tests/base-client.test.ts +364 -0
- package/tests/command-client.test.ts +444 -0
- package/tests/file-client.test.ts +831 -0
- package/tests/file-stream.test.ts +310 -0
- package/tests/get-sandbox.test.ts +149 -0
- package/tests/git-client.test.ts +487 -0
- package/tests/port-client.test.ts +293 -0
- package/tests/process-client.test.ts +683 -0
- package/tests/request-handler.test.ts +292 -0
- package/tests/sandbox.test.ts +739 -0
- package/tests/sse-parser.test.ts +291 -0
- package/tests/utility-client.test.ts +339 -0
- package/tests/version.test.ts +16 -0
- package/tests/wrangler.jsonc +35 -0
- package/tsconfig.json +9 -1
- package/tsdown.config.ts +12 -0
- package/vitest.config.ts +31 -0
- package/container_src/index.ts +0 -2900
- package/container_src/package.json +0 -9
- package/src/client.ts +0 -1929
- package/tests/client.example.ts +0 -308
- package/tests/connection-test.ts +0 -81
- package/tests/simple-test.ts +0 -81
- package/tests/test1.ts +0 -281
- package/tests/test2.ts +0 -929
package/README.md
CHANGED
|
@@ -1,65 +1,169 @@
|
|
|
1
|
-
|
|
1
|
+
<img width="1362" height="450" alt="Image" src="https://github.com/user-attachments/assets/6f770ae3-0a14-4d2b-9aed-a304ee5446c5" />
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# Cloudflare Sandbox SDK
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/@cloudflare/sandbox)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
**Build secure, isolated code execution environments on Cloudflare.**
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
30
|
+
### 2. Test locally
|
|
37
31
|
|
|
38
|
-
|
|
39
|
-
|
|
32
|
+
Start the development server:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npm run dev
|
|
40
36
|
```
|
|
41
37
|
|
|
42
|
-
|
|
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
|
-
|
|
45
|
-
|
|
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
|
-
|
|
50
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
-
|
|
60
|
-
-
|
|
61
|
-
-
|
|
62
|
-
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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)
|