@cloudflare/sandbox 0.0.0-bb855ca → 0.0.0-c39674b

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 (95) hide show
  1. package/CHANGELOG.md +151 -0
  2. package/Dockerfile +107 -66
  3. package/README.md +88 -710
  4. package/dist/chunk-BFVUNTP4.js +104 -0
  5. package/dist/chunk-BFVUNTP4.js.map +1 -0
  6. package/dist/chunk-EKSWCBCA.js +86 -0
  7. package/dist/chunk-EKSWCBCA.js.map +1 -0
  8. package/dist/chunk-JXZMAU2C.js +559 -0
  9. package/dist/chunk-JXZMAU2C.js.map +1 -0
  10. package/dist/chunk-UZQBJBJF.js +7 -0
  11. package/dist/chunk-UZQBJBJF.js.map +1 -0
  12. package/dist/chunk-YEZBBFK7.js +2420 -0
  13. package/dist/chunk-YEZBBFK7.js.map +1 -0
  14. package/dist/chunk-Z532A7QC.js +78 -0
  15. package/dist/chunk-Z532A7QC.js.map +1 -0
  16. package/dist/file-stream.d.ts +43 -0
  17. package/dist/file-stream.js +9 -0
  18. package/dist/file-stream.js.map +1 -0
  19. package/dist/index.d.ts +9 -0
  20. package/dist/index.js +67 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/interpreter.d.ts +33 -0
  23. package/dist/interpreter.js +8 -0
  24. package/dist/interpreter.js.map +1 -0
  25. package/dist/request-handler.d.ts +18 -0
  26. package/dist/request-handler.js +13 -0
  27. package/dist/request-handler.js.map +1 -0
  28. package/dist/sandbox-DMlNr93l.d.ts +596 -0
  29. package/dist/sandbox.d.ts +4 -0
  30. package/dist/sandbox.js +13 -0
  31. package/dist/sandbox.js.map +1 -0
  32. package/dist/security.d.ts +31 -0
  33. package/dist/security.js +13 -0
  34. package/dist/security.js.map +1 -0
  35. package/dist/sse-parser.d.ts +28 -0
  36. package/dist/sse-parser.js +11 -0
  37. package/dist/sse-parser.js.map +1 -0
  38. package/dist/version.d.ts +8 -0
  39. package/dist/version.js +7 -0
  40. package/dist/version.js.map +1 -0
  41. package/package.json +13 -5
  42. package/src/clients/base-client.ts +280 -0
  43. package/src/clients/command-client.ts +115 -0
  44. package/src/clients/file-client.ts +269 -0
  45. package/src/clients/git-client.ts +92 -0
  46. package/src/clients/index.ts +64 -0
  47. package/src/clients/interpreter-client.ts +329 -0
  48. package/src/clients/port-client.ts +105 -0
  49. package/src/clients/process-client.ts +177 -0
  50. package/src/clients/sandbox-client.ts +41 -0
  51. package/src/clients/types.ts +84 -0
  52. package/src/clients/utility-client.ts +119 -0
  53. package/src/errors/adapter.ts +180 -0
  54. package/src/errors/classes.ts +469 -0
  55. package/src/errors/index.ts +105 -0
  56. package/src/file-stream.ts +164 -0
  57. package/src/index.ts +85 -21
  58. package/src/interpreter.ts +22 -13
  59. package/src/request-handler.ts +69 -43
  60. package/src/sandbox.ts +663 -444
  61. package/src/security.ts +14 -23
  62. package/src/sse-parser.ts +4 -8
  63. package/src/version.ts +6 -0
  64. package/startup.sh +3 -0
  65. package/tests/base-client.test.ts +328 -0
  66. package/tests/command-client.test.ts +407 -0
  67. package/tests/file-client.test.ts +643 -0
  68. package/tests/file-stream.test.ts +306 -0
  69. package/tests/get-sandbox.test.ts +110 -0
  70. package/tests/git-client.test.ts +328 -0
  71. package/tests/port-client.test.ts +301 -0
  72. package/tests/process-client.test.ts +658 -0
  73. package/tests/sandbox.test.ts +465 -0
  74. package/tests/sse-parser.test.ts +290 -0
  75. package/tests/utility-client.test.ts +332 -0
  76. package/tests/version.test.ts +16 -0
  77. package/tests/wrangler.jsonc +35 -0
  78. package/tsconfig.json +9 -1
  79. package/vitest.config.ts +31 -0
  80. package/container_src/bun.lock +0 -122
  81. package/container_src/handler/exec.ts +0 -340
  82. package/container_src/handler/file.ts +0 -844
  83. package/container_src/handler/git.ts +0 -182
  84. package/container_src/handler/ports.ts +0 -314
  85. package/container_src/handler/process.ts +0 -640
  86. package/container_src/index.ts +0 -531
  87. package/container_src/jupyter-server.ts +0 -336
  88. package/container_src/mime-processor.ts +0 -255
  89. package/container_src/package.json +0 -18
  90. package/container_src/startup.sh +0 -52
  91. package/container_src/types.ts +0 -108
  92. package/src/client.ts +0 -1021
  93. package/src/interpreter-types.ts +0 -383
  94. package/src/jupyter-client.ts +0 -266
  95. package/src/types.ts +0 -401
package/src/types.ts DELETED
@@ -1,401 +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
- // Code Interpreter API
375
- createCodeContext(options?: CreateContextOptions): Promise<CodeContext>;
376
- runCode(code: string, options?: RunCodeOptions): Promise<ExecutionResult>;
377
- runCodeStream(code: string, options?: RunCodeOptions): Promise<ReadableStream>;
378
- listCodeContexts(): Promise<CodeContext[]>;
379
- deleteCodeContext(contextId: string): Promise<void>;
380
- }
381
-
382
- // Type Guards
383
-
384
- export function isExecResult(value: any): value is ExecResult {
385
- return value &&
386
- typeof value.success === 'boolean' &&
387
- typeof value.exitCode === 'number' &&
388
- typeof value.stdout === 'string' &&
389
- typeof value.stderr === 'string';
390
- }
391
-
392
- export function isProcess(value: any): value is Process {
393
- return value &&
394
- typeof value.id === 'string' &&
395
- typeof value.command === 'string' &&
396
- typeof value.status === 'string';
397
- }
398
-
399
- export function isProcessStatus(value: string): value is ProcessStatus {
400
- return ['starting', 'running', 'completed', 'failed', 'killed', 'error'].includes(value);
401
- }