@drej/core 0.2.0 → 0.4.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.mts +497 -0
- package/dist/index.mjs +603 -0
- package/package.json +20 -7
- package/dist/errors.d.ts +0 -31
- package/dist/index.d.ts +0 -10
- package/dist/ledger.d.ts +0 -67
- package/dist/logger.d.ts +0 -23
- package/dist/steps.d.ts +0 -100
- package/dist/validate.d.ts +0 -2
- package/dist/workflow.d.ts +0 -99
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
import { ControlClient, ExecClient, FileInfo, SSEEvent } from "@drej/opensandbox";
|
|
2
|
+
|
|
3
|
+
//#region src/ledger.d.ts
|
|
4
|
+
/** Status of a sandbox session derived from its ledger events. */
|
|
5
|
+
declare enum SandboxStatus {
|
|
6
|
+
Running = "running",
|
|
7
|
+
Completed = "completed"
|
|
8
|
+
}
|
|
9
|
+
/** Derived metadata for a sandbox session, computed from its ledger events. */
|
|
10
|
+
interface SandboxDetails {
|
|
11
|
+
/** User-provided name (or auto-generated). */
|
|
12
|
+
name: string;
|
|
13
|
+
sandboxId: string;
|
|
14
|
+
status: SandboxStatus;
|
|
15
|
+
/** Unix timestamp (ms) of the `sandbox_created` event. */
|
|
16
|
+
startedAt: number;
|
|
17
|
+
/** Unix timestamp (ms) of the `sandbox_closed` event, if the session has ended. */
|
|
18
|
+
completedAt?: number;
|
|
19
|
+
/** Number of exec() calls that completed. */
|
|
20
|
+
execCount: number;
|
|
21
|
+
}
|
|
22
|
+
/** Options for filtering session listings. */
|
|
23
|
+
interface ListSandboxOptions {
|
|
24
|
+
status?: SandboxStatus;
|
|
25
|
+
/** Max number of results to return. */
|
|
26
|
+
limit?: number;
|
|
27
|
+
/** Return only sessions that started before this Unix timestamp (ms). */
|
|
28
|
+
before?: number;
|
|
29
|
+
}
|
|
30
|
+
/** Events emitted during execution and stored in the ledger. */
|
|
31
|
+
declare enum LedgerEvent {
|
|
32
|
+
/** Emitted when a sandbox is created and reaches Running state. */
|
|
33
|
+
SandboxCreated = "sandbox_created",
|
|
34
|
+
/** Emitted at the start of each exec() or execCode() call. */
|
|
35
|
+
ExecStart = "exec_start",
|
|
36
|
+
/** Streaming output chunk from exec() or execCode(). */
|
|
37
|
+
ExecEvent = "exec_event",
|
|
38
|
+
/** Emitted when an exec() or execCode() call completes. */
|
|
39
|
+
ExecComplete = "exec_complete",
|
|
40
|
+
/** Emitted when checkpoint() captures a snapshot. */
|
|
41
|
+
CheckpointCreated = "checkpoint_created",
|
|
42
|
+
/** Emitted when a sandbox is closed. */
|
|
43
|
+
SandboxClosed = "sandbox_closed",
|
|
44
|
+
/** Emitted once when a workflow run starts, before any steps execute. */
|
|
45
|
+
RunStarted = "run_started",
|
|
46
|
+
/** Emitted at the beginning of each step. */
|
|
47
|
+
StepStart = "step_start",
|
|
48
|
+
/** Emitted when a step finishes successfully. */
|
|
49
|
+
StepComplete = "step_complete",
|
|
50
|
+
/** Emitted when a step throws an unrecoverable error. */
|
|
51
|
+
StepFailed = "step_failed",
|
|
52
|
+
/** Emitted when a step's rollback handler completes during saga compensation. */
|
|
53
|
+
StepRolledBack = "step_rolled_back",
|
|
54
|
+
/** Emitted after all steps finish without error. */
|
|
55
|
+
WorkflowComplete = "workflow_complete",
|
|
56
|
+
/** Emitted after rollback completes following a step failure. */
|
|
57
|
+
WorkflowFailed = "workflow_failed",
|
|
58
|
+
/** Durable resumption point written after each successful step. */
|
|
59
|
+
Checkpoint = "checkpoint",
|
|
60
|
+
/** Emitted when a sandbox snapshot is captured mid-run. */
|
|
61
|
+
Snapshot = "snapshot"
|
|
62
|
+
}
|
|
63
|
+
/** A single event record written to the storage adapter during a session. */
|
|
64
|
+
interface LedgerEntry {
|
|
65
|
+
/** Unix timestamp in milliseconds. */
|
|
66
|
+
ts: number;
|
|
67
|
+
/** Sandbox session name. */
|
|
68
|
+
name: string;
|
|
69
|
+
sandboxId: string;
|
|
70
|
+
/** Zero-based index of the step that produced this event. `-1` for session-level events. */
|
|
71
|
+
stepIndex: number;
|
|
72
|
+
/** Parallel branch index, when this step is inside a `parallel()` block. */
|
|
73
|
+
branch?: number;
|
|
74
|
+
event: LedgerEvent;
|
|
75
|
+
/** Event-specific data (step output, snapshot ID, exec text, etc.). */
|
|
76
|
+
payload?: unknown;
|
|
77
|
+
/** Error message when `event` is `StepFailed` or `WorkflowFailed`. */
|
|
78
|
+
error?: string;
|
|
79
|
+
}
|
|
80
|
+
/** A recorded checkpoint for a sandbox session. */
|
|
81
|
+
interface CheckpointInfo {
|
|
82
|
+
/** OpenSandbox snapshot ID. */
|
|
83
|
+
snapshotId: string;
|
|
84
|
+
/** User-supplied name, if provided when calling `sb.checkpoint(name)`. */
|
|
85
|
+
tag?: string;
|
|
86
|
+
/** Unix timestamp (ms) when the checkpoint was created. */
|
|
87
|
+
createdAt: number;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Persisted record for a named, reusable sandbox environment.
|
|
91
|
+
* Written once when the environment is first built; updated on rebuild.
|
|
92
|
+
*/
|
|
93
|
+
interface EnvironmentRecord {
|
|
94
|
+
/** User-provided environment name. */
|
|
95
|
+
name: string;
|
|
96
|
+
/** OpenSandbox snapshot ID to restore from. */
|
|
97
|
+
snapshotId: string;
|
|
98
|
+
/** Raw image URI resolved at build time. */
|
|
99
|
+
image: string;
|
|
100
|
+
/** Unix timestamp (ms) when the environment was last built. */
|
|
101
|
+
builtAt: number;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Persistence interface for session event storage.
|
|
105
|
+
*
|
|
106
|
+
* Implement this interface to plug in any storage backend. drej ships two
|
|
107
|
+
* official implementations: `@drej/sqlite` (local dev, zero infra) and
|
|
108
|
+
* `@drej/postgres` (production).
|
|
109
|
+
*
|
|
110
|
+
* @example
|
|
111
|
+
* ```ts
|
|
112
|
+
* import { SQLiteAdapter } from "@drej/sqlite";
|
|
113
|
+
* const client = new Drej({ baseUrl, adapter: new SQLiteAdapter("./drej.db") });
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
interface IStorageAdapter {
|
|
117
|
+
/** Run migrations / open connections. Must be called before first use. */
|
|
118
|
+
connect?(): Promise<void>;
|
|
119
|
+
/** Release connections and resources. Call on graceful shutdown. */
|
|
120
|
+
close?(): Promise<void>;
|
|
121
|
+
/** Persist a single ledger event. Called automatically during execution. */
|
|
122
|
+
append(entry: LedgerEntry): Promise<void>;
|
|
123
|
+
/** Return all events for a specific session, in ascending timestamp order. */
|
|
124
|
+
readAll(name: string, sandboxId: string): Promise<LedgerEntry[]>;
|
|
125
|
+
/** Return the most recent checkpoint entry for a session, or `null` if none exists. */
|
|
126
|
+
lastCheckpoint(name: string, sandboxId: string): Promise<LedgerEntry | null>;
|
|
127
|
+
/** Return details for all sessions with a given name, newest first. */
|
|
128
|
+
listSandboxDetails(name: string, opts?: ListSandboxOptions): Promise<SandboxDetails[]>;
|
|
129
|
+
/** Return details for sessions across all names, newest first. */
|
|
130
|
+
listAllSandboxDetails(opts?: ListSandboxOptions): Promise<SandboxDetails[]>;
|
|
131
|
+
/** Return details for a single session, or `null` if not found. */
|
|
132
|
+
getSandboxDetails(name: string, sandboxId: string): Promise<SandboxDetails | null>;
|
|
133
|
+
/** Delete all ledger events for a session. */
|
|
134
|
+
deleteSandbox(name: string, sandboxId: string): Promise<void>;
|
|
135
|
+
/** Return all checkpoints for a session in creation order. */
|
|
136
|
+
listCheckpoints(name: string, sandboxId: string): Promise<CheckpointInfo[]>;
|
|
137
|
+
/** Return the cached record for a named environment, or null if not built yet. */
|
|
138
|
+
getEnvironment(name: string): Promise<EnvironmentRecord | null>;
|
|
139
|
+
/** Upsert an environment record after a successful build. */
|
|
140
|
+
saveEnvironment(record: EnvironmentRecord): Promise<void>;
|
|
141
|
+
/** Remove the record for a named environment. Does not delete the server-side snapshot. */
|
|
142
|
+
deleteEnvironment(name: string): Promise<void>;
|
|
143
|
+
/** Return all environment records, newest first. */
|
|
144
|
+
listEnvironments(): Promise<EnvironmentRecord[]>;
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/logger.d.ts
|
|
148
|
+
declare enum LogLevel {
|
|
149
|
+
Debug = 0,
|
|
150
|
+
Info = 1,
|
|
151
|
+
Warn = 2,
|
|
152
|
+
Error = 3,
|
|
153
|
+
Silent = 4
|
|
154
|
+
}
|
|
155
|
+
interface ILogger {
|
|
156
|
+
debug(msg: string, meta?: Record<string, unknown>): void;
|
|
157
|
+
info(msg: string, meta?: Record<string, unknown>): void;
|
|
158
|
+
warn(msg: string, meta?: Record<string, unknown>): void;
|
|
159
|
+
error(msg: string, meta?: Record<string, unknown>): void;
|
|
160
|
+
}
|
|
161
|
+
declare class ConsoleLogger implements ILogger {
|
|
162
|
+
private readonly minLevel;
|
|
163
|
+
constructor(minLevel?: LogLevel);
|
|
164
|
+
private log;
|
|
165
|
+
debug(msg: string, meta?: Record<string, unknown>): void;
|
|
166
|
+
info(msg: string, meta?: Record<string, unknown>): void;
|
|
167
|
+
warn(msg: string, meta?: Record<string, unknown>): void;
|
|
168
|
+
error(msg: string, meta?: Record<string, unknown>): void;
|
|
169
|
+
}
|
|
170
|
+
declare const noopLogger: ILogger;
|
|
171
|
+
//#endregion
|
|
172
|
+
//#region src/exec-handle.d.ts
|
|
173
|
+
interface ExecResult {
|
|
174
|
+
stdout: string;
|
|
175
|
+
stderr: string;
|
|
176
|
+
exitCode: number;
|
|
177
|
+
}
|
|
178
|
+
type ExecDriver = {
|
|
179
|
+
type: "stream";
|
|
180
|
+
gen: AsyncGenerator<SSEEvent>;
|
|
181
|
+
onDone: (r: ExecResult) => Promise<void>;
|
|
182
|
+
} | {
|
|
183
|
+
type: "replay";
|
|
184
|
+
result: ExecResult;
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Returned by `Sandbox.exec()` and `Sandbox.execCode()`.
|
|
188
|
+
*
|
|
189
|
+
* Implements `PromiseLike<ExecResult>` so `await sb.exec(...)` works naturally.
|
|
190
|
+
* Also exposes `pipe()`, `stdout()`, and `result()` for streaming output.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* // simple await
|
|
195
|
+
* const { exitCode } = await sb.exec("npm test");
|
|
196
|
+
*
|
|
197
|
+
* // stream to stdout
|
|
198
|
+
* await sb.exec("npm run build").pipe(process.stdout);
|
|
199
|
+
*
|
|
200
|
+
* // async generator
|
|
201
|
+
* for await (const chunk of sb.exec("npm test").stdout()) process.stdout.write(chunk);
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
declare class ExecHandle implements PromiseLike<ExecResult> {
|
|
205
|
+
private readonly _promise;
|
|
206
|
+
private readonly _chunks;
|
|
207
|
+
private _wakeup;
|
|
208
|
+
private _done;
|
|
209
|
+
private _err;
|
|
210
|
+
private _hasErr;
|
|
211
|
+
constructor(driver: ExecDriver);
|
|
212
|
+
private _drain;
|
|
213
|
+
private _notify;
|
|
214
|
+
then<T, E>(onfulfilled?: ((value: ExecResult) => T | PromiseLike<T>) | null, onrejected?: ((reason: unknown) => E | PromiseLike<E>) | null): Promise<T | E>;
|
|
215
|
+
/** Resolve the full result: `{ stdout, stderr, exitCode }`. */
|
|
216
|
+
result(): Promise<ExecResult>;
|
|
217
|
+
/** Async generator yielding stdout chunks as they arrive. */
|
|
218
|
+
stdout(): AsyncGenerator<string>;
|
|
219
|
+
/** Pipe stdout to any writable with a `write(chunk: string)` method. */
|
|
220
|
+
pipe(writable: {
|
|
221
|
+
write(chunk: string): unknown;
|
|
222
|
+
}): Promise<void>;
|
|
223
|
+
}
|
|
224
|
+
//#endregion
|
|
225
|
+
//#region src/sandbox.d.ts
|
|
226
|
+
interface ExecOptions {
|
|
227
|
+
/** Working directory inside the sandbox. */
|
|
228
|
+
cwd?: string;
|
|
229
|
+
/** Environment variables to set for this exec. */
|
|
230
|
+
env?: Record<string, string>;
|
|
231
|
+
/** Abort the command after this many milliseconds. */
|
|
232
|
+
timeoutMs?: number;
|
|
233
|
+
/**
|
|
234
|
+
* Throw `CommandError` if the command exits with a non-zero code.
|
|
235
|
+
* Defaults to `true`.
|
|
236
|
+
*/
|
|
237
|
+
strict?: boolean;
|
|
238
|
+
/**
|
|
239
|
+
* Path to the shell binary used to execute the command.
|
|
240
|
+
* Overrides the sandbox-level default set in `SandboxOptions.shell`.
|
|
241
|
+
* Defaults to `"/bin/sh"`.
|
|
242
|
+
*/
|
|
243
|
+
shell?: string;
|
|
244
|
+
}
|
|
245
|
+
interface ExecCodeOptions {
|
|
246
|
+
/** Execution context (stateful interpreter session). */
|
|
247
|
+
context?: {
|
|
248
|
+
id: string;
|
|
249
|
+
language: import("@drej/opensandbox").CodeLanguage;
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
/** Lifecycle hooks for observability. Pass via `SandboxDeps.hooks`. */
|
|
253
|
+
interface SandboxHooks {
|
|
254
|
+
onSandboxCreated?(sandboxId: string, name: string): void;
|
|
255
|
+
onExecStart?(sandboxId: string, seq: number, cmd: string): void;
|
|
256
|
+
onExecComplete?(sandboxId: string, seq: number, result: ExecResult): void;
|
|
257
|
+
onCheckpoint?(sandboxId: string, snapshotId: string, name?: string): void;
|
|
258
|
+
onSandboxClosed?(sandboxId: string): void;
|
|
259
|
+
onSandboxFailed?(sandboxId: string, error: Error): void;
|
|
260
|
+
}
|
|
261
|
+
/** Internal dependencies injected by `DrejClient`. */
|
|
262
|
+
interface SandboxDeps {
|
|
263
|
+
control: ControlClient;
|
|
264
|
+
adapter: IStorageAdapter;
|
|
265
|
+
hooks?: SandboxHooks;
|
|
266
|
+
/** Called when `close()` completes — used by `Drej` for concurrency accounting. */
|
|
267
|
+
onClose?: () => void;
|
|
268
|
+
/** Default shell for all `exec()` calls on this sandbox. Defaults to `"/bin/sh"`. */
|
|
269
|
+
shell?: string;
|
|
270
|
+
/** Called by `fork()` to create a new Sandbox from a snapshot — injected by `Drej`. */
|
|
271
|
+
fork?: (snapshotId: string, tag?: string) => Promise<Sandbox>;
|
|
272
|
+
/** Route execd and proxy calls through the OpenSandbox server. Required when the server runs in Docker. */
|
|
273
|
+
useServerProxy?: boolean;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Resolve an ExecClient for a sandbox. Calls getEndpoint once (each call
|
|
277
|
+
* returns a different ephemeral proxy port) then polls listContexts until
|
|
278
|
+
* execd is ready to accept connections.
|
|
279
|
+
*/
|
|
280
|
+
declare function resolveExecClient(control: ControlClient, sandboxId: string, useServerProxy?: boolean, retries?: number, delayMs?: number): Promise<ExecClient>;
|
|
281
|
+
/**
|
|
282
|
+
* A live sandbox container. Returned by `DrejClient.sandbox()` and `DrejClient.resume()`.
|
|
283
|
+
*
|
|
284
|
+
* Call `exec()` to run commands, `checkpoint()` to snapshot state, and `close()`
|
|
285
|
+
* when done. Multiple sandboxes can be held simultaneously — just assign to
|
|
286
|
+
* different variables.
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* ```ts
|
|
290
|
+
* const sb = await client.sandbox({ image: "node:22", resources: { cpu: "500m", memory: "256Mi" } });
|
|
291
|
+
* await sb.exec("npm ci");
|
|
292
|
+
* await sb.checkpoint();
|
|
293
|
+
* await sb.exec("npm test").pipe(process.stdout);
|
|
294
|
+
* await sb.close();
|
|
295
|
+
* ```
|
|
296
|
+
*/
|
|
297
|
+
declare class Sandbox {
|
|
298
|
+
/** OpenSandbox container ID — also the unique ledger key for this session. */
|
|
299
|
+
readonly sandboxId: string;
|
|
300
|
+
/** User-provided name (or auto-generated). */
|
|
301
|
+
readonly name: string;
|
|
302
|
+
private readonly _deps;
|
|
303
|
+
/** Cached exec results for replay mode (populated by DrejClient.resume()). */
|
|
304
|
+
private readonly _replayCache;
|
|
305
|
+
private _execClient;
|
|
306
|
+
private _seq;
|
|
307
|
+
private _closed;
|
|
308
|
+
constructor(sandboxId: string, name: string, deps: SandboxDeps, replayCache?: Map<number, ExecResult>);
|
|
309
|
+
private _getExecClient;
|
|
310
|
+
private _emit;
|
|
311
|
+
/**
|
|
312
|
+
* Execute a shell command inside the sandbox.
|
|
313
|
+
*
|
|
314
|
+
* Returns an `ExecHandle` — await it for the result, call `.pipe()` to stream
|
|
315
|
+
* stdout, or use `.stdout()` as an async generator. Every call is logged to
|
|
316
|
+
* the ledger; replayed execs in a resumed sandbox return cached output
|
|
317
|
+
* instantly without re-running.
|
|
318
|
+
*
|
|
319
|
+
* @example
|
|
320
|
+
* ```ts
|
|
321
|
+
* const { exitCode } = await sb.exec("npm test");
|
|
322
|
+
* await sb.exec("npm run build").pipe(process.stdout);
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
325
|
+
exec(cmd: string, opts?: ExecOptions): ExecHandle;
|
|
326
|
+
/**
|
|
327
|
+
* Create a code execution context for use with `execCode()`.
|
|
328
|
+
*
|
|
329
|
+
* The code interpreter requires a context for every `execCode()` call. Call
|
|
330
|
+
* this once per session (or once per isolated scope), then pass the returned
|
|
331
|
+
* object as `opts.context` to `execCode()`. Variables defined in one call
|
|
332
|
+
* are visible to subsequent calls sharing the same context.
|
|
333
|
+
*
|
|
334
|
+
* @example
|
|
335
|
+
* ```ts
|
|
336
|
+
* const ctx = await sb.createCodeContext(CodeLanguage.Python);
|
|
337
|
+
* await sb.execCode('x = 42', { context: ctx });
|
|
338
|
+
* await sb.execCode('print(x)', { context: ctx }); // prints 42
|
|
339
|
+
* ```
|
|
340
|
+
*/
|
|
341
|
+
createCodeContext(language: import("@drej/opensandbox").CodeLanguage): Promise<import("@drej/opensandbox").CodeContext>;
|
|
342
|
+
/**
|
|
343
|
+
* Execute code via the sandbox's code interpreter (Python, JS, etc.).
|
|
344
|
+
*
|
|
345
|
+
* Uses the execd `/code` endpoint for stateful, Jupyter-style execution.
|
|
346
|
+
* Contexts persist across calls — use `opts.context` to target a specific one.
|
|
347
|
+
* Create a context first with `sb.createCodeContext(language)`.
|
|
348
|
+
*/
|
|
349
|
+
execCode(code: string, opts?: ExecCodeOptions): ExecHandle;
|
|
350
|
+
/** Write a file into the sandbox. */
|
|
351
|
+
writeFile(path: string, content: string): Promise<void>;
|
|
352
|
+
/** Read a file from the sandbox as a UTF-8 string. */
|
|
353
|
+
readFile(path: string): Promise<string>;
|
|
354
|
+
/** Delete a file from the sandbox. */
|
|
355
|
+
deleteFile(path: string): Promise<void>;
|
|
356
|
+
/** Move or rename a file inside the sandbox. */
|
|
357
|
+
moveFile(from: string, to: string): Promise<void>;
|
|
358
|
+
/** List files in a directory inside the sandbox. */
|
|
359
|
+
listDirectory(path: string, opts?: {
|
|
360
|
+
depth?: number;
|
|
361
|
+
}): Promise<import("@drej/opensandbox").FileInfo[]>;
|
|
362
|
+
/** Search for files matching a glob pattern inside the sandbox. */
|
|
363
|
+
searchFiles(pattern: string, path?: string): Promise<string[]>;
|
|
364
|
+
/** Create a directory (and parents) inside the sandbox. */
|
|
365
|
+
createDirectory(path: string): Promise<void>;
|
|
366
|
+
/** Delete a directory inside the sandbox. */
|
|
367
|
+
deleteDirectory(path: string): Promise<void>;
|
|
368
|
+
/** Return metadata for a file or directory (size, type, mode, timestamps). */
|
|
369
|
+
getFileInfo(path: string): Promise<import("@drej/opensandbox").FileInfo>;
|
|
370
|
+
/**
|
|
371
|
+
* Replace substrings in one or more files inside the sandbox.
|
|
372
|
+
*
|
|
373
|
+
* More efficient than `readFile` → string replace → `writeFile` for targeted edits.
|
|
374
|
+
*
|
|
375
|
+
* @example
|
|
376
|
+
* ```ts
|
|
377
|
+
* await sb.replaceInFiles([{ path: "/app/config.json", old: "localhost", new: "0.0.0.0" }]);
|
|
378
|
+
* ```
|
|
379
|
+
*/
|
|
380
|
+
replaceInFiles(replacements: Array<{
|
|
381
|
+
path: string;
|
|
382
|
+
old: string;
|
|
383
|
+
new: string;
|
|
384
|
+
}>): Promise<void>;
|
|
385
|
+
/**
|
|
386
|
+
* Copy a file from this sandbox into another sandbox.
|
|
387
|
+
*
|
|
388
|
+
* Reads the file as a UTF-8 string and writes it to the same path on the target.
|
|
389
|
+
* Use this to move results between a fork and its origin, or between parallel sandboxes.
|
|
390
|
+
*
|
|
391
|
+
* @example
|
|
392
|
+
* ```ts
|
|
393
|
+
* await sb.transfer("/app/output.json", fork);
|
|
394
|
+
* ```
|
|
395
|
+
*/
|
|
396
|
+
transfer(path: string, target: Sandbox): Promise<void>;
|
|
397
|
+
/**
|
|
398
|
+
* Return a proxied URL and auth headers for a port inside the sandbox.
|
|
399
|
+
*
|
|
400
|
+
* Use this to send HTTP requests to a server running inside the sandbox.
|
|
401
|
+
*
|
|
402
|
+
* @example
|
|
403
|
+
* ```ts
|
|
404
|
+
* await sb.exec("node server.js &");
|
|
405
|
+
* const { url, headers } = await sb.proxy(3000);
|
|
406
|
+
* const res = await fetch(`${url}/health`, { headers });
|
|
407
|
+
* ```
|
|
408
|
+
*/
|
|
409
|
+
proxy(port: number): Promise<{
|
|
410
|
+
url: string;
|
|
411
|
+
headers: Record<string, string>;
|
|
412
|
+
}>;
|
|
413
|
+
/** Return current CPU and memory usage for this sandbox. */
|
|
414
|
+
metrics(): Promise<{
|
|
415
|
+
cpu: number;
|
|
416
|
+
memory: number;
|
|
417
|
+
timestamp: string;
|
|
418
|
+
}>;
|
|
419
|
+
/** Return all checkpoints for this sandbox in creation order. */
|
|
420
|
+
listCheckpoints(): Promise<CheckpointInfo[]>;
|
|
421
|
+
/**
|
|
422
|
+
* Snapshot the current sandbox and return a new independent `Sandbox` from that state.
|
|
423
|
+
*
|
|
424
|
+
* The original sandbox keeps running. Both operate on separate containers restored
|
|
425
|
+
* from the same snapshot. Equivalent to `checkpoint()` followed by `resume()` on a
|
|
426
|
+
* clone, but without closing the original.
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
* ```ts
|
|
430
|
+
* await sb.exec("npm ci");
|
|
431
|
+
* const fork = await sb.fork("after-install");
|
|
432
|
+
*
|
|
433
|
+
* await sb.exec("npm test"); // runs on original
|
|
434
|
+
* await fork.exec("npm run build"); // runs on fork
|
|
435
|
+
* ```
|
|
436
|
+
*/
|
|
437
|
+
fork(tag?: string): Promise<Sandbox>;
|
|
438
|
+
/**
|
|
439
|
+
* Capture a snapshot of the sandbox's current filesystem state.
|
|
440
|
+
*
|
|
441
|
+
* Writes a `checkpoint_created` event to the ledger with the snapshot ID.
|
|
442
|
+
* Use `DrejClient.resume(sandboxId)` to restore from the latest checkpoint.
|
|
443
|
+
*/
|
|
444
|
+
checkpoint(name?: string): Promise<void>;
|
|
445
|
+
private _waitForSnapshot;
|
|
446
|
+
/**
|
|
447
|
+
* Delete the sandbox container and release its resources.
|
|
448
|
+
*
|
|
449
|
+
* Always call `close()` when done — even on error — to avoid leaking containers.
|
|
450
|
+
* Idempotent: subsequent calls are no-ops.
|
|
451
|
+
*/
|
|
452
|
+
close(): Promise<void>;
|
|
453
|
+
}
|
|
454
|
+
//#endregion
|
|
455
|
+
//#region src/errors.d.ts
|
|
456
|
+
/** Base class for all drej workflow runtime errors. */
|
|
457
|
+
declare class WorkflowError extends Error {
|
|
458
|
+
constructor(message: string);
|
|
459
|
+
}
|
|
460
|
+
/** Thrown when a sandbox fails to create, boot, or reach `Running` state. */
|
|
461
|
+
declare class SandboxError extends WorkflowError {
|
|
462
|
+
/** The sandbox ID, if one was assigned before the failure. */
|
|
463
|
+
readonly sandboxId?: string | undefined;
|
|
464
|
+
constructor(message: string, /** The sandbox ID, if one was assigned before the failure. */
|
|
465
|
+
|
|
466
|
+
sandboxId?: string | undefined);
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Thrown when execd inside a sandbox never becomes ready within the retry window.
|
|
470
|
+
* The sandbox is `Running` from the control plane's perspective, but the exec
|
|
471
|
+
* daemon is not accepting connections.
|
|
472
|
+
*/
|
|
473
|
+
declare class ExecConnectionError extends WorkflowError {
|
|
474
|
+
readonly sandboxId: string;
|
|
475
|
+
constructor(sandboxId: string);
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Thrown when an `exec()` step exits with a non-zero exit code and the
|
|
479
|
+
* `strict` option is enabled. Carries the exit code and original command.
|
|
480
|
+
*/
|
|
481
|
+
declare class CommandError extends WorkflowError {
|
|
482
|
+
readonly exitCode: number;
|
|
483
|
+
readonly command: string;
|
|
484
|
+
readonly sandboxId: string;
|
|
485
|
+
constructor(exitCode: number, command: string, sandboxId: string);
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Thrown when a step exceeds its `timeoutMs` limit (either set per-step or
|
|
489
|
+
* via `RunOptions.stepTimeoutMs`). The workflow will roll back after this error.
|
|
490
|
+
*/
|
|
491
|
+
declare class StepTimeoutError extends WorkflowError {
|
|
492
|
+
readonly stepId: string;
|
|
493
|
+
readonly timeoutMs: number;
|
|
494
|
+
constructor(stepId: string, timeoutMs: number);
|
|
495
|
+
}
|
|
496
|
+
//#endregion
|
|
497
|
+
export { type CheckpointInfo, CommandError, ConsoleLogger, type EnvironmentRecord, type ExecCodeOptions, ExecConnectionError, ExecHandle, type ExecOptions, type ExecResult, type FileInfo, type ILogger, type IStorageAdapter, type LedgerEntry, LedgerEvent, type ListSandboxOptions, LogLevel, Sandbox, type SandboxDeps, type SandboxDetails, SandboxError, type SandboxHooks, SandboxStatus, StepTimeoutError, WorkflowError, noopLogger, resolveExecClient };
|