@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.
Files changed (120) hide show
  1. package/.turbo/turbo-build.log +44 -0
  2. package/CHANGELOG.md +6 -14
  3. package/Dockerfile +82 -18
  4. package/README.md +89 -824
  5. package/dist/{chunk-JTKON2SH.js → chunk-BCJ7SF3Q.js} +9 -5
  6. package/dist/chunk-BCJ7SF3Q.js.map +1 -0
  7. package/dist/chunk-BFVUNTP4.js +104 -0
  8. package/dist/chunk-BFVUNTP4.js.map +1 -0
  9. package/dist/{chunk-NNGBXDMY.js → chunk-EKSWCBCA.js} +3 -6
  10. package/dist/chunk-EKSWCBCA.js.map +1 -0
  11. package/dist/chunk-HGF554LH.js +2236 -0
  12. package/dist/chunk-HGF554LH.js.map +1 -0
  13. package/dist/{chunk-6UAWTJ5S.js → chunk-Z532A7QC.js} +13 -20
  14. package/dist/{chunk-6UAWTJ5S.js.map → chunk-Z532A7QC.js.map} +1 -1
  15. package/dist/file-stream.d.ts +16 -38
  16. package/dist/file-stream.js +1 -2
  17. package/dist/index.d.ts +6 -5
  18. package/dist/index.js +35 -39
  19. package/dist/index.js.map +1 -1
  20. package/dist/interpreter.d.ts +3 -3
  21. package/dist/interpreter.js +2 -2
  22. package/dist/request-handler.d.ts +4 -3
  23. package/dist/request-handler.js +4 -7
  24. package/dist/sandbox-D9K2ypln.d.ts +583 -0
  25. package/dist/sandbox.d.ts +3 -3
  26. package/dist/sandbox.js +4 -7
  27. package/dist/security.d.ts +4 -3
  28. package/dist/security.js +3 -3
  29. package/dist/sse-parser.js +1 -1
  30. package/package.json +11 -5
  31. package/src/clients/base-client.ts +280 -0
  32. package/src/clients/command-client.ts +115 -0
  33. package/src/clients/file-client.ts +269 -0
  34. package/src/clients/git-client.ts +92 -0
  35. package/src/clients/index.ts +63 -0
  36. package/src/{interpreter-client.ts → clients/interpreter-client.ts} +148 -171
  37. package/src/clients/port-client.ts +105 -0
  38. package/src/clients/process-client.ts +177 -0
  39. package/src/clients/sandbox-client.ts +41 -0
  40. package/src/clients/types.ts +84 -0
  41. package/src/clients/utility-client.ts +94 -0
  42. package/src/errors/adapter.ts +180 -0
  43. package/src/errors/classes.ts +469 -0
  44. package/src/errors/index.ts +105 -0
  45. package/src/file-stream.ts +119 -117
  46. package/src/index.ts +81 -69
  47. package/src/interpreter.ts +17 -8
  48. package/src/request-handler.ts +69 -43
  49. package/src/sandbox.ts +694 -533
  50. package/src/security.ts +14 -23
  51. package/src/sse-parser.ts +4 -8
  52. package/startup.sh +3 -0
  53. package/tests/base-client.test.ts +328 -0
  54. package/tests/command-client.test.ts +407 -0
  55. package/tests/file-client.test.ts +643 -0
  56. package/tests/file-stream.test.ts +306 -0
  57. package/tests/git-client.test.ts +328 -0
  58. package/tests/port-client.test.ts +301 -0
  59. package/tests/process-client.test.ts +658 -0
  60. package/tests/sandbox.test.ts +465 -0
  61. package/tests/sse-parser.test.ts +290 -0
  62. package/tests/utility-client.test.ts +266 -0
  63. package/tests/wrangler.jsonc +35 -0
  64. package/tsconfig.json +9 -1
  65. package/vitest.config.ts +31 -0
  66. package/container_src/bun.lock +0 -76
  67. package/container_src/circuit-breaker.ts +0 -121
  68. package/container_src/control-process.ts +0 -784
  69. package/container_src/handler/exec.ts +0 -185
  70. package/container_src/handler/file.ts +0 -457
  71. package/container_src/handler/git.ts +0 -130
  72. package/container_src/handler/ports.ts +0 -314
  73. package/container_src/handler/process.ts +0 -568
  74. package/container_src/handler/session.ts +0 -92
  75. package/container_src/index.ts +0 -601
  76. package/container_src/interpreter-service.ts +0 -276
  77. package/container_src/isolation.ts +0 -1213
  78. package/container_src/mime-processor.ts +0 -255
  79. package/container_src/package.json +0 -18
  80. package/container_src/runtime/executors/javascript/node_executor.ts +0 -123
  81. package/container_src/runtime/executors/python/ipython_executor.py +0 -338
  82. package/container_src/runtime/executors/typescript/ts_executor.ts +0 -138
  83. package/container_src/runtime/process-pool.ts +0 -464
  84. package/container_src/shell-escape.ts +0 -42
  85. package/container_src/startup.sh +0 -11
  86. package/container_src/types.ts +0 -131
  87. package/dist/chunk-32UDXUPC.js +0 -671
  88. package/dist/chunk-32UDXUPC.js.map +0 -1
  89. package/dist/chunk-5DILEXGY.js +0 -85
  90. package/dist/chunk-5DILEXGY.js.map +0 -1
  91. package/dist/chunk-D3U63BZP.js +0 -240
  92. package/dist/chunk-D3U63BZP.js.map +0 -1
  93. package/dist/chunk-FXYPFGOZ.js +0 -129
  94. package/dist/chunk-FXYPFGOZ.js.map +0 -1
  95. package/dist/chunk-JTKON2SH.js.map +0 -1
  96. package/dist/chunk-NNGBXDMY.js.map +0 -1
  97. package/dist/chunk-SQLJNZ3K.js +0 -674
  98. package/dist/chunk-SQLJNZ3K.js.map +0 -1
  99. package/dist/chunk-W7TVRPBG.js +0 -108
  100. package/dist/chunk-W7TVRPBG.js.map +0 -1
  101. package/dist/client-B3RUab0s.d.ts +0 -225
  102. package/dist/client.d.ts +0 -4
  103. package/dist/client.js +0 -7
  104. package/dist/client.js.map +0 -1
  105. package/dist/errors.d.ts +0 -95
  106. package/dist/errors.js +0 -27
  107. package/dist/errors.js.map +0 -1
  108. package/dist/interpreter-client.d.ts +0 -4
  109. package/dist/interpreter-client.js +0 -9
  110. package/dist/interpreter-client.js.map +0 -1
  111. package/dist/interpreter-types.d.ts +0 -259
  112. package/dist/interpreter-types.js +0 -9
  113. package/dist/interpreter-types.js.map +0 -1
  114. package/dist/types.d.ts +0 -453
  115. package/dist/types.js +0 -45
  116. package/dist/types.js.map +0 -1
  117. package/src/client.ts +0 -1048
  118. package/src/errors.ts +0 -219
  119. package/src/interpreter-types.ts +0 -390
  120. package/src/types.ts +0 -571
