@cloudflare/sandbox 0.4.1 → 0.4.3

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.
package/dist/index.js CHANGED
@@ -13,36 +13,46 @@ import {
13
13
  UtilityClient,
14
14
  getSandbox,
15
15
  proxyToSandbox
16
- } from "./chunk-HGF554LH.js";
16
+ } from "./chunk-EXQOIRZI.js";
17
17
  import {
18
- CodeInterpreter
19
- } from "./chunk-BCJ7SF3Q.js";
18
+ CodeInterpreter,
19
+ Execution,
20
+ LogLevel,
21
+ ResultImpl,
22
+ TraceContext,
23
+ createLogger,
24
+ createNoOpLogger,
25
+ getLogger,
26
+ isExecResult,
27
+ isProcess,
28
+ isProcessStatus,
29
+ runWithLogger
30
+ } from "./chunk-JXZMAU2C.js";
20
31
  import "./chunk-Z532A7QC.js";
21
32
  import {
22
33
  asyncIterableToSSEStream,
23
34
  parseSSEStream,
24
35
  responseToAsyncIterable
25
36
  } from "./chunk-EKSWCBCA.js";
26
-
27
- // src/index.ts
28
- export * from "@repo/shared";
29
- import {
30
- isExecResult,
31
- isProcess,
32
- isProcessStatus
33
- } from "@repo/shared";
34
37
  export {
35
38
  CodeInterpreter,
36
39
  CommandClient,
40
+ Execution,
37
41
  FileClient,
38
42
  GitClient,
43
+ LogLevel as LogLevelEnum,
39
44
  PortClient,
40
45
  ProcessClient,
46
+ ResultImpl,
41
47
  Sandbox,
42
48
  SandboxClient,
49
+ TraceContext,
43
50
  UtilityClient,
44
51
  asyncIterableToSSEStream,
45
52
  collectFile,
53
+ createLogger,
54
+ createNoOpLogger,
55
+ getLogger,
46
56
  getSandbox,
47
57
  isExecResult,
48
58
  isProcess,
@@ -50,6 +60,7 @@ export {
50
60
  parseSSEStream,
51
61
  proxyToSandbox,
52
62
  responseToAsyncIterable,
63
+ runWithLogger,
53
64
  streamFile
54
65
  };
55
66
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["// Export the main Sandbox class and utilities\n\n\n// Export the new client architecture\nexport {\n CommandClient,\n FileClient,\n GitClient,\n PortClient,\n ProcessClient,\n SandboxClient,\n UtilityClient\n} from \"./clients\";\nexport { getSandbox, Sandbox } from \"./sandbox\";\n\n// Legacy types are now imported from the new client architecture\n\n// Export core SDK types for consumers\nexport type {\n BaseExecOptions,\n ExecEvent,\n ExecOptions,\n ExecResult,FileChunk, FileMetadata, FileStreamEvent, \n ISandbox,\n LogEvent,\n Process,\n ProcessOptions,\n ProcessStatus,\n StreamOptions \n} from \"@repo/shared\";\nexport * from '@repo/shared';\n// Export type guards for runtime validation\nexport {\n isExecResult,\n isProcess,\n isProcessStatus\n} from \"@repo/shared\";\n// Export all client types from new architecture\nexport type {\n BaseApiResponse,\n CommandsResponse,\n ContainerStub,\n ErrorResponse,\n\n // Command client types\n ExecuteRequest,\n ExecuteResponse as CommandExecuteResponse,\n\n // Port client types\n ExposePortRequest,\n FileOperationRequest,\n\n // Git client types\n GitCheckoutRequest,\n GitCheckoutResult,\n // Base client types\n HttpClientOptions as SandboxClientOptions,\n\n // File client types\n MkdirRequest,\n\n // Utility client types\n PingResponse,\n PortCloseResult,\n PortExposeResult,\n PortListResult,\n ProcessCleanupResult,\n ProcessInfoResult,\n ProcessKillResult,\n ProcessListResult,\n ProcessLogsResult,\n ProcessStartResult,\n ReadFileRequest,\n RequestConfig,\n ResponseHandler,\n SessionRequest,\n\n // Process client types\n StartProcessRequest,\n UnexposePortRequest,\n WriteFileRequest\n} from \"./clients\";\nexport type { ExecutionCallbacks, InterpreterClient } from './clients/interpreter-client.js';\n// Export file streaming utilities for binary file support\nexport { collectFile, streamFile } from './file-stream';\n// Export interpreter functionality\nexport { CodeInterpreter } from './interpreter.js';\n// Re-export request handler utilities\nexport {\n proxyToSandbox, type RouteInfo, type SandboxEnv\n} from './request-handler';\n// Export SSE parser for converting ReadableStream to AsyncIterable\nexport { asyncIterableToSSEStream, parseSSEStream, responseToAsyncIterable } from \"./sse-parser\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA8BA,cAAc;AAEd;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;","names":[]}
1
+ {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  CodeInterpreter
3
- } from "./chunk-BCJ7SF3Q.js";
3
+ } from "./chunk-JXZMAU2C.js";
4
4
  import "./chunk-Z532A7QC.js";
