@cloudflare/sandbox 0.2.0 → 0.2.1

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 (51) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/Dockerfile +31 -7
  3. package/README.md +226 -2
  4. package/container_src/bun.lock +122 -0
  5. package/container_src/index.ts +171 -1
  6. package/container_src/jupyter-server.ts +336 -0
  7. package/container_src/mime-processor.ts +255 -0
  8. package/container_src/package.json +9 -0
  9. package/container_src/startup.sh +52 -0
  10. package/dist/{chunk-YVZ3K26G.js → chunk-CUHYLCMT.js} +9 -21
  11. package/dist/chunk-CUHYLCMT.js.map +1 -0
  12. package/dist/chunk-EGC5IYXA.js +108 -0
  13. package/dist/chunk-EGC5IYXA.js.map +1 -0
  14. package/dist/chunk-FKBV7CZS.js +113 -0
  15. package/dist/chunk-FKBV7CZS.js.map +1 -0
  16. package/dist/{chunk-ZJN2PQOS.js → chunk-IATLC32Y.js} +173 -74
  17. package/dist/chunk-IATLC32Y.js.map +1 -0
  18. package/dist/{chunk-6THNBO4S.js → chunk-S5FFBU4Y.js} +1 -1
  19. package/dist/{chunk-6THNBO4S.js.map → chunk-S5FFBU4Y.js.map} +1 -1
  20. package/dist/chunk-SYMWNYWA.js +185 -0
  21. package/dist/chunk-SYMWNYWA.js.map +1 -0
  22. package/dist/{client-BXYlxy-j.d.ts → client-C7rKCYBD.d.ts} +42 -4
  23. package/dist/client.d.ts +2 -1
  24. package/dist/client.js +1 -1
  25. package/dist/index.d.ts +2 -1
  26. package/dist/index.js +10 -4
  27. package/dist/interpreter-types.d.ts +259 -0
  28. package/dist/interpreter-types.js +9 -0
  29. package/dist/interpreter-types.js.map +1 -0
  30. package/dist/interpreter.d.ts +33 -0
  31. package/dist/interpreter.js +8 -0
  32. package/dist/interpreter.js.map +1 -0
  33. package/dist/jupyter-client.d.ts +4 -0
  34. package/dist/jupyter-client.js +8 -0
  35. package/dist/jupyter-client.js.map +1 -0
  36. package/dist/request-handler.d.ts +2 -1
  37. package/dist/request-handler.js +7 -3
  38. package/dist/sandbox.d.ts +2 -1
  39. package/dist/sandbox.js +7 -3
  40. package/dist/types.d.ts +8 -0
  41. package/dist/types.js +1 -1
  42. package/package.json +1 -1
  43. package/src/client.ts +37 -54
  44. package/src/index.ts +13 -4
  45. package/src/interpreter-types.ts +383 -0
  46. package/src/interpreter.ts +150 -0
  47. package/src/jupyter-client.ts +266 -0
  48. package/src/sandbox.ts +281 -153
  49. package/src/types.ts +15 -0
  50. package/dist/chunk-YVZ3K26G.js.map +0 -1
  51. package/dist/chunk-ZJN2PQOS.js.map +0 -1
@@ -1,5 +1,6 @@
1
- import { S as Sandbox } from './client-BXYlxy-j.js';
1
+ import { S as Sandbox } from './client-C7rKCYBD.js';
2
2
  import '@cloudflare/containers';
3
+ import './interpreter-types.js';
3
4
  import './types.js';
4
5
 
5
6
  interface SandboxEnv {
@@ -1,10 +1,14 @@
1
1
  import {
2
2
  isLocalhostPattern,
3
3
  proxyToSandbox
4
- } from "./chunk-ZJN2PQOS.js";
5
- import "./chunk-YVZ3K26G.js";
4
+ } from "./chunk-IATLC32Y.js";
5
+ import "./chunk-NNGBXDMY.js";
6
+ import "./chunk-S5FFBU4Y.js";
7
+ import "./chunk-FKBV7CZS.js";
8
+ import "./chunk-EGC5IYXA.js";
9
+ import "./chunk-SYMWNYWA.js";
10
+ import "./chunk-CUHYLCMT.js";
6
11
  import "./chunk-6UAWTJ5S.js";
