@deftai/isolinear-client 0.0.0-stage → 0.5.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.ts +1 -0
- package/dist/index.js +368 -0
- package/package.json +24 -4
- package/src/index.ts +368 -0
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "../src/index.ts";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @deftai/isolinear-client — Zero-dependency ESM TypeScript SDK for Isolinear.
|
|
3
|
+
*
|
|
4
|
+
* Works identically in Cloudflare Workers (workerd) and Node.js 20+ against either:
|
|
5
|
+
* - a local `isol serve` daemon, or
|
|
6
|
+
* - the Cloudflare Worker + Durable Object control plane (`deploy/cloudflare/`).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
export class IsolinearError extends Error {
|
|
139
|
+
status ;
|
|
140
|
+
body ;
|
|
141
|
+
|
|
142
|
+
constructor(message , status , body ) {
|
|
143
|
+
super(message);
|
|
144
|
+
this.name = "IsolinearError";
|
|
145
|
+
this.status = status;
|
|
146
|
+
this.body = body;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function encodeBase64(input ) {
|
|
151
|
+
const bytes = typeof input === "string" ? new TextEncoder().encode(input) : input;
|
|
152
|
+
let bin = "";
|
|
153
|
+
const chunk = 0x8000;
|
|
154
|
+
for (let i = 0; i < bytes.length; i += chunk) {
|
|
155
|
+
bin += String.fromCharCode(...bytes.subarray(i, i + chunk));
|
|
156
|
+
}
|
|
157
|
+
return btoa(bin);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function decodeBase64(b64 ) {
|
|
161
|
+
const bin = atob(b64);
|
|
162
|
+
const out = new Uint8Array(bin.length);
|
|
163
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
164
|
+
return out;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function encodeInputMap(files ) {
|
|
168
|
+
if (!files) return undefined;
|
|
169
|
+
const out = {};
|
|
170
|
+
for (const [k, v] of Object.entries(files)) out[k] = encodeBase64(v);
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function decodeOutputMap(raw ) {
|
|
175
|
+
const out = {};
|
|
176
|
+
if (!raw) return out;
|
|
177
|
+
for (const [k, v] of Object.entries(raw)) out[k] = decodeBase64(v);
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export class IsolinearClient {
|
|
182
|
+
baseUrl ;
|
|
183
|
+
token ;
|
|
184
|
+
fetchImpl ;
|
|
185
|
+
|
|
186
|
+
constructor(opts ) {
|
|
187
|
+
this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
|
|
188
|
+
this.token = opts.token;
|
|
189
|
+
this.fetchImpl = opts.fetch ?? globalThis.fetch.bind(globalThis);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
headers(extra ) {
|
|
193
|
+
const h = { ...(extra ?? {}) };
|
|
194
|
+
if (this.token) h["Authorization"] = `Bearer ${this.token}`;
|
|
195
|
+
return h;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async requestJson (method , path , body ) {
|
|
199
|
+
const init = {
|
|
200
|
+
method,
|
|
201
|
+
headers: this.headers(body !== undefined ? { "Content-Type": "application/json" } : undefined),
|
|
202
|
+
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
203
|
+
};
|
|
204
|
+
const resp = await this.fetchImpl(`${this.baseUrl}${path}`, init);
|
|
205
|
+
const text = await resp.text();
|
|
206
|
+
let parsed = text;
|
|
207
|
+
try {
|
|
208
|
+
parsed = JSON.parse(text);
|
|
209
|
+
} catch {
|
|
210
|
+
// keep raw text
|
|
211
|
+
}
|
|
212
|
+
if (!resp.ok) {
|
|
213
|
+
const msg =
|
|
214
|
+
typeof parsed === "object" && parsed !== null && "error" in parsed
|
|
215
|
+
? String((parsed ).error)
|
|
216
|
+
: `HTTP ${resp.status}: ${text}`;
|
|
217
|
+
throw new IsolinearError(msg, resp.status, parsed);
|
|
218
|
+
}
|
|
219
|
+
return parsed ;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Node health, arch, backend (`hvf`/`kvm`/`linux-systrap`), Verin module count, and cached digests. */
|
|
223
|
+
async healthz() {
|
|
224
|
+
return this.requestJson ("GET", "/healthz");
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Run a hardened, single-call stateless untrusted job (`POST /v1/jobs`).
|
|
229
|
+
* Enforces Verin `--egress=none` by default, hard timeout preemption, and zero cross-tenant persistence.
|
|
230
|
+
*/
|
|
231
|
+
async runJob(req ) {
|
|
232
|
+
const raw = await this.requestJson
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
("POST", "/v1/jobs", {
|
|
245
|
+
bundle: req.bundle,
|
|
246
|
+
bundle_url: req.bundleUrl,
|
|
247
|
+
digest: req.digest,
|
|
248
|
+
egress: req.egress ?? "none",
|
|
249
|
+
timeout_ms: req.timeoutMs ?? 5000,
|
|
250
|
+
memory_mb: req.memoryMb ?? 256,
|
|
251
|
+
input_files: encodeInputMap(req.inputFiles),
|
|
252
|
+
cmd: req.cmd,
|
|
253
|
+
script: req.script,
|
|
254
|
+
output_paths: req.outputPaths,
|
|
255
|
+
});
|
|
256
|
+
return {
|
|
257
|
+
ok: raw.ok,
|
|
258
|
+
profile: raw.profile,
|
|
259
|
+
egress: raw.egress,
|
|
260
|
+
exitCode: raw.exit_code,
|
|
261
|
+
timedOut: raw.timed_out,
|
|
262
|
+
durationUs: raw.duration_us,
|
|
263
|
+
egressBlockedCount: raw.egress_blocked_count,
|
|
264
|
+
peakRssBytes: raw.peak_rss_bytes,
|
|
265
|
+
stdout: raw.stdout,
|
|
266
|
+
stderr: raw.stderr,
|
|
267
|
+
outputs: decodeOutputMap(raw.outputs),
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
async createSandbox(req = {}) {
|
|
272
|
+
return this.requestJson ("POST", "/v1/sandboxes", {
|
|
273
|
+
id: req.id,
|
|
274
|
+
bundle: req.bundle,
|
|
275
|
+
bundle_url: req.bundleUrl,
|
|
276
|
+
digest: req.digest,
|
|
277
|
+
kernel: req.kernel ?? "verin",
|
|
278
|
+
egress: req.egress ?? "none",
|
|
279
|
+
timeout_ms: req.timeoutMs ?? 10000,
|
|
280
|
+
memory_mb: req.memoryMb ?? 0,
|
|
281
|
+
input_files: encodeInputMap(req.inputFiles),
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async listSandboxes() {
|
|
286
|
+
return this.requestJson("GET", "/v1/sandboxes");
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async exec(id , req ) {
|
|
290
|
+
const raw = await this.requestJson
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
("POST", `/v1/sandboxes/${encodeURIComponent(id)}/exec`, {
|
|
302
|
+
cmd: req.cmd,
|
|
303
|
+
script: req.script,
|
|
304
|
+
timeout_ms: req.timeoutMs,
|
|
305
|
+
egress: req.egress,
|
|
306
|
+
input_files: encodeInputMap(req.inputFiles),
|
|
307
|
+
output_paths: req.outputPaths,
|
|
308
|
+
});
|
|
309
|
+
return {
|
|
310
|
+
ok: raw.ok,
|
|
311
|
+
id: raw.id,
|
|
312
|
+
exitCode: raw.exit_code,
|
|
313
|
+
timedOut: raw.timed_out,
|
|
314
|
+
durationUs: raw.duration_us,
|
|
315
|
+
rssBytes: raw.rss_bytes,
|
|
316
|
+
egressBlockedCount: raw.egress_blocked_count,
|
|
317
|
+
stdout: raw.stdout,
|
|
318
|
+
stderr: raw.stderr,
|
|
319
|
+
outputs: decodeOutputMap(raw.outputs),
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
async fork(id , dstId ) {
|
|
324
|
+
return this.requestJson("POST", `/v1/sandboxes/${encodeURIComponent(id)}/fork`, dstId ? { dst_id: dstId } : {});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
async reset(id ) {
|
|
328
|
+
return this.requestJson("POST", `/v1/sandboxes/${encodeURIComponent(id)}/reset`, {});
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
async snapshot(id , req = {}) {
|
|
332
|
+
return this.requestJson("POST", `/v1/sandboxes/${encodeURIComponent(id)}/snapshot`, {
|
|
333
|
+
label: req.label,
|
|
334
|
+
upload_url: req.uploadUrl,
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
async deleteSandbox(id ) {
|
|
339
|
+
return this.requestJson("DELETE", `/v1/sandboxes/${encodeURIComponent(id)}`);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
async displaySnap(id ) {
|
|
343
|
+
const resp = await this.fetchImpl(
|
|
344
|
+
`${this.baseUrl}/v1/sandboxes/${encodeURIComponent(id)}/display/snap.png`,
|
|
345
|
+
{ headers: this.headers() },
|
|
346
|
+
);
|
|
347
|
+
if (!resp.ok) throw new IsolinearError(`displaySnap failed: HTTP ${resp.status}`, resp.status);
|
|
348
|
+
return new Uint8Array(await resp.arrayBuffer());
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async displayPatches(id , since = 0) {
|
|
352
|
+
return this.requestJson("GET", `/v1/sandboxes/${encodeURIComponent(id)}/display/patches?since=${since}`);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
async displayInput(id , payload ) {
|
|
356
|
+
return this.requestJson("POST", `/v1/sandboxes/${encodeURIComponent(id)}/display/input`, payload);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
vncWebSocketUrl(id ) {
|
|
360
|
+
const wsBase = this.baseUrl.replace(/^http:/, "ws:").replace(/^https:/, "wss:");
|
|
361
|
+
const q = this.token ? `?token=${encodeURIComponent(this.token)}` : "";
|
|
362
|
+
return `${wsBase}/v1/sandboxes/${encodeURIComponent(id)}/vnc${q}`;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
async mcpCall(id , rpcPayload ) {
|
|
366
|
+
return this.requestJson("POST", `/v1/sandboxes/${encodeURIComponent(id)}/mcp`, rpcPayload);
|
|
367
|
+
}
|
|
368
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@deftai/isolinear-client",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Zero-dependency ESM TypeScript client for Isolinear microVM sandboxes & untrusted jobs (Cloudflare Workers + Node.js 20+)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist",
|
|
17
|
+
"src"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "node scripts/build.mjs",
|
|
21
|
+
"test": "node test/integration.test.mjs"
|
|
22
|
+
},
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=20.0.0"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @deftai/isolinear-client — Zero-dependency ESM TypeScript SDK for Isolinear.
|
|
3
|
+
*
|
|
4
|
+
* Works identically in Cloudflare Workers (workerd) and Node.js 20+ against either:
|
|
5
|
+
* - a local `isol serve` daemon, or
|
|
6
|
+
* - the Cloudflare Worker + Durable Object control plane (`deploy/cloudflare/`).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export type EgressPolicy = "none" | "offline" | "readonly" | "full";
|
|
10
|
+
export type KernelMode = "verin" | "linux";
|
|
11
|
+
|
|
12
|
+
export interface IsolinearClientOptions {
|
|
13
|
+
/** Base HTTP(S) URL of `isol serve` or the Cloudflare Worker gateway. */
|
|
14
|
+
baseUrl: string;
|
|
15
|
+
/** Bearer token matching `--auth-token-env` (or Cloudflare Worker API key). */
|
|
16
|
+
token?: string;
|
|
17
|
+
/** Optional custom fetch implementation (defaults to globalThis.fetch). */
|
|
18
|
+
fetch?: typeof fetch;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface HealthzResponse {
|
|
22
|
+
ok: boolean;
|
|
23
|
+
node_id: string;
|
|
24
|
+
label: string;
|
|
25
|
+
arch: string;
|
|
26
|
+
backend: string;
|
|
27
|
+
verin_modules: number;
|
|
28
|
+
static_guards: number;
|
|
29
|
+
active_sandboxes: number;
|
|
30
|
+
max_vms: number;
|
|
31
|
+
cached_bundles: string[];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface RunJobRequest {
|
|
35
|
+
bundle?: string;
|
|
36
|
+
bundleUrl?: string;
|
|
37
|
+
digest?: string;
|
|
38
|
+
egress?: Exclude<EgressPolicy, "full">;
|
|
39
|
+
timeoutMs?: number;
|
|
40
|
+
memoryMb?: number;
|
|
41
|
+
inputFiles?: Record<string, Uint8Array | string>;
|
|
42
|
+
cmd?: string[];
|
|
43
|
+
script?: string;
|
|
44
|
+
outputPaths?: string[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface RunJobResponse {
|
|
48
|
+
ok: boolean;
|
|
49
|
+
profile: string;
|
|
50
|
+
egress: string;
|
|
51
|
+
exitCode: number;
|
|
52
|
+
timedOut: boolean;
|
|
53
|
+
durationUs: number;
|
|
54
|
+
egressBlockedCount: number;
|
|
55
|
+
peakRssBytes: number;
|
|
56
|
+
stdout: string;
|
|
57
|
+
stderr: string;
|
|
58
|
+
outputs: Record<string, Uint8Array>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface CreateSandboxRequest {
|
|
62
|
+
id?: string;
|
|
63
|
+
bundle?: string;
|
|
64
|
+
bundleUrl?: string;
|
|
65
|
+
digest?: string;
|
|
66
|
+
kernel?: KernelMode;
|
|
67
|
+
egress?: EgressPolicy;
|
|
68
|
+
timeoutMs?: number;
|
|
69
|
+
memoryMb?: number;
|
|
70
|
+
inputFiles?: Record<string, Uint8Array | string>;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface CreateSandboxResponse {
|
|
74
|
+
ok: boolean;
|
|
75
|
+
id: string;
|
|
76
|
+
sandbox_id: string;
|
|
77
|
+
kernel: string;
|
|
78
|
+
egress: string;
|
|
79
|
+
timeout_ms: number;
|
|
80
|
+
memory_mb: number;
|
|
81
|
+
bundle_digest: string;
|
|
82
|
+
created_ms: number;
|
|
83
|
+
elapsed_us: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface ExecSandboxRequest {
|
|
87
|
+
cmd?: string[];
|
|
88
|
+
script?: string;
|
|
89
|
+
timeoutMs?: number;
|
|
90
|
+
egress?: EgressPolicy;
|
|
91
|
+
inputFiles?: Record<string, Uint8Array | string>;
|
|
92
|
+
outputPaths?: string[];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface ExecSandboxResponse {
|
|
96
|
+
ok: boolean;
|
|
97
|
+
id: string;
|
|
98
|
+
exitCode: number;
|
|
99
|
+
timedOut: boolean;
|
|
100
|
+
durationUs: number;
|
|
101
|
+
rssBytes: number;
|
|
102
|
+
egressBlockedCount: number;
|
|
103
|
+
stdout: string;
|
|
104
|
+
stderr: string;
|
|
105
|
+
outputs: Record<string, Uint8Array>;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface ForkSandboxResponse {
|
|
109
|
+
ok: boolean;
|
|
110
|
+
src_id: string;
|
|
111
|
+
id: string;
|
|
112
|
+
sandbox_id: string;
|
|
113
|
+
elapsed_us: number;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface ResetSandboxResponse {
|
|
117
|
+
ok: boolean;
|
|
118
|
+
id: string;
|
|
119
|
+
reset: boolean;
|
|
120
|
+
elapsed_us: number;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export interface SnapshotSandboxRequest {
|
|
124
|
+
label?: string;
|
|
125
|
+
uploadUrl?: string;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface SnapshotSandboxResponse {
|
|
129
|
+
ok: boolean;
|
|
130
|
+
id: string;
|
|
131
|
+
label: string;
|
|
132
|
+
sha256: string;
|
|
133
|
+
bytes: number;
|
|
134
|
+
uploaded: boolean;
|
|
135
|
+
elapsed_us: number;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export class IsolinearError extends Error {
|
|
139
|
+
readonly status: number;
|
|
140
|
+
readonly body?: unknown;
|
|
141
|
+
|
|
142
|
+
constructor(message: string, status: number, body?: unknown) {
|
|
143
|
+
super(message);
|
|
144
|
+
this.name = "IsolinearError";
|
|
145
|
+
this.status = status;
|
|
146
|
+
this.body = body;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export function encodeBase64(input: Uint8Array | string): string {
|
|
151
|
+
const bytes = typeof input === "string" ? new TextEncoder().encode(input) : input;
|
|
152
|
+
let bin = "";
|
|
153
|
+
const chunk = 0x8000;
|
|
154
|
+
for (let i = 0; i < bytes.length; i += chunk) {
|
|
155
|
+
bin += String.fromCharCode(...bytes.subarray(i, i + chunk));
|
|
156
|
+
}
|
|
157
|
+
return btoa(bin);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function decodeBase64(b64: string): Uint8Array {
|
|
161
|
+
const bin = atob(b64);
|
|
162
|
+
const out = new Uint8Array(bin.length);
|
|
163
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
164
|
+
return out;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function encodeInputMap(files?: Record<string, Uint8Array | string>): Record<string, string> | undefined {
|
|
168
|
+
if (!files) return undefined;
|
|
169
|
+
const out: Record<string, string> = {};
|
|
170
|
+
for (const [k, v] of Object.entries(files)) out[k] = encodeBase64(v);
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function decodeOutputMap(raw?: Record<string, string>): Record<string, Uint8Array> {
|
|
175
|
+
const out: Record<string, Uint8Array> = {};
|
|
176
|
+
if (!raw) return out;
|
|
177
|
+
for (const [k, v] of Object.entries(raw)) out[k] = decodeBase64(v);
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export class IsolinearClient {
|
|
182
|
+
private readonly baseUrl: string;
|
|
183
|
+
private readonly token?: string;
|
|
184
|
+
private readonly fetchImpl: typeof fetch;
|
|
185
|
+
|
|
186
|
+
constructor(opts: IsolinearClientOptions) {
|
|
187
|
+
this.baseUrl = opts.baseUrl.replace(/\/+$/, "");
|
|
188
|
+
this.token = opts.token;
|
|
189
|
+
this.fetchImpl = opts.fetch ?? globalThis.fetch.bind(globalThis);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private headers(extra?: Record<string, string>): Record<string, string> {
|
|
193
|
+
const h: Record<string, string> = { ...(extra ?? {}) };
|
|
194
|
+
if (this.token) h["Authorization"] = `Bearer ${this.token}`;
|
|
195
|
+
return h;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
private async requestJson<T>(method: string, path: string, body?: unknown): Promise<T> {
|
|
199
|
+
const init: RequestInit = {
|
|
200
|
+
method,
|
|
201
|
+
headers: this.headers(body !== undefined ? { "Content-Type": "application/json" } : undefined),
|
|
202
|
+
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
203
|
+
};
|
|
204
|
+
const resp = await this.fetchImpl(`${this.baseUrl}${path}`, init);
|
|
205
|
+
const text = await resp.text();
|
|
206
|
+
let parsed: unknown = text;
|
|
207
|
+
try {
|
|
208
|
+
parsed = JSON.parse(text);
|
|
209
|
+
} catch {
|
|
210
|
+
// keep raw text
|
|
211
|
+
}
|
|
212
|
+
if (!resp.ok) {
|
|
213
|
+
const msg =
|
|
214
|
+
typeof parsed === "object" && parsed !== null && "error" in parsed
|
|
215
|
+
? String((parsed as { error: unknown }).error)
|
|
216
|
+
: `HTTP ${resp.status}: ${text}`;
|
|
217
|
+
throw new IsolinearError(msg, resp.status, parsed);
|
|
218
|
+
}
|
|
219
|
+
return parsed as T;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Node health, arch, backend (`hvf`/`kvm`/`linux-systrap`), Verin module count, and cached digests. */
|
|
223
|
+
async healthz(): Promise<HealthzResponse> {
|
|
224
|
+
return this.requestJson<HealthzResponse>("GET", "/healthz");
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Run a hardened, single-call stateless untrusted job (`POST /v1/jobs`).
|
|
229
|
+
* Enforces Verin `--egress=none` by default, hard timeout preemption, and zero cross-tenant persistence.
|
|
230
|
+
*/
|
|
231
|
+
async runJob(req: RunJobRequest): Promise<RunJobResponse> {
|
|
232
|
+
const raw = await this.requestJson<{
|
|
233
|
+
ok: boolean;
|
|
234
|
+
profile: string;
|
|
235
|
+
egress: string;
|
|
236
|
+
exit_code: number;
|
|
237
|
+
timed_out: boolean;
|
|
238
|
+
duration_us: number;
|
|
239
|
+
egress_blocked_count: number;
|
|
240
|
+
peak_rss_bytes: number;
|
|
241
|
+
stdout: string;
|
|
242
|
+
stderr: string;
|
|
243
|
+
outputs?: Record<string, string>;
|
|
244
|
+
}>("POST", "/v1/jobs", {
|
|
245
|
+
bundle: req.bundle,
|
|
246
|
+
bundle_url: req.bundleUrl,
|
|
247
|
+
digest: req.digest,
|
|
248
|
+
egress: req.egress ?? "none",
|
|
249
|
+
timeout_ms: req.timeoutMs ?? 5000,
|
|
250
|
+
memory_mb: req.memoryMb ?? 256,
|
|
251
|
+
input_files: encodeInputMap(req.inputFiles),
|
|
252
|
+
cmd: req.cmd,
|
|
253
|
+
script: req.script,
|
|
254
|
+
output_paths: req.outputPaths,
|
|
255
|
+
});
|
|
256
|
+
return {
|
|
257
|
+
ok: raw.ok,
|
|
258
|
+
profile: raw.profile,
|
|
259
|
+
egress: raw.egress,
|
|
260
|
+
exitCode: raw.exit_code,
|
|
261
|
+
timedOut: raw.timed_out,
|
|
262
|
+
durationUs: raw.duration_us,
|
|
263
|
+
egressBlockedCount: raw.egress_blocked_count,
|
|
264
|
+
peakRssBytes: raw.peak_rss_bytes,
|
|
265
|
+
stdout: raw.stdout,
|
|
266
|
+
stderr: raw.stderr,
|
|
267
|
+
outputs: decodeOutputMap(raw.outputs),
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
async createSandbox(req: CreateSandboxRequest = {}): Promise<CreateSandboxResponse> {
|
|
272
|
+
return this.requestJson<CreateSandboxResponse>("POST", "/v1/sandboxes", {
|
|
273
|
+
id: req.id,
|
|
274
|
+
bundle: req.bundle,
|
|
275
|
+
bundle_url: req.bundleUrl,
|
|
276
|
+
digest: req.digest,
|
|
277
|
+
kernel: req.kernel ?? "verin",
|
|
278
|
+
egress: req.egress ?? "none",
|
|
279
|
+
timeout_ms: req.timeoutMs ?? 10000,
|
|
280
|
+
memory_mb: req.memoryMb ?? 0,
|
|
281
|
+
input_files: encodeInputMap(req.inputFiles),
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
async listSandboxes(): Promise<{ ok: boolean; count: number; max_vms: number; sandboxes: unknown[] }> {
|
|
286
|
+
return this.requestJson("GET", "/v1/sandboxes");
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
async exec(id: string, req: ExecSandboxRequest): Promise<ExecSandboxResponse> {
|
|
290
|
+
const raw = await this.requestJson<{
|
|
291
|
+
ok: boolean;
|
|
292
|
+
id: string;
|
|
293
|
+
exit_code: number;
|
|
294
|
+
timed_out: boolean;
|
|
295
|
+
duration_us: number;
|
|
296
|
+
rss_bytes: number;
|
|
297
|
+
egress_blocked_count: number;
|
|
298
|
+
stdout: string;
|
|
299
|
+
stderr: string;
|
|
300
|
+
outputs?: Record<string, string>;
|
|
301
|
+
}>("POST", `/v1/sandboxes/${encodeURIComponent(id)}/exec`, {
|
|
302
|
+
cmd: req.cmd,
|
|
303
|
+
script: req.script,
|
|
304
|
+
timeout_ms: req.timeoutMs,
|
|
305
|
+
egress: req.egress,
|
|
306
|
+
input_files: encodeInputMap(req.inputFiles),
|
|
307
|
+
output_paths: req.outputPaths,
|
|
308
|
+
});
|
|
309
|
+
return {
|
|
310
|
+
ok: raw.ok,
|
|
311
|
+
id: raw.id,
|
|
312
|
+
exitCode: raw.exit_code,
|
|
313
|
+
timedOut: raw.timed_out,
|
|
314
|
+
durationUs: raw.duration_us,
|
|
315
|
+
rssBytes: raw.rss_bytes,
|
|
316
|
+
egressBlockedCount: raw.egress_blocked_count,
|
|
317
|
+
stdout: raw.stdout,
|
|
318
|
+
stderr: raw.stderr,
|
|
319
|
+
outputs: decodeOutputMap(raw.outputs),
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
async fork(id: string, dstId?: string): Promise<ForkSandboxResponse> {
|
|
324
|
+
return this.requestJson("POST", `/v1/sandboxes/${encodeURIComponent(id)}/fork`, dstId ? { dst_id: dstId } : {});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
async reset(id: string): Promise<ResetSandboxResponse> {
|
|
328
|
+
return this.requestJson("POST", `/v1/sandboxes/${encodeURIComponent(id)}/reset`, {});
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
async snapshot(id: string, req: SnapshotSandboxRequest = {}): Promise<SnapshotSandboxResponse> {
|
|
332
|
+
return this.requestJson("POST", `/v1/sandboxes/${encodeURIComponent(id)}/snapshot`, {
|
|
333
|
+
label: req.label,
|
|
334
|
+
upload_url: req.uploadUrl,
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
async deleteSandbox(id: string): Promise<{ ok: boolean; id: string; deleted: boolean }> {
|
|
339
|
+
return this.requestJson("DELETE", `/v1/sandboxes/${encodeURIComponent(id)}`);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
async displaySnap(id: string): Promise<Uint8Array> {
|
|
343
|
+
const resp = await this.fetchImpl(
|
|
344
|
+
`${this.baseUrl}/v1/sandboxes/${encodeURIComponent(id)}/display/snap.png`,
|
|
345
|
+
{ headers: this.headers() },
|
|
346
|
+
);
|
|
347
|
+
if (!resp.ok) throw new IsolinearError(`displaySnap failed: HTTP ${resp.status}`, resp.status);
|
|
348
|
+
return new Uint8Array(await resp.arrayBuffer());
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
async displayPatches(id: string, since = 0): Promise<{ ok: boolean; rev: number; patches: unknown[] }> {
|
|
352
|
+
return this.requestJson("GET", `/v1/sandboxes/${encodeURIComponent(id)}/display/patches?since=${since}`);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
async displayInput(id: string, payload: Record<string, unknown>): Promise<{ ok: boolean; injected: boolean }> {
|
|
356
|
+
return this.requestJson("POST", `/v1/sandboxes/${encodeURIComponent(id)}/display/input`, payload);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
vncWebSocketUrl(id: string): string {
|
|
360
|
+
const wsBase = this.baseUrl.replace(/^http:/, "ws:").replace(/^https:/, "wss:");
|
|
361
|
+
const q = this.token ? `?token=${encodeURIComponent(this.token)}` : "";
|
|
362
|
+
return `${wsBase}/v1/sandboxes/${encodeURIComponent(id)}/vnc${q}`;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
async mcpCall(id: string, rpcPayload: Record<string, unknown>): Promise<unknown> {
|
|
366
|
+
return this.requestJson("POST", `/v1/sandboxes/${encodeURIComponent(id)}/mcp`, rpcPayload);
|
|
367
|
+
}
|
|
368
|
+
}
|
package/README.md
DELETED