5
5
  export {
6
6
  CodeInterpreter
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  isLocalhostPattern,
3
3
  proxyToSandbox
4
- } from "./chunk-HGF554LH.js";
5
- import "./chunk-BCJ7SF3Q.js";
4
+ } from "./chunk-EXQOIRZI.js";
5
+ import "./chunk-JXZMAU2C.js";
6
6
  import "./chunk-Z532A7QC.js";
7
7
  import "./chunk-EKSWCBCA.js";
8
8
  export {
package/dist/sandbox.js CHANGED
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  Sandbox,
3
3
  getSandbox
4
- } from "./chunk-HGF554LH.js";
5
- import "./chunk-BCJ7SF3Q.js";
4
+ } from "./chunk-EXQOIRZI.js";
5
+ import "./chunk-JXZMAU2C.js";
6
6
  import "./chunk-Z532A7QC.js";
7
7
  import "./chunk-EKSWCBCA.js";
8
8
  export {
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "@cloudflare/sandbox",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/cloudflare/sandbox-sdk"
7
7
  },
8
8
  "description": "A sandboxed environment for running commands",
9
9
  "dependencies": {
10
- "@cloudflare/containers": "^0.0.28",
10
+ "@cloudflare/containers": "^0.0.28"
11
+ },
12
+ "devDependencies": {
11
13
  "@repo/shared": "^0.0.0"
12
14
  },
