@larkup/sandbox 0.1.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.
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * @larkup/sandbox — Main entry point.
3
+ *
4
+ * Re-exports the public API for the sandbox package.
5
+ */
6
+ export { SandboxManager } from './sandbox-manager.js';
7
+ export { checkDockerHealth, buildSandboxImage, ensureImage, executeInDocker, } from './docker-runner.js';
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Sandbox manager — high-level API for code execution.
3
+ *
4
+ * Provides a unified interface that routes to the appropriate backend
5
+ * (Docker, E2B, etc.) based on configuration. Handles health checks,
6
+ * image setup, and execution lifecycle.
7
+ */
8
+ import type { SandboxConfig, ExecutionRequest, ExecutionResult, SandboxHealthCheck } from './types.js';
9
+ export declare class SandboxManager {
10
+ private config;
11
+ constructor(config?: Partial<SandboxConfig>);
12
+ /** Check if the sandbox backend is available and ready. */
13
+ healthCheck(): Promise<SandboxHealthCheck>;
14
+ /** Set up the sandbox backend (e.g., build Docker image). */
15
+ setup(onProgress?: (msg: string) => void): Promise<void>;
16
+ /** Execute code in the sandbox. */
17
+ execute(request: ExecutionRequest): Promise<ExecutionResult>;
18
+ }
@@ -0,0 +1,97 @@
1
+ /**
2
+ * Sandbox manager — high-level API for code execution.
3
+ *
4
+ * Provides a unified interface that routes to the appropriate backend
5
+ * (Docker, E2B, etc.) based on configuration. Handles health checks,
6
+ * image setup, and execution lifecycle.
7
+ */
8
+ import { checkDockerHealth, buildSandboxImage, ensureImage, executeInDocker, } from './docker-runner.js';
9
+ const defaultDockerConfig = {
10
+ imageName: 'larkup-sandbox:latest',
11
+ memoryMB: 512,
12
+ cpuShares: 1024,
13
+ timeoutMs: 30_000,
14
+ networkDisabled: true,
15
+ };
16
+ export class SandboxManager {
17
+ config;
18
+ constructor(config) {
19
+ this.config = {
20
+ backend: config?.backend ?? 'docker',
21
+ docker: config?.docker,
22
+ remote: config?.remote,
23
+ };
24
+ }
25
+ /** Check if the sandbox backend is available and ready. */
26
+ async healthCheck() {
27
+ switch (this.config.backend) {
28
+ case 'docker':
29
+ return checkDockerHealth();
30
+ case 'e2b':
31
+ case 'modal':
32
+ case 'custom':
33
+ return {
34
+ status: 'error',
35
+ backend: this.config.backend,
36
+ error: `${this.config.backend} backend is not yet implemented. Use Docker for local execution.`,
37
+ };
38
+ default:
39
+ return {
40
+ status: 'error',
41
+ backend: this.config.backend,
42
+ error: `Unknown backend: ${this.config.backend}`,
43
+ };
44
+ }
45
+ }
46
+ /** Set up the sandbox backend (e.g., build Docker image). */
47
+ async setup(onProgress) {
48
+ switch (this.config.backend) {
49
+ case 'docker':
50
+ await buildSandboxImage(onProgress);
51
+ break;
52
+ default:
53
+ throw new Error(`Setup not supported for backend: ${this.config.backend}`);
54
+ }
55
+ }
56
+ /** Execute code in the sandbox. */
57
+ async execute(request) {
58
+ switch (this.config.backend) {
59
+ case 'docker': {
60
+ // Ensure image is available
61
+ const imageReady = await ensureImage();
62
+ if (!imageReady) {
63
+ return {
64
+ stdout: '',
65
+ stderr: 'Sandbox image not available. Run setup first or ensure Docker is installed.',
66
+ exitCode: 1,
67
+ artifacts: [],
68
+ executionTimeMs: 0,
69
+ };
70
+ }
71
+ const dockerConfig = {
72
+ ...defaultDockerConfig,
73
+ ...this.config.docker,
74
+ };
75
+ return executeInDocker(request, dockerConfig);
76
+ }
77
+ case 'e2b':
78
+ case 'modal':
79
+ case 'custom':
80
+ return {
81
+ stdout: '',
82
+ stderr: `${this.config.backend} backend is not yet implemented.`,
83
+ exitCode: 1,
84
+ artifacts: [],
85
+ executionTimeMs: 0,
86
+ };
87
+ default:
88
+ return {
89
+ stdout: '',
90
+ stderr: `Unknown backend: ${this.config.backend}`,
91
+ exitCode: 1,
92
+ artifacts: [],
93
+ executionTimeMs: 0,
94
+ };
95
+ }
96
+ }
97
+ }
@@ -0,0 +1,71 @@
1
+ export type SandboxBackend = "docker" | "e2b" | "modal" | "custom";
2
+ export interface DockerConfig {
3
+ /** Docker image to use (default: "larkup-sandbox:latest") */
4
+ imageName: string;
5
+ /** Memory limit in MB (default: 512) */
6
+ memoryMB: number;
7
+ /** CPU shares (default: 1024 = 1 core) */
8
+ cpuShares: number;
9
+ /** Max execution time in ms (default: 30000) */
10
+ timeoutMs: number;
11
+ /** Disable network access in the container (default: true) */
12
+ networkDisabled: boolean;
13
+ }
14
+ export interface RemoteConfig {
15
+ provider: "e2b" | "modal" | "custom";
16
+ apiKey: string;
17
+ endpoint?: string;
18
+ }
19
+ export interface SandboxConfig {
20
+ backend: SandboxBackend;
21
+ docker?: Partial<DockerConfig>;
22
+ remote?: RemoteConfig;
23
+ }
24
+ export declare const DEFAULT_DOCKER_CONFIG: DockerConfig;
25
+ export type SandboxLanguage = "python" | "javascript" | "typescript";
26
+ export interface SandboxFile {
27
+ /** Filename inside the sandbox (e.g., "data.csv") */
28
+ name: string;
29
+ /** File content as a string (text) or base64 (binary) */
30
+ content: string;
31
+ /** Whether content is base64-encoded (default: false) */
32
+ isBase64?: boolean;
33
+ }
34
+ export interface ExecutionRequest {
35
+ /** Code to execute */
36
+ code: string;
37
+ /** Programming language */
38
+ language: SandboxLanguage;
39
+ /** Files to mount into the sandbox working directory */
40
+ files?: SandboxFile[];
41
+ /** Max execution time in ms (overrides config default) */
42
+ timeout?: number;
43
+ }
44
+ export interface ExecutionArtifact {
45
+ /** Filename of the generated artifact */
46
+ name: string;
47
+ /** MIME type (e.g., "image/png", "text/csv") */
48
+ mimeType: string;
49
+ /** Content as base64-encoded string */
50
+ data: string;
51
+ }
52
+ export interface ExecutionResult {
53
+ /** Standard output from the execution */
54
+ stdout: string;
55
+ /** Standard error output */
56
+ stderr: string;
57
+ /** Process exit code (0 = success) */
58
+ exitCode: number;
59
+ /** Generated files (charts, CSVs, etc.) */
60
+ artifacts: ExecutionArtifact[];
61
+ /** Wall-clock execution time in ms */
62
+ executionTimeMs: number;
63
+ }
64
+ export type SandboxStatus = "ready" | "docker-not-found" | "image-not-found" | "building-image" | "error";
65
+ export interface SandboxHealthCheck {
66
+ status: SandboxStatus;
67
+ backend: SandboxBackend;
68
+ dockerVersion?: string;
69
+ imageReady?: boolean;
70
+ error?: string;
71
+ }
package/dist/types.js ADDED
@@ -0,0 +1,7 @@
1
+ export const DEFAULT_DOCKER_CONFIG = {
2
+ imageName: "larkup-sandbox:latest",
3
+ memoryMB: 512,
4
+ cpuShares: 1024,
5
+ timeoutMs: 30_000,
6
+ networkDisabled: true,
7
+ };
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@larkup/sandbox",
3
+ "version": "0.1.1",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "type": "module",
8
+ "main": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./dist/index.d.ts",
13
+ "import": "./dist/index.js",
14
+ "default": "./dist/index.js"
15
+ },
16
+ "./types": {
17
+ "types": "./dist/types.d.ts",
18
+ "import": "./dist/types.js",
19
+ "default": "./dist/types.js"
20
+ },
21
+ "./docker-runner": {
22
+ "types": "./dist/docker-runner.d.ts",
23
+ "import": "./dist/docker-runner.js",
24
+ "default": "./dist/docker-runner.js"
25
+ },
26
+ "./sandbox-manager": {
27
+ "types": "./dist/sandbox-manager.d.ts",
28
+ "import": "./dist/sandbox-manager.js",
29
+ "default": "./dist/sandbox-manager.js"
30
+ }
31
+ },
32
+ "dependencies": {
33
+ "dockerode": "^4.0.7"
34
+ },
35
+ "devDependencies": {
36
+ "@types/dockerode": "^3.3.38",
37
+ "typescript": "5.7.3"
38
+ },
39
+ "scripts": {
40
+ "build": "tsc --outDir dist",
41
+ "type-check": "tsc --noEmit"
42
+ }
43
+ }