@insforge/instabox-sdk 0.1.1 → 0.2.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/README.md +62 -25
- package/dist/index.cjs +152 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +135 -17
- package/dist/index.d.ts +135 -17
- package/dist/index.js +152 -23
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/skills/instabox/SKILL.md +27 -14
package/dist/index.js
CHANGED
|
@@ -101,6 +101,7 @@ function readEnv(name) {
|
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
// src/commands.ts
|
|
104
|
+
var MAX_STDIN_BYTES = 1024 * 1024;
|
|
104
105
|
var Commands = class {
|
|
105
106
|
/** @internal */
|
|
106
107
|
constructor(http, sandboxId, defaultEnvs) {
|
|
@@ -113,7 +114,7 @@ var Commands = class {
|
|
|
113
114
|
defaultEnvs;
|
|
114
115
|
async run(cmd, opts = {}) {
|
|
115
116
|
if (opts.background) {
|
|
116
|
-
const wrapped = `nohup sh -c ${shellQuote(cmd)} >/dev/null 2>&1 & echo $!`;
|
|
117
|
+
const wrapped = `command -v setsid >/dev/null || { echo 'setsid not found' >&2; exit 127; }; setsid nohup sh -c ${shellQuote(cmd)} >/dev/null 2>&1 & echo $!`;
|
|
117
118
|
if (opts.stdin !== void 0)
|
|
118
119
|
throw new InstaboxError("stdin is not supported for background commands", { code: "bad_request" });
|
|
119
120
|
const r2 = await this.exec(wrapped, { cwd: opts.cwd, envs: opts.envs, timeoutMs: opts.timeoutMs });
|
|
@@ -123,7 +124,7 @@ var Commands = class {
|
|
|
123
124
|
throw new InstaboxError("exec returned an invalid background PID", { code: "invalid_response" });
|
|
124
125
|
return {
|
|
125
126
|
pid,
|
|
126
|
-
kill: async () => (await this.exec(`kill
|
|
127
|
+
kill: async () => (await this.exec(`kill -s TERM -- -${pid}`)).exitCode === 0
|
|
127
128
|
};
|
|
128
129
|
}
|
|
129
130
|
const r = await this.exec(cmd, opts);
|
|
@@ -138,7 +139,12 @@ var Commands = class {
|
|
|
138
139
|
const body = { cmd };
|
|
139
140
|
if (opts.cwd !== void 0) body.cwd = opts.cwd;
|
|
140
141
|
if (Object.keys(envs).length) body.envs = envs;
|
|
141
|
-
if (opts.stdin !== void 0)
|
|
142
|
+
if (opts.stdin !== void 0) {
|
|
143
|
+
if (new TextEncoder().encode(opts.stdin).length > MAX_STDIN_BYTES) {
|
|
144
|
+
throw new InstaboxError(`stdin is longer than ${MAX_STDIN_BYTES} bytes`, { code: "bad_request" });
|
|
145
|
+
}
|
|
146
|
+
body.stdin = opts.stdin;
|
|
147
|
+
}
|
|
142
148
|
if (opts.timeoutMs !== void 0) {
|
|
143
149
|
if (!Number.isFinite(opts.timeoutMs) || opts.timeoutMs <= 0 || opts.timeoutMs > 6e5) {
|
|
144
150
|
throw new InstaboxError("command timeoutMs must be greater than 0 and at most 600 000", { code: "bad_request" });
|
|
@@ -178,7 +184,7 @@ var Files = class {
|
|
|
178
184
|
}
|
|
179
185
|
commands;
|
|
180
186
|
async read(path, opts = {}) {
|
|
181
|
-
const p =
|
|
187
|
+
const p = arg(path);
|
|
182
188
|
const r = await this.commands.exec(
|
|
183
189
|
`[ -f ${p} ] || { echo "no such file: "${p} >&2; exit 3; }; s=$(wc -c < ${p}) || exit 1; [ "$s" -le ${MAX_FILE_BYTES} ] || { echo "file too large: $s bytes" >&2; exit 4; }; printf '%s' "$s"`
|
|
184
190
|
);
|
|
@@ -207,7 +213,10 @@ var Files = class {
|
|
|
207
213
|
}
|
|
208
214
|
return opts.format === "bytes" ? bytes : new TextDecoder().decode(bytes);
|
|
209
215
|
}
|
|
210
|
-
/**
|
|
216
|
+
/**
|
|
217
|
+
* Writes a file, creating parent directories. Replaces the file atomically (temp file + rename).
|
|
218
|
+
* Throws `is_a_directory` when `path` is a directory.
|
|
219
|
+
*/
|
|
211
220
|
async write(path, data) {
|
|
212
221
|
const bytes = typeof data === "string" ? new TextEncoder().encode(data) : data;
|
|
213
222
|
if (bytes.length > MAX_FILE_BYTES) {
|
|
@@ -215,23 +224,24 @@ var Files = class {
|
|
|
215
224
|
code: "file_too_large"
|
|
216
225
|
});
|
|
217
226
|
}
|
|
218
|
-
const p =
|
|
219
|
-
const tmp =
|
|
227
|
+
const p = arg(path);
|
|
228
|
+
const tmp = arg(`${path}.instabox-${Math.random().toString(36).slice(2, 10)}.tmp`);
|
|
220
229
|
const b64 = bytesToBase64(bytes);
|
|
221
230
|
const chunks = [];
|
|
222
231
|
for (let i = 0; i < b64.length; i += WRITE_CHUNK_B64) chunks.push(b64.slice(i, i + WRITE_CHUNK_B64));
|
|
223
232
|
if (chunks.length === 0) chunks.push("");
|
|
233
|
+
const notDir = `if [ -d ${p} ]; then echo "is a directory: "${p} >&2; exit 21; fi; `;
|
|
224
234
|
try {
|
|
225
235
|
for (let i = 0; i < chunks.length; i++) {
|
|
226
236
|
const redirect = i === 0 ? ">" : ">>";
|
|
227
|
-
const prefix = i === 0 ?
|
|
237
|
+
const prefix = i === 0 ? `${notDir}mkdir -p -- "$(dirname -- ${p})" && ` : "";
|
|
228
238
|
const r2 = await this.commands.exec(`${prefix}printf '%s' '${chunks[i]}' | base64 -d ${redirect} ${tmp}`);
|
|
229
|
-
if (r2.exitCode !== 0) throw
|
|
239
|
+
if (r2.exitCode !== 0) throw writeError(r2, path);
|
|
230
240
|
}
|
|
231
|
-
const r = await this.commands.exec(
|
|
232
|
-
if (r.exitCode !== 0) throw
|
|
241
|
+
const r = await this.commands.exec(`${notDir}mv -f -- ${tmp} ${p}`);
|
|
242
|
+
if (r.exitCode !== 0) throw writeError(r, path);
|
|
233
243
|
} catch (err) {
|
|
234
|
-
await this.commands.exec(`rm -f ${tmp}`).catch(() => {
|
|
244
|
+
await this.commands.exec(`rm -f -- ${tmp}`).catch(() => {
|
|
235
245
|
});
|
|
236
246
|
throw err;
|
|
237
247
|
}
|
|
@@ -239,7 +249,7 @@ var Files = class {
|
|
|
239
249
|
}
|
|
240
250
|
/** Lists the entries of a directory (not recursive). */
|
|
241
251
|
async list(path) {
|
|
242
|
-
const p =
|
|
252
|
+
const p = arg(path);
|
|
243
253
|
const r = await this.commands.exec(
|
|
244
254
|
`[ -d ${p} ] || { echo "no such directory: "${p} >&2; exit 3; }; cd ${p} && for f in .* *; do case "$f" in .|..) continue ;; esac; [ -e "$f" ] || [ -L "$f" ] || continue; if [ -d "$f" ]; then printf 'd\\t%s\\0' "$f"; else printf 'f\\t%s\\0' "$f"; fi; done`
|
|
245
255
|
);
|
|
@@ -254,22 +264,29 @@ var Files = class {
|
|
|
254
264
|
}
|
|
255
265
|
/** Removes a file or a directory (recursively). Missing paths are fine. */
|
|
256
266
|
async remove(path) {
|
|
257
|
-
const r = await this.commands.exec(`rm -rf -- ${
|
|
267
|
+
const r = await this.commands.exec(`rm -rf -- ${arg(path)}`);
|
|
258
268
|
if (r.exitCode !== 0) throw new CommandExitError(r);
|
|
259
269
|
}
|
|
260
270
|
/** True if a file or directory exists at `path`. */
|
|
261
271
|
async exists(path) {
|
|
262
|
-
const r = await this.commands.exec(`test -e ${
|
|
272
|
+
const r = await this.commands.exec(`test -e ${arg(path)}`);
|
|
263
273
|
return r.exitCode === 0;
|
|
264
274
|
}
|
|
265
275
|
};
|
|
276
|
+
function arg(path) {
|
|
277
|
+
return shellQuote(path.startsWith("-") ? `./${path}` : path);
|
|
278
|
+
}
|
|
279
|
+
function writeError(result, path) {
|
|
280
|
+
if (result.exitCode === 21) return new InstaboxError(`is a directory: ${path}`, { code: "is_a_directory" });
|
|
281
|
+
return new CommandExitError(result);
|
|
282
|
+
}
|
|
266
283
|
function ensureComplete(result) {
|
|
267
284
|
if (result.exitCode !== 0) throw new CommandExitError(result);
|
|
268
285
|
if (result.truncated) throw new InstaboxError("file command output was truncated", { code: "output_truncated" });
|
|
269
286
|
}
|
|
270
287
|
|
|
271
288
|
// src/http.ts
|
|
272
|
-
var DEFAULT_API_URL = "https://api.
|
|
289
|
+
var DEFAULT_API_URL = "https://api.instabox.sh";
|
|
273
290
|
var MAX_RETRIES = 3;
|
|
274
291
|
var HttpClient = class {
|
|
275
292
|
apiUrl;
|
|
@@ -282,7 +299,7 @@ var HttpClient = class {
|
|
|
282
299
|
constructor(opts = {}) {
|
|
283
300
|
const apiKey = opts.apiKey ?? readEnv("INSTABOX_API_KEY");
|
|
284
301
|
if (!apiKey) {
|
|
285
|
-
throw new
|
|
302
|
+
throw new InstaboxAuthError(
|
|
286
303
|
"No instabox API key. Set the INSTABOX_API_KEY environment variable or pass { apiKey } (create one on the API Keys page of the instabox dashboard).",
|
|
287
304
|
{ code: "missing_api_key" }
|
|
288
305
|
);
|
|
@@ -408,13 +425,15 @@ var HttpClient = class {
|
|
|
408
425
|
};
|
|
409
426
|
|
|
410
427
|
// src/sandbox.ts
|
|
411
|
-
var DEFAULT_TEMPLATE = "
|
|
428
|
+
var DEFAULT_TEMPLATE = "agent-browser";
|
|
429
|
+
var FALLBACK_TEMPLATE = "universal";
|
|
412
430
|
function toInfo(m) {
|
|
413
431
|
const info = {
|
|
414
432
|
sandboxId: m.id,
|
|
415
433
|
templateId: m.template?.id,
|
|
416
434
|
name: m.name,
|
|
417
435
|
metadata: { ...m.metadata },
|
|
436
|
+
vault: m.vault ?? true,
|
|
418
437
|
startedAt: new Date(m.createdAt),
|
|
419
438
|
onTimeout: m.onTimeout ?? "pause",
|
|
420
439
|
state: m.state,
|
|
@@ -423,8 +442,19 @@ function toInfo(m) {
|
|
|
423
442
|
if (m.snapshot?.id) info.snapshotId = m.snapshot.id;
|
|
424
443
|
if (m.timeoutAt) info.endAt = new Date(m.timeoutAt);
|
|
425
444
|
if (m.archiveAt) info.archiveAt = new Date(m.archiveAt);
|
|
445
|
+
if (m.autoSuspendAfterSeconds) info.autoSuspendMs = m.autoSuspendAfterSeconds * 1e3;
|
|
426
446
|
return info;
|
|
427
447
|
}
|
|
448
|
+
function toIdleSeconds(ms) {
|
|
449
|
+
if (ms === 0) return 0;
|
|
450
|
+
const s = Math.ceil(ms / 1e3);
|
|
451
|
+
if (!Number.isFinite(ms) || s < 120 || s > 86400) {
|
|
452
|
+
throw new InstaboxError(`autoSuspendMs must be 0 or between 120 000 (2 min) and 86 400 000 (24 h); got ${ms}`, {
|
|
453
|
+
code: "bad_request"
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
return s;
|
|
457
|
+
}
|
|
428
458
|
function toSeconds(ms, field = "timeoutMs") {
|
|
429
459
|
const s = Math.ceil(ms / 1e3);
|
|
430
460
|
if (!Number.isFinite(ms) || s < 60 || s > 86400) {
|
|
@@ -453,12 +483,13 @@ var Sandbox = class _Sandbox {
|
|
|
453
483
|
files;
|
|
454
484
|
machine;
|
|
455
485
|
static async create(a, b) {
|
|
456
|
-
const template = typeof a === "string" ? a : DEFAULT_TEMPLATE;
|
|
457
486
|
const opts = (typeof a === "string" ? b : a) ?? {};
|
|
458
487
|
const http = new HttpClient(opts);
|
|
459
488
|
const body = { name: opts.name ?? `sbx-${randomSuffix()}` };
|
|
460
489
|
if (opts.timeoutMs !== void 0) body.timeoutSeconds = toSeconds(opts.timeoutMs);
|
|
461
490
|
if (opts.archiveMs !== void 0) body.archiveSeconds = toSeconds(opts.archiveMs, "archiveMs");
|
|
491
|
+
if (opts.autoSuspendMs !== void 0) body.autoSuspendAfterSeconds = toIdleSeconds(opts.autoSuspendMs);
|
|
492
|
+
const template = typeof a === "string" ? a : await defaultTemplate(http);
|
|
462
493
|
Object.assign(body, await resolveSource(http, template));
|
|
463
494
|
if (opts.vcpus !== void 0) body.vcpus = opts.vcpus;
|
|
464
495
|
if (opts.memMiB !== void 0) body.memMiB = opts.memMiB;
|
|
@@ -466,7 +497,8 @@ var Sandbox = class _Sandbox {
|
|
|
466
497
|
if (opts.rootDiskGiB !== void 0) body.rootDiskGiB = opts.rootDiskGiB;
|
|
467
498
|
if (opts.onTimeout) body.onTimeout = opts.onTimeout;
|
|
468
499
|
if (opts.metadata) body.metadata = opts.metadata;
|
|
469
|
-
|
|
500
|
+
if (opts.vault === false) body.vault = false;
|
|
501
|
+
const op = await createMachine(http, body);
|
|
470
502
|
const done = await http.waitForOperation(op);
|
|
471
503
|
const machine = await getMachine(http, done.machineId);
|
|
472
504
|
return new _Sandbox(http, machine, opts);
|
|
@@ -521,8 +553,14 @@ var Sandbox = class _Sandbox {
|
|
|
521
553
|
throw err;
|
|
522
554
|
}
|
|
523
555
|
}
|
|
524
|
-
/**
|
|
556
|
+
/**
|
|
557
|
+
* Saves the sandbox's disks as a private snapshot. Requires images:write. The sandbox may be running;
|
|
558
|
+
* the snapshot is then returned with `status: 'pending'` and becomes `ready` once saved. For a stopped
|
|
559
|
+
* sandbox it waits first for a recent stop's upload to object storage (see `untilSynced`).
|
|
560
|
+
*/
|
|
525
561
|
async createSnapshot(opts) {
|
|
562
|
+
await this.settle();
|
|
563
|
+
await this.untilSynced();
|
|
526
564
|
return this.http.request("POST", `/v1/machines/${enc(this.sandboxId)}/snapshots`, { body: opts });
|
|
527
565
|
}
|
|
528
566
|
/** Fetches the current state of the sandbox. */
|
|
@@ -539,12 +577,45 @@ var Sandbox = class _Sandbox {
|
|
|
539
577
|
if (opts.onTimeout) body.onTimeout = opts.onTimeout;
|
|
540
578
|
this.machine = await this.http.request("PATCH", `/v1/machines/${enc(this.sandboxId)}`, { body });
|
|
541
579
|
}
|
|
580
|
+
/**
|
|
581
|
+
* Pauses the sandbox once it has been idle `idleMs` (whole seconds, 2 min to 24 h); 0 turns the idle
|
|
582
|
+
* pause off. Unlike a timeout the setting stays across pause and resume.
|
|
583
|
+
*/
|
|
584
|
+
async setAutoSuspend(idleMs) {
|
|
585
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc(this.sandboxId)}`, {
|
|
586
|
+
body: { autoSuspendAfterSeconds: toIdleSeconds(idleMs) }
|
|
587
|
+
});
|
|
588
|
+
}
|
|
542
589
|
/** Replaces the sandbox's metadata (`{}` clears it). */
|
|
543
590
|
async setMetadata(metadata) {
|
|
544
591
|
this.machine = await this.http.request("PATCH", `/v1/machines/${enc(this.sandboxId)}`, {
|
|
545
592
|
body: { metadata }
|
|
546
593
|
});
|
|
547
594
|
}
|
|
595
|
+
/**
|
|
596
|
+
* What the Vault writes into this sandbox: whether it takes the Vault, the items that apply with its
|
|
597
|
+
* current metadata (names and paths, never values) and the last write. Requires machines:read.
|
|
598
|
+
* Throws `not_found` on a server without the Vault.
|
|
599
|
+
*/
|
|
600
|
+
async vault() {
|
|
601
|
+
return this.http.request("GET", `/v1/machines/${enc(this.sandboxId)}/vault`);
|
|
602
|
+
}
|
|
603
|
+
/**
|
|
604
|
+
* Writes the account's current Vault items into the running sandbox now, the same write as after a boot,
|
|
605
|
+
* and returns its record (`error` set when the guest refused it). Requires machines:write.
|
|
606
|
+
*/
|
|
607
|
+
async pushVault() {
|
|
608
|
+
return this.http.request("POST", `/v1/machines/${enc(this.sandboxId)}/vault`);
|
|
609
|
+
}
|
|
610
|
+
/**
|
|
611
|
+
* Turns the Vault off for this sandbox, for good, in any state: its next boot, resume or push removes
|
|
612
|
+
* what the Vault wrote and nothing is written again. Requires machines:write.
|
|
613
|
+
*/
|
|
614
|
+
async disableVault() {
|
|
615
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc(this.sandboxId)}`, {
|
|
616
|
+
body: { vault: false }
|
|
617
|
+
});
|
|
618
|
+
}
|
|
548
619
|
/** True if the sandbox is running (fetches fresh state). */
|
|
549
620
|
async isRunning() {
|
|
550
621
|
return (await this.getInfo()).state === "running";
|
|
@@ -572,6 +643,7 @@ var Sandbox = class _Sandbox {
|
|
|
572
643
|
/** Copies this sandbox (root disk and `/data`) into a new sandbox and waits until it runs. */
|
|
573
644
|
async fork(opts = {}) {
|
|
574
645
|
if (opts.timeoutMs !== void 0) toSeconds(opts.timeoutMs);
|
|
646
|
+
if (opts.autoSuspendMs !== void 0) toIdleSeconds(opts.autoSuspendMs);
|
|
575
647
|
await this.settle();
|
|
576
648
|
if (this.machine.state !== "stopped") {
|
|
577
649
|
if (opts.stopSource === false) {
|
|
@@ -581,6 +653,7 @@ var Sandbox = class _Sandbox {
|
|
|
581
653
|
}
|
|
582
654
|
await this.stop();
|
|
583
655
|
}
|
|
656
|
+
await this.untilSynced();
|
|
584
657
|
const body = { name: opts.name ?? `${this.machine.name}-fork-${randomSuffix(4)}` };
|
|
585
658
|
if (opts.vcpus !== void 0) body.vcpus = opts.vcpus;
|
|
586
659
|
if (opts.memMiB !== void 0) body.memMiB = opts.memMiB;
|
|
@@ -588,7 +661,9 @@ var Sandbox = class _Sandbox {
|
|
|
588
661
|
if (opts.rootDiskGiB !== void 0) body.rootDiskGiB = opts.rootDiskGiB;
|
|
589
662
|
if (opts.timeoutMs !== void 0) body.timeoutSeconds = toSeconds(opts.timeoutMs);
|
|
590
663
|
if (opts.onTimeout) body.onTimeout = opts.onTimeout;
|
|
664
|
+
if (opts.autoSuspendMs !== void 0) body.autoSuspendAfterSeconds = toIdleSeconds(opts.autoSuspendMs);
|
|
591
665
|
if (opts.metadata) body.metadata = opts.metadata;
|
|
666
|
+
if (opts.vault === false) body.vault = false;
|
|
592
667
|
const op = await this.http.request("POST", `/v1/machines/${enc(this.sandboxId)}/fork`, { body });
|
|
593
668
|
const done = await this.http.waitForOperation(op);
|
|
594
669
|
return new _Sandbox(this.http, await getMachine(this.http, done.machineId));
|
|
@@ -600,11 +675,18 @@ var Sandbox = class _Sandbox {
|
|
|
600
675
|
/**
|
|
601
676
|
* Issues a short-lived SSH certificate for `publicKey` (authorized_keys format).
|
|
602
677
|
* Connect with `ssh -p <port> -o CertificateFile=<cert> -i <key> <username>@<host>` after adding
|
|
603
|
-
* `knownHostsLine` to known_hosts. The sandbox must be running.
|
|
678
|
+
* `knownHostsLine` to known_hosts. The sandbox must be running. `ttlSeconds`: 1 to 86 400 (24 h);
|
|
679
|
+
* default: the server's (1 h).
|
|
604
680
|
*/
|
|
605
681
|
async sshCredentials(publicKey, opts = {}) {
|
|
606
682
|
const body = { publicKey: publicKey.trim() };
|
|
607
|
-
if (opts.ttlSeconds
|
|
683
|
+
if (opts.ttlSeconds !== void 0) {
|
|
684
|
+
if (!Number.isInteger(opts.ttlSeconds) || opts.ttlSeconds < 1 || opts.ttlSeconds > 86400) {
|
|
685
|
+
const msg = `ttlSeconds must be a whole number between 1 and 86 400 (24 h); got ${opts.ttlSeconds}`;
|
|
686
|
+
throw new InstaboxError(msg, { code: "bad_request" });
|
|
687
|
+
}
|
|
688
|
+
body.ttlSeconds = opts.ttlSeconds;
|
|
689
|
+
}
|
|
608
690
|
const c = await this.http.request("POST", `/v1/machines/${enc(this.sandboxId)}/connections`, { body });
|
|
609
691
|
return {
|
|
610
692
|
host: c.host,
|
|
@@ -630,6 +712,27 @@ var Sandbox = class _Sandbox {
|
|
|
630
712
|
this.machine = await getMachine(this.http, this.sandboxId);
|
|
631
713
|
}
|
|
632
714
|
}
|
|
715
|
+
/**
|
|
716
|
+
* Waits until a stopped sandbox's disks are in object storage (`storage.synced`). A stop returns
|
|
717
|
+
* once the host has the snapshot and uploads it after, so a fork or snapshot right after a stop
|
|
718
|
+
* was refused ("not uploaded to object storage"), which took long enough after a pause and resume
|
|
719
|
+
* to fail every time. Only a stopped sandbox is waited for; the server says what else is wrong.
|
|
720
|
+
*/
|
|
721
|
+
async untilSynced() {
|
|
722
|
+
const deadline = Date.now() + this.http.operationTimeoutMs;
|
|
723
|
+
let delay = this.http.pollIntervalMs;
|
|
724
|
+
while (this.machine.state === "stopped" && !this.machine.storage.synced) {
|
|
725
|
+
if (Date.now() + delay > deadline) {
|
|
726
|
+
throw new InstaboxTimeoutError(
|
|
727
|
+
`${this.sandboxId}'s disks were not uploaded to object storage within ${this.http.operationTimeoutMs} ms`,
|
|
728
|
+
{ code: "operation_timeout" }
|
|
729
|
+
);
|
|
730
|
+
}
|
|
731
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
732
|
+
delay = Math.min(delay * 2, 2e3);
|
|
733
|
+
this.machine = await getMachine(this.http, this.sandboxId);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
633
736
|
async mutate(method, action) {
|
|
634
737
|
const op = await this.http.request(method, `/v1/machines/${enc(this.sandboxId)}/${action}`);
|
|
635
738
|
await this.http.waitForOperation(op);
|
|
@@ -649,6 +752,30 @@ async function killMachine(http, id) {
|
|
|
649
752
|
throw err;
|
|
650
753
|
}
|
|
651
754
|
}
|
|
755
|
+
async function createMachine(http, body) {
|
|
756
|
+
const deadline = Date.now() + http.operationTimeoutMs;
|
|
757
|
+
let delay = http.pollIntervalMs;
|
|
758
|
+
for (; ; ) {
|
|
759
|
+
try {
|
|
760
|
+
return await http.request("POST", "/v1/machines", { body });
|
|
761
|
+
} catch (err) {
|
|
762
|
+
const saving = body.snapshotId !== void 0 && err instanceof InstaboxError && err.status === 409 && err.code === "operation_conflict" && /still being saved/i.test(err.message);
|
|
763
|
+
if (!saving) throw err;
|
|
764
|
+
if (Date.now() + delay > deadline) {
|
|
765
|
+
throw new InstaboxTimeoutError(
|
|
766
|
+
`snapshot ${String(body.snapshotId)} was still being saved after ${http.operationTimeoutMs} ms`,
|
|
767
|
+
{ code: "operation_timeout", cause: err }
|
|
768
|
+
);
|
|
769
|
+
}
|
|
770
|
+
await sleep(delay);
|
|
771
|
+
delay = Math.min(delay * 2, 2e3);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
async function defaultTemplate(http) {
|
|
776
|
+
const { templates } = await http.request("GET", "/v1/templates");
|
|
777
|
+
return templates.some((t) => t.id === DEFAULT_TEMPLATE && !t.hidden) ? DEFAULT_TEMPLATE : FALLBACK_TEMPLATE;
|
|
778
|
+
}
|
|
652
779
|
async function resolveSource(http, ref) {
|
|
653
780
|
if (/^sn_[a-z2-7]{26}$/.test(ref)) return { snapshotId: ref };
|
|
654
781
|
const { templates } = await http.request("GET", "/v1/templates");
|
|
@@ -666,12 +793,14 @@ export {
|
|
|
666
793
|
Commands,
|
|
667
794
|
DEFAULT_API_URL,
|
|
668
795
|
DEFAULT_TEMPLATE,
|
|
796
|
+
FALLBACK_TEMPLATE,
|
|
669
797
|
Files,
|
|
670
798
|
InstaboxAuthError,
|
|
671
799
|
InstaboxError,
|
|
672
800
|
InstaboxQuotaError,
|
|
673
801
|
InstaboxTimeoutError,
|
|
674
802
|
MAX_FILE_BYTES,
|
|
803
|
+
MAX_STDIN_BYTES,
|
|
675
804
|
Sandbox
|
|
676
805
|
};
|
|
677
806
|
//# sourceMappingURL=index.js.map
|