13
15
  "tags": [
package/src/sandbox.ts CHANGED
@@ -110,7 +110,6 @@ export class Sandbox<Env = unknown> extends Container<Env> implements ISandbox {
110
110
  if (!this.baseUrl) {
111
111
  this.baseUrl = baseUrl;
112
112
  await this.ctx.storage.put('baseUrl', baseUrl);
113
- console.log(`[Sandbox] Stored base URL: ${baseUrl}`);
114
113
  } else {
115
114
  if(this.baseUrl !== baseUrl) {
116
115
  throw new Error('Base URL already set and different from one previously provided');
@@ -1,117 +0,0 @@
1
- import {
2
- validateLanguage
3
- } from "./chunk-Z532A7QC.js";
4
-
5
- // src/interpreter.ts
6
- import {
7
- Execution,
8
- ResultImpl
9
- } from "@repo/shared";
10
- var CodeInterpreter = class {
11
- interpreterClient;
12
- contexts = /* @__PURE__ */ new Map();
13
- constructor(sandbox) {
14
- this.interpreterClient = sandbox.client.interpreter;
15
- }
16
- /**
17
- * Create a new code execution context
18
- */
19
- async createCodeContext(options = {}) {
20
- validateLanguage(options.language);
21
- const context = await this.interpreterClient.createCodeContext(options);
22
- this.contexts.set(context.id, context);
23
- return context;
24
- }
25
- /**
26
- * Run code with optional context
27
- */
28
- async runCode(code, options = {}) {
29
- let context = options.context;
30
- if (!context) {
31
- const language = options.language || "python";
32
- context = await this.getOrCreateDefaultContext(language);
33
- }
34
- const execution = new Execution(code, context);
35
- await this.interpreterClient.runCodeStream(context.id, code, options.language, {
36
- onStdout: (output) => {
37
- execution.logs.stdout.push(output.text);
38
- if (options.onStdout) return options.onStdout(output);
39
- },
40
- onStderr: (output) => {
41
- execution.logs.stderr.push(output.text);
42
- if (options.onStderr) return options.onStderr(output);
43
- },
44
- onResult: async (result) => {
45
- execution.results.push(new ResultImpl(result));
46
- if (options.onResult) return options.onResult(result);
47
- },
48
- onError: (error) => {
49
- execution.error = error;
50
- if (options.onError) return options.onError(error);
51
- }
52
- });
53
- return execution;
54
- }
55
- /**
56
- * Run code and return a streaming response
57
- */
58
- async runCodeStream(code, options = {}) {
59
- let context = options.context;
60
- if (!context) {
61
- const language = options.language || "python";
62
- context = await this.getOrCreateDefaultContext(language);
63
- }
64
- const response = await this.interpreterClient.doFetch("/api/execute/code", {
65
- method: "POST",
66
- headers: {
67
- "Content-Type": "application/json",
68
- Accept: "text/event-stream"
69
- },
70
- body: JSON.stringify({
71
- context_id: context.id,
72
- code,
73
- language: options.language
74
- })
75
- });
76
- if (!response.ok) {
77
- const errorData = await response.json().catch(() => ({ error: "Unknown error" }));
78
- throw new Error(
79
- errorData.error || `Failed to execute code: ${response.status}`
80
- );
81
- }
82
- if (!response.body) {
83
- throw new Error("No response body for streaming execution");
84
- }
85
- return response.body;
86
- }
87
- /**
88
- * List all code contexts
89
- */
90
- async listCodeContexts() {
91
- const contexts = await this.interpreterClient.listCodeContexts();
92
- for (const context of contexts) {
93
- this.contexts.set(context.id, context);
94
- }
95
- return contexts;
96
- }
97
- /**
98
- * Delete a code context
99
- */
100
- async deleteCodeContext(contextId) {
101
- await this.interpreterClient.deleteCodeContext(contextId);
102
- this.contexts.delete(contextId);
103
- }
104
- async getOrCreateDefaultContext(language) {
105
- for (const context of this.contexts.values()) {
106
- if (context.language === language) {
107
- return context;
108
- }
109
- }
110
- return this.createCodeContext({ language });
111
- }
112
- };
113
-
114
- export {
115
- CodeInterpreter
116
- };
117
- //# sourceMappingURL=chunk-BCJ7SF3Q.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/interpreter.ts"],"sourcesContent":["import {\n type CodeContext,\n type CreateContextOptions,\n Execution,\n type ExecutionError,\n type OutputMessage,\n type Result,\n ResultImpl,\n type RunCodeOptions,\n} from \"@repo/shared\";\nimport type { InterpreterClient } from \"./clients/interpreter-client.js\";\nimport type { Sandbox } from \"./sandbox.js\";\nimport { validateLanguage } from \"./security.js\";\n\nexport class CodeInterpreter {\n private interpreterClient: InterpreterClient;\n private contexts = new Map<string, CodeContext>();\n\n constructor(sandbox: Sandbox) {\n // In init-testing architecture, client is a SandboxClient with an interpreter property\n this.interpreterClient = (sandbox.client as any).interpreter as InterpreterClient;\n }\n\n /**\n * Create a new code execution context\n */\n async createCodeContext(\n options: CreateContextOptions = {}\n ): Promise<CodeContext> {\n // Validate language before sending to container\n validateLanguage(options.language);\n\n const context = await this.interpreterClient.createCodeContext(options);\n this.contexts.set(context.id, context);\n return context;\n }\n\n /**\n * Run code with optional context\n */\n async runCode(\n code: string,\n options: RunCodeOptions = {}\n ): Promise<Execution> {\n // Get or create context\n let context = options.context;\n if (!context) {\n // Try to find or create a default context for the language\n const language = options.language || \"python\";\n context = await this.getOrCreateDefaultContext(language);\n }\n\n // Create execution object to collect results\n const execution = new Execution(code, context);\n\n // Stream execution\n await this.interpreterClient.runCodeStream(context.id, code, options.language, {\n onStdout: (output: OutputMessage) => {\n execution.logs.stdout.push(output.text);\n if (options.onStdout) return options.onStdout(output);\n },\n onStderr: (output: OutputMessage) => {\n execution.logs.stderr.push(output.text);\n if (options.onStderr) return options.onStderr(output);\n },\n onResult: async (result: Result) => {\n execution.results.push(new ResultImpl(result) as any);\n if (options.onResult) return options.onResult(result);\n },\n onError: (error: ExecutionError) => {\n execution.error = error;\n if (options.onError) return options.onError(error);\n },\n });\n\n return execution;\n }\n\n /**\n * Run code and return a streaming response\n */\n async runCodeStream(\n code: string,\n options: RunCodeOptions = {}\n ): Promise<ReadableStream> {\n // Get or create context\n let context = options.context;\n if (!context) {\n const language = options.language || \"python\";\n context = await this.getOrCreateDefaultContext(language);\n }\n\n // Create streaming response\n // Note: doFetch is protected but we need direct access for raw stream response\n const response = await (this.interpreterClient as any).doFetch(\"/api/execute/code\", {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n Accept: \"text/event-stream\",\n },\n body: JSON.stringify({\n context_id: context.id,\n code,\n language: options.language,\n }),\n });\n\n if (!response.ok) {\n const errorData = (await response\n .json()\n .catch(() => ({ error: \"Unknown error\" }))) as { error?: string };\n throw new Error(\n errorData.error || `Failed to execute code: ${response.status}`\n );\n }\n\n if (!response.body) {\n throw new Error(\"No response body for streaming execution\");\n }\n\n return response.body;\n }\n\n /**\n * List all code contexts\n */\n async listCodeContexts(): Promise<CodeContext[]> {\n const contexts = await this.interpreterClient.listCodeContexts();\n\n // Update local cache\n for (const context of contexts) {\n this.contexts.set(context.id, context);\n }\n\n return contexts;\n }\n\n /**\n * Delete a code context\n */\n async deleteCodeContext(contextId: string): Promise<void> {\n await this.interpreterClient.deleteCodeContext(contextId);\n this.contexts.delete(contextId);\n }\n\n private async getOrCreateDefaultContext(\n language: \"python\" | \"javascript\" | \"typescript\"\n ): Promise<CodeContext> {\n // Check if we have a cached context for this language\n for (const context of this.contexts.values()) {\n if (context.language === language) {\n return context;\n }\n }\n\n // Create new default context\n return this.createCodeContext({ language });\n }\n}\n"],"mappings":";;;;;AAAA;AAAA,EAGE;AAAA,EAIA;AAAA,OAEK;AAKA,IAAM,kBAAN,MAAsB;AAAA,EACnB;AAAA,EACA,WAAW,oBAAI,IAAyB;AAAA,EAEhD,YAAY,SAAkB;AAE5B,SAAK,oBAAqB,QAAQ,OAAe;AAAA,EACnD;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,kBACJ,UAAgC,CAAC,GACX;AAEtB,qBAAiB,QAAQ,QAAQ;AAEjC,UAAM,UAAU,MAAM,KAAK,kBAAkB,kBAAkB,OAAO;AACtE,SAAK,SAAS,IAAI,QAAQ,IAAI,OAAO;AACrC,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA,EAKA,MAAM,QACJ,MACA,UAA0B,CAAC,GACP;AAEpB,QAAI,UAAU,QAAQ;AACtB,QAAI,CAAC,SAAS;AAEZ,YAAM,WAAW,QAAQ,YAAY;AACrC,gBAAU,MAAM,KAAK,0BAA0B,QAAQ;AAAA,IACzD;AAGA,UAAM,YAAY,IAAI,UAAU,MAAM,OAAO;AAG7C,UAAM,KAAK,kBAAkB,cAAc,QAAQ,IAAI,MAAM,QAAQ,UAAU;AAAA,MAC7E,UAAU,CAAC,WAA0B;AACnC,kBAAU,KAAK,OAAO,KAAK,OAAO,IAAI;AACtC,YAAI,QAAQ,SAAU,QAAO,QAAQ,SA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