@cloudflare/sandbox 0.0.0-02ee8fe

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 (80) hide show
  1. package/CHANGELOG.md +311 -0
  2. package/Dockerfile +143 -0
  3. package/README.md +162 -0
  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-UJ3TV4M6.js +7 -0
  11. package/dist/chunk-UJ3TV4M6.js.map +1 -0
  12. package/dist/chunk-YE265ASX.js +2484 -0
  13. package/dist/chunk-YE265ASX.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-CLZWpfGc.d.ts +613 -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 +44 -0
  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 +295 -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 +93 -0
  58. package/src/interpreter.ts +159 -0
  59. package/src/request-handler.ts +180 -0
  60. package/src/sandbox.ts +1045 -0
  61. package/src/security.ts +104 -0
  62. package/src/sse-parser.ts +143 -0
  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 +719 -0
  68. package/tests/file-stream.test.ts +306 -0
  69. package/tests/get-sandbox.test.ts +149 -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/request-handler.test.ts +240 -0
  74. package/tests/sandbox.test.ts +554 -0
  75. package/tests/sse-parser.test.ts +290 -0
  76. package/tests/utility-client.test.ts +332 -0
  77. package/tests/version.test.ts +16 -0
  78. package/tests/wrangler.jsonc +35 -0
  79. package/tsconfig.json +11 -0
  80. package/vitest.config.ts +31 -0
