@sandboxxjs/core 0.1.0 → 0.2.0

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.d.ts CHANGED
@@ -1,87 +1,176 @@
1
- /**
2
- * Core type definitions for SandboX
3
- */
4
- type Runtime = "bash" | "node" | "python" | "docker";
5
- type IsolatorType = "local" | "cloudflare" | "e2b" | "firecracker" | "docker";
1
+ import { WithState, StateLog } from "@sandboxxjs/state";
2
+ type IsolatorType = "local" | "cloudflare" | "e2b" | "docker";
3
+ type Runtime = "shell" | "node" | "python";
4
+ interface StateConfig {
5
+ /** Simple environment variable initialization */
6
+ env?: Record<string, string>;
7
+ /** Initialize state from StateLog */
8
+ initializeLog?: StateLog;
9
+ /** Enable state recording */
10
+ enableRecord?: boolean;
11
+ }
6
12
  interface SandboxConfig {
7
- runtime: Runtime;
13
+ /** Isolator type */
8
14
  isolator: IsolatorType;
15
+ /** Runtime type (default: shell) */
16
+ runtime?: Runtime;
17
+ /** Resource limits */
9
18
  limits?: ResourceLimits;
10
- isolation?: IsolationConfig;
19
+ /** State configuration */
20
+ state?: StateConfig;
21
+ /** Node-specific configuration */
22
+ node?: NodeConfig;
23
+ /** Python-specific configuration */
24
+ python?: PythonConfig;
11
25
  }
12
26
  interface ResourceLimits {
27
+ /** Timeout in milliseconds */
13
28
  timeout?: number;
29
+ /** Memory limit in bytes */
14
30
  memory?: number;
31
+ /** CPU limit as percentage */
15
32
  cpu?: number;
16
33
  }
