@cloudflare/sandbox 0.5.6 → 0.6.0

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 (56) hide show
  1. package/Dockerfile +54 -56
  2. package/dist/index.d.ts +1 -0
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +3 -1
  5. package/dist/index.js.map +1 -1
  6. package/package.json +11 -6
  7. package/.turbo/turbo-build.log +0 -23
  8. package/CHANGELOG.md +0 -463
  9. package/src/clients/base-client.ts +0 -356
  10. package/src/clients/command-client.ts +0 -133
  11. package/src/clients/file-client.ts +0 -300
  12. package/src/clients/git-client.ts +0 -98
  13. package/src/clients/index.ts +0 -64
  14. package/src/clients/interpreter-client.ts +0 -339
  15. package/src/clients/port-client.ts +0 -105
  16. package/src/clients/process-client.ts +0 -198
  17. package/src/clients/sandbox-client.ts +0 -39
  18. package/src/clients/types.ts +0 -88
  19. package/src/clients/utility-client.ts +0 -156
  20. package/src/errors/adapter.ts +0 -238
  21. package/src/errors/classes.ts +0 -594
  22. package/src/errors/index.ts +0 -109
  23. package/src/file-stream.ts +0 -175
  24. package/src/index.ts +0 -121
  25. package/src/interpreter.ts +0 -168
  26. package/src/openai/index.ts +0 -465
  27. package/src/request-handler.ts +0 -184
  28. package/src/sandbox.ts +0 -1937
  29. package/src/security.ts +0 -119
  30. package/src/sse-parser.ts +0 -147
  31. package/src/storage-mount/credential-detection.ts +0 -41
  32. package/src/storage-mount/errors.ts +0 -51
  33. package/src/storage-mount/index.ts +0 -17
  34. package/src/storage-mount/provider-detection.ts +0 -93
  35. package/src/storage-mount/types.ts +0 -17
  36. package/src/version.ts +0 -6
  37. package/tests/base-client.test.ts +0 -582
  38. package/tests/command-client.test.ts +0 -444
  39. package/tests/file-client.test.ts +0 -831
  40. package/tests/file-stream.test.ts +0 -310
  41. package/tests/get-sandbox.test.ts +0 -172
  42. package/tests/git-client.test.ts +0 -455
  43. package/tests/openai-shell-editor.test.ts +0 -434
  44. package/tests/port-client.test.ts +0 -283
  45. package/tests/process-client.test.ts +0 -649
  46. package/tests/request-handler.test.ts +0 -292
  47. package/tests/sandbox.test.ts +0 -890
  48. package/tests/sse-parser.test.ts +0 -291
  49. package/tests/storage-mount/credential-detection.test.ts +0 -119
  50. package/tests/storage-mount/provider-detection.test.ts +0 -77
  51. package/tests/utility-client.test.ts +0 -339
  52. package/tests/version.test.ts +0 -16
  53. package/tests/wrangler.jsonc +0 -35
  54. package/tsconfig.json +0 -11
  55. package/tsdown.config.ts +0 -13
  56. package/vitest.config.ts +0 -31