7
- import "./chunk-6THNBO4S.js";
8
12
  export {
9
13
  isLocalhostPattern,
10
14
  proxyToSandbox
package/dist/sandbox.d.ts CHANGED
@@ -1,3 +1,4 @@
1
- export { S as Sandbox, g as getSandbox } from './client-BXYlxy-j.js';
1
+ export { S as Sandbox, g as getSandbox } from './client-C7rKCYBD.js';
2
2
  import '@cloudflare/containers';
3
+ import './interpreter-types.js';
3
4
  import './types.js';
package/dist/sandbox.js CHANGED
@@ -1,10 +1,14 @@
1
1
  import {
2
2
  Sandbox,
3
3
  getSandbox
4
- } from "./chunk-ZJN2PQOS.js";
5
- import "./chunk-YVZ3K26G.js";
4
+ } from "./chunk-IATLC32Y.js";
5
+ import "./chunk-NNGBXDMY.js";
6
+ import "./chunk-S5FFBU4Y.js";
7
+ import "./chunk-FKBV7CZS.js";
8
+ import "./chunk-EGC5IYXA.js";
9
+ import "./chunk-SYMWNYWA.js";
10
+ import "./chunk-CUHYLCMT.js";
6
11
  import "./chunk-6UAWTJ5S.js";
