@cloudflare/sandbox 0.3.7 → 0.4.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.
- package/.turbo/turbo-build.log +44 -0
- package/CHANGELOG.md +6 -14
- package/Dockerfile +82 -18
- package/README.md +89 -824
- package/dist/{chunk-JTKON2SH.js → chunk-BCJ7SF3Q.js} +9 -5
- package/dist/chunk-BCJ7SF3Q.js.map +1 -0
- package/dist/chunk-BFVUNTP4.js +104 -0
- package/dist/chunk-BFVUNTP4.js.map +1 -0
- package/dist/{chunk-NNGBXDMY.js → chunk-EKSWCBCA.js} +3 -6
- package/dist/chunk-EKSWCBCA.js.map +1 -0
- package/dist/chunk-HGF554LH.js +2236 -0
- package/dist/chunk-HGF554LH.js.map +1 -0
- package/dist/{chunk-6UAWTJ5S.js → chunk-Z532A7QC.js} +13 -20
- package/dist/{chunk-6UAWTJ5S.js.map → chunk-Z532A7QC.js.map} +1 -1
- package/dist/file-stream.d.ts +16 -38
- package/dist/file-stream.js +1 -2
- package/dist/index.d.ts +6 -5
- package/dist/index.js +35 -39
- package/dist/index.js.map +1 -1
- package/dist/interpreter.d.ts +3 -3
- package/dist/interpreter.js +2 -2
- package/dist/request-handler.d.ts +4 -3
- package/dist/request-handler.js +4 -7
- package/dist/sandbox-D9K2ypln.d.ts +583 -0
- package/dist/sandbox.d.ts +3 -3
- package/dist/sandbox.js +4 -7
- package/dist/security.d.ts +4 -3
- package/dist/security.js +3 -3
- package/dist/sse-parser.js +1 -1
- package/package.json +11 -5
- package/src/clients/base-client.ts +280 -0
- package/src/clients/command-client.ts +115 -0
- package/src/clients/file-client.ts +269 -0
- package/src/clients/git-client.ts +92 -0
- package/src/clients/index.ts +63 -0
- package/src/{interpreter-client.ts → clients/interpreter-client.ts} +148 -171
- package/src/clients/port-client.ts +105 -0
- package/src/clients/process-client.ts +177 -0
- package/src/clients/sandbox-client.ts +41 -0
- package/src/clients/types.ts +84 -0
- package/src/clients/utility-client.ts +94 -0
- package/src/errors/adapter.ts +180 -0
- package/src/errors/classes.ts +469 -0
- package/src/errors/index.ts +105 -0
- package/src/file-stream.ts +119 -117
- package/src/index.ts +81 -69
- package/src/interpreter.ts +17 -8
- package/src/request-handler.ts +69 -43
- package/src/sandbox.ts +694 -533
- package/src/security.ts +14 -23
- package/src/sse-parser.ts +4 -8
- package/startup.sh +3 -0
- package/tests/base-client.test.ts +328 -0
- package/tests/command-client.test.ts +407 -0
- package/tests/file-client.test.ts +643 -0
- package/tests/file-stream.test.ts +306 -0
- package/tests/git-client.test.ts +328 -0
- package/tests/port-client.test.ts +301 -0
- package/tests/process-client.test.ts +658 -0
- package/tests/sandbox.test.ts +465 -0
- package/tests/sse-parser.test.ts +290 -0
- package/tests/utility-client.test.ts +266 -0
- package/tests/wrangler.jsonc +35 -0
- package/tsconfig.json +9 -1
- package/vitest.config.ts +31 -0
- package/container_src/bun.lock +0 -76
- package/container_src/circuit-breaker.ts +0 -121
- package/container_src/control-process.ts +0 -784
- package/container_src/handler/exec.ts +0 -185
- package/container_src/handler/file.ts +0 -457
- package/container_src/handler/git.ts +0 -130
- package/container_src/handler/ports.ts +0 -314
- package/container_src/handler/process.ts +0 -568
- package/container_src/handler/session.ts +0 -92
- package/container_src/index.ts +0 -601
- package/container_src/interpreter-service.ts +0 -276
- package/container_src/isolation.ts +0 -1213
- package/container_src/mime-processor.ts +0 -255
- package/container_src/package.json +0 -18
- package/container_src/runtime/executors/javascript/node_executor.ts +0 -123
- package/container_src/runtime/executors/python/ipython_executor.py +0 -338
- package/container_src/runtime/executors/typescript/ts_executor.ts +0 -138
- package/container_src/runtime/process-pool.ts +0 -464
- package/container_src/shell-escape.ts +0 -42
- package/container_src/startup.sh +0 -11
- package/container_src/types.ts +0 -131
- package/dist/chunk-32UDXUPC.js +0 -671
- package/dist/chunk-32UDXUPC.js.map +0 -1
- package/dist/chunk-5DILEXGY.js +0 -85
- package/dist/chunk-5DILEXGY.js.map +0 -1
- package/dist/chunk-D3U63BZP.js +0 -240
- package/dist/chunk-D3U63BZP.js.map +0 -1
- package/dist/chunk-FXYPFGOZ.js +0 -129
- package/dist/chunk-FXYPFGOZ.js.map +0 -1
- package/dist/chunk-JTKON2SH.js.map +0 -1
- package/dist/chunk-NNGBXDMY.js.map +0 -1
- package/dist/chunk-SQLJNZ3K.js +0 -674
- package/dist/chunk-SQLJNZ3K.js.map +0 -1
- package/dist/chunk-W7TVRPBG.js +0 -108
- package/dist/chunk-W7TVRPBG.js.map +0 -1
- package/dist/client-B3RUab0s.d.ts +0 -225
- package/dist/client.d.ts +0 -4
- package/dist/client.js +0 -7
- package/dist/client.js.map +0 -1
- package/dist/errors.d.ts +0 -95
- package/dist/errors.js +0 -27
- package/dist/errors.js.map +0 -1
- package/dist/interpreter-client.d.ts +0 -4
- package/dist/interpreter-client.js +0 -9
- package/dist/interpreter-client.js.map +0 -1
- package/dist/interpreter-types.d.ts +0 -259
- package/dist/interpreter-types.js +0 -9
- package/dist/interpreter-types.js.map +0 -1
- package/dist/types.d.ts +0 -453
- package/dist/types.js +0 -45
- package/dist/types.js.map +0 -1
- package/src/client.ts +0 -1048
- package/src/errors.ts +0 -219
- package/src/interpreter-types.ts +0 -390
- package/src/types.ts +0 -571
|
@@ -1,276 +0,0 @@
|
|
|
1
|
-
import { randomUUID } from "node:crypto";
|
|
2
|
-
import { type InterpreterLanguage, processPool, type RichOutput } from "./runtime/process-pool";
|
|
3
|
-
|
|
4
|
-
export interface CreateContextRequest {
|
|
5
|
-
language?: string;
|
|
6
|
-
cwd?: string;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export interface Context {
|
|
10
|
-
id: string;
|
|
11
|
-
language: string;
|
|
12
|
-
cwd: string;
|
|
13
|
-
createdAt: string;
|
|
14
|
-
lastUsed: string;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface HealthStatus {
|
|
18
|
-
ready: boolean;
|
|
19
|
-
initializing: boolean;
|
|
20
|
-
progress: number;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export class InterpreterNotReadyError extends Error {
|
|
24
|
-
progress: number;
|
|
25
|
-
retryAfter: number;
|
|
26
|
-
|
|
27
|
-
constructor(message: string, progress: number = 100, retryAfter: number = 1) {
|
|
28
|
-
super(message);
|
|
29
|
-
this.progress = progress;
|
|
30
|
-
this.retryAfter = retryAfter;
|
|
31
|
-
this.name = "InterpreterNotReadyError";
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export class InterpreterService {
|
|
36
|
-
private contexts: Map<string, Context> = new Map();
|
|
37
|
-
|
|
38
|
-
async getHealthStatus(): Promise<HealthStatus> {
|
|
39
|
-
return {
|
|
40
|
-
ready: true,
|
|
41
|
-
initializing: false,
|
|
42
|
-
progress: 100,
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async createContext(request: CreateContextRequest): Promise<Context> {
|
|
47
|
-
const id = randomUUID();
|
|
48
|
-
const language = this.mapLanguage(request.language || "python");
|
|
49
|
-
|
|
50
|
-
const context: Context = {
|
|
51
|
-
id,
|
|
52
|
-
language,
|
|
53
|
-
cwd: request.cwd || "/workspace",
|
|
54
|
-
createdAt: new Date().toISOString(),
|
|
55
|
-
lastUsed: new Date().toISOString(),
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
this.contexts.set(id, context);
|
|
59
|
-
console.log(`[InterpreterService] Created context ${id} for ${language}`);
|
|
60
|
-
|
|
61
|
-
return context;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
async listContexts(): Promise<Context[]> {
|
|
65
|
-
return Array.from(this.contexts.values());
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
async deleteContext(contextId: string): Promise<void> {
|
|
69
|
-
if (!this.contexts.has(contextId)) {
|
|
70
|
-
throw new Error(`Context ${contextId} not found`);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
this.contexts.delete(contextId);
|
|
74
|
-
console.log(`[InterpreterService] Deleted context ${contextId}`);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
async executeCode(
|
|
78
|
-
contextId: string,
|
|
79
|
-
code: string,
|
|
80
|
-
language?: string
|
|
81
|
-
): Promise<Response> {
|
|
82
|
-
const context = this.contexts.get(contextId);
|
|
83
|
-
if (!context) {
|
|
84
|
-
return new Response(
|
|
85
|
-
JSON.stringify({
|
|
86
|
-
error: `Context ${contextId} not found`,
|
|
87
|
-
}),
|
|
88
|
-
{
|
|
89
|
-
status: 404,
|
|
90
|
-
headers: { "Content-Type": "application/json" },
|
|
91
|
-
}
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
context.lastUsed = new Date().toISOString();
|
|
96
|
-
|
|
97
|
-
const execLanguage = this.mapLanguage(language || context.language);
|
|
98
|
-
|
|
99
|
-
// Store reference to this for use in async function
|
|
100
|
-
const self = this;
|
|
101
|
-
|
|
102
|
-
const stream = new ReadableStream({
|
|
103
|
-
async start(controller) {
|
|
104
|
-
const encoder = new TextEncoder();
|
|
105
|
-
const startTime = Date.now();
|
|
106
|
-
|
|
107
|
-
try {
|
|
108
|
-
const result = await processPool.execute(
|
|
109
|
-
execLanguage,
|
|
110
|
-
code,
|
|
111
|
-
contextId,
|
|
112
|
-
30000
|
|
113
|
-
);
|
|
114
|
-
|
|
115
|
-
const totalTime = Date.now() - startTime;
|
|
116
|
-
console.log(`[InterpreterService] Code execution completed in ${totalTime}ms`);
|
|
117
|
-
|
|
118
|
-
if (result.stdout) {
|
|
119
|
-
controller.enqueue(
|
|
120
|
-
encoder.encode(
|
|
121
|
-
`${JSON.stringify({
|
|
122
|
-
type: "stdout",
|
|
123
|
-
text: result.stdout,
|
|
124
|
-
})}\n`
|
|
125
|
-
)
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (result.stderr) {
|
|
130
|
-
controller.enqueue(
|
|
131
|
-
encoder.encode(
|
|
132
|
-
`${JSON.stringify({
|
|
133
|
-
type: "stderr",
|
|
134
|
-
text: result.stderr,
|
|
135
|
-
})}\n`
|
|
136
|
-
)
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (result.outputs && result.outputs.length > 0) {
|
|
141
|
-
for (const output of result.outputs) {
|
|
142
|
-
const outputData = self.formatOutputData(output);
|
|
143
|
-
controller.enqueue(
|
|
144
|
-
encoder.encode(
|
|
145
|
-
`${JSON.stringify({
|
|
146
|
-
type: "result",
|
|
147
|
-
...outputData,
|
|
148
|
-
metadata: output.metadata || {},
|
|
149
|
-
})}\n`
|
|
150
|
-
)
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
if (result.success) {
|
|
156
|
-
controller.enqueue(
|
|
157
|
-
encoder.encode(
|
|
158
|
-
`${JSON.stringify({
|
|
159
|
-
type: "execution_complete",
|
|
160
|
-
execution_count: 1,
|
|
161
|
-
})}\n`
|
|
162
|
-
)
|
|
163
|
-
);
|
|
164
|
-
} else if (result.error) {
|
|
165
|
-
controller.enqueue(
|
|
166
|
-
encoder.encode(
|
|
167
|
-
`${JSON.stringify({
|
|
168
|
-
type: "error",
|
|
169
|
-
ename: result.error.type || "ExecutionError",
|
|
170
|
-
evalue: result.error.message || "Code execution failed",
|
|
171
|
-
traceback: result.error.traceback ? result.error.traceback.split('\n') : [],
|
|
172
|
-
})}\n`
|
|
173
|
-
)
|
|
174
|
-
);
|
|
175
|
-
} else {
|
|
176
|
-
controller.enqueue(
|
|
177
|
-
encoder.encode(
|
|
178
|
-
`${JSON.stringify({
|
|
179
|
-
type: "error",
|
|
180
|
-
ename: "ExecutionError",
|
|
181
|
-
evalue: result.stderr || "Code execution failed",
|
|
182
|
-
traceback: [],
|
|
183
|
-
})}\n`
|
|
184
|
-
)
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
controller.close();
|
|
189
|
-
} catch (error) {
|
|
190
|
-
console.error(`[InterpreterService] Code execution failed:`, error);
|
|
191
|
-
|
|
192
|
-
controller.enqueue(
|
|
193
|
-
encoder.encode(
|
|
194
|
-
`${JSON.stringify({
|
|
195
|
-
type: "error",
|
|
196
|
-
ename: "InternalError",
|
|
197
|
-
evalue: error instanceof Error ? error.message : String(error),
|
|
198
|
-
traceback: [],
|
|
199
|
-
})}\n`
|
|
200
|
-
)
|
|
201
|
-
);
|
|
202
|
-
|
|
203
|
-
controller.close();
|
|
204
|
-
}
|
|
205
|
-
},
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
return new Response(stream, {
|
|
209
|
-
headers: {
|
|
210
|
-
"Content-Type": "text/event-stream",
|
|
211
|
-
"Cache-Control": "no-cache",
|
|
212
|
-
Connection: "keep-alive",
|
|
213
|
-
},
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
private mapLanguage(language: string): InterpreterLanguage {
|
|
218
|
-
const normalized = language.toLowerCase();
|
|
219
|
-
|
|
220
|
-
switch (normalized) {
|
|
221
|
-
case "python":
|
|
222
|
-
case "python3":
|
|
223
|
-
return "python";
|
|
224
|
-
case "javascript":
|
|
225
|
-
case "js":
|
|
226
|
-
case "node":
|
|
227
|
-
return "javascript";
|
|
228
|
-
case "typescript":
|
|
229
|
-
case "ts":
|
|
230
|
-
return "typescript";
|
|
231
|
-
default:
|
|
232
|
-
console.warn(
|
|
233
|
-
`[InterpreterService] Unknown language ${language}, defaulting to python`
|
|
234
|
-
);
|
|
235
|
-
return "python";
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
private formatOutputData(output: RichOutput): Record<string, unknown> {
|
|
240
|
-
const result: Record<string, unknown> = {};
|
|
241
|
-
|
|
242
|
-
switch (output.type) {
|
|
243
|
-
case "image":
|
|
244
|
-
result.png = output.data;
|
|
245
|
-
break;
|
|
246
|
-
case "jpeg":
|
|
247
|
-
result.jpeg = output.data;
|
|
248
|
-
break;
|
|
249
|
-
case "svg":
|
|
250
|
-
result.svg = output.data;
|
|
251
|
-
break;
|
|
252
|
-
case "html":
|
|
253
|
-
result.html = output.data;
|
|
254
|
-
break;
|
|
255
|
-
case "json":
|
|
256
|
-
result.json = typeof output.data === 'string' ? JSON.parse(output.data) : output.data;
|
|
257
|
-
break;
|
|
258
|
-
case "latex":
|
|
259
|
-
result.latex = output.data;
|
|
260
|
-
break;
|
|
261
|
-
case "markdown":
|
|
262
|
-
result.markdown = output.data;
|
|
263
|
-
break;
|
|
264
|
-
case "javascript":
|
|
265
|
-
result.javascript = output.data;
|
|
266
|
-
break;
|
|
267
|
-
case "text":
|
|
268
|
-
result.text = output.data;
|
|
269
|
-
break;
|
|
270
|
-
default:
|
|
271
|
-
result.text = output.data || '';
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
return result;
|
|
275
|
-
}
|
|
276
|
-
}
|