@cloudflare/sandbox 0.0.0-444d2da → 0.0.0-4572082

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 (79) hide show
  1. package/CHANGELOG.md +227 -0
  2. package/Dockerfile +130 -9
  3. package/README.md +149 -50
  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-EXQOIRZI.js +2351 -0
  9. package/dist/chunk-EXQOIRZI.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 +14 -8
  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/clients/interpreter-client.ts +329 -0
  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 +83 -119
  53. package/src/interpreter.ts +159 -0
  54. package/src/request-handler.ts +170 -0
  55. package/src/sandbox.ts +916 -0
  56. package/src/security.ts +104 -0
  57. package/src/sse-parser.ts +143 -0
  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/index.ts +0 -2900
  73. package/container_src/package.json +0 -9
  74. package/src/client.ts +0 -1929
  75. package/tests/client.example.ts +0 -308
  76. package/tests/connection-test.ts +0 -81
  77. package/tests/simple-test.ts +0 -81
  78. package/tests/test1.ts +0 -281
  79. package/tests/test2.ts +0 -929
@@ -0,0 +1,164 @@
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(stream: ReadableStream<Uint8Array>): AsyncGenerator<FileStreamEvent> {
7
+ const reader = stream.getReader();
8
+ const decoder = new TextDecoder();
9
+ let buffer = '';
10
+
11
+ try {
12
+ while (true) {
13
+ const { done, value } = await reader.read();
14
+
15
+ if (done) {
16
+ break;
17
+ }
18
+
19
+ buffer += decoder.decode(value, { stream: true });
20
+ const lines = buffer.split('\n');
21
+
22
+ // Keep the last incomplete line in the buffer
23
+ buffer = lines.pop() || '';
24
+
25
+ for (const line of lines) {
26
+ if (line.startsWith('data: ')) {
27
+ const data = line.slice(6); // Remove 'data: ' prefix
28
+ try {
29
+ const event = JSON.parse(data) as FileStreamEvent;
30
+ yield event;
31
+ } catch {
32
+ // Skip invalid JSON events and continue processing
33
+ }
34
+ }
35
+ }
36
+ }
37
+ } finally {
38
+ reader.releaseLock();
39
+ }
40
+ }
41
+
42
+ /**
43
+ * Stream a file from the sandbox with automatic base64 decoding for binary files
44
+ *
45
+ * @param stream - The ReadableStream from readFileStream()
46
+ * @returns AsyncGenerator that yields FileChunk (string for text, Uint8Array for binary)
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * const stream = await sandbox.readFileStream('/path/to/file.png');
51
+ * for await (const chunk of streamFile(stream)) {
52
+ * if (chunk instanceof Uint8Array) {
53
+ * // Binary chunk
54
+ * console.log('Binary chunk:', chunk.length, 'bytes');
55
+ * } else {
56
+ * // Text chunk
57
+ * console.log('Text chunk:', chunk);
58
+ * }
59
+ * }
60
+ * ```
61
+ */
62
+ export async function* streamFile(stream: ReadableStream<Uint8Array>): AsyncGenerator<FileChunk, FileMetadata> {
63
+ let metadata: FileMetadata | null = null;
64
+
65
+ for await (const event of parseSSE(stream)) {
66
+ switch (event.type) {
67
+ case 'metadata':
68
+ metadata = {
69
+ mimeType: event.mimeType,
70
+ size: event.size,
71
+ isBinary: event.isBinary,
72
+ encoding: event.encoding,
73
+ };
74
+ break;
75
+
76
+ case 'chunk':
77
+ if (!metadata) {
78
+ throw new Error('Received chunk before metadata');
79
+ }
80
+
81
+ if (metadata.isBinary && metadata.encoding === 'base64') {
82
+ // Decode base64 to Uint8Array for binary files
83
+ const binaryString = atob(event.data);
84
+ const bytes = new Uint8Array(binaryString.length);
85
+ for (let i = 0; i < binaryString.length; i++) {
86
+ bytes[i] = binaryString.charCodeAt(i);
87
+ }
88
+ yield bytes;
89
+ } else {
90
+ // Text files - yield as-is
91
+ yield event.data;
92
+ }
93
+ break;
94
+
95
+ case 'complete':
96
+ if (!metadata) {
97
+ throw new Error('Stream completed without metadata');
98
+ }
99
+ return metadata;
100
+
101
+ case 'error':
102
+ throw new Error(`File streaming error: ${event.error}`);
103
+ }
104
+ }
105
+
106
+ throw new Error('Stream ended unexpectedly');
107
+ }
108
+
109
+ /**
110
+ * Collect an entire file into memory from a stream
111
+ *
112
+ * @param stream - The ReadableStream from readFileStream()
113
+ * @returns Object containing the file content and metadata
114
+ *
115
+ * @example
116
+ * ```ts
117
+ * const stream = await sandbox.readFileStream('/path/to/file.txt');
118
+ * const { content, metadata } = await collectFile(stream);
119
+ * console.log('Content:', content);
120
+ * console.log('MIME type:', metadata.mimeType);
121
+ * ```
122
+ */
123
+ export async function collectFile(stream: ReadableStream<Uint8Array>): Promise<{
124
+ content: string | Uint8Array;
125
+ metadata: FileMetadata;
126
+ }> {
127
+ const chunks: Array<string | Uint8Array> = [];
128
+
129
+ // Iterate through the generator and get the return value (metadata)
130
+ const generator = streamFile(stream);
131
+ let result = await generator.next();
132
+
133
+ while (!result.done) {
134
+ chunks.push(result.value);
135
+ result = await generator.next();
136
+ }
137
+
138
+ const metadata = result.value;
139
+
140
+ if (!metadata) {
141
+ throw new Error('Failed to get file metadata');
142
+ }
143
+
144
+ // Combine chunks based on type
145
+ if (metadata.isBinary) {
146
+ // Binary file - combine Uint8Arrays
147
+ const totalLength = chunks.reduce((sum, chunk) =>
148
+ sum + (chunk instanceof Uint8Array ? chunk.length : 0), 0
149
+ );
150
+ const combined = new Uint8Array(totalLength);
151
+ let offset = 0;
152
+ for (const chunk of chunks) {
153
+ if (chunk instanceof Uint8Array) {
154
+ combined.set(chunk, offset);
155
+ offset += chunk.length;
156
+ }
157
+ }
158
+ return { content: combined, metadata };
159
+ } else {
160
+ // Text file - combine strings
161
+ const combined = chunks.filter(c => typeof c === 'string').join('');
162
+ return { content: combined, metadata };
163
+ }
164
+ }
package/src/index.ts CHANGED
@@ -1,129 +1,93 @@
1
- import { Container, getContainer } from "@cloudflare/containers";
2
- import { HttpClient } from "./client";
1
+ // Export the main Sandbox class and utilities
3
2
 
