@cloudflare/sandbox 0.0.0-e1fa354 → 0.0.0-e489cbb

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