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