@cloudflare/sandbox 0.0.0-db09b4d → 0.0.0-dcf36ef

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 (43) hide show
  1. package/CHANGELOG.md +235 -0
  2. package/Dockerfile +103 -10
  3. package/README.md +859 -22
  4. package/container_src/bun.lock +76 -0
  5. package/container_src/circuit-breaker.ts +121 -0
  6. package/container_src/control-process.ts +784 -0
  7. package/container_src/handler/exec.ts +185 -0
  8. package/container_src/handler/file.ts +457 -0
  9. package/container_src/handler/git.ts +130 -0
  10. package/container_src/handler/ports.ts +314 -0
  11. package/container_src/handler/process.ts +568 -0
  12. package/container_src/handler/session.ts +92 -0
  13. package/container_src/index.ts +447 -2466
  14. package/container_src/interpreter-service.ts +276 -0
  15. package/container_src/isolation.ts +1213 -0
  16. package/container_src/mime-processor.ts +255 -0
  17. package/container_src/package.json +9 -0
  18. package/container_src/runtime/executors/javascript/node_executor.ts +123 -0
  19. package/container_src/runtime/executors/python/ipython_executor.py +338 -0
  20. package/container_src/runtime/executors/typescript/ts_executor.ts +138 -0
  21. package/container_src/runtime/process-pool.ts +464 -0
  22. package/container_src/shell-escape.ts +42 -0
  23. package/container_src/startup.sh +11 -0
  24. package/container_src/types.ts +131 -0
  25. package/package.json +6 -8
  26. package/src/client.ts +510 -1186
  27. package/src/errors.ts +219 -0
  28. package/src/file-stream.ts +162 -0
  29. package/src/index.ts +78 -76
  30. package/src/interpreter-client.ts +352 -0
  31. package/src/interpreter-types.ts +390 -0
  32. package/src/interpreter.ts +150 -0
  33. package/src/request-handler.ts +144 -0
  34. package/src/sandbox.ts +756 -0
  35. package/src/security.ts +113 -0
  36. package/src/sse-parser.ts +147 -0
  37. package/src/types.ts +571 -0
  38. package/tsconfig.json +1 -1
  39. package/tests/client.example.ts +0 -308
  40. package/tests/connection-test.ts +0 -81
  41. package/tests/simple-test.ts +0 -81
  42. package/tests/test1.ts +0 -281
  43. package/tests/test2.ts +0 -710
package/CHANGELOG.md CHANGED
@@ -1,5 +1,240 @@
1
1
  # @cloudflare/sandbox
2
2
 
