@cloudflare/sandbox 0.0.9 → 0.1.0

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 +10 -0
  2. package/Dockerfile +1 -14
  3. package/container_src/handler/exec.ts +337 -0
  4. package/container_src/handler/file.ts +844 -0
  5. package/container_src/handler/git.ts +182 -0
  6. package/container_src/handler/ports.ts +314 -0
  7. package/container_src/handler/process.ts +640 -0
  8. package/container_src/index.ts +82 -2973
  9. package/container_src/types.ts +103 -0
  10. package/dist/chunk-6THNBO4S.js +46 -0
  11. package/dist/chunk-6THNBO4S.js.map +1 -0
  12. package/dist/chunk-6UAWTJ5S.js +85 -0
  13. package/dist/chunk-6UAWTJ5S.js.map +1 -0
  14. package/dist/chunk-G4XT4SP7.js +638 -0
  15. package/dist/chunk-G4XT4SP7.js.map +1 -0
  16. package/dist/chunk-ISFOIYQC.js +585 -0
  17. package/dist/chunk-ISFOIYQC.js.map +1 -0
  18. package/dist/chunk-NNGBXDMY.js +89 -0
  19. package/dist/chunk-NNGBXDMY.js.map +1 -0
  20. package/dist/client-Da-mLX4p.d.ts +210 -0
  21. package/dist/client.d.ts +2 -1
  22. package/dist/client.js +3 -37
  23. package/dist/index.d.ts +3 -1
  24. package/dist/index.js +13 -3
  25. package/dist/request-handler.d.ts +2 -1
  26. package/dist/request-handler.js +4 -2
  27. package/dist/sandbox.d.ts +2 -1
  28. package/dist/sandbox.js +4 -2
  29. package/dist/security.d.ts +30 -0
  30. package/dist/security.js +13 -0
  31. package/dist/security.js.map +1 -0
  32. package/dist/sse-parser.d.ts +28 -0
  33. package/dist/sse-parser.js +11 -0
  34. package/dist/sse-parser.js.map +1 -0
  35. package/dist/types.d.ts +284 -0
  36. package/dist/types.js +19 -0
  37. package/dist/types.js.map +1 -0
  38. package/package.json +2 -7
  39. package/src/client.ts +235 -1286
  40. package/src/index.ts +6 -0
  41. package/src/request-handler.ts +69 -20
  42. package/src/sandbox.ts +463 -70
  43. package/src/security.ts +113 -0
  44. package/src/sse-parser.ts +147 -0
  45. package/src/types.ts +386 -0
  46. package/README.md +0 -65
  47. package/dist/chunk-4J5LQCCN.js +0 -1446
  48. package/dist/chunk-4J5LQCCN.js.map +0 -1
  49. package/dist/chunk-5SZ3RVJZ.js +0 -250
  50. package/dist/chunk-5SZ3RVJZ.js.map +0 -1
  51. package/dist/client-BuVjqV00.d.ts +0 -247
  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/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @cloudflare/sandbox
2
2
 
