@buddhilive/sandbox 0.1.0-beta.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.
@@ -0,0 +1,219 @@
1
+ export declare interface FileStat {
2
+ isFile: boolean;
3
+ isDirectory: boolean;
4
+ isSymbolicLink: boolean;
5
+ size: number;
6
+ mtimeMs: number;
7
+ mode: number;
8
+ }
9
+
10
+ export declare class FsNamespace {
11
+ private bridge;
12
+ private adapter;
13
+ constructor(bridge: WorkerBridge, adapter?: PersistenceAdapter | null);
14
+ setPersistenceAdapter(adapter: PersistenceAdapter | null): void;
15
+ writeFile(path: string, data: string | Uint8Array): Promise<void>;
16
+ readFile(path: string): Promise<Uint8Array>;
17
+ readFile(path: string, encoding: 'utf-8'): Promise<string>;
18
+ mkdir(path: string, options?: {
19
+ recursive?: boolean;
20
+ }): Promise<void>;
21
+ readdir(path: string): Promise<string[]>;
22
+ rm(path: string, options?: {
23
+ recursive?: boolean;
24
+ }): Promise<void>;
25
+ stat(path: string): Promise<FileStat>;
26
+ symlink(target: string, path: string): Promise<void>;
27
+ }
28
+
29
+ export declare interface ListenEvent {
30
+ port: number;
31
+ url: string;
32
+ }
33
+
34
+ export declare class OOMError extends SandboxError {
35
+ constructor(message?: string);
36
+ }
37
+
38
+ export declare interface PersistenceAdapter {
39
+ save(path: string, data: Uint8Array): Promise<void>;
40
+ load(path: string): Promise<Uint8Array | null>;
41
+ delete(path: string): Promise<void>;
42
+ clear(): Promise<void>;
43
+ }
44
+
45
+ declare type PortListener = (event: ListenEvent) => void;
46
+
47
+ export declare class PortsNamespace {
48
+ private bridge;
49
+ private listeners;
50
+ private activePorts;
51
+ constructor(bridge: WorkerBridge);
52
+ on(event: 'listen' | 'close', listener: PortListener): () => void;
53
+ off(event: 'listen' | 'close', listener: PortListener): void;
54
+ getPreviewUrl(port: number): string | undefined;
55
+ }
56
+
57
+ export declare interface ProcessExecResult {
58
+ stdout: string;
59
+ stderr: string;
60
+ exitCode: number;
61
+ }
62
+
63
+ export declare interface ProcessHandle {
64
+ readonly pid: number;
65
+ readonly stdout: ReadableStream<Uint8Array>;
66
+ readonly stderr: ReadableStream<Uint8Array>;
67
+ readonly stdin: WritableStream<Uint8Array>;
68
+ readonly exit: Promise<number>;
69
+ kill(signal?: string): Promise<void>;
70
+ }
71
+
72
+ export declare class ProcessNamespace {
73
+ private bridge;
74
+ private exitResolvers;
75
+ constructor(bridge: WorkerBridge);
76
+ spawn(command: string, args?: string[], options?: {
77
+ env?: Record<string, string>;
78
+ cwd?: string;
79
+ }): Promise<ProcessHandle>;
80
+ exec(commandLine: string): Promise<ProcessExecResult>;
81
+ kill(pid: number, signal?: string): Promise<void>;
82
+ }
83
+
84
+ export declare class Sandbox {
85
+ readonly fs: FsNamespace;
86
+ readonly process: ProcessNamespace;
87
+ readonly ports: PortsNamespace;
88
+ private bridge;
89
+ private isDisposed;
90
+ private constructor();
91
+ static create(options?: SandboxOptions): Promise<Sandbox>;
92
+ dispose(): Promise<void>;
93
+ }
94
+
95
+ export declare class SandboxError extends Error {
96
+ readonly code?: string | undefined;
97
+ constructor(message: string, code?: string | undefined);
98
+ }
99
+
100
+ /**
101
+ * Core type declarations for @buddhilive/sandbox
102
+ */
103
+ export declare interface SandboxOptions {
104
+ /** Maximum memory quota in megabytes (default: 512) */
105
+ maxMemoryMb?: number;
106
+ /** Default command execution timeout in milliseconds (default: 30000) */
107
+ commandTimeoutMs?: number;
108
+ /** NPM registry URL (default: 'https://registry.npmjs.org') */
109
+ registryUrl?: string;
110
+ /** Optional persistence adapter for VirtualFS state */
111
+ persistenceAdapter?: 'opfs' | 'indexeddb' | null;
112
+ /** Optional URL or custom path to sandbox.worker.js */
113
+ workerUrl?: string;
114
+ /** Optional URL or custom path to WebAssembly core binary */
115
+ wasmUrl?: string;
116
+ }
117
+
118
+ declare class WorkerBridge {
119
+ private worker;
120
+ private pendingRequests;
121
+ private messageListeners;
122
+ constructor(worker: Worker);
123
+ postMessage(msg: WorkerInboundMessage, transfer?: Transferable[]): void;
124
+ request<T = any>(msg: WorkerInboundMessage & {
125
+ id: string;
126
+ }): Promise<T>;
127
+ onMessage(listener: (msg: WorkerOutboundMessage) => void): () => void;
128
+ terminate(): void;
129
+ }
130
+
131
+ declare type WorkerInboundMessage = {
132
+ type: 'init';
133
+ options: SandboxOptions;
134
+ } | {
135
+ type: 'fs:read';
136
+ id: string;
137
+ path: string;
138
+ } | {
139
+ type: 'fs:write';
140
+ id: string;
141
+ path: string;
142
+ data: Uint8Array;
143
+ } | {
144
+ type: 'fs:mkdir';
145
+ id: string;
146
+ path: string;
147
+ recursive?: boolean;
148
+ } | {
149
+ type: 'fs:readdir';
150
+ id: string;
151
+ path: string;
152
+ } | {
153
+ type: 'fs:rm';
154
+ id: string;
155
+ path: string;
156
+ recursive?: boolean;
157
+ } | {
158
+ type: 'fs:stat';
159
+ id: string;
160
+ path: string;
161
+ } | {
162
+ type: 'fs:symlink';
163
+ id: string;
164
+ target: string;
165
+ path: string;
166
+ } | {
167
+ type: 'process:spawn';
168
+ id: string;
169
+ command: string;
170
+ args: string[];
171
+ env?: Record<string, string>;
172
+ cwd?: string;
173
+ } | {
174
+ type: 'process:kill';
175
+ pid: number;
176
+ signal?: string;
177
+ } | {
178
+ type: 'port:listen_ack';
179
+ port: number;
180
+ messagePort: MessagePort;
181
+ };
182
+
183
+ declare type WorkerOutboundMessage = {
184
+ type: 'ready';
185
+ } | {
186
+ type: 'error';
187
+ message: string;
188
+ code?: string;
189
+ } | {
190
+ type: 'fs:response';
191
+ id: string;
192
+ error?: string;
193
+ result?: unknown;
194
+ } | {
195
+ type: 'process:spawned';
196
+ id: string;
197
+ pid: number;
198
+ stdoutSab: SharedArrayBuffer;
199
+ stderrSab: SharedArrayBuffer;
200
+ stdinSab: SharedArrayBuffer;
201
+ } | {
202
+ type: 'process:exit';
203
+ pid: number;
204
+ code: number;
205
+ } | {
206
+ type: 'port:listen';
207
+ port: number;
208
+ } | {
209
+ type: 'port:close';
210
+ port: number;
211
+ } | {
212
+ type: 'toolchain:needed';
213
+ pkg: string;
214
+ } | {
215
+ type: 'oom';
216
+ message: string;
217
+ };
218
+
219
+ export { }