17
- interface IsolationConfig {
18
- networkAccess?: boolean;
19
- fileSystemAccess?: boolean;
20
- envVars?: Record<string, string>;
34
+ interface NodeConfig {
35
+ /** Package manager */
36
+ packageManager?: "npm" | "yarn" | "pnpm" | "bun";
37
+ /** Node version */
38
+ version?: string;
21
39
  }
22
- interface ExecuteOptions {
23
- code: string;
24
- env?: Record<string, string>;
25
- timeout?: number;
40
+ interface PythonConfig {
41
+ /** Python version */
42
+ version?: string;
43
+ /** Whether to use virtual environment */
44
+ useVenv?: boolean;
45
+ }
46
+ /**
47
+ * Base Sandbox interface - 4 core APIs
48
+ */
49
+ interface Sandbox {
50
+ /** Execute shell command */
51
+ shell(command: string): Promise<ShellResult>;
52
+ /** Upload file to sandbox */
53
+ upload(data: Buffer, remotePath: string): Promise<void>;
54
+ /** Download file from sandbox */
55
+ download(remotePath: string): Promise<Buffer>;
56
+ /** Destroy sandbox */
57
+ destroy(): Promise<void>;
58
+ }
59
+ /**
60
+ * Shell execution result
61
+ */
62
+ interface ShellResult {
63
+ /** Whether execution succeeded */
64
+ success: boolean;
65
+ /** Standard output */
66
+ stdout: string;
67
+ /** Standard error */
68
+ stderr: string;
69
+ /** Exit code */
70
+ exitCode: number;
71
+ /** Execution time in milliseconds */
72
+ executionTime: number;
73
+ }
74
+ /**
75
+ * Code execution capability
76
+ */
77
+ interface WithExecute {
78
+ execute(code: string): Promise<ExecuteResult>;
26
79
  }
27
80
  interface ExecuteResult {
81
+ /** Whether execution succeeded */
28
82
  success: boolean;
29
- data?: unknown;
30
- stdout?: string;
31
- stderr?: string;
32
- error?: string;
33
- exitCode?: number;
34
- metadata: {
35
- executionTime: number
36
- timestamp: string
37
- };
38
- }
39
- interface FileSystem {
40
- write(path: string, data: string): Promise<void>;
41
- read(path: string): Promise<string>;
42
- list(path: string): Promise<string[]>;
43
- delete(path: string): Promise<void>;
44
- exists(path: string): Promise<boolean>;
45
- }
46
- type EventHandler = (...args: unknown[]) => void;
47
- declare class Sandbox {
48
- private isolator;
49
- private runtime;
50
- private eventHandlers;
51
- fs: FileSystem;
52
- constructor(config: SandboxConfig);
53
- private createIsolator;
54
- private createRuntime;
55
- execute(options: ExecuteOptions): Promise<ExecuteResult>;
56
- writeFile(filePath: string, data: string): Promise<void>;
57
- readFile(filePath: string): Promise<string>;
58
- destroy(): Promise<void>;
59
- on(event: string, handler: EventHandler): void;
60
- private emit;
83
+ /** Standard output */
84
+ stdout: string;
85
+ /** Standard error */
86
+ stderr: string;
87
+ /** Exit code */
88
+ exitCode: number;
89
+ /** Execution time in milliseconds */
90
+ executionTime: number;
91
+ }
92
+ /** Node sandbox = Base + State + Execute */
93
+ type NodeSandbox = Sandbox & WithState & WithExecute;
94
+ /** Python sandbox = Base + State + Execute */
95
+ type PythonSandbox = Sandbox & WithState & WithExecute;
96
+ /** Sandbox with State only */
97
+ type StateSandbox = Sandbox & WithState;
98
+ /** Sandbox constructor type */
99
+ type SandboxConstructor<T extends Sandbox = Sandbox> = new (config: SandboxConfig) => T;
100
+ /** Mixin function type */
101
+ type SandboxMixin<
102
+ T extends Sandbox,
103
+ U
104
+ > = (Base: SandboxConstructor<T>) => SandboxConstructor<T & U>;
105
+ interface ShellOptions {
106
+ timeout?: number;
107
+ env?: Record<string, string>;
61
108
  }
62
109
  declare abstract class Isolator {
63
- abstract execute(options: ExecuteOptions): Promise<ExecuteResult>;
64
- abstract getFileSystem(): FileSystem;
110
+ /**
111
+ * Execute shell command in isolated environment
112
+ */
113
+ abstract shell(command: string, options?: ShellOptions): Promise<ShellResult>;
114
+ /**
115
+ * Upload file to isolated environment
116
+ */
117
+ abstract upload(data: Buffer, remotePath: string): Promise<void>;
118
+ /**
119
+ * Download file from isolated environment
120
+ */
121
+ abstract download(remotePath: string): Promise<Buffer>;
122
+ /**
123
+ * Destroy isolator and cleanup resources
124
+ */
65
125
  abstract destroy(): Promise<void>;
66
126
  }
127
+ declare class BaseSandbox implements Sandbox {
128
+ protected isolator: Isolator;
129
+ protected config: SandboxConfig;
130
+ constructor(config: SandboxConfig);
131
+ private createIsolator;
132
+ /**
133
+ * Execute shell command
134
+ */
135
+ shell(command: string): Promise<ShellResult>;
136
+ /**
137
+ * Upload file to sandbox
138
+ */
139
+ upload(data: Buffer, remotePath: string): Promise<void>;
140
+ /**
141
+ * Download file from sandbox
142
+ */
143
+ download(remotePath: string): Promise<Buffer>;
144
+ /**
145
+ * Destroy sandbox and cleanup resources
146
+ */
147
+ destroy(): Promise<void>;
148
+ }
67
149
  declare class LocalIsolator extends Isolator {
68
150
  private workDir;
69
- private runtime;
70
- constructor(runtime?: string);
71
- execute(options: ExecuteOptions): Promise<ExecuteResult>;
151
+ constructor();
72
152
  /**
73
- * Build command array based on runtime
153
+ * Execute shell command
154
+ */
155
+ shell(command: string, options?: ShellOptions): Promise<ShellResult>;
156
+ /**
157
+ * Upload file to sandbox
158
+ */
159
+ upload(data: Buffer, remotePath: string): Promise<void>;
160
+ /**
161
+ * Download file from sandbox
162
+ */
163
+ download(remotePath: string): Promise<Buffer>;
164
+ /**
165
+ * Destroy isolator and cleanup
74
166
  */
75
- private buildCommand;
76
- getFileSystem(): FileSystem;
77
167
  destroy(): Promise<void>;
78
168
  }
79
169
  declare class CloudflareContainerIsolator extends Isolator {
80
170
  private serverProcess?;
81
171
  private serverUrl?;
82
172
  private isReady;
83
- private runtime;
84
- constructor(runtime?: string);
173
+ constructor();
85
174
  /**
86
175
  * Find an available port
87
176
  */
@@ -98,22 +187,37 @@ declare class CloudflareContainerIsolator extends Isolator {
98
187
  * Ensure server is running
99
188
  */
100
189
  private ensureServerRunning;
101
- execute(options: ExecuteOptions): Promise<ExecuteResult>;
102
- getFileSystem(): FileSystem;
190
+ /**
191
+ * Execute shell command
192
+ */
193
+ shell(command: string, options?: ShellOptions): Promise<ShellResult>;
194
+ /**
195
+ * Upload file to sandbox
196
+ */
197
+ upload(_data: Buffer, _remotePath: string): Promise<void>;
198
+ /**
199
+ * Download file from sandbox
200
+ */
201
+ download(_remotePath: string): Promise<Buffer>;
202
+ /**
203
+ * Destroy isolator and cleanup
204
+ */
103
205
  destroy(): Promise<void>;
104
206
  }
105
- declare abstract class Runtime2 {
106
- abstract execute(options: ExecuteOptions): Promise<ExecuteResult>;
107
- abstract prepare(): Promise<void>;
108
- abstract cleanup(): Promise<void>;
109
- }
110
- declare class GenericRuntime extends Runtime2 {
111
- private isolator;
112
- constructor(isolator: Isolator);
113
- execute(options: ExecuteOptions): Promise<ExecuteResult>;
114
- prepare(): Promise<void>;
115
- cleanup(): Promise<void>;
116
- }
207
+ import { StateFS, StateEnv, StateStorage, buildStateLog, loadStateLog, StateLog as StateLog2, StateLogEntry, FileSystem, Environment, Storage, WithState as WithState3, StateError, FileSystemError } from "@sandboxxjs/state";
208
+ import { WithState as WithState2 } from "@sandboxxjs/state";
209
+ /**
210
+ * Add state capabilities to sandbox
211
+ */
212
+ declare function withState<T extends Sandbox>(Base: SandboxConstructor<T>): SandboxConstructor<T & WithState2>;
213
+ /**
214
+ * Add Node.js execute capability to sandbox
215
+ */
216
+ declare function withNodeExecute<T extends Sandbox>(Base: SandboxConstructor<T>): SandboxConstructor<T & WithExecute>;
217
+ /**
218
+ * Add Python execute capability to sandbox
219
+ */
220
+ declare function withPythonExecute<T extends Sandbox>(Base: SandboxConstructor<T>): SandboxConstructor<T & WithExecute>;
117
221
  /**
118
222
  * Error types for SandboX
119
223
  */
@@ -129,7 +233,4 @@ declare class TimeoutError extends SandboxError {
129
233
  declare class IsolationError extends SandboxError {
130
234
  constructor(message: string);
131
235
  }
132
- declare class FileSystemError extends SandboxError {
133
- constructor(message: string);
134
- }
135
- export { TimeoutError, SandboxError, SandboxConfig, Sandbox, Runtime2 as Runtime, ResourceLimits, LocalIsolator, IsolatorType, Isolator, IsolationError, IsolationConfig, GenericRuntime, FileSystemError, FileSystem, ExecutionError, ExecuteResult, ExecuteOptions, EventHandler, CloudflareContainerIsolator };
236
+ export { withState, withPythonExecute, withNodeExecute, loadStateLog, buildStateLog, WithState3 as WithState, WithExecute, TimeoutError, Storage, StateStorage, StateSandbox, StateLogEntry, StateLog2 as StateLog, StateFS, StateError, StateEnv, StateConfig, ShellResult, SandboxMixin, SandboxError, SandboxConstructor, SandboxConfig, Sandbox, Runtime, ResourceLimits, PythonSandbox, PythonConfig, NodeSandbox, NodeConfig, LocalIsolator, IsolatorType, Isolator, IsolationError, FileSystemError, FileSystem, ExecutionError, ExecuteResult, Environment, CloudflareContainerIsolator, BaseSandbox };