@insforge/instabox-sdk 0.7.0 → 0.8.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 +32 -10
- package/dist/index.cjs +150 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +49 -1
- package/dist/index.d.ts +49 -1
- package/dist/index.js +150 -52
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/skills/instabox/SKILL.md +10 -8
package/README.md
CHANGED
|
@@ -32,14 +32,14 @@ The key is sent as the `X-API-Key` header.
|
|
|
32
32
|
|
|
33
33
|
### Scopes
|
|
34
34
|
|
|
35
|
-
| Scope | Allows
|
|
36
|
-
| ----------------- |
|
|
37
|
-
| `machines:read` | `Sandbox.list`, `getInfo`, `isRunning`, `vault`
|
|
38
|
-
| `machines:write` | `create`, `connect` (resume), `pause`, `resume`, `stop`, `setAutoSuspend`, `setMetadata`, `disableVault`, `pushVault`, `commands`, `files`, `sshCredentials` |
|
|
39
|
-
| `machines:delete` | `kill`
|
|
40
|
-
| `images:read` | Resolve private snapshot names, `listSnapshots`, `getSnapshot`
|
|
41
|
-
| `images:write` | `createSnapshot`
|
|
42
|
-
| `images:delete` | `deleteSnapshot`
|
|
35
|
+
| Scope | Allows |
|
|
36
|
+
| ----------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
|
37
|
+
| `machines:read` | `Sandbox.list`, `getInfo`, `isRunning`, `vault`, `ports.list` |
|
|
38
|
+
| `machines:write` | `create`, `connect` (resume), `pause`, `resume`, `stop`, `setAutoSuspend`, `setMetadata`, `disableVault`, `pushVault`, `commands`, `files`, `sshCredentials`, `ports.publish`, `ports.unpublish` |
|
|
39
|
+
| `machines:delete` | `kill` |
|
|
40
|
+
| `images:read` | Resolve private snapshot names, `listSnapshots`, `getSnapshot` |
|
|
41
|
+
| `images:write` | `createSnapshot` |
|
|
42
|
+
| `images:delete` | `deleteSnapshot` |
|
|
43
43
|
|
|
44
44
|
`:write` and `:delete` imply `:read`. Creating from a template or a snapshot ID needs only machine scopes. Creating from a snapshot name also needs `images:read`. Snapshot permissions are named `images:*` by the current API. Add `images:write` and `images:delete` if your code saves and deletes snapshots. A call outside the key's scopes throws `InstaboxAuthError` (`403 insufficient_scope`).
|
|
45
45
|
|
|
@@ -170,7 +170,7 @@ const { stdout } = await sandbox.commands.run('sha256sum', { stdin: new Uint8Arr
|
|
|
170
170
|
- What `download` brings: everything under `remoteDir`, dot files and empty directories included.
|
|
171
171
|
- Symlinks are copied as symlinks (their target text as is), never followed. Sockets, FIFOs and devices are skipped. File and directory permission bits are kept; owners are not (files belong to the user that unpacks them).
|
|
172
172
|
- The destination and its parents are created. Existing files there are replaced; files the archive does not name are kept (a merge, not a mirror).
|
|
173
|
-
- `download` refuses an archive entry that would land outside `localDir` (an absolute path, `..`, or a path through a symlink), with `invalid_response
|
|
173
|
+
- `download` refuses an archive entry that would land outside `localDir` (an absolute path, `..`, or a path through a symlink), with `invalid_response` On Windows it also refuses names Windows would not keep as they are: a `:` anywhere, a device name (`CON`, `NUL.txt`, `COM1`...) or a trailing dot or space.
|
|
174
174
|
- Both return a summary: `upload` resolves to `{ path, files, bytes }` (the resolved sandbox directory, regular files sent, archive size); `download` to `{ path, remotePath, files, bytes }`.
|
|
175
175
|
- Errors: a missing local directory throws `local_not_found`, a local error `local_io_error`; a missing sandbox directory `not_found` (404); a file where a directory was expected `not_a_directory`; a failure of `tar` in the sandbox `transfer_failed` (`permission_denied`, 403, when it was refused access). Every error is an `InstaboxError`.
|
|
176
176
|
- They need Node.js (or Bun, Deno) for the local file system; in a browser or a Worker they throw `unsupported`. `read`, `readStream`, `write` and everything else work there too.
|
|
@@ -372,6 +372,21 @@ async function onCancel(userId: string) {
|
|
|
372
372
|
|
|
373
373
|
The desktop must be running when the link is opened: a link to a paused sandbox does not wake it, so `connect()` before you hand it out. An open viewer does not count as activity, so with the default idle pause a sandbox someone only watches still pauses after 30 idle minutes. Each open viewer counts toward your account's limit of open terminals and desktops (8 unless we raised it for you). Keep `vault: false` for sandboxes your users can use: anything running in one can read what the Vault writes.
|
|
374
374
|
|
|
375
|
+
## Ports: a URL for a server in the sandbox
|
|
376
|
+
|
|
377
|
+
`sandbox.ports` gives a server listening in the sandbox an HTTPS URL, as `instabox port publish` does. WebSockets and streaming responses pass through.
|
|
378
|
+
|
|
379
|
+
```ts
|
|
380
|
+
const { url } = await sandbox.ports.publish(3000) // https://3000-<id>.instabox.tech on the hosted service
|
|
381
|
+
const web = await sandbox.ports.publish(8080, { name: 'web', access: 'private' })
|
|
382
|
+
console.log(web.tokenUrl) // the URL with ?_token=ibp_..., the link to share
|
|
383
|
+
await sandbox.ports.publish(8080, { access: 'private', rotateToken: true }) // new token, the old one stops working
|
|
384
|
+
await sandbox.ports.list() // [{ port, name, protocol, url, access, createdAt }]
|
|
385
|
+
await sandbox.ports.unpublish(3000) // false if it was not published
|
|
386
|
+
```
|
|
387
|
+
|
|
388
|
+
A port stays published until you unpublish it, and publishing it again keeps its URL. The URL answers while the sandbox runs. Ports run from 1024 to 65535, except the desktop's and Orca's control ports. A new port is public: anyone with the URL reaches it. A private port answers only requests that carry its token, as the link's `_token` parameter (the browser then keeps it in a cookie) or an `Authorization: Bearer` header. Only the publish that creates or rotates a token returns it. The domain is the server's, so use the returned `url` instead of building one. Publishing and unpublishing need `machines:write`, listing needs `machines:read`.
|
|
389
|
+
|
|
375
390
|
## Metrics and usage
|
|
376
391
|
|
|
377
392
|
```ts
|
|
@@ -422,6 +437,7 @@ Reads, and mutations (which always carry an `Idempotency-Key`), are retried up t
|
|
|
422
437
|
- At most 32 requests per user run at once, across all your sandboxes. Commands, file reads, desktop, port-forward and vault-push calls share this limit. A `background: true` command counts only while it starts; each `wait()` holds one while it waits (it polls the job's log, up to 25 seconds per request). More throw `InstaboxQuotaError` (429, retryable).
|
|
423
438
|
- A command started with `run` runs for at most 10 minutes; longer ones need `background: true`. A background command takes no `stdin`, and its stdout and stderr share one log.
|
|
424
439
|
- A file moves at up to 5 GiB per `write` (the server's limit) in one request; the CLI's `instabox push` splits a file of 64 MiB or more into 4 parallel uploads, which the SDK does not do yet.
|
|
440
|
+
- Published ports serve at most 128 requests at once per account, across all its sandboxes. Open WebSockets and streams count until they close. A request over the limit waits up to 2 seconds for a slot, then gets `429`.
|
|
425
441
|
|
|
426
442
|
## Agent skill
|
|
427
443
|
|
|
@@ -449,4 +465,10 @@ git tag v0.4.0 && git push origin v0.4.0
|
|
|
449
465
|
The Release workflow checks that the tag is the package's version and that it is not on npm yet, runs the
|
|
450
466
|
checks and publishes through npm trusted publishing (OIDC): no token is stored anywhere. Once, a maintainer
|
|
451
467
|
adds the trusted publisher on npmjs.com (the package's Settings, Trusted Publisher, GitHub Actions: organization
|
|
452
|
-
`InsForge`, repository `instabox-sdk`, workflow `release.yml`, no environment).
|
|
468
|
+
`InsForge`, repository `instabox-sdk`, workflow `release.yml`, no environment) with **Allow npm dist-tag** on.
|
|
469
|
+
|
|
470
|
+
The new version is published under the `staging` dist-tag. `latest` (`next` for a prerelease) moves to it only
|
|
471
|
+
once its tarball downloads and installs from a clean cache, which can take a few minutes after the publish
|
|
472
|
+
(`scripts/npm-promote-latest.sh`, at most 15 minutes). If it never installs in that time, or npm refuses the
|
|
473
|
+
dist-tag, `latest` stays on the previous version and the run fails; re-run it once npm serves the tarball and it
|
|
474
|
+
promotes the version without publishing it again. `npm view @insforge/instabox-sdk dist-tags` shows both tags.
|
package/dist/index.cjs
CHANGED
|
@@ -47,6 +47,7 @@ __export(index_exports, {
|
|
|
47
47
|
MAX_FILE_BYTES: () => MAX_FILE_BYTES,
|
|
48
48
|
MAX_STDIN_BYTES: () => MAX_STDIN_BYTES,
|
|
49
49
|
MAX_UPLOAD_BYTES: () => MAX_UPLOAD_BYTES,
|
|
50
|
+
Ports: () => Ports,
|
|
50
51
|
Sandbox: () => Sandbox,
|
|
51
52
|
isLimitCode: () => isLimitCode
|
|
52
53
|
});
|
|
@@ -355,7 +356,7 @@ ${cmd}`;
|
|
|
355
356
|
try {
|
|
356
357
|
r = o.onStdout || o.onStderr ? await this.execStream(cmd, o) : await this.exec(cmd, o);
|
|
357
358
|
} finally {
|
|
358
|
-
if (staged
|
|
359
|
+
if (staged) await this.exec(`rm -f -- ${shellQuote(staged)}`, { noHistory: true }).catch(() => {
|
|
359
360
|
});
|
|
360
361
|
}
|
|
361
362
|
if (r.exitCode !== 0) throw new CommandExitError(r);
|
|
@@ -1329,24 +1330,44 @@ function unsafe(name) {
|
|
|
1329
1330
|
code: "invalid_response"
|
|
1330
1331
|
});
|
|
1331
1332
|
}
|
|
1332
|
-
|
|
1333
|
+
var RESERVED = /* @__PURE__ */ new Set([
|
|
1334
|
+
"CON",
|
|
1335
|
+
"PRN",
|
|
1336
|
+
"AUX",
|
|
1337
|
+
"NUL",
|
|
1338
|
+
"CONIN$",
|
|
1339
|
+
"CONOUT$",
|
|
1340
|
+
...["COM", "LPT"].flatMap((d) => [1, 2, 3, 4, 5, 6, 7, 8, 9, "\xB9", "\xB2", "\xB3"].map((i) => `${d}${i}`))
|
|
1341
|
+
]);
|
|
1342
|
+
var WINDOWS = globalThis.process?.platform === "win32";
|
|
1343
|
+
function components(name, windows = WINDOWS) {
|
|
1333
1344
|
if (name.startsWith("/")) throw unsafe(name);
|
|
1334
1345
|
const parts = [];
|
|
1335
1346
|
for (const p of name.split("/")) {
|
|
1336
1347
|
if (p === "" || p === ".") continue;
|
|
1337
1348
|
if (p === ".." || p.includes("\\") || p.includes("\0")) throw unsafe(name);
|
|
1349
|
+
if (windows && (p.includes(":") || p.endsWith(".") || p.endsWith(" ") || RESERVED.has(p.split(".")[0].replace(/ +$/, "").toUpperCase()))) {
|
|
1350
|
+
throw unsafe(name);
|
|
1351
|
+
}
|
|
1338
1352
|
parts.push(p);
|
|
1339
1353
|
}
|
|
1340
1354
|
return parts;
|
|
1341
1355
|
}
|
|
1342
|
-
async function unpackLocal(n, src, dest) {
|
|
1356
|
+
async function unpackLocal(n, src, dest, windows = WINDOWS) {
|
|
1343
1357
|
await n.fsp.mkdir(dest, { recursive: true });
|
|
1344
1358
|
const root = await n.fsp.realpath(dest);
|
|
1345
1359
|
const gunzip = n.zlib.createGunzip();
|
|
1346
1360
|
n.stream.pipeline(n.stream.Readable.from(src, { objectMode: false }), gunzip, () => {
|
|
1347
1361
|
});
|
|
1348
|
-
const dirModes =
|
|
1362
|
+
const dirModes = /* @__PURE__ */ new Map();
|
|
1349
1363
|
let files = 0;
|
|
1364
|
+
const writable = async (abs, st) => {
|
|
1365
|
+
const mode = st.mode & 4095;
|
|
1366
|
+
if ((mode & 192) === 192 || dirModes.has(abs)) return;
|
|
1367
|
+
dirModes.set(abs, mode);
|
|
1368
|
+
await n.fsp.chmod(abs, mode | 192);
|
|
1369
|
+
};
|
|
1370
|
+
await writable(root, await n.fsp.stat(root));
|
|
1350
1371
|
const ensureDir = async (parts, name) => {
|
|
1351
1372
|
let cur = root;
|
|
1352
1373
|
for (const p of parts) {
|
|
@@ -1359,6 +1380,7 @@ async function unpackLocal(n, src, dest) {
|
|
|
1359
1380
|
}
|
|
1360
1381
|
if (!st) await n.fsp.mkdir(cur, { mode: 493 });
|
|
1361
1382
|
else if (!st.isDirectory()) throw unsafe(name);
|
|
1383
|
+
else await writable(cur, st);
|
|
1362
1384
|
}
|
|
1363
1385
|
return cur;
|
|
1364
1386
|
};
|
|
@@ -1376,39 +1398,49 @@ async function unpackLocal(n, src, dest) {
|
|
|
1376
1398
|
}
|
|
1377
1399
|
await n.fsp.unlink(abs);
|
|
1378
1400
|
};
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1401
|
+
let ok = false;
|
|
1402
|
+
try {
|
|
1403
|
+
for await (const e of untarStream(gunzip)) {
|
|
1404
|
+
const parts = components(e.name, windows);
|
|
1405
|
+
if (parts.length === 0) {
|
|
1406
|
+
for await (const _ of e.data) void _;
|
|
1407
|
+
continue;
|
|
1408
|
+
}
|
|
1409
|
+
const parent = await ensureDir(parts.slice(0, -1), e.name);
|
|
1410
|
+
const abs = n.path.join(parent, parts[parts.length - 1]);
|
|
1411
|
+
if (e.type === "dir") {
|
|
1412
|
+
await ensureDir(parts, e.name);
|
|
1413
|
+
dirModes.set(abs, e.mode);
|
|
1414
|
+
} else if (e.type === "file") {
|
|
1415
|
+
await clear(abs, e.name);
|
|
1416
|
+
await n.sp.pipeline(
|
|
1417
|
+
n.stream.Readable.from(e.data, { objectMode: false }),
|
|
1418
|
+
n.fs.createWriteStream(abs, { flags: "wx", mode: e.mode | 128 })
|
|
1419
|
+
);
|
|
1420
|
+
await n.fsp.chmod(abs, e.mode);
|
|
1421
|
+
files++;
|
|
1422
|
+
} else if (e.type === "symlink") {
|
|
1423
|
+
await clear(abs, e.name);
|
|
1424
|
+
await n.fsp.symlink(e.linkname, abs);
|
|
1425
|
+
} else if (e.type === "link") {
|
|
1426
|
+
const tparts = components(e.linkname, windows);
|
|
1427
|
+
const target = n.path.join(await ensureDir(tparts.slice(0, -1), e.linkname), tparts[tparts.length - 1] ?? "");
|
|
1428
|
+
const st = await n.fsp.lstat(target).catch(() => void 0);
|
|
1429
|
+
if (!st?.isFile()) throw unsafe(e.linkname);
|
|
1430
|
+
await clear(abs, e.name);
|
|
1431
|
+
await n.fsp.copyFile(target, abs);
|
|
1432
|
+
files++;
|
|
1433
|
+
}
|
|
1384
1434
|
}
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
n.fs.createWriteStream(abs, { flags: "wx", mode: e.mode | 128 })
|
|
1395
|
-
);
|
|
1396
|
-
await n.fsp.chmod(abs, e.mode);
|
|
1397
|
-
files++;
|
|
1398
|
-
} else if (e.type === "symlink") {
|
|
1399
|
-
await clear(abs, e.name);
|
|
1400
|
-
await n.fsp.symlink(e.linkname, abs);
|
|
1401
|
-
} else if (e.type === "link") {
|
|
1402
|
-
const tparts = components(e.linkname);
|
|
1403
|
-
const target = n.path.join(await ensureDir(tparts.slice(0, -1), e.linkname), tparts[tparts.length - 1] ?? "");
|
|
1404
|
-
const st = await n.fsp.lstat(target).catch(() => void 0);
|
|
1405
|
-
if (!st?.isFile()) throw unsafe(e.linkname);
|
|
1406
|
-
await clear(abs, e.name);
|
|
1407
|
-
await n.fsp.copyFile(target, abs);
|
|
1408
|
-
files++;
|
|
1409
|
-
}
|
|
1410
|
-
}
|
|
1411
|
-
for (const [abs, mode] of dirModes.reverse()) await n.fsp.chmod(abs, mode);
|
|
1435
|
+
ok = true;
|
|
1436
|
+
} finally {
|
|
1437
|
+
const depth = (p) => p.split(n.path.sep).length;
|
|
1438
|
+
for (const [abs, mode] of [...dirModes].sort((a, b) => depth(b[0]) - depth(a[0]))) {
|
|
1439
|
+
await n.fsp.chmod(abs, mode).catch((err) => {
|
|
1440
|
+
if (ok) throw err;
|
|
1441
|
+
});
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1412
1444
|
return files;
|
|
1413
1445
|
}
|
|
1414
1446
|
|
|
@@ -2464,6 +2496,69 @@ var HttpClient = class {
|
|
|
2464
2496
|
}
|
|
2465
2497
|
};
|
|
2466
2498
|
|
|
2499
|
+
// src/ports.ts
|
|
2500
|
+
var enc3 = encodeURIComponent;
|
|
2501
|
+
function checkPort(port) {
|
|
2502
|
+
if (!Number.isInteger(port)) {
|
|
2503
|
+
throw new InstaboxError(`port must be a whole number; got ${port}`, { code: "bad_request" });
|
|
2504
|
+
}
|
|
2505
|
+
return port;
|
|
2506
|
+
}
|
|
2507
|
+
function fromServer(p) {
|
|
2508
|
+
const out = {
|
|
2509
|
+
port: p.port,
|
|
2510
|
+
name: p.name,
|
|
2511
|
+
protocol: p.protocol,
|
|
2512
|
+
url: p.url,
|
|
2513
|
+
access: p.access,
|
|
2514
|
+
createdAt: new Date(p.createdAt)
|
|
2515
|
+
};
|
|
2516
|
+
if (p.token) {
|
|
2517
|
+
out.token = p.token;
|
|
2518
|
+
out.tokenUrl = p.tokenUrl;
|
|
2519
|
+
}
|
|
2520
|
+
return out;
|
|
2521
|
+
}
|
|
2522
|
+
var Ports = class {
|
|
2523
|
+
/** @internal */
|
|
2524
|
+
constructor(http, sandboxId) {
|
|
2525
|
+
this.http = http;
|
|
2526
|
+
this.sandboxId = sandboxId;
|
|
2527
|
+
}
|
|
2528
|
+
http;
|
|
2529
|
+
sandboxId;
|
|
2530
|
+
/**
|
|
2531
|
+
* Publishes `port` (1024-65535) until `unpublish()`; publishing it again keeps its URL. The URL answers
|
|
2532
|
+
* while the sandbox runs, WebSockets included. Requires machines:write.
|
|
2533
|
+
*/
|
|
2534
|
+
async publish(port, opts = {}) {
|
|
2535
|
+
const body = { port: checkPort(port) };
|
|
2536
|
+
if (opts.name !== void 0) body.name = opts.name;
|
|
2537
|
+
if (opts.access !== void 0) body.access = opts.access;
|
|
2538
|
+
if (opts.rotateToken) body.rotateToken = true;
|
|
2539
|
+
const r = await this.http.request("POST", `/v1/machines/${enc3(this.sandboxId)}/ports`, { body });
|
|
2540
|
+
return fromServer(r);
|
|
2541
|
+
}
|
|
2542
|
+
/** The sandbox's published ports. Tokens are not included. Requires machines:read. */
|
|
2543
|
+
async list() {
|
|
2544
|
+
const r = await this.http.request("GET", `/v1/machines/${enc3(this.sandboxId)}/ports`);
|
|
2545
|
+
return r.ports.map(fromServer);
|
|
2546
|
+
}
|
|
2547
|
+
/**
|
|
2548
|
+
* Removes the port's URL and closes its open connections. Returns false if the port was not published.
|
|
2549
|
+
* Requires machines:write.
|
|
2550
|
+
*/
|
|
2551
|
+
async unpublish(port) {
|
|
2552
|
+
try {
|
|
2553
|
+
await this.http.request("DELETE", `/v1/machines/${enc3(this.sandboxId)}/ports/${checkPort(port)}`);
|
|
2554
|
+
return true;
|
|
2555
|
+
} catch (err) {
|
|
2556
|
+
if (err instanceof InstaboxError && err.status === 404 && err.code === "not_found") return false;
|
|
2557
|
+
throw err;
|
|
2558
|
+
}
|
|
2559
|
+
}
|
|
2560
|
+
};
|
|
2561
|
+
|
|
2467
2562
|
// src/sandbox.ts
|
|
2468
2563
|
var DEFAULT_TEMPLATE = "agent-browser";
|
|
2469
2564
|
var FALLBACK_TEMPLATE = "universal";
|
|
@@ -2509,7 +2604,7 @@ function toSeconds(ms, field) {
|
|
|
2509
2604
|
return s;
|
|
2510
2605
|
}
|
|
2511
2606
|
var METADATA_KEY = /^[A-Za-z0-9_.-]{1,64}$/;
|
|
2512
|
-
var
|
|
2607
|
+
var enc4 = encodeURIComponent;
|
|
2513
2608
|
var Sandbox = class _Sandbox {
|
|
2514
2609
|
constructor(http, machine, opts = {}) {
|
|
2515
2610
|
this.http = http;
|
|
@@ -2519,6 +2614,7 @@ var Sandbox = class _Sandbox {
|
|
|
2519
2614
|
this.files = new Files(this.commands, http, machine.id);
|
|
2520
2615
|
this.commands.stageStdin = (bytes, signal) => this.files.stageStdin(bytes, signal);
|
|
2521
2616
|
this.desktop = new Desktop(http, machine.id, this.commands, this.files);
|
|
2617
|
+
this.ports = new Ports(http, machine.id);
|
|
2522
2618
|
}
|
|
2523
2619
|
http;
|
|
2524
2620
|
/** The machine ID (`m_...`). */
|
|
@@ -2529,6 +2625,8 @@ var Sandbox = class _Sandbox {
|
|
|
2529
2625
|
files;
|
|
2530
2626
|
/** See and control the sandbox's graphical desktop (screenshots, mouse, keyboard, a link to watch it). */
|
|
2531
2627
|
desktop;
|
|
2628
|
+
/** Publish a port at an HTTPS URL, list published ports, unpublish them. */
|
|
2629
|
+
ports;
|
|
2532
2630
|
machine;
|
|
2533
2631
|
static async create(a, b) {
|
|
2534
2632
|
const opts = (typeof a === "string" ? b : a) ?? {};
|
|
@@ -2608,12 +2706,12 @@ var Sandbox = class _Sandbox {
|
|
|
2608
2706
|
}
|
|
2609
2707
|
/** Fetches a snapshot by ID. Requires images:read. */
|
|
2610
2708
|
static async getSnapshot(snapshotId, opts = {}) {
|
|
2611
|
-
return new HttpClient(opts).request("GET", `/v1/snapshots/${
|
|
2709
|
+
return new HttpClient(opts).request("GET", `/v1/snapshots/${enc4(snapshotId)}`);
|
|
2612
2710
|
}
|
|
2613
2711
|
/** Deletes a snapshot. Returns false if absent. Requires images:delete. */
|
|
2614
2712
|
static async deleteSnapshot(snapshotId, opts = {}) {
|
|
2615
2713
|
try {
|
|
2616
|
-
await new HttpClient(opts).request("DELETE", `/v1/snapshots/${
|
|
2714
|
+
await new HttpClient(opts).request("DELETE", `/v1/snapshots/${enc4(snapshotId)}`);
|
|
2617
2715
|
return true;
|
|
2618
2716
|
} catch (err) {
|
|
2619
2717
|
if (err instanceof InstaboxError && err.status === 404 && err.code === "not_found") return false;
|
|
@@ -2628,7 +2726,7 @@ var Sandbox = class _Sandbox {
|
|
|
2628
2726
|
async createSnapshot(opts) {
|
|
2629
2727
|
await this.settle();
|
|
2630
2728
|
await this.untilSynced();
|
|
2631
|
-
return this.http.request("POST", `/v1/machines/${
|
|
2729
|
+
return this.http.request("POST", `/v1/machines/${enc4(this.sandboxId)}/snapshots`, { body: opts });
|
|
2632
2730
|
}
|
|
2633
2731
|
/** This sandbox's recent CPU and memory samples; see `Sandbox.getMetrics`. */
|
|
2634
2732
|
async getMetrics(opts = {}) {
|
|
@@ -2648,7 +2746,7 @@ var Sandbox = class _Sandbox {
|
|
|
2648
2746
|
* pause off. The setting stays across pause and resume.
|
|
2649
2747
|
*/
|
|
2650
2748
|
async setAutoSuspend(idleMs) {
|
|
2651
|
-
this.machine = await this.http.request("PATCH", `/v1/machines/${
|
|
2749
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc4(this.sandboxId)}`, {
|
|
2652
2750
|
body: { autoSuspendAfterSeconds: toIdleSeconds(idleMs) }
|
|
2653
2751
|
});
|
|
2654
2752
|
}
|
|
@@ -2662,7 +2760,7 @@ var Sandbox = class _Sandbox {
|
|
|
2662
2760
|
}
|
|
2663
2761
|
/** Replaces the sandbox's metadata (`{}` clears it). */
|
|
2664
2762
|
async setMetadata(metadata) {
|
|
2665
|
-
this.machine = await this.http.request("PATCH", `/v1/machines/${
|
|
2763
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc4(this.sandboxId)}`, {
|
|
2666
2764
|
body: { metadata }
|
|
2667
2765
|
});
|
|
2668
2766
|
}
|
|
@@ -2672,21 +2770,21 @@ var Sandbox = class _Sandbox {
|
|
|
2672
2770
|
* Throws `not_found` on a server without the Vault.
|
|
2673
2771
|
*/
|
|
2674
2772
|
async vault() {
|
|
2675
|
-
return this.http.request("GET", `/v1/machines/${
|
|
2773
|
+
return this.http.request("GET", `/v1/machines/${enc4(this.sandboxId)}/vault`);
|
|
2676
2774
|
}
|
|
2677
2775
|
/**
|
|
2678
2776
|
* Writes the account's current Vault items into the running sandbox now, the same write as after a boot,
|
|
2679
2777
|
* and returns its record (`error` set when the guest refused it). Requires machines:write.
|
|
2680
2778
|
*/
|
|
2681
2779
|
async pushVault() {
|
|
2682
|
-
return this.http.request("POST", `/v1/machines/${
|
|
2780
|
+
return this.http.request("POST", `/v1/machines/${enc4(this.sandboxId)}/vault`);
|
|
2683
2781
|
}
|
|
2684
2782
|
/**
|
|
2685
2783
|
* Turns the Vault off for this sandbox, for good, in any state: its next boot, resume or push removes
|
|
2686
2784
|
* what the Vault wrote and nothing is written again. Requires machines:write.
|
|
2687
2785
|
*/
|
|
2688
2786
|
async disableVault() {
|
|
2689
|
-
this.machine = await this.http.request("PATCH", `/v1/machines/${
|
|
2787
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc4(this.sandboxId)}`, {
|
|
2690
2788
|
body: { vault: false }
|
|
2691
2789
|
});
|
|
2692
2790
|
}
|
|
@@ -2733,7 +2831,7 @@ var Sandbox = class _Sandbox {
|
|
|
2733
2831
|
}
|
|
2734
2832
|
body.ttlSeconds = opts.ttlSeconds;
|
|
2735
2833
|
}
|
|
2736
|
-
const c = await this.http.request("POST", `/v1/machines/${
|
|
2834
|
+
const c = await this.http.request("POST", `/v1/machines/${enc4(this.sandboxId)}/connections`, { body });
|
|
2737
2835
|
return {
|
|
2738
2836
|
host: c.host,
|
|
2739
2837
|
port: c.port,
|
|
@@ -2752,7 +2850,7 @@ var Sandbox = class _Sandbox {
|
|
|
2752
2850
|
async settle(refresh = true) {
|
|
2753
2851
|
if (refresh) this.machine = await getMachine(this.http, this.sandboxId);
|
|
2754
2852
|
if (this.machine.operationId) {
|
|
2755
|
-
const op = await this.http.request("GET", `/v1/operations/${
|
|
2853
|
+
const op = await this.http.request("GET", `/v1/operations/${enc4(this.machine.operationId)}`);
|
|
2756
2854
|
await this.http.waitForOperation(op).catch(() => {
|
|
2757
2855
|
});
|
|
2758
2856
|
this.machine = await getMachine(this.http, this.sandboxId);
|
|
@@ -2780,29 +2878,29 @@ var Sandbox = class _Sandbox {
|
|
|
2780
2878
|
}
|
|
2781
2879
|
}
|
|
2782
2880
|
async mutate(method, action) {
|
|
2783
|
-
const op = await this.http.request(method, `/v1/machines/${
|
|
2881
|
+
const op = await this.http.request(method, `/v1/machines/${enc4(this.sandboxId)}/${action}`);
|
|
2784
2882
|
await this.http.waitForOperation(op);
|
|
2785
2883
|
this.machine = await getMachine(this.http, this.sandboxId);
|
|
2786
2884
|
}
|
|
2787
2885
|
};
|
|
2788
2886
|
async function getMachine(http, id) {
|
|
2789
|
-
return http.request("GET", `/v1/machines/${
|
|
2887
|
+
return http.request("GET", `/v1/machines/${enc4(id)}`);
|
|
2790
2888
|
}
|
|
2791
2889
|
async function getMetrics(http, id, opts) {
|
|
2792
|
-
const out = await http.request("GET", `/v1/machines/${
|
|
2890
|
+
const out = await http.request("GET", `/v1/machines/${enc4(id)}/metrics`, {
|
|
2793
2891
|
query: { since: opts.since?.toISOString() }
|
|
2794
2892
|
});
|
|
2795
2893
|
return out.metrics.map((s) => ({ ...s, timestamp: new Date(s.timestamp) }));
|
|
2796
2894
|
}
|
|
2797
2895
|
async function getUsage(http, id, opts) {
|
|
2798
|
-
const u = await http.request("GET", `/v1/machines/${
|
|
2896
|
+
const u = await http.request("GET", `/v1/machines/${enc4(id)}/usage`, {
|
|
2799
2897
|
query: { from: opts.from?.toISOString(), to: opts.to?.toISOString() }
|
|
2800
2898
|
});
|
|
2801
2899
|
return { ...u, from: new Date(u.from), to: new Date(u.to) };
|
|
2802
2900
|
}
|
|
2803
2901
|
async function killMachine(http, id) {
|
|
2804
2902
|
try {
|
|
2805
|
-
const op = await http.request("DELETE", `/v1/machines/${
|
|
2903
|
+
const op = await http.request("DELETE", `/v1/machines/${enc4(id)}`, {
|
|
2806
2904
|
headers: { "X-Confirm-Delete": id }
|
|
2807
2905
|
});
|
|
2808
2906
|
await http.waitForOperation(op);
|