@cloudflare/sandbox 0.2.4 → 0.3.1

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 +75 -0
  2. package/Dockerfile +9 -11
  3. package/README.md +69 -7
  4. package/container_src/control-process.ts +784 -0
  5. package/container_src/handler/exec.ts +99 -254
  6. package/container_src/handler/file.ts +179 -837
  7. package/container_src/handler/git.ts +28 -80
  8. package/container_src/handler/process.ts +443 -515
  9. package/container_src/handler/session.ts +92 -0
  10. package/container_src/index.ts +68 -130
  11. package/container_src/isolation.ts +1038 -0
  12. package/container_src/shell-escape.ts +42 -0
  13. package/container_src/types.ts +27 -13
  14. package/dist/{chunk-HHUDRGPY.js → chunk-BEQUGUY4.js} +2 -2
  15. package/dist/{chunk-CKIGERRS.js → chunk-LFLJGISB.js} +240 -264
  16. package/dist/chunk-LFLJGISB.js.map +1 -0
  17. package/dist/{chunk-3CQ6THKA.js → chunk-SMUEY5JR.js} +85 -103
  18. package/dist/chunk-SMUEY5JR.js.map +1 -0
  19. package/dist/{client-Ce40ujDF.d.ts → client-Dny_ro_v.d.ts} +41 -25
  20. package/dist/client.d.ts +1 -1
  21. package/dist/client.js +1 -1
  22. package/dist/index.d.ts +2 -2
  23. package/dist/index.js +8 -9
  24. package/dist/interpreter.d.ts +1 -1
  25. package/dist/jupyter-client.d.ts +1 -1
  26. package/dist/jupyter-client.js +2 -2
  27. package/dist/request-handler.d.ts +1 -1
  28. package/dist/request-handler.js +3 -5
  29. package/dist/sandbox.d.ts +1 -1
  30. package/dist/sandbox.js +3 -5
  31. package/dist/types.d.ts +10 -21
  32. package/dist/types.js +35 -9
  33. package/dist/types.js.map +1 -1
  34. package/package.json +2 -2
  35. package/src/client.ts +120 -135
  36. package/src/index.ts +8 -0
  37. package/src/sandbox.ts +290 -331
  38. package/src/types.ts +15 -24
  39. package/dist/chunk-3CQ6THKA.js.map +0 -1
  40. package/dist/chunk-6EWSYSO7.js +0 -46
  41. package/dist/chunk-6EWSYSO7.js.map +0 -1
  42. package/dist/chunk-CKIGERRS.js.map +0 -1
  43. /package/dist/{chunk-HHUDRGPY.js.map → chunk-BEQUGUY4.js.map} +0 -0
@@ -1,94 +1,42 @@
1
- import { spawn } from "node:child_process";
2
1
  import { randomBytes } from "node:crypto";
3
- import type { GitCheckoutRequest, SessionData } from "../types";
2
+ import type { Session, SessionManager } from "../isolation";
3
+ import type { GitCheckoutRequest } from "../types";
4
4
 
5
- function executeGitCheckout(
6
- sessions: Map<string, SessionData>,
5
+ async function executeGitCheckout(
6
+ sessionManager: SessionManager,
7
+ sessionId: string | undefined,
7
8
  repoUrl: string,
8
9
  branch: string,
9
- targetDir: string,
10
- sessionId?: string
10
+ targetDir: string
11
11
  ): Promise<{
12
12
  success: boolean;
13
13
  stdout: string;
14
14
  stderr: string;
15
15
  exitCode: number;
16
16
  }> {
17
- return new Promise((resolve, reject) => {
18
- // First, clone the repository
19
- const cloneChild = spawn(
20
- "git",
21
- ["clone", "-b", branch, repoUrl, targetDir],
22
- {
23
- shell: true,
24
- stdio: ["pipe", "pipe", "pipe"],
25
- }
26
- );
27
-
28
- // Store the process reference for cleanup if sessionId is provided
29
- if (sessionId && sessions.has(sessionId)) {
30
- const session = sessions.get(sessionId)!;
31
- session.activeProcess = cloneChild;
17
+ // Execute git clone through the session to respect working directory
18
+ const command = `git clone -b ${branch} ${repoUrl} ${targetDir}`;
19
+
20
+ // Use specific session if provided, otherwise use default session
21
+ let session: Session | undefined;
22
+
23
+ if (sessionId) {
24
+ session = sessionManager.getSession(sessionId);
25
+ if (!session) {
26
+ throw new Error(`Session '${sessionId}' not found`);
32
27
  }
33
-
34
- let stdout = "";
35
- let stderr = "";
36
-
37
- cloneChild.stdout?.on("data", (data) => {
38
- stdout += data.toString();
39
- });
40
-
41
- cloneChild.stderr?.on("data", (data) => {
42
- stderr += data.toString();
43
- });
44
-
45
- cloneChild.on("close", (code) => {
46
- // Clear the active process reference
47
- if (sessionId && sessions.has(sessionId)) {
48
- const session = sessions.get(sessionId)!;
49
- session.activeProcess = null;
50
- }
51
-
52
- if (code === 0) {
53
- console.log(
54
- `[Server] Repository cloned successfully: ${repoUrl} to ${targetDir}`
55
- );
56
- resolve({
57
- exitCode: code || 0,
58
- stderr,
59
- stdout,
60
- success: true,
61
- });
62
- } else {
63
- console.error(
64
- `[Server] Failed to clone repository: ${repoUrl}, Exit code: ${code}`
65
- );
66
- resolve({
67
- exitCode: code || 1,
68
- stderr,
69
- stdout,
70
- success: false,
71
- });
72
- }
73
- });
74
-
75
- cloneChild.on("error", (error) => {
76
- // Clear the active process reference
77
- if (sessionId && sessions.has(sessionId)) {
78
- const session = sessions.get(sessionId)!;
79
- session.activeProcess = null;
80
- }
81
-
82
- console.error(`[Server] Error cloning repository: ${repoUrl}`, error);
83
- reject(error);
84
- });
85
- });
28
+ } else {
29
+ // Use the centralized method to get or create default session
30
+ session = await sessionManager.getOrCreateDefaultSession();
31
+ }
32
+
33
+ return session.exec(command);
86
34
  }
87
35
 
88
36
  export async function handleGitCheckoutRequest(
89
- sessions: Map<string, SessionData>,
90
37
  req: Request,
91
- corsHeaders: Record<string, string>
38
+ corsHeaders: Record<string, string>,
39
+ sessionManager: SessionManager
92
40
  ): Promise<Response> {
93
41
  try {
94
42
  const body = (await req.json()) as GitCheckoutRequest;
@@ -133,15 +81,15 @@ export async function handleGitCheckoutRequest(
133
81
  `repo_${Date.now()}_${randomBytes(6).toString('hex')}`;
134
82
 
135
83
  console.log(
136
- `[Server] Checking out repository: ${repoUrl} to ${checkoutDir}`
84
+ `[Server] Checking out repository: ${repoUrl} to ${checkoutDir}${sessionId ? ` in session: ${sessionId}` : ''}`
137
85
  );
138
86
 
139
87
  const result = await executeGitCheckout(
140
- sessions,
88
+ sessionManager,
89
+ sessionId,
141
90
  repoUrl,
142
91
  branch,
143
- checkoutDir,
144
- sessionId
92
+ checkoutDir
145
93
  );
146
94
 
147
95
  return new Response(