@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/types.ts DELETED
@@ -1,502 +0,0 @@
1
- // Core Types
2
-
3
- export interface BaseExecOptions {
4
- /**
5
- * Maximum execution time in milliseconds
6
- */
7
- timeout?: number;
8
-
9
- /**
10
- * Environment variables for the command
11
- */
12
- env?: Record<string, string>;
13
-
14
- /**
15
- * Working directory for command execution
16
- */
17
- cwd?: string;
18
-
19
- /**
20
- * Text encoding for output (default: 'utf8')
21
- */
22
- encoding?: string;
23
- }
24
-
25
- export interface ExecOptions extends BaseExecOptions {
26
- /**
27
- * Enable real-time output streaming via callbacks
28
- */
29
- stream?: boolean;
30
-
31
- /**
32
- * Callback for real-time output data
33
- */
34
- onOutput?: (stream: 'stdout' | 'stderr', data: string) => void;
35
-
36
- /**
37
- * Callback when command completes (only when stream: true)
38
- */
39
- onComplete?: (result: ExecResult) => void;
40
-
41
- /**
42
- * Callback for execution errors
43
- */
44
- onError?: (error: Error) => void;
45
-
46
- /**
47
- * AbortSignal for cancelling execution
48
- */
49
- signal?: AbortSignal;
50
- }
51
-
52
- export interface ExecResult {
53
- /**
54
- * Whether the command succeeded (exitCode === 0)
55
- */
56
- success: boolean;
57
-
58
- /**
59
- * Process exit code
60
- */
61
- exitCode: number;
62
-
63
- /**
64
- * Standard output content
65
- */
66
- stdout: string;
67
-
68
- /**
69
- * Standard error content
70
- */
71
- stderr: string;
72
-
73
- /**
74
- * Command that was executed
75
- */
76
- command: string;
77
-
78
-
79
- /**
80
- * Execution duration in milliseconds
81
- */
82
- duration: number;
83
-
84
- /**
85
- * ISO timestamp when command started
86
- */
87
- timestamp: string;
88
- }
89
-
90
- // Background Process Types
91
-
92
- export interface ProcessOptions extends BaseExecOptions {
93
- /**
94
- * Custom process ID for later reference
95
- * If not provided, a UUID will be generated
96
- */
97
- processId?: string;
98
-
99
- /**
100
- * Automatically cleanup process record after exit (default: true)
101
- */
102
- autoCleanup?: boolean;
103
-
104
- /**
105
- * Callback when process exits
106
- */
107
- onExit?: (code: number | null) => void;
108
-
109
- /**
110
- * Callback for real-time output (background processes)
111
- */
112
- onOutput?: (stream: 'stdout' | 'stderr', data: string) => void;
113
-
114
- /**
115
- * Callback when process starts successfully
116
- */
117
- onStart?: (process: Process) => void;
118
-
119
- /**
120
- * Callback for process errors
121
- */
122
- onError?: (error: Error) => void;
123
- }
124
-
125
- export type ProcessStatus =
126
- | 'starting' // Process is being initialized
127
- | 'running' // Process is actively running
128
- | 'completed' // Process exited successfully (code 0)
129
- | 'failed' // Process exited with non-zero code
130
- | 'killed' // Process was terminated by signal
131
- | 'error'; // Process failed to start or encountered error
132
-
133
- export interface Process {
134
- /**
135
- * Unique process identifier
136
- */
137
- readonly id: string;
138
-
139
- /**
140
- * System process ID (if available and running)
141
- */
142
- readonly pid?: number;
143
-
144
- /**
145
- * Command that was executed
146
- */
147
- readonly command: string;
148
-
149
-
150
- /**
151
- * Current process status
152
- */
153
- readonly status: ProcessStatus;
154
-
155
- /**
156
- * When the process was started
157
- */
158
- readonly startTime: Date;
159
-
160
- /**
161
- * When the process ended (if completed)
162
- */
163
- readonly endTime?: Date;
164
-
165
- /**
166
- * Process exit code (if completed)
167
- */
168
- readonly exitCode?: number;
169
-
170
- /**
171
- * Kill the process
172
- */
173
- kill(signal?: string): Promise<void>;
174
-
175
- /**
176
- * Get current process status (refreshed)
177
- */
178
- getStatus(): Promise<ProcessStatus>;
179
-
180
- /**
181
- * Get accumulated logs
182
- */
183
- getLogs(): Promise<{ stdout: string; stderr: string }>;
184
- }
185
-
186
- // Streaming Types
187
-
188
- export interface ExecEvent {
189
- type: 'start' | 'stdout' | 'stderr' | 'complete' | 'error';
190
- timestamp: string;
191
- data?: string;
192
- command?: string;
193
- exitCode?: number;
194
- result?: ExecResult;
195
- error?: string; // Changed to string for serialization
196
- }
197
-
198
- export interface LogEvent {
199
- type: 'stdout' | 'stderr' | 'exit' | 'error';
200
- timestamp: string;
201
- data: string;
202
- processId: string;
203
- exitCode?: number; // For 'exit' events
204
- }
205
-
206
- export interface StreamOptions extends BaseExecOptions {
207
- /**
208
- * Buffer size for streaming output
209
- */
210
- bufferSize?: number;
211
-
212
- /**
213
- * AbortSignal for cancelling stream
214
- */
215
- signal?: AbortSignal;
216
- }
217
-
218
- // Error Types
219
-
220
- export class SandboxError extends Error {
221
- constructor(message: string, public code?: string) {
222
- super(message);
223
- this.name = 'SandboxError';
224
- }
225
- }
226
-
227
- export class ProcessNotFoundError extends SandboxError {
228
- constructor(processId: string) {
229
- super(`Process not found: ${processId}`, 'PROCESS_NOT_FOUND');
230
- this.name = 'ProcessNotFoundError';
231
- }
232
- }
233
-
234
- export class ProcessAlreadyExistsError extends SandboxError {
235
- constructor(processId: string) {
236
- super(`Process already exists: ${processId}`, 'PROCESS_EXISTS');
237
- this.name = 'ProcessAlreadyExistsError';
238
- }
239
- }
240
-
241
- export class ExecutionTimeoutError extends SandboxError {
242
- constructor(timeout: number) {
243
- super(`Execution timed out after ${timeout}ms`, 'EXECUTION_TIMEOUT');
244
- this.name = 'ExecutionTimeoutError';
245
- }
246
- }
247
-
248
- // Internal Container Types
249
-
250
- export interface ProcessRecord {
251
- id: string;
252
- pid?: number;
253
- command: string;
254
- status: ProcessStatus;
255
- startTime: Date;
256
- endTime?: Date;
257
- exitCode?: number;
258
-
259
- // Internal fields
260
- stdout: string; // Accumulated output (ephemeral)
261
- stderr: string; // Accumulated output (ephemeral)
262
-
263
- // Streaming
264
- outputListeners: Set<(stream: 'stdout' | 'stderr', data: string) => void>;
265
- statusListeners: Set<(status: ProcessStatus) => void>;
266
- }
267
-
268
- // Container Request/Response Types
269
-
270
- export interface StartProcessRequest {
271
- command: string;
272
- options?: {
273
- processId?: string;
274
- timeout?: number;
275
- env?: Record<string, string>;
276
- cwd?: string;
277
- encoding?: string;
278
- autoCleanup?: boolean;
279
- };
280
- }
281
-
282
- export interface StartProcessResponse {
283
- process: {
284
- id: string;
285
- pid?: number;
286
- command: string;
287
- status: ProcessStatus;
288
- startTime: string;
289
- endTime?: string | null;
290
- exitCode?: number | null;
291
- sessionId: string;
292
- };
293
- }
294
-
295
- export interface ListProcessesResponse {
296
- processes: Array<{
297
- id: string;
298
- pid?: number;
299
- command: string;
300
- status: ProcessStatus;
301
- startTime: string;
302
- endTime?: string;
303
- exitCode?: number;
304
- }>;
305
- }
306
-
307
- export interface GetProcessResponse {
308
- process: {
309
- id: string;
310
- pid?: number;
311
- command: string;
312
- status: ProcessStatus;
313
- startTime: string;
314
- endTime?: string;
315
- exitCode?: number;
316
- } | null;
317
- }
318
-
319
- export interface GetProcessLogsResponse {
320
- stdout: string;
321
- stderr: string;
322
- processId: string;
323
- }
324
-
325
- // Import code interpreter types
326
- import type {
327
- CodeContext,
328
- CreateContextOptions,
329
- ExecutionResult,
330
- RunCodeOptions
331
- } from './interpreter-types';
332
-
333
- // Main Sandbox Interface
334
-
335
- export interface ISandbox {
336
- // Enhanced execution API
337
- exec(command: string, options?: ExecOptions): Promise<ExecResult>;
338
-
339
- // Background process management
340
- startProcess(command: string, options?: ProcessOptions): Promise<Process>;
341
- listProcesses(): Promise<Process[]>;
342
- getProcess(id: string): Promise<Process | null>;
343
- killProcess(id: string, signal?: string): Promise<void>;
344
- killAllProcesses(): Promise<number>;
345
-
346
- // Advanced streaming - returns ReadableStream that can be converted to AsyncIterable
347
- execStream(command: string, options?: StreamOptions): Promise<ReadableStream<Uint8Array>>;
348
- streamProcessLogs(processId: string, options?: { signal?: AbortSignal }): Promise<ReadableStream<Uint8Array>>;
349
-
350
- // Utility methods
351
- cleanupCompletedProcesses(): Promise<number>;
352
- getProcessLogs(id: string): Promise<{ stdout: string; stderr: string }>;
353
-
354
- // File operations
355
- gitCheckout(repoUrl: string, options: { branch?: string; targetDir?: string }): Promise<GitCheckoutResponse>;
356
- mkdir(path: string, options?: { recursive?: boolean }): Promise<MkdirResponse>;
357
- writeFile(path: string, content: string, options?: { encoding?: string }): Promise<WriteFileResponse>;
358
- deleteFile(path: string): Promise<DeleteFileResponse>;
359
- renameFile(oldPath: string, newPath: string): Promise<RenameFileResponse>;
360
- moveFile(sourcePath: string, destinationPath: string): Promise<MoveFileResponse>;
361
- readFile(path: string, options?: { encoding?: string }): Promise<ReadFileResponse>;
362
- listFiles(path: string, options?: { recursive?: boolean; includeHidden?: boolean }): Promise<ListFilesResponse>;
363
-
364
- // Port management
365
- exposePort(port: number, options: { name?: string; hostname: string }): Promise<{ url: string; port: number; name?: string }>;
366
- unexposePort(port: number): Promise<void>;
367
- getExposedPorts(hostname: string): Promise<Array<{ url: string; port: number; name?: string; exposedAt: string }>>;
368
-
369
- // Environment management
370
- setEnvVars(envVars: Record<string, string>): Promise<void>;
371
- setSandboxName(name: string): Promise<void>;
372
-
373
- // Code Interpreter API
374
- createCodeContext(options?: CreateContextOptions): Promise<CodeContext>;
375
- runCode(code: string, options?: RunCodeOptions): Promise<ExecutionResult>;
376
- runCodeStream(code: string, options?: RunCodeOptions): Promise<ReadableStream>;
377
- listCodeContexts(): Promise<CodeContext[]>;
378
- deleteCodeContext(contextId: string): Promise<void>;
379
- }
380
-
381
- // Execution session returned by createSession()
382
- // Sessions are full-featured sandbox objects with scoped execution context
383
- // Inherits all ISandbox methods except createSession (sessions can't create sub-sessions),
384
- // and setSandboxName (sessions inherit sandbox name).
385
- export interface ExecutionSession extends Omit<ISandbox, 'createSession' | 'setSandboxName'> {
386
- /**
387
- * Session ID
388
- */
389
- id: string;
390
- }
391
-
392
- // API Response Types
393
-
394
- export interface ExecuteResponse {
395
- success: boolean;
396
- stdout: string;
397
- stderr: string;
398
- exitCode: number;
399
- command: string;
400
- timestamp: string;
401
- }
402
-
403
- export interface GitCheckoutResponse {
404
- success: boolean;
405
- stdout: string;
406
- stderr: string;
407
- exitCode: number;
408
- repoUrl: string;
409
- branch: string;
410
- targetDir: string;
411
- timestamp: string;
412
- }
413
-
414
- export interface MkdirResponse {
415
- success: boolean;
416
- stdout: string;
417
- stderr: string;
418
- exitCode: number;
419
- path: string;
420
- recursive: boolean;
421
- timestamp: string;
422
- }
423
-
424
- export interface WriteFileResponse {
425
- success: boolean;
426
- exitCode: number;
427
- path: string;
428
- timestamp: string;
429
- }
430
-
431
- export interface ReadFileResponse {
432
- success: boolean;
433
- exitCode: number;
434
- path: string;
435
- content: string;
436
- timestamp: string;
437
- }
438
-
439
- export interface DeleteFileResponse {
440
- success: boolean;
441
- exitCode: number;
442
- path: string;
443
- timestamp: string;
444
- }
445
-
446
- export interface RenameFileResponse {
447
- success: boolean;
448
- exitCode: number;
449
- oldPath: string;
450
- newPath: string;
451
- timestamp: string;
452
- }
453
-
454
- export interface MoveFileResponse {
455
- success: boolean;
456
- exitCode: number;
457
- sourcePath: string;
458
- destinationPath: string;
459
- timestamp: string;
460
- }
461
-
462
- export interface ListFilesResponse {
463
- success: boolean;
464
- exitCode: number;
465
- path: string;
466
- files: Array<{
467
- name: string;
468
- absolutePath: string;
469
- relativePath: string;
470
- type: 'file' | 'directory' | 'symlink' | 'other';
471
- size: number;
472
- modifiedAt: string;
473
- mode: string;
474
- permissions: {
475
- readable: boolean;
476
- writable: boolean;
477
- executable: boolean;
478
- };
479
- }>;
480
- timestamp: string;
481
- }
482
-
483
- // Type Guards
484
-
485
- export function isExecResult(value: any): value is ExecResult {
486
- return value &&
487
- typeof value.success === 'boolean' &&
488
- typeof value.exitCode === 'number' &&
489
- typeof value.stdout === 'string' &&
490
- typeof value.stderr === 'string';
491
- }
492
-
493
- export function isProcess(value: any): value is Process {
494
- return value &&
495
- typeof value.id === 'string' &&
496
- typeof value.command === 'string' &&
497
- typeof value.status === 'string';
498
- }
499
-
500
- export function isProcessStatus(value: string): value is ProcessStatus {
501
- return ['starting', 'running', 'completed', 'failed', 'killed', 'error'].includes(value);
502
- }