4
- export function getSandbox(ns: DurableObjectNamespace<Sandbox>, id: string) {
5
- return getContainer(ns, id);
6
- }
7
3
 
8
- export class Sandbox<Env = unknown> extends Container<Env> {
9
- defaultPort = 3000; // The default port for the container to listen on
10
- sleepAfter = "3m"; // Sleep the sandbox if no requests are made in this timeframe
4
+ // Export the new client architecture
5
+ export {
6
+ CommandClient,
7
+ FileClient,
8
+ GitClient,
9
+ PortClient,
10
+ ProcessClient,
11
+ SandboxClient,
12
+ UtilityClient
13
+ } from "./clients";
14
+ export { getSandbox, Sandbox } from "./sandbox";
11
15
 
12
- client: HttpClient = new HttpClient({
13
- onCommandComplete: (success, exitCode, stdout, stderr, command, args) => {
14
- console.log(
15
- `[Container] Command completed: ${command}, Success: ${success}, Exit code: ${exitCode}`
16
- );
17
- },
18
- onCommandStart: (command, args) => {
19
- console.log(`[Container] Command started: ${command} ${args.join(" ")}`);
20
- },
21
- onError: (error, command, args) => {
22
- console.error(`[Container] Command error: ${error}`);
23
- },
24
- onOutput: (stream, data, command) => {
25
- console.log(`[Container] [${stream}] ${data}`);
26
- },
27
- port: this.defaultPort,
28
- });
16
+ // Legacy types are now imported from the new client architecture
29
17
 
30
- envVars = {
31
- MESSAGE: "I was passed in via the Sandbox class!",
32
- };
18
+ // Export core SDK types for consumers
19
+ export type {
20
+ BaseExecOptions,
21
+ ExecEvent,
22
+ ExecOptions,
23
+ ExecResult,FileChunk, FileMetadata, FileStreamEvent,
24
+ ISandbox,
25
+ LogEvent,
26
+ Process,
27
+ ProcessOptions,
28
+ ProcessStatus,
29
+ StreamOptions
30
+ } from "@repo/shared";
31
+ export * from '@repo/shared';
32
+ // Export type guards for runtime validation
33
+ export {
34
+ isExecResult,
35
+ isProcess,
36
+ isProcessStatus
37
+ } from "@repo/shared";
38
+ // Export all client types from new architecture
39
+ export type {
40
+ BaseApiResponse,
41
+ CommandsResponse,
42
+ ContainerStub,
43
+ ErrorResponse,
33
44
 
34
- override onStart() {
35
- console.log("Sandbox successfully started");
36
- }
45
+ // Command client types
46
+ ExecuteRequest,
47
+ ExecuteResponse as CommandExecuteResponse,
37
48
 
38
- override onStop() {
39
- console.log("Sandbox successfully shut down");
40
- if (this.client) {
41
- this.client.clearSession();
42
- }
43
- }
49
+ // Port client types
50
+ ExposePortRequest,
51
+ FileOperationRequest,
44
52
 
45
- override onError(error: unknown) {
46
- console.log("Sandbox error:", error);
47
- }
53
+ // Git client types
54
+ GitCheckoutRequest,
55
+ GitCheckoutResult,
56
+ // Base client types
57
+ HttpClientOptions as SandboxClientOptions,
48
58
 
49
- async exec(command: string, args: string[], options?: { stream?: boolean }) {
50
- if (options?.stream) {
51
- return this.client.executeStream(command, args);
52
- }
53
- return this.client.execute(command, args);
54
- }
59
+ // File client types
60
+ MkdirRequest,
55
61
 
56
- async gitCheckout(
57
- repoUrl: string,
58
- options: { branch?: string; targetDir?: string; stream?: boolean }
59
- ) {
60
- if (options?.stream) {
61
- return this.client.gitCheckoutStream(
62
- repoUrl,
63
- options.branch,
64
- options.targetDir
65
- );
66
- }
67
- return this.client.gitCheckout(repoUrl, options.branch, options.targetDir);
68
- }
62
+ // Utility client types
63
+ PingResponse,
64
+ PortCloseResult,
65
+ PortExposeResult,
66
+ PortListResult,
67
+ ProcessCleanupResult,
68
+ ProcessInfoResult,
69
+ ProcessKillResult,
70
+ ProcessListResult,
71
+ ProcessLogsResult,
72
+ ProcessStartResult,
73
+ ReadFileRequest,
74
+ RequestConfig,
75
+ ResponseHandler,
76
+ SessionRequest,
69
77
 
70
- async mkdir(
71
- path: string,
72
- options: { recursive?: boolean; stream?: boolean }
73
- ) {
74
- if (options?.stream) {
75
- return this.client.mkdirStream(path, options.recursive);
76
- }
77
- return this.client.mkdir(path, options.recursive);
78
- }
79
-
80
- async writeFile(
81
- path: string,
82
- content: string,
83
- options: { encoding?: string; stream?: boolean }
84
- ) {
85
- if (options?.stream) {
86
- return this.client.writeFileStream(path, content, options.encoding);
87
- }
88
- return this.client.writeFile(path, content, options.encoding);
89
- }
90
-
91
- async deleteFile(path: string, options: { stream?: boolean }) {
92
- if (options?.stream) {
93
- return this.client.deleteFileStream(path);
94
- }
95
- return this.client.deleteFile(path);
96
- }
97
-
98
- async renameFile(
99
- oldPath: string,
100
- newPath: string,
101
- options: { stream?: boolean }
102
- ) {
103
- if (options?.stream) {
104
- return this.client.renameFileStream(oldPath, newPath);
105
- }
106
- return this.client.renameFile(oldPath, newPath);
107
- }
108
-
109
- async moveFile(
110
- sourcePath: string,
111
- destinationPath: string,
112
- options: { stream?: boolean }
113
- ) {
114
- if (options?.stream) {
115
- return this.client.moveFileStream(sourcePath, destinationPath);
116
- }
117
- return this.client.moveFile(sourcePath, destinationPath);
118
- }
119
-
120
- async readFile(
121
- path: string,
122
- options: { encoding?: string; stream?: boolean }
123
- ) {
124
- if (options?.stream) {
125
- return this.client.readFileStream(path, options.encoding);
126
- }
127
- return this.client.readFile(path, options.encoding);
128
- }
129
- }
78
+ // Process client types
79
+ StartProcessRequest,
80
+ UnexposePortRequest,
81
+ WriteFileRequest
82
+ } from "./clients";
83
+ export type { ExecutionCallbacks, InterpreterClient } from './clients/interpreter-client.js';
84
+ // Export file streaming utilities for binary file support
85
+ export { collectFile, streamFile } from './file-stream';
86
+ // Export interpreter functionality
87
+ export { CodeInterpreter } from './interpreter.js';
88
+ // Re-export request handler utilities
89
+ export {
90
+ proxyToSandbox, type RouteInfo, type SandboxEnv
91
+ } from './request-handler';
92
+ // Export SSE parser for converting ReadableStream to AsyncIterable
93
+ export { asyncIterableToSSEStream, parseSSEStream, responseToAsyncIterable } from "./sse-parser";
@@ -0,0 +1,159 @@
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).interpreter as InterpreterClient;
22
+ }
23
+
24
+ /**
25
+ * Create a new code execution context
26
+ */
27
+ async createCodeContext(
28
+ options: CreateContextOptions = {}
29
+ ): Promise<CodeContext> {
30
+ // Validate language before sending to container
31
+ validateLanguage(options.language);
32
+
33
+ const context = await this.interpreterClient.createCodeContext(options);
34
+ this.contexts.set(context.id, context);
35
+ return context;
36
+ }
37
+
38
+ /**
39
+ * Run code with optional context
40
+ */
41
+ async runCode(
42
+ code: string,
43
+ options: RunCodeOptions = {}
44
+ ): Promise<Execution> {
45
+ // Get or create context
46
+ let context = options.context;
47
+ if (!context) {
48
+ // Try to find or create a default context for the language
49
+ const language = options.language || "python";
50
+ context = await this.getOrCreateDefaultContext(language);
51
+ }
52
+
53
+ // Create execution object to collect results
54
+ const execution = new Execution(code, context);
55
+
56
+ // Stream execution
57
+ await this.interpreterClient.runCodeStream(context.id, code, options.language, {
58
+ onStdout: (output: OutputMessage) => {
59
+ execution.logs.stdout.push(output.text);
60
+ if (options.onStdout) return options.onStdout(output);
61
+ },
62
+ onStderr: (output: OutputMessage) => {
63
+ execution.logs.stderr.push(output.text);
64
+ if (options.onStderr) return options.onStderr(output);
65
+ },
66
+ onResult: async (result: Result) => {
67
+ execution.results.push(new ResultImpl(result) as any);
68
+ if (options.onResult) return options.onResult(result);
69
+ },
70
+ onError: (error: ExecutionError) => {
71
+ execution.error = error;
72
+ if (options.onError) return options.onError(error);
73
+ },
74
+ });
75
+
76
+ return execution;
77
+ }
78
+
79
+ /**
80
+ * Run code and return a streaming response
81
+ */
82
+ async runCodeStream(
83
+ code: string,
84
+ options: RunCodeOptions = {}
85
+ ): Promise<ReadableStream> {
86
+ // Get or create context
87
+ let context = options.context;
88
+ if (!context) {
89
+ const language = options.language || "python";
90
+ context = await this.getOrCreateDefaultContext(language);
91
+ }
92
+
93
+ // Create streaming response
94
+ // Note: doFetch is protected but we need direct access for raw stream response
95
+ const response = await (this.interpreterClient as any).doFetch("/api/execute/code", {
96
+ method: "POST",
97
+ headers: {
98
+ "Content-Type": "application/json",
99
+ Accept: "text/event-stream",
100
+ },
101
+ body: JSON.stringify({
102
+ context_id: context.id,
103
+ code,
104
+ language: options.language,
105
+ }),
106
+ });
107
+
108
+ if (!response.ok) {
109
+ const errorData = (await response
110
+ .json()
111
+ .catch(() => ({ error: "Unknown error" }))) as { error?: string };
112
+ throw new Error(
113
+ errorData.error || `Failed to execute code: ${response.status}`
114
+ );
115
+ }
116
+
117
+ if (!response.body) {
118
+ throw new Error("No response body for streaming execution");
119
+ }
120
+
121
+ return response.body;
122
+ }
123
+
124
+ /**
125
+ * List all code contexts
126
+ */
127
+ async listCodeContexts(): Promise<CodeContext[]> {
128
+ const contexts = await this.interpreterClient.listCodeContexts();
129
+
130
+ // Update local cache
131
+ for (const context of contexts) {
132
+ this.contexts.set(context.id, context);
133
+ }
134
+
135
+ return contexts;
136
+ }
137
+
138
+ /**
139
+ * Delete a code context
140
+ */
141
+ async deleteCodeContext(contextId: string): Promise<void> {
142
+ await this.interpreterClient.deleteCodeContext(contextId);
143
+ this.contexts.delete(contextId);
144
+ }
145
+
146
+ private async getOrCreateDefaultContext(
147
+ language: "python" | "javascript" | "typescript"
148
+ ): Promise<CodeContext> {
149
+ // Check if we have a cached context for this language
150
+ for (const context of this.contexts.values()) {
151
+ if (context.language === language) {
152
+ return context;
153
+ }
154
+ }
155
+
156
+ // Create new default context
157
+ return this.createCodeContext({ language });
158
+ }
159
+ }
@@ -0,0 +1,170 @@
1
+ import { createLogger, type LogContext, TraceContext } from "@repo/shared";
2
+ import { getSandbox, type Sandbox } from "./sandbox";
3
+ import {
4
+ sanitizeSandboxId,
5
+ validatePort
6
+ } from "./security";
7
+
8
+ export interface SandboxEnv {
9
+ Sandbox: DurableObjectNamespace<Sandbox>;
10
+ }
11
+
12
+ export interface RouteInfo {
13
+ port: number;
14
+ sandboxId: string;
15
+ path: string;
16
+ token: string;
17
+ }
18
+
19
+ export async function proxyToSandbox<E extends SandboxEnv>(
20
+ request: Request,
21
+ env: E
22
+ ): Promise<Response | null> {
23
+ // Create logger context for this request
24
+ const traceId = TraceContext.fromHeaders(request.headers) || TraceContext.generate();
25
+ const logger = createLogger({
26
+ component: 'sandbox-do',
27
+ traceId,
28
+ operation: 'proxy'
29
+ });
30
+
31
+ try {
32
+ const url = new URL(request.url);
33
+ const routeInfo = extractSandboxRoute(url);
34
+
35
+ if (!routeInfo) {
36
+ return null; // Not a request to an exposed container port
37
+ }
38
+
39
+ const { sandboxId, port, path, token } = routeInfo;
40
+ const sandbox = getSandbox(env.Sandbox, sandboxId);
41
+
42
+ // Critical security check: Validate token (mandatory for all user ports)
43
+ // Skip check for control plane port 3000
44
+ if (port !== 3000) {
45
+ // Validate the token matches the port
46
+ const isValidToken = await sandbox.validatePortToken(port, token);
47
+ if (!isValidToken) {
48
+ logger.warn('Invalid token access blocked', {
49
+ port,
50
+ sandboxId,
51
+ path,
52
+ hostname: url.hostname,
53
+ url: request.url,
54
+ method: request.method,
55
+ userAgent: request.headers.get('User-Agent') || 'unknown'
56
+ });
57
+
58
+ return new Response(
59
+ JSON.stringify({
60
+ error: `Access denied: Invalid token or port not exposed`,
61
+ code: 'INVALID_TOKEN'
62
+ }),
63
+ {
64
+ status: 404,
65
+ headers: {
66
+ 'Content-Type': 'application/json'
67
+ }
68
+ }
69
+ );
70
+ }
71
+ }
72
+
73
+ // Build proxy request with proper headers
74
+ let proxyUrl: string;
75
+
76
+ // Route based on the target port
77
+ if (port !== 3000) {
78
+ // Route directly to user's service on the specified port
79
+ proxyUrl = `http://localhost:${port}${path}${url.search}`;
80
+ } else {
81
+ // Port 3000 is our control plane - route normally
82
+ proxyUrl = `http://localhost:3000${path}${url.search}`;
83
+ }
84
+
85
+ const proxyRequest = new Request(proxyUrl, {
86
+ method: request.method,
87
+ headers: {
88
+ ...Object.fromEntries(request.headers),
89
+ 'X-Original-URL': request.url,
90
+ 'X-Forwarded-Host': url.hostname,
91
+ 'X-Forwarded-Proto': url.protocol.replace(':', ''),
92
+ 'X-Sandbox-Name': sandboxId, // Pass the friendly name
93
+ },
94
+ body: request.body,
95
+ // @ts-expect-error - duplex required for body streaming in modern runtimes
96
+ duplex: 'half',
97
+ });
98
+
99
+ return sandbox.containerFetch(proxyRequest, port);
100
+ } catch (error) {
101
+ logger.error('Proxy routing error', error instanceof Error ? error : new Error(String(error)));
102
+ return new Response('Proxy routing error', { status: 500 });
103
+ }
104
+ }
105
+
106
+ function extractSandboxRoute(url: URL): RouteInfo | null {
107
+ // Parse subdomain pattern: port-sandboxId-token.domain (tokens mandatory)
108
+ const subdomainMatch = url.hostname.match(/^(\d{4,5})-([^.-][^.]*[^.-]|[^.-])-([a-zA-Z0-9_-]{12,20})\.(.+)$/);
109
+
110
+ if (!subdomainMatch) {
111
+ return null;
112
+ }
113
+
114
+ const portStr = subdomainMatch[1];
115
+ const sandboxId = subdomainMatch[2];
116
+ const token = subdomainMatch[3]; // Mandatory token
117
+ const domain = subdomainMatch[4];
118
+
119
+ const port = parseInt(portStr, 10);
120
+ if (!validatePort(port)) {
121
+ return null;
122
+ }
123
+
124
+ let sanitizedSandboxId: string;
125
+ try {
126
+ sanitizedSandboxId = sanitizeSandboxId(sandboxId);
127
+ } catch (error) {
128
+ return null;
129
+ }
130
+
131
+ // DNS subdomain length limit is 63 characters
132
+ if (sandboxId.length > 63) {
133
+ return null;
134
+ }
135
+
136
+ return {
137
+ port,
138
+ sandboxId: sanitizedSandboxId,
139
+ path: url.pathname || "/",
140
+ token,
141
+ };
142
+ }
143
+
144
+ export function isLocalhostPattern(hostname: string): boolean {
145
+ // Handle IPv6 addresses in brackets (with or without port)
146
+ if (hostname.startsWith('[')) {
147
+ if (hostname.includes(']:')) {
148
+ // [::1]:port format
149
+ const ipv6Part = hostname.substring(0, hostname.indexOf(']:') + 1);
150
+ return ipv6Part === '[::1]';
151
+ } else {
152
+ // [::1] format without port
153
+ return hostname === '[::1]';
154
+ }
155
+ }
156
+
157
+ // Handle bare IPv6 without brackets
158
+ if (hostname === '::1') {
159
+ return true;
160
+ }
161
+
162
+ // For IPv4 and regular hostnames, split on colon to remove port
163
+ const hostPart = hostname.split(":")[0];
164
+
165
+ return (
166
+ hostPart === "localhost" ||
167
+ hostPart === "127.0.0.1" ||
168
+ hostPart === "0.0.0.0"
169
+ );
170
+ }