@insforge/instabox-sdk 0.7.1 → 0.9.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 +34 -9
- package/dist/index.cjs +110 -16
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +63 -2
- package/dist/index.d.ts +63 -2
- package/dist/index.js +110 -16
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/skills/instabox/SKILL.md +12 -8
package/dist/index.d.cts
CHANGED
|
@@ -174,7 +174,7 @@ interface MachineList {
|
|
|
174
174
|
interface Operation {
|
|
175
175
|
id: string;
|
|
176
176
|
machineId: string;
|
|
177
|
-
type: 'create' | 'start' | 'stop' | 'suspend' | 'resume' | 'delete';
|
|
177
|
+
type: 'create' | 'start' | 'stop' | 'suspend' | 'resume' | 'resize' | 'delete';
|
|
178
178
|
state: 'queued' | 'running' | 'succeeded' | 'failed';
|
|
179
179
|
phase?: string;
|
|
180
180
|
error: ApiError | null;
|
|
@@ -851,6 +851,52 @@ declare class Desktop {
|
|
|
851
851
|
private run;
|
|
852
852
|
}
|
|
853
853
|
|
|
854
|
+
/** `public`: anyone with the URL reaches it. `private`: only requests that carry the port's token. */
|
|
855
|
+
type PortAccess = 'public' | 'private';
|
|
856
|
+
/** A sandbox port published at a URL. */
|
|
857
|
+
interface PublicPort {
|
|
858
|
+
port: number;
|
|
859
|
+
/** Shown on the dashboard: `Port <n>` unless one was given. */
|
|
860
|
+
name: string;
|
|
861
|
+
/** `http`, or `tcp` for a raw TCP port published with `instabox port publish --tcp`. */
|
|
862
|
+
protocol: 'http' | 'tcp';
|
|
863
|
+
/** `https://<port>-<id>.<domain>` (`tls://<host>:<port>` for tcp), on the server's own domain. */
|
|
864
|
+
url: string;
|
|
865
|
+
access: PortAccess;
|
|
866
|
+
/** A private port's token (`ibp_...`), returned only by the publish that created or rotated it. */
|
|
867
|
+
token?: string;
|
|
868
|
+
/** `url` with the token as its `_token` query parameter: the link to share. Set with `token`. */
|
|
869
|
+
tokenUrl?: string;
|
|
870
|
+
createdAt: Date;
|
|
871
|
+
}
|
|
872
|
+
interface PublishPortOpts {
|
|
873
|
+
/** Name shown on the dashboard. Omitted: `Port <n>` for a new port, the current name otherwise. */
|
|
874
|
+
name?: string;
|
|
875
|
+
/** Omitted: public for a new port, the current access otherwise. */
|
|
876
|
+
access?: PortAccess;
|
|
877
|
+
/** Give a private port a new token: the old one stops working and its connections close. */
|
|
878
|
+
rotateToken?: boolean;
|
|
879
|
+
}
|
|
880
|
+
/** URLs for servers listening in the sandbox, as `instabox port publish` makes them. */
|
|
881
|
+
declare class Ports {
|
|
882
|
+
private readonly http;
|
|
883
|
+
private readonly sandboxId;
|
|
884
|
+
/** @internal */
|
|
885
|
+
constructor(http: HttpClient, sandboxId: string);
|
|
886
|
+
/**
|
|
887
|
+
* Publishes `port` (1024-65535) until `unpublish()`; publishing it again keeps its URL. The URL answers
|
|
888
|
+
* while the sandbox runs, WebSockets included. Requires machines:write.
|
|
889
|
+
*/
|
|
890
|
+
publish(port: number, opts?: PublishPortOpts): Promise<PublicPort>;
|
|
891
|
+
/** The sandbox's published ports. Tokens are not included. Requires machines:read. */
|
|
892
|
+
list(): Promise<PublicPort[]>;
|
|
893
|
+
/**
|
|
894
|
+
* Removes the port's URL and closes its open connections. Returns false if the port was not published.
|
|
895
|
+
* Requires machines:write.
|
|
896
|
+
*/
|
|
897
|
+
unpublish(port: number): Promise<boolean>;
|
|
898
|
+
}
|
|
899
|
+
|
|
854
900
|
declare const DEFAULT_TEMPLATE = "agent-browser";
|
|
855
901
|
/** The default when a server does not offer `DEFAULT_TEMPLATE` (an older or self-hosted one). */
|
|
856
902
|
declare const FALLBACK_TEMPLATE = "universal";
|
|
@@ -928,6 +974,11 @@ interface CreateSnapshotOpts {
|
|
|
928
974
|
/** Save only the root disk; omit the persistent /data disk. */
|
|
929
975
|
rootOnly?: boolean;
|
|
930
976
|
}
|
|
977
|
+
/** One of the plan's sizes: `instabox whoami --json` lists them as `allowedSpecs`. */
|
|
978
|
+
interface ResizeOpts {
|
|
979
|
+
vcpus: number;
|
|
980
|
+
memMiB: number;
|
|
981
|
+
}
|
|
931
982
|
/** One load sample of a sandbox, 30 s apart. Measured on the host; bytes throughout. */
|
|
932
983
|
interface SandboxMetrics {
|
|
933
984
|
timestamp: Date;
|
|
@@ -993,6 +1044,8 @@ declare class Sandbox {
|
|
|
993
1044
|
readonly files: Files;
|
|
994
1045
|
/** See and control the sandbox's graphical desktop (screenshots, mouse, keyboard, a link to watch it). */
|
|
995
1046
|
readonly desktop: Desktop;
|
|
1047
|
+
/** Publish a port at an HTTPS URL, list published ports, unpublish them. */
|
|
1048
|
+
readonly ports: Ports;
|
|
996
1049
|
private machine;
|
|
997
1050
|
private constructor();
|
|
998
1051
|
/**
|
|
@@ -1046,6 +1099,14 @@ declare class Sandbox {
|
|
|
1046
1099
|
* sends no request.
|
|
1047
1100
|
*/
|
|
1048
1101
|
setTimeout(..._args: unknown[]): Promise<never>;
|
|
1102
|
+
/**
|
|
1103
|
+
* Gives the sandbox another of its plan's sizes (paid plans). A stopped sandbox takes it at its next
|
|
1104
|
+
* start. A running or paused one is stopped, resized and started again by one server operation: files
|
|
1105
|
+
* stay, running processes and paused memory do not. If it cannot start at the new size, the server
|
|
1106
|
+
* starts it again at the old one and this throws. A size the plan does not offer throws
|
|
1107
|
+
* `InstaboxQuotaError` (`spec_not_allowed`). Requires machines:write.
|
|
1108
|
+
*/
|
|
1109
|
+
resize(opts: ResizeOpts): Promise<void>;
|
|
1049
1110
|
/** Replaces the sandbox's metadata (`{}` clears it). */
|
|
1050
1111
|
setMetadata(metadata: Record<string, string>): Promise<void>;
|
|
1051
1112
|
/**
|
|
@@ -1101,4 +1162,4 @@ declare class Sandbox {
|
|
|
1101
1162
|
private mutate;
|
|
1102
1163
|
}
|
|
1103
1164
|
|
|
1104
|
-
export { type ApiError, CommandExitError, type CommandHandle, type CommandOptions, type CommandResult, type CommandWaitOptions, Commands, type Connection, type ConnectionOptions, type CreateSnapshotOpts, DEFAULT_API_URL, DEFAULT_TEMPLATE, DESKTOP_DISPLAY, Desktop, type DesktopStatus, type DesktopStream, type DownloadInfo, type DownloadOptions, type EntryInfo, type ExecResponse, FALLBACK_TEMPLATE, type FileStream, Files, type HostTrust, InstaboxAuthError, InstaboxError, type InstaboxErrorInit, InstaboxNotUtf8Error, InstaboxQuotaError, InstaboxTimeoutError, LIMIT_CODES, MAX_FILE_BYTES, MAX_STDIN_BYTES, MAX_UPLOAD_BYTES, type Machine, type MachineList, type MachineMetric, type MachineMetrics, type MachineSpec, type MachineState, type MachineUsage, type MachineVault, type MachineVaultItem, type MouseButton, type Operation, type ReadStreamOptions, Sandbox, type SandboxConnectOpts, type SandboxInfo, type SandboxListOpts, type SandboxMetrics, type SandboxMetricsOpts, type SandboxOpts, type SandboxUsage, type SandboxUsageOpts, type ScrollDirection, type Snapshot, type SnapshotList, type SshCredentials, type Template, type TemplateRef, type UploadInfo, type UploadOptions, type Usage, type UsageCost, type UsageLimits, type UsageMachineStorage, type UsageStorage, type VaultApplied, type VaultFile, type VaultKept, type WriteData, type WriteInfo, type WriteOptions, isLimitCode };
|
|
1165
|
+
export { type ApiError, CommandExitError, type CommandHandle, type CommandOptions, type CommandResult, type CommandWaitOptions, Commands, type Connection, type ConnectionOptions, type CreateSnapshotOpts, DEFAULT_API_URL, DEFAULT_TEMPLATE, DESKTOP_DISPLAY, Desktop, type DesktopStatus, type DesktopStream, type DownloadInfo, type DownloadOptions, type EntryInfo, type ExecResponse, FALLBACK_TEMPLATE, type FileStream, Files, type HostTrust, InstaboxAuthError, InstaboxError, type InstaboxErrorInit, InstaboxNotUtf8Error, InstaboxQuotaError, InstaboxTimeoutError, LIMIT_CODES, MAX_FILE_BYTES, MAX_STDIN_BYTES, MAX_UPLOAD_BYTES, type Machine, type MachineList, type MachineMetric, type MachineMetrics, type MachineSpec, type MachineState, type MachineUsage, type MachineVault, type MachineVaultItem, type MouseButton, type Operation, type PortAccess, Ports, type PublicPort, type PublishPortOpts, type ReadStreamOptions, type ResizeOpts, Sandbox, type SandboxConnectOpts, type SandboxInfo, type SandboxListOpts, type SandboxMetrics, type SandboxMetricsOpts, type SandboxOpts, type SandboxUsage, type SandboxUsageOpts, type ScrollDirection, type Snapshot, type SnapshotList, type SshCredentials, type Template, type TemplateRef, type UploadInfo, type UploadOptions, type Usage, type UsageCost, type UsageLimits, type UsageMachineStorage, type UsageStorage, type VaultApplied, type VaultFile, type VaultKept, type WriteData, type WriteInfo, type WriteOptions, isLimitCode };
|
package/dist/index.d.ts
CHANGED
|
@@ -174,7 +174,7 @@ interface MachineList {
|
|
|
174
174
|
interface Operation {
|
|
175
175
|
id: string;
|
|
176
176
|
machineId: string;
|
|
177
|
-
type: 'create' | 'start' | 'stop' | 'suspend' | 'resume' | 'delete';
|
|
177
|
+
type: 'create' | 'start' | 'stop' | 'suspend' | 'resume' | 'resize' | 'delete';
|
|
178
178
|
state: 'queued' | 'running' | 'succeeded' | 'failed';
|
|
179
179
|
phase?: string;
|
|
180
180
|
error: ApiError | null;
|
|
@@ -851,6 +851,52 @@ declare class Desktop {
|
|
|
851
851
|
private run;
|
|
852
852
|
}
|
|
853
853
|
|
|
854
|
+
/** `public`: anyone with the URL reaches it. `private`: only requests that carry the port's token. */
|
|
855
|
+
type PortAccess = 'public' | 'private';
|
|
856
|
+
/** A sandbox port published at a URL. */
|
|
857
|
+
interface PublicPort {
|
|
858
|
+
port: number;
|
|
859
|
+
/** Shown on the dashboard: `Port <n>` unless one was given. */
|
|
860
|
+
name: string;
|
|
861
|
+
/** `http`, or `tcp` for a raw TCP port published with `instabox port publish --tcp`. */
|
|
862
|
+
protocol: 'http' | 'tcp';
|
|
863
|
+
/** `https://<port>-<id>.<domain>` (`tls://<host>:<port>` for tcp), on the server's own domain. */
|
|
864
|
+
url: string;
|
|
865
|
+
access: PortAccess;
|
|
866
|
+
/** A private port's token (`ibp_...`), returned only by the publish that created or rotated it. */
|
|
867
|
+
token?: string;
|
|
868
|
+
/** `url` with the token as its `_token` query parameter: the link to share. Set with `token`. */
|
|
869
|
+
tokenUrl?: string;
|
|
870
|
+
createdAt: Date;
|
|
871
|
+
}
|
|
872
|
+
interface PublishPortOpts {
|
|
873
|
+
/** Name shown on the dashboard. Omitted: `Port <n>` for a new port, the current name otherwise. */
|
|
874
|
+
name?: string;
|
|
875
|
+
/** Omitted: public for a new port, the current access otherwise. */
|
|
876
|
+
access?: PortAccess;
|
|
877
|
+
/** Give a private port a new token: the old one stops working and its connections close. */
|
|
878
|
+
rotateToken?: boolean;
|
|
879
|
+
}
|
|
880
|
+
/** URLs for servers listening in the sandbox, as `instabox port publish` makes them. */
|
|
881
|
+
declare class Ports {
|
|
882
|
+
private readonly http;
|
|
883
|
+
private readonly sandboxId;
|
|
884
|
+
/** @internal */
|
|
885
|
+
constructor(http: HttpClient, sandboxId: string);
|
|
886
|
+
/**
|
|
887
|
+
* Publishes `port` (1024-65535) until `unpublish()`; publishing it again keeps its URL. The URL answers
|
|
888
|
+
* while the sandbox runs, WebSockets included. Requires machines:write.
|
|
889
|
+
*/
|
|
890
|
+
publish(port: number, opts?: PublishPortOpts): Promise<PublicPort>;
|
|
891
|
+
/** The sandbox's published ports. Tokens are not included. Requires machines:read. */
|
|
892
|
+
list(): Promise<PublicPort[]>;
|
|
893
|
+
/**
|
|
894
|
+
* Removes the port's URL and closes its open connections. Returns false if the port was not published.
|
|
895
|
+
* Requires machines:write.
|
|
896
|
+
*/
|
|
897
|
+
unpublish(port: number): Promise<boolean>;
|
|
898
|
+
}
|
|
899
|
+
|
|
854
900
|
declare const DEFAULT_TEMPLATE = "agent-browser";
|
|
855
901
|
/** The default when a server does not offer `DEFAULT_TEMPLATE` (an older or self-hosted one). */
|
|
856
902
|
declare const FALLBACK_TEMPLATE = "universal";
|
|
@@ -928,6 +974,11 @@ interface CreateSnapshotOpts {
|
|
|
928
974
|
/** Save only the root disk; omit the persistent /data disk. */
|
|
929
975
|
rootOnly?: boolean;
|
|
930
976
|
}
|
|
977
|
+
/** One of the plan's sizes: `instabox whoami --json` lists them as `allowedSpecs`. */
|
|
978
|
+
interface ResizeOpts {
|
|
979
|
+
vcpus: number;
|
|
980
|
+
memMiB: number;
|
|
981
|
+
}
|
|
931
982
|
/** One load sample of a sandbox, 30 s apart. Measured on the host; bytes throughout. */
|
|
932
983
|
interface SandboxMetrics {
|
|
933
984
|
timestamp: Date;
|
|
@@ -993,6 +1044,8 @@ declare class Sandbox {
|
|
|
993
1044
|
readonly files: Files;
|
|
994
1045
|
/** See and control the sandbox's graphical desktop (screenshots, mouse, keyboard, a link to watch it). */
|
|
995
1046
|
readonly desktop: Desktop;
|
|
1047
|
+
/** Publish a port at an HTTPS URL, list published ports, unpublish them. */
|
|
1048
|
+
readonly ports: Ports;
|
|
996
1049
|
private machine;
|
|
997
1050
|
private constructor();
|
|
998
1051
|
/**
|
|
@@ -1046,6 +1099,14 @@ declare class Sandbox {
|
|
|
1046
1099
|
* sends no request.
|
|
1047
1100
|
*/
|
|
1048
1101
|
setTimeout(..._args: unknown[]): Promise<never>;
|
|
1102
|
+
/**
|
|
1103
|
+
* Gives the sandbox another of its plan's sizes (paid plans). A stopped sandbox takes it at its next
|
|
1104
|
+
* start. A running or paused one is stopped, resized and started again by one server operation: files
|
|
1105
|
+
* stay, running processes and paused memory do not. If it cannot start at the new size, the server
|
|
1106
|
+
* starts it again at the old one and this throws. A size the plan does not offer throws
|
|
1107
|
+
* `InstaboxQuotaError` (`spec_not_allowed`). Requires machines:write.
|
|
1108
|
+
*/
|
|
1109
|
+
resize(opts: ResizeOpts): Promise<void>;
|
|
1049
1110
|
/** Replaces the sandbox's metadata (`{}` clears it). */
|
|
1050
1111
|
setMetadata(metadata: Record<string, string>): Promise<void>;
|
|
1051
1112
|
/**
|
|
@@ -1101,4 +1162,4 @@ declare class Sandbox {
|
|
|
1101
1162
|
private mutate;
|
|
1102
1163
|
}
|
|
1103
1164
|
|
|
1104
|
-
export { type ApiError, CommandExitError, type CommandHandle, type CommandOptions, type CommandResult, type CommandWaitOptions, Commands, type Connection, type ConnectionOptions, type CreateSnapshotOpts, DEFAULT_API_URL, DEFAULT_TEMPLATE, DESKTOP_DISPLAY, Desktop, type DesktopStatus, type DesktopStream, type DownloadInfo, type DownloadOptions, type EntryInfo, type ExecResponse, FALLBACK_TEMPLATE, type FileStream, Files, type HostTrust, InstaboxAuthError, InstaboxError, type InstaboxErrorInit, InstaboxNotUtf8Error, InstaboxQuotaError, InstaboxTimeoutError, LIMIT_CODES, MAX_FILE_BYTES, MAX_STDIN_BYTES, MAX_UPLOAD_BYTES, type Machine, type MachineList, type MachineMetric, type MachineMetrics, type MachineSpec, type MachineState, type MachineUsage, type MachineVault, type MachineVaultItem, type MouseButton, type Operation, type ReadStreamOptions, Sandbox, type SandboxConnectOpts, type SandboxInfo, type SandboxListOpts, type SandboxMetrics, type SandboxMetricsOpts, type SandboxOpts, type SandboxUsage, type SandboxUsageOpts, type ScrollDirection, type Snapshot, type SnapshotList, type SshCredentials, type Template, type TemplateRef, type UploadInfo, type UploadOptions, type Usage, type UsageCost, type UsageLimits, type UsageMachineStorage, type UsageStorage, type VaultApplied, type VaultFile, type VaultKept, type WriteData, type WriteInfo, type WriteOptions, isLimitCode };
|
|
1165
|
+
export { type ApiError, CommandExitError, type CommandHandle, type CommandOptions, type CommandResult, type CommandWaitOptions, Commands, type Connection, type ConnectionOptions, type CreateSnapshotOpts, DEFAULT_API_URL, DEFAULT_TEMPLATE, DESKTOP_DISPLAY, Desktop, type DesktopStatus, type DesktopStream, type DownloadInfo, type DownloadOptions, type EntryInfo, type ExecResponse, FALLBACK_TEMPLATE, type FileStream, Files, type HostTrust, InstaboxAuthError, InstaboxError, type InstaboxErrorInit, InstaboxNotUtf8Error, InstaboxQuotaError, InstaboxTimeoutError, LIMIT_CODES, MAX_FILE_BYTES, MAX_STDIN_BYTES, MAX_UPLOAD_BYTES, type Machine, type MachineList, type MachineMetric, type MachineMetrics, type MachineSpec, type MachineState, type MachineUsage, type MachineVault, type MachineVaultItem, type MouseButton, type Operation, type PortAccess, Ports, type PublicPort, type PublishPortOpts, type ReadStreamOptions, type ResizeOpts, Sandbox, type SandboxConnectOpts, type SandboxInfo, type SandboxListOpts, type SandboxMetrics, type SandboxMetricsOpts, type SandboxOpts, type SandboxUsage, type SandboxUsageOpts, type ScrollDirection, type Snapshot, type SnapshotList, type SshCredentials, type Template, type TemplateRef, type UploadInfo, type UploadOptions, type Usage, type UsageCost, type UsageLimits, type UsageMachineStorage, type UsageStorage, type VaultApplied, type VaultFile, type VaultKept, type WriteData, type WriteInfo, type WriteOptions, isLimitCode };
|
package/dist/index.js
CHANGED
|
@@ -2441,6 +2441,69 @@ var HttpClient = class {
|
|
|
2441
2441
|
}
|
|
2442
2442
|
};
|
|
2443
2443
|
|
|
2444
|
+
// src/ports.ts
|
|
2445
|
+
var enc3 = encodeURIComponent;
|
|
2446
|
+
function checkPort(port) {
|
|
2447
|
+
if (!Number.isInteger(port)) {
|
|
2448
|
+
throw new InstaboxError(`port must be a whole number; got ${port}`, { code: "bad_request" });
|
|
2449
|
+
}
|
|
2450
|
+
return port;
|
|
2451
|
+
}
|
|
2452
|
+
function fromServer(p) {
|
|
2453
|
+
const out = {
|
|
2454
|
+
port: p.port,
|
|
2455
|
+
name: p.name,
|
|
2456
|
+
protocol: p.protocol,
|
|
2457
|
+
url: p.url,
|
|
2458
|
+
access: p.access,
|
|
2459
|
+
createdAt: new Date(p.createdAt)
|
|
2460
|
+
};
|
|
2461
|
+
if (p.token) {
|
|
2462
|
+
out.token = p.token;
|
|
2463
|
+
out.tokenUrl = p.tokenUrl;
|
|
2464
|
+
}
|
|
2465
|
+
return out;
|
|
2466
|
+
}
|
|
2467
|
+
var Ports = class {
|
|
2468
|
+
/** @internal */
|
|
2469
|
+
constructor(http, sandboxId) {
|
|
2470
|
+
this.http = http;
|
|
2471
|
+
this.sandboxId = sandboxId;
|
|
2472
|
+
}
|
|
2473
|
+
http;
|
|
2474
|
+
sandboxId;
|
|
2475
|
+
/**
|
|
2476
|
+
* Publishes `port` (1024-65535) until `unpublish()`; publishing it again keeps its URL. The URL answers
|
|
2477
|
+
* while the sandbox runs, WebSockets included. Requires machines:write.
|
|
2478
|
+
*/
|
|
2479
|
+
async publish(port, opts = {}) {
|
|
2480
|
+
const body = { port: checkPort(port) };
|
|
2481
|
+
if (opts.name !== void 0) body.name = opts.name;
|
|
2482
|
+
if (opts.access !== void 0) body.access = opts.access;
|
|
2483
|
+
if (opts.rotateToken) body.rotateToken = true;
|
|
2484
|
+
const r = await this.http.request("POST", `/v1/machines/${enc3(this.sandboxId)}/ports`, { body });
|
|
2485
|
+
return fromServer(r);
|
|
2486
|
+
}
|
|
2487
|
+
/** The sandbox's published ports. Tokens are not included. Requires machines:read. */
|
|
2488
|
+
async list() {
|
|
2489
|
+
const r = await this.http.request("GET", `/v1/machines/${enc3(this.sandboxId)}/ports`);
|
|
2490
|
+
return r.ports.map(fromServer);
|
|
2491
|
+
}
|
|
2492
|
+
/**
|
|
2493
|
+
* Removes the port's URL and closes its open connections. Returns false if the port was not published.
|
|
2494
|
+
* Requires machines:write.
|
|
2495
|
+
*/
|
|
2496
|
+
async unpublish(port) {
|
|
2497
|
+
try {
|
|
2498
|
+
await this.http.request("DELETE", `/v1/machines/${enc3(this.sandboxId)}/ports/${checkPort(port)}`);
|
|
2499
|
+
return true;
|
|
2500
|
+
} catch (err) {
|
|
2501
|
+
if (err instanceof InstaboxError && err.status === 404 && err.code === "not_found") return false;
|
|
2502
|
+
throw err;
|
|
2503
|
+
}
|
|
2504
|
+
}
|
|
2505
|
+
};
|
|
2506
|
+
|
|
2444
2507
|
// src/sandbox.ts
|
|
2445
2508
|
var DEFAULT_TEMPLATE = "agent-browser";
|
|
2446
2509
|
var FALLBACK_TEMPLATE = "universal";
|
|
@@ -2485,8 +2548,14 @@ function toSeconds(ms, field) {
|
|
|
2485
2548
|
}
|
|
2486
2549
|
return s;
|
|
2487
2550
|
}
|
|
2551
|
+
function wholeCount(field, n) {
|
|
2552
|
+
if (!Number.isInteger(n) || n < 1) {
|
|
2553
|
+
throw new InstaboxError(`${field} must be a whole number from 1; got ${n}`, { code: "bad_request" });
|
|
2554
|
+
}
|
|
2555
|
+
return n;
|
|
2556
|
+
}
|
|
2488
2557
|
var METADATA_KEY = /^[A-Za-z0-9_.-]{1,64}$/;
|
|
2489
|
-
var
|
|
2558
|
+
var enc4 = encodeURIComponent;
|
|
2490
2559
|
var Sandbox = class _Sandbox {
|
|
2491
2560
|
constructor(http, machine, opts = {}) {
|
|
2492
2561
|
this.http = http;
|
|
@@ -2496,6 +2565,7 @@ var Sandbox = class _Sandbox {
|
|
|
2496
2565
|
this.files = new Files(this.commands, http, machine.id);
|
|
2497
2566
|
this.commands.stageStdin = (bytes, signal) => this.files.stageStdin(bytes, signal);
|
|
2498
2567
|
this.desktop = new Desktop(http, machine.id, this.commands, this.files);
|
|
2568
|
+
this.ports = new Ports(http, machine.id);
|
|
2499
2569
|
}
|
|
2500
2570
|
http;
|
|
2501
2571
|
/** The machine ID (`m_...`). */
|
|
@@ -2506,6 +2576,8 @@ var Sandbox = class _Sandbox {
|
|
|
2506
2576
|
files;
|
|
2507
2577
|
/** See and control the sandbox's graphical desktop (screenshots, mouse, keyboard, a link to watch it). */
|
|
2508
2578
|
desktop;
|
|
2579
|
+
/** Publish a port at an HTTPS URL, list published ports, unpublish them. */
|
|
2580
|
+
ports;
|
|
2509
2581
|
machine;
|
|
2510
2582
|
static async create(a, b) {
|
|
2511
2583
|
const opts = (typeof a === "string" ? b : a) ?? {};
|
|
@@ -2585,12 +2657,12 @@ var Sandbox = class _Sandbox {
|
|
|
2585
2657
|
}
|
|
2586
2658
|
/** Fetches a snapshot by ID. Requires images:read. */
|
|
2587
2659
|
static async getSnapshot(snapshotId, opts = {}) {
|
|
2588
|
-
return new HttpClient(opts).request("GET", `/v1/snapshots/${
|
|
2660
|
+
return new HttpClient(opts).request("GET", `/v1/snapshots/${enc4(snapshotId)}`);
|
|
2589
2661
|
}
|
|
2590
2662
|
/** Deletes a snapshot. Returns false if absent. Requires images:delete. */
|
|
2591
2663
|
static async deleteSnapshot(snapshotId, opts = {}) {
|
|
2592
2664
|
try {
|
|
2593
|
-
await new HttpClient(opts).request("DELETE", `/v1/snapshots/${
|
|
2665
|
+
await new HttpClient(opts).request("DELETE", `/v1/snapshots/${enc4(snapshotId)}`);
|
|
2594
2666
|
return true;
|
|
2595
2667
|
} catch (err) {
|
|
2596
2668
|
if (err instanceof InstaboxError && err.status === 404 && err.code === "not_found") return false;
|
|
@@ -2605,7 +2677,7 @@ var Sandbox = class _Sandbox {
|
|
|
2605
2677
|
async createSnapshot(opts) {
|
|
2606
2678
|
await this.settle();
|
|
2607
2679
|
await this.untilSynced();
|
|
2608
|
-
return this.http.request("POST", `/v1/machines/${
|
|
2680
|
+
return this.http.request("POST", `/v1/machines/${enc4(this.sandboxId)}/snapshots`, { body: opts });
|
|
2609
2681
|
}
|
|
2610
2682
|
/** This sandbox's recent CPU and memory samples; see `Sandbox.getMetrics`. */
|
|
2611
2683
|
async getMetrics(opts = {}) {
|
|
@@ -2625,7 +2697,7 @@ var Sandbox = class _Sandbox {
|
|
|
2625
2697
|
* pause off. The setting stays across pause and resume.
|
|
2626
2698
|
*/
|
|
2627
2699
|
async setAutoSuspend(idleMs) {
|
|
2628
|
-
this.machine = await this.http.request("PATCH", `/v1/machines/${
|
|
2700
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc4(this.sandboxId)}`, {
|
|
2629
2701
|
body: { autoSuspendAfterSeconds: toIdleSeconds(idleMs) }
|
|
2630
2702
|
});
|
|
2631
2703
|
}
|
|
@@ -2637,9 +2709,30 @@ var Sandbox = class _Sandbox {
|
|
|
2637
2709
|
async setTimeout(..._args) {
|
|
2638
2710
|
throw timeoutRemoved("setTimeout()", "Use setAutoSuspend(ms) to pause the sandbox once it has been idle that long");
|
|
2639
2711
|
}
|
|
2712
|
+
/**
|
|
2713
|
+
* Gives the sandbox another of its plan's sizes (paid plans). A stopped sandbox takes it at its next
|
|
2714
|
+
* start. A running or paused one is stopped, resized and started again by one server operation: files
|
|
2715
|
+
* stay, running processes and paused memory do not. If it cannot start at the new size, the server
|
|
2716
|
+
* starts it again at the old one and this throws. A size the plan does not offer throws
|
|
2717
|
+
* `InstaboxQuotaError` (`spec_not_allowed`). Requires machines:write.
|
|
2718
|
+
*/
|
|
2719
|
+
async resize(opts) {
|
|
2720
|
+
const vcpus = wholeCount("vcpus", opts.vcpus);
|
|
2721
|
+
const memMiB = wholeCount("memMiB", opts.memMiB);
|
|
2722
|
+
await this.settle();
|
|
2723
|
+
if (this.machine.spec.vcpus === vcpus && this.machine.spec.memMiB === memMiB) return;
|
|
2724
|
+
const body = { vcpus, memMiB };
|
|
2725
|
+
if (this.machine.state !== "stopped") body.apply = "restart";
|
|
2726
|
+
const r = await this.http.request("PATCH", `/v1/machines/${enc4(this.sandboxId)}`, { body });
|
|
2727
|
+
try {
|
|
2728
|
+
if ("machineId" in r) await this.http.waitForOperation(r);
|
|
2729
|
+
} finally {
|
|
2730
|
+
this.machine = await getMachine(this.http, this.sandboxId).catch(() => this.machine);
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2640
2733
|
/** Replaces the sandbox's metadata (`{}` clears it). */
|
|
2641
2734
|
async setMetadata(metadata) {
|
|
2642
|
-
this.machine = await this.http.request("PATCH", `/v1/machines/${
|
|
2735
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc4(this.sandboxId)}`, {
|
|
2643
2736
|
body: { metadata }
|
|
2644
2737
|
});
|
|
2645
2738
|
}
|
|
@@ -2649,21 +2742,21 @@ var Sandbox = class _Sandbox {
|
|
|
2649
2742
|
* Throws `not_found` on a server without the Vault.
|
|
2650
2743
|
*/
|
|
2651
2744
|
async vault() {
|
|
2652
|
-
return this.http.request("GET", `/v1/machines/${
|
|
2745
|
+
return this.http.request("GET", `/v1/machines/${enc4(this.sandboxId)}/vault`);
|
|
2653
2746
|
}
|
|
2654
2747
|
/**
|
|
2655
2748
|
* Writes the account's current Vault items into the running sandbox now, the same write as after a boot,
|
|
2656
2749
|
* and returns its record (`error` set when the guest refused it). Requires machines:write.
|
|
2657
2750
|
*/
|
|
2658
2751
|
async pushVault() {
|
|
2659
|
-
return this.http.request("POST", `/v1/machines/${
|
|
2752
|
+
return this.http.request("POST", `/v1/machines/${enc4(this.sandboxId)}/vault`);
|
|
2660
2753
|
}
|
|
2661
2754
|
/**
|
|
2662
2755
|
* Turns the Vault off for this sandbox, for good, in any state: its next boot, resume or push removes
|
|
2663
2756
|
* what the Vault wrote and nothing is written again. Requires machines:write.
|
|
2664
2757
|
*/
|
|
2665
2758
|
async disableVault() {
|
|
2666
|
-
this.machine = await this.http.request("PATCH", `/v1/machines/${
|
|
2759
|
+
this.machine = await this.http.request("PATCH", `/v1/machines/${enc4(this.sandboxId)}`, {
|
|
2667
2760
|
body: { vault: false }
|
|
2668
2761
|
});
|
|
2669
2762
|
}
|
|
@@ -2710,7 +2803,7 @@ var Sandbox = class _Sandbox {
|
|
|
2710
2803
|
}
|
|
2711
2804
|
body.ttlSeconds = opts.ttlSeconds;
|
|
2712
2805
|
}
|
|
2713
|
-
const c = await this.http.request("POST", `/v1/machines/${
|
|
2806
|
+
const c = await this.http.request("POST", `/v1/machines/${enc4(this.sandboxId)}/connections`, { body });
|
|
2714
2807
|
return {
|
|
2715
2808
|
host: c.host,
|
|
2716
2809
|
port: c.port,
|
|
@@ -2729,7 +2822,7 @@ var Sandbox = class _Sandbox {
|
|
|
2729
2822
|
async settle(refresh = true) {
|
|
2730
2823
|
if (refresh) this.machine = await getMachine(this.http, this.sandboxId);
|
|
2731
2824
|
if (this.machine.operationId) {
|
|
2732
|
-
const op = await this.http.request("GET", `/v1/operations/${
|
|
2825
|
+
const op = await this.http.request("GET", `/v1/operations/${enc4(this.machine.operationId)}`);
|
|
2733
2826
|
await this.http.waitForOperation(op).catch(() => {
|
|
2734
2827
|
});
|
|
2735
2828
|
this.machine = await getMachine(this.http, this.sandboxId);
|
|
@@ -2757,29 +2850,29 @@ var Sandbox = class _Sandbox {
|
|
|
2757
2850
|
}
|
|
2758
2851
|
}
|
|
2759
2852
|
async mutate(method, action) {
|
|
2760
|
-
const op = await this.http.request(method, `/v1/machines/${
|
|
2853
|
+
const op = await this.http.request(method, `/v1/machines/${enc4(this.sandboxId)}/${action}`);
|
|
2761
2854
|
await this.http.waitForOperation(op);
|
|
2762
2855
|
this.machine = await getMachine(this.http, this.sandboxId);
|
|
2763
2856
|
}
|
|
2764
2857
|
};
|
|
2765
2858
|
async function getMachine(http, id) {
|
|
2766
|
-
return http.request("GET", `/v1/machines/${
|
|
2859
|
+
return http.request("GET", `/v1/machines/${enc4(id)}`);
|
|
2767
2860
|
}
|
|
2768
2861
|
async function getMetrics(http, id, opts) {
|
|
2769
|
-
const out = await http.request("GET", `/v1/machines/${
|
|
2862
|
+
const out = await http.request("GET", `/v1/machines/${enc4(id)}/metrics`, {
|
|
2770
2863
|
query: { since: opts.since?.toISOString() }
|
|
2771
2864
|
});
|
|
2772
2865
|
return out.metrics.map((s) => ({ ...s, timestamp: new Date(s.timestamp) }));
|
|
2773
2866
|
}
|
|
2774
2867
|
async function getUsage(http, id, opts) {
|
|
2775
|
-
const u = await http.request("GET", `/v1/machines/${
|
|
2868
|
+
const u = await http.request("GET", `/v1/machines/${enc4(id)}/usage`, {
|
|
2776
2869
|
query: { from: opts.from?.toISOString(), to: opts.to?.toISOString() }
|
|
2777
2870
|
});
|
|
2778
2871
|
return { ...u, from: new Date(u.from), to: new Date(u.to) };
|
|
2779
2872
|
}
|
|
2780
2873
|
async function killMachine(http, id) {
|
|
2781
2874
|
try {
|
|
2782
|
-
const op = await http.request("DELETE", `/v1/machines/${
|
|
2875
|
+
const op = await http.request("DELETE", `/v1/machines/${enc4(id)}`, {
|
|
2783
2876
|
headers: { "X-Confirm-Delete": id }
|
|
2784
2877
|
});
|
|
2785
2878
|
await http.waitForOperation(op);
|
|
@@ -2843,6 +2936,7 @@ export {
|
|
|
2843
2936
|
MAX_FILE_BYTES,
|
|
2844
2937
|
MAX_STDIN_BYTES,
|
|
2845
2938
|
MAX_UPLOAD_BYTES,
|
|
2939
|
+
Ports,
|
|
2846
2940
|
Sandbox,
|
|
2847
2941
|
isLimitCode
|
|
2848
2942
|
};
|