3
+ ## 0.3.7
4
+
5
+ ### Patch Changes
6
+
7
+ - [#98](https://github.com/cloudflare/sandbox-sdk/pull/98) [`d951819`](https://github.com/cloudflare/sandbox-sdk/commit/d951819b18e9cff9ce09851c6855f05039973b65) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Bake wrangler, vite & opencode in base image
8
+
9
+ ## 0.3.6
10
+
11
+ ### Patch Changes
12
+
13
+ - [#90](https://github.com/cloudflare/sandbox-sdk/pull/90) [`66cc85b`](https://github.com/cloudflare/sandbox-sdk/commit/66cc85b679b466b3ffb1f00fbd697670fc186f06) Thanks [@eastlondoner](https://github.com/eastlondoner)! - set bun idletimeout
14
+
15
+ ## 0.3.5
16
+
17
+ ### Patch Changes
18
+
19
+ - [#88](https://github.com/cloudflare/sandbox-sdk/pull/88) [`46eb4e6`](https://github.com/cloudflare/sandbox-sdk/commit/46eb4e6b6c671b682fc74f83563ccf5f316011cb) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Add binary file support with automatic MIME detection and streaming
20
+
21
+ ## 0.3.4
22
+
23
+ ### Patch Changes
24
+
25
+ - [#86](https://github.com/cloudflare/sandbox-sdk/pull/86) [`feafd32`](https://github.com/cloudflare/sandbox-sdk/commit/feafd32a51f50dfaf4994bddcbfb56d46cada622) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Fix session reuse to reuse existing healthy session
26
+
27
+ ## 0.3.3
28
+
29
+ ### Patch Changes
30
+
31
+ - [#83](https://github.com/cloudflare/sandbox-sdk/pull/83) [`eec5bb6`](https://github.com/cloudflare/sandbox-sdk/commit/eec5bb6203dd5d775b4b54e91c26de25eeb767ce) Thanks [@mikenomitch](https://github.com/mikenomitch)! - Bump containers package version
32
+
33
+ ## 0.3.2
34
+
35
+ ### Patch Changes
36
+
37
+ - [#76](https://github.com/cloudflare/sandbox-sdk/pull/76) [`ef9e320`](https://github.com/cloudflare/sandbox-sdk/commit/ef9e320dcef30e57797fef6ebd9a9383fa9720d9) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Replace Jupyter with lightweight interpreters for >90% faster cold starts for `.runCode` calls, while maintaining full code execution capabilities and rich output support.
38
+
39
+ ## 0.3.1
40
+
41
+ ### Patch Changes
42
+
43
+ - [#71](https://github.com/cloudflare/sandbox-sdk/pull/71) [`fb3c9c2`](https://github.com/cloudflare/sandbox-sdk/commit/fb3c9c22242d9d4f157c26f547f1e697ef7875f9) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Bump containers package version
44
+
45
+ - [#70](https://github.com/cloudflare/sandbox-sdk/pull/70) [`e1fa354`](https://github.com/cloudflare/sandbox-sdk/commit/e1fa354ab1bc7b0e89db4901b67028ebf1a93d0a) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Fix escaped quotes in file write operations
46
+
47
+ - [#68](https://github.com/cloudflare/sandbox-sdk/pull/68) [`69b91d1`](https://github.com/cloudflare/sandbox-sdk/commit/69b91d1a8f6afb63262cc381ea93e94a033ed5e8) Thanks [@CyrusNuevoDia](https://github.com/CyrusNuevoDia)! - Configurable timeouts via environment variables in isolation.ts
48
+
49
+ - [#66](https://github.com/cloudflare/sandbox-sdk/pull/66) [`eca93b9`](https://github.com/cloudflare/sandbox-sdk/commit/eca93b97e40fa0d3bd9dc27af2cc214ec355b696) Thanks [@peterp](https://github.com/peterp)! - Determine if the port is specified in the URL.
50
+
51
+ ## 0.3.0
52
+
53
+ ### Minor Changes
54
+
55
+ - [#59](https://github.com/cloudflare/sandbox-sdk/pull/59) [`b6757f7`](https://github.com/cloudflare/sandbox-sdk/commit/b6757f730c34381d5a70d513944bbf9840f598ab) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Add process isolation for sandbox commands
56
+
57
+ Implements PID namespace isolation to protect control plane processes (Jupyter, Bun) from sandboxed code. Commands executed via `exec()` now run in isolated namespaces that cannot see or interact with system processes.
58
+
59
+ **Key security improvements:**
60
+
61
+ - Control plane processes are hidden from sandboxed commands
62
+ - Platform secrets in `/proc/1/environ` are inaccessible
63
+ - Ports 8888 (Jupyter) and 3000 (Bun) are protected from hijacking
64
+
65
+ **Breaking changes:**
66
+
67
+ 1. **Removed `sessionId` parameter**: The `sessionId` parameter has been removed from all methods (`exec()`, `execStream()`, `startProcess()`, etc.). Each sandbox now maintains its own persistent session automatically.
68
+
69
+ ```javascript
70
+ // Before: manual session management
71
+ await sandbox.exec("cd /app", { sessionId: "my-session" });
72
+
73
+ // After: automatic session per sandbox
74
+ await sandbox.exec("cd /app");
75
+ ```
76
+
77
+ 2. **Commands now maintain state**: Commands within the same sandbox now share state (working directory, environment variables, background processes). Previously each command was stateless.
78
+
79
+ ```javascript
80
+ // Before: each exec was independent
81
+ await sandbox.exec("cd /app");
82
+ await sandbox.exec("pwd"); // Output: /workspace
83
+
84
+ // After: state persists in session
85
+ await sandbox.exec("cd /app");
86
+ await sandbox.exec("pwd"); // Output: /app
87
+ ```
88
+
89
+ **Migration guide:**
90
+
91
+ - Remove `sessionId` from all method calls - each sandbox maintains its own session
92
+ - If you need isolated execution contexts within the same sandbox, use `sandbox.createSession()`:
93
+ ```javascript
94
+ // Create independent sessions with different environments
95
+ const buildSession = await sandbox.createSession({
96
+ name: "build",
97
+ env: { NODE_ENV: "production" },
98
+ cwd: "/build",
99
+ });
100
+ const testSession = await sandbox.createSession({
101
+ name: "test",
102
+ env: { NODE_ENV: "test" },
103
+ cwd: "/test",
104
+ });
105
+ ```
106
+ - Environment variables set in one command persist to the next
107
+ - Background processes remain active until explicitly killed
108
+ - Requires CAP_SYS_ADMIN (available in production, falls back gracefully in dev)
109
+
110
+ ### Patch Changes
111
+
112
+ - [#62](https://github.com/cloudflare/sandbox-sdk/pull/62) [`4bedc3a`](https://github.com/cloudflare/sandbox-sdk/commit/4bedc3aba347f3d4090a6efe2c9778bac00ce74a) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Fix broken build due to bun lockfile not being used
113
+
114
+ ## 0.2.4
115
+
116
+ ### Patch Changes
117
+
118
+ - [#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
119
+
120
+ ## 0.2.3
121
+
122
+ ### Patch Changes
123
+
124
+ - [#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
125
+
126
+ ## 0.2.2
127
+
128
+ ### Patch Changes
129
+
130
+ - [#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
131
+
132
+ ## 0.2.1
133
+
134
+ ### Patch Changes
135
+
136
+ - [#49](https://github.com/cloudflare/sandbox-sdk/pull/49) [`d81d2a5`](https://github.com/cloudflare/sandbox-sdk/commit/d81d2a563c9af8947d5444019ed4d6156db563e3) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Implement code interpreter API
137
+
138
+ ## 0.2.0
139
+
140
+ ### Minor Changes
141
+
142
+ - [#47](https://github.com/cloudflare/sandbox-sdk/pull/47) [`8a93d0c`](https://github.com/cloudflare/sandbox-sdk/commit/8a93d0cae18a25bda6506b8b0a08d9e9eb3bb290) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Change default directory to a clean /workspace
143
+
144
+ ## 0.1.4
145
+
146
+ ### Patch Changes
147
+
148
+ - [#46](https://github.com/cloudflare/sandbox-sdk/pull/46) [`7de28be`](https://github.com/cloudflare/sandbox-sdk/commit/7de28be482d9634551572d548c7c4b5842df812d) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Update README
149
+
150
+ - [#44](https://github.com/cloudflare/sandbox-sdk/pull/44) [`215ab49`](https://github.com/cloudflare/sandbox-sdk/commit/215ab494427d7e2a92bb9a25384cb493a221c200) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Update example to use env & cwd
151
+
152
+ - [#42](https://github.com/cloudflare/sandbox-sdk/pull/42) [`bb72193`](https://github.com/cloudflare/sandbox-sdk/commit/bb72193ad75695979bd1132206f481e91fe37325) Thanks [@jonasnobile](https://github.com/jonasnobile)! - Propagate `cwd` and `env` options in `executeCommand`
153
+
154
+ - [#27](https://github.com/cloudflare/sandbox-sdk/pull/27) [`fd5ec7f`](https://github.com/cloudflare/sandbox-sdk/commit/fd5ec7f34bc12b06320a89356c4af07801f52d64) Thanks [@threepointone](https://github.com/threepointone)! - remove yarn and pnpm from the image
155
+
156
+ ## 0.1.3
157
+
158
+ ### Patch Changes
159
+
160
+ - [#32](https://github.com/cloudflare/sandbox-sdk/pull/32) [`1a42464`](https://github.com/cloudflare/sandbox-sdk/commit/1a4246479369c5d0160705caf192aa1816540d52) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Bring back package README
161
+
162
+ ## 0.1.2
163
+
164
+ ### Patch Changes
165
+
166
+ - [#30](https://github.com/cloudflare/sandbox-sdk/pull/30) [`30e5c25`](https://github.com/cloudflare/sandbox-sdk/commit/30e5c25cf7d4b07f9049724206c531e2d5d29d5c) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Remove actions timeout
167
+
168
+ - [#29](https://github.com/cloudflare/sandbox-sdk/pull/29) [`d78508f`](https://github.com/cloudflare/sandbox-sdk/commit/d78508f7287a59e0423edd2999c2c83e9e34ccfd) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Create multi-platform Docker image and switch to Cloudflare official repo
169
+
170
+ ## 0.1.1
171
+
172
+ ### Patch Changes
173
+
174
+ - [`157dde9`](https://github.com/cloudflare/sandbox-sdk/commit/157dde9b1f23e9bb6f3e9c3f0514b639a8813897) Thanks [@threepointone](https://github.com/threepointone)! - update deps
175
+
176
+ - [`a04f6b6`](https://github.com/cloudflare/sandbox-sdk/commit/a04f6b6c0b2ef9e3ce0851b53769f1c10d8c6de6) Thanks [@threepointone](https://github.com/threepointone)! - trigger a build with updated deps
177
+
178
+ ## 0.1.0
179
+
180
+ ### Minor Changes
181
+
182
+ - [#24](https://github.com/cloudflare/sandbox-sdk/pull/24) [`cecde0a`](https://github.com/cloudflare/sandbox-sdk/commit/cecde0a7530a87deffd8562fb8b01d66ee80ee19) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Redesign command execution API
183
+
184
+ ### Patch Changes
185
+
186
+ - [#22](https://github.com/cloudflare/sandbox-sdk/pull/22) [`f5fcd52`](https://github.com/cloudflare/sandbox-sdk/commit/f5fcd52025d1f7958a374e69d75e3fc590275f3f) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Allow setting env variables dynamically and remove command restrictions
187
+
188
+ ## 0.0.9
189
+
190
+ ### Patch Changes
191
+
192
+ - [#20](https://github.com/cloudflare/sandbox-sdk/pull/20) [`f106fda`](https://github.com/cloudflare/sandbox-sdk/commit/f106fdac98e7ef35677326290d45cbf3af88982c) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - add preview URLs and dynamic port forwarding
193
+
194
+ ## 0.0.8
195
+
196
+ ### Patch Changes
197
+
198
+ - [`60af265`](https://github.com/cloudflare/sandbox-sdk/commit/60af265d834e83fd30a921a3e1be232f13fe24da) Thanks [@threepointone](https://github.com/threepointone)! - update dependencies
199
+
200
+ ## 0.0.7
201
+
202
+ ### Patch Changes
203
+
204
+ - [`d1c7c99`](https://github.com/cloudflare/sandbox-sdk/commit/d1c7c99df6555eff71bcd59852e4b8eed2ad8cb6) Thanks [@threepointone](https://github.com/threepointone)! - fix file operations
205
+
206
+ ## 0.0.6
207
+
208
+ ### Patch Changes
209
+
210
+ - [#9](https://github.com/cloudflare/sandbox-sdk/pull/9) [`24f5470`](https://github.com/cloudflare/sandbox-sdk/commit/24f547048d5a26137de4656cea13d83ad2cc0b43) Thanks [@ItsWendell](https://github.com/ItsWendell)! - fix baseUrl for stub and stub forwarding
211
+
212
+ ## 0.0.5
213
+
214
+ ### Patch Changes
215
+
216
+ - [#5](https://github.com/cloudflare/sandbox-sdk/pull/5) [`7c15b81`](https://github.com/cloudflare/sandbox-sdk/commit/7c15b817899e4d9e1f25747aaf439e5e9e880d15) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Make package ready for deployment
217
+
218
+ ## 0.0.4
219
+
220
+ ### Patch Changes
221
+
222
+ - [`c0d9d33`](https://github.com/cloudflare/sandbox-sdk/commit/c0d9d3396badee1eab45e6b4a73d48957f31409b) Thanks [@threepointone](https://github.com/threepointone)! - actually work
223
+
224
+ - [`444d2da`](https://github.com/cloudflare/sandbox-sdk/commit/444d2dafde9a0f190e50c879b0e768da1b289b51) Thanks [@threepointone](https://github.com/threepointone)! - add experimental label
225
+
226
+ ## 0.0.3
227
+
228
+ ### Patch Changes
229
+
230
+ - [`2b087c4`](https://github.com/cloudflare/sandbox-sdk/commit/2b087c40a29697c20dad19b4e3b8512f5d404bd3) Thanks [@ghostwriternr](https://github.com/ghostwriternr)! - Fix worker unable to find container port
231
+
232
+ ## 0.0.2
233
+
234
+ ### Patch Changes
235
+
236
+ - [`52f02f0`](https://github.com/cloudflare/sandbox-sdk/commit/52f02f0625ef9f8eac695e51f93fa79651c0206d) Thanks [@threepointone](https://github.com/threepointone)! - readFile
237
+
3
238
  ## 0.0.1
4
239
 
5
240
  ### Patch Changes
package/Dockerfile CHANGED
@@ -1,16 +1,109 @@
1
- # syntax=docker/dockerfile:1
1
+ # Sandbox base image with development tools, Python, Node.js, and Bun
2
+ FROM oven/bun:latest AS bun-source
3
+ FROM ubuntu:22.04
2
4
 
3
- FROM oven/bun:latest
4
- # Set destination for COPY
5
- WORKDIR /app
5
+ # Prevent interactive prompts during package installation
6
+ ENV DEBIAN_FRONTEND=noninteractive
6
7
 
7
- # Install git
8
- RUN apt-get update && apt-get install -y git
8
+ # Install essential system packages and development tools
9
+ RUN apt-get update && apt-get install -y \
10
+ # Basic utilities
11
+ curl \
12
+ wget \
13
+ git \
14
+ unzip \
15
+ zip \
16
+ file \
17
+ # Process management
18
+ procps \
19
+ htop \
20
+ # Build tools
21
+ build-essential \
22
+ pkg-config \
23
+ # Network tools
24
+ net-tools \
25
+ iputils-ping \
26
+ dnsutils \
27
+ # Text processing
28
+ jq \
29
+ vim \
30
+ nano \
31
+ # Python dependencies
32
+ python3.11 \
33
+ python3.11-dev \
34
+ python3-pip \
35
+ python3.11-venv \
36
+ # Other useful tools
37
+ ca-certificates \
38
+ gnupg \
39
+ lsb-release \
40
+ strace \
41
+ && rm -rf /var/lib/apt/lists/*
9
42
 
10
- COPY container_src/* ./
11
- # RUN bun install
43
+ # Set Python 3.11 as default python3
44
+ RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
12
45
 
46
+ # Install Node.js 20 LTS using official NodeSource setup script
47
+ RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \
48
+ && apt-get install -y nodejs \
49
+ && rm -rf /var/lib/apt/lists/*
50
+
51
+ # Install Bun from official image (avoids architecture compatibility issues)
52
+ COPY --from=bun-source /usr/local/bin/bun /usr/local/bin/bun
53
+ COPY --from=bun-source /usr/local/bin/bunx /usr/local/bin/bunx
54
+
55
+ # Install development tools globally
56
+ RUN npm install -g \
57
+ wrangler \
58
+ vite \
59
+ opencode-ai
60
+
61
+ # Install essential Python packages for code execution
62
+ RUN pip3 install --no-cache-dir \
63
+ matplotlib \
64
+ numpy \
65
+ pandas \
66
+ ipython
67
+
68
+ # Set up container server directory
69
+ WORKDIR /container-server
70
+
71
+ # Verify installations
72
+ RUN python3 --version && \
73
+ node --version && \
74
+ npm --version && \
75
+ bun --version
76
+
77
+ # Copy container source files to server directory
78
+ COPY container_src/package.json container_src/bun.lock ./
79
+ RUN bun install --frozen-lockfile
80
+
81
+ COPY container_src/ ./
82
+
83
+ # Compile TypeScript files using the locally installed TypeScript
84
+ RUN npx tsc control-process.ts --outDir . --module commonjs --target es2020 --esModuleInterop --skipLibCheck
85
+ RUN cd runtime/executors/javascript && npx tsc node_executor.ts --module commonjs --target es2020 --esModuleInterop --skipLibCheck
86
+ RUN cd runtime/executors/typescript && npx tsc ts_executor.ts --module commonjs --target es2020 --esModuleInterop --skipLibCheck
87
+
88
+ # Configure process pool sizes (can be overridden at runtime)
89
+ ENV PYTHON_POOL_MIN_SIZE=3
90
+ ENV PYTHON_POOL_MAX_SIZE=15
91
+ ENV JAVASCRIPT_POOL_MIN_SIZE=3
92
+ ENV JAVASCRIPT_POOL_MAX_SIZE=10
93
+ ENV TYPESCRIPT_POOL_MIN_SIZE=3
94
+ ENV TYPESCRIPT_POOL_MAX_SIZE=10
95
+
96
+ # Create clean workspace directory for user code
97
+ # Architecture:
98
+ # /container-server/ - SDK infrastructure (server, executors, dependencies)
99
+ # /workspace/ - User's clean workspace for their code
100
+ RUN mkdir -p /workspace
101
+
102
+ # Expose the application port (3000 for control)
13
103
  EXPOSE 3000
14
- # Run
15
- CMD ["bun", "index.ts"]
16
104
 
105
+ # Make startup script executable
106
+ RUN chmod +x startup.sh
107
+
108
+ # Use startup script
109
+ CMD ["./startup.sh"]