@prized/sdk 0.0.0-stage → 0.1.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/LICENSE +202 -0
- package/README.md +666 -2
- package/dist/cjs/commands.d.ts +199 -0
- package/dist/cjs/commands.js +597 -0
- package/dist/cjs/errors.d.ts +113 -0
- package/dist/cjs/errors.js +306 -0
- package/dist/cjs/files.d.ts +101 -0
- package/dist/cjs/files.js +387 -0
- package/dist/cjs/http.d.ts +226 -0
- package/dist/cjs/http.js +618 -0
- package/dist/cjs/index.d.ts +12 -0
- package/dist/cjs/index.js +23 -0
- package/dist/cjs/outcome.d.ts +76 -0
- package/dist/cjs/outcome.js +170 -0
- package/dist/cjs/output.d.ts +70 -0
- package/dist/cjs/output.js +383 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/sandbox.d.ts +218 -0
- package/dist/cjs/sandbox.js +836 -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 +88 -0
- package/dist/cjs/types.d.ts +111 -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 +199 -0
- package/dist/esm/commands.js +590 -0
- package/dist/esm/errors.d.ts +113 -0
- package/dist/esm/errors.js +297 -0
- package/dist/esm/files.d.ts +101 -0
- package/dist/esm/files.js +381 -0
- package/dist/esm/http.d.ts +226 -0
- package/dist/esm/http.js +601 -0
- package/dist/esm/index.d.ts +12 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/outcome.d.ts +76 -0
- package/dist/esm/outcome.js +155 -0
- package/dist/esm/output.d.ts +70 -0
- package/dist/esm/output.js +374 -0
- package/dist/esm/sandbox.d.ts +218 -0
- package/dist/esm/sandbox.js +832 -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 +83 -0
- package/dist/esm/types.d.ts +111 -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,111 @@
|
|
|
1
|
+
/** A sandbox's lifecycle state. */
|
|
2
|
+
export type SandboxState = 'starting' | 'running' | 'pausing' | 'paused' | 'resizing' | 'deleting' | 'deleted' | 'error';
|
|
3
|
+
/** A sandbox shape. Memory travels in MiB; `memoryGb` is the same number in GiB. */
|
|
4
|
+
export interface Resources {
|
|
5
|
+
vcpu: number;
|
|
6
|
+
memoryMb: number;
|
|
7
|
+
memoryGb: number;
|
|
8
|
+
diskGb: number;
|
|
9
|
+
}
|
|
10
|
+
/** What a create asks for. Every field is optional: the template's default fills the gaps. */
|
|
11
|
+
export interface ResourcesInput {
|
|
12
|
+
vcpu?: number;
|
|
13
|
+
/** Memory in GiB (`memoryGb: 4` is 4096 MiB). Give this or `memoryMb`, not both. */
|
|
14
|
+
memoryGb?: number;
|
|
15
|
+
/** Memory in MiB. */
|
|
16
|
+
memoryMb?: number;
|
|
17
|
+
diskGb?: number;
|
|
18
|
+
}
|
|
19
|
+
/** What the API says about a sandbox. */
|
|
20
|
+
export interface SandboxInfo {
|
|
21
|
+
id: string;
|
|
22
|
+
state: SandboxState;
|
|
23
|
+
template: string | null;
|
|
24
|
+
resources: Resources;
|
|
25
|
+
labels: Record<string, string>;
|
|
26
|
+
/** Idle minutes before a running sandbox pauses itself (null: never). */
|
|
27
|
+
autoPauseMin: number | null;
|
|
28
|
+
/** Minutes after pausing before the sandbox deletes itself (null: never). */
|
|
29
|
+
autoDeleteMin: number | null;
|
|
30
|
+
/**
|
|
31
|
+
* When the sandbox's run time ends and it is deleted. Null while a paused
|
|
32
|
+
* sandbox's clock is stopped: a resume on a paid plan starts a fresh one;
|
|
33
|
+
* on the free tier it carries on the running time left.
|
|
34
|
+
*/
|
|
35
|
+
expiresAt: Date | null;
|
|
36
|
+
createdAt: Date | null;
|
|
37
|
+
lastActiveAt: Date | null;
|
|
38
|
+
/** Why the sandbox is in `error`, when it is. */
|
|
39
|
+
errorReason: string | null;
|
|
40
|
+
/** The base URL its exec and files calls go to. */
|
|
41
|
+
toolboxBase: string | null;
|
|
42
|
+
/** The document exactly as the API sent it. */
|
|
43
|
+
raw: Record<string, unknown>;
|
|
44
|
+
}
|
|
45
|
+
/** One entry of `Sandbox.templates()`. */
|
|
46
|
+
export interface Template {
|
|
47
|
+
id: string;
|
|
48
|
+
defaults: Resources;
|
|
49
|
+
min: Resources;
|
|
50
|
+
max: Resources;
|
|
51
|
+
durability: string;
|
|
52
|
+
/** The run time a sandbox gets when its create gives no `maxRuntimeMin` (for your workspace's plan). */
|
|
53
|
+
defaultMaxRuntimeMin: number;
|
|
54
|
+
}
|
|
55
|
+
/** What one command produced. */
|
|
56
|
+
export interface ExecResult {
|
|
57
|
+
stdout: string;
|
|
58
|
+
stderr: string;
|
|
59
|
+
/** The command's exit code; -1 when it timed out or ended without reporting one. */
|
|
60
|
+
exitCode: number;
|
|
61
|
+
/** How long the command ran, in ms. */
|
|
62
|
+
durationMs: number;
|
|
63
|
+
/**
|
|
64
|
+
* True when the command hit its timeout and was killed (`exitCode` is then
|
|
65
|
+
* -1). `exec()` and `wait()` throw CommandTimeoutError with such a result
|
|
66
|
+
* rather than return it.
|
|
67
|
+
*/
|
|
68
|
+
timedOut: boolean;
|
|
69
|
+
/**
|
|
70
|
+
* True when stdout or stderr reached the 4 MiB per-stream cap of a single
|
|
71
|
+
* call: the command kept running, the rest of that stream was dropped.
|
|
72
|
+
* Pass `fullOutput: true` for commands that may print more (it keeps up
|
|
73
|
+
* to 100 MiB of each stream, and sets this past that). `truncatedReason`
|
|
74
|
+
* says why.
|
|
75
|
+
*/
|
|
76
|
+
truncated: boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Why `truncated` is true; absent when it is false. `output_limit`: a
|
|
79
|
+
* stream reached its cap (4 MiB in one call, 100 MiB with `fullOutput`).
|
|
80
|
+
* `write_failed` (`fullOutput` only): the sandbox could not save a stream
|
|
81
|
+
* as the command printed it, most often because its disk is full, so that
|
|
82
|
+
* stream stops where the write failed, however short; free some space and
|
|
83
|
+
* run it again.
|
|
84
|
+
*/
|
|
85
|
+
truncatedReason?: TruncatedReason;
|
|
86
|
+
}
|
|
87
|
+
/** Why a command's output was cut: ExecResult.truncatedReason. */
|
|
88
|
+
export type TruncatedReason = 'output_limit' | 'write_failed';
|
|
89
|
+
/** One directory entry, as `sandbox.files.list()` returns it. */
|
|
90
|
+
export interface FileInfo {
|
|
91
|
+
name: string;
|
|
92
|
+
path: string;
|
|
93
|
+
/** Size in bytes. */
|
|
94
|
+
size: number;
|
|
95
|
+
isDir: boolean;
|
|
96
|
+
isSymlink: boolean;
|
|
97
|
+
/** Permissions as `ls -l` prints them, e.g. `-rw-r--r--`. */
|
|
98
|
+
mode: string;
|
|
99
|
+
modified: Date | null;
|
|
100
|
+
}
|
|
101
|
+
/** What a write or an upload reports. */
|
|
102
|
+
export interface WriteInfo {
|
|
103
|
+
path: string;
|
|
104
|
+
size: number;
|
|
105
|
+
}
|
|
106
|
+
export declare function parseTime(value: unknown): Date | null;
|
|
107
|
+
export declare function parseResources(value: unknown): Resources;
|
|
108
|
+
export declare function parseSandbox(doc: unknown): SandboxInfo;
|
|
109
|
+
export declare function parseTemplate(entry: unknown): Template | null;
|
|
110
|
+
/** The wire shape of a create's `resources`: memory in MiB, integers only. */
|
|
111
|
+
export declare function resourcesToWire(input: ResourcesInput): Record<string, number>;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// Public types and the parsers that turn API documents into them.
|
|
2
|
+
import { PrizedError } from './errors.js';
|
|
3
|
+
function int(value, fallback = 0) {
|
|
4
|
+
return typeof value === 'number' && Number.isInteger(value) ? value : fallback;
|
|
5
|
+
}
|
|
6
|
+
function optInt(value) {
|
|
7
|
+
return typeof value === 'number' && Number.isInteger(value) ? value : null;
|
|
8
|
+
}
|
|
9
|
+
function optString(value) {
|
|
10
|
+
return typeof value === 'string' ? value : null;
|
|
11
|
+
}
|
|
12
|
+
export function parseTime(value) {
|
|
13
|
+
if (typeof value !== 'string' || value === '')
|
|
14
|
+
return null;
|
|
15
|
+
const at = Date.parse(value);
|
|
16
|
+
return Number.isNaN(at) ? null : new Date(at);
|
|
17
|
+
}
|
|
18
|
+
function record(value) {
|
|
19
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
|
20
|
+
}
|
|
21
|
+
export function parseResources(value) {
|
|
22
|
+
const r = record(value);
|
|
23
|
+
const memoryMb = int(r.memoryMb);
|
|
24
|
+
return { vcpu: int(r.vcpu), memoryMb, memoryGb: memoryMb / 1024, diskGb: int(r.diskGb) };
|
|
25
|
+
}
|
|
26
|
+
export function parseSandbox(doc) {
|
|
27
|
+
const d = record(doc);
|
|
28
|
+
if (typeof d.id !== 'string' || d.id === '') {
|
|
29
|
+
throw new PrizedError('bad_response', 'The API answered without a sandbox document.');
|
|
30
|
+
}
|
|
31
|
+
const labels = {};
|
|
32
|
+
for (const [key, value] of Object.entries(record(d.labels))) {
|
|
33
|
+
if (typeof value === 'string')
|
|
34
|
+
labels[key] = value;
|
|
35
|
+
}
|
|
36
|
+
return {
|
|
37
|
+
id: d.id,
|
|
38
|
+
state: (typeof d.state === 'string' ? d.state : 'error'),
|
|
39
|
+
template: optString(d.template),
|
|
40
|
+
resources: parseResources(d.resources),
|
|
41
|
+
labels,
|
|
42
|
+
autoPauseMin: optInt(d.autoPauseMin),
|
|
43
|
+
autoDeleteMin: optInt(d.autoDeleteMin),
|
|
44
|
+
expiresAt: parseTime(d.expiresAt),
|
|
45
|
+
createdAt: parseTime(d.createdAt),
|
|
46
|
+
lastActiveAt: parseTime(d.lastActiveAt),
|
|
47
|
+
errorReason: optString(d.errorReason),
|
|
48
|
+
toolboxBase: optString(d.toolboxBase),
|
|
49
|
+
raw: d,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export function parseTemplate(entry) {
|
|
53
|
+
const e = record(entry);
|
|
54
|
+
if (typeof e.id !== 'string' || e.id === '')
|
|
55
|
+
return null;
|
|
56
|
+
const resources = record(e.resources);
|
|
57
|
+
return {
|
|
58
|
+
id: e.id,
|
|
59
|
+
defaults: parseResources(resources.defaults),
|
|
60
|
+
min: parseResources(resources.min),
|
|
61
|
+
max: parseResources(resources.max),
|
|
62
|
+
durability: optString(e.durability) ?? 'ephemeral',
|
|
63
|
+
defaultMaxRuntimeMin: int(e.defaultMaxRuntimeMin),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** The wire shape of a create's `resources`: memory in MiB, integers only. */
|
|
67
|
+
export function resourcesToWire(input) {
|
|
68
|
+
const out = {};
|
|
69
|
+
const whole = (name, value) => {
|
|
70
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
71
|
+
throw new PrizedError('invalid_argument', `resources.${name} must be a positive whole number (got ${value})`);
|
|
72
|
+
}
|
|
73
|
+
return value;
|
|
74
|
+
};
|
|
75
|
+
if (input.vcpu !== undefined)
|
|
76
|
+
out.vcpu = whole('vcpu', input.vcpu);
|
|
77
|
+
if (input.memoryGb !== undefined && input.memoryMb !== undefined) {
|
|
78
|
+
if (Math.round(input.memoryGb * 1024) !== input.memoryMb) {
|
|
79
|
+
throw new PrizedError('invalid_argument', 'Give resources.memoryGb or resources.memoryMb, not both');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (input.memoryMb !== undefined) {
|
|
83
|
+
out.memoryMb = whole('memoryMb', input.memoryMb);
|
|
84
|
+
}
|
|
85
|
+
else if (input.memoryGb !== undefined) {
|
|
86
|
+
const mb = input.memoryGb * 1024;
|
|
87
|
+
if (!Number.isInteger(mb) || mb <= 0) {
|
|
88
|
+
throw new PrizedError('invalid_argument', `resources.memoryGb must be a whole number of MiB (got ${input.memoryGb} GiB)`);
|
|
89
|
+
}
|
|
90
|
+
out.memoryMb = mb;
|
|
91
|
+
}
|
|
92
|
+
if (input.diskGb !== undefined)
|
|
93
|
+
out.diskGb = whole('diskGb', input.diskGb);
|
|
94
|
+
return out;
|
|
95
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,55 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prized/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Prized Sandboxes (beta): create microVM sandboxes, run commands, move files, pause, resume and fork them from TypeScript.",
|
|
5
|
+
"homepage": "https://prized.dev/docs/sandboxes",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"prized",
|
|
9
|
+
"sandbox",
|
|
10
|
+
"sandboxes",
|
|
11
|
+
"microvm",
|
|
12
|
+
"agents",
|
|
13
|
+
"code-execution"
|
|
14
|
+
],
|
|
15
|
+
"type": "module",
|
|
16
|
+
"main": "./dist/cjs/index.js",
|
|
17
|
+
"module": "./dist/esm/index.js",
|
|
18
|
+
"types": "./dist/esm/index.d.ts",
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"import": {
|
|
22
|
+
"types": "./dist/esm/index.d.ts",
|
|
23
|
+
"default": "./dist/esm/index.js"
|
|
24
|
+
},
|
|
25
|
+
"require": {
|
|
26
|
+
"types": "./dist/cjs/index.d.ts",
|
|
27
|
+
"default": "./dist/cjs/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"./package.json": "./package.json"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"dist",
|
|
34
|
+
"README.md",
|
|
35
|
+
"LICENSE"
|
|
36
|
+
],
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
},
|
|
40
|
+
"sideEffects": false,
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=18"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
46
|
+
"build": "node scripts/build.mjs",
|
|
47
|
+
"test": "node scripts/test.mjs",
|
|
48
|
+
"prepublishOnly": "node scripts/build.mjs",
|
|
49
|
+
"pack-check": "node scripts/pack-check.mjs"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@types/node": "22.20.4",
|
|
53
|
+
"typescript": "5.9.3"
|
|
54
|
+
}
|
|
55
|
+
}
|