@@ -1,175 +0,0 @@
1
- import type { FileChunk, FileMetadata, FileStreamEvent } from '@repo/shared';
2
-
3
- /**
4
- * Parse SSE (Server-Sent Events) lines from a stream
5
- */
6
- async function* parseSSE(
7
- stream: ReadableStream<Uint8Array>
8
- ): AsyncGenerator<FileStreamEvent> {
9
- const reader = stream.getReader();
10
- const decoder = new TextDecoder();
11
- let buffer = '';
12
-
13
- try {
14
- while (true) {
15
- const { done, value } = await reader.read();
16
-
17
- if (done) {
18
- break;
19
- }
20
-
21
- buffer += decoder.decode(value, { stream: true });
22
- const lines = buffer.split('\n');
23
-
24
- // Keep the last incomplete line in the buffer
25
- buffer = lines.pop() || '';
26
-
27
- for (const line of lines) {
28
- if (line.startsWith('data: ')) {
29
- const data = line.slice(6); // Remove 'data: ' prefix
30
- try {
31
- const event = JSON.parse(data) as FileStreamEvent;
32
- yield event;
33
- } catch {
34
- // Skip invalid JSON events and continue processing
35
- }
36
- }
37
- }
38
- }
39
- } finally {
40
- // Cancel the stream first to properly terminate HTTP connections when breaking early
41
- try {
42
- await reader.cancel();
43
- } catch {
44
- // Ignore cancel errors (stream may already be closed)
45
- }
46
- reader.releaseLock();
47
- }
48
- }
49
-
50
- /**
51
- * Stream a file from the sandbox with automatic base64 decoding for binary files
52
- *
53
- * @param stream - The ReadableStream from readFileStream()
54
- * @returns AsyncGenerator that yields FileChunk (string for text, Uint8Array for binary)
55
- *
56
- * @example
57
- * ```ts
58
- * const stream = await sandbox.readFileStream('/path/to/file.png');
59
- * for await (const chunk of streamFile(stream)) {
60
- * if (chunk instanceof Uint8Array) {
61
- * // Binary chunk
62
- * console.log('Binary chunk:', chunk.length, 'bytes');
63
- * } else {
64
- * // Text chunk
65
- * console.log('Text chunk:', chunk);
66
- * }
67
- * }
68
- * ```
69
- */
70
- export async function* streamFile(
71
- stream: ReadableStream<Uint8Array>
72
- ): AsyncGenerator<FileChunk, FileMetadata> {
73
- let metadata: FileMetadata | null = null;
74
-
75
- for await (const event of parseSSE(stream)) {
76
- switch (event.type) {
77
- case 'metadata':
78
- metadata = {
79
- mimeType: event.mimeType,
80
- size: event.size,
81
- isBinary: event.isBinary,
82
- encoding: event.encoding
83
- };
84
- break;
85
-
86
- case 'chunk':
87
- if (!metadata) {
88
- throw new Error('Received chunk before metadata');
89
- }
90
-
91
- if (metadata.isBinary && metadata.encoding === 'base64') {
92
- // Decode base64 to Uint8Array for binary files
93
- const binaryString = atob(event.data);
94
- const bytes = new Uint8Array(binaryString.length);
95
- for (let i = 0; i < binaryString.length; i++) {
96
- bytes[i] = binaryString.charCodeAt(i);
97
- }
98
- yield bytes;
99
- } else {
100
- // Text files - yield as-is
101
- yield event.data;
102
- }
103
- break;
104
-
105
- case 'complete':
106
- if (!metadata) {
107
- throw new Error('Stream completed without metadata');
108
- }
109
- return metadata;
110
-
111
- case 'error':
112
- throw new Error(`File streaming error: ${event.error}`);
113
- }
114
- }
115
-
116
- throw new Error('Stream ended unexpectedly');
117
- }
118
-
119
- /**
120
- * Collect an entire file into memory from a stream
121
- *
122
- * @param stream - The ReadableStream from readFileStream()
123
- * @returns Object containing the file content and metadata
124
- *
125
- * @example
126
- * ```ts
127
- * const stream = await sandbox.readFileStream('/path/to/file.txt');
128
- * const { content, metadata } = await collectFile(stream);
129
- * console.log('Content:', content);
130
- * console.log('MIME type:', metadata.mimeType);
131
- * ```
132
- */
133
- export async function collectFile(stream: ReadableStream<Uint8Array>): Promise<{
134
- content: string | Uint8Array;
135
- metadata: FileMetadata;
136
- }> {
137
- const chunks: Array<string | Uint8Array> = [];
138
-
139
- // Iterate through the generator and get the return value (metadata)
140
- const generator = streamFile(stream);
141
- let result = await generator.next();
142
-
143
- while (!result.done) {
144
- chunks.push(result.value);
145
- result = await generator.next();
146
- }
147
-
148
- const metadata = result.value;
149
-
150
- if (!metadata) {
151
- throw new Error('Failed to get file metadata');
152
- }
153
-
154
- // Combine chunks based on type
155
- if (metadata.isBinary) {
156
- // Binary file - combine Uint8Arrays
157
- const totalLength = chunks.reduce(
158
- (sum, chunk) => sum + (chunk instanceof Uint8Array ? chunk.length : 0),
159
- 0
160
- );
161
- const combined = new Uint8Array(totalLength);
162
- let offset = 0;
163
- for (const chunk of chunks) {
164
- if (chunk instanceof Uint8Array) {
165
- combined.set(chunk, offset);
166
- offset += chunk.length;
167
- }
168
- }
169
- return { content: combined, metadata };
170
- } else {
171
- // Text file - combine strings
172
- const combined = chunks.filter((c) => typeof c === 'string').join('');
173
- return { content: combined, metadata };
174
- }
175
- }
package/src/index.ts DELETED
@@ -1,121 +0,0 @@
1
- // Export the main Sandbox class and utilities
2
-
3
- // Export the new client architecture
4
- export {
5
- CommandClient,
6
- FileClient,
7
- GitClient,
8
- PortClient,
9
- ProcessClient,
10
- SandboxClient,
11
- UtilityClient
12
- } from './clients';
13
- export { getSandbox, Sandbox } from './sandbox';
14
-
15
- // Legacy types are now imported from the new client architecture
16
-
17
- // Export core SDK types for consumers
18
- export type {
19
- BaseExecOptions,
20
- BucketCredentials,
21
- BucketProvider,
22
- CodeContext,
23
- CreateContextOptions,
24
- ExecEvent,
25
- ExecOptions,
26
- ExecResult,
27
- ExecutionResult,
28
- ExecutionSession,
29
- FileChunk,
30
- FileMetadata,
31
- FileStreamEvent,
32
- GitCheckoutResult,
33
- ISandbox,
34
- ListFilesOptions,
35
- LogEvent,
36
- MountBucketOptions,
37
- Process,
38
- ProcessOptions,
39
- ProcessStatus,
40
- RunCodeOptions,
41
- SandboxOptions,
42
- SessionOptions,
43
- StreamOptions
44
- } from '@repo/shared';
45
- // Export type guards for runtime validation
46
- export { isExecResult, isProcess, isProcessStatus } from '@repo/shared';
47
- // Export all client types from new architecture
48
- export type {
49
- BaseApiResponse,
50
- CommandsResponse,
51
- ContainerStub,
52
-
53
- // Utility client types
54
- CreateSessionRequest,
55
- CreateSessionResponse,
56
- DeleteSessionRequest,
57
- DeleteSessionResponse,
58
- ErrorResponse,
59
-
60
- // Command client types
61
- ExecuteRequest,
62
- ExecuteResponse as CommandExecuteResponse,
63
-
64
- // Port client types
65
- ExposePortRequest,
66
- FileOperationRequest,
67
-
68
- // Git client types
69
- GitCheckoutRequest,
70
- // Base client types
71
- HttpClientOptions as SandboxClientOptions,
72
-
73
- // File client types
74
- MkdirRequest,
75
- PingResponse,
76
- PortCloseResult,
77
- PortExposeResult,
78
- PortListResult,
79
- ProcessCleanupResult,
80
- ProcessInfoResult,
81
- ProcessKillResult,
82
- ProcessListResult,
83
- ProcessLogsResult,
84
- ProcessStartResult,
85
- ReadFileRequest,
86
- RequestConfig,
87
- ResponseHandler,
88
- SessionRequest,
89
-
90
- // Process client types
91
- StartProcessRequest,
92
- UnexposePortRequest,
93
- WriteFileRequest
94
- } from './clients';
95
- export type {
96
- ExecutionCallbacks,
97
- InterpreterClient
98
- } from './clients/interpreter-client.js';
99
- // Export file streaming utilities for binary file support
100
- export { collectFile, streamFile } from './file-stream';
101
- // Export interpreter functionality
102
- export { CodeInterpreter } from './interpreter.js';
103
- // Re-export request handler utilities
104
- export {
105
- proxyToSandbox,
106
- type RouteInfo,
107
- type SandboxEnv
108
- } from './request-handler';
109
- // Export SSE parser for converting ReadableStream to AsyncIterable
110
- export {
111
- asyncIterableToSSEStream,
112
- parseSSEStream,
113
- responseToAsyncIterable
114
- } from './sse-parser';
115
- // Export bucket mounting errors
116
- export {
117
- BucketMountError,
118
- InvalidMountConfigError,
119
- MissingCredentialsError,
120
- S3FSMountError
121
- } from './storage-mount/errors';
@@ -1,168 +0,0 @@
1
- import {
2
- type CodeContext,
3
- type CreateContextOptions,
4
- Execution,
5
- type ExecutionError,
6
- type OutputMessage,
7
- type Result,
8
- ResultImpl,
9
- type RunCodeOptions
10
- } from '@repo/shared';
11
- import type { InterpreterClient } from './clients/interpreter-client.js';
12
- import type { Sandbox } from './sandbox.js';
13
- import { validateLanguage } from './security.js';
14
-
15
- export class CodeInterpreter {
16
- private interpreterClient: InterpreterClient;
17
- private contexts = new Map<string, CodeContext>();
18
-
19
- constructor(sandbox: Sandbox) {
20
- // In init-testing architecture, client is a SandboxClient with an interpreter property
21
- this.interpreterClient = (sandbox.client as any)
22
- .interpreter as InterpreterClient;
23
- }
24
-
25
- /**
26
- * Create a new code execution context
27
- */
28
- async createCodeContext(
29
- options: CreateContextOptions = {}
30
- ): Promise<CodeContext> {
31
- // Validate language before sending to container
32
- validateLanguage(options.language);
33
-
34
- const context = await this.interpreterClient.createCodeContext(options);
35
- this.contexts.set(context.id, context);
36
- return context;
37
- }
38
-
39
- /**
40
- * Run code with optional context
41
- */
42
- async runCode(
43
- code: string,
44
- options: RunCodeOptions = {}
45
- ): Promise<Execution> {
46
- // Get or create context
47
- let context = options.context;
48
- if (!context) {
49
- // Try to find or create a default context for the language
50
- const language = options.language || 'python';
51
- context = await this.getOrCreateDefaultContext(language);
52
- }
53
-
54
- // Create execution object to collect results
55
- const execution = new Execution(code, context);
56
-
57
- // Stream execution
58
- await this.interpreterClient.runCodeStream(
59
- context.id,
60
- code,
61
- options.language,
62
- {
63
- onStdout: (output: OutputMessage) => {
64
- execution.logs.stdout.push(output.text);
65
- if (options.onStdout) return options.onStdout(output);
66
- },
67
- onStderr: (output: OutputMessage) => {
68
- execution.logs.stderr.push(output.text);
69
- if (options.onStderr) return options.onStderr(output);
70
- },
71
- onResult: async (result: Result) => {
72
- execution.results.push(new ResultImpl(result) as any);
73
- if (options.onResult) return options.onResult(result);
74
- },
75
- onError: (error: ExecutionError) => {
76
- execution.error = error;
77
- if (options.onError) return options.onError(error);
78
- }
79
- }
80
- );
81
-
82
- return execution;
83
- }
84
-
85
- /**
86
- * Run code and return a streaming response
87
- */
88
- async runCodeStream(
89
- code: string,
90
- options: RunCodeOptions = {}
91
- ): Promise<ReadableStream> {
92
- // Get or create context
93
- let context = options.context;
94
- if (!context) {
95
- const language = options.language || 'python';
96
- context = await this.getOrCreateDefaultContext(language);
97
- }
98
-
99
- // Create streaming response
100
- // Note: doFetch is protected but we need direct access for raw stream response
101
- const response = await (this.interpreterClient as any).doFetch(
102
- '/api/execute/code',
103
- {
104
- method: 'POST',
105
- headers: {
106
- 'Content-Type': 'application/json',
107
- Accept: 'text/event-stream'
108
- },
109
- body: JSON.stringify({
110
- context_id: context.id,
111
- code,
112
- language: options.language
113
- })
114
- }
115
- );
116
-
117
- if (!response.ok) {
118
- const errorData = (await response
119
- .json()
120
- .catch(() => ({ error: 'Unknown error' }))) as { error?: string };
121
- throw new Error(
122
- errorData.error || `Failed to execute code: ${response.status}`
123
- );
124
- }
125
-
126
- if (!response.body) {
127
- throw new Error('No response body for streaming execution');
128
- }
129
-
130
- return response.body;
131
- }
132
-
133
- /**
134
- * List all code contexts
135
- */
136
- async listCodeContexts(): Promise<CodeContext[]> {
137
- const contexts = await this.interpreterClient.listCodeContexts();
138
-
139
- // Update local cache
140
- for (const context of contexts) {
141
- this.contexts.set(context.id, context);
142
- }
143
-
144
- return contexts;
145
- }
146
-
147
- /**
148
- * Delete a code context
149
- */
150
- async deleteCodeContext(contextId: string): Promise<void> {
151
- await this.interpreterClient.deleteCodeContext(contextId);
152
- this.contexts.delete(contextId);
153
- }
154
-
155
- private async getOrCreateDefaultContext(
156
- language: 'python' | 'javascript' | 'typescript'
157
- ): Promise<CodeContext> {
158
- // Check if we have a cached context for this language
159
- for (const context of this.contexts.values()) {
160
- if (context.language === language) {
161
- return context;
162
- }
163
- }
164
-
165
- // Create new default context
166
- return this.createCodeContext({ language });
167
- }
168
- }