@@ -0,0 +1,84 @@
1
+ import type { Logger } from '@repo/shared';
2
+
3
+ /**
4
+ * Minimal interface for container fetch functionality
5
+ */
6
+ export interface ContainerStub {
7
+ containerFetch(url: string, options: RequestInit, port?: number): Promise<Response>;
8
+ }
9
+
10
+ /**
11
+ * Shared HTTP client configuration options
12
+ */
13
+ export interface HttpClientOptions {
14
+ logger?: Logger;
15
+ baseUrl?: string;
16
+ port?: number;
17
+ stub?: ContainerStub;
18
+ onCommandComplete?: (
19
+ success: boolean,
20
+ exitCode: number,
21
+ stdout: string,
22
+ stderr: string,
23
+ command: string
24
+ ) => void;
25
+ onError?: (error: string, command?: string) => void;
26
+ }
27
+
28
+ /**
29
+ * Base response interface for all API responses
30
+ */
31
+ export interface BaseApiResponse {
32
+ success: boolean;
33
+ timestamp: string;
34
+ }
35
+
36
+ /**
37
+ * Standard error response structure - matches BaseHandler.createErrorResponse()
38
+ */
39
+ export interface ApiErrorResponse {
40
+ success: false;
41
+ error: string;
42
+ code: string;
43
+ details?: any;
44
+ timestamp: string;
45
+ }
46
+
47
+ /**
48
+ * Validation error response structure - matches ValidationMiddleware
49
+ */
50
+ export interface ValidationErrorResponse {
51
+ error: string;
52
+ message: string;
53
+ details?: any[];
54
+ timestamp: string;
55
+ }
56
+
57
+ /**
58
+ * Legacy error response interface - deprecated, use ApiErrorResponse
59
+ */
60
+ export interface ErrorResponse {
61
+ error: string;
62
+ details?: string;
63
+ code?: string;
64
+ }
65
+
66
+ /**
67
+ * HTTP request configuration
68
+ */
69
+ export interface RequestConfig extends RequestInit {
70
+ endpoint: string;
71
+ data?: Record<string, any>;
72
+ }
73
+
74
+ /**
75
+ * Typed response handler
76
+ */
77
+ export type ResponseHandler<T> = (response: Response) => Promise<T>;
78
+
79
+ /**
80
+ * Common session-aware request interface
81
+ */
82
+ export interface SessionRequest {
83
+ sessionId?: string;
84
+ }
@@ -0,0 +1,119 @@
1
+ import { BaseHttpClient } from './base-client';
2
+ import type { BaseApiResponse, HttpClientOptions } from './types';
3
+
4
+ /**
5
+ * Response interface for ping operations
6
+ */
7
+ export interface PingResponse extends BaseApiResponse {
8
+ message: string;
9
+ uptime?: number;
10
+ }
11
+
12
+ /**
13
+ * Response interface for getting available commands
14
+ */
15
+ export interface CommandsResponse extends BaseApiResponse {
16
+ availableCommands: string[];
17
+ count: number;
18
+ }
19
+
20
+ /**
21
+ * Response interface for getting container version
22
+ */
23
+ export interface VersionResponse extends BaseApiResponse {
24
+ version: string;
25
+ }
26
+
27
+ /**
28
+ * Request interface for creating sessions
29
+ */
30
+ export interface CreateSessionRequest {
31
+ id: string;
32
+ env?: Record<string, string>;
33
+ cwd?: string;
34
+ }
35
+
36
+ /**
37
+ * Response interface for creating sessions
38
+ */
39
+ export interface CreateSessionResponse extends BaseApiResponse {
40
+ id: string;
41
+ message: string;
42
+ }
43
+
44
+ /**
45
+ * Client for health checks and utility operations
46
+ */
47
+ export class UtilityClient extends BaseHttpClient {
48
+
49
+ /**
50
+ * Ping the sandbox to check if it's responsive
51
+ */
52
+ async ping(): Promise<string> {
53
+ try {
54
+ const response = await this.get<PingResponse>('/api/ping');
55
+
56
+ this.logSuccess('Ping successful', response.message);
57
+ return response.message;
58
+ } catch (error) {
59
+ this.logError('ping', error);
60
+ throw error;
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Get list of available commands in the sandbox environment
66
+ */
67
+ async getCommands(): Promise<string[]> {
68
+ try {
69
+ const response = await this.get<CommandsResponse>('/api/commands');
70
+
71
+ this.logSuccess(
72
+ 'Commands retrieved',
73
+ `${response.count} commands available`
74
+ );
75
+
76
+ return response.availableCommands;
77
+ } catch (error) {
78
+ this.logError('getCommands', error);
79
+ throw error;
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Create a new execution session
85
+ * @param options - Session configuration (id, env, cwd)
86
+ */
87
+ async createSession(options: CreateSessionRequest): Promise<CreateSessionResponse> {
88
+ try {
89
+ const response = await this.post<CreateSessionResponse>(
90
+ '/api/session/create',
91
+ options
92
+ );
93
+
94
+ this.logSuccess('Session created', `ID: ${options.id}`);
95
+ return response;
96
+ } catch (error) {
97
+ this.logError('createSession', error);
98
+ throw error;
99
+ }
100
+ }
101
+
102
+ /**
103
+ * Get the container version
104
+ * Returns the version embedded in the Docker image during build
105
+ */
106
+ async getVersion(): Promise<string> {
107
+ try {
108
+ const response = await this.get<VersionResponse>('/api/version');
109
+
110
+ this.logSuccess('Version retrieved', response.version);
111
+ return response.version;
112
+ } catch (error) {
113
+ // If version endpoint doesn't exist (old container), return 'unknown'
114
+ // This allows for backward compatibility
115
+ this.logger.debug('Failed to get container version (may be old container)', { error });
116
+ return 'unknown';
117
+ }
118
+ }
119
+ }
@@ -0,0 +1,180 @@
1
+ /**
2
+ * Error adapter that converts ErrorResponse to appropriate Error class
3
+ *
4
+ * Simple switch statement - we trust the container sends correct context
5
+ * No validation overhead since we control both sides
6
+ */
7
+
8
+ import type {
9
+ CodeExecutionContext,
10
+ CommandErrorContext,
11
+ CommandNotFoundContext,
12
+ ContextNotFoundContext,ErrorResponse,
13
+ FileExistsContext,
14
+ FileNotFoundContext,
15
+ FileSystemContext,
16
+ GitAuthFailedContext,
17
+ GitBranchNotFoundContext,
18
+ GitErrorContext,
19
+ GitRepositoryNotFoundContext,
20
+ InternalErrorContext,
21
+ InterpreterNotReadyContext,
22
+ InvalidPortContext,
23
+ PortAlreadyExposedContext,
24
+ PortErrorContext,
25
+ PortNotExposedContext,
26
+ ProcessErrorContext,
27
+ ProcessNotFoundContext,
28
+ ValidationFailedContext,} from '@repo/shared/errors';
29
+ import { ErrorCode } from '@repo/shared/errors';
30
+
31
+ import {
32
+ CodeExecutionError,
33
+ CommandError,
34
+ CommandNotFoundError,
35
+ ContextNotFoundError,
36
+ CustomDomainRequiredError,
37
+ FileExistsError,
38
+ FileNotFoundError,
39
+ FileSystemError,
40
+ GitAuthenticationError,
41
+ GitBranchNotFoundError,
42
+ GitCheckoutError,
43
+ GitCloneError,
44
+ GitError,
45
+ GitNetworkError,
46
+ GitRepositoryNotFoundError,
47
+ InterpreterNotReadyError,
48
+ InvalidGitUrlError,
49
+ InvalidPortError,
50
+ PermissionDeniedError,
51
+ PortAlreadyExposedError,
52
+ PortError,
53
+ PortInUseError,
54
+ PortNotExposedError,
55
+ ProcessError,
56
+ ProcessNotFoundError,
57
+ SandboxError,
58
+ ServiceNotRespondingError,
59
+ ValidationFailedError,
60
+ } from './classes';
61
+
62
+ /**
63
+ * Convert ErrorResponse to appropriate Error class
64
+ * Simple switch statement - we trust the container sends correct context
65
+ */
66
+ export function createErrorFromResponse(errorResponse: ErrorResponse): Error {
67
+ // We trust the container sends correct context, use type assertions
68
+ switch (errorResponse.code) {
69
+ // File System Errors
70
+ case ErrorCode.FILE_NOT_FOUND:
71
+ return new FileNotFoundError(errorResponse as unknown as ErrorResponse<FileNotFoundContext>);
72
+
73
+ case ErrorCode.FILE_EXISTS:
74
+ return new FileExistsError(errorResponse as unknown as ErrorResponse<FileExistsContext>);
75
+
76
+ case ErrorCode.PERMISSION_DENIED:
77
+ return new PermissionDeniedError(errorResponse as unknown as ErrorResponse<FileSystemContext>);
78
+
79
+ case ErrorCode.IS_DIRECTORY:
80
+ case ErrorCode.NOT_DIRECTORY:
81
+ case ErrorCode.NO_SPACE:
82
+ case ErrorCode.TOO_MANY_FILES:
83
+ case ErrorCode.RESOURCE_BUSY:
84
+ case ErrorCode.READ_ONLY:
85
+ case ErrorCode.NAME_TOO_LONG:
86
+ case ErrorCode.TOO_MANY_LINKS:
87
+ case ErrorCode.FILESYSTEM_ERROR:
88
+ return new FileSystemError(errorResponse as unknown as ErrorResponse<FileSystemContext>);
89
+
90
+ // Command Errors
91
+ case ErrorCode.COMMAND_NOT_FOUND:
92
+ return new CommandNotFoundError(errorResponse as unknown as ErrorResponse<CommandNotFoundContext>);
93
+
94
+ case ErrorCode.COMMAND_PERMISSION_DENIED:
95
+ case ErrorCode.COMMAND_EXECUTION_ERROR:
96
+ case ErrorCode.INVALID_COMMAND:
97
+ case ErrorCode.STREAM_START_ERROR:
98
+ return new CommandError(errorResponse as unknown as ErrorResponse<CommandErrorContext>);
99
+
100
+ // Process Errors
101
+ case ErrorCode.PROCESS_NOT_FOUND:
102
+ return new ProcessNotFoundError(errorResponse as unknown as ErrorResponse<ProcessNotFoundContext>);
103
+
104
+ case ErrorCode.PROCESS_PERMISSION_DENIED:
105
+ case ErrorCode.PROCESS_ERROR:
106
+ return new ProcessError(errorResponse as unknown as ErrorResponse<ProcessErrorContext>);
107
+
108
+ // Port Errors
109
+ case ErrorCode.PORT_ALREADY_EXPOSED:
110
+ return new PortAlreadyExposedError(errorResponse as unknown as ErrorResponse<PortAlreadyExposedContext>);
111
+
112
+ case ErrorCode.PORT_NOT_EXPOSED:
113
+ return new PortNotExposedError(errorResponse as unknown as ErrorResponse<PortNotExposedContext>);
114
+
115
+ case ErrorCode.INVALID_PORT_NUMBER:
116
+ case ErrorCode.INVALID_PORT:
117
+ return new InvalidPortError(errorResponse as unknown as ErrorResponse<InvalidPortContext>);
118
+
119
+ case ErrorCode.SERVICE_NOT_RESPONDING:
120
+ return new ServiceNotRespondingError(errorResponse as unknown as ErrorResponse<PortErrorContext>);
121
+
122
+ case ErrorCode.PORT_IN_USE:
123
+ return new PortInUseError(errorResponse as unknown as ErrorResponse<PortErrorContext>);
124
+
125
+ case ErrorCode.PORT_OPERATION_ERROR:
126
+ return new PortError(errorResponse as unknown as ErrorResponse<PortErrorContext>);
127
+
128
+ case ErrorCode.CUSTOM_DOMAIN_REQUIRED:
129
+ return new CustomDomainRequiredError(errorResponse as unknown as ErrorResponse<InternalErrorContext>);
130
+
131
+ // Git Errors
132
+ case ErrorCode.GIT_REPOSITORY_NOT_FOUND:
133
+ return new GitRepositoryNotFoundError(errorResponse as unknown as ErrorResponse<GitRepositoryNotFoundContext>);
134
+
135
+ case ErrorCode.GIT_AUTH_FAILED:
136
+ return new GitAuthenticationError(errorResponse as unknown as ErrorResponse<GitAuthFailedContext>);
137
+
138
+ case ErrorCode.GIT_BRANCH_NOT_FOUND:
139
+ return new GitBranchNotFoundError(errorResponse as unknown as ErrorResponse<GitBranchNotFoundContext>);
140
+
141
+ case ErrorCode.GIT_NETWORK_ERROR:
142
+ return new GitNetworkError(errorResponse as unknown as ErrorResponse<GitErrorContext>);
143
+
144
+ case ErrorCode.GIT_CLONE_FAILED:
145
+ return new GitCloneError(errorResponse as unknown as ErrorResponse<GitErrorContext>);
146
+
147
+ case ErrorCode.GIT_CHECKOUT_FAILED:
148
+ return new GitCheckoutError(errorResponse as unknown as ErrorResponse<GitErrorContext>);
149
+
150
+ case ErrorCode.INVALID_GIT_URL:
151
+ return new InvalidGitUrlError(errorResponse as unknown as ErrorResponse<ValidationFailedContext>);
152
+
153
+ case ErrorCode.GIT_OPERATION_FAILED:
154
+ return new GitError(errorResponse as unknown as ErrorResponse<GitErrorContext>);
155
+
156
+ // Code Interpreter Errors
157
+ case ErrorCode.INTERPRETER_NOT_READY:
158
+ return new InterpreterNotReadyError(errorResponse as unknown as ErrorResponse<InterpreterNotReadyContext>);
159
+
160
+ case ErrorCode.CONTEXT_NOT_FOUND:
161
+ return new ContextNotFoundError(errorResponse as unknown as ErrorResponse<ContextNotFoundContext>);
162
+
163
+ case ErrorCode.CODE_EXECUTION_ERROR:
164
+ return new CodeExecutionError(errorResponse as unknown as ErrorResponse<CodeExecutionContext>);
165
+
166
+ // Validation Errors
167
+ case ErrorCode.VALIDATION_FAILED:
168
+ return new ValidationFailedError(errorResponse as unknown as ErrorResponse<ValidationFailedContext>);
169
+
170
+ // Generic Errors
171
+ case ErrorCode.INVALID_JSON_RESPONSE:
172
+ case ErrorCode.UNKNOWN_ERROR:
173
+ case ErrorCode.INTERNAL_ERROR:
174
+ return new SandboxError(errorResponse as unknown as ErrorResponse<InternalErrorContext>);
175
+
176
+ default:
177
+ // Fallback for unknown error codes
178
+ return new SandboxError(errorResponse);
179
+ }
180
+ }