@prized/sdk 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +625 -2
- package/dist/cjs/commands.d.ts +168 -0
- package/dist/cjs/commands.js +567 -0
- package/dist/cjs/errors.d.ts +73 -0
- package/dist/cjs/errors.js +258 -0
- package/dist/cjs/files.d.ts +86 -0
- package/dist/cjs/files.js +366 -0
- package/dist/cjs/http.d.ts +210 -0
- package/dist/cjs/http.js +570 -0
- package/dist/cjs/index.d.ts +12 -0
- package/dist/cjs/index.js +22 -0
- package/dist/cjs/outcome.d.ts +75 -0
- package/dist/cjs/outcome.js +167 -0
- package/dist/cjs/output.d.ts +59 -0
- package/dist/cjs/output.js +288 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/sandbox.d.ts +218 -0
- package/dist/cjs/sandbox.js +810 -0
- package/dist/cjs/shell.d.ts +152 -0
- package/dist/cjs/shell.js +393 -0
- package/dist/cjs/toolbox.d.ts +48 -0
- package/dist/cjs/toolbox.js +86 -0
- package/dist/cjs/types.d.ts +99 -0
- package/dist/cjs/types.js +102 -0
- package/dist/cjs/version.d.ts +2 -0
- package/dist/cjs/version.js +5 -0
- package/dist/esm/commands.d.ts +168 -0
- package/dist/esm/commands.js +560 -0
- package/dist/esm/errors.d.ts +73 -0
- package/dist/esm/errors.js +250 -0
- package/dist/esm/files.d.ts +86 -0
- package/dist/esm/files.js +360 -0
- package/dist/esm/http.d.ts +210 -0
- package/dist/esm/http.js +553 -0
- package/dist/esm/index.d.ts +12 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/outcome.d.ts +75 -0
- package/dist/esm/outcome.js +152 -0
- package/dist/esm/output.d.ts +59 -0
- package/dist/esm/output.js +280 -0
- package/dist/esm/sandbox.d.ts +218 -0
- package/dist/esm/sandbox.js +806 -0
- package/dist/esm/shell.d.ts +152 -0
- package/dist/esm/shell.js +379 -0
- package/dist/esm/toolbox.d.ts +48 -0
- package/dist/esm/toolbox.js +81 -0
- package/dist/esm/types.d.ts +99 -0
- package/dist/esm/types.js +95 -0
- package/dist/esm/version.d.ts +2 -0
- package/dist/esm/version.js +2 -0
- package/package.json +53 -4
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { CommandHandle, type ExecOptions } from './commands.js';
|
|
2
|
+
import { Files } from './files.js';
|
|
3
|
+
import { type ClientOptions } from './http.js';
|
|
4
|
+
import { type ExecResult, type Resources, type ResourcesInput, type SandboxInfo, type SandboxState, type Template } from './types.js';
|
|
5
|
+
/** The header a request to a sandbox port's URL carries the API key in. */
|
|
6
|
+
export declare const PREVIEW_KEY_HEADER = "Prized-Key";
|
|
7
|
+
/** The template a create uses when it names none. */
|
|
8
|
+
export declare const DEFAULT_TEMPLATE = "prized-base-v1";
|
|
9
|
+
/** How long create, resume and fork wait for `running` by default. */
|
|
10
|
+
export declare const DEFAULT_START_TIMEOUT_MS = 180000;
|
|
11
|
+
/** How long pause and delete wait by default. */
|
|
12
|
+
export declare const DEFAULT_SETTLE_TIMEOUT_MS = 120000;
|
|
13
|
+
/** Cleanups still running after the call that started them has answered.
|
|
14
|
+
* Not exported from the package; the tests await it. */
|
|
15
|
+
export declare const backgroundCleanups: Set<Promise<void>>;
|
|
16
|
+
/** Edge warm-ups (warmEdge) not yet settled. Not exported from the package;
|
|
17
|
+
* the tests await it. */
|
|
18
|
+
export declare const backgroundWarmups: Set<Promise<void>>;
|
|
19
|
+
export interface CreateOptions extends ClientOptions {
|
|
20
|
+
/** Template id. Default `prized-base-v1`; `Sandbox.templates()` lists them. */
|
|
21
|
+
template?: string;
|
|
22
|
+
/** vCPU, memory and disk. Anything left out takes the template's default. */
|
|
23
|
+
resources?: ResourcesInput;
|
|
24
|
+
/** Environment variables for every command in the sandbox. */
|
|
25
|
+
envVars?: Record<string, string>;
|
|
26
|
+
/** Your own key/value tags (up to 32), filterable in `Sandbox.list`. */
|
|
27
|
+
labels?: Record<string, string>;
|
|
28
|
+
/**
|
|
29
|
+
* Run time in minutes; the sandbox is deleted when it ends. Paid plans:
|
|
30
|
+
* 1 440 (24 hours) by default, up to 7 days (10 080; a custom plan has no
|
|
31
|
+
* maximum), and every resume restarts the clock. The free tier: 60
|
|
32
|
+
* minutes of running at most (paused time does not count). Change it later
|
|
33
|
+
* with `prized sandbox extend` or the API's `extend` and `timeout` routes.
|
|
34
|
+
*/
|
|
35
|
+
maxRuntimeMin?: number;
|
|
36
|
+
/** Pause after this many idle minutes (0: never). */
|
|
37
|
+
autoPauseMin?: number;
|
|
38
|
+
/** Delete this many minutes after pausing (0: never). */
|
|
39
|
+
autoDeleteMin?: number;
|
|
40
|
+
/**
|
|
41
|
+
* How long the create may take, in ms: the request, its retries and
|
|
42
|
+
* (unless `wait: false`) the wait for `running`. Default 180 000.
|
|
43
|
+
* Deleting a sandbox that failed (up to 5 s) comes on top.
|
|
44
|
+
*/
|
|
45
|
+
timeoutMs?: number;
|
|
46
|
+
/**
|
|
47
|
+
* Makes a retried create return the same sandbox instead of a second one.
|
|
48
|
+
* The SDK sends a fresh key per call when you give none, so its own
|
|
49
|
+
* retries are always safe.
|
|
50
|
+
*/
|
|
51
|
+
idempotencyKey?: string;
|
|
52
|
+
/** Resolve as soon as the API accepts the create, without waiting for `running`. */
|
|
53
|
+
wait?: boolean;
|
|
54
|
+
signal?: AbortSignal;
|
|
55
|
+
}
|
|
56
|
+
export interface ListOptions extends ClientOptions {
|
|
57
|
+
/** Only sandboxes carrying every one of these labels. */
|
|
58
|
+
labels?: Record<string, string>;
|
|
59
|
+
/** Only sandboxes in this state (or any of these states). */
|
|
60
|
+
state?: SandboxState | SandboxState[];
|
|
61
|
+
}
|
|
62
|
+
export interface WaitableOptions {
|
|
63
|
+
/** Wait for the change to land. Default true (false for delete). */
|
|
64
|
+
wait?: boolean;
|
|
65
|
+
/**
|
|
66
|
+
* How long the call may take, in ms: the request, its retries and
|
|
67
|
+
* (unless `wait: false`) the wait. Default 180 000 for resume and fork,
|
|
68
|
+
* 120 000 for pause and delete. Deleting a fork's copies that failed
|
|
69
|
+
* comes on top.
|
|
70
|
+
*/
|
|
71
|
+
timeoutMs?: number;
|
|
72
|
+
signal?: AbortSignal;
|
|
73
|
+
}
|
|
74
|
+
export interface ForkOptions extends WaitableOptions {
|
|
75
|
+
/** How many copies. Default 1. */
|
|
76
|
+
count?: number;
|
|
77
|
+
/** Labels for the copies, in place of the parent's. Default: the parent's labels. */
|
|
78
|
+
labels?: Record<string, string>;
|
|
79
|
+
/**
|
|
80
|
+
* Each copy's lifetime in minutes, up to the workspace's maximum. Default:
|
|
81
|
+
* on a paid workspace, the parent's runtime, starting at the fork; on the
|
|
82
|
+
* free tier, copies keep the parent's deletion time (this may only shorten it).
|
|
83
|
+
*/
|
|
84
|
+
maxRuntimeMin?: number;
|
|
85
|
+
/** Makes a retried fork return the same copies. The SDK sends one per call when you give none. */
|
|
86
|
+
idempotencyKey?: string;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* A Prized sandbox: a microVM your code creates, runs commands in, and
|
|
90
|
+
* pauses, resumes, forks and deletes.
|
|
91
|
+
*
|
|
92
|
+
* const sandbox = await Sandbox.create({ template: 'prized-base-v1' });
|
|
93
|
+
* const { stdout } = await sandbox.exec('python3 --version');
|
|
94
|
+
* await sandbox.delete();
|
|
95
|
+
*/
|
|
96
|
+
export declare class Sandbox {
|
|
97
|
+
/** The sandbox id (`sbx_…`). */
|
|
98
|
+
readonly id: string;
|
|
99
|
+
/** Read, write, list, remove, upload and download files. */
|
|
100
|
+
readonly files: Files;
|
|
101
|
+
private readonly config;
|
|
102
|
+
private current;
|
|
103
|
+
private readonly toolbox;
|
|
104
|
+
private constructor();
|
|
105
|
+
/** Everything the API said about this sandbox, as of the last call. */
|
|
106
|
+
get info(): SandboxInfo;
|
|
107
|
+
/** The state as of the last call; `refresh()` reads it fresh. */
|
|
108
|
+
get state(): SandboxState;
|
|
109
|
+
get template(): string | null;
|
|
110
|
+
get resources(): Resources;
|
|
111
|
+
get labels(): Record<string, string>;
|
|
112
|
+
get createdAt(): Date | null;
|
|
113
|
+
get expiresAt(): Date | null;
|
|
114
|
+
get errorReason(): string | null;
|
|
115
|
+
toJSON(): Omit<SandboxInfo, 'raw'>;
|
|
116
|
+
/**
|
|
117
|
+
* Create a sandbox and wait until it is running, ready for commands.
|
|
118
|
+
* If it fails or the wait runs out, the sandbox is deleted and the error
|
|
119
|
+
* thrown. Throws PrizedError: `paid_balance_required`, `insufficient_balance`,
|
|
120
|
+
* `sandbox_quota_exceeded` (see `reason`), `template_not_found`,
|
|
121
|
+
* `invalid_resources`, `sandbox_error`, `timeout`, …
|
|
122
|
+
*/
|
|
123
|
+
static create(options?: CreateOptions): Promise<Sandbox>;
|
|
124
|
+
/** An existing sandbox by id, in whatever state it is in. */
|
|
125
|
+
static connect(id: string, options?: ClientOptions): Promise<Sandbox>;
|
|
126
|
+
/** The workspace's sandboxes (deleted ones excluded), newest first; `labels` is filtered by the API. */
|
|
127
|
+
static list(options?: ListOptions): Promise<Sandbox[]>;
|
|
128
|
+
/**
|
|
129
|
+
* Delete every sandbox carrying all of `labels` (at least one) and resolve
|
|
130
|
+
* with their ids (the API takes up to 250 a request; this repeats it until
|
|
131
|
+
* none are left). Safe to repeat: sandboxes already being
|
|
132
|
+
* deleted are in the answer again, and none matching resolves `[]`. Each
|
|
133
|
+
* counts toward the workspace's quotas until it is gone;
|
|
134
|
+
* `Sandbox.list({ labels })` coming back empty means they all are.
|
|
135
|
+
*/
|
|
136
|
+
static deleteByLabels(labels: Record<string, string>, options?: ClientOptions): Promise<string[]>;
|
|
137
|
+
/** The templates you can create sandboxes from, with their default, minimum and maximum shapes. */
|
|
138
|
+
static templates(options?: ClientOptions): Promise<Template[]>;
|
|
139
|
+
/**
|
|
140
|
+
* Run a command (a shell string, or an argv array the SDK quotes) and
|
|
141
|
+
* return its output. A non-zero exit is not an error: read `exitCode`.
|
|
142
|
+
* With `background: true` it returns a CommandHandle at once.
|
|
143
|
+
*/
|
|
144
|
+
exec(command: string | readonly string[], options: ExecOptions & {
|
|
145
|
+
background: true;
|
|
146
|
+
}): Promise<CommandHandle>;
|
|
147
|
+
exec(command: string | readonly string[], options?: ExecOptions & {
|
|
148
|
+
background?: false;
|
|
149
|
+
}): Promise<ExecResult>;
|
|
150
|
+
exec(command: string | readonly string[], options?: ExecOptions): Promise<ExecResult | CommandHandle>;
|
|
151
|
+
/** Read the sandbox again: `state` and the other fields update. Does not count as activity. */
|
|
152
|
+
refresh(): Promise<this>;
|
|
153
|
+
/**
|
|
154
|
+
* Count as activity now: the `autoPauseMin` idle clock starts over.
|
|
155
|
+
* Commands and file transfers already count; this is for idle stretches.
|
|
156
|
+
*/
|
|
157
|
+
keepAlive(): Promise<void>;
|
|
158
|
+
/**
|
|
159
|
+
* Pause: memory and disk are kept, and only storage is billed. Waits for
|
|
160
|
+
* `paused` unless `wait: false`.
|
|
161
|
+
*/
|
|
162
|
+
pause(options?: WaitableOptions): Promise<void>;
|
|
163
|
+
/** Resume a paused sandbox, with every process where it left off. Waits for `running` unless `wait: false`. */
|
|
164
|
+
resume(options?: WaitableOptions): Promise<void>;
|
|
165
|
+
/**
|
|
166
|
+
* Fork a running or paused sandbox into `count` new sandboxes, each with
|
|
167
|
+
* every process of this one running. Resolves once every copy is running,
|
|
168
|
+
* ready for commands (unless `wait: false`); if any copy fails or the
|
|
169
|
+
* wait runs out, every copy is deleted and the error thrown. The parent is
|
|
170
|
+
* left as it was: running, or paused. `timeoutMs` bounds the request and
|
|
171
|
+
* the wait together.
|
|
172
|
+
*/
|
|
173
|
+
fork(options?: ForkOptions): Promise<Sandbox[]>;
|
|
174
|
+
/**
|
|
175
|
+
* Delete the sandbox and everything in it. Returns once the API accepts;
|
|
176
|
+
* `wait: true` also waits until it is gone (a deleting sandbox still
|
|
177
|
+
* counts toward the workspace's quota). `timeoutMs` (default 120 000)
|
|
178
|
+
* bounds the whole call, its retries and the wait included.
|
|
179
|
+
*/
|
|
180
|
+
delete(options?: WaitableOptions): Promise<void>;
|
|
181
|
+
/**
|
|
182
|
+
* The https URL `port` of the sandbox answers at, for a request that
|
|
183
|
+
* carries a workspace-wide API key with Sandboxes: Write in the
|
|
184
|
+
* `Prized-Key` header (`previewHeaders()`): a web app, an API or a dev
|
|
185
|
+
* server listening on that port in the sandbox on 127.0.0.1 or 0.0.0.0. A
|
|
186
|
+
* request to a paused sandbox resumes it and waits up to 10 seconds, as
|
|
187
|
+
* `exec` does. Nothing is created: the URL is the sandbox's own. Throws
|
|
188
|
+
* `preview_unavailable` when the deployment has sandbox ingress off.
|
|
189
|
+
*/
|
|
190
|
+
previewUrl(port: number): string;
|
|
191
|
+
/** The headers a request to `previewUrl(port)` sends: this client's API key in
|
|
192
|
+
* `Prized-Key`. The header never reaches the app. Send them with
|
|
193
|
+
* `redirect: 'manual'`: fetch resends custom headers to whatever host a
|
|
194
|
+
* redirect names, so only ever send them to the sandbox's preview URL. */
|
|
195
|
+
previewHeaders(): Record<string, string>;
|
|
196
|
+
/** POST the verb, the call and its retries within `deadline`; with `hold`,
|
|
197
|
+
* held by the API until the sandbox is that (each attempt asks only for
|
|
198
|
+
* what is left). */
|
|
199
|
+
private verb;
|
|
200
|
+
/**
|
|
201
|
+
* Poll until the sandbox reaches `want`; throws on error/deleted or the
|
|
202
|
+
* deadline. The first read goes out at once (a held answer that has not
|
|
203
|
+
* settled is usually a breath away), then 100 ms, doubling to
|
|
204
|
+
* pollIntervalMs, and the last just before the deadline (Polls).
|
|
205
|
+
* `reportedMs` is the caller's whole budget, for the error.
|
|
206
|
+
*/
|
|
207
|
+
private waitFor;
|
|
208
|
+
/** Best-effort delete after a failed wait, bounded (as the Python SDK's); never throws. */
|
|
209
|
+
private discard;
|
|
210
|
+
/**
|
|
211
|
+
* Poll the list until every fork copy is running or has failed, by
|
|
212
|
+
* `deadline`: one list call per poll, however many copies, every
|
|
213
|
+
* pollIntervalMs, and the last just before the deadline (Polls),
|
|
214
|
+
* so even a budget shorter than one interval polls once. `reportedMs` is
|
|
215
|
+
* the caller's whole budget, for the error.
|
|
216
|
+
*/
|
|
217
|
+
private static settle;
|
|
218
|
+
}
|