@cloudflare/sandbox 0.13.0-next.649.1 → 0.13.0-next.681.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/Dockerfile +6 -11
- package/README.md +2 -2
- package/dist/bridge/index.d.ts.map +1 -1
- package/dist/bridge/index.js +68 -49
- package/dist/bridge/index.js.map +1 -1
- package/dist/codes-y-U76vnQ.d.ts +79 -0
- package/dist/codes-y-U76vnQ.d.ts.map +1 -0
- package/dist/contexts-DY1LHU1v.d.ts +327 -0
- package/dist/contexts-DY1LHU1v.d.ts.map +1 -0
- package/dist/{dist-mAH_7Ui7.js → dist-BStBkGIC.js} +50 -93
- package/dist/dist-BStBkGIC.js.map +1 -0
- package/dist/errors/index.d.ts +4 -0
- package/dist/errors/index.js +4 -0
- package/dist/errors-ewgSNicb.js +741 -0
- package/dist/errors-ewgSNicb.js.map +1 -0
- package/dist/{errors-CpoDEfUZ.js → errors-k3B8orjH.js} +41 -4
- package/dist/errors-k3B8orjH.js.map +1 -0
- package/dist/extensions/index.d.ts +2 -0
- package/dist/extensions/index.js +4 -0
- package/dist/extensions-CepYdzro.js +191 -0
- package/dist/extensions-CepYdzro.js.map +1 -0
- package/dist/git/index.d.ts +74 -0
- package/dist/git/index.d.ts.map +1 -0
- package/dist/git/index.js +345 -0
- package/dist/git/index.js.map +1 -0
- package/dist/index-B7QgIs0N.d.ts +1841 -0
- package/dist/index-B7QgIs0N.d.ts.map +1 -0
- package/dist/index-Dy6u9r60.d.ts +390 -0
- package/dist/index-Dy6u9r60.d.ts.map +1 -0
- package/dist/index.d.ts +27 -189
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -43
- package/dist/index.js.map +1 -1
- package/dist/interpreter/index.d.ts +309 -0
- package/dist/interpreter/index.d.ts.map +1 -0
- package/dist/interpreter/index.js +264 -0
- package/dist/interpreter/index.js.map +1 -0
- package/dist/openai/index.d.ts +3 -2
- package/dist/openai/index.d.ts.map +1 -1
- package/dist/openai/index.js +2 -2
- package/dist/openai/index.js.map +1 -1
- package/dist/opencode/index.d.ts +137 -160
- package/dist/opencode/index.d.ts.map +1 -1
- package/dist/opencode/index.js +269 -159
- package/dist/opencode/index.js.map +1 -1
- package/dist/sandbox-DHNO89IF.d.ts +828 -0
- package/dist/sandbox-DHNO89IF.d.ts.map +1 -0
- package/dist/{sandbox-B-FLGbkP.js → sandbox-boKWPIcd.js} +8214 -7267
- package/dist/sandbox-boKWPIcd.js.map +1 -0
- package/dist/sidecar/index.d.ts +77 -0
- package/dist/sidecar/index.d.ts.map +1 -0
- package/dist/sidecar/index.js +201 -0
- package/dist/sidecar/index.js.map +1 -0
- package/dist/xterm/index.d.ts +6 -6
- package/dist/xterm/index.d.ts.map +1 -1
- package/dist/xterm/index.js +6 -6
- package/dist/xterm/index.js.map +1 -1
- package/package.json +36 -3
- package/dist/contexts-DPFhc2nR.d.ts +0 -238
- package/dist/contexts-DPFhc2nR.d.ts.map +0 -1
- package/dist/dist-mAH_7Ui7.js.map +0 -1
- package/dist/errors-CpoDEfUZ.js.map +0 -1
- package/dist/sandbox-44kEJjAc.d.ts +0 -2652
- package/dist/sandbox-44kEJjAc.d.ts.map +0 -1
- package/dist/sandbox-B-FLGbkP.js.map +0 -1
|
@@ -0,0 +1,1841 @@
|
|
|
1
|
+
import { RpcTarget } from "cloudflare:workers";
|
|
2
|
+
|
|
3
|
+
//#region ../shared/dist/pty-types.d.ts
|
|
4
|
+
interface PtyOptions {
|
|
5
|
+
cols?: number;
|
|
6
|
+
rows?: number;
|
|
7
|
+
shell?: string;
|
|
8
|
+
}
|
|
9
|
+
interface TerminalOptions {
|
|
10
|
+
id?: string;
|
|
11
|
+
cwd?: string;
|
|
12
|
+
shell?: string;
|
|
13
|
+
}
|
|
14
|
+
interface TerminalCreateOptions {
|
|
15
|
+
id: string;
|
|
16
|
+
cwd?: string;
|
|
17
|
+
shell?: string;
|
|
18
|
+
cols?: number;
|
|
19
|
+
rows?: number;
|
|
20
|
+
}
|
|
21
|
+
interface TerminalConnectOptions {
|
|
22
|
+
cols?: number;
|
|
23
|
+
rows?: number;
|
|
24
|
+
}
|
|
25
|
+
interface SandboxTerminal {
|
|
26
|
+
readonly id: string;
|
|
27
|
+
connect(request: Request, options?: TerminalConnectOptions): Promise<Response>;
|
|
28
|
+
destroy(): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region ../shared/dist/types.d.ts
|
|
32
|
+
interface BaseExecOptions {
|
|
33
|
+
/**
|
|
34
|
+
* Maximum execution time in milliseconds
|
|
35
|
+
*/
|
|
36
|
+
timeout?: number;
|
|
37
|
+
/**
|
|
38
|
+
* Environment variables for this command invocation.
|
|
39
|
+
* Values temporarily override session-level/container-level env for the
|
|
40
|
+
* duration of the command but do not persist after it completes.
|
|
41
|
+
* Undefined values are skipped (treated as "not configured").
|
|
42
|
+
*/
|
|
43
|
+
env?: Record<string, string | undefined>;
|
|
44
|
+
/**
|
|
45
|
+
* Working directory for command execution
|
|
46
|
+
*/
|
|
47
|
+
cwd?: string;
|
|
48
|
+
/**
|
|
49
|
+
* Text encoding for output (default: 'utf8')
|
|
50
|
+
*/
|
|
51
|
+
encoding?: string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* @deprecated Use `SandboxExecOptions` instead. Will be removed in a future
|
|
55
|
+
* release once all callers migrate to `sandbox.exec(cmd, options)` returning
|
|
56
|
+
* a `SandboxProcessPromise`. See `docs/EXEC_MIGRATION.md`.
|
|
57
|
+
*/
|
|
58
|
+
interface ExecOptions extends BaseExecOptions {
|
|
59
|
+
/**
|
|
60
|
+
* Enable real-time output streaming via callbacks
|
|
61
|
+
*/
|
|
62
|
+
stream?: boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Callback for real-time output data
|
|
65
|
+
*/
|
|
66
|
+
onOutput?: (stream: 'stdout' | 'stderr', data: string) => void;
|
|
67
|
+
/**
|
|
68
|
+
* Callback when command completes (only when stream: true)
|
|
69
|
+
*/
|
|
70
|
+
onComplete?: (result: ExecResult) => void;
|
|
71
|
+
/**
|
|
72
|
+
* Callback for execution errors
|
|
73
|
+
*/
|
|
74
|
+
onError?: (error: Error) => void;
|
|
75
|
+
/**
|
|
76
|
+
* AbortSignal for cancelling execution
|
|
77
|
+
*/
|
|
78
|
+
signal?: AbortSignal;
|
|
79
|
+
/**
|
|
80
|
+
* Whether this command was initiated by the user or by internal
|
|
81
|
+
* infrastructure (backup, bucket mount, env setup, etc.).
|
|
82
|
+
* Defaults to 'user' when omitted.
|
|
83
|
+
*/
|
|
84
|
+
origin?: 'user' | 'internal';
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* @deprecated Use `SandboxExecOutput` (returned by
|
|
88
|
+
* `SandboxProcess.output()`) instead. Will be removed alongside `ExecOptions`.
|
|
89
|
+
*/
|
|
90
|
+
interface ExecResult {
|
|
91
|
+
/**
|
|
92
|
+
* Whether the command succeeded (exitCode === 0)
|
|
93
|
+
*/
|
|
94
|
+
success: boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Process exit code
|
|
97
|
+
*/
|
|
98
|
+
exitCode: number;
|
|
99
|
+
/**
|
|
100
|
+
* Standard output content
|
|
101
|
+
*/
|
|
102
|
+
stdout: string;
|
|
103
|
+
/**
|
|
104
|
+
* Standard error content
|
|
105
|
+
*/
|
|
106
|
+
stderr: string;
|
|
107
|
+
/**
|
|
108
|
+
* Command that was executed
|
|
109
|
+
*/
|
|
110
|
+
command: string;
|
|
111
|
+
/**
|
|
112
|
+
* Execution duration in milliseconds
|
|
113
|
+
*/
|
|
114
|
+
duration: number;
|
|
115
|
+
/**
|
|
116
|
+
* ISO timestamp when command started
|
|
117
|
+
*/
|
|
118
|
+
timestamp: string;
|
|
119
|
+
/**
|
|
120
|
+
* Session ID if provided
|
|
121
|
+
*/
|
|
122
|
+
sessionId?: string;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Result from waiting for a log pattern
|
|
126
|
+
*/
|
|
127
|
+
interface WaitForLogResult {
|
|
128
|
+
/** The log line that matched */
|
|
129
|
+
line: string;
|
|
130
|
+
/** Regex capture groups (if condition was a RegExp) */
|
|
131
|
+
match?: RegExpMatchArray;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Result from waiting for process exit
|
|
135
|
+
*/
|
|
136
|
+
interface WaitForExitResult {
|
|
137
|
+
/** Process exit code */
|
|
138
|
+
exitCode: number;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Options for waiting for a port to become ready
|
|
142
|
+
*/
|
|
143
|
+
interface WaitForPortOptions {
|
|
144
|
+
/**
|
|
145
|
+
* Check mode
|
|
146
|
+
* - 'http': Make an HTTP request and check for success status (default)
|
|
147
|
+
* - 'tcp': Just check if TCP connection succeeds
|
|
148
|
+
* @default 'http'
|
|
149
|
+
*/
|
|
150
|
+
mode?: 'http' | 'tcp';
|
|
151
|
+
/**
|
|
152
|
+
* HTTP path to check (only used when mode is 'http')
|
|
153
|
+
* @default '/'
|
|
154
|
+
*/
|
|
155
|
+
path?: string;
|
|
156
|
+
/**
|
|
157
|
+
* Expected HTTP status code or range (only used when mode is 'http')
|
|
158
|
+
* - Single number: exact match (e.g., 200)
|
|
159
|
+
* - Object with min/max: range match (e.g., { min: 200, max: 399 })
|
|
160
|
+
* @default { min: 200, max: 399 }
|
|
161
|
+
*/
|
|
162
|
+
status?: number | {
|
|
163
|
+
min: number;
|
|
164
|
+
max: number;
|
|
165
|
+
};
|
|
166
|
+
/**
|
|
167
|
+
* Maximum time to wait in milliseconds
|
|
168
|
+
* @default no timeout
|
|
169
|
+
*/
|
|
170
|
+
timeout?: number;
|
|
171
|
+
/**
|
|
172
|
+
* Interval between checks in milliseconds
|
|
173
|
+
* @default 500
|
|
174
|
+
*/
|
|
175
|
+
interval?: number;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Request body for port readiness check endpoint
|
|
179
|
+
*/
|
|
180
|
+
interface PortCheckRequest {
|
|
181
|
+
port: number;
|
|
182
|
+
mode: 'http' | 'tcp';
|
|
183
|
+
path?: string;
|
|
184
|
+
statusMin?: number;
|
|
185
|
+
statusMax?: number;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Request body for streaming port watch endpoint
|
|
189
|
+
*/
|
|
190
|
+
interface PortWatchRequest extends PortCheckRequest {
|
|
191
|
+
/** Process ID to monitor - stream closes if process exits */
|
|
192
|
+
processId?: string;
|
|
193
|
+
/** Internal polling interval in ms (default: 500) */
|
|
194
|
+
interval?: number;
|
|
195
|
+
}
|
|
196
|
+
interface ProcessQueryOptions {
|
|
197
|
+
/**
|
|
198
|
+
* Optional session ID used to bind returned process handles to an explicit session.
|
|
199
|
+
*/
|
|
200
|
+
sessionId?: string;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* @deprecated Use `SandboxExecOptions` with `processId` set instead. The
|
|
204
|
+
* unified `sandbox.exec()` returns the same `SandboxProcess` handle whether
|
|
205
|
+
* the caller wants a foreground or background process.
|
|
206
|
+
*/
|
|
207
|
+
interface ProcessOptions extends BaseExecOptions {
|
|
208
|
+
/**
|
|
209
|
+
* Optional session ID to run the background process in.
|
|
210
|
+
*
|
|
211
|
+
* When omitted, the sandbox's default execution policy applies.
|
|
212
|
+
*/
|
|
213
|
+
sessionId?: string;
|
|
214
|
+
/**
|
|
215
|
+
* Custom process ID for later reference
|
|
216
|
+
* If not provided, a UUID will be generated
|
|
217
|
+
*/
|
|
218
|
+
processId?: string;
|
|
219
|
+
/**
|
|
220
|
+
* Automatically cleanup process record after exit (default: true)
|
|
221
|
+
*/
|
|
222
|
+
autoCleanup?: boolean;
|
|
223
|
+
/**
|
|
224
|
+
* Callback when process exits
|
|
225
|
+
*/
|
|
226
|
+
onExit?: (code: number | null) => void;
|
|
227
|
+
/**
|
|
228
|
+
* Callback for real-time output (background processes)
|
|
229
|
+
*/
|
|
230
|
+
onOutput?: (stream: 'stdout' | 'stderr', data: string) => void;
|
|
231
|
+
/**
|
|
232
|
+
* Callback when process starts successfully
|
|
233
|
+
*/
|
|
234
|
+
onStart?: (process: Process) => void;
|
|
235
|
+
/**
|
|
236
|
+
* Callback for process errors
|
|
237
|
+
*/
|
|
238
|
+
onError?: (error: Error) => void;
|
|
239
|
+
}
|
|
240
|
+
type ProcessStatus = 'starting' | 'running' | 'completed' | 'failed' | 'killed' | 'error';
|
|
241
|
+
/**
|
|
242
|
+
* @deprecated Use `SandboxProcess` instead. The unified handle has the same
|
|
243
|
+
* `id`/`pid`/`command`/`status`/`startTime`/`sessionId` fields plus
|
|
244
|
+
* `stdin`/`stdout`/`stderr` streams, an `exitCode` promise, `output()` and
|
|
245
|
+
* `kill()` that mirror the `ctx.container.exec()` contract.
|
|
246
|
+
*/
|
|
247
|
+
interface Process {
|
|
248
|
+
/**
|
|
249
|
+
* Unique process identifier
|
|
250
|
+
*/
|
|
251
|
+
readonly id: string;
|
|
252
|
+
/**
|
|
253
|
+
* System process ID (if available and running)
|
|
254
|
+
*/
|
|
255
|
+
readonly pid?: number;
|
|
256
|
+
/**
|
|
257
|
+
* Command that was executed
|
|
258
|
+
*/
|
|
259
|
+
readonly command: string;
|
|
260
|
+
/**
|
|
261
|
+
* Current process status
|
|
262
|
+
*/
|
|
263
|
+
readonly status: ProcessStatus;
|
|
264
|
+
/**
|
|
265
|
+
* When the process was started
|
|
266
|
+
*/
|
|
267
|
+
readonly startTime: Date;
|
|
268
|
+
/**
|
|
269
|
+
* When the process ended (if completed)
|
|
270
|
+
*/
|
|
271
|
+
readonly endTime?: Date;
|
|
272
|
+
/**
|
|
273
|
+
* Process exit code (if completed)
|
|
274
|
+
*/
|
|
275
|
+
readonly exitCode?: number;
|
|
276
|
+
/**
|
|
277
|
+
* Session ID if provided
|
|
278
|
+
*/
|
|
279
|
+
readonly sessionId?: string;
|
|
280
|
+
/**
|
|
281
|
+
* Kill the process
|
|
282
|
+
*/
|
|
283
|
+
kill(signal?: string): Promise<void>;
|
|
284
|
+
/**
|
|
285
|
+
* Get current process status (refreshed)
|
|
286
|
+
*/
|
|
287
|
+
getStatus(): Promise<ProcessStatus>;
|
|
288
|
+
/**
|
|
289
|
+
* Get accumulated logs
|
|
290
|
+
*/
|
|
291
|
+
getLogs(): Promise<{
|
|
292
|
+
stdout: string;
|
|
293
|
+
stderr: string;
|
|
294
|
+
}>;
|
|
295
|
+
/**
|
|
296
|
+
* Wait for a log pattern to appear in process output
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* const proc = await sandbox.startProcess("python train.py");
|
|
300
|
+
* await proc.waitForLog("Epoch 1 complete");
|
|
301
|
+
* await proc.waitForLog(/Epoch (\d+) complete/);
|
|
302
|
+
*/
|
|
303
|
+
waitForLog(pattern: string | RegExp, timeout?: number): Promise<WaitForLogResult>;
|
|
304
|
+
/**
|
|
305
|
+
* Wait for a port to become ready
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* // Wait for HTTP endpoint to return 200-399
|
|
309
|
+
* const proc = await sandbox.startProcess("npm run dev");
|
|
310
|
+
* await proc.waitForPort(3000);
|
|
311
|
+
*
|
|
312
|
+
* @example
|
|
313
|
+
* // Wait for specific health endpoint
|
|
314
|
+
* await proc.waitForPort(3000, { path: '/health', status: 200 });
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* // TCP-only check (just verify port is accepting connections)
|
|
318
|
+
* await proc.waitForPort(5432, { mode: 'tcp' });
|
|
319
|
+
*/
|
|
320
|
+
waitForPort(port: number, options?: WaitForPortOptions): Promise<void>;
|
|
321
|
+
/**
|
|
322
|
+
* Wait for the process to exit
|
|
323
|
+
*
|
|
324
|
+
* Returns the exit code. Use getProcessLogs() or streamProcessLogs()
|
|
325
|
+
* to retrieve output after the process exits.
|
|
326
|
+
*/
|
|
327
|
+
waitForExit(timeout?: number): Promise<WaitForExitResult>;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* @deprecated Internal SSE frame type. Consumers should read
|
|
331
|
+
* `SandboxProcess.stdout` / `.stderr` (raw byte streams) instead of parsing
|
|
332
|
+
* SSE frames. Retained while the legacy `execStream` HTTP route exists.
|
|
333
|
+
*/
|
|
334
|
+
interface ExecEvent {
|
|
335
|
+
type: 'start' | 'stdout' | 'stderr' | 'complete' | 'error';
|
|
336
|
+
timestamp: string;
|
|
337
|
+
data?: string;
|
|
338
|
+
command?: string;
|
|
339
|
+
exitCode?: number;
|
|
340
|
+
result?: ExecResult;
|
|
341
|
+
error?: string;
|
|
342
|
+
sessionId?: string;
|
|
343
|
+
pid?: number;
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* @deprecated Internal SSE frame type for `streamProcessLogs`. Consumers
|
|
347
|
+
* should read `SandboxProcess.stdout` / `.stderr` instead.
|
|
348
|
+
*/
|
|
349
|
+
interface LogEvent {
|
|
350
|
+
type: 'stdout' | 'stderr' | 'exit' | 'error';
|
|
351
|
+
timestamp: string;
|
|
352
|
+
data: string;
|
|
353
|
+
processId: string;
|
|
354
|
+
sessionId?: string;
|
|
355
|
+
exitCode?: number;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Options for `sandbox.exec(cmd, options)`.
|
|
359
|
+
*
|
|
360
|
+
* Mirrors `ContainerExecOptions` from
|
|
361
|
+
* `@cloudflare/workers-types/experimental` (`cwd`, `env`, `stdin`, `stdout`,
|
|
362
|
+
* `stderr`) with sandbox-specific extensions for sessions, named process
|
|
363
|
+
* ids, timeouts, and cancellation.
|
|
364
|
+
*
|
|
365
|
+
* Notable differences from the runtime contract:
|
|
366
|
+
* - `env` values may be `undefined` (treated as "not configured"). The
|
|
367
|
+
* container API requires `Record<string, string>`; the SDK strips
|
|
368
|
+
* `undefined` entries before forwarding.
|
|
369
|
+
* - `stdin` additionally accepts a plain `string` for convenience.
|
|
370
|
+
* - `user` is not yet supported (no session-backend story).
|
|
371
|
+
*/
|
|
372
|
+
interface SandboxExecOptions {
|
|
373
|
+
/** Working directory for command execution. */
|
|
374
|
+
cwd?: string;
|
|
375
|
+
/**
|
|
376
|
+
* Environment variables for this command invocation.
|
|
377
|
+
*
|
|
378
|
+
* Values temporarily override session-level/container-level env for the
|
|
379
|
+
* duration of the command but do not persist after it completes.
|
|
380
|
+
* Undefined values are skipped (treated as "not configured").
|
|
381
|
+
*/
|
|
382
|
+
env?: Record<string, string | undefined>;
|
|
383
|
+
/**
|
|
384
|
+
* Standard input source.
|
|
385
|
+
*
|
|
386
|
+
* - `"pipe"` — `SandboxProcess.stdin` is a `WritableStream<Uint8Array>` the
|
|
387
|
+
* caller can write to.
|
|
388
|
+
* - `ReadableStream<Uint8Array>` — the stream is piped into the command.
|
|
389
|
+
* - `string` — convenience: the string is utf-8 encoded and piped in.
|
|
390
|
+
* - omitted — stdin is closed.
|
|
391
|
+
*/
|
|
392
|
+
stdin?: ReadableStream<Uint8Array> | string | 'pipe';
|
|
393
|
+
/**
|
|
394
|
+
* Standard output disposition.
|
|
395
|
+
*
|
|
396
|
+
* - `"pipe"` (default) — `SandboxProcess.stdout` exposes a
|
|
397
|
+
* `ReadableStream<Uint8Array>` of the output.
|
|
398
|
+
* - `"ignore"` — output is discarded; `SandboxProcess.stdout` is `null`.
|
|
399
|
+
*/
|
|
400
|
+
stdout?: 'pipe' | 'ignore';
|
|
401
|
+
/**
|
|
402
|
+
* Standard error disposition.
|
|
403
|
+
*
|
|
404
|
+
* - `"pipe"` (default) — `SandboxProcess.stderr` exposes a
|
|
405
|
+
* `ReadableStream<Uint8Array>` of the error output.
|
|
406
|
+
* - `"ignore"` — stderr is discarded; `SandboxProcess.stderr` is `null`.
|
|
407
|
+
* - `"combined"` — stderr is merged into stdout; `SandboxProcess.stderr`
|
|
408
|
+
* is `null`.
|
|
409
|
+
*/
|
|
410
|
+
stderr?: 'pipe' | 'ignore' | 'combined';
|
|
411
|
+
/**
|
|
412
|
+
* Session ID providing working-directory and environment scope.
|
|
413
|
+
*
|
|
414
|
+
* When omitted, the sandbox's default execution policy applies (typically
|
|
415
|
+
* the persistent default session).
|
|
416
|
+
*/
|
|
417
|
+
sessionId?: string;
|
|
418
|
+
/**
|
|
419
|
+
* Client-chosen process id for later re-attach via
|
|
420
|
+
* `sandbox.getProcess(id)`.
|
|
421
|
+
*
|
|
422
|
+
* When omitted, a UUID is generated.
|
|
423
|
+
*/
|
|
424
|
+
processId?: string;
|
|
425
|
+
/**
|
|
426
|
+
* Whether to remove the process record on exit.
|
|
427
|
+
*
|
|
428
|
+
* Currently retained for compatibility with legacy process options. Process
|
|
429
|
+
* records are kept so `output()`, `getLogs()`, and re-attach can read logs;
|
|
430
|
+
* callers can use `cleanupCompletedProcesses()` to remove completed records.
|
|
431
|
+
*/
|
|
432
|
+
autoCleanup?: boolean;
|
|
433
|
+
/**
|
|
434
|
+
* Wall-clock timeout in milliseconds. When the timeout fires the
|
|
435
|
+
* implementation sends `SIGTERM`; `exitCode` resolves with the resulting
|
|
436
|
+
* code (typically `143` / `-15`).
|
|
437
|
+
*/
|
|
438
|
+
timeout?: number;
|
|
439
|
+
/**
|
|
440
|
+
* External cancellation signal. Aborting triggers `kill()` and propagates
|
|
441
|
+
* to in-flight stream consumers.
|
|
442
|
+
*/
|
|
443
|
+
signal?: AbortSignal;
|
|
444
|
+
/**
|
|
445
|
+
* Whether this command was initiated by the user or by internal
|
|
446
|
+
* infrastructure (backup, bucket mount, env setup, etc.).
|
|
447
|
+
*
|
|
448
|
+
* Sandbox-only; not part of the containers contract. Defaults to
|
|
449
|
+
* `'user'` when omitted.
|
|
450
|
+
*/
|
|
451
|
+
origin?: 'user' | 'internal';
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Buffered output returned by `SandboxProcess.output()`.
|
|
455
|
+
*
|
|
456
|
+
* Mirrors `ExecOutput` from the containers contract (`stdout`, `stderr`,
|
|
457
|
+
* `exitCode`) with sandbox-specific telemetry fields preserved from the
|
|
458
|
+
* legacy `ExecResult` so existing callers can migrate without losing
|
|
459
|
+
* information.
|
|
460
|
+
*
|
|
461
|
+
* `stdout` / `stderr` are `string` by default (utf-8) or `ArrayBuffer`
|
|
462
|
+
* when `output({ encoding: 'buffer' })` is requested.
|
|
463
|
+
*/
|
|
464
|
+
type SandboxOutputMode = 'pipe' | 'ignore';
|
|
465
|
+
type SandboxStderrMode = 'pipe' | 'ignore' | 'combined';
|
|
466
|
+
type SandboxOutputEncoding = 'utf8' | 'buffer';
|
|
467
|
+
interface SandboxExecOutputBase<T> {
|
|
468
|
+
/** Standard output content. */
|
|
469
|
+
stdout: T;
|
|
470
|
+
/** Standard error content. */
|
|
471
|
+
stderr: T;
|
|
472
|
+
/** Process exit code. */
|
|
473
|
+
exitCode: number;
|
|
474
|
+
/** True iff `exitCode === 0`. */
|
|
475
|
+
success: boolean;
|
|
476
|
+
/** Execution duration in milliseconds, measured by the SDK. */
|
|
477
|
+
duration: number;
|
|
478
|
+
/** Command that was executed (display form). */
|
|
479
|
+
command: string;
|
|
480
|
+
/** ISO timestamp when the command started. */
|
|
481
|
+
timestamp: string;
|
|
482
|
+
/** Session ID, if the command ran in a session. */
|
|
483
|
+
sessionId?: string;
|
|
484
|
+
}
|
|
485
|
+
type SandboxExecStringOutput = SandboxExecOutputBase<string>;
|
|
486
|
+
type SandboxExecBufferOutput = SandboxExecOutputBase<ArrayBuffer>;
|
|
487
|
+
type SandboxExecOutput = SandboxExecStringOutput | SandboxExecBufferOutput;
|
|
488
|
+
/**
|
|
489
|
+
* Unified process handle returned (resolved) by `sandbox.exec()`.
|
|
490
|
+
*
|
|
491
|
+
* The first nine members are a superset of `ExecProcess` from the
|
|
492
|
+
* containers runtime contract (`stdin`, `stdout`, `stderr`, `pid`,
|
|
493
|
+
* `exitCode`, `output()`, `kill()`). The remaining members are
|
|
494
|
+
* sandbox-specific extensions for log replay, named processes, and the
|
|
495
|
+
* existing `waitFor*` helpers.
|
|
496
|
+
*
|
|
497
|
+
* On re-attach (via `sandbox.getProcess(id)`):
|
|
498
|
+
* - `stdin` is `null` (the original writer owns the pipe).
|
|
499
|
+
* - `stdout`/`stderr` are replay-then-tail streams sourced from the
|
|
500
|
+
* per-process log file.
|
|
501
|
+
*/
|
|
502
|
+
interface SandboxProcess {
|
|
503
|
+
readonly stdin: WritableStream<Uint8Array> | null;
|
|
504
|
+
readonly stdout: ReadableStream<Uint8Array> | null;
|
|
505
|
+
readonly stderr: ReadableStream<Uint8Array> | null;
|
|
506
|
+
readonly pid: number;
|
|
507
|
+
readonly exitCode: Promise<number>;
|
|
508
|
+
/**
|
|
509
|
+
* Drain stdout/stderr and await exit. Mirrors `ExecProcess.output()`.
|
|
510
|
+
*
|
|
511
|
+
* Default encoding is utf-8 strings; pass `{ encoding: 'buffer' }` to get
|
|
512
|
+
* `ArrayBuffer` instead (matches the containers contract).
|
|
513
|
+
*/
|
|
514
|
+
output(options: {
|
|
515
|
+
encoding: 'buffer';
|
|
516
|
+
}): Promise<SandboxExecBufferOutput>;
|
|
517
|
+
output(options?: {
|
|
518
|
+
encoding?: 'utf8';
|
|
519
|
+
}): Promise<SandboxExecStringOutput>;
|
|
520
|
+
output(options?: {
|
|
521
|
+
encoding?: SandboxOutputEncoding;
|
|
522
|
+
}): Promise<SandboxExecOutput>;
|
|
523
|
+
/** Sugar for `(await this.output({ encoding: 'utf8' })).stdout` as a string. */
|
|
524
|
+
text(): Promise<string>;
|
|
525
|
+
/** Sugar for `JSON.parse(await this.text())`. */
|
|
526
|
+
json<T = unknown>(): Promise<T>;
|
|
527
|
+
/**
|
|
528
|
+
* Terminate the process.
|
|
529
|
+
*
|
|
530
|
+
* Defaults to `SIGTERM` (15).
|
|
531
|
+
*/
|
|
532
|
+
kill(signal?: number | string): void;
|
|
533
|
+
/** Stable sandbox process id (survives re-attach). */
|
|
534
|
+
readonly id: string;
|
|
535
|
+
/** Command that was executed (display form). */
|
|
536
|
+
readonly command: string;
|
|
537
|
+
/** Session ID, if the command was scoped to a session. */
|
|
538
|
+
readonly sessionId?: string;
|
|
539
|
+
/** When the process started. */
|
|
540
|
+
readonly startTime: Date;
|
|
541
|
+
/** Current status snapshot. Cheap; uses local state where possible. */
|
|
542
|
+
status(): Promise<ProcessStatus>;
|
|
543
|
+
/** Replay buffered logs (post-exit or mid-flight). */
|
|
544
|
+
getLogs(): Promise<{
|
|
545
|
+
stdout: string;
|
|
546
|
+
stderr: string;
|
|
547
|
+
}>;
|
|
548
|
+
/** Wait for a log pattern to appear in stdout/stderr. */
|
|
549
|
+
waitForLog(pattern: string | RegExp, timeoutMs?: number): Promise<WaitForLogResult>;
|
|
550
|
+
/** Wait for a TCP port (or HTTP endpoint) to become ready. */
|
|
551
|
+
waitForPort(port: number, options?: WaitForPortOptions): Promise<void>;
|
|
552
|
+
/** Wait for the process to exit. */
|
|
553
|
+
waitForExit(timeoutMs?: number): Promise<WaitForExitResult>;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* The thenable returned by `sandbox.exec(cmd, options)`.
|
|
557
|
+
*
|
|
558
|
+
* Resolves to a `SandboxProcess` (so `await sandbox.exec(cmd)` works as
|
|
559
|
+
* expected) and additionally exposes the convenience members directly so
|
|
560
|
+
* callers can write `await sandbox.exec(cmd).output()` / `.text()` /
|
|
561
|
+
* `.json()` / `.kill()` without an extra `await`.
|
|
562
|
+
*
|
|
563
|
+
* Modeled on `Bun.spawn()` / `Deno.Command`.
|
|
564
|
+
*/
|
|
565
|
+
interface SandboxProcessPromise extends Promise<SandboxProcess> {
|
|
566
|
+
/** Equivalent to `(await this).output(options)`. */
|
|
567
|
+
output(options: {
|
|
568
|
+
encoding: 'buffer';
|
|
569
|
+
}): Promise<SandboxExecBufferOutput>;
|
|
570
|
+
output(options?: {
|
|
571
|
+
encoding?: 'utf8';
|
|
572
|
+
}): Promise<SandboxExecStringOutput>;
|
|
573
|
+
output(options?: {
|
|
574
|
+
encoding?: SandboxOutputEncoding;
|
|
575
|
+
}): Promise<SandboxExecOutput>;
|
|
576
|
+
/** Sugar for `(await this.output({ encoding: 'utf8' })).stdout` as a string. */
|
|
577
|
+
text(): Promise<string>;
|
|
578
|
+
/** Sugar for `JSON.parse(await this.text())`. */
|
|
579
|
+
json<T = unknown>(): Promise<T>;
|
|
580
|
+
/** Equivalent to `(await this).kill(signal)`. */
|
|
581
|
+
kill(signal?: number | string): Promise<void>;
|
|
582
|
+
}
|
|
583
|
+
interface SessionOptions {
|
|
584
|
+
/**
|
|
585
|
+
* Optional session ID (auto-generated if not provided)
|
|
586
|
+
*/
|
|
587
|
+
id?: string;
|
|
588
|
+
/**
|
|
589
|
+
* Session name for identification
|
|
590
|
+
*/
|
|
591
|
+
name?: string;
|
|
592
|
+
/**
|
|
593
|
+
* Environment variables for this session.
|
|
594
|
+
* Undefined values are skipped (treated as "not configured").
|
|
595
|
+
*/
|
|
596
|
+
env?: Record<string, string | undefined>;
|
|
597
|
+
/**
|
|
598
|
+
* Working directory
|
|
599
|
+
*/
|
|
600
|
+
cwd?: string;
|
|
601
|
+
/**
|
|
602
|
+
* Enable PID namespace isolation (requires CAP_SYS_ADMIN)
|
|
603
|
+
*/
|
|
604
|
+
isolation?: boolean;
|
|
605
|
+
/**
|
|
606
|
+
* Maximum amount of time a command can run in milliseconds
|
|
607
|
+
*/
|
|
608
|
+
commandTimeoutMs?: number;
|
|
609
|
+
}
|
|
610
|
+
interface SandboxOptions {
|
|
611
|
+
/**
|
|
612
|
+
* Duration after which the sandbox instance will sleep if no requests are received
|
|
613
|
+
* Can be:
|
|
614
|
+
* - A string like "30s", "3m", "5m", "1h" (seconds, minutes, or hours)
|
|
615
|
+
* - A number representing seconds (e.g., 180 for 3 minutes)
|
|
616
|
+
* Default: "10m" (10 minutes)
|
|
617
|
+
*
|
|
618
|
+
* Note: Ignored when keepAlive is true
|
|
619
|
+
*/
|
|
620
|
+
sleepAfter?: string | number;
|
|
621
|
+
/**
|
|
622
|
+
* Keep the container alive indefinitely by preventing automatic shutdown
|
|
623
|
+
* When true, the container will never auto-timeout and must be explicitly destroyed
|
|
624
|
+
* - Any scenario where activity can't be automatically detected
|
|
625
|
+
*
|
|
626
|
+
* Important: You MUST call sandbox.destroy() when done to avoid resource leaks
|
|
627
|
+
*
|
|
628
|
+
* Default: false
|
|
629
|
+
*/
|
|
630
|
+
keepAlive?: boolean;
|
|
631
|
+
/**
|
|
632
|
+
* When true (the default), implicit operations automatically create and reuse
|
|
633
|
+
* a persistent default shell session. Set to false to run implicit top-level
|
|
634
|
+
* operations sessionlessly, where each command spawns a fresh process with no
|
|
635
|
+
* shared shell state. Explicit per-call session IDs continue to work normally
|
|
636
|
+
* when this is false.
|
|
637
|
+
*
|
|
638
|
+
* Default: true
|
|
639
|
+
*/
|
|
640
|
+
enableDefaultSession?: boolean;
|
|
641
|
+
/**
|
|
642
|
+
* Normalize sandbox ID to lowercase for preview URL compatibility
|
|
643
|
+
*
|
|
644
|
+
* Required for preview URLs because hostnames are case-insensitive (RFC 3986), which
|
|
645
|
+
* would route requests to a different Durable Object instance with IDs containing uppercase letters.
|
|
646
|
+
*
|
|
647
|
+
* **Important:** Different normalizeId values create different Durable Object instances:
|
|
648
|
+
* - `getSandbox(ns, "MyProject")` → DO key: "MyProject"
|
|
649
|
+
* - `getSandbox(ns, "MyProject", {normalizeId: true})` → DO key: "myproject"
|
|
650
|
+
*
|
|
651
|
+
* **Future change:** In a future version, this will default to `true` (automatically lowercase all IDs).
|
|
652
|
+
* IDs with uppercase letters will trigger a warning. To prepare, use lowercase IDs or explicitly
|
|
653
|
+
* pass `normalizeId: true`.
|
|
654
|
+
*
|
|
655
|
+
* @example
|
|
656
|
+
* getSandbox(ns, "my-project") // Works with preview URLs (lowercase)
|
|
657
|
+
* getSandbox(ns, "MyProject", {normalizeId: true}) // Normalized to "myproject"
|
|
658
|
+
*
|
|
659
|
+
* @default false
|
|
660
|
+
*/
|
|
661
|
+
normalizeId?: boolean;
|
|
662
|
+
/**
|
|
663
|
+
* Container startup timeout configuration
|
|
664
|
+
*
|
|
665
|
+
* Tune timeouts based on your container's characteristics. SDK defaults (30s instance, 90s ports)
|
|
666
|
+
* work for most use cases. Adjust for heavy containers or fail-fast applications.
|
|
667
|
+
*
|
|
668
|
+
* Can also be configured via environment variables:
|
|
669
|
+
* - SANDBOX_INSTANCE_TIMEOUT_MS
|
|
670
|
+
* - SANDBOX_PORT_TIMEOUT_MS
|
|
671
|
+
* - SANDBOX_POLL_INTERVAL_MS
|
|
672
|
+
*
|
|
673
|
+
* Precedence: options > env vars > SDK defaults
|
|
674
|
+
*
|
|
675
|
+
* @example
|
|
676
|
+
* // Heavy containers (ML models, large apps)
|
|
677
|
+
* getSandbox(ns, id, {
|
|
678
|
+
* containerTimeouts: { portReadyTimeoutMS: 180_000 }
|
|
679
|
+
* })
|
|
680
|
+
*
|
|
681
|
+
* @example
|
|
682
|
+
* // Fail-fast for latency-sensitive apps
|
|
683
|
+
* getSandbox(ns, id, {
|
|
684
|
+
* containerTimeouts: {
|
|
685
|
+
* instanceGetTimeoutMS: 15_000,
|
|
686
|
+
* portReadyTimeoutMS: 30_000
|
|
687
|
+
* }
|
|
688
|
+
* })
|
|
689
|
+
*/
|
|
690
|
+
containerTimeouts?: {
|
|
691
|
+
/**
|
|
692
|
+
* Time to wait for container instance provisioning
|
|
693
|
+
* @default 30000 (30s) - or SANDBOX_INSTANCE_TIMEOUT_MS env var
|
|
694
|
+
*/
|
|
695
|
+
instanceGetTimeoutMS?: number;
|
|
696
|
+
/**
|
|
697
|
+
* Time to wait for application startup and ports to be ready
|
|
698
|
+
* @default 90000 (90s) - or SANDBOX_PORT_TIMEOUT_MS env var
|
|
699
|
+
*/
|
|
700
|
+
portReadyTimeoutMS?: number;
|
|
701
|
+
/**
|
|
702
|
+
* How often to poll for container readiness
|
|
703
|
+
* @default 300 (300ms) - or SANDBOX_POLL_INTERVAL_MS env var
|
|
704
|
+
*/
|
|
705
|
+
waitIntervalMS?: number;
|
|
706
|
+
};
|
|
707
|
+
}
|
|
708
|
+
/**
|
|
709
|
+
* Execution session - isolated execution context within a sandbox
|
|
710
|
+
* Returned by sandbox.createSession()
|
|
711
|
+
* Provides the same API as ISandbox but bound to a specific session
|
|
712
|
+
*/
|
|
713
|
+
interface MkdirResult {
|
|
714
|
+
success: boolean;
|
|
715
|
+
path: string;
|
|
716
|
+
recursive: boolean;
|
|
717
|
+
timestamp: string;
|
|
718
|
+
exitCode?: number;
|
|
719
|
+
}
|
|
720
|
+
interface WriteFileResult {
|
|
721
|
+
success: boolean;
|
|
722
|
+
path: string;
|
|
723
|
+
timestamp: string;
|
|
724
|
+
exitCode?: number;
|
|
725
|
+
}
|
|
726
|
+
/**
|
|
727
|
+
* Valid `encoding` values accepted by `readFile` / `writeFile` options.
|
|
728
|
+
*
|
|
729
|
+
* - `'utf-8'` / `'utf8'` — treat content as text.
|
|
730
|
+
* - `'base64'` — treat content as base64-encoded binary.
|
|
731
|
+
* - `'none'` — streaming variant of `readFile`, returns a
|
|
732
|
+
* `ReadableStream<Uint8Array>` of raw bytes (see `ReadFileStreamResult`).
|
|
733
|
+
*/
|
|
734
|
+
type FileEncoding = 'utf-8' | 'utf8' | 'base64' | 'none';
|
|
735
|
+
interface ReadFileResult {
|
|
736
|
+
success: boolean;
|
|
737
|
+
path: string;
|
|
738
|
+
content: string;
|
|
739
|
+
timestamp: string;
|
|
740
|
+
exitCode?: number;
|
|
741
|
+
/**
|
|
742
|
+
* Encoding used for content (utf-8 for text, base64 for binary)
|
|
743
|
+
*/
|
|
744
|
+
encoding?: 'utf-8' | 'base64';
|
|
745
|
+
/**
|
|
746
|
+
* Whether the file is detected as binary
|
|
747
|
+
*/
|
|
748
|
+
isBinary?: boolean;
|
|
749
|
+
/**
|
|
750
|
+
* MIME type of the file (e.g., 'image/png', 'text/plain')
|
|
751
|
+
*/
|
|
752
|
+
mimeType?: string;
|
|
753
|
+
/**
|
|
754
|
+
* File size in bytes
|
|
755
|
+
*/
|
|
756
|
+
size?: number;
|
|
757
|
+
}
|
|
758
|
+
/**
|
|
759
|
+
* Result of `readFile()` with `encoding: 'none'`.
|
|
760
|
+
*
|
|
761
|
+
* `content` is a raw binary `ReadableStream<Uint8Array>` delivered directly
|
|
762
|
+
* over the capnp channel — no base64 encoding, no SSE framing, no buffering.
|
|
763
|
+
*/
|
|
764
|
+
interface ReadFileStreamResult {
|
|
765
|
+
success: true;
|
|
766
|
+
path: string;
|
|
767
|
+
content: ReadableStream<Uint8Array>;
|
|
768
|
+
size: number;
|
|
769
|
+
mimeType: string;
|
|
770
|
+
timestamp: string;
|
|
771
|
+
}
|
|
772
|
+
interface DeleteFileResult {
|
|
773
|
+
success: boolean;
|
|
774
|
+
path: string;
|
|
775
|
+
timestamp: string;
|
|
776
|
+
exitCode?: number;
|
|
777
|
+
}
|
|
778
|
+
interface RenameFileResult {
|
|
779
|
+
success: boolean;
|
|
780
|
+
path: string;
|
|
781
|
+
newPath: string;
|
|
782
|
+
timestamp: string;
|
|
783
|
+
exitCode?: number;
|
|
784
|
+
}
|
|
785
|
+
interface MoveFileResult {
|
|
786
|
+
success: boolean;
|
|
787
|
+
path: string;
|
|
788
|
+
newPath: string;
|
|
789
|
+
timestamp: string;
|
|
790
|
+
exitCode?: number;
|
|
791
|
+
}
|
|
792
|
+
interface FileExistsResult {
|
|
793
|
+
success: boolean;
|
|
794
|
+
path: string;
|
|
795
|
+
exists: boolean;
|
|
796
|
+
timestamp: string;
|
|
797
|
+
}
|
|
798
|
+
interface FileInfo {
|
|
799
|
+
name: string;
|
|
800
|
+
absolutePath: string;
|
|
801
|
+
relativePath: string;
|
|
802
|
+
type: 'file' | 'directory' | 'symlink' | 'other';
|
|
803
|
+
size: number;
|
|
804
|
+
modifiedAt: string;
|
|
805
|
+
mode: string;
|
|
806
|
+
permissions: {
|
|
807
|
+
readable: boolean;
|
|
808
|
+
writable: boolean;
|
|
809
|
+
executable: boolean;
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
interface ListFilesOptions {
|
|
813
|
+
recursive?: boolean;
|
|
814
|
+
includeHidden?: boolean;
|
|
815
|
+
/**
|
|
816
|
+
* Optional session ID used to resolve relative paths and execution context.
|
|
817
|
+
*
|
|
818
|
+
* When omitted, the sandbox's default execution policy applies.
|
|
819
|
+
*/
|
|
820
|
+
sessionId?: string;
|
|
821
|
+
}
|
|
822
|
+
interface ListFilesResult {
|
|
823
|
+
success: boolean;
|
|
824
|
+
path: string;
|
|
825
|
+
files: FileInfo[];
|
|
826
|
+
count: number;
|
|
827
|
+
timestamp: string;
|
|
828
|
+
exitCode?: number;
|
|
829
|
+
}
|
|
830
|
+
/**
|
|
831
|
+
* SSE events for file streaming
|
|
832
|
+
*/
|
|
833
|
+
type FileStreamEvent = {
|
|
834
|
+
type: 'metadata';
|
|
835
|
+
mimeType: string;
|
|
836
|
+
size: number;
|
|
837
|
+
isBinary: boolean;
|
|
838
|
+
encoding: 'utf-8' | 'base64';
|
|
839
|
+
} | {
|
|
840
|
+
type: 'chunk';
|
|
841
|
+
data: string;
|
|
842
|
+
} | {
|
|
843
|
+
type: 'complete';
|
|
844
|
+
bytesRead: number;
|
|
845
|
+
} | {
|
|
846
|
+
type: 'error';
|
|
847
|
+
error: string;
|
|
848
|
+
};
|
|
849
|
+
/**
|
|
850
|
+
* File metadata from streaming
|
|
851
|
+
*/
|
|
852
|
+
interface FileMetadata {
|
|
853
|
+
mimeType: string;
|
|
854
|
+
size: number;
|
|
855
|
+
isBinary: boolean;
|
|
856
|
+
encoding: 'utf-8' | 'base64';
|
|
857
|
+
}
|
|
858
|
+
/**
|
|
859
|
+
* File stream chunk - either string (text) or Uint8Array (binary, auto-decoded)
|
|
860
|
+
*/
|
|
861
|
+
type FileChunk = string | Uint8Array;
|
|
862
|
+
/**
|
|
863
|
+
* Options for watching a directory.
|
|
864
|
+
*
|
|
865
|
+
* `watch()` resolves only after the watcher is established on the filesystem.
|
|
866
|
+
* The returned SSE stream can be consumed with `parseSSEStream()`.
|
|
867
|
+
*/
|
|
868
|
+
interface WatchOptions {
|
|
869
|
+
/**
|
|
870
|
+
* Watch subdirectories recursively
|
|
871
|
+
* @default true
|
|
872
|
+
*/
|
|
873
|
+
recursive?: boolean;
|
|
874
|
+
/**
|
|
875
|
+
* Glob patterns to include (e.g., '*.ts', '*.js').
|
|
876
|
+
* If not specified, all files are included.
|
|
877
|
+
* Cannot be used together with `exclude`.
|
|
878
|
+
*/
|
|
879
|
+
include?: string[];
|
|
880
|
+
/**
|
|
881
|
+
* Glob patterns to exclude (e.g., 'node_modules', '.git').
|
|
882
|
+
* Cannot be used together with `include`.
|
|
883
|
+
* @default ['.git', 'node_modules', '.DS_Store']
|
|
884
|
+
*/
|
|
885
|
+
exclude?: string[];
|
|
886
|
+
/**
|
|
887
|
+
* Session to run the watch in.
|
|
888
|
+
* If omitted, the default session is used.
|
|
889
|
+
*/
|
|
890
|
+
sessionId?: string;
|
|
891
|
+
}
|
|
892
|
+
/**
|
|
893
|
+
* Options for checking whether a path changed while disconnected.
|
|
894
|
+
*
|
|
895
|
+
* Pass the `version` returned from a previous `checkChanges()` call to learn
|
|
896
|
+
* whether the path is unchanged, changed, or needs a full resync because the
|
|
897
|
+
* retained change state was reset. Change state lives only for the current
|
|
898
|
+
* container lifetime and may expire while idle.
|
|
899
|
+
*/
|
|
900
|
+
interface CheckChangesOptions extends WatchOptions {
|
|
901
|
+
/**
|
|
902
|
+
* Version returned by a previous `checkChanges()` call.
|
|
903
|
+
*/
|
|
904
|
+
since?: string;
|
|
905
|
+
}
|
|
906
|
+
/**
|
|
907
|
+
* @internal SSE event types for container communication
|
|
908
|
+
*/
|
|
909
|
+
type FileWatchEventType = 'create' | 'modify' | 'delete' | 'move_from' | 'move_to' | 'attrib';
|
|
910
|
+
/**
|
|
911
|
+
* @internal Request body for starting a file watch
|
|
912
|
+
*/
|
|
913
|
+
interface WatchRequest {
|
|
914
|
+
path: string;
|
|
915
|
+
recursive?: boolean;
|
|
916
|
+
events?: FileWatchEventType[];
|
|
917
|
+
include?: string[];
|
|
918
|
+
exclude?: string[];
|
|
919
|
+
sessionId?: string;
|
|
920
|
+
}
|
|
921
|
+
/**
|
|
922
|
+
* @internal Request body for checking retained change state.
|
|
923
|
+
*/
|
|
924
|
+
interface CheckChangesRequest extends WatchRequest {
|
|
925
|
+
since?: string;
|
|
926
|
+
}
|
|
927
|
+
/**
|
|
928
|
+
* SSE events emitted by `sandbox.watch()`.
|
|
929
|
+
*/
|
|
930
|
+
type FileWatchSSEEvent = {
|
|
931
|
+
type: 'watching';
|
|
932
|
+
path: string;
|
|
933
|
+
watchId: string;
|
|
934
|
+
} | {
|
|
935
|
+
type: 'event';
|
|
936
|
+
eventType: FileWatchEventType;
|
|
937
|
+
path: string;
|
|
938
|
+
isDirectory: boolean;
|
|
939
|
+
timestamp: string;
|
|
940
|
+
} | {
|
|
941
|
+
type: 'error';
|
|
942
|
+
error: string;
|
|
943
|
+
} | {
|
|
944
|
+
type: 'stopped';
|
|
945
|
+
reason: string;
|
|
946
|
+
};
|
|
947
|
+
/**
|
|
948
|
+
* Result returned by `checkChanges()`.
|
|
949
|
+
*/
|
|
950
|
+
type CheckChangesResult = {
|
|
951
|
+
success: true;
|
|
952
|
+
status: 'unchanged' | 'changed';
|
|
953
|
+
version: string;
|
|
954
|
+
timestamp: string;
|
|
955
|
+
} | {
|
|
956
|
+
success: true;
|
|
957
|
+
status: 'resync';
|
|
958
|
+
reason: 'expired' | 'restarted';
|
|
959
|
+
version: string;
|
|
960
|
+
timestamp: string;
|
|
961
|
+
};
|
|
962
|
+
interface ProcessStartResult {
|
|
963
|
+
success: boolean;
|
|
964
|
+
processId: string;
|
|
965
|
+
pid?: number;
|
|
966
|
+
command: string;
|
|
967
|
+
timestamp: string;
|
|
968
|
+
}
|
|
969
|
+
interface ProcessListResult {
|
|
970
|
+
success: boolean;
|
|
971
|
+
processes: Array<{
|
|
972
|
+
id: string;
|
|
973
|
+
pid?: number;
|
|
974
|
+
command: string;
|
|
975
|
+
status: ProcessStatus;
|
|
976
|
+
startTime: string;
|
|
977
|
+
endTime?: string;
|
|
978
|
+
exitCode?: number;
|
|
979
|
+
stdout?: SandboxOutputMode;
|
|
980
|
+
stderr?: SandboxStderrMode;
|
|
981
|
+
}>;
|
|
982
|
+
timestamp: string;
|
|
983
|
+
}
|
|
984
|
+
interface ProcessInfoResult {
|
|
985
|
+
success: boolean;
|
|
986
|
+
process: {
|
|
987
|
+
id: string;
|
|
988
|
+
pid?: number;
|
|
989
|
+
command: string;
|
|
990
|
+
status: ProcessStatus;
|
|
991
|
+
startTime: string;
|
|
992
|
+
endTime?: string;
|
|
993
|
+
exitCode?: number;
|
|
994
|
+
stdout?: SandboxOutputMode;
|
|
995
|
+
stderr?: SandboxStderrMode;
|
|
996
|
+
};
|
|
997
|
+
timestamp: string;
|
|
998
|
+
}
|
|
999
|
+
interface ProcessKillResult {
|
|
1000
|
+
success: boolean;
|
|
1001
|
+
processId: string;
|
|
1002
|
+
signal?: string;
|
|
1003
|
+
timestamp: string;
|
|
1004
|
+
}
|
|
1005
|
+
interface ProcessLogsResult {
|
|
1006
|
+
success: boolean;
|
|
1007
|
+
processId: string;
|
|
1008
|
+
stdout: string;
|
|
1009
|
+
stderr: string;
|
|
1010
|
+
timestamp: string;
|
|
1011
|
+
}
|
|
1012
|
+
interface ProcessCleanupResult {
|
|
1013
|
+
success: boolean;
|
|
1014
|
+
message?: string;
|
|
1015
|
+
killedCount?: number;
|
|
1016
|
+
cleanedCount: number;
|
|
1017
|
+
timestamp: string;
|
|
1018
|
+
}
|
|
1019
|
+
interface SessionDeleteResult {
|
|
1020
|
+
success: boolean;
|
|
1021
|
+
sessionId: string;
|
|
1022
|
+
timestamp: string;
|
|
1023
|
+
}
|
|
1024
|
+
interface ExecutionSession {
|
|
1025
|
+
/** Unique session identifier */
|
|
1026
|
+
readonly id: string;
|
|
1027
|
+
/**
|
|
1028
|
+
* Session-scoped exec primitive. See `ISandbox.exec`.
|
|
1029
|
+
*
|
|
1030
|
+
* Caller-provided `options.sessionId` is ignored — this wrapper pins
|
|
1031
|
+
* execution to the session that produced it.
|
|
1032
|
+
*/
|
|
1033
|
+
exec(command: string | string[], options?: SandboxExecOptions): SandboxProcessPromise;
|
|
1034
|
+
/** Session-scoped buffered execution helper. See `ISandbox.run`. */
|
|
1035
|
+
run(command: string, options?: ExecOptions): Promise<ExecResult>;
|
|
1036
|
+
getProcess(id: string): Promise<SandboxProcess | null>;
|
|
1037
|
+
listProcesses(): Promise<SandboxProcess[]>;
|
|
1038
|
+
killAllProcesses(): Promise<number>;
|
|
1039
|
+
cleanupCompletedProcesses(): Promise<number>;
|
|
1040
|
+
writeFile(path: string, content: string | ReadableStream<Uint8Array>, options?: {
|
|
1041
|
+
encoding?: string;
|
|
1042
|
+
}): Promise<WriteFileResult>;
|
|
1043
|
+
readFile(path: string, options: {
|
|
1044
|
+
encoding: 'none';
|
|
1045
|
+
}): Promise<ReadFileStreamResult>;
|
|
1046
|
+
readFile(path: string, options?: {
|
|
1047
|
+
encoding?: Exclude<FileEncoding, 'none'>;
|
|
1048
|
+
}): Promise<ReadFileResult>;
|
|
1049
|
+
readFileStream(path: string): Promise<ReadableStream<Uint8Array>>;
|
|
1050
|
+
watch(path: string, options?: Omit<WatchOptions, 'sessionId'>): Promise<ReadableStream<Uint8Array>>;
|
|
1051
|
+
checkChanges(path: string, options?: Omit<CheckChangesOptions, 'sessionId'>): Promise<CheckChangesResult>;
|
|
1052
|
+
mkdir(path: string, options?: {
|
|
1053
|
+
recursive?: boolean;
|
|
1054
|
+
}): Promise<MkdirResult>;
|
|
1055
|
+
deleteFile(path: string): Promise<DeleteFileResult>;
|
|
1056
|
+
renameFile(oldPath: string, newPath: string): Promise<RenameFileResult>;
|
|
1057
|
+
moveFile(sourcePath: string, destinationPath: string): Promise<MoveFileResult>;
|
|
1058
|
+
listFiles(path: string, options?: ListFilesOptions): Promise<ListFilesResult>;
|
|
1059
|
+
exists(path: string): Promise<FileExistsResult>;
|
|
1060
|
+
setEnvVars(envVars: Record<string, string | undefined>): Promise<void>;
|
|
1061
|
+
mountBucket(bucket: string, mountPath: string, options: MountBucketOptions): Promise<void>;
|
|
1062
|
+
unmountBucket(mountPath: string): Promise<void>;
|
|
1063
|
+
createBackup(options: BackupOptions): Promise<DirectoryBackup>;
|
|
1064
|
+
restoreBackup(backup: DirectoryBackup): Promise<RestoreBackupResult>;
|
|
1065
|
+
terminal(request: Request, options?: PtyOptions): Promise<Response>;
|
|
1066
|
+
}
|
|
1067
|
+
/**
|
|
1068
|
+
* Options for creating a directory backup
|
|
1069
|
+
*/
|
|
1070
|
+
interface BackupCompressionOptions {
|
|
1071
|
+
format?: 'gzip' | 'lz4' | 'zstd';
|
|
1072
|
+
threads?: number;
|
|
1073
|
+
}
|
|
1074
|
+
interface BackupOptions {
|
|
1075
|
+
/** Directory to back up. Must be absolute and under `/workspace`, `/home`, `/tmp`, `/var/tmp`, or `/app`. */
|
|
1076
|
+
dir: string;
|
|
1077
|
+
/** Human-readable name for this backup. Optional. */
|
|
1078
|
+
name?: string;
|
|
1079
|
+
/** Seconds until automatic garbage collection. Default: 259200 (3 days). No upper limit. */
|
|
1080
|
+
ttl?: number;
|
|
1081
|
+
/**
|
|
1082
|
+
* Respect git ignore rules for the backup directory when it is inside a git repository.
|
|
1083
|
+
*
|
|
1084
|
+
* Default: false.
|
|
1085
|
+
* If the directory is not inside a git repository, no git-based exclusions are applied.
|
|
1086
|
+
* If git is not installed in the container, a warning is logged and gitignore rules are skipped.
|
|
1087
|
+
*/
|
|
1088
|
+
gitignore?: boolean;
|
|
1089
|
+
/**
|
|
1090
|
+
* Glob patterns to exclude from the backup.
|
|
1091
|
+
* These are passed directly to mksquashfs as wildcard exclude patterns.
|
|
1092
|
+
*
|
|
1093
|
+
* @example ['node_modules', '*.log', '.cache']
|
|
1094
|
+
*/
|
|
1095
|
+
excludes?: string[];
|
|
1096
|
+
/**
|
|
1097
|
+
* Use local R2 binding for backup storage instead of presigned URLs.
|
|
1098
|
+
* Required for local development where presigned URLs and FUSE are unavailable.
|
|
1099
|
+
* When true, the DO resolves BACKUP_BUCKET from its own env as an R2 binding.
|
|
1100
|
+
*/
|
|
1101
|
+
localBucket?: boolean;
|
|
1102
|
+
compression?: BackupCompressionOptions;
|
|
1103
|
+
/**
|
|
1104
|
+
* Use parallel multipart upload to R2 for large archives.
|
|
1105
|
+
* Significantly speeds up uploads for archives over 10 MiB.
|
|
1106
|
+
* Default: true.
|
|
1107
|
+
*/
|
|
1108
|
+
multipart?: boolean;
|
|
1109
|
+
}
|
|
1110
|
+
/**
|
|
1111
|
+
* Handle representing a stored directory backup.
|
|
1112
|
+
* Serializable metadata returned by createBackup().
|
|
1113
|
+
* Store it anywhere and later pass it to restoreBackup().
|
|
1114
|
+
*/
|
|
1115
|
+
interface DirectoryBackup {
|
|
1116
|
+
/** Unique backup identifier */
|
|
1117
|
+
readonly id: string;
|
|
1118
|
+
/** Directory to restore into. Must be under `/workspace`, `/home`, `/tmp`, `/var/tmp`, or `/app`. */
|
|
1119
|
+
readonly dir: string;
|
|
1120
|
+
/** Whether this backup was created with local R2 binding mode. */
|
|
1121
|
+
readonly localBucket?: boolean;
|
|
1122
|
+
}
|
|
1123
|
+
/**
|
|
1124
|
+
* Result returned from a successful restoreBackup() call
|
|
1125
|
+
*/
|
|
1126
|
+
interface RestoreBackupResult {
|
|
1127
|
+
success: boolean;
|
|
1128
|
+
/** The directory that was restored */
|
|
1129
|
+
dir: string;
|
|
1130
|
+
/** Backup ID that was restored */
|
|
1131
|
+
id: string;
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
* Supported S3-compatible storage providers
|
|
1135
|
+
*/
|
|
1136
|
+
type BucketProvider = 'r2' | 's3' | 'gcs';
|
|
1137
|
+
/**
|
|
1138
|
+
* Credentials for S3-compatible storage
|
|
1139
|
+
*/
|
|
1140
|
+
interface BucketCredentials {
|
|
1141
|
+
accessKeyId: string;
|
|
1142
|
+
secretAccessKey: string;
|
|
1143
|
+
}
|
|
1144
|
+
/**
|
|
1145
|
+
* Options for mounting an S3-compatible bucket via s3fs-FUSE (production)
|
|
1146
|
+
*/
|
|
1147
|
+
interface RemoteMountBucketOptions {
|
|
1148
|
+
/**
|
|
1149
|
+
* S3-compatible endpoint URL
|
|
1150
|
+
*
|
|
1151
|
+
* Examples:
|
|
1152
|
+
* - R2: 'https://abc123.r2.cloudflarestorage.com'
|
|
1153
|
+
* - AWS S3: 'https://s3.us-west-2.amazonaws.com'
|
|
1154
|
+
* - GCS: 'https://storage.googleapis.com'
|
|
1155
|
+
*/
|
|
1156
|
+
endpoint: string;
|
|
1157
|
+
/**
|
|
1158
|
+
* Optional provider hint for automatic s3fs flag configuration
|
|
1159
|
+
* If not specified, will attempt to detect from endpoint URL.
|
|
1160
|
+
*
|
|
1161
|
+
* Examples:
|
|
1162
|
+
* - 'r2' - Cloudflare R2 (adds nomixupload)
|
|
1163
|
+
* - 's3' - Amazon S3 (standard configuration)
|
|
1164
|
+
* - 'gcs' - Google Cloud Storage (no special flags needed)
|
|
1165
|
+
*/
|
|
1166
|
+
provider?: BucketProvider;
|
|
1167
|
+
/**
|
|
1168
|
+
* Explicit credentials (overrides env var auto-detection)
|
|
1169
|
+
*/
|
|
1170
|
+
credentials?: BucketCredentials;
|
|
1171
|
+
/**
|
|
1172
|
+
* Mount filesystem as read-only
|
|
1173
|
+
* Default: false
|
|
1174
|
+
*/
|
|
1175
|
+
readOnly?: boolean;
|
|
1176
|
+
/**
|
|
1177
|
+
* Advanced: Override or extend s3fs options
|
|
1178
|
+
*
|
|
1179
|
+
* These will be merged with provider-specific defaults.
|
|
1180
|
+
* To override defaults completely, specify all options here.
|
|
1181
|
+
*
|
|
1182
|
+
* Common options:
|
|
1183
|
+
* - 'use_path_request_style' - Use path-style URLs (bucket/path vs bucket.host/path)
|
|
1184
|
+
* - 'nomixupload' - Disable mixed multipart uploads (needed for some providers)
|
|
1185
|
+
* - 'nomultipart' - Disable all multipart operations
|
|
1186
|
+
* - 'sigv2' - Use signature version 2 instead of v4
|
|
1187
|
+
* - 'no_check_certificate' - Skip SSL certificate validation (dev/testing only)
|
|
1188
|
+
*/
|
|
1189
|
+
s3fsOptions?: string[];
|
|
1190
|
+
/**
|
|
1191
|
+
* Optional prefix/subdirectory within the bucket to mount.
|
|
1192
|
+
*
|
|
1193
|
+
* When specified, only the contents under this prefix are visible at the
|
|
1194
|
+
* mount point, scoping the mount to a subdirectory of the bucket.
|
|
1195
|
+
*
|
|
1196
|
+
* Must start with '/' (e.g., '/workspaces/project123' or '/data/uploads/')
|
|
1197
|
+
*/
|
|
1198
|
+
prefix?: string;
|
|
1199
|
+
/**
|
|
1200
|
+
* Keep real credentials in the Durable Object; write dummy credentials into
|
|
1201
|
+
* the container-side s3fs password file. Outbound s3fs requests are
|
|
1202
|
+
* intercepted by the DO, signed with real credentials, and forwarded to the
|
|
1203
|
+
* configured endpoint.
|
|
1204
|
+
*
|
|
1205
|
+
* Default: false (real credentials are written into the container)
|
|
1206
|
+
*/
|
|
1207
|
+
credentialProxy?: boolean;
|
|
1208
|
+
}
|
|
1209
|
+
/**
|
|
1210
|
+
* Options for mounting a local R2 binding via bidirectional sync (local dev)
|
|
1211
|
+
*
|
|
1212
|
+
* Used during local development with `wrangler dev`. The Durable Object
|
|
1213
|
+
* resolves the R2 binding from its own env using the `bucket` parameter
|
|
1214
|
+
* and syncs files via polling instead of s3fs-FUSE.
|
|
1215
|
+
*/
|
|
1216
|
+
interface LocalMountBucketOptions {
|
|
1217
|
+
/**
|
|
1218
|
+
* Must be true to indicate local R2 binding mode.
|
|
1219
|
+
*/
|
|
1220
|
+
localBucket: true;
|
|
1221
|
+
/**
|
|
1222
|
+
* Optional prefix/subdirectory within the bucket to sync.
|
|
1223
|
+
*
|
|
1224
|
+
* When specified, only the contents under this prefix will be visible
|
|
1225
|
+
* at the mount point.
|
|
1226
|
+
*/
|
|
1227
|
+
prefix?: string;
|
|
1228
|
+
/**
|
|
1229
|
+
* Mount filesystem as read-only
|
|
1230
|
+
* Default: false
|
|
1231
|
+
*/
|
|
1232
|
+
readOnly?: boolean;
|
|
1233
|
+
}
|
|
1234
|
+
/**
|
|
1235
|
+
* Options for mounting an R2 binding via credential-less egress interception.
|
|
1236
|
+
*/
|
|
1237
|
+
interface R2BindingMountBucketOptions {
|
|
1238
|
+
/**
|
|
1239
|
+
* Must not be set — distinguishes this variant from RemoteMountBucketOptions.
|
|
1240
|
+
*/
|
|
1241
|
+
endpoint?: never;
|
|
1242
|
+
/**
|
|
1243
|
+
* Optional prefix/subdirectory within the bucket to mount.
|
|
1244
|
+
*
|
|
1245
|
+
* When specified, only the contents under this prefix will be visible
|
|
1246
|
+
* at the mount point.
|
|
1247
|
+
*/
|
|
1248
|
+
prefix?: string;
|
|
1249
|
+
/**
|
|
1250
|
+
* Mount filesystem as read-only
|
|
1251
|
+
* Default: false
|
|
1252
|
+
*/
|
|
1253
|
+
readOnly?: boolean;
|
|
1254
|
+
/**
|
|
1255
|
+
* Advanced: Override or extend s3fs options.
|
|
1256
|
+
* Provider defaults for R2 are still applied automatically.
|
|
1257
|
+
*/
|
|
1258
|
+
s3fsOptions?: string[];
|
|
1259
|
+
}
|
|
1260
|
+
/**
|
|
1261
|
+
* Options for mounting a bucket — remote (s3fs-FUSE), local (R2 binding sync),
|
|
1262
|
+
* or R2 egress (credential-less s3fs via egress interception).
|
|
1263
|
+
*/
|
|
1264
|
+
type MountBucketOptions = RemoteMountBucketOptions | LocalMountBucketOptions | R2BindingMountBucketOptions;
|
|
1265
|
+
interface ISandbox {
|
|
1266
|
+
/**
|
|
1267
|
+
* Execute a command in the sandbox.
|
|
1268
|
+
*
|
|
1269
|
+
* Mirrors the `ctx.container.exec()` contract from the Cloudflare
|
|
1270
|
+
* Containers runtime, with sandbox-specific extensions for sessions,
|
|
1271
|
+
* named process ids, and re-attach.
|
|
1272
|
+
*
|
|
1273
|
+
* Returns a `SandboxProcessPromise` — a thenable that resolves to a
|
|
1274
|
+
* `SandboxProcess` (`stdin` / `stdout` / `stderr` / `exitCode` /
|
|
1275
|
+
* `kill()` / `output()` plus sandbox extensions) and exposes
|
|
1276
|
+
* `.output()` / `.text()` / `.json()` / `.kill()` directly for the
|
|
1277
|
+
* common "run a command" case:
|
|
1278
|
+
*
|
|
1279
|
+
* @example
|
|
1280
|
+
* // Buffered result
|
|
1281
|
+
* const { stdout } = await sandbox.exec("npm test").output();
|
|
1282
|
+
*
|
|
1283
|
+
* @example
|
|
1284
|
+
* // Long-running, with re-attach by id
|
|
1285
|
+
* const dev = await sandbox.exec("npm run dev", { processId: "dev" });
|
|
1286
|
+
* await dev.waitForPort(3000);
|
|
1287
|
+
*
|
|
1288
|
+
* @example
|
|
1289
|
+
* // Stream stdout straight to the client
|
|
1290
|
+
* const proc = await sandbox.exec(["python", "train.py"]);
|
|
1291
|
+
* return new Response(proc.stdout);
|
|
1292
|
+
*/
|
|
1293
|
+
exec(command: string | string[], options?: SandboxExecOptions): SandboxProcessPromise;
|
|
1294
|
+
/**
|
|
1295
|
+
* Buffered, session-state-preserving execution helper.
|
|
1296
|
+
*
|
|
1297
|
+
* Uses the foreground session execution path, so shell state changes
|
|
1298
|
+
* (`cd`, aliases, functions, exports) persist across calls. Prefer
|
|
1299
|
+
* `exec()` for the containers-compatible process handle; use `run()`
|
|
1300
|
+
* when you explicitly want buffered session-shell semantics.
|
|
1301
|
+
*/
|
|
1302
|
+
run(command: string, options?: ExecOptions & {
|
|
1303
|
+
sessionId?: string;
|
|
1304
|
+
}): Promise<ExecResult>;
|
|
1305
|
+
/**
|
|
1306
|
+
* Re-attach to an existing process by id.
|
|
1307
|
+
*
|
|
1308
|
+
* The returned `SandboxProcess`'s `stdin` is `null` and `stdout`/`stderr`
|
|
1309
|
+
* are replay-then-tail streams sourced from the per-process log file.
|
|
1310
|
+
* Mirrors `ExecProcess` ownership semantics where a non-owner has
|
|
1311
|
+
* nullable pipes.
|
|
1312
|
+
*
|
|
1313
|
+
* Returns `null` if no such process exists.
|
|
1314
|
+
*/
|
|
1315
|
+
getProcess(id: string, options?: ProcessQueryOptions): Promise<SandboxProcess | null>;
|
|
1316
|
+
/**
|
|
1317
|
+
* Snapshot of all known processes.
|
|
1318
|
+
*
|
|
1319
|
+
* Snapshot entries have `stdin`/`stdout`/`stderr` set to `null`; call
|
|
1320
|
+
* `getProcess(id)` to acquire a re-attached handle with live streams.
|
|
1321
|
+
*/
|
|
1322
|
+
listProcesses(options?: ProcessQueryOptions): Promise<SandboxProcess[]>;
|
|
1323
|
+
killAllProcesses(): Promise<number>;
|
|
1324
|
+
cleanupCompletedProcesses(): Promise<number>;
|
|
1325
|
+
writeFile(path: string, content: string | ReadableStream<Uint8Array>, options?: {
|
|
1326
|
+
encoding?: string;
|
|
1327
|
+
}): Promise<WriteFileResult>;
|
|
1328
|
+
readFile(path: string, options: {
|
|
1329
|
+
encoding: 'none';
|
|
1330
|
+
}): Promise<ReadFileStreamResult>;
|
|
1331
|
+
readFile(path: string, options?: {
|
|
1332
|
+
encoding?: Exclude<FileEncoding, 'none'>;
|
|
1333
|
+
}): Promise<ReadFileResult>;
|
|
1334
|
+
readFileStream(path: string): Promise<ReadableStream<Uint8Array>>;
|
|
1335
|
+
watch(path: string, options?: WatchOptions): Promise<ReadableStream<Uint8Array>>;
|
|
1336
|
+
checkChanges(path: string, options?: CheckChangesOptions): Promise<CheckChangesResult>;
|
|
1337
|
+
mkdir(path: string, options?: {
|
|
1338
|
+
recursive?: boolean;
|
|
1339
|
+
}): Promise<MkdirResult>;
|
|
1340
|
+
deleteFile(path: string): Promise<DeleteFileResult>;
|
|
1341
|
+
renameFile(oldPath: string, newPath: string): Promise<RenameFileResult>;
|
|
1342
|
+
moveFile(sourcePath: string, destinationPath: string): Promise<MoveFileResult>;
|
|
1343
|
+
listFiles(path: string, options?: ListFilesOptions): Promise<ListFilesResult>;
|
|
1344
|
+
exists(path: string, options?: {
|
|
1345
|
+
sessionId?: string;
|
|
1346
|
+
}): Promise<FileExistsResult>;
|
|
1347
|
+
setEnvVars(envVars: Record<string, string | undefined>): Promise<void>;
|
|
1348
|
+
mountBucket(bucket: string, mountPath: string, options: MountBucketOptions): Promise<void>;
|
|
1349
|
+
unmountBucket(mountPath: string): Promise<void>;
|
|
1350
|
+
createSession(options?: SessionOptions): Promise<ExecutionSession>;
|
|
1351
|
+
deleteSession(sessionId: string): Promise<SessionDeleteResult>;
|
|
1352
|
+
/**
|
|
1353
|
+
* Returns the Cloudflare placement ID observed during the most recent
|
|
1354
|
+
* session-create handshake with the container. `null` when the container
|
|
1355
|
+
* does not expose `CLOUDFLARE_PLACEMENT_ID` (local development). `undefined`
|
|
1356
|
+
* when no handshake has been observed yet on this sandbox.
|
|
1357
|
+
*/
|
|
1358
|
+
getContainerPlacementId(): Promise<string | null | undefined>;
|
|
1359
|
+
createBackup(options: BackupOptions): Promise<DirectoryBackup>;
|
|
1360
|
+
restoreBackup(backup: DirectoryBackup): Promise<RestoreBackupResult>;
|
|
1361
|
+
wsConnect(request: Request, port: number): Promise<Response>;
|
|
1362
|
+
}
|
|
1363
|
+
declare function isExecResult(value: any): value is ExecResult;
|
|
1364
|
+
declare function isProcess(value: any): value is Process;
|
|
1365
|
+
declare function isProcessStatus(value: string): value is ProcessStatus;
|
|
1366
|
+
//#endregion
|
|
1367
|
+
//#region ../shared/dist/request-types.d.ts
|
|
1368
|
+
/**
|
|
1369
|
+
* Result for a single uploaded part (ETag returned by R2).
|
|
1370
|
+
*/
|
|
1371
|
+
interface UploadedPart {
|
|
1372
|
+
partNumber: number;
|
|
1373
|
+
etag: string;
|
|
1374
|
+
}
|
|
1375
|
+
/**
|
|
1376
|
+
* Response after the container has uploaded all parts.
|
|
1377
|
+
*/
|
|
1378
|
+
interface UploadPartsResponse {
|
|
1379
|
+
success: boolean;
|
|
1380
|
+
parts: UploadedPart[];
|
|
1381
|
+
}
|
|
1382
|
+
/**
|
|
1383
|
+
* Response from the container after creating a backup archive
|
|
1384
|
+
*/
|
|
1385
|
+
interface CreateBackupResponse {
|
|
1386
|
+
success: boolean;
|
|
1387
|
+
/** Size of the archive in bytes */
|
|
1388
|
+
sizeBytes: number;
|
|
1389
|
+
/** Path to the archive file in the container */
|
|
1390
|
+
archivePath: string;
|
|
1391
|
+
}
|
|
1392
|
+
/**
|
|
1393
|
+
* Response from the container after restoring a backup
|
|
1394
|
+
*/
|
|
1395
|
+
interface RestoreBackupResponse {
|
|
1396
|
+
success: boolean;
|
|
1397
|
+
/** Directory that was restored */
|
|
1398
|
+
dir: string;
|
|
1399
|
+
}
|
|
1400
|
+
//#endregion
|
|
1401
|
+
//#region ../shared/dist/rpc-types.d.ts
|
|
1402
|
+
interface SandboxAPI {
|
|
1403
|
+
commands: SandboxCommandsAPI;
|
|
1404
|
+
files: SandboxFilesAPI;
|
|
1405
|
+
processes: SandboxProcessesAPI;
|
|
1406
|
+
ports: SandboxPortsAPI;
|
|
1407
|
+
utils: SandboxUtilsAPI;
|
|
1408
|
+
backup: SandboxBackupAPI;
|
|
1409
|
+
watch: SandboxWatchAPI;
|
|
1410
|
+
tunnels: SandboxTunnelsAPI;
|
|
1411
|
+
terminals: SandboxTerminalsAPI;
|
|
1412
|
+
extensions: SandboxExtensionsAPI;
|
|
1413
|
+
}
|
|
1414
|
+
interface CommandExecuteOptions {
|
|
1415
|
+
sessionId?: string;
|
|
1416
|
+
timeoutMs?: number;
|
|
1417
|
+
env?: Record<string, string | undefined>;
|
|
1418
|
+
cwd?: string;
|
|
1419
|
+
origin?: 'user' | 'internal';
|
|
1420
|
+
}
|
|
1421
|
+
interface SandboxCommandsAPI {
|
|
1422
|
+
execute(command: string, options?: CommandExecuteOptions): Promise<{
|
|
1423
|
+
success: boolean;
|
|
1424
|
+
exitCode: number;
|
|
1425
|
+
stdout: string;
|
|
1426
|
+
stderr: string;
|
|
1427
|
+
command: string;
|
|
1428
|
+
timestamp: string;
|
|
1429
|
+
}>;
|
|
1430
|
+
}
|
|
1431
|
+
interface FileSessionOptions {
|
|
1432
|
+
sessionId?: string;
|
|
1433
|
+
}
|
|
1434
|
+
interface ReadFileStreamOptions extends FileSessionOptions {}
|
|
1435
|
+
interface ReadFileBinaryOptions extends FileSessionOptions {
|
|
1436
|
+
encoding: 'none';
|
|
1437
|
+
}
|
|
1438
|
+
interface ReadFileOptions extends FileSessionOptions {
|
|
1439
|
+
encoding?: Exclude<FileEncoding, 'none'>;
|
|
1440
|
+
}
|
|
1441
|
+
interface WriteFileOptions extends FileSessionOptions {
|
|
1442
|
+
encoding?: string;
|
|
1443
|
+
permissions?: string;
|
|
1444
|
+
}
|
|
1445
|
+
interface MkdirOptions extends FileSessionOptions {
|
|
1446
|
+
recursive?: boolean;
|
|
1447
|
+
}
|
|
1448
|
+
interface SandboxFilesAPI {
|
|
1449
|
+
readFile(path: string, options: ReadFileBinaryOptions): Promise<ReadFileStreamResult>;
|
|
1450
|
+
readFile(path: string, options?: ReadFileOptions): Promise<ReadFileResult>;
|
|
1451
|
+
readFileStream(path: string, options?: ReadFileStreamOptions): Promise<ReadableStream<Uint8Array>>;
|
|
1452
|
+
writeFile(path: string, content: string, options?: WriteFileOptions): Promise<WriteFileResult>;
|
|
1453
|
+
writeFileStream(path: string, stream: ReadableStream<Uint8Array>, options?: FileSessionOptions): Promise<{
|
|
1454
|
+
success: boolean;
|
|
1455
|
+
path: string;
|
|
1456
|
+
bytesWritten: number;
|
|
1457
|
+
timestamp: string;
|
|
1458
|
+
}>;
|
|
1459
|
+
deleteFile(path: string, options?: FileSessionOptions): Promise<DeleteFileResult>;
|
|
1460
|
+
renameFile(oldPath: string, newPath: string, options?: FileSessionOptions): Promise<RenameFileResult>;
|
|
1461
|
+
moveFile(sourcePath: string, destinationPath: string, options?: FileSessionOptions): Promise<MoveFileResult>;
|
|
1462
|
+
mkdir(path: string, options?: MkdirOptions): Promise<MkdirResult>;
|
|
1463
|
+
listFiles(path: string, options?: ListFilesOptions): Promise<ListFilesResult>;
|
|
1464
|
+
exists(path: string, options?: FileSessionOptions): Promise<FileExistsResult>;
|
|
1465
|
+
}
|
|
1466
|
+
interface ProcessStartOptions {
|
|
1467
|
+
sessionId?: string;
|
|
1468
|
+
processId?: string;
|
|
1469
|
+
timeoutMs?: number;
|
|
1470
|
+
env?: Record<string, string | undefined>;
|
|
1471
|
+
cwd?: string;
|
|
1472
|
+
encoding?: string;
|
|
1473
|
+
autoCleanup?: boolean;
|
|
1474
|
+
origin?: 'user' | 'internal';
|
|
1475
|
+
stdout?: 'pipe' | 'ignore';
|
|
1476
|
+
stderr?: 'pipe' | 'ignore' | 'combined';
|
|
1477
|
+
}
|
|
1478
|
+
interface SandboxProcessesAPI {
|
|
1479
|
+
startProcess(command: string, options?: ProcessStartOptions, stdin?: ReadableStream<Uint8Array>): Promise<ProcessStartResult>;
|
|
1480
|
+
listProcesses(): Promise<ProcessListResult>;
|
|
1481
|
+
getProcess(id: string): Promise<ProcessInfoResult>;
|
|
1482
|
+
killProcess(id: string, signal?: number): Promise<ProcessKillResult>;
|
|
1483
|
+
killAllProcesses(): Promise<ProcessCleanupResult>;
|
|
1484
|
+
getProcessLogs(id: string): Promise<ProcessLogsResult>;
|
|
1485
|
+
streamProcessLogs(id: string): Promise<ReadableStream<Uint8Array>>;
|
|
1486
|
+
}
|
|
1487
|
+
interface SandboxPortsAPI {
|
|
1488
|
+
watchPort(request: PortWatchRequest): Promise<ReadableStream<Uint8Array>>;
|
|
1489
|
+
}
|
|
1490
|
+
interface SessionCreateOptions {
|
|
1491
|
+
id: string;
|
|
1492
|
+
env?: Record<string, string | undefined>;
|
|
1493
|
+
cwd?: string;
|
|
1494
|
+
commandTimeoutMs?: number;
|
|
1495
|
+
}
|
|
1496
|
+
interface SandboxUtilsAPI {
|
|
1497
|
+
ping(): Promise<string>;
|
|
1498
|
+
getVersion(): Promise<string>;
|
|
1499
|
+
getCommands(): Promise<string[]>;
|
|
1500
|
+
createSession(options: SessionCreateOptions): Promise<{
|
|
1501
|
+
success: boolean;
|
|
1502
|
+
id: string;
|
|
1503
|
+
message: string;
|
|
1504
|
+
timestamp: string;
|
|
1505
|
+
containerPlacementId?: string | null;
|
|
1506
|
+
}>;
|
|
1507
|
+
deleteSession(sessionId: string): Promise<{
|
|
1508
|
+
success: boolean;
|
|
1509
|
+
sessionId: string;
|
|
1510
|
+
timestamp: string;
|
|
1511
|
+
}>;
|
|
1512
|
+
listSessions(): Promise<{
|
|
1513
|
+
sessions: string[];
|
|
1514
|
+
}>;
|
|
1515
|
+
}
|
|
1516
|
+
interface BackupCreateArchiveOptions {
|
|
1517
|
+
sessionId?: string;
|
|
1518
|
+
excludes?: string[];
|
|
1519
|
+
gitignore?: boolean;
|
|
1520
|
+
compression?: BackupCompressionOptions;
|
|
1521
|
+
}
|
|
1522
|
+
interface BackupRestoreArchiveOptions {
|
|
1523
|
+
sessionId?: string;
|
|
1524
|
+
}
|
|
1525
|
+
interface SandboxBackupAPI {
|
|
1526
|
+
createArchive(dir: string, archivePath: string, options?: BackupCreateArchiveOptions): Promise<CreateBackupResponse>;
|
|
1527
|
+
restoreArchive(dir: string, archivePath: string, options?: BackupRestoreArchiveOptions): Promise<RestoreBackupResponse>;
|
|
1528
|
+
uploadParts(request: {
|
|
1529
|
+
archivePath: string;
|
|
1530
|
+
parts: Array<{
|
|
1531
|
+
partNumber: number;
|
|
1532
|
+
url: string;
|
|
1533
|
+
offset: number;
|
|
1534
|
+
size: number;
|
|
1535
|
+
}>;
|
|
1536
|
+
sessionId?: string;
|
|
1537
|
+
}): Promise<UploadPartsResponse>;
|
|
1538
|
+
}
|
|
1539
|
+
interface SandboxWatchAPI {
|
|
1540
|
+
watch(request: WatchRequest): Promise<ReadableStream<Uint8Array>>;
|
|
1541
|
+
checkChanges(request: CheckChangesRequest): Promise<CheckChangesResult>;
|
|
1542
|
+
}
|
|
1543
|
+
/**
|
|
1544
|
+
* Public-facing tunnel record. Discriminated on the presence of `name`:
|
|
1545
|
+
* quick tunnels (`*.trycloudflare.com`) omit it, named tunnels carry the
|
|
1546
|
+
* label that was passed to `get(port, { name })`.
|
|
1547
|
+
*/
|
|
1548
|
+
type TunnelInfo = QuickTunnelInfo | NamedTunnelInfo;
|
|
1549
|
+
interface QuickTunnelInfo {
|
|
1550
|
+
id: string;
|
|
1551
|
+
port: number;
|
|
1552
|
+
/** `https://<random>.trycloudflare.com`. */
|
|
1553
|
+
url: string;
|
|
1554
|
+
/** Hostname portion of `url`. */
|
|
1555
|
+
hostname: string;
|
|
1556
|
+
createdAt: string;
|
|
1557
|
+
/** Absent on quick tunnels; narrows the union. */
|
|
1558
|
+
name?: never;
|
|
1559
|
+
}
|
|
1560
|
+
interface NamedTunnelInfo {
|
|
1561
|
+
/** Cloudflare tunnel UUID (8-4-4-4-12). */
|
|
1562
|
+
id: string;
|
|
1563
|
+
port: number;
|
|
1564
|
+
/** `https://<hostname>`. */
|
|
1565
|
+
url: string;
|
|
1566
|
+
/** Full hostname bound to the tunnel (without scheme). */
|
|
1567
|
+
hostname: string;
|
|
1568
|
+
createdAt: string;
|
|
1569
|
+
/** Label originally passed via `TunnelOptions.name`. */
|
|
1570
|
+
name: string;
|
|
1571
|
+
}
|
|
1572
|
+
/**
|
|
1573
|
+
* Options accepted by `sandbox.tunnels.get(port, options)`. Omitting
|
|
1574
|
+
* `name` (or omitting the options object) selects the zero-config quick
|
|
1575
|
+
* tunnel; setting `name` selects the named-tunnel flow.
|
|
1576
|
+
*/
|
|
1577
|
+
interface TunnelOptions {
|
|
1578
|
+
/**
|
|
1579
|
+
* Single DNS label under the configured zone. The full hostname is
|
|
1580
|
+
* `<name>.<zone-name>`. See `validateTunnelName` for the format rules.
|
|
1581
|
+
*/
|
|
1582
|
+
name?: string;
|
|
1583
|
+
}
|
|
1584
|
+
type TunnelRunMode = 'quick' | 'named';
|
|
1585
|
+
interface TunnelRunIdentity {
|
|
1586
|
+
tunnelId: string;
|
|
1587
|
+
runId: string;
|
|
1588
|
+
}
|
|
1589
|
+
type TunnelRunRef = TunnelRunIdentity;
|
|
1590
|
+
interface EnsureQuickTunnelRunRequest extends TunnelRunIdentity {
|
|
1591
|
+
mode: 'quick';
|
|
1592
|
+
port: number;
|
|
1593
|
+
readyTimeoutMs?: number;
|
|
1594
|
+
stopGraceMs?: number;
|
|
1595
|
+
}
|
|
1596
|
+
interface EnsureNamedTunnelRunRequest extends TunnelRunIdentity {
|
|
1597
|
+
mode: 'named';
|
|
1598
|
+
port: number;
|
|
1599
|
+
cloudflaredToken: string;
|
|
1600
|
+
readyTimeoutMs?: number;
|
|
1601
|
+
stopGraceMs?: number;
|
|
1602
|
+
}
|
|
1603
|
+
type EnsureTunnelRunRequest = EnsureQuickTunnelRunRequest | EnsureNamedTunnelRunRequest;
|
|
1604
|
+
interface QuickTunnelRunSnapshot extends TunnelRunIdentity {
|
|
1605
|
+
mode: 'quick';
|
|
1606
|
+
port: number;
|
|
1607
|
+
url: string;
|
|
1608
|
+
hostname: string;
|
|
1609
|
+
startedAt: string;
|
|
1610
|
+
}
|
|
1611
|
+
interface NamedTunnelRunSnapshot extends TunnelRunIdentity {
|
|
1612
|
+
mode: 'named';
|
|
1613
|
+
port: number;
|
|
1614
|
+
startedAt: string;
|
|
1615
|
+
}
|
|
1616
|
+
type TunnelRunSnapshot = QuickTunnelRunSnapshot | NamedTunnelRunSnapshot;
|
|
1617
|
+
interface EnsureTunnelRunResult {
|
|
1618
|
+
run: TunnelRunSnapshot;
|
|
1619
|
+
started: boolean;
|
|
1620
|
+
}
|
|
1621
|
+
type StopTunnelRunRequest = TunnelRunRef;
|
|
1622
|
+
interface StopTunnelRunResult {
|
|
1623
|
+
stopped: boolean;
|
|
1624
|
+
}
|
|
1625
|
+
interface TunnelRunExitEvent extends TunnelRunIdentity {
|
|
1626
|
+
mode: TunnelRunMode;
|
|
1627
|
+
port: number;
|
|
1628
|
+
exitCode: number | null;
|
|
1629
|
+
}
|
|
1630
|
+
interface SandboxTerminalsAPI {
|
|
1631
|
+
createTerminal(options: TerminalCreateOptions): Promise<{
|
|
1632
|
+
success: true;
|
|
1633
|
+
id: string;
|
|
1634
|
+
}>;
|
|
1635
|
+
destroyTerminal(id: string): Promise<{
|
|
1636
|
+
success: true;
|
|
1637
|
+
id: string;
|
|
1638
|
+
}>;
|
|
1639
|
+
}
|
|
1640
|
+
interface SandboxTunnelsAPI {
|
|
1641
|
+
/** Ensure a runtime-local cloudflared process for a DO-issued run id. */
|
|
1642
|
+
ensureTunnelRun(request: EnsureTunnelRunRequest): Promise<EnsureTunnelRunResult>;
|
|
1643
|
+
/** Stop the matching runtime-local cloudflared process. */
|
|
1644
|
+
stopTunnelRun(request: StopTunnelRunRequest): Promise<StopTunnelRunResult>;
|
|
1645
|
+
}
|
|
1646
|
+
/**
|
|
1647
|
+
* Author-facing description of a sidecar extension. An extension is an
|
|
1648
|
+
* npm-style package: an `.tgz` whose embedded `package.json` declares the
|
|
1649
|
+
* sidecar entrypoint (via `bin`) and any extension metadata (under the
|
|
1650
|
+
* `sandboxExtension` key).
|
|
1651
|
+
*
|
|
1652
|
+
* The SDK ships the tarball bytes; the container hashes them, provisions a
|
|
1653
|
+
* dedicated directory keyed by content hash, derives identity from the
|
|
1654
|
+
* embedded `package.json`, installs the package with `bun add`, and spawns
|
|
1655
|
+
* the declared bin under Bun.
|
|
1656
|
+
*
|
|
1657
|
+
* `ExtensionPackage` carries tarball bytes produced by an extension build.
|
|
1658
|
+
* The SDK sends those bytes only when the container has not provisioned the
|
|
1659
|
+
* package hash yet.
|
|
1660
|
+
*/
|
|
1661
|
+
interface ExtensionPackage {
|
|
1662
|
+
/** Raw `.tgz` bytes. */
|
|
1663
|
+
tarball: Uint8Array;
|
|
1664
|
+
/**
|
|
1665
|
+
* Bin entry to run when the embedded `package.json` declares more than one.
|
|
1666
|
+
* Defaults to the value of `sandboxExtension.bin` in `package.json`, then
|
|
1667
|
+
* to the single bin entry if there is exactly one.
|
|
1668
|
+
*/
|
|
1669
|
+
bin?: string;
|
|
1670
|
+
/**
|
|
1671
|
+
* Max time to wait for the sidecar to accept a capnweb connection on its
|
|
1672
|
+
* unix socket. Falls back to `sandboxExtension.readinessTimeoutMs` in
|
|
1673
|
+
* `package.json`, then to a host default.
|
|
1674
|
+
*/
|
|
1675
|
+
readinessTimeoutMs?: number;
|
|
1676
|
+
/**
|
|
1677
|
+
* Run lifecycle scripts during `bun add`. Defaults to false — provisioning
|
|
1678
|
+
* happens before the sidecar is supervised, so install-time side effects
|
|
1679
|
+
* are deliberately opt-in.
|
|
1680
|
+
*/
|
|
1681
|
+
allowInstallScripts?: boolean;
|
|
1682
|
+
}
|
|
1683
|
+
/** Health snapshot for a provisioned extension. */
|
|
1684
|
+
interface ExtensionHealth {
|
|
1685
|
+
packageHash: string;
|
|
1686
|
+
id: string;
|
|
1687
|
+
version: string;
|
|
1688
|
+
/** Tarball is on disk and the package.json has been read. */
|
|
1689
|
+
provisioned: boolean;
|
|
1690
|
+
/** Sidecar process is currently running. */
|
|
1691
|
+
running: boolean;
|
|
1692
|
+
pid: number | null;
|
|
1693
|
+
/** Whether a `__ping__` round-tripped over capnweb. */
|
|
1694
|
+
responsive: boolean;
|
|
1695
|
+
}
|
|
1696
|
+
/**
|
|
1697
|
+
* Connect request payload. The SDK hashes the tarball locally and first sends
|
|
1698
|
+
* only the hash. If the current host process has not provisioned that hash,
|
|
1699
|
+
* it responds with an `ExtensionTarballRequired` error and the SDK retries
|
|
1700
|
+
* with `tarball` populated.
|
|
1701
|
+
*/
|
|
1702
|
+
interface ExtensionConnectRequest {
|
|
1703
|
+
packageHash: string;
|
|
1704
|
+
/** Only sent when the host has not seen this hash yet. */
|
|
1705
|
+
tarball?: Uint8Array;
|
|
1706
|
+
bin?: string;
|
|
1707
|
+
readinessTimeoutMs?: number;
|
|
1708
|
+
allowInstallScripts?: boolean;
|
|
1709
|
+
}
|
|
1710
|
+
/**
|
|
1711
|
+
* Control surface for container sidecar extensions.
|
|
1712
|
+
*
|
|
1713
|
+
* `connect` is the only entry point: it provisions on first use, supervises
|
|
1714
|
+
* lazily, and returns the sidecar's capnweb remote main as a stub. Calls on
|
|
1715
|
+
* the stub are proxied through the container's capnweb session to the
|
|
1716
|
+
* sidecar's capnweb session — callback parameters (including streaming
|
|
1717
|
+
* handlers) round-trip across both hops.
|
|
1718
|
+
*/
|
|
1719
|
+
interface SandboxExtensionsAPI {
|
|
1720
|
+
/**
|
|
1721
|
+
* Provision the package (if new) and return the sidecar's typed remote
|
|
1722
|
+
* main. The result is a capnweb stub whose methods correspond to the
|
|
1723
|
+
* sidecar's `RpcTarget` surface.
|
|
1724
|
+
*/
|
|
1725
|
+
connect(req: ExtensionConnectRequest): Promise<unknown>;
|
|
1726
|
+
/** Health snapshot, probing the sidecar with a `__ping__` when running. */
|
|
1727
|
+
health(packageHash: string): Promise<ExtensionHealth>;
|
|
1728
|
+
/** Stop a sidecar and release its capnweb session. */
|
|
1729
|
+
stop(packageHash: string): Promise<void>;
|
|
1730
|
+
}
|
|
1731
|
+
/**
|
|
1732
|
+
* RPC surface the Sandbox DO exposes to the container over the existing
|
|
1733
|
+
* capnweb session. The container reaches it via
|
|
1734
|
+
* `session.getRemoteMain<SandboxControlCallback>()` and invokes methods
|
|
1735
|
+
* to push control-plane events (today: tunnel exits) back to the DO.
|
|
1736
|
+
*
|
|
1737
|
+
* One stable target per sandbox; not per-tunnel. Reusable seam for
|
|
1738
|
+
* future container→DO events.
|
|
1739
|
+
*/
|
|
1740
|
+
interface SandboxControlCallback {
|
|
1741
|
+
/** Called by the container when a runtime-local tunnel run exits. */
|
|
1742
|
+
onTunnelRunExit(event: TunnelRunExitEvent): Promise<void>;
|
|
1743
|
+
}
|
|
1744
|
+
//#endregion
|
|
1745
|
+
//#region src/extensions/index.d.ts
|
|
1746
|
+
interface HTTPAuthHostConfig {
|
|
1747
|
+
token: string;
|
|
1748
|
+
username?: string;
|
|
1749
|
+
type?: 'basic' | 'bearer';
|
|
1750
|
+
}
|
|
1751
|
+
interface HTTPAuthInterceptorParams {
|
|
1752
|
+
hosts: Record<string, HTTPAuthHostConfig>;
|
|
1753
|
+
}
|
|
1754
|
+
/**
|
|
1755
|
+
* The slice of the Sandbox an extension captures: its control `client`, unified
|
|
1756
|
+
* `exec()` surface, and sandbox-level environment variables. Narrow on purpose
|
|
1757
|
+
* \u2014 an extension never holds the whole instance.
|
|
1758
|
+
*
|
|
1759
|
+
* `envVars` mirrors what the Sandbox applies to sessionless execution, so an
|
|
1760
|
+
* extension that drives commands through `client` directly can still honour
|
|
1761
|
+
* sandbox-level env (tokens, proxy settings) without an explicit session.
|
|
1762
|
+
*/
|
|
1763
|
+
type SandboxLike = {
|
|
1764
|
+
readonly client: SandboxAPI;
|
|
1765
|
+
readonly exec?: (command: string | string[], options?: SandboxExecOptions) => SandboxProcessPromise;
|
|
1766
|
+
readonly envVars?: Record<string, string>;
|
|
1767
|
+
registerGitAuthInterceptor?: (params: HTTPAuthInterceptorParams) => Promise<void>;
|
|
1768
|
+
};
|
|
1769
|
+
/**
|
|
1770
|
+
* The one base class for *every* extension.
|
|
1771
|
+
*
|
|
1772
|
+
* - SDK-only: just drives existing sub-APIs.
|
|
1773
|
+
* - Sidecar: pass an {@link ExtensionPackage} to `super(sandbox, pkg)` and
|
|
1774
|
+
* call `this.sidecar<T>()` to get a typed capnweb stub of the sidecar.
|
|
1775
|
+
*
|
|
1776
|
+
* The sidecar accessor throws a clear error if no package was supplied, so an
|
|
1777
|
+
* extension only "becomes" a sidecar extension when it opts in.
|
|
1778
|
+
*
|
|
1779
|
+
* ```ts
|
|
1780
|
+
* // SDK-only
|
|
1781
|
+
* class Git extends SandboxExtension {
|
|
1782
|
+
* constructor(s: SandboxLike) { super(s); }
|
|
1783
|
+
* async status(sid: string) {
|
|
1784
|
+
* return this.exec('git status', { sessionId: sid }).output();
|
|
1785
|
+
* }
|
|
1786
|
+
* }
|
|
1787
|
+
*
|
|
1788
|
+
* // Sidecar
|
|
1789
|
+
* import sidecarTarballBytes from './sidecar-package.tgz';
|
|
1790
|
+
* import type { MyAPI } from './shared';
|
|
1791
|
+
*
|
|
1792
|
+
* class MyExt extends SandboxExtension {
|
|
1793
|
+
* constructor(s: SandboxLike) { super(s, { tarball: new Uint8Array(sidecarTarballBytes) }); }
|
|
1794
|
+
* async run(input: string): Promise<string> {
|
|
1795
|
+
* const api = await this.sidecar<MyAPI>();
|
|
1796
|
+
* return api.run(input);
|
|
1797
|
+
* }
|
|
1798
|
+
* }
|
|
1799
|
+
* ```
|
|
1800
|
+
*
|
|
1801
|
+
* RPC-safety: the sandbox lives in `#sandbox` and is reached only through the
|
|
1802
|
+
* `protected` `client` getter (a prototype accessor, not an own property),
|
|
1803
|
+
* so it is never serialised across RPC. Only the public methods you add form
|
|
1804
|
+
* the extension's RPC surface.
|
|
1805
|
+
*/
|
|
1806
|
+
declare abstract class SandboxExtension extends RpcTarget {
|
|
1807
|
+
#private;
|
|
1808
|
+
protected constructor(sandbox: SandboxLike, pkg?: ExtensionPackage);
|
|
1809
|
+
/** The container control client. Use inside your own methods, lazily. */
|
|
1810
|
+
protected get client(): SandboxAPI;
|
|
1811
|
+
/** Unified process-handle exec surface from the owning Sandbox. */
|
|
1812
|
+
protected exec(command: string | string[], options?: SandboxExecOptions): SandboxProcessPromise;
|
|
1813
|
+
/**
|
|
1814
|
+
* Sandbox-level environment variables. Apply these to sessionless execution
|
|
1815
|
+
* so commands driven straight through `client` still inherit sandbox env
|
|
1816
|
+
* (tokens, proxy settings) the way the Sandbox's own sessionless path does.
|
|
1817
|
+
*/
|
|
1818
|
+
protected get envVars(): Record<string, string>;
|
|
1819
|
+
protected get httpAuthInterceptor(): ((params: HTTPAuthInterceptorParams) => Promise<void>) | undefined;
|
|
1820
|
+
/**
|
|
1821
|
+
* Return the sidecar's capnweb remote main, provisioning + spawning on
|
|
1822
|
+
* demand. `T` is the typed interface the sidecar exposes (its
|
|
1823
|
+
* `SandboxSidecar` subclass shape). Each call reconnects through the host
|
|
1824
|
+
* so a crashed sidecar can be restarted transparently on the next use.
|
|
1825
|
+
*
|
|
1826
|
+
* Streaming is just a method that takes a callback parameter: capnweb
|
|
1827
|
+
* stubs the callback and routes invocations back through the SDK\u2192container
|
|
1828
|
+
* \u2192sidecar hops.
|
|
1829
|
+
*/
|
|
1830
|
+
protected sidecar<T extends object>(): Promise<T>;
|
|
1831
|
+
/** Health snapshot for this extension's sidecar. */
|
|
1832
|
+
protected sidecarHealth(): Promise<ExtensionHealth>;
|
|
1833
|
+
/**
|
|
1834
|
+
* Stop this extension's sidecar. The next `sidecar()` call will respawn on
|
|
1835
|
+
* demand.
|
|
1836
|
+
*/
|
|
1837
|
+
protected stopSidecar(): Promise<void>;
|
|
1838
|
+
}
|
|
1839
|
+
//#endregion
|
|
1840
|
+
export { ReadFileStreamResult as $, ExecEvent as A, ISandbox as B, BaseExecOptions as C, CheckChangesResult as D, CheckChangesOptions as E, FileEncoding as F, MkdirResult as G, ListFilesResult as H, FileExistsResult as I, Process as J, MountBucketOptions as K, FileMetadata as L, ExecResult as M, ExecutionSession as N, DeleteFileResult as O, FileChunk as P, ReadFileResult as Q, FileStreamEvent as R, BackupOptions as S, BucketProvider as T, LocalMountBucketOptions as U, ListFilesOptions as V, LogEvent as W, ProcessQueryOptions as X, ProcessOptions as Y, ProcessStatus as Z, SandboxTunnelsAPI as _, PtyOptions as _t, ExtensionHealth as a, SandboxOptions as at, TunnelInfo as b, TerminalCreateOptions as bt, QuickTunnelInfo as c, SessionDeleteResult as ct, SandboxControlCallback as d, WaitForPortOptions as dt, RemoteMountBucketOptions as et, SandboxExtensionsAPI as f, WatchOptions as ft, SandboxTerminalsAPI as g, isProcessStatus as gt, SandboxProcessesAPI as h, isProcess as ht, SandboxLike as i, SandboxExecOutput as it, ExecOptions as j, DirectoryBackup as k, SandboxBackupAPI as l, SessionOptions as lt, SandboxPortsAPI as m, isExecResult as mt, HTTPAuthInterceptorParams as n, RestoreBackupResult as nt, ExtensionPackage as o, SandboxProcess as ot, SandboxFilesAPI as p, WriteFileResult as pt, MoveFileResult as q, SandboxExtension as r, SandboxExecOptions as rt, NamedTunnelInfo as s, SandboxProcessPromise as st, HTTPAuthHostConfig as t, RenameFileResult as tt, SandboxCommandsAPI as u, WaitForLogResult as ut, SandboxUtilsAPI as v, SandboxTerminal as vt, BucketCredentials as w, TunnelOptions as x, TerminalOptions as xt, SandboxWatchAPI as y, TerminalConnectOptions as yt, FileWatchSSEEvent as z };
|
|
1841
|
+
//# sourceMappingURL=index-B7QgIs0N.d.ts.map
|