@insforge/instabox-sdk 0.2.2 → 0.3.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/README.md +28 -1
- package/dist/index.cjs +280 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +91 -2
- package/dist/index.d.ts +91 -2
- package/dist/index.js +280 -19
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/instabox/SKILL.md +2 -0
package/README.md
CHANGED
|
@@ -216,6 +216,33 @@ console.log(`ssh -p ${ssh.port} ${ssh.username}@${ssh.host}`)
|
|
|
216
216
|
|
|
217
217
|
The sandbox must be running. Certificates last 1 hour by default; `ttlSeconds` takes 1 to 86 400 (24 hours), other values throw before the request. Pausing or stopping prevents connections while the sandbox is not running; an unexpired certificate can be used again after resume or start. Deleting the sandbox or revoking the API key that issued the certificate revokes access.
|
|
218
218
|
|
|
219
|
+
## Desktop: see it and control it
|
|
220
|
+
|
|
221
|
+
Sandboxes from the default `agent-browser` template have a desktop (Xfce and Chromium on display `:1`). `sandbox.desktop` starts it, gives an agent a mouse, a keyboard and screenshots, and makes a link people can watch it through.
|
|
222
|
+
|
|
223
|
+
```ts
|
|
224
|
+
await sandbox.desktop.start() // { installed, running, width: 1280, height: 800 }; a no-op when running
|
|
225
|
+
await sandbox.desktop.open('https://example.com')
|
|
226
|
+
const png = await sandbox.desktop.screenshot() // Uint8Array, PNG
|
|
227
|
+
|
|
228
|
+
await sandbox.desktop.click(640, 400) // { button: 'left' | 'right' | 'middle', double: true }
|
|
229
|
+
await sandbox.desktop.move(100, 200)
|
|
230
|
+
await sandbox.desktop.drag([100, 200], [300, 400])
|
|
231
|
+
await sandbox.desktop.scroll({ direction: 'down', amount: 3 })
|
|
232
|
+
await sandbox.desktop.type('Hello "world" ✓\n') // literal text, sent through stdin
|
|
233
|
+
await sandbox.desktop.press('Enter')
|
|
234
|
+
await sandbox.desktop.press(['ctrl', 'l']) // a chord
|
|
235
|
+
const { x, y } = await sandbox.desktop.cursorPosition()
|
|
236
|
+
|
|
237
|
+
// A page that shows the desktop full-screen: open it, or put it in an <iframe>.
|
|
238
|
+
const { url, expiresAt } = await sandbox.desktop.streamUrl({ viewOnly: true, ttlSeconds: 3600 })
|
|
239
|
+
await sandbox.desktop.revokeStreamUrls() // every link stops working; open viewers are disconnected
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
`start({ width, height })` sizes the screen (800-2560 x 600-1600) of a desktop it starts; on other templates it installs the desktop first, about half a minute once. Coordinates are screen pixels from the top left. Keys take common names (`Enter`, `Esc`, `Tab`, `Backspace`, arrows, `F1`-`F24`, `ctrl`, `alt`, `shift`, `cmd`) or X keysym names. Input and screenshots run `xdotool` and `scrot` in the sandbox, so they need the `machines:exec` (or `machines:write`) scope; a failing one throws `CommandExitError`.
|
|
243
|
+
|
|
244
|
+
`streamUrl` needs no instabox account to open: anyone with the link can connect until `expiresAt` (60 s to 24 h, default 1 h). Expiry stops new connections; revoke ends open sessions: a viewer already watching stays until they leave or you call `revokeStreamUrls()`, which ends every link of the sandbox and disconnects its viewers within a few seconds. Revoke when someone's access should end, for example when a tenant's session or subscription ends. Links are view-only by default, enforced by the server; `viewOnly: false` lets the viewer use the mouse and keyboard. The sandbox and its desktop must be running when the link is opened.
|
|
245
|
+
|
|
219
246
|
## Errors
|
|
220
247
|
|
|
221
248
|
| Class | When |
|
|
@@ -256,7 +283,7 @@ INSTABOX_LIVE=1 INSTABOX_API_KEY=... npm run test:live # creates and deletes o
|
|
|
256
283
|
Bump `version` in `package.json` in a PR. After it merges, tag that commit and push the tag:
|
|
257
284
|
|
|
258
285
|
```bash
|
|
259
|
-
git tag v0.
|
|
286
|
+
git tag v0.3.0 && git push origin v0.3.0
|
|
260
287
|
```
|
|
261
288
|
|
|
262
289
|
The Release workflow checks that the tag is the package's version and that it is not on npm yet, runs the
|
package/dist/index.cjs
CHANGED
|
@@ -24,6 +24,8 @@ __export(index_exports, {
|
|
|
24
24
|
Commands: () => Commands,
|
|
25
25
|
DEFAULT_API_URL: () => DEFAULT_API_URL,
|
|
26
26
|
DEFAULT_TEMPLATE: () => DEFAULT_TEMPLATE,
|
|
27
|
+
DESKTOP_DISPLAY: () => DESKTOP_DISPLAY,
|
|
28
|
+
Desktop: () => Desktop,
|
|
27
29
|
FALLBACK_TEMPLATE: () => FALLBACK_TEMPLATE,
|
|
28
30
|
Files: () => Files,
|
|
29
31
|
InstaboxAuthError: () => InstaboxAuthError,
|
|
@@ -211,6 +213,261 @@ var Commands = class {
|
|
|
211
213
|
}
|
|
212
214
|
};
|
|
213
215
|
|
|
216
|
+
// src/desktop.ts
|
|
217
|
+
var DESKTOP_DISPLAY = ":1";
|
|
218
|
+
var BUTTONS = { left: 1, middle: 2, right: 3 };
|
|
219
|
+
var WHEEL = { up: 4, down: 5, left: 6, right: 7 };
|
|
220
|
+
var KEYS = {
|
|
221
|
+
enter: "Return",
|
|
222
|
+
return: "Return",
|
|
223
|
+
esc: "Escape",
|
|
224
|
+
escape: "Escape",
|
|
225
|
+
tab: "Tab",
|
|
226
|
+
space: "space",
|
|
227
|
+
backspace: "BackSpace",
|
|
228
|
+
delete: "Delete",
|
|
229
|
+
del: "Delete",
|
|
230
|
+
insert: "Insert",
|
|
231
|
+
home: "Home",
|
|
232
|
+
end: "End",
|
|
233
|
+
pageup: "Prior",
|
|
234
|
+
pagedown: "Next",
|
|
235
|
+
up: "Up",
|
|
236
|
+
down: "Down",
|
|
237
|
+
left: "Left",
|
|
238
|
+
right: "Right",
|
|
239
|
+
ctrl: "ctrl",
|
|
240
|
+
control: "ctrl",
|
|
241
|
+
alt: "alt",
|
|
242
|
+
shift: "shift",
|
|
243
|
+
meta: "super",
|
|
244
|
+
cmd: "super",
|
|
245
|
+
command: "super",
|
|
246
|
+
super: "super",
|
|
247
|
+
win: "super",
|
|
248
|
+
"+": "plus"
|
|
249
|
+
};
|
|
250
|
+
var enc = encodeURIComponent;
|
|
251
|
+
function keysym(key) {
|
|
252
|
+
if (typeof key !== "string" || !key || /\s/.test(key)) {
|
|
253
|
+
throw new InstaboxError(`invalid key ${JSON.stringify(key)}`, { code: "bad_request" });
|
|
254
|
+
}
|
|
255
|
+
const lower = key.toLowerCase();
|
|
256
|
+
if (KEYS[lower]) return KEYS[lower];
|
|
257
|
+
if (/^f([1-9]|1[0-9]|2[0-4])$/.test(lower)) return lower.toUpperCase();
|
|
258
|
+
return key;
|
|
259
|
+
}
|
|
260
|
+
function coord(name, v) {
|
|
261
|
+
if (!Number.isInteger(v) || v < 0 || v > 16384) {
|
|
262
|
+
throw new InstaboxError(`${name} must be a whole number of pixels from 0; got ${v}`, { code: "bad_request" });
|
|
263
|
+
}
|
|
264
|
+
return v;
|
|
265
|
+
}
|
|
266
|
+
var SCREENSHOT_MAX_BYTES = 32 * 1024 * 1024;
|
|
267
|
+
var Desktop = class {
|
|
268
|
+
/** @internal */
|
|
269
|
+
constructor(http, sandboxId, commands, files) {
|
|
270
|
+
this.http = http;
|
|
271
|
+
this.sandboxId = sandboxId;
|
|
272
|
+
this.commands = commands;
|
|
273
|
+
this.files = files;
|
|
274
|
+
}
|
|
275
|
+
http;
|
|
276
|
+
sandboxId;
|
|
277
|
+
commands;
|
|
278
|
+
files;
|
|
279
|
+
/**
|
|
280
|
+
* Starts the desktop and resolves once it runs; a running desktop is left as it is (its size too).
|
|
281
|
+
* On a template without the desktop it is installed first (about half a minute, once). `width` and
|
|
282
|
+
* `height` size the screen: 800-2560 x 600-1600, default 1280 x 800.
|
|
283
|
+
*/
|
|
284
|
+
async start(opts = {}) {
|
|
285
|
+
const size = {};
|
|
286
|
+
for (const [k, lo, hi] of [
|
|
287
|
+
["width", 800, 2560],
|
|
288
|
+
["height", 600, 1600]
|
|
289
|
+
]) {
|
|
290
|
+
const v = opts[k];
|
|
291
|
+
if (v === void 0) continue;
|
|
292
|
+
if (!Number.isInteger(v) || v < lo || v > hi) {
|
|
293
|
+
throw new InstaboxError(`${k} must be a whole number from ${lo} to ${hi}; got ${v}`, { code: "bad_request" });
|
|
294
|
+
}
|
|
295
|
+
size[k] = v;
|
|
296
|
+
}
|
|
297
|
+
let s = await this.call({ action: "start", ...size });
|
|
298
|
+
if (!s.installed) {
|
|
299
|
+
if (!s.installing) s = await this.call({ action: "install" });
|
|
300
|
+
const deadline = Date.now() + this.http.operationTimeoutMs;
|
|
301
|
+
while (!s.installed) {
|
|
302
|
+
if (s.failed) {
|
|
303
|
+
throw new InstaboxError(
|
|
304
|
+
`the desktop could not be installed: ${s.log ?? "see /tmp/instabox-desktop-install.log"}`,
|
|
305
|
+
{
|
|
306
|
+
code: "desktop_install_failed"
|
|
307
|
+
}
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
if (Date.now() > deadline) {
|
|
311
|
+
throw new InstaboxTimeoutError(`the desktop was not installed within ${this.http.operationTimeoutMs} ms`, {
|
|
312
|
+
code: "operation_timeout"
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
await sleep(this.http.pollIntervalMs * 8);
|
|
316
|
+
s = await this.call({ action: "status" });
|
|
317
|
+
}
|
|
318
|
+
s = await this.call({ action: "start", ...size });
|
|
319
|
+
}
|
|
320
|
+
if (!s.running) throw new InstaboxError("the desktop did not start", { code: "desktop_not_running" });
|
|
321
|
+
return this.status();
|
|
322
|
+
}
|
|
323
|
+
/** Whether the desktop runs, and its screen size. */
|
|
324
|
+
async status() {
|
|
325
|
+
const s = await this.call({ action: "status" });
|
|
326
|
+
const out = { installed: s.installed, running: s.running };
|
|
327
|
+
if (s.running) {
|
|
328
|
+
const [w, h] = (await this.xdo("getdisplaygeometry")).stdout.trim().split(/\s+/).map(Number);
|
|
329
|
+
if (Number.isInteger(w) && Number.isInteger(h)) Object.assign(out, { width: w, height: h });
|
|
330
|
+
}
|
|
331
|
+
return out;
|
|
332
|
+
}
|
|
333
|
+
/** A PNG of the whole screen. */
|
|
334
|
+
async screenshot() {
|
|
335
|
+
const path = `/tmp/instabox-screenshot-${randomSuffix()}.png`;
|
|
336
|
+
const p = shellQuote(path);
|
|
337
|
+
try {
|
|
338
|
+
await this.run(
|
|
339
|
+
`if command -v scrot >/dev/null; then scrot -o ${p}; elif command -v import >/dev/null; then import -window root png:${p}; else echo 'no screenshot tool in the sandbox (scrot or ImageMagick import)' >&2; exit 127; fi`
|
|
340
|
+
);
|
|
341
|
+
return await this.files.read(path, { format: "bytes", maxBytes: SCREENSHOT_MAX_BYTES });
|
|
342
|
+
} finally {
|
|
343
|
+
await this.commands.exec(`rm -f ${p}`).catch(() => {
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
/** Clicks at (x, y): the left button by default; `double` clicks twice. */
|
|
348
|
+
async click(x, y, opts = {}) {
|
|
349
|
+
const button = BUTTONS[opts.button ?? "left"];
|
|
350
|
+
if (!button)
|
|
351
|
+
throw new InstaboxError(`button must be left, right or middle; got ${opts.button}`, { code: "bad_request" });
|
|
352
|
+
const repeat = opts.double ? " --repeat 2 --delay 80" : "";
|
|
353
|
+
await this.xdo(`mousemove ${coord("x", x)} ${coord("y", y)} click${repeat} ${button}`);
|
|
354
|
+
}
|
|
355
|
+
/** Moves the pointer to (x, y). */
|
|
356
|
+
async move(x, y) {
|
|
357
|
+
await this.xdo(`mousemove ${coord("x", x)} ${coord("y", y)}`);
|
|
358
|
+
}
|
|
359
|
+
/** Drags with the left button from `from` to `to`, each `[x, y]`. */
|
|
360
|
+
async drag(from, to) {
|
|
361
|
+
await this.xdo(
|
|
362
|
+
`mousemove ${coord("x", from[0])} ${coord("y", from[1])} mousedown 1 sleep 0.1 mousemove ${coord("x", to[0])} ${coord("y", to[1])} sleep 0.1 mouseup 1`
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
/** Scrolls the wheel at the pointer, `amount` notches (default 3). */
|
|
366
|
+
async scroll(opts) {
|
|
367
|
+
const wheel = WHEEL[opts.direction];
|
|
368
|
+
const n = opts.amount ?? 3;
|
|
369
|
+
if (!wheel)
|
|
370
|
+
throw new InstaboxError(`direction must be up, down, left or right; got ${opts.direction}`, {
|
|
371
|
+
code: "bad_request"
|
|
372
|
+
});
|
|
373
|
+
if (!Number.isInteger(n) || n < 1 || n > 100) {
|
|
374
|
+
throw new InstaboxError(`amount must be a whole number from 1 to 100; got ${n}`, { code: "bad_request" });
|
|
375
|
+
}
|
|
376
|
+
await this.xdo(`click --repeat ${n} --delay 30 ${wheel}`);
|
|
377
|
+
}
|
|
378
|
+
/** Types `text` as is (quotes, newlines and Unicode included) into the focused window. */
|
|
379
|
+
async type(text, opts = {}) {
|
|
380
|
+
if (typeof text !== "string") throw new InstaboxError("text must be a string", { code: "bad_request" });
|
|
381
|
+
if (!text) return;
|
|
382
|
+
const delay = opts.delayMs ?? 12;
|
|
383
|
+
if (!Number.isInteger(delay) || delay < 0 || delay > 1e3) {
|
|
384
|
+
throw new InstaboxError(`delayMs must be a whole number from 0 to 1000; got ${delay}`, { code: "bad_request" });
|
|
385
|
+
}
|
|
386
|
+
await this.xdo(`type --delay ${delay} --file -`, text);
|
|
387
|
+
}
|
|
388
|
+
/** Presses a key (`'Enter'`, `'a'`, `'F5'`) or a chord (`['ctrl', 'l']`). */
|
|
389
|
+
async press(keys) {
|
|
390
|
+
const list = Array.isArray(keys) ? keys : [keys];
|
|
391
|
+
if (!list.length) throw new InstaboxError("press needs a key", { code: "bad_request" });
|
|
392
|
+
await this.xdo(`key -- ${shellQuote(list.map(keysym).join("+"))}`);
|
|
393
|
+
}
|
|
394
|
+
/** Opens `url` (http or https) in the desktop's browser. */
|
|
395
|
+
async open(url) {
|
|
396
|
+
let u;
|
|
397
|
+
try {
|
|
398
|
+
u = new URL(url);
|
|
399
|
+
} catch {
|
|
400
|
+
throw new InstaboxError(`invalid URL: ${url}`, { code: "bad_request" });
|
|
401
|
+
}
|
|
402
|
+
if (u.protocol !== "http:" && u.protocol !== "https:") {
|
|
403
|
+
throw new InstaboxError(`only http and https URLs open in the browser; got ${u.protocol}`, {
|
|
404
|
+
code: "bad_request"
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
await this.run(
|
|
408
|
+
`b=$(command -v chromium || command -v chromium-browser || command -v google-chrome) || { echo 'no browser in the sandbox' >&2; exit 127; }; setsid nohup "$b" --no-first-run --no-default-browser-check --password-store=basic ${shellQuote(u.href)} >/dev/null 2>&1 </dev/null &`
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
/** Where the pointer is. */
|
|
412
|
+
async cursorPosition() {
|
|
413
|
+
const out = (await this.xdo("getmouselocation --shell")).stdout;
|
|
414
|
+
const x = Number(/^X=(\d+)/m.exec(out)?.[1]);
|
|
415
|
+
const y = Number(/^Y=(\d+)/m.exec(out)?.[1]);
|
|
416
|
+
if (!Number.isInteger(x) || !Number.isInteger(y)) {
|
|
417
|
+
throw new InstaboxError("could not read the pointer position", { code: "invalid_response" });
|
|
418
|
+
}
|
|
419
|
+
return { x, y };
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* A link that shows the desktop full-screen in a browser, for people without an instabox account:
|
|
423
|
+
* open it or embed it in an iframe. View-only by default (the server drops the viewer's input).
|
|
424
|
+
* `ttlSeconds`: 60 to 86 400, default 3 600. The expiry stops new connections only; a viewer already
|
|
425
|
+
* watching stays until they leave or `revokeStreamUrls()` is called.
|
|
426
|
+
*/
|
|
427
|
+
async streamUrl(opts = {}) {
|
|
428
|
+
const body = { viewOnly: opts.viewOnly ?? true };
|
|
429
|
+
if (opts.ttlSeconds !== void 0) {
|
|
430
|
+
if (!Number.isInteger(opts.ttlSeconds) || opts.ttlSeconds < 60 || opts.ttlSeconds > 86400) {
|
|
431
|
+
throw new InstaboxError(`ttlSeconds must be a whole number from 60 to 86 400; got ${opts.ttlSeconds}`, {
|
|
432
|
+
code: "bad_request"
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
body.ttlSeconds = opts.ttlSeconds;
|
|
436
|
+
}
|
|
437
|
+
const r = await this.http.request(
|
|
438
|
+
"POST",
|
|
439
|
+
`/v1/machines/${enc(this.sandboxId)}/desktop/stream`,
|
|
440
|
+
{ body }
|
|
441
|
+
);
|
|
442
|
+
return { url: r.url, expiresAt: new Date(r.expiresAt) };
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Revokes every link `streamUrl()` made for this sandbox: they stop working and open viewers are
|
|
446
|
+
* disconnected within a few seconds. Links made afterwards work.
|
|
447
|
+
*/
|
|
448
|
+
async revokeStreamUrls() {
|
|
449
|
+
await this.http.request("DELETE", `/v1/machines/${enc(this.sandboxId)}/desktop/stream`);
|
|
450
|
+
}
|
|
451
|
+
call(body) {
|
|
452
|
+
return this.http.request("POST", `/v1/machines/${enc(this.sandboxId)}/desktop`, {
|
|
453
|
+
body,
|
|
454
|
+
timeoutMs: 9e4
|
|
455
|
+
});
|
|
456
|
+
}
|
|
457
|
+
/** Runs `xdotool <args>` on the desktop's display. */
|
|
458
|
+
xdo(args, stdin) {
|
|
459
|
+
return this.run(`xdotool ${args}`, stdin);
|
|
460
|
+
}
|
|
461
|
+
async run(cmd, stdin) {
|
|
462
|
+
const r = await this.commands.exec(
|
|
463
|
+
`export DISPLAY=${DESKTOP_DISPLAY}; ${cmd}`,
|
|
464
|
+
stdin === void 0 ? {} : { stdin }
|
|
465
|
+
);
|
|
466
|
+
if (r.exitCode !== 0) throw new CommandExitError(r);
|
|
467
|
+
return r;
|
|
468
|
+
}
|
|
469
|
+
};
|
|
470
|
+
|
|
214
471
|
// src/files.ts
|
|
215
472
|
var MAX_FILE_BYTES = 10 * 1024 * 1024;
|
|
216
473
|
var WRITE_CHUNK_B64 = 48 * 1024;
|
|
@@ -223,18 +480,19 @@ var Files = class {
|
|
|
223
480
|
commands;
|
|
224
481
|
async read(path, opts = {}) {
|
|
225
482
|
const p = arg(path);
|
|
483
|
+
const limit = opts.maxBytes ?? MAX_FILE_BYTES;
|
|
226
484
|
const r = await this.commands.exec(
|
|
227
|
-
`[ -f ${p} ] || { echo "no such file: "${p} >&2; exit 3; }; s=$(wc -c < ${p}) || exit 1; [ "$s" -le ${
|
|
485
|
+
`[ -f ${p} ] || { echo "no such file: "${p} >&2; exit 3; }; s=$(wc -c < ${p}) || exit 1; [ "$s" -le ${limit} ] || { echo "file too large: $s bytes" >&2; exit 4; }; printf '%s' "$s"`
|
|
228
486
|
);
|
|
229
487
|
if (r.exitCode === 3) throw new InstaboxError(`file not found: ${path}`, { status: 404, code: "not_found" });
|
|
230
488
|
if (r.exitCode === 4) {
|
|
231
|
-
throw new InstaboxError(`${path} is larger than the ${
|
|
489
|
+
throw new InstaboxError(`${path} is larger than the ${limit}-byte limit of files.read`, {
|
|
232
490
|
code: "file_too_large"
|
|
233
491
|
});
|
|
234
492
|
}
|
|
235
493
|
ensureComplete(r);
|
|
236
494
|
const size = Number(r.stdout.trim());
|
|
237
|
-
if (!Number.isSafeInteger(size) || size < 0 || size >
|
|
495
|
+
if (!Number.isSafeInteger(size) || size < 0 || size > limit) {
|
|
238
496
|
throw new InstaboxError("invalid file size returned by exec", { code: "invalid_response" });
|
|
239
497
|
}
|
|
240
498
|
const bytes = new Uint8Array(size);
|
|
@@ -503,7 +761,7 @@ function toSeconds(ms, field = "timeoutMs") {
|
|
|
503
761
|
return s;
|
|
504
762
|
}
|
|
505
763
|
var METADATA_KEY = /^[A-Za-z0-9_.-]{1,64}$/;
|
|
506
|
-
var
|
|
764
|
+
var enc2 = encodeURIComponent;
|
|
507
765
|
var Sandbox = class _Sandbox {
|
|
508
766
|
constructor(http, machine, opts = {}) {
|
|
509
767
|
this.http = http;
|
|
@@ -511,6 +769,7 @@ var Sandbox = class _Sandbox {
|
|
|
511
769
|
this.machine = machine;
|
|
512
770
|
this.commands = new Commands(http, machine.id, { ...opts.envs });
|
|
513
771
|
this.files = new Files(this.commands);
|
|
772
|
+
this.desktop = new Desktop(http, machine.id, this.commands, this.files);
|
|
514
773
|
}
|
|
515
774
|
http;
|
|
516
775
|
/** The machine ID (`m_...`). */
|
|
@@ -519,6 +778,8 @@ var Sandbox = class _Sandbox {
|
|
|
519
778
|
commands;
|
|
520
779
|
/** Read and write files. Needs a instabox server with the exec endpoint. */
|
|
521
780
|
files;
|
|
781
|
+
/** See and control the sandbox's graphical desktop (screenshots, mouse, keyboard, a link to watch it). */
|
|
782
|
+
desktop;
|
|
522
783
|
machine;
|
|
523
784
|
static async create(a, b) {
|
|
524
785
|
const opts = (typeof a === "string" ? b : a) ?? {};
|
|
@@ -579,12 +840,12 @@ var Sandbox = class _Sandbox {
|
|
|
579
840
|
}
|
|
580
841
|
/** Fetches a snapshot by ID. Requires images:read. */
|
|
581
842
|
static async getSnapshot(snapshotId, opts = {}) {
|
|
582
|
-
return new HttpClient(opts).request("GET", `/v1/snapshots/${
|
|
843
|
+
return new HttpClient(opts).request("GET", `/v1/snapshots/${enc2(snapshotId)}`);
|
|
583
844
|
}
|
|
584
845
|
/** Deletes a snapshot. Returns false if absent. Requires images:delete. */
|
|
585
846
|
static async deleteSnapshot(snapshotId, opts = {}) {
|
|
586
847
|
try {
|
|
587
|
-
await new HttpClient(opts).request("DELETE", `/v1/snapshots/${
|
|
848
|
+
await new HttpClient(opts).request("DELETE", `/v1/snapshots/${enc2(snapshotId)}`);
|
|
588
849
|
return true;
|
|
589
850
|
} catch (err) {
|
|
590
851
|
if (err instanceof InstaboxError && err.status === 404 && err.code === "not_found") return false;
|
|
@@ -599,7 +860,7 @@ var Sandbox = class _Sandbox {
|
|
|
599
860
|
async createSnapshot(opts) {
|
|
600
861
|
await this.settle();
|
|
601
862
|
await this.untilSynced();
|
|
602
|
-
return this.http.request("POST", `/v1/machines/${
|
|
863
|
+
return this.http.request("POST", `/v1/machines/${enc2(this.sandboxId)}/snapshots`, { body: opts });
|
|
603
864
|
}
|
|
604
865
|
/** Fetches the current state of the sandbox. */
|
|
605
866
|
async getInfo() {
|
|
@@ -613,20 +874,20 @@ var Sandbox = class _Sandbox {
|
|
|
613
874
|
async setTimeout(timeoutMs, opts = {}) {
|
|
614
875
|
const body = { timeoutSeconds: timeoutMs === 0 ? 0 : toSeconds(timeoutMs) };
|
|
615
876
|
if (opts.onTimeout) body.onTimeout = opts.onTimeout;
|
|
616
|
-
this.machine = await this.http.request("PATCH", `/v1/machines/${
|
|
877
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc2(this.sandboxId)}`, { body });
|
|
617
878
|
}
|
|
618
879
|
/**
|
|
619
880
|
* Pauses the sandbox once it has been idle `idleMs` (whole seconds, 2 min to 24 h); 0 turns the idle
|
|
620
881
|
* pause off. Unlike a timeout the setting stays across pause and resume.
|
|
621
882
|
*/
|
|
622
883
|
async setAutoSuspend(idleMs) {
|
|
623
|
-
this.machine = await this.http.request("PATCH", `/v1/machines/${
|
|
884
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc2(this.sandboxId)}`, {
|
|
624
885
|
body: { autoSuspendAfterSeconds: toIdleSeconds(idleMs) }
|
|
625
886
|
});
|
|
626
887
|
}
|
|
627
888
|
/** Replaces the sandbox's metadata (`{}` clears it). */
|
|
628
889
|
async setMetadata(metadata) {
|
|
629
|
-
this.machine = await this.http.request("PATCH", `/v1/machines/${
|
|
890
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc2(this.sandboxId)}`, {
|
|
630
891
|
body: { metadata }
|
|
631
892
|
});
|
|
632
893
|
}
|
|
@@ -636,21 +897,21 @@ var Sandbox = class _Sandbox {
|
|
|
636
897
|
* Throws `not_found` on a server without the Vault.
|
|
637
898
|
*/
|
|
638
899
|
async vault() {
|
|
639
|
-
return this.http.request("GET", `/v1/machines/${
|
|
900
|
+
return this.http.request("GET", `/v1/machines/${enc2(this.sandboxId)}/vault`);
|
|
640
901
|
}
|
|
641
902
|
/**
|
|
642
903
|
* Writes the account's current Vault items into the running sandbox now, the same write as after a boot,
|
|
643
904
|
* and returns its record (`error` set when the guest refused it). Requires machines:write.
|
|
644
905
|
*/
|
|
645
906
|
async pushVault() {
|
|
646
|
-
return this.http.request("POST", `/v1/machines/${
|
|
907
|
+
return this.http.request("POST", `/v1/machines/${enc2(this.sandboxId)}/vault`);
|
|
647
908
|
}
|
|
648
909
|
/**
|
|
649
910
|
* Turns the Vault off for this sandbox, for good, in any state: its next boot, resume or push removes
|
|
650
911
|
* what the Vault wrote and nothing is written again. Requires machines:write.
|
|
651
912
|
*/
|
|
652
913
|
async disableVault() {
|
|
653
|
-
this.machine = await this.http.request("PATCH", `/v1/machines/${
|
|
914
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc2(this.sandboxId)}`, {
|
|
654
915
|
body: { vault: false }
|
|
655
916
|
});
|
|
656
917
|
}
|
|
@@ -702,7 +963,7 @@ var Sandbox = class _Sandbox {
|
|
|
702
963
|
if (opts.autoSuspendMs !== void 0) body.autoSuspendAfterSeconds = toIdleSeconds(opts.autoSuspendMs);
|
|
703
964
|
if (opts.metadata) body.metadata = opts.metadata;
|
|
704
965
|
if (opts.vault === false) body.vault = false;
|
|
705
|
-
const op = await this.http.request("POST", `/v1/machines/${
|
|
966
|
+
const op = await this.http.request("POST", `/v1/machines/${enc2(this.sandboxId)}/fork`, { body });
|
|
706
967
|
const done = await this.http.waitForOperation(op);
|
|
707
968
|
return new _Sandbox(this.http, await getMachine(this.http, done.machineId));
|
|
708
969
|
}
|
|
@@ -725,7 +986,7 @@ var Sandbox = class _Sandbox {
|
|
|
725
986
|
}
|
|
726
987
|
body.ttlSeconds = opts.ttlSeconds;
|
|
727
988
|
}
|
|
728
|
-
const c = await this.http.request("POST", `/v1/machines/${
|
|
989
|
+
const c = await this.http.request("POST", `/v1/machines/${enc2(this.sandboxId)}/connections`, { body });
|
|
729
990
|
return {
|
|
730
991
|
host: c.host,
|
|
731
992
|
port: c.port,
|
|
@@ -744,7 +1005,7 @@ var Sandbox = class _Sandbox {
|
|
|
744
1005
|
async settle(refresh = true) {
|
|
745
1006
|
if (refresh) this.machine = await getMachine(this.http, this.sandboxId);
|
|
746
1007
|
if (this.machine.operationId) {
|
|
747
|
-
const op = await this.http.request("GET", `/v1/operations/${
|
|
1008
|
+
const op = await this.http.request("GET", `/v1/operations/${enc2(this.machine.operationId)}`);
|
|
748
1009
|
await this.http.waitForOperation(op).catch(() => {
|
|
749
1010
|
});
|
|
750
1011
|
this.machine = await getMachine(this.http, this.sandboxId);
|
|
@@ -772,17 +1033,17 @@ var Sandbox = class _Sandbox {
|
|
|
772
1033
|
}
|
|
773
1034
|
}
|
|
774
1035
|
async mutate(method, action) {
|
|
775
|
-
const op = await this.http.request(method, `/v1/machines/${
|
|
1036
|
+
const op = await this.http.request(method, `/v1/machines/${enc2(this.sandboxId)}/${action}`);
|
|
776
1037
|
await this.http.waitForOperation(op);
|
|
777
1038
|
this.machine = await getMachine(this.http, this.sandboxId);
|
|
778
1039
|
}
|
|
779
1040
|
};
|
|
780
1041
|
async function getMachine(http, id) {
|
|
781
|
-
return http.request("GET", `/v1/machines/${
|
|
1042
|
+
return http.request("GET", `/v1/machines/${enc2(id)}`);
|
|
782
1043
|
}
|
|
783
1044
|
async function killMachine(http, id) {
|
|
784
1045
|
try {
|
|
785
|
-
const op = await http.request("DELETE", `/v1/machines/${
|
|
1046
|
+
const op = await http.request("DELETE", `/v1/machines/${enc2(id)}`);
|
|
786
1047
|
await http.waitForOperation(op);
|
|
787
1048
|
return true;
|
|
788
1049
|
} catch (err) {
|