@cloudflare/sandbox 0.0.0-0dad837 → 0.0.0-104f455

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