package/src/errors.ts DELETED
@@ -1,219 +0,0 @@
1
- /**
2
- * Standard error response from the sandbox API
3
- */
4
- export interface SandboxErrorResponse {
5
- error?: string;
6
- status?: string;
7
- progress?: number;
8
- }
9
-
10
- /**
11
- * Base error class for all Sandbox-related errors
12
- */
13
- export class SandboxError extends Error {
14
- constructor(message: string) {
15
- super(message);
16
- this.name = this.constructor.name;
17
-
18
- // Maintains proper stack trace for where our error was thrown (only available on V8)
19
- if (Error.captureStackTrace) {
20
- Error.captureStackTrace(this, this.constructor);
21
- }
22
- }
23
- }
24
-
25
- /**
26
- * Error thrown when interpreter functionality is requested but the service is still initializing.
27
- *
28
- * Note: With the current implementation, requests wait for interpreter to be ready.
29
- * This error is only thrown when:
30
- * 1. The request times out waiting for interpreter (default: 30 seconds)
31
- * 2. interpreter initialization actually fails
32
- *
33
- * Most requests will succeed after a delay, not throw this error.
34
- */
35
- export class InterpreterNotReadyError extends SandboxError {
36
- public readonly code = "INTERPRETER_NOT_READY";
37
- public readonly retryAfter: number;
38
- public readonly progress?: number;
39
-
40
- constructor(
41
- message?: string,
42
- options?: { retryAfter?: number; progress?: number }
43
- ) {
44
- super(
45
- message ||
46
- "Interpreter is still initializing. Please retry in a few seconds."
47
- );
48
- this.retryAfter = options?.retryAfter || 5;
49
- this.progress = options?.progress;
50
- }
51
- }
52
-
53
- /**
54
- * Error thrown when a context is not found
55
- */
56
- export class ContextNotFoundError extends SandboxError {
57
- public readonly code = "CONTEXT_NOT_FOUND";
58
- public readonly contextId: string;
59
-
60
- constructor(contextId: string) {
61
- super(`Context ${contextId} not found`);
62
- this.contextId = contextId;
63
- }
64
- }
65
-
66
- /**
67
- * Error thrown when code execution fails
68
- */
69
- export class CodeExecutionError extends SandboxError {
70
- public readonly code = "CODE_EXECUTION_ERROR";
71
- public readonly executionError?: {
72
- ename?: string;
73
- evalue?: string;
74
- traceback?: string[];
75
- };
76
-
77
- constructor(message: string, executionError?: any) {
78
- super(message);
79
- this.executionError = executionError;
80
- }
81
- }
82
-
83
- /**
84
- * Error thrown when the sandbox container is not ready
85
- */
86
- export class ContainerNotReadyError extends SandboxError {
87
- public readonly code = "CONTAINER_NOT_READY";
88
-
89
- constructor(message?: string) {
90
- super(
91
- message ||
92
- "Container is not ready. Please wait for initialization to complete."
93
- );
94
- }
95
- }
96
-
97
- /**
98
- * Error thrown when a network request to the sandbox fails
99
- */
100
- export class SandboxNetworkError extends SandboxError {
101
- public readonly code = "NETWORK_ERROR";
102
- public readonly statusCode?: number;
103
- public readonly statusText?: string;
104
-
105
- constructor(message: string, statusCode?: number, statusText?: string) {
106
- super(message);
107
- this.statusCode = statusCode;
108
- this.statusText = statusText;
109
- }
110
- }
111
-
112
- /**
113
- * Error thrown when service is temporarily unavailable (e.g., circuit breaker open)
114
- */
115
- export class ServiceUnavailableError extends SandboxError {
116
- public readonly code = "SERVICE_UNAVAILABLE";
117
- public readonly retryAfter?: number;
118
-
119
- constructor(message?: string, retryAfter?: number) {
120
- // Simple, user-friendly message without implementation details
121
- super(message || "Service temporarily unavailable");
122
- this.retryAfter = retryAfter;
123
- }
124
- }
125
-
126
- /**
127
- * Type guard to check if an error is a InterpreterNotReadyError
128
- */
129
- export function isInterpreterNotReadyError(
130
- error: unknown
131
- ): error is InterpreterNotReadyError {
132
- return error instanceof InterpreterNotReadyError;
133
- }
134
-
135
- /**
136
- * Type guard to check if an error is any SandboxError
137
- */
138
- export function isSandboxError(error: unknown): error is SandboxError {
139
- return error instanceof SandboxError;
140
- }
141
-
142
- /**
143
- * Helper to determine if an error is retryable
144
- */
145
- export function isRetryableError(error: unknown): boolean {
146
- if (
147
- error instanceof InterpreterNotReadyError ||
148
- error instanceof ContainerNotReadyError ||
149
- error instanceof ServiceUnavailableError
150
- ) {
151
- return true;
152
- }
153
-
154
- if (error instanceof SandboxNetworkError) {
155
- // Retry on 502, 503, 504 (gateway/service unavailable errors)
156
- return error.statusCode
157
- ? [502, 503, 504].includes(error.statusCode)
158
- : false;
159
- }
160
-
161
- return false;
162
- }
163
-
164
- /**
165
- * Parse error response from the sandbox API and return appropriate error instance
166
- */
167
- export async function parseErrorResponse(
168
- response: Response
169
- ): Promise<SandboxError> {
170
- let data: SandboxErrorResponse;
171
-
172
- try {
173
- data = (await response.json()) as SandboxErrorResponse;
174
- } catch {
175
- // If JSON parsing fails, return a generic network error
176
- return new SandboxNetworkError(
177
- `Request failed with status ${response.status}`,
178
- response.status,
179
- response.statusText
180
- );
181
- }
182
-
183
- // Check for specific error types based on response
184
- if (response.status === 503) {
185
- // Circuit breaker error
186
- if (data.status === "circuit_open") {
187
- return new ServiceUnavailableError(
188
- "Service temporarily unavailable",
189
- parseInt(response.headers.get("Retry-After") || "30")
190
- );
191
- }
192
-
193
- // Interpreter initialization error
194
- if (data.status === "initializing") {
195
- return new InterpreterNotReadyError(data.error, {
196
- retryAfter: parseInt(response.headers.get("Retry-After") || "5"),
197
- progress: data.progress,
198
- });
199
- }
200
- }
201
-
202
- // Check for context not found
203
- if (
204
- response.status === 404 &&
205
- data.error?.includes("Context") &&
206
- data.error?.includes("not found")
207
- ) {
208
- const contextId =
209
- data.error.match(/Context (\S+) not found/)?.[1] || "unknown";
210
- return new ContextNotFoundError(contextId);
211
- }
212
-
213
- // Default network error
214
- return new SandboxNetworkError(
215
- data.error || `Request failed with status ${response.status}`,
216
- response.status,
217
- response.statusText
218
- );
219
- }
@@ -1,390 +0,0 @@
1
- // Context Management
2
- export interface CreateContextOptions {
3
- /**
4
- * Programming language for the context
5
- * @default 'python'
6
- */
7
- language?: "python" | "javascript" | "typescript";
8
-
9
- /**
10
- * Working directory for the context
11
- * @default '/workspace'
12
- */
13
- cwd?: string;
14
-
15
- /**
16
- * Environment variables for the context
17
- */
18
- envVars?: Record<string, string>;
19
-
20
- /**
21
- * Request timeout in milliseconds
22
- * @default 30000
23
- */
24
- timeout?: number;
25
- }
26
-
27
- export interface CodeContext {
28
- /**
29
- * Unique identifier for the context
30
- */
31
- readonly id: string;
32
-
33
- /**
34
- * Programming language of the context
35
- */
36
- readonly language: string;
37
-
38
- /**
39
- * Current working directory
40
- */
41
- readonly cwd: string;
42
-
43
- /**
44
- * When the context was created
45
- */
46
- readonly createdAt: Date;
47
-
48
- /**
49
- * When the context was last used
50
- */
51
- readonly lastUsed: Date;
52
- }
53
-
54
- // Execution Options
55
- export interface RunCodeOptions {
56
- /**
57
- * Context to run the code in. If not provided, uses default context for the language
58
- */
59
- context?: CodeContext;
60
-
61
- /**
62
- * Language to use if context is not provided
63
- * @default 'python'
64
- */
65
- language?: "python" | "javascript" | "typescript";
66
-
67
- /**
68
- * Environment variables for this execution
69
- */
70
- envVars?: Record<string, string>;
71
-
72
- /**
73
- * Execution timeout in milliseconds
74
- * @default 60000
75
- */
76
- timeout?: number;
77
-
78
- /**
79
- * AbortSignal for cancelling execution
80
- */
81
- signal?: AbortSignal;
82
-
83
- /**
84
- * Callback for stdout output
85
- */
86
- onStdout?: (output: OutputMessage) => void | Promise<void>;
87
-
88
- /**
89
- * Callback for stderr output
90
- */
91
- onStderr?: (output: OutputMessage) => void | Promise<void>;
92
-
93
- /**
94
- * Callback for execution results (charts, tables, etc)
95
- */
96
- onResult?: (result: Result) => void | Promise<void>;
97
-
98
- /**
99
- * Callback for execution errors
100
- */
101
- onError?: (error: ExecutionError) => void | Promise<void>;
102
- }
103
-
104
- // Output Messages
105
- export interface OutputMessage {
106
- /**
107
- * The output text
108
- */
109
- text: string;
110
-
111
- /**
112
- * Timestamp of the output
113
- */
114
- timestamp: number;
115
- }
116
-
117
- // Execution Results
118
- export interface Result {
119
- /**
120
- * Plain text representation
121
- */
122
- text?: string;
123
-
124
- /**
125
- * HTML representation (tables, formatted output)
126
- */
127
- html?: string;
128
-
129
- /**
130
- * PNG image data (base64 encoded)
131
- */
132
- png?: string;
133
-
134
- /**
135
- * JPEG image data (base64 encoded)
136
- */
137
- jpeg?: string;
138
-
139
- /**
140
- * SVG image data
141
- */
142
- svg?: string;
143
-
144
- /**
145
- * LaTeX representation
146
- */
147
- latex?: string;
148
-
149
- /**
150
- * Markdown representation
151
- */
152
- markdown?: string;
153
-
154
- /**
155
- * JavaScript code to execute
156
- */
157
- javascript?: string;
158
-
159
- /**
160
- * JSON data
161
- */
162
- json?: any;
163
-
164
- /**
165
- * Chart data if the result is a visualization
166
- */
167
- chart?: ChartData;
168
-
169
- /**
170
- * Raw data object
171
- */
172
- data?: any;
173
-
174
- /**
175
- * Available output formats
176
- */
177
- formats(): string[];
178
- }
179
-
180
- // Chart Data
181
- export interface ChartData {
182
- /**
183
- * Type of chart
184
- */
185
- type:
186
- | "line"
187
- | "bar"
188
- | "scatter"
189
- | "pie"
190
- | "histogram"
191
- | "heatmap"
192
- | "unknown";
193
-
194
- /**
195
- * Chart title
196
- */
197
- title?: string;
198
-
199
- /**
200
- * Chart data (format depends on library)
201
- */
202
- data: any;
203
-
204
- /**
205
- * Chart layout/configuration
206
- */
207
- layout?: any;
208
-
209
- /**
210
- * Additional configuration
211
- */
212
- config?: any;
213
-
214
- /**
215
- * Library that generated the chart
216
- */
217
- library?: "matplotlib" | "plotly" | "altair" | "seaborn" | "unknown";
218
-
219
- /**
220
- * Base64 encoded image if available
221
- */
222
- image?: string;
223
- }
224
-
225
- // Execution Error
226
- export interface ExecutionError {
227
- /**
228
- * Error name/type (e.g., 'NameError', 'SyntaxError')
229
- */
230
- name: string;
231
-
232
- /**
233
- * Error message
234
- */
235
- value: string;
236
-
237
- /**
238
- * Stack trace
239
- */
240
- traceback: string[];
241
-
242
- /**
243
- * Line number where error occurred
244
- */
245
- lineNumber?: number;
246
- }
247
-
248
- // Serializable execution result
249
- export interface ExecutionResult {
250
- code: string;
251
- logs: {
252
- stdout: string[];
253
- stderr: string[];
254
- };
255
- error?: ExecutionError;
256
- executionCount?: number;
257
- results: Array<{
258
- text?: string;
259
- html?: string;
260
- png?: string;
261
- jpeg?: string;
262
- svg?: string;
263
- latex?: string;
264
- markdown?: string;
265
- javascript?: string;
266
- json?: any;
267
- chart?: ChartData;
268
- data?: any;
269
- }>;
270
- }
271
-
272
- // Execution Result Container
273
- export class Execution {
274
- /**
275
- * All results from the execution
276
- */
277
- public results: Result[] = [];
278
-
279
- /**
280
- * Accumulated stdout and stderr
281
- */
282
- public logs = {
283
- stdout: [] as string[],
284
- stderr: [] as string[],
285
- };
286
-
287
- /**
288
- * Execution error if any
289
- */
290
- public error?: ExecutionError;
291
-
292
- /**
293
- * Execution count (for interpreter)
294
- */
295
- public executionCount?: number;
296
-
297
- constructor(
298
- public readonly code: string,
299
- public readonly context: CodeContext
300
- ) {}
301
-
302
- /**
303
- * Convert to a plain object for serialization
304
- */
305
- toJSON(): ExecutionResult {
306
- return {
307
- code: this.code,
308
- logs: this.logs,
309
- error: this.error,
310
- executionCount: this.executionCount,
311
- results: this.results.map((result) => ({
312
- text: result.text,
313
- html: result.html,
314
- png: result.png,
315
- jpeg: result.jpeg,
316
- svg: result.svg,
317
- latex: result.latex,
318
- markdown: result.markdown,
319
- javascript: result.javascript,
320
- json: result.json,
321
- chart: result.chart,
322
- data: result.data,
323
- })),
324
- };
325
- }
326
- }
327
-
328
- // Implementation of Result
329
- export class ResultImpl implements Result {
330
- constructor(private raw: any) {}
331
-
332
- get text(): string | undefined {
333
- return this.raw.text || this.raw.data?.["text/plain"];
334
- }
335
-
336
- get html(): string | undefined {
337
- return this.raw.html || this.raw.data?.["text/html"];
338
- }
339
-
340
- get png(): string | undefined {
341
- return this.raw.png || this.raw.data?.["image/png"];
342
- }
343
-
344
- get jpeg(): string | undefined {
345
- return this.raw.jpeg || this.raw.data?.["image/jpeg"];
346
- }
347
-
348
- get svg(): string | undefined {
349
- return this.raw.svg || this.raw.data?.["image/svg+xml"];
350
- }
351
-
352
- get latex(): string | undefined {
353
- return this.raw.latex || this.raw.data?.["text/latex"];
354
- }
355
-
356
- get markdown(): string | undefined {
357
- return this.raw.markdown || this.raw.data?.["text/markdown"];
358
- }
359
-
360
- get javascript(): string | undefined {
361
- return this.raw.javascript || this.raw.data?.["application/javascript"];
362
- }
363
-
364
- get json(): any {
365
- return this.raw.json || this.raw.data?.["application/json"];
366
- }
367
-
368
- get chart(): ChartData | undefined {
369
- return this.raw.chart;
370
- }
371
-
372
- get data(): any {
373
- return this.raw.data;
374
- }
375
-
376
- formats(): string[] {
377
- const formats: string[] = [];
378
- if (this.text) formats.push("text");
379
- if (this.html) formats.push("html");
380
- if (this.png) formats.push("png");
381
- if (this.jpeg) formats.push("jpeg");
382
- if (this.svg) formats.push("svg");
383
- if (this.latex) formats.push("latex");
384
- if (this.markdown) formats.push("markdown");
385
- if (this.javascript) formats.push("javascript");
386
- if (this.json) formats.push("json");
387
- if (this.chart) formats.push("chart");
388
- return formats;
389
- }
390
- }