@cloudflare/sandbox 0.0.0-4aceb32 โ 0.0.0-4bedc3a
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 +18 -0
- package/Dockerfile +19 -17
- package/README.md +69 -7
- package/container_src/control-process.ts +784 -0
- package/container_src/handler/exec.ts +99 -254
- package/container_src/handler/file.ts +204 -642
- package/container_src/handler/git.ts +28 -80
- package/container_src/handler/process.ts +443 -515
- package/container_src/handler/session.ts +92 -0
- package/container_src/index.ts +74 -129
- package/container_src/isolation.ts +1039 -0
- package/container_src/jupyter-service.ts +15 -2
- package/container_src/jupyter_config.py +48 -0
- package/container_src/shell-escape.ts +42 -0
- package/container_src/startup.sh +33 -32
- package/container_src/types.ts +35 -12
- package/package.json +1 -1
- package/src/client.ts +175 -187
- package/src/index.ts +23 -13
- package/src/sandbox.ts +298 -333
- package/src/types.ts +125 -24
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @cloudflare/sandbox
|
|
2
2
|
|
|
3
|
+
## 0.2.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#57](https://github.com/cloudflare/sandbox-sdk/pull/57) [`12bbd12`](https://github.com/cloudflare/sandbox-sdk/commit/12bbd1229c07ef8c1c0bf58a4235a27938155b08) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Add listFiles method
|
|
8
|
+
|
|
9
|
+
## 0.2.3
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#53](https://github.com/cloudflare/sandbox-sdk/pull/53) [`c87db11`](https://github.com/cloudflare/sandbox-sdk/commit/c87db117693a86cfb667bf09fb7720d6a6e0524d) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Improve jupyterlab config to speed up startup
|
|
14
|
+
|
|
15
|
+
## 0.2.2
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- [#51](https://github.com/cloudflare/sandbox-sdk/pull/51) [`4aceb32`](https://github.com/cloudflare/sandbox-sdk/commit/4aceb3215c836f59afcb88b2b325016b3f623f46) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Handle intermittent interpreter failures and decouple jupyter startup
|
|
20
|
+
|
|
3
21
|
## 0.2.1
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/Dockerfile
CHANGED
|
@@ -33,22 +33,17 @@ RUN apt-get update && apt-get install -y \
|
|
|
33
33
|
python3-pip \
|
|
34
34
|
python3.11-venv \
|
|
35
35
|
# Other useful tools
|
|
36
|
-
sudo \
|
|
37
36
|
ca-certificates \
|
|
38
37
|
gnupg \
|
|
39
38
|
lsb-release \
|
|
39
|
+
strace \
|
|
40
40
|
&& rm -rf /var/lib/apt/lists/*
|
|
41
41
|
|
|
42
42
|
# Set Python 3.11 as default python3
|
|
43
43
|
RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
|
|
44
44
|
|
|
45
|
-
# Install Node.js 20 LTS
|
|
46
|
-
|
|
47
|
-
RUN apt-get update && apt-get install -y ca-certificates curl gnupg \
|
|
48
|
-
&& mkdir -p /etc/apt/keyrings \
|
|
49
|
-
&& curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
|
|
50
|
-
&& echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list \
|
|
51
|
-
&& apt-get update \
|
|
45
|
+
# Install Node.js 20 LTS using official NodeSource setup script
|
|
46
|
+
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
|
|
52
47
|
&& apt-get install -y nodejs \
|
|
53
48
|
&& rm -rf /var/lib/apt/lists/*
|
|
54
49
|
|
|
@@ -56,20 +51,23 @@ RUN apt-get update && apt-get install -y ca-certificates curl gnupg \
|
|
|
56
51
|
COPY --from=bun-source /usr/local/bin/bun /usr/local/bin/bun
|
|
57
52
|
COPY --from=bun-source /usr/local/bin/bunx /usr/local/bin/bunx
|
|
58
53
|
|
|
59
|
-
# Install Jupyter
|
|
54
|
+
# Install minimal Jupyter components
|
|
60
55
|
RUN pip3 install --no-cache-dir \
|
|
61
|
-
jupyter \
|
|
62
|
-
|
|
56
|
+
jupyter-server \
|
|
57
|
+
jupyter-client \
|
|
63
58
|
ipykernel \
|
|
64
|
-
|
|
59
|
+
orjson \
|
|
60
|
+
&& python3 -m ipykernel install --user --name python3
|
|
61
|
+
|
|
62
|
+
# Install scientific packages
|
|
63
|
+
RUN pip3 install --no-cache-dir \
|
|
65
64
|
matplotlib \
|
|
66
65
|
numpy \
|
|
67
66
|
pandas \
|
|
68
|
-
seaborn
|
|
69
|
-
&& python3 -m ipykernel install --user --name python3
|
|
67
|
+
seaborn
|
|
70
68
|
|
|
71
69
|
# Install JavaScript kernel (ijavascript) - using E2B's fork
|
|
72
|
-
RUN npm install -g
|
|
70
|
+
RUN npm install -g git+https://github.com/e2b-dev/ijavascript.git \
|
|
73
71
|
&& ijsinstall --install=global
|
|
74
72
|
|
|
75
73
|
# Set up container server directory
|
|
@@ -84,11 +82,15 @@ RUN python3 --version && \
|
|
|
84
82
|
jupyter kernelspec list
|
|
85
83
|
|
|
86
84
|
# Copy container source files to server directory
|
|
87
|
-
COPY container_src/package.json ./
|
|
88
|
-
RUN bun install
|
|
85
|
+
COPY container_src/package.json container_src/bun.lock ./
|
|
86
|
+
RUN bun install --frozen-lockfile
|
|
89
87
|
|
|
90
88
|
COPY container_src/ ./
|
|
91
89
|
|
|
90
|
+
# Compile TypeScript control process
|
|
91
|
+
# Use npx -p typescript to ensure we get the right tsc command
|
|
92
|
+
RUN npx -p typescript tsc control-process.ts --outDir . --module commonjs --target es2020 --esModuleInterop --skipLibCheck
|
|
93
|
+
|
|
92
94
|
# Create clean workspace directory for users
|
|
93
95
|
RUN mkdir -p /workspace
|
|
94
96
|
|
package/README.md
CHANGED
|
@@ -72,7 +72,7 @@ npm install @cloudflare/sandbox
|
|
|
72
72
|
1. **Create a Dockerfile** (temporary requirement, will be removed in future releases):
|
|
73
73
|
|
|
74
74
|
```dockerfile
|
|
75
|
-
FROM docker.io/cloudflare/sandbox:0.2.
|
|
75
|
+
FROM docker.io/cloudflare/sandbox:0.2.4
|
|
76
76
|
|
|
77
77
|
# Expose the ports you want to expose
|
|
78
78
|
EXPOSE 3000
|
|
@@ -254,6 +254,14 @@ console.log(result.stdout); // "production"
|
|
|
254
254
|
- `unexposePort(port)` - Remove port exposure
|
|
255
255
|
- `getExposedPorts()` - List all exposed ports with their URLs
|
|
256
256
|
|
|
257
|
+
#### Session Methods
|
|
258
|
+
|
|
259
|
+
- `createSession(options)` - Create an isolated execution session
|
|
260
|
+
- `name`: Session identifier
|
|
261
|
+
- `env`: Environment variables for this session
|
|
262
|
+
- `cwd`: Working directory
|
|
263
|
+
- `isolation`: Enable PID namespace isolation (requires CAP_SYS_ADMIN)
|
|
264
|
+
|
|
257
265
|
<h2 id="code-interpreter">๐งช Code Interpreter</h2>
|
|
258
266
|
|
|
259
267
|
The Sandbox SDK includes powerful code interpreter capabilities, allowing you to execute Python and JavaScript code with rich outputs including charts, tables, and formatted data.
|
|
@@ -703,17 +711,71 @@ for await (const log of parseSSEStream<LogEvent>(logStream)) {
|
|
|
703
711
|
|
|
704
712
|
### Session Management
|
|
705
713
|
|
|
706
|
-
|
|
714
|
+
The SDK provides two approaches for managing execution context:
|
|
715
|
+
|
|
716
|
+
#### Implicit Sessions (Recommended)
|
|
717
|
+
|
|
718
|
+
Each sandbox maintains its own persistent session automatically:
|
|
707
719
|
|
|
708
720
|
```typescript
|
|
709
|
-
const
|
|
721
|
+
const sandbox = getSandbox(env.Sandbox, "my-app");
|
|
710
722
|
|
|
711
|
-
//
|
|
712
|
-
await sandbox.exec("cd /
|
|
713
|
-
await sandbox.exec("
|
|
714
|
-
|
|
723
|
+
// These commands share state (pwd, env vars, etc.)
|
|
724
|
+
await sandbox.exec("cd /app");
|
|
725
|
+
await sandbox.exec("pwd"); // Output: /app
|
|
726
|
+
await sandbox.exec("export MY_VAR=hello");
|
|
727
|
+
await sandbox.exec("echo $MY_VAR"); // Output: hello
|
|
715
728
|
```
|
|
716
729
|
|
|
730
|
+
#### Explicit Sessions for Advanced Use Cases
|
|
731
|
+
|
|
732
|
+
Create isolated execution contexts within the same sandbox:
|
|
733
|
+
|
|
734
|
+
```typescript
|
|
735
|
+
const sandbox = getSandbox(env.Sandbox, "multi-env");
|
|
736
|
+
|
|
737
|
+
// Create independent sessions with different environments
|
|
738
|
+
const buildSession = await sandbox.createSession({
|
|
739
|
+
name: "build",
|
|
740
|
+
env: { NODE_ENV: "production" },
|
|
741
|
+
cwd: "/build"
|
|
742
|
+
});
|
|
743
|
+
|
|
744
|
+
const testSession = await sandbox.createSession({
|
|
745
|
+
name: "test",
|
|
746
|
+
env: { NODE_ENV: "test" },
|
|
747
|
+
cwd: "/test"
|
|
748
|
+
});
|
|
749
|
+
|
|
750
|
+
// Run commands in parallel with different contexts
|
|
751
|
+
await Promise.all([
|
|
752
|
+
buildSession.exec("npm run build"),
|
|
753
|
+
testSession.exec("npm test")
|
|
754
|
+
]);
|
|
755
|
+
```
|
|
756
|
+
|
|
757
|
+
#### Security with AI Agents
|
|
758
|
+
|
|
759
|
+
When using AI coding agents, separate development from execution:
|
|
760
|
+
|
|
761
|
+
```typescript
|
|
762
|
+
// Phase 1: AI agent writes code (with API keys)
|
|
763
|
+
const devSession = await sandbox.createSession({
|
|
764
|
+
name: "ai-development",
|
|
765
|
+
env: { ANTHROPIC_API_KEY: process.env.ANTHROPIC_API_KEY }
|
|
766
|
+
});
|
|
767
|
+
await devSession.exec('opencode "build a web server"');
|
|
768
|
+
|
|
769
|
+
// Phase 2: Run the generated code (without API keys)
|
|
770
|
+
const appSession = await sandbox.createSession({
|
|
771
|
+
name: "app-runtime",
|
|
772
|
+
env: { PORT: "3000" } // Only app-specific vars
|
|
773
|
+
});
|
|
774
|
+
await appSession.exec("node server.js");
|
|
775
|
+
```
|
|
776
|
+
|
|
777
|
+
> **Best Practice**: Keep AI agent credentials separate from your application runtime to prevent accidental exposure of API keys.
|
|
778
|
+
|
|
717
779
|
<h2 id="debugging">๐ Debugging</h2>
|
|
718
780
|
|
|
719
781
|
Enable verbose logging:
|