7
- import "./chunk-6THNBO4S.js";
8
12
  export {
9
13
  Sandbox,
10
14
  getSandbox
package/dist/types.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { CreateContextOptions, CodeContext, RunCodeOptions, ExecutionResult } from './interpreter-types.js';
2
+
1
3
  interface BaseExecOptions {
2
4
  /**
3
5
  * Session ID for grouping related commands
@@ -260,6 +262,7 @@ interface GetProcessLogsResponse {
260
262
  stderr: string;
261
263
  processId: string;
262
264
  }
265
+
263
266
  interface ISandbox {
264
267
  exec(command: string, options?: ExecOptions): Promise<ExecResult>;
265
268
  startProcess(command: string, options?: ProcessOptions): Promise<Process>;
@@ -276,6 +279,11 @@ interface ISandbox {
276
279
  stdout: string;
277
280
  stderr: string;
278
281
  }>;
282
+ createCodeContext(options?: CreateContextOptions): Promise<CodeContext>;
283
+ runCode(code: string, options?: RunCodeOptions): Promise<ExecutionResult>;
284
+ runCodeStream(code: string, options?: RunCodeOptions): Promise<ReadableStream>;
285
+ listCodeContexts(): Promise<CodeContext[]>;
286
+ deleteCodeContext(contextId: string): Promise<void>;
279
287
  }
280
288
  declare function isExecResult(value: any): value is ExecResult;
281
289
  declare function isProcess(value: any): value is Process;
package/dist/types.js CHANGED
@@ -6,7 +6,7 @@ import {
6
6
  isExecResult,
7
7
  isProcess,
8
8
  isProcessStatus
9
- } from "./chunk-6THNBO4S.js";
9
+ } from "./chunk-S5FFBU4Y.js";
10
10
  export {
11
11
  ExecutionTimeoutError,
12
12
  ProcessAlreadyExistsError,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudflare/sandbox",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/cloudflare/sandbox-sdk"
package/src/client.ts CHANGED
@@ -6,7 +6,7 @@ import type {
6
6
  GetProcessResponse,
7
7
  ListProcessesResponse,
8
8
  StartProcessRequest,
9
- StartProcessResponse
9
+ StartProcessResponse,
10
10
  } from "./types";
11
11
 
12
12
  export interface ExecuteResponse {
@@ -18,22 +18,6 @@ export interface ExecuteResponse {
18
18
  timestamp: string;
19
19
  }
20
20
 
21
- interface SessionResponse {
22
- sessionId: string;
23
- message: string;
24
- timestamp: string;
25
- }
26
-
27
- interface SessionListResponse {
28
- sessions: Array<{
29
- sessionId: string;
30
- hasActiveProcess: boolean;
31
- createdAt: string;
32
- }>;
33
- count: number;
34
- timestamp: string;
35
- }
36
-
37
21
  interface CommandsResponse {
38
22
  availableCommands: string[];
39
23
  timestamp: string;
@@ -209,7 +193,7 @@ export class HttpClient {
209
193
  this.baseUrl = this.options.baseUrl!;
210
194
  }
211
195
 
212
- private async doFetch(
196
+ protected async doFetch(
213
197
  path: string,
214
198
  options?: RequestInit
215
199
  ): Promise<Response> {
@@ -252,7 +236,7 @@ export class HttpClient {
252
236
 
253
237
  async execute(
254
238
  command: string,
255
- options: Pick<BaseExecOptions, 'sessionId' | 'cwd' | 'env'>
239
+ options: Pick<BaseExecOptions, "sessionId" | "cwd" | "env">
256
240
  ): Promise<ExecuteResponse> {
257
241
  try {
258
242
  const targetSessionId = options.sessionId || this.sessionId;
@@ -305,7 +289,6 @@ export class HttpClient {
305
289
  }
306
290
  }
307
291
 
308
-
309
292
  async executeCommandStream(
310
293
  command: string,
311
294
  sessionId?: string
@@ -320,7 +303,7 @@ export class HttpClient {
320
303
  }),
321
304
  headers: {
322
305
  "Content-Type": "application/json",
323
- "Accept": "text/event-stream",
306
+ Accept: "text/event-stream",
324
307
  },
325
308
  method: "POST",
326
309
  });
@@ -338,9 +321,7 @@ export class HttpClient {
338
321
  throw new Error("No response body for streaming request");
339
322
  }
340
323
 
341
- console.log(
342
- `[HTTP Client] Started command stream: ${command}`
343
- );
324
+ console.log(`[HTTP Client] Started command stream: ${command}`);
344
325
 
345
326
  return response.body;
346
327
  } catch (error) {
@@ -392,7 +373,6 @@ export class HttpClient {
392
373
  }
393
374
  }
394
375
 
395
-
396
376
  async mkdir(
397
377
  path: string,
398
378
  recursive: boolean = false,
@@ -434,7 +414,6 @@ export class HttpClient {
434
414
  }
435
415
  }
436
416
 
437
-
438
417
  async writeFile(
439
418
  path: string,
440
419
  content: string,
@@ -478,7 +457,6 @@ export class HttpClient {
478
457
  }
479
458
  }
480
459
 
481
-
482
460
  async readFile(
483
461
  path: string,
484
462
  encoding: string = "utf-8",
@@ -520,7 +498,6 @@ export class HttpClient {
520
498
  }
521
499
  }
522
500
 
523
-
524
501
  async deleteFile(
525
502
  path: string,
526
503
  sessionId?: string
@@ -560,7 +537,6 @@ export class HttpClient {
560
537
  }
561
538
  }
562
539
 
563
-
564
540
  async renameFile(
565
541
  oldPath: string,
566
542
  newPath: string,
@@ -602,7 +578,6 @@ export class HttpClient {
602
578
  }
603
579
  }
604
580
 
605
-
606
581
  async moveFile(
607
582
  sourcePath: string,
608
583
  destinationPath: string,
@@ -644,7 +619,6 @@ export class HttpClient {
644
619
  }
645
620
  }
646
621
 
647
-
648
622
  async exposePort(port: number, name?: string): Promise<ExposePortResponse> {
649
623
  try {
650
624
  const response = await this.doFetch(`/api/expose-port`, {
@@ -670,7 +644,9 @@ export class HttpClient {
670
644
 
671
645
  const data: ExposePortResponse = await response.json();
672
646
  console.log(
673
- `[HTTP Client] Port exposed: ${port}${name ? ` (${name})` : ""}, Success: ${data.success}`
647
+ `[HTTP Client] Port exposed: ${port}${
648
+ name ? ` (${name})` : ""
649
+ }, Success: ${data.success}`
674
650
  );
675
651
 
676
652
  return data;
@@ -732,9 +708,7 @@ export class HttpClient {
732
708
  }
733
709
 
734
710
  const data: GetExposedPortsResponse = await response.json();
735
- console.log(
736
- `[HTTP Client] Got ${data.count} exposed ports`
737
- );
711
+ console.log(`[HTTP Client] Got ${data.count} exposed ports`);
738
712
 
739
713
  return data;
740
714
  } catch (error) {
@@ -871,9 +845,7 @@ export class HttpClient {
871
845
  }
872
846
 
873
847
  const data: ListProcessesResponse = await response.json();
874
- console.log(
875
- `[HTTP Client] Listed ${data.processes.length} processes`
876
- );
848
+ console.log(`[HTTP Client] Listed ${data.processes.length} processes`);
877
849
 
878
850
  return data;
879
851
  } catch (error) {
@@ -902,7 +874,9 @@ export class HttpClient {
902
874
 
903
875
  const data: GetProcessResponse = await response.json();
904
876
  console.log(
905
- `[HTTP Client] Got process ${processId}: ${data.process?.status || 'not found'}`
877
+ `[HTTP Client] Got process ${processId}: ${
878
+ data.process?.status || "not found"
879
+ }`
906
880
  );
907
881
 
908
882
  return data;
@@ -912,7 +886,9 @@ export class HttpClient {
912
886
  }
913
887
  }
914
888
 
915
- async killProcess(processId: string): Promise<{ success: boolean; message: string }> {
889
+ async killProcess(
890
+ processId: string
891
+ ): Promise<{ success: boolean; message: string }> {
916
892
  try {
917
893
  const response = await this.doFetch(`/api/process/${processId}`, {
918
894
  headers: {
@@ -930,10 +906,11 @@ export class HttpClient {
930
906
  );
931
907
  }
932
908
 
933
- const data = await response.json() as { success: boolean; message: string };
934
- console.log(
935
- `[HTTP Client] Killed process ${processId}`
936
- );
909
+ const data = (await response.json()) as {
910
+ success: boolean;
911
+ message: string;
912
+ };
913
+ console.log(`[HTTP Client] Killed process ${processId}`);
937
914
 
938
915
  return data;
939
916
  } catch (error) {
@@ -942,7 +919,11 @@ export class HttpClient {
942
919
  }
943
920
  }
944
921
 
945
- async killAllProcesses(): Promise<{ success: boolean; killedCount: number; message: string }> {
922
+ async killAllProcesses(): Promise<{
923
+ success: boolean;
924
+ killedCount: number;
925
+ message: string;
926
+ }> {
946
927
  try {
947
928
  const response = await this.doFetch("/api/process/kill-all", {
948
929
  headers: {
@@ -960,10 +941,12 @@ export class HttpClient {
960
941
  );
961
942
  }
962
943
 
963
- const data = await response.json() as { success: boolean; killedCount: number; message: string };
964
- console.log(
965
- `[HTTP Client] Killed ${data.killedCount} processes`
966
- );
944
+ const data = (await response.json()) as {
945
+ success: boolean;
946
+ killedCount: number;
947
+ message: string;
948
+ };
949
+ console.log(`[HTTP Client] Killed ${data.killedCount} processes`);
967
950
 
968
951
  return data;
969
952
  } catch (error) {
@@ -991,9 +974,7 @@ export class HttpClient {
991
974
  }
992
975
 
993
976
  const data: GetProcessLogsResponse = await response.json();
994
- console.log(
995
- `[HTTP Client] Got logs for process ${processId}`
996
- );
977
+ console.log(`[HTTP Client] Got logs for process ${processId}`);
997
978
 
998
979
  return data;
999
980
  } catch (error) {
@@ -1002,11 +983,13 @@ export class HttpClient {
1002
983
  }
1003
984
  }
1004
985
 
1005
- async streamProcessLogs(processId: string): Promise<ReadableStream<Uint8Array>> {
986
+ async streamProcessLogs(
987
+ processId: string
988
+ ): Promise<ReadableStream<Uint8Array>> {
1006
989
  try {
1007
990
  const response = await this.doFetch(`/api/process/${processId}/stream`, {
1008
991
  headers: {
1009
- "Accept": "text/event-stream",
992
+ Accept: "text/event-stream",
1010
993
  "Cache-Control": "no-cache",
1011
994
  },
1012
995
  method: "GET",
package/src/index.ts CHANGED
@@ -5,16 +5,25 @@ export type {
5
5
  MkdirResponse, MoveFileResponse,
6
6
  ReadFileResponse, RenameFileResponse, WriteFileResponse
7
7
  } from "./client";
8
-
8
+ // Export code interpreter types
9
+ export type {
10
+ ChartData,
11
+ CodeContext,
12
+ CreateContextOptions,
13
+ Execution,
14
+ ExecutionError,
15
+ OutputMessage,
16
+ Result,
17
+ RunCodeOptions
18
+ } from "./interpreter-types";
19
+ // Export the implementations
20
+ export { ResultImpl } from "./interpreter-types";
9
21
  // Re-export request handler utilities
10
22
  export {
11
23
  proxyToSandbox, type RouteInfo, type SandboxEnv
12
24
  } from './request-handler';
13
-
14
25
  export { getSandbox, Sandbox } from "./sandbox";
15
-
16
26
  // Export SSE parser for converting ReadableStream to AsyncIterable
17
27
  export { asyncIterableToSSEStream, parseSSEStream, responseToAsyncIterable } from "./sse-parser";
18
-
19
28
  // Export event types for streaming
20
29
  export type { ExecEvent, LogEvent } from "./types";