@cloudflare/sandbox 0.0.0-dc66e8e → 0.0.0-e1fa354

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.
@@ -0,0 +1,92 @@
1
+ import type { SessionManager } from "../isolation";
2
+ import type { CreateSessionRequest } from "../types";
3
+
4
+ export async function handleCreateSession(
5
+ req: Request,
6
+ corsHeaders: Record<string, string>,
7
+ sessionManager: SessionManager
8
+ ) {
9
+ try {
10
+ const body = (await req.json()) as CreateSessionRequest;
11
+ const { id, env, cwd, isolation } = body;
12
+
13
+ if (!id) {
14
+ return new Response(
15
+ JSON.stringify({ error: "Session ID is required" }),
16
+ {
17
+ status: 400,
18
+ headers: {
19
+ "Content-Type": "application/json",
20
+ ...corsHeaders,
21
+ },
22
+ }
23
+ );
24
+ }
25
+
26
+ await sessionManager.createSession({
27
+ id,
28
+ env: env || {},
29
+ cwd: cwd || "/workspace",
30
+ isolation: isolation !== false,
31
+ });
32
+
33
+ console.log(`[Container] Session '${id}' created successfully`);
34
+ console.log(
35
+ `[Container] Available sessions now: ${sessionManager
36
+ .listSessions()
37
+ .join(", ")}`
38
+ );
39
+
40
+ return new Response(
41
+ JSON.stringify({
42
+ success: true,
43
+ id,
44
+ message: `Session '${id}' created with${
45
+ isolation !== false ? "" : "out"
46
+ } isolation`,
47
+ }),
48
+ {
49
+ headers: {
50
+ "Content-Type": "application/json",
51
+ ...corsHeaders,
52
+ },
53
+ }
54
+ );
55
+ } catch (error) {
56
+ console.error("[Container] Failed to create session:", error);
57
+ return new Response(
58
+ JSON.stringify({
59
+ error: "Failed to create session",
60
+ message:
61
+ error instanceof Error ? error.message : String(error),
62
+ }),
63
+ {
64
+ status: 500,
65
+ headers: {
66
+ "Content-Type": "application/json",
67
+ ...corsHeaders,
68
+ },
69
+ }
70
+ );
71
+ }
72
+ }
73
+
74
+ export function handleListSessions(
75
+ corsHeaders: Record<string, string>,
76
+ sessionManager: SessionManager
77
+ ) {
78
+ const sessionList = sessionManager.listSessions();
79
+ return new Response(
80
+ JSON.stringify({
81
+ count: sessionList.length,
82
+ sessions: sessionList,
83
+ timestamp: new Date().toISOString(),
84
+ }),
85
+ {
86
+ headers: {
87
+ "Content-Type": "application/json",
88
+ ...corsHeaders,
89
+ },
90
+ }
91
+ );
92
+ }