3
+ ## 0.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#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
8
+
9
+ ### Patch Changes
10
+
11
+ - [#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
12
+
3
13
  ## 0.0.9
4
14
 
5
15
  ### Patch Changes
package/Dockerfile CHANGED
@@ -59,22 +59,9 @@ RUN curl -fsSL https://bun.sh/install | bash \
59
59
  # Install global npm packages as root
60
60
  RUN npm install -g yarn pnpm
61
61
 
62
- # Create a non-root user for running applications
63
- RUN useradd -m -s /bin/bash sandbox \
64
- && echo "sandbox ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
65
-
66
62
  # Set up working directory
67
63
  WORKDIR /app
68
64
 
69
- # Set ownership of the app directory to sandbox user
70
- RUN chown -R sandbox:sandbox /app
71
-
72
- # Set environment variables
73
- ENV PATH="/home/sandbox/.local/bin:${PATH}"
74
-
75
- # Switch to non-root user
76
- USER sandbox
77
-
78
65
  # Verify installations
79
66
  RUN python3 --version && \
80
67
  node --version && \
@@ -84,7 +71,7 @@ RUN python3 --version && \
84
71
  pnpm --version
85
72
 
86
73
  # Copy container source files
87
- COPY --chown=sandbox:sandbox container_src/* ./
74
+ COPY container_src/ ./
88
75
 
89
76
  # Expose the application port
90
77
  EXPOSE 3000
@@ -0,0 +1,337 @@
1
+ import { type SpawnOptions, spawn } from "node:child_process";
2
+ import type { ExecuteRequest, SessionData } from "../types";
3
+
4
+ function executeCommand(
5
+ sessions: Map<string, SessionData>,
6
+ command: string,
7
+ sessionId?: string,
8
+ background?: boolean
9
+ ): Promise<{
10
+ success: boolean;
11
+ stdout: string;
12
+ stderr: string;
13
+ exitCode: number;
14
+ }> {
15
+ return new Promise((resolve, reject) => {
16
+ const spawnOptions: SpawnOptions = {
17
+ shell: true,
18
+ stdio: ["pipe", "pipe", "pipe"] as const,
19
+ detached: background || false,
20
+ };
21
+
22
+ const child = spawn(command, spawnOptions);
23
+
24
+ // Store the process reference for cleanup if sessionId is provided
25
+ if (sessionId && sessions.has(sessionId)) {
26
+ const session = sessions.get(sessionId)!;
27
+ session.activeProcess = child;
28
+ }
29
+
30
+ let stdout = "";
31
+ let stderr = "";
32
+
33
+ child.stdout?.on("data", (data) => {
34
+ stdout += data.toString();
35
+ });
36
+
37
+ child.stderr?.on("data", (data) => {
38
+ stderr += data.toString();
39
+ });
40
+
41
+ if (background) {
42
+ // For background processes, unref and return quickly
43
+ child.unref();
44
+
45
+ // Collect initial output for 100ms then return
46
+ setTimeout(() => {
47
+ resolve({
48
+ exitCode: 0, // Process is still running
49
+ stderr,
50
+ stdout,
51
+ success: true,
52
+ });
53
+ }, 100);
54
+
55
+ // Still handle errors
56
+ child.on("error", (error) => {
57
+ console.error(`[Server] Background process error: ${command}`, error);
58
+ // Don't reject since we might have already resolved
59
+ });
60
+ } else {
61
+ // Normal synchronous execution
62
+ child.on("close", (code) => {
63
+ // Clear the active process reference
64
+ if (sessionId && sessions.has(sessionId)) {
65
+ const session = sessions.get(sessionId)!;
66
+ session.activeProcess = null;
67
+ }
68
+
69
+ console.log(`[Server] Command completed: ${command}, Exit code: ${code}`);
70
+
71
+ resolve({
72
+ exitCode: code || 0,
73
+ stderr,
74
+ stdout,
75
+ success: code === 0,
76
+ });
77
+ });
78
+
79
+ child.on("error", (error) => {
80
+ // Clear the active process reference
81
+ if (sessionId && sessions.has(sessionId)) {
82
+ const session = sessions.get(sessionId)!;
83
+ session.activeProcess = null;
84
+ }
85
+
86
+ reject(error);
87
+ });
88
+ }
89
+ });
90
+ }
91
+
92
+ export async function handleExecuteRequest(
93
+ sessions: Map<string, SessionData>,
94
+ req: Request,
95
+ corsHeaders: Record<string, string>
96
+ ): Promise<Response> {
97
+ try {
98
+ const body = (await req.json()) as ExecuteRequest;
99
+ const { command, sessionId, background } = body;
100
+
101
+ if (!command || typeof command !== "string") {
102
+ return new Response(
103
+ JSON.stringify({
104
+ error: "Command is required and must be a string",
105
+ }),
106
+ {
107
+ headers: {
108
+ "Content-Type": "application/json",
109
+ ...corsHeaders,
110
+ },
111
+ status: 400,
112
+ }
113
+ );
114
+ }
115
+
116
+ console.log(`[Server] Executing command: ${command}`);
117
+
118
+ const result = await executeCommand(sessions, command, sessionId, background);
119
+
120
+ return new Response(
121
+ JSON.stringify({
122
+ command,
123
+ exitCode: result.exitCode,
124
+ stderr: result.stderr,
125
+ stdout: result.stdout,
126
+ success: result.success,
127
+ timestamp: new Date().toISOString(),
128
+ }),
129
+ {
130
+ headers: {
131
+ "Content-Type": "application/json",
132
+ ...corsHeaders,
133
+ },
134
+ }
135
+ );
136
+ } catch (error) {
137
+ console.error("[Server] Error in handleExecuteRequest:", error);
138
+ return new Response(
139
+ JSON.stringify({
140
+ error: "Failed to execute command",
141
+ message: error instanceof Error ? error.message : "Unknown error",
142
+ }),
143
+ {
144
+ headers: {
145
+ "Content-Type": "application/json",
146
+ ...corsHeaders,
147
+ },
148
+ status: 500,
149
+ }
150
+ );
151
+ }
152
+ }
153
+
154
+ export async function handleStreamingExecuteRequest(
155
+ sessions: Map<string, SessionData>,
156
+ req: Request,
157
+ corsHeaders: Record<string, string>
158
+ ): Promise<Response> {
159
+ try {
160
+ const body = (await req.json()) as ExecuteRequest;
161
+ const { command, sessionId, background } = body;
162
+
163
+ if (!command || typeof command !== "string") {
164
+ return new Response(
165
+ JSON.stringify({
166
+ error: "Command is required and must be a string",
167
+ }),
168
+ {
169
+ headers: {
170
+ "Content-Type": "application/json",
171
+ ...corsHeaders,
172
+ },
173
+ status: 400,
174
+ }
175
+ );
176
+ }
177
+
178
+ console.log(
179
+ `[Server] Executing streaming command: ${command}`
180
+ );
181
+
182
+ const stream = new ReadableStream({
183
+ start(controller) {
184
+ const spawnOptions: SpawnOptions = {
185
+ shell: true,
186
+ stdio: ["pipe", "pipe", "pipe"] as const,
187
+ detached: background || false,
188
+ };
189
+
190
+ const child = spawn(command, spawnOptions);
191
+
192
+ // Store the process reference for cleanup if sessionId is provided
193
+ if (sessionId && sessions.has(sessionId)) {
194
+ const session = sessions.get(sessionId)!;
195
+ session.activeProcess = child;
196
+ }
197
+
198
+ // For background processes, unref to prevent blocking
199
+ if (background) {
200
+ child.unref();
201
+ }
202
+
203
+ let stdout = "";
204
+ let stderr = "";
205
+
206
+ // Send command start event
207
+ controller.enqueue(
208
+ new TextEncoder().encode(
209
+ `data: ${JSON.stringify({
210
+ type: "start",
211
+ timestamp: new Date().toISOString(),
212
+ command,
213
+ background: background || false,
214
+ })}\n\n`
215
+ )
216
+ );
217
+
218
+ child.stdout?.on("data", (data) => {
219
+ const output = data.toString();
220
+ stdout += output;
221
+
222
+ // Send real-time output
223
+ controller.enqueue(
224
+ new TextEncoder().encode(
225
+ `data: ${JSON.stringify({
226
+ type: "stdout",
227
+ timestamp: new Date().toISOString(),
228
+ data: output,
229
+ command,
230
+ })}\n\n`
231
+ )
232
+ );
233
+ });
234
+
235
+ child.stderr?.on("data", (data) => {
236
+ const output = data.toString();
237
+ stderr += output;
238
+
239
+ // Send real-time error output
240
+ controller.enqueue(
241
+ new TextEncoder().encode(
242
+ `data: ${JSON.stringify({
243
+ type: "stderr",
244
+ timestamp: new Date().toISOString(),
245
+ data: output,
246
+ command,
247
+ })}\n\n`
248
+ )
249
+ );
250
+ });
251
+
252
+ child.on("close", (code) => {
253
+ // Clear the active process reference
254
+ if (sessionId && sessions.has(sessionId)) {
255
+ const session = sessions.get(sessionId)!;
256
+ session.activeProcess = null;
257
+ }
258
+
259
+ console.log(
260
+ `[Server] Command completed: ${command}, Exit code: ${code}`
261
+ );
262
+
263
+ // Send command completion event
264
+ controller.enqueue(
265
+ new TextEncoder().encode(
266
+ `data: ${JSON.stringify({
267
+ type: "complete",
268
+ timestamp: new Date().toISOString(),
269
+ command,
270
+ exitCode: code,
271
+ result: {
272
+ success: code === 0,
273
+ exitCode: code,
274
+ stdout,
275
+ stderr,
276
+ command,
277
+ timestamp: new Date().toISOString(),
278
+ },
279
+ })}\n\n`
280
+ )
281
+ );
282
+
283
+ // For non-background processes, close the stream
284
+ // For background processes with streaming, the stream stays open
285
+ if (!background) {
286
+ controller.close();
287
+ }
288
+ });
289
+
290
+ child.on("error", (error) => {
291
+ // Clear the active process reference
292
+ if (sessionId && sessions.has(sessionId)) {
293
+ const session = sessions.get(sessionId)!;
294
+ session.activeProcess = null;
295
+ }
296
+
297
+ controller.enqueue(
298
+ new TextEncoder().encode(
299
+ `data: ${JSON.stringify({
300
+ type: "error",
301
+ timestamp: new Date().toISOString(),
302
+ error: error.message,
303
+ command,
304
+ })}\n\n`
305
+ )
306
+ );
307
+
308
+ controller.close();
309
+ });
310
+ },
311
+ });
312
+
313
+ return new Response(stream, {
314
+ headers: {
315
+ "Cache-Control": "no-cache",
316
+ Connection: "keep-alive",
317
+ "Content-Type": "text/event-stream",
318
+ ...corsHeaders,
319
+ },
320
+ });
321
+ } catch (error) {
322
+ console.error("[Server] Error in handleStreamingExecuteRequest:", error);
323
+ return new Response(
324
+ JSON.stringify({
325
+ error: "Failed to execute streaming command",
326
+ message: error instanceof Error ? error.message : "Unknown error",
327
+ }),
328
+ {
329
+ headers: {
330
+ "Content-Type": "application/json",
331
+ ...corsHeaders,
332
+ },
333
+ status: 500,
334
+ }
335
+ );
336